blob: e530bc34c4cffc0d3f9ede9890c61716b1f22f31 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/notifier.h>
9#include <linux/reboot.h>
10#include <linux/prctl.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080011#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/init.h>
13#include <linux/highuid.h>
14#include <linux/security.h>
15#include <linux/syscalls.h>
16
17#include <asm/uaccess.h>
18
Heiko Carstensca013e92009-01-14 14:14:19 +010019SYSCALL_DEFINE3(chown16, const char __user *, filename, old_uid_t, user, old_gid_t, group)
Linus Torvalds1da177e2005-04-16 15:20:36 -070020{
OGAWA Hirofumi5a7b46b2006-04-20 06:41:39 +090021 long ret = sys_chown(filename, low2highuid(user), low2highgid(group));
22 /* avoid REGPARM breakage on x86: */
Roland McGrath54a01512008-04-10 15:37:38 -070023 asmlinkage_protect(3, ret, filename, user, group);
OGAWA Hirofumi5a7b46b2006-04-20 06:41:39 +090024 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025}
26
Heiko Carstensca013e92009-01-14 14:14:19 +010027SYSCALL_DEFINE3(lchown16, const char __user *, filename, old_uid_t, user, old_gid_t, group)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
OGAWA Hirofumi5a7b46b2006-04-20 06:41:39 +090029 long ret = sys_lchown(filename, low2highuid(user), low2highgid(group));
30 /* avoid REGPARM breakage on x86: */
Roland McGrath54a01512008-04-10 15:37:38 -070031 asmlinkage_protect(3, ret, filename, user, group);
OGAWA Hirofumi5a7b46b2006-04-20 06:41:39 +090032 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033}
34
Heiko Carstensca013e92009-01-14 14:14:19 +010035SYSCALL_DEFINE3(fchown16, unsigned int, fd, old_uid_t, user, old_gid_t, group)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
OGAWA Hirofumi5a7b46b2006-04-20 06:41:39 +090037 long ret = sys_fchown(fd, low2highuid(user), low2highgid(group));
38 /* avoid REGPARM breakage on x86: */
Roland McGrath54a01512008-04-10 15:37:38 -070039 asmlinkage_protect(3, ret, fd, user, group);
OGAWA Hirofumi5a7b46b2006-04-20 06:41:39 +090040 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041}
42
Heiko Carstensa6b42e82009-01-14 14:14:20 +010043SYSCALL_DEFINE2(setregid16, old_gid_t, rgid, old_gid_t, egid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
OGAWA Hirofumi5a7b46b2006-04-20 06:41:39 +090045 long ret = sys_setregid(low2highgid(rgid), low2highgid(egid));
46 /* avoid REGPARM breakage on x86: */
Roland McGrath54a01512008-04-10 15:37:38 -070047 asmlinkage_protect(2, ret, rgid, egid);
OGAWA Hirofumi5a7b46b2006-04-20 06:41:39 +090048 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
Heiko Carstensa6b42e82009-01-14 14:14:20 +010051SYSCALL_DEFINE1(setgid16, old_gid_t, gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
OGAWA Hirofumi5a7b46b2006-04-20 06:41:39 +090053 long ret = sys_setgid(low2highgid(gid));
54 /* avoid REGPARM breakage on x86: */
Roland McGrath54a01512008-04-10 15:37:38 -070055 asmlinkage_protect(1, ret, gid);
OGAWA Hirofumi5a7b46b2006-04-20 06:41:39 +090056 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
Heiko Carstensa6b42e82009-01-14 14:14:20 +010059SYSCALL_DEFINE2(setreuid16, old_uid_t, ruid, old_uid_t, euid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
OGAWA Hirofumi5a7b46b2006-04-20 06:41:39 +090061 long ret = sys_setreuid(low2highuid(ruid), low2highuid(euid));
62 /* avoid REGPARM breakage on x86: */
Roland McGrath54a01512008-04-10 15:37:38 -070063 asmlinkage_protect(2, ret, ruid, euid);
OGAWA Hirofumi5a7b46b2006-04-20 06:41:39 +090064 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
Heiko Carstensa6b42e82009-01-14 14:14:20 +010067SYSCALL_DEFINE1(setuid16, old_uid_t, uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
OGAWA Hirofumi5a7b46b2006-04-20 06:41:39 +090069 long ret = sys_setuid(low2highuid(uid));
70 /* avoid REGPARM breakage on x86: */
Roland McGrath54a01512008-04-10 15:37:38 -070071 asmlinkage_protect(1, ret, uid);
OGAWA Hirofumi5a7b46b2006-04-20 06:41:39 +090072 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
Heiko Carstensa6b42e82009-01-14 14:14:20 +010075SYSCALL_DEFINE3(setresuid16, old_uid_t, ruid, old_uid_t, euid, old_uid_t, suid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
OGAWA Hirofumi5a7b46b2006-04-20 06:41:39 +090077 long ret = sys_setresuid(low2highuid(ruid), low2highuid(euid),
78 low2highuid(suid));
79 /* avoid REGPARM breakage on x86: */
Roland McGrath54a01512008-04-10 15:37:38 -070080 asmlinkage_protect(3, ret, ruid, euid, suid);
OGAWA Hirofumi5a7b46b2006-04-20 06:41:39 +090081 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
Heiko Carstensa6b42e82009-01-14 14:14:20 +010084SYSCALL_DEFINE3(getresuid16, old_uid_t __user *, ruid, old_uid_t __user *, euid, old_uid_t __user *, suid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
David Howells86a264a2008-11-14 10:39:18 +110086 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 int retval;
88
David Howells86a264a2008-11-14 10:39:18 +110089 if (!(retval = put_user(high2lowuid(cred->uid), ruid)) &&
90 !(retval = put_user(high2lowuid(cred->euid), euid)))
91 retval = put_user(high2lowuid(cred->suid), suid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 return retval;
94}
95
Heiko Carstensa6b42e82009-01-14 14:14:20 +010096SYSCALL_DEFINE3(setresgid16, old_gid_t, rgid, old_gid_t, egid, old_gid_t, sgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
OGAWA Hirofumi5a7b46b2006-04-20 06:41:39 +090098 long ret = sys_setresgid(low2highgid(rgid), low2highgid(egid),
99 low2highgid(sgid));
100 /* avoid REGPARM breakage on x86: */
Roland McGrath54a01512008-04-10 15:37:38 -0700101 asmlinkage_protect(3, ret, rgid, egid, sgid);
OGAWA Hirofumi5a7b46b2006-04-20 06:41:39 +0900102 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Heiko Carstensa6b42e82009-01-14 14:14:20 +0100105
106SYSCALL_DEFINE3(getresgid16, old_gid_t __user *, rgid, old_gid_t __user *, egid, old_gid_t __user *, sgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
David Howells86a264a2008-11-14 10:39:18 +1100108 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 int retval;
110
David Howells86a264a2008-11-14 10:39:18 +1100111 if (!(retval = put_user(high2lowgid(cred->gid), rgid)) &&
112 !(retval = put_user(high2lowgid(cred->egid), egid)))
113 retval = put_user(high2lowgid(cred->sgid), sgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 return retval;
116}
117
Heiko Carstensa6b42e82009-01-14 14:14:20 +0100118SYSCALL_DEFINE1(setfsuid16, old_uid_t, uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
OGAWA Hirofumi5a7b46b2006-04-20 06:41:39 +0900120 long ret = sys_setfsuid(low2highuid(uid));
121 /* avoid REGPARM breakage on x86: */
Roland McGrath54a01512008-04-10 15:37:38 -0700122 asmlinkage_protect(1, ret, uid);
OGAWA Hirofumi5a7b46b2006-04-20 06:41:39 +0900123 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
Heiko Carstensa6b42e82009-01-14 14:14:20 +0100126SYSCALL_DEFINE1(setfsgid16, old_gid_t, gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
OGAWA Hirofumi5a7b46b2006-04-20 06:41:39 +0900128 long ret = sys_setfsgid(low2highgid(gid));
129 /* avoid REGPARM breakage on x86: */
Roland McGrath54a01512008-04-10 15:37:38 -0700130 asmlinkage_protect(1, ret, gid);
OGAWA Hirofumi5a7b46b2006-04-20 06:41:39 +0900131 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134static int groups16_to_user(old_gid_t __user *grouplist,
135 struct group_info *group_info)
136{
Eric W. Biedermanae2975b2011-11-14 15:56:38 -0800137 struct user_namespace *user_ns = current_user_ns();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 int i;
139 old_gid_t group;
Eric W. Biedermanae2975b2011-11-14 15:56:38 -0800140 kgid_t kgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 for (i = 0; i < group_info->ngroups; i++) {
Eric W. Biedermanae2975b2011-11-14 15:56:38 -0800143 kgid = GROUP_AT(group_info, i);
144 group = high2lowgid(from_kgid_munged(user_ns, kgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (put_user(group, grouplist+i))
146 return -EFAULT;
147 }
148
149 return 0;
150}
151
152static int groups16_from_user(struct group_info *group_info,
153 old_gid_t __user *grouplist)
154{
Eric W. Biedermanae2975b2011-11-14 15:56:38 -0800155 struct user_namespace *user_ns = current_user_ns();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 int i;
157 old_gid_t group;
Eric W. Biedermanae2975b2011-11-14 15:56:38 -0800158 kgid_t kgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 for (i = 0; i < group_info->ngroups; i++) {
161 if (get_user(group, grouplist+i))
162 return -EFAULT;
Eric W. Biedermanae2975b2011-11-14 15:56:38 -0800163
164 kgid = make_kgid(user_ns, low2highgid(group));
165 if (!gid_valid(kgid))
166 return -EINVAL;
167
168 GROUP_AT(group_info, i) = kgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
170
171 return 0;
172}
173
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100174SYSCALL_DEFINE2(getgroups16, int, gidsetsize, old_gid_t __user *, grouplist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
David Howells86a264a2008-11-14 10:39:18 +1100176 const struct cred *cred = current_cred();
177 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 if (gidsetsize < 0)
180 return -EINVAL;
181
David Howells86a264a2008-11-14 10:39:18 +1100182 i = cred->group_info->ngroups;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (gidsetsize) {
184 if (i > gidsetsize) {
185 i = -EINVAL;
186 goto out;
187 }
David Howells86a264a2008-11-14 10:39:18 +1100188 if (groups16_to_user(grouplist, cred->group_info)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 i = -EFAULT;
190 goto out;
191 }
192 }
193out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return i;
195}
196
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100197SYSCALL_DEFINE2(setgroups16, int, gidsetsize, old_gid_t __user *, grouplist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 struct group_info *group_info;
200 int retval;
201
Serge E. Hallynb0e77592011-03-23 16:43:24 -0700202 if (!nsown_capable(CAP_SETGID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return -EPERM;
204 if ((unsigned)gidsetsize > NGROUPS_MAX)
205 return -EINVAL;
206
207 group_info = groups_alloc(gidsetsize);
208 if (!group_info)
209 return -ENOMEM;
210 retval = groups16_from_user(group_info, grouplist);
211 if (retval) {
212 put_group_info(group_info);
213 return retval;
214 }
215
216 retval = set_current_groups(group_info);
217 put_group_info(group_info);
218
219 return retval;
220}
221
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100222SYSCALL_DEFINE0(getuid16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
David Howells86a264a2008-11-14 10:39:18 +1100224 return high2lowuid(current_uid());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100227SYSCALL_DEFINE0(geteuid16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
David Howells86a264a2008-11-14 10:39:18 +1100229 return high2lowuid(current_euid());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100232SYSCALL_DEFINE0(getgid16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
David Howells86a264a2008-11-14 10:39:18 +1100234 return high2lowgid(current_gid());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100237SYSCALL_DEFINE0(getegid16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
David Howells86a264a2008-11-14 10:39:18 +1100239 return high2lowgid(current_egid());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}