blob: ce74a4901d2b058595af031cf6c408c48dc5e1f1 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Wrapper functions for 16bit uid back compatibility. All nicely tied
4 * together in the faint hope we can take the out in five years time.
5 */
6
7#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/notifier.h>
10#include <linux/reboot.h>
11#include <linux/prctl.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080012#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/init.h>
14#include <linux/highuid.h>
15#include <linux/security.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010016#include <linux/cred.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/syscalls.h>
18
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080019#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Heiko Carstensca013e92009-01-14 14:14:19 +010021SYSCALL_DEFINE3(chown16, const char __user *, filename, old_uid_t, user, old_gid_t, group)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022{
Al Viro2cf09662013-01-21 15:25:54 -050023 return sys_chown(filename, low2highuid(user), low2highgid(group));
Linus Torvalds1da177e2005-04-16 15:20:36 -070024}
25
Heiko Carstensca013e92009-01-14 14:14:19 +010026SYSCALL_DEFINE3(lchown16, const char __user *, filename, old_uid_t, user, old_gid_t, group)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
Al Viro2cf09662013-01-21 15:25:54 -050028 return sys_lchown(filename, low2highuid(user), low2highgid(group));
Linus Torvalds1da177e2005-04-16 15:20:36 -070029}
30
Heiko Carstensca013e92009-01-14 14:14:19 +010031SYSCALL_DEFINE3(fchown16, unsigned int, fd, old_uid_t, user, old_gid_t, group)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
Al Viro2cf09662013-01-21 15:25:54 -050033 return sys_fchown(fd, low2highuid(user), low2highgid(group));
Linus Torvalds1da177e2005-04-16 15:20:36 -070034}
35
Heiko Carstensa6b42e82009-01-14 14:14:20 +010036SYSCALL_DEFINE2(setregid16, old_gid_t, rgid, old_gid_t, egid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Al Viro2cf09662013-01-21 15:25:54 -050038 return sys_setregid(low2highgid(rgid), low2highgid(egid));
Linus Torvalds1da177e2005-04-16 15:20:36 -070039}
40
Heiko Carstensa6b42e82009-01-14 14:14:20 +010041SYSCALL_DEFINE1(setgid16, old_gid_t, gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Al Viro2cf09662013-01-21 15:25:54 -050043 return sys_setgid(low2highgid(gid));
Linus Torvalds1da177e2005-04-16 15:20:36 -070044}
45
Heiko Carstensa6b42e82009-01-14 14:14:20 +010046SYSCALL_DEFINE2(setreuid16, old_uid_t, ruid, old_uid_t, euid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Al Viro2cf09662013-01-21 15:25:54 -050048 return sys_setreuid(low2highuid(ruid), low2highuid(euid));
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
Heiko Carstensa6b42e82009-01-14 14:14:20 +010051SYSCALL_DEFINE1(setuid16, old_uid_t, uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Al Viro2cf09662013-01-21 15:25:54 -050053 return sys_setuid(low2highuid(uid));
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
Heiko Carstensa6b42e82009-01-14 14:14:20 +010056SYSCALL_DEFINE3(setresuid16, old_uid_t, ruid, old_uid_t, euid, old_uid_t, suid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Al Viro2cf09662013-01-21 15:25:54 -050058 return sys_setresuid(low2highuid(ruid), low2highuid(euid),
OGAWA Hirofumi5a7b46b2006-04-20 06:41:39 +090059 low2highuid(suid));
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61
Eric W. Biedermana29c33f2012-02-07 18:51:01 -080062SYSCALL_DEFINE3(getresuid16, old_uid_t __user *, ruidp, old_uid_t __user *, euidp, old_uid_t __user *, suidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
David Howells86a264a2008-11-14 10:39:18 +110064 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -080066 old_uid_t ruid, euid, suid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Eric W. Biedermana29c33f2012-02-07 18:51:01 -080068 ruid = high2lowuid(from_kuid_munged(cred->user_ns, cred->uid));
69 euid = high2lowuid(from_kuid_munged(cred->user_ns, cred->euid));
70 suid = high2lowuid(from_kuid_munged(cred->user_ns, cred->suid));
71
72 if (!(retval = put_user(ruid, ruidp)) &&
73 !(retval = put_user(euid, euidp)))
74 retval = put_user(suid, suidp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 return retval;
77}
78
Heiko Carstensa6b42e82009-01-14 14:14:20 +010079SYSCALL_DEFINE3(setresgid16, old_gid_t, rgid, old_gid_t, egid, old_gid_t, sgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Al Viro2cf09662013-01-21 15:25:54 -050081 return sys_setresgid(low2highgid(rgid), low2highgid(egid),
OGAWA Hirofumi5a7b46b2006-04-20 06:41:39 +090082 low2highgid(sgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
Heiko Carstensa6b42e82009-01-14 14:14:20 +010085
Eric W. Biedermana29c33f2012-02-07 18:51:01 -080086SYSCALL_DEFINE3(getresgid16, old_gid_t __user *, rgidp, old_gid_t __user *, egidp, old_gid_t __user *, sgidp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
David Howells86a264a2008-11-14 10:39:18 +110088 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 int retval;
Eric W. Biedermana29c33f2012-02-07 18:51:01 -080090 old_gid_t rgid, egid, sgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Eric W. Biedermana29c33f2012-02-07 18:51:01 -080092 rgid = high2lowgid(from_kgid_munged(cred->user_ns, cred->gid));
93 egid = high2lowgid(from_kgid_munged(cred->user_ns, cred->egid));
94 sgid = high2lowgid(from_kgid_munged(cred->user_ns, cred->sgid));
95
96 if (!(retval = put_user(rgid, rgidp)) &&
97 !(retval = put_user(egid, egidp)))
98 retval = put_user(sgid, sgidp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 return retval;
101}
102
Heiko Carstensa6b42e82009-01-14 14:14:20 +0100103SYSCALL_DEFINE1(setfsuid16, old_uid_t, uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Al Viro2cf09662013-01-21 15:25:54 -0500105 return sys_setfsuid(low2highuid(uid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Heiko Carstensa6b42e82009-01-14 14:14:20 +0100108SYSCALL_DEFINE1(setfsgid16, old_gid_t, gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Al Viro2cf09662013-01-21 15:25:54 -0500110 return sys_setfsgid(low2highgid(gid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
113static int groups16_to_user(old_gid_t __user *grouplist,
114 struct group_info *group_info)
115{
Eric W. Biedermanae2975b2011-11-14 15:56:38 -0800116 struct user_namespace *user_ns = current_user_ns();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 int i;
118 old_gid_t group;
Eric W. Biedermanae2975b2011-11-14 15:56:38 -0800119 kgid_t kgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 for (i = 0; i < group_info->ngroups; i++) {
Alexey Dobriyan81243ea2016-10-07 17:03:12 -0700122 kgid = group_info->gid[i];
Eric W. Biedermanae2975b2011-11-14 15:56:38 -0800123 group = high2lowgid(from_kgid_munged(user_ns, kgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (put_user(group, grouplist+i))
125 return -EFAULT;
126 }
127
128 return 0;
129}
130
131static int groups16_from_user(struct group_info *group_info,
132 old_gid_t __user *grouplist)
133{
Eric W. Biedermanae2975b2011-11-14 15:56:38 -0800134 struct user_namespace *user_ns = current_user_ns();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 int i;
136 old_gid_t group;
Eric W. Biedermanae2975b2011-11-14 15:56:38 -0800137 kgid_t kgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 for (i = 0; i < group_info->ngroups; i++) {
140 if (get_user(group, grouplist+i))
141 return -EFAULT;
Eric W. Biedermanae2975b2011-11-14 15:56:38 -0800142
143 kgid = make_kgid(user_ns, low2highgid(group));
144 if (!gid_valid(kgid))
145 return -EINVAL;
146
Alexey Dobriyan81243ea2016-10-07 17:03:12 -0700147 group_info->gid[i] = kgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 }
149
150 return 0;
151}
152
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100153SYSCALL_DEFINE2(getgroups16, int, gidsetsize, old_gid_t __user *, grouplist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
David Howells86a264a2008-11-14 10:39:18 +1100155 const struct cred *cred = current_cred();
156 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 if (gidsetsize < 0)
159 return -EINVAL;
160
David Howells86a264a2008-11-14 10:39:18 +1100161 i = cred->group_info->ngroups;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 if (gidsetsize) {
163 if (i > gidsetsize) {
164 i = -EINVAL;
165 goto out;
166 }
David Howells86a264a2008-11-14 10:39:18 +1100167 if (groups16_to_user(grouplist, cred->group_info)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 i = -EFAULT;
169 goto out;
170 }
171 }
172out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return i;
174}
175
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100176SYSCALL_DEFINE2(setgroups16, int, gidsetsize, old_gid_t __user *, grouplist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 struct group_info *group_info;
179 int retval;
180
Eric W. Biederman7ff4d902014-12-05 17:19:27 -0600181 if (!may_setgroups())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return -EPERM;
183 if ((unsigned)gidsetsize > NGROUPS_MAX)
184 return -EINVAL;
185
186 group_info = groups_alloc(gidsetsize);
187 if (!group_info)
188 return -ENOMEM;
189 retval = groups16_from_user(group_info, grouplist);
190 if (retval) {
191 put_group_info(group_info);
192 return retval;
193 }
194
195 retval = set_current_groups(group_info);
196 put_group_info(group_info);
197
198 return retval;
199}
200
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100201SYSCALL_DEFINE0(getuid16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800203 return high2lowuid(from_kuid_munged(current_user_ns(), current_uid()));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100206SYSCALL_DEFINE0(geteuid16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800208 return high2lowuid(from_kuid_munged(current_user_ns(), current_euid()));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100211SYSCALL_DEFINE0(getgid16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800213 return high2lowgid(from_kgid_munged(current_user_ns(), current_gid()));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
Heiko Carstens003d7ab2009-01-14 14:14:21 +0100216SYSCALL_DEFINE0(getegid16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Eric W. Biedermana29c33f2012-02-07 18:51:01 -0800218 return high2lowgid(from_kgid_munged(current_user_ns(), current_egid()));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}