blob: dcffcce9d75e91ee5cd45095d585ff714886c253 [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{
Al Viro2cf09662013-01-21 15:25:54 -050021 return sys_chown(filename, low2highuid(user), low2highgid(group));
Linus Torvalds1da177e2005-04-16 15:20:36 -070022}
23
Heiko Carstensca013e92009-01-14 14:14:19 +010024SYSCALL_DEFINE3(lchown16, const char __user *, filename, old_uid_t, user, old_gid_t, group)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
Al Viro2cf09662013-01-21 15:25:54 -050026 return sys_lchown(filename, low2highuid(user), low2highgid(group));
Linus Torvalds1da177e2005-04-16 15:20:36 -070027}
28
Heiko Carstensca013e92009-01-14 14:14:19 +010029SYSCALL_DEFINE3(fchown16, unsigned int, fd, old_uid_t, user, old_gid_t, group)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
Al Viro2cf09662013-01-21 15:25:54 -050031 return sys_fchown(fd, low2highuid(user), low2highgid(group));
Linus Torvalds1da177e2005-04-16 15:20:36 -070032}
33
Heiko Carstensa6b42e82009-01-14 14:14:20 +010034SYSCALL_DEFINE2(setregid16, old_gid_t, rgid, old_gid_t, egid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Al Viro2cf09662013-01-21 15:25:54 -050036 return sys_setregid(low2highgid(rgid), low2highgid(egid));
Linus Torvalds1da177e2005-04-16 15:20:36 -070037}
38
Heiko Carstensa6b42e82009-01-14 14:14:20 +010039SYSCALL_DEFINE1(setgid16, old_gid_t, gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Al Viro2cf09662013-01-21 15:25:54 -050041 return sys_setgid(low2highgid(gid));
Linus Torvalds1da177e2005-04-16 15:20:36 -070042}
43
Heiko Carstensa6b42e82009-01-14 14:14:20 +010044SYSCALL_DEFINE2(setreuid16, old_uid_t, ruid, old_uid_t, euid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Al Viro2cf09662013-01-21 15:25:54 -050046 return sys_setreuid(low2highuid(ruid), low2highuid(euid));
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
Heiko Carstensa6b42e82009-01-14 14:14:20 +010049SYSCALL_DEFINE1(setuid16, old_uid_t, uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Al Viro2cf09662013-01-21 15:25:54 -050051 return sys_setuid(low2highuid(uid));
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
Heiko Carstensa6b42e82009-01-14 14:14:20 +010054SYSCALL_DEFINE3(setresuid16, old_uid_t, ruid, old_uid_t, euid, old_uid_t, suid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Al Viro2cf09662013-01-21 15:25:54 -050056 return sys_setresuid(low2highuid(ruid), low2highuid(euid),
OGAWA Hirofumi5a7b46b2006-04-20 06:41:39 +090057 low2highuid(suid));
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
59
Eric W. Biedermana29c33f2012-02-07 18:51:01 -080060SYSCALL_DEFINE3(getresuid16, old_uid_t __user *, ruidp, old_uid_t __user *, euidp, old_uid_t __user *, suidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
David Howells86a264a2008-11-14 10:39:18 +110062 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -080064 old_uid_t ruid, euid, suid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Eric W. Biedermana29c33f2012-02-07 18:51:01 -080066 ruid = high2lowuid(from_kuid_munged(cred->user_ns, cred->uid));
67 euid = high2lowuid(from_kuid_munged(cred->user_ns, cred->euid));
68 suid = high2lowuid(from_kuid_munged(cred->user_ns, cred->suid));
69
70 if (!(retval = put_user(ruid, ruidp)) &&
71 !(retval = put_user(euid, euidp)))
72 retval = put_user(suid, suidp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 return retval;
75}
76
Heiko Carstensa6b42e82009-01-14 14:14:20 +010077SYSCALL_DEFINE3(setresgid16, old_gid_t, rgid, old_gid_t, egid, old_gid_t, sgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Al Viro2cf09662013-01-21 15:25:54 -050079 return sys_setresgid(low2highgid(rgid), low2highgid(egid),
OGAWA Hirofumi5a7b46b2006-04-20 06:41:39 +090080 low2highgid(sgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
Heiko Carstensa6b42e82009-01-14 14:14:20 +010083
Eric W. Biedermana29c33f2012-02-07 18:51:01 -080084SYSCALL_DEFINE3(getresgid16, old_gid_t __user *, rgidp, old_gid_t __user *, egidp, old_gid_t __user *, sgidp)
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;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -080088 old_gid_t rgid, egid, sgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Eric W. Biedermana29c33f2012-02-07 18:51:01 -080090 rgid = high2lowgid(from_kgid_munged(cred->user_ns, cred->gid));
91 egid = high2lowgid(from_kgid_munged(cred->user_ns, cred->egid));
92 sgid = high2lowgid(from_kgid_munged(cred->user_ns, cred->sgid));
93
94 if (!(retval = put_user(rgid, rgidp)) &&
95 !(retval = put_user(egid, egidp)))
96 retval = put_user(sgid, sgidp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 return retval;
99}
100
Heiko Carstensa6b42e82009-01-14 14:14:20 +0100101SYSCALL_DEFINE1(setfsuid16, old_uid_t, uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Al Viro2cf09662013-01-21 15:25:54 -0500103 return sys_setfsuid(low2highuid(uid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Heiko Carstensa6b42e82009-01-14 14:14:20 +0100106SYSCALL_DEFINE1(setfsgid16, old_gid_t, gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Al Viro2cf09662013-01-21 15:25:54 -0500108 return sys_setfsgid(low2highgid(gid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
111static int groups16_to_user(old_gid_t __user *grouplist,
112 struct group_info *group_info)
113{
Eric W. Biedermanae2975b2011-11-14 15:56:38 -0800114 struct user_namespace *user_ns = current_user_ns();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 int i;
116 old_gid_t group;
Eric W. Biedermanae2975b2011-11-14 15:56:38 -0800117 kgid_t kgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 for (i = 0; i < group_info->ngroups; i++) {
Alexey Dobriyan81243ea2016-10-07 17:03:12 -0700120 kgid = group_info->gid[i];
Eric W. Biedermanae2975b2011-11-14 15:56:38 -0800121 group = high2lowgid(from_kgid_munged(user_ns, kgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if (put_user(group, grouplist+i))
123 return -EFAULT;
124 }
125
126 return 0;
127}
128
129static int groups16_from_user(struct group_info *group_info,
130 old_gid_t __user *grouplist)
131{
Eric W. Biedermanae2975b2011-11-14 15:56:38 -0800132 struct user_namespace *user_ns = current_user_ns();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 int i;
134 old_gid_t group;
Eric W. Biedermanae2975b2011-11-14 15:56:38 -0800135 kgid_t kgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 for (i = 0; i < group_info->ngroups; i++) {
138 if (get_user(group, grouplist+i))
139 return -EFAULT;
Eric W. Biedermanae2975b2011-11-14 15:56:38 -0800140
141 kgid = make_kgid(user_ns, low2highgid(group));
142 if (!gid_valid(kgid))
143 return -EINVAL;
144
Alexey Dobriyan81243ea2016-10-07 17:03:12 -0700145 group_info->gid[i] = kgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 }
147
148 return 0;
149}
150
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100151SYSCALL_DEFINE2(getgroups16, int, gidsetsize, old_gid_t __user *, grouplist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
David Howells86a264a2008-11-14 10:39:18 +1100153 const struct cred *cred = current_cred();
154 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 if (gidsetsize < 0)
157 return -EINVAL;
158
David Howells86a264a2008-11-14 10:39:18 +1100159 i = cred->group_info->ngroups;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (gidsetsize) {
161 if (i > gidsetsize) {
162 i = -EINVAL;
163 goto out;
164 }
David Howells86a264a2008-11-14 10:39:18 +1100165 if (groups16_to_user(grouplist, cred->group_info)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 i = -EFAULT;
167 goto out;
168 }
169 }
170out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return i;
172}
173
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100174SYSCALL_DEFINE2(setgroups16, int, gidsetsize, old_gid_t __user *, grouplist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 struct group_info *group_info;
177 int retval;
178
Eric W. Biederman7ff4d902014-12-05 17:19:27 -0600179 if (!may_setgroups())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return -EPERM;
181 if ((unsigned)gidsetsize > NGROUPS_MAX)
182 return -EINVAL;
183
184 group_info = groups_alloc(gidsetsize);
185 if (!group_info)
186 return -ENOMEM;
187 retval = groups16_from_user(group_info, grouplist);
188 if (retval) {
189 put_group_info(group_info);
190 return retval;
191 }
192
Thiago Rafael Becker79258d92017-12-14 15:33:12 -0800193 groups_sort(group_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 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}