blob: 5c2dc5b2bf4fe8ecd05e2423d1a820e25b16d1ec [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>
Ingo Molnar5b825c32017-02-02 17:54:15 +010015#include <linux/cred.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/syscalls.h>
17
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080018#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Heiko Carstensca013e92009-01-14 14:14:19 +010020SYSCALL_DEFINE3(chown16, const char __user *, filename, old_uid_t, user, old_gid_t, group)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{
Al Viro2cf09662013-01-21 15:25:54 -050022 return sys_chown(filename, low2highuid(user), low2highgid(group));
Linus Torvalds1da177e2005-04-16 15:20:36 -070023}
24
Heiko Carstensca013e92009-01-14 14:14:19 +010025SYSCALL_DEFINE3(lchown16, const char __user *, filename, old_uid_t, user, old_gid_t, group)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
Al Viro2cf09662013-01-21 15:25:54 -050027 return sys_lchown(filename, low2highuid(user), low2highgid(group));
Linus Torvalds1da177e2005-04-16 15:20:36 -070028}
29
Heiko Carstensca013e92009-01-14 14:14:19 +010030SYSCALL_DEFINE3(fchown16, unsigned int, fd, old_uid_t, user, old_gid_t, group)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Al Viro2cf09662013-01-21 15:25:54 -050032 return sys_fchown(fd, low2highuid(user), low2highgid(group));
Linus Torvalds1da177e2005-04-16 15:20:36 -070033}
34
Heiko Carstensa6b42e82009-01-14 14:14:20 +010035SYSCALL_DEFINE2(setregid16, old_gid_t, rgid, old_gid_t, egid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
Al Viro2cf09662013-01-21 15:25:54 -050037 return sys_setregid(low2highgid(rgid), low2highgid(egid));
Linus Torvalds1da177e2005-04-16 15:20:36 -070038}
39
Heiko Carstensa6b42e82009-01-14 14:14:20 +010040SYSCALL_DEFINE1(setgid16, old_gid_t, gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Al Viro2cf09662013-01-21 15:25:54 -050042 return sys_setgid(low2highgid(gid));
Linus Torvalds1da177e2005-04-16 15:20:36 -070043}
44
Heiko Carstensa6b42e82009-01-14 14:14:20 +010045SYSCALL_DEFINE2(setreuid16, old_uid_t, ruid, old_uid_t, euid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
Al Viro2cf09662013-01-21 15:25:54 -050047 return sys_setreuid(low2highuid(ruid), low2highuid(euid));
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
Heiko Carstensa6b42e82009-01-14 14:14:20 +010050SYSCALL_DEFINE1(setuid16, old_uid_t, uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Al Viro2cf09662013-01-21 15:25:54 -050052 return sys_setuid(low2highuid(uid));
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
Heiko Carstensa6b42e82009-01-14 14:14:20 +010055SYSCALL_DEFINE3(setresuid16, old_uid_t, ruid, old_uid_t, euid, old_uid_t, suid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Al Viro2cf09662013-01-21 15:25:54 -050057 return sys_setresuid(low2highuid(ruid), low2highuid(euid),
OGAWA Hirofumi5a7b46b2006-04-20 06:41:39 +090058 low2highuid(suid));
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
Eric W. Biedermana29c33f2012-02-07 18:51:01 -080061SYSCALL_DEFINE3(getresuid16, old_uid_t __user *, ruidp, old_uid_t __user *, euidp, old_uid_t __user *, suidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
David Howells86a264a2008-11-14 10:39:18 +110063 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -080065 old_uid_t ruid, euid, suid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Eric W. Biedermana29c33f2012-02-07 18:51:01 -080067 ruid = high2lowuid(from_kuid_munged(cred->user_ns, cred->uid));
68 euid = high2lowuid(from_kuid_munged(cred->user_ns, cred->euid));
69 suid = high2lowuid(from_kuid_munged(cred->user_ns, cred->suid));
70
71 if (!(retval = put_user(ruid, ruidp)) &&
72 !(retval = put_user(euid, euidp)))
73 retval = put_user(suid, suidp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 return retval;
76}
77
Heiko Carstensa6b42e82009-01-14 14:14:20 +010078SYSCALL_DEFINE3(setresgid16, old_gid_t, rgid, old_gid_t, egid, old_gid_t, sgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Al Viro2cf09662013-01-21 15:25:54 -050080 return sys_setresgid(low2highgid(rgid), low2highgid(egid),
OGAWA Hirofumi5a7b46b2006-04-20 06:41:39 +090081 low2highgid(sgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
Heiko Carstensa6b42e82009-01-14 14:14:20 +010084
Eric W. Biedermana29c33f2012-02-07 18:51:01 -080085SYSCALL_DEFINE3(getresgid16, old_gid_t __user *, rgidp, old_gid_t __user *, egidp, old_gid_t __user *, sgidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
David Howells86a264a2008-11-14 10:39:18 +110087 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -080089 old_gid_t rgid, egid, sgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Eric W. Biedermana29c33f2012-02-07 18:51:01 -080091 rgid = high2lowgid(from_kgid_munged(cred->user_ns, cred->gid));
92 egid = high2lowgid(from_kgid_munged(cred->user_ns, cred->egid));
93 sgid = high2lowgid(from_kgid_munged(cred->user_ns, cred->sgid));
94
95 if (!(retval = put_user(rgid, rgidp)) &&
96 !(retval = put_user(egid, egidp)))
97 retval = put_user(sgid, sgidp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 return retval;
100}
101
Heiko Carstensa6b42e82009-01-14 14:14:20 +0100102SYSCALL_DEFINE1(setfsuid16, old_uid_t, uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Al Viro2cf09662013-01-21 15:25:54 -0500104 return sys_setfsuid(low2highuid(uid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
Heiko Carstensa6b42e82009-01-14 14:14:20 +0100107SYSCALL_DEFINE1(setfsgid16, old_gid_t, gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Al Viro2cf09662013-01-21 15:25:54 -0500109 return sys_setfsgid(low2highgid(gid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
112static int groups16_to_user(old_gid_t __user *grouplist,
113 struct group_info *group_info)
114{
Eric W. Biedermanae2975b2011-11-14 15:56:38 -0800115 struct user_namespace *user_ns = current_user_ns();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 int i;
117 old_gid_t group;
Eric W. Biedermanae2975b2011-11-14 15:56:38 -0800118 kgid_t kgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 for (i = 0; i < group_info->ngroups; i++) {
Alexey Dobriyan81243ea2016-10-07 17:03:12 -0700121 kgid = group_info->gid[i];
Eric W. Biedermanae2975b2011-11-14 15:56:38 -0800122 group = high2lowgid(from_kgid_munged(user_ns, kgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (put_user(group, grouplist+i))
124 return -EFAULT;
125 }
126
127 return 0;
128}
129
130static int groups16_from_user(struct group_info *group_info,
131 old_gid_t __user *grouplist)
132{
Eric W. Biedermanae2975b2011-11-14 15:56:38 -0800133 struct user_namespace *user_ns = current_user_ns();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 int i;
135 old_gid_t group;
Eric W. Biedermanae2975b2011-11-14 15:56:38 -0800136 kgid_t kgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 for (i = 0; i < group_info->ngroups; i++) {
139 if (get_user(group, grouplist+i))
140 return -EFAULT;
Eric W. Biedermanae2975b2011-11-14 15:56:38 -0800141
142 kgid = make_kgid(user_ns, low2highgid(group));
143 if (!gid_valid(kgid))
144 return -EINVAL;
145
Alexey Dobriyan81243ea2016-10-07 17:03:12 -0700146 group_info->gid[i] = kgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
148
149 return 0;
150}
151
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100152SYSCALL_DEFINE2(getgroups16, int, gidsetsize, old_gid_t __user *, grouplist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
David Howells86a264a2008-11-14 10:39:18 +1100154 const struct cred *cred = current_cred();
155 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 if (gidsetsize < 0)
158 return -EINVAL;
159
David Howells86a264a2008-11-14 10:39:18 +1100160 i = cred->group_info->ngroups;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (gidsetsize) {
162 if (i > gidsetsize) {
163 i = -EINVAL;
164 goto out;
165 }
David Howells86a264a2008-11-14 10:39:18 +1100166 if (groups16_to_user(grouplist, cred->group_info)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 i = -EFAULT;
168 goto out;
169 }
170 }
171out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return i;
173}
174
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100175SYSCALL_DEFINE2(setgroups16, int, gidsetsize, old_gid_t __user *, grouplist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 struct group_info *group_info;
178 int retval;
179
Eric W. Biederman7ff4d902014-12-05 17:19:27 -0600180 if (!may_setgroups())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return -EPERM;
182 if ((unsigned)gidsetsize > NGROUPS_MAX)
183 return -EINVAL;
184
185 group_info = groups_alloc(gidsetsize);
186 if (!group_info)
187 return -ENOMEM;
188 retval = groups16_from_user(group_info, grouplist);
189 if (retval) {
190 put_group_info(group_info);
191 return retval;
192 }
193
194 retval = set_current_groups(group_info);
195 put_group_info(group_info);
196
197 return retval;
198}
199
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100200SYSCALL_DEFINE0(getuid16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800202 return high2lowuid(from_kuid_munged(current_user_ns(), current_uid()));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100205SYSCALL_DEFINE0(geteuid16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800207 return high2lowuid(from_kuid_munged(current_user_ns(), current_euid()));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100210SYSCALL_DEFINE0(getgid16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800212 return high2lowgid(from_kgid_munged(current_user_ns(), current_gid()));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100215SYSCALL_DEFINE0(getegid16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800217 return high2lowgid(from_kgid_munged(current_user_ns(), current_egid()));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}