1/*
2 * Copyright (c) Red Hat Inc., 2008
3 * Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved.
4 *
5 * This program is free software;  you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY;  without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program;  if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20/* Author: Masatake YAMATO <yamato@redhat.com> */
21
22#ifndef __LTP_COMPAT_16_H__
23#define __LTP_COMPAT_16_H__
24
25#include <errno.h>
26#include <grp.h>
27#if defined(__GLIBC__) || defined(__ANDROID__)
28#include <sys/fsuid.h>
29#endif
30#include <sys/types.h>
31#include <unistd.h>
32
33#include "compat_gid.h"
34#include "compat_uid.h"
35#include "linux_syscall_numbers.h"
36
37int setresuid(uid_t ruid, uid_t euid, uid_t suid);
38int setresgid(gid_t rgid, gid_t egid, gid_t sgid);
39
40
41/* If the platform has __NR_sys_name32 defined it
42 * means that __NR_sys_name is a 16-bit version of
43 * sys_name() syscall
44 */
45#ifdef TST_USE_COMPAT16_SYSCALL
46# define LTP_CREATE_SYSCALL(sys_name, cleanup, ...) \
47	if (__NR_##sys_name##32 != __LTP__NR_INVALID_SYSCALL) { \
48		return ltp_syscall(__NR_##sys_name, ##__VA_ARGS__); \
49	} else { \
50		tst_brkm(TCONF, cleanup, \
51			"16-bit version of %s() is not supported on your " \
52			"platform", #sys_name); \
53	}
54#else
55# define LTP_CREATE_SYSCALL(sys_name, cleanup, ...) \
56	(void) cleanup;                             \
57	return sys_name(__VA_ARGS__)
58#endif
59
60#define UID16_CHECK(uid, sys_name, cleanup) \
61if (!UID_SIZE_CHECK(uid)) { \
62	tst_brkm(TBROK, cleanup, \
63		"uid %d of %s is too large for testing 16-bit version " \
64		"of %s()", uid, #uid, #sys_name); \
65}
66
67#define GID16_CHECK(gid, sys_name, cleanup) \
68if (!GID_SIZE_CHECK(gid)) { \
69	tst_brkm(TBROK, cleanup, \
70		"gid %d of %s is too large for testing 16-bit version " \
71		"of %s()", gid, #gid, #sys_name); \
72}
73
74
75int SETGROUPS(void (cleanup)(void), size_t gidsetsize, GID_T *list)
76{
77	LTP_CREATE_SYSCALL(setgroups, cleanup, gidsetsize, list);
78}
79
80int GETGROUPS(void (cleanup)(void), size_t gidsetsize, GID_T *list)
81{
82	LTP_CREATE_SYSCALL(getgroups, cleanup, gidsetsize, list);
83}
84
85int SETUID(void (cleanup)(void), UID_T uid)
86{
87	LTP_CREATE_SYSCALL(setuid, cleanup, uid);
88}
89
90UID_T GETUID(void (cleanup)(void))
91{
92	LTP_CREATE_SYSCALL(getuid, cleanup);
93}
94
95int SETGID(void (cleanup)(void), GID_T gid)
96{
97	LTP_CREATE_SYSCALL(setgid, cleanup, gid);
98}
99
100GID_T GETGID(void (cleanup)(void))
101{
102	LTP_CREATE_SYSCALL(getgid, cleanup);
103}
104
105UID_T GETEUID(void (cleanup)(void))
106{
107	LTP_CREATE_SYSCALL(geteuid, cleanup);
108}
109
110GID_T GETEGID(void (cleanup)(void))
111{
112	LTP_CREATE_SYSCALL(getegid, cleanup);
113}
114
115int SETFSUID(void (cleanup)(void), UID_T uid)
116{
117	LTP_CREATE_SYSCALL(setfsuid, cleanup, uid);
118}
119
120int SETFSGID(void (cleanup)(void), GID_T gid)
121{
122	LTP_CREATE_SYSCALL(setfsgid, cleanup, gid);
123}
124
125int SETREUID(void (cleanup)(void), UID_T ruid, UID_T euid)
126{
127	LTP_CREATE_SYSCALL(setreuid, cleanup, ruid, euid);
128}
129int SETREGID(void (cleanup)(void), GID_T rgid, GID_T egid)
130{
131	LTP_CREATE_SYSCALL(setregid, cleanup, rgid, egid);
132}
133
134int SETRESUID(void (cleanup)(void), UID_T ruid, UID_T euid, UID_T suid)
135{
136	LTP_CREATE_SYSCALL(setresuid, cleanup, ruid, euid, suid);
137}
138
139int SETRESGID(void (cleanup)(void), GID_T rgid, GID_T egid, GID_T sgid)
140{
141	LTP_CREATE_SYSCALL(setresgid, cleanup, rgid, egid, sgid);
142}
143
144int FCHOWN(void (cleanup)(void), unsigned int fd, UID_T owner, GID_T group)
145{
146	LTP_CREATE_SYSCALL(fchown, cleanup, fd, owner, group);
147}
148
149int LCHOWN(void (cleanup)(void), const char *path, UID_T owner, GID_T group)
150{
151	LTP_CREATE_SYSCALL(lchown, cleanup, path, owner, group);
152}
153
154int CHOWN(void (cleanup)(void), const char *path, UID_T owner, GID_T group)
155{
156	LTP_CREATE_SYSCALL(chown, cleanup, path, owner, group);
157}
158#endif /* __LTP_COMPAT_16_H__ */
159