xref: /linux/kernel/uid16.c (revision 86a264ab)
1 /*
2  *	Wrapper functions for 16bit uid back compatibility. All nicely tied
3  *	together in the faint hope we can take the out in five years time.
4  */
5 
6 #include <linux/mm.h>
7 #include <linux/utsname.h>
8 #include <linux/mman.h>
9 #include <linux/notifier.h>
10 #include <linux/reboot.h>
11 #include <linux/prctl.h>
12 #include <linux/capability.h>
13 #include <linux/init.h>
14 #include <linux/highuid.h>
15 #include <linux/security.h>
16 #include <linux/syscalls.h>
17 
18 #include <asm/uaccess.h>
19 
20 asmlinkage long sys_chown16(const char __user * filename, old_uid_t user, old_gid_t group)
21 {
22 	long ret = sys_chown(filename, low2highuid(user), low2highgid(group));
23 	/* avoid REGPARM breakage on x86: */
24 	asmlinkage_protect(3, ret, filename, user, group);
25 	return ret;
26 }
27 
28 asmlinkage long sys_lchown16(const char __user * filename, old_uid_t user, old_gid_t group)
29 {
30 	long ret = sys_lchown(filename, low2highuid(user), low2highgid(group));
31 	/* avoid REGPARM breakage on x86: */
32 	asmlinkage_protect(3, ret, filename, user, group);
33 	return ret;
34 }
35 
36 asmlinkage long sys_fchown16(unsigned int fd, old_uid_t user, old_gid_t group)
37 {
38 	long ret = sys_fchown(fd, low2highuid(user), low2highgid(group));
39 	/* avoid REGPARM breakage on x86: */
40 	asmlinkage_protect(3, ret, fd, user, group);
41 	return ret;
42 }
43 
44 asmlinkage long sys_setregid16(old_gid_t rgid, old_gid_t egid)
45 {
46 	long ret = sys_setregid(low2highgid(rgid), low2highgid(egid));
47 	/* avoid REGPARM breakage on x86: */
48 	asmlinkage_protect(2, ret, rgid, egid);
49 	return ret;
50 }
51 
52 asmlinkage long sys_setgid16(old_gid_t gid)
53 {
54 	long ret = sys_setgid(low2highgid(gid));
55 	/* avoid REGPARM breakage on x86: */
56 	asmlinkage_protect(1, ret, gid);
57 	return ret;
58 }
59 
60 asmlinkage long sys_setreuid16(old_uid_t ruid, old_uid_t euid)
61 {
62 	long ret = sys_setreuid(low2highuid(ruid), low2highuid(euid));
63 	/* avoid REGPARM breakage on x86: */
64 	asmlinkage_protect(2, ret, ruid, euid);
65 	return ret;
66 }
67 
68 asmlinkage long sys_setuid16(old_uid_t uid)
69 {
70 	long ret = sys_setuid(low2highuid(uid));
71 	/* avoid REGPARM breakage on x86: */
72 	asmlinkage_protect(1, ret, uid);
73 	return ret;
74 }
75 
76 asmlinkage long sys_setresuid16(old_uid_t ruid, old_uid_t euid, old_uid_t suid)
77 {
78 	long ret = sys_setresuid(low2highuid(ruid), low2highuid(euid),
79 				 low2highuid(suid));
80 	/* avoid REGPARM breakage on x86: */
81 	asmlinkage_protect(3, ret, ruid, euid, suid);
82 	return ret;
83 }
84 
85 asmlinkage long sys_getresuid16(old_uid_t __user *ruid, old_uid_t __user *euid, old_uid_t __user *suid)
86 {
87 	const struct cred *cred = current_cred();
88 	int retval;
89 
90 	if (!(retval   = put_user(high2lowuid(cred->uid),  ruid)) &&
91 	    !(retval   = put_user(high2lowuid(cred->euid), euid)))
92 		retval = put_user(high2lowuid(cred->suid), suid);
93 
94 	return retval;
95 }
96 
97 asmlinkage long sys_setresgid16(old_gid_t rgid, old_gid_t egid, old_gid_t sgid)
98 {
99 	long ret = sys_setresgid(low2highgid(rgid), low2highgid(egid),
100 				 low2highgid(sgid));
101 	/* avoid REGPARM breakage on x86: */
102 	asmlinkage_protect(3, ret, rgid, egid, sgid);
103 	return ret;
104 }
105 
106 asmlinkage long sys_getresgid16(old_gid_t __user *rgid, old_gid_t __user *egid, old_gid_t __user *sgid)
107 {
108 	const struct cred *cred = current_cred();
109 	int retval;
110 
111 	if (!(retval   = put_user(high2lowgid(cred->gid),  rgid)) &&
112 	    !(retval   = put_user(high2lowgid(cred->egid), egid)))
113 		retval = put_user(high2lowgid(cred->sgid), sgid);
114 
115 	return retval;
116 }
117 
118 asmlinkage long sys_setfsuid16(old_uid_t uid)
119 {
120 	long ret = sys_setfsuid(low2highuid(uid));
121 	/* avoid REGPARM breakage on x86: */
122 	asmlinkage_protect(1, ret, uid);
123 	return ret;
124 }
125 
126 asmlinkage long sys_setfsgid16(old_gid_t gid)
127 {
128 	long ret = sys_setfsgid(low2highgid(gid));
129 	/* avoid REGPARM breakage on x86: */
130 	asmlinkage_protect(1, ret, gid);
131 	return ret;
132 }
133 
134 static int groups16_to_user(old_gid_t __user *grouplist,
135     struct group_info *group_info)
136 {
137 	int i;
138 	old_gid_t group;
139 
140 	for (i = 0; i < group_info->ngroups; i++) {
141 		group = high2lowgid(GROUP_AT(group_info, i));
142 		if (put_user(group, grouplist+i))
143 			return -EFAULT;
144 	}
145 
146 	return 0;
147 }
148 
149 static int groups16_from_user(struct group_info *group_info,
150     old_gid_t __user *grouplist)
151 {
152 	int i;
153 	old_gid_t group;
154 
155 	for (i = 0; i < group_info->ngroups; i++) {
156 		if (get_user(group, grouplist+i))
157 			return  -EFAULT;
158 		GROUP_AT(group_info, i) = low2highgid(group);
159 	}
160 
161 	return 0;
162 }
163 
164 asmlinkage long sys_getgroups16(int gidsetsize, old_gid_t __user *grouplist)
165 {
166 	const struct cred *cred = current_cred();
167 	int i;
168 
169 	if (gidsetsize < 0)
170 		return -EINVAL;
171 
172 	i = cred->group_info->ngroups;
173 	if (gidsetsize) {
174 		if (i > gidsetsize) {
175 			i = -EINVAL;
176 			goto out;
177 		}
178 		if (groups16_to_user(grouplist, cred->group_info)) {
179 			i = -EFAULT;
180 			goto out;
181 		}
182 	}
183 out:
184 	return i;
185 }
186 
187 asmlinkage long sys_setgroups16(int gidsetsize, old_gid_t __user *grouplist)
188 {
189 	struct group_info *group_info;
190 	int retval;
191 
192 	if (!capable(CAP_SETGID))
193 		return -EPERM;
194 	if ((unsigned)gidsetsize > NGROUPS_MAX)
195 		return -EINVAL;
196 
197 	group_info = groups_alloc(gidsetsize);
198 	if (!group_info)
199 		return -ENOMEM;
200 	retval = groups16_from_user(group_info, grouplist);
201 	if (retval) {
202 		put_group_info(group_info);
203 		return retval;
204 	}
205 
206 	retval = set_current_groups(group_info);
207 	put_group_info(group_info);
208 
209 	return retval;
210 }
211 
212 asmlinkage long sys_getuid16(void)
213 {
214 	return high2lowuid(current_uid());
215 }
216 
217 asmlinkage long sys_geteuid16(void)
218 {
219 	return high2lowuid(current_euid());
220 }
221 
222 asmlinkage long sys_getgid16(void)
223 {
224 	return high2lowgid(current_gid());
225 }
226 
227 asmlinkage long sys_getegid16(void)
228 {
229 	return high2lowgid(current_egid());
230 }
231