1 // SPDX-License-Identifier: GPL-2.0
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.
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/cred.h>
17 #include <linux/syscalls.h>
19 #include <linux/uaccess.h>
23 SYSCALL_DEFINE3(chown16, const char __user *, filename, old_uid_t, user, old_gid_t, group)
25 return ksys_chown(filename, low2highuid(user), low2highgid(group));
28 SYSCALL_DEFINE3(lchown16, const char __user *, filename, old_uid_t, user, old_gid_t, group)
30 return ksys_lchown(filename, low2highuid(user), low2highgid(group));
33 SYSCALL_DEFINE3(fchown16, unsigned int, fd, old_uid_t, user, old_gid_t, group)
35 return ksys_fchown(fd, low2highuid(user), low2highgid(group));
38 SYSCALL_DEFINE2(setregid16, old_gid_t, rgid, old_gid_t, egid)
40 return __sys_setregid(low2highgid(rgid), low2highgid(egid));
43 SYSCALL_DEFINE1(setgid16, old_gid_t, gid)
45 return __sys_setgid(low2highgid(gid));
48 SYSCALL_DEFINE2(setreuid16, old_uid_t, ruid, old_uid_t, euid)
50 return __sys_setreuid(low2highuid(ruid), low2highuid(euid));
53 SYSCALL_DEFINE1(setuid16, old_uid_t, uid)
55 return __sys_setuid(low2highuid(uid));
58 SYSCALL_DEFINE3(setresuid16, old_uid_t, ruid, old_uid_t, euid, old_uid_t, suid)
60 return __sys_setresuid(low2highuid(ruid), low2highuid(euid),
64 SYSCALL_DEFINE3(getresuid16, old_uid_t __user *, ruidp, old_uid_t __user *, euidp, old_uid_t __user *, suidp)
66 const struct cred *cred = current_cred();
68 old_uid_t ruid, euid, suid;
70 ruid = high2lowuid(from_kuid_munged(cred->user_ns, cred->uid));
71 euid = high2lowuid(from_kuid_munged(cred->user_ns, cred->euid));
72 suid = high2lowuid(from_kuid_munged(cred->user_ns, cred->suid));
74 if (!(retval = put_user(ruid, ruidp)) &&
75 !(retval = put_user(euid, euidp)))
76 retval = put_user(suid, suidp);
81 SYSCALL_DEFINE3(setresgid16, old_gid_t, rgid, old_gid_t, egid, old_gid_t, sgid)
83 return __sys_setresgid(low2highgid(rgid), low2highgid(egid),
87 SYSCALL_DEFINE3(getresgid16, old_gid_t __user *, rgidp, old_gid_t __user *, egidp, old_gid_t __user *, sgidp)
89 const struct cred *cred = current_cred();
91 old_gid_t rgid, egid, sgid;
93 rgid = high2lowgid(from_kgid_munged(cred->user_ns, cred->gid));
94 egid = high2lowgid(from_kgid_munged(cred->user_ns, cred->egid));
95 sgid = high2lowgid(from_kgid_munged(cred->user_ns, cred->sgid));
97 if (!(retval = put_user(rgid, rgidp)) &&
98 !(retval = put_user(egid, egidp)))
99 retval = put_user(sgid, sgidp);
104 SYSCALL_DEFINE1(setfsuid16, old_uid_t, uid)
106 return __sys_setfsuid(low2highuid(uid));
109 SYSCALL_DEFINE1(setfsgid16, old_gid_t, gid)
111 return __sys_setfsgid(low2highgid(gid));
114 static int groups16_to_user(old_gid_t __user *grouplist,
115 struct group_info *group_info)
117 struct user_namespace *user_ns = current_user_ns();
122 for (i = 0; i < group_info->ngroups; i++) {
123 kgid = group_info->gid[i];
124 group = high2lowgid(from_kgid_munged(user_ns, kgid));
125 if (put_user(group, grouplist+i))
132 static int groups16_from_user(struct group_info *group_info,
133 old_gid_t __user *grouplist)
135 struct user_namespace *user_ns = current_user_ns();
140 for (i = 0; i < group_info->ngroups; i++) {
141 if (get_user(group, grouplist+i))
144 kgid = make_kgid(user_ns, low2highgid(group));
145 if (!gid_valid(kgid))
148 group_info->gid[i] = kgid;
154 SYSCALL_DEFINE2(getgroups16, int, gidsetsize, old_gid_t __user *, grouplist)
156 const struct cred *cred = current_cred();
162 i = cred->group_info->ngroups;
164 if (i > gidsetsize) {
168 if (groups16_to_user(grouplist, cred->group_info)) {
177 SYSCALL_DEFINE2(setgroups16, int, gidsetsize, old_gid_t __user *, grouplist)
179 struct group_info *group_info;
182 if (!may_setgroups())
184 if ((unsigned)gidsetsize > NGROUPS_MAX)
187 group_info = groups_alloc(gidsetsize);
190 retval = groups16_from_user(group_info, grouplist);
192 put_group_info(group_info);
196 groups_sort(group_info);
197 retval = set_current_groups(group_info);
198 put_group_info(group_info);
203 SYSCALL_DEFINE0(getuid16)
205 return high2lowuid(from_kuid_munged(current_user_ns(), current_uid()));
208 SYSCALL_DEFINE0(geteuid16)
210 return high2lowuid(from_kuid_munged(current_user_ns(), current_euid()));
213 SYSCALL_DEFINE0(getgid16)
215 return high2lowgid(from_kgid_munged(current_user_ns(), current_gid()));
218 SYSCALL_DEFINE0(getegid16)
220 return high2lowgid(from_kgid_munged(current_user_ns(), current_egid()));