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>
21 SYSCALL_DEFINE3(chown16
, const char __user
*, filename
, old_uid_t
, user
, old_gid_t
, group
)
23 return sys_chown(filename
, low2highuid(user
), low2highgid(group
));
26 SYSCALL_DEFINE3(lchown16
, const char __user
*, filename
, old_uid_t
, user
, old_gid_t
, group
)
28 return sys_lchown(filename
, low2highuid(user
), low2highgid(group
));
31 SYSCALL_DEFINE3(fchown16
, unsigned int, fd
, old_uid_t
, user
, old_gid_t
, group
)
33 return sys_fchown(fd
, low2highuid(user
), low2highgid(group
));
36 SYSCALL_DEFINE2(setregid16
, old_gid_t
, rgid
, old_gid_t
, egid
)
38 return sys_setregid(low2highgid(rgid
), low2highgid(egid
));
41 SYSCALL_DEFINE1(setgid16
, old_gid_t
, gid
)
43 return sys_setgid(low2highgid(gid
));
46 SYSCALL_DEFINE2(setreuid16
, old_uid_t
, ruid
, old_uid_t
, euid
)
48 return sys_setreuid(low2highuid(ruid
), low2highuid(euid
));
51 SYSCALL_DEFINE1(setuid16
, old_uid_t
, uid
)
53 return sys_setuid(low2highuid(uid
));
56 SYSCALL_DEFINE3(setresuid16
, old_uid_t
, ruid
, old_uid_t
, euid
, old_uid_t
, suid
)
58 return sys_setresuid(low2highuid(ruid
), low2highuid(euid
),
62 SYSCALL_DEFINE3(getresuid16
, old_uid_t __user
*, ruidp
, old_uid_t __user
*, euidp
, old_uid_t __user
*, suidp
)
64 const struct cred
*cred
= current_cred();
66 old_uid_t ruid
, euid
, suid
;
68 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
));
72 if (!(retval
= put_user(ruid
, ruidp
)) &&
73 !(retval
= put_user(euid
, euidp
)))
74 retval
= put_user(suid
, suidp
);
79 SYSCALL_DEFINE3(setresgid16
, old_gid_t
, rgid
, old_gid_t
, egid
, old_gid_t
, sgid
)
81 return sys_setresgid(low2highgid(rgid
), low2highgid(egid
),
86 SYSCALL_DEFINE3(getresgid16
, old_gid_t __user
*, rgidp
, old_gid_t __user
*, egidp
, old_gid_t __user
*, sgidp
)
88 const struct cred
*cred
= current_cred();
90 old_gid_t rgid
, egid
, sgid
;
92 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
));
96 if (!(retval
= put_user(rgid
, rgidp
)) &&
97 !(retval
= put_user(egid
, egidp
)))
98 retval
= put_user(sgid
, sgidp
);
103 SYSCALL_DEFINE1(setfsuid16
, old_uid_t
, uid
)
105 return sys_setfsuid(low2highuid(uid
));
108 SYSCALL_DEFINE1(setfsgid16
, old_gid_t
, gid
)
110 return sys_setfsgid(low2highgid(gid
));
113 static int groups16_to_user(old_gid_t __user
*grouplist
,
114 struct group_info
*group_info
)
116 struct user_namespace
*user_ns
= current_user_ns();
121 for (i
= 0; i
< group_info
->ngroups
; i
++) {
122 kgid
= group_info
->gid
[i
];
123 group
= high2lowgid(from_kgid_munged(user_ns
, kgid
));
124 if (put_user(group
, grouplist
+i
))
131 static int groups16_from_user(struct group_info
*group_info
,
132 old_gid_t __user
*grouplist
)
134 struct user_namespace
*user_ns
= current_user_ns();
139 for (i
= 0; i
< group_info
->ngroups
; i
++) {
140 if (get_user(group
, grouplist
+i
))
143 kgid
= make_kgid(user_ns
, low2highgid(group
));
144 if (!gid_valid(kgid
))
147 group_info
->gid
[i
] = kgid
;
153 SYSCALL_DEFINE2(getgroups16
, int, gidsetsize
, old_gid_t __user
*, grouplist
)
155 const struct cred
*cred
= current_cred();
161 i
= cred
->group_info
->ngroups
;
163 if (i
> gidsetsize
) {
167 if (groups16_to_user(grouplist
, cred
->group_info
)) {
176 SYSCALL_DEFINE2(setgroups16
, int, gidsetsize
, old_gid_t __user
*, grouplist
)
178 struct group_info
*group_info
;
181 if (!may_setgroups())
183 if ((unsigned)gidsetsize
> NGROUPS_MAX
)
186 group_info
= groups_alloc(gidsetsize
);
189 retval
= groups16_from_user(group_info
, grouplist
);
191 put_group_info(group_info
);
195 groups_sort(group_info
);
196 retval
= set_current_groups(group_info
);
197 put_group_info(group_info
);
202 SYSCALL_DEFINE0(getuid16
)
204 return high2lowuid(from_kuid_munged(current_user_ns(), current_uid()));
207 SYSCALL_DEFINE0(geteuid16
)
209 return high2lowuid(from_kuid_munged(current_user_ns(), current_euid()));
212 SYSCALL_DEFINE0(getgid16
)
214 return high2lowgid(from_kgid_munged(current_user_ns(), current_gid()));
217 SYSCALL_DEFINE0(getegid16
)
219 return high2lowgid(from_kgid_munged(current_user_ns(), current_egid()));