Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/mman.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/notifier.h> |
| 10 | #include <linux/reboot.h> |
| 11 | #include <linux/prctl.h> |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 12 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/init.h> |
| 14 | #include <linux/highuid.h> |
| 15 | #include <linux/security.h> |
Ingo Molnar | 5b825c3 | 2017-02-02 17:54:15 +0100 | [diff] [blame] | 16 | #include <linux/cred.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/syscalls.h> |
| 18 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 19 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Heiko Carstens | ca013e9 | 2009-01-14 14:14:19 +0100 | [diff] [blame] | 21 | SYSCALL_DEFINE3(chown16, const char __user *, filename, old_uid_t, user, old_gid_t, group) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | { |
Al Viro | 2cf0966 | 2013-01-21 15:25:54 -0500 | [diff] [blame] | 23 | return sys_chown(filename, low2highuid(user), low2highgid(group)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | } |
| 25 | |
Heiko Carstens | ca013e9 | 2009-01-14 14:14:19 +0100 | [diff] [blame] | 26 | SYSCALL_DEFINE3(lchown16, const char __user *, filename, old_uid_t, user, old_gid_t, group) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | { |
Al Viro | 2cf0966 | 2013-01-21 15:25:54 -0500 | [diff] [blame] | 28 | return sys_lchown(filename, low2highuid(user), low2highgid(group)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Heiko Carstens | ca013e9 | 2009-01-14 14:14:19 +0100 | [diff] [blame] | 31 | SYSCALL_DEFINE3(fchown16, unsigned int, fd, old_uid_t, user, old_gid_t, group) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | { |
Al Viro | 2cf0966 | 2013-01-21 15:25:54 -0500 | [diff] [blame] | 33 | return sys_fchown(fd, low2highuid(user), low2highgid(group)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Heiko Carstens | a6b42e8 | 2009-01-14 14:14:20 +0100 | [diff] [blame] | 36 | SYSCALL_DEFINE2(setregid16, old_gid_t, rgid, old_gid_t, egid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | { |
Al Viro | 2cf0966 | 2013-01-21 15:25:54 -0500 | [diff] [blame] | 38 | return sys_setregid(low2highgid(rgid), low2highgid(egid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Heiko Carstens | a6b42e8 | 2009-01-14 14:14:20 +0100 | [diff] [blame] | 41 | SYSCALL_DEFINE1(setgid16, old_gid_t, gid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | { |
Al Viro | 2cf0966 | 2013-01-21 15:25:54 -0500 | [diff] [blame] | 43 | return sys_setgid(low2highgid(gid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Heiko Carstens | a6b42e8 | 2009-01-14 14:14:20 +0100 | [diff] [blame] | 46 | SYSCALL_DEFINE2(setreuid16, old_uid_t, ruid, old_uid_t, euid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | { |
Al Viro | 2cf0966 | 2013-01-21 15:25:54 -0500 | [diff] [blame] | 48 | return sys_setreuid(low2highuid(ruid), low2highuid(euid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Heiko Carstens | a6b42e8 | 2009-01-14 14:14:20 +0100 | [diff] [blame] | 51 | SYSCALL_DEFINE1(setuid16, old_uid_t, uid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { |
Al Viro | 2cf0966 | 2013-01-21 15:25:54 -0500 | [diff] [blame] | 53 | return sys_setuid(low2highuid(uid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Heiko Carstens | a6b42e8 | 2009-01-14 14:14:20 +0100 | [diff] [blame] | 56 | SYSCALL_DEFINE3(setresuid16, old_uid_t, ruid, old_uid_t, euid, old_uid_t, suid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | { |
Al Viro | 2cf0966 | 2013-01-21 15:25:54 -0500 | [diff] [blame] | 58 | return sys_setresuid(low2highuid(ruid), low2highuid(euid), |
OGAWA Hirofumi | 5a7b46b | 2006-04-20 06:41:39 +0900 | [diff] [blame] | 59 | low2highuid(suid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Eric W. Biederman | a29c33f | 2012-02-07 18:51:01 -0800 | [diff] [blame] | 62 | SYSCALL_DEFINE3(getresuid16, old_uid_t __user *, ruidp, old_uid_t __user *, euidp, old_uid_t __user *, suidp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 64 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | int retval; |
Eric W. Biederman | a29c33f | 2012-02-07 18:51:01 -0800 | [diff] [blame] | 66 | old_uid_t ruid, euid, suid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Eric W. Biederman | a29c33f | 2012-02-07 18:51:01 -0800 | [diff] [blame] | 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)); |
| 71 | |
| 72 | if (!(retval = put_user(ruid, ruidp)) && |
| 73 | !(retval = put_user(euid, euidp))) |
| 74 | retval = put_user(suid, suidp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
| 76 | return retval; |
| 77 | } |
| 78 | |
Heiko Carstens | a6b42e8 | 2009-01-14 14:14:20 +0100 | [diff] [blame] | 79 | SYSCALL_DEFINE3(setresgid16, old_gid_t, rgid, old_gid_t, egid, old_gid_t, sgid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { |
Al Viro | 2cf0966 | 2013-01-21 15:25:54 -0500 | [diff] [blame] | 81 | return sys_setresgid(low2highgid(rgid), low2highgid(egid), |
OGAWA Hirofumi | 5a7b46b | 2006-04-20 06:41:39 +0900 | [diff] [blame] | 82 | low2highgid(sgid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Heiko Carstens | a6b42e8 | 2009-01-14 14:14:20 +0100 | [diff] [blame] | 85 | |
Eric W. Biederman | a29c33f | 2012-02-07 18:51:01 -0800 | [diff] [blame] | 86 | SYSCALL_DEFINE3(getresgid16, old_gid_t __user *, rgidp, old_gid_t __user *, egidp, old_gid_t __user *, sgidp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | { |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 88 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | int retval; |
Eric W. Biederman | a29c33f | 2012-02-07 18:51:01 -0800 | [diff] [blame] | 90 | old_gid_t rgid, egid, sgid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Eric W. Biederman | a29c33f | 2012-02-07 18:51:01 -0800 | [diff] [blame] | 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)); |
| 95 | |
| 96 | if (!(retval = put_user(rgid, rgidp)) && |
| 97 | !(retval = put_user(egid, egidp))) |
| 98 | retval = put_user(sgid, sgidp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
| 100 | return retval; |
| 101 | } |
| 102 | |
Heiko Carstens | a6b42e8 | 2009-01-14 14:14:20 +0100 | [diff] [blame] | 103 | SYSCALL_DEFINE1(setfsuid16, old_uid_t, uid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | { |
Al Viro | 2cf0966 | 2013-01-21 15:25:54 -0500 | [diff] [blame] | 105 | return sys_setfsuid(low2highuid(uid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Heiko Carstens | a6b42e8 | 2009-01-14 14:14:20 +0100 | [diff] [blame] | 108 | SYSCALL_DEFINE1(setfsgid16, old_gid_t, gid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | { |
Al Viro | 2cf0966 | 2013-01-21 15:25:54 -0500 | [diff] [blame] | 110 | return sys_setfsgid(low2highgid(gid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static int groups16_to_user(old_gid_t __user *grouplist, |
| 114 | struct group_info *group_info) |
| 115 | { |
Eric W. Biederman | ae2975b | 2011-11-14 15:56:38 -0800 | [diff] [blame] | 116 | struct user_namespace *user_ns = current_user_ns(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | int i; |
| 118 | old_gid_t group; |
Eric W. Biederman | ae2975b | 2011-11-14 15:56:38 -0800 | [diff] [blame] | 119 | kgid_t kgid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
| 121 | for (i = 0; i < group_info->ngroups; i++) { |
Alexey Dobriyan | 81243ea | 2016-10-07 17:03:12 -0700 | [diff] [blame] | 122 | kgid = group_info->gid[i]; |
Eric W. Biederman | ae2975b | 2011-11-14 15:56:38 -0800 | [diff] [blame] | 123 | group = high2lowgid(from_kgid_munged(user_ns, kgid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | if (put_user(group, grouplist+i)) |
| 125 | return -EFAULT; |
| 126 | } |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static int groups16_from_user(struct group_info *group_info, |
| 132 | old_gid_t __user *grouplist) |
| 133 | { |
Eric W. Biederman | ae2975b | 2011-11-14 15:56:38 -0800 | [diff] [blame] | 134 | struct user_namespace *user_ns = current_user_ns(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | int i; |
| 136 | old_gid_t group; |
Eric W. Biederman | ae2975b | 2011-11-14 15:56:38 -0800 | [diff] [blame] | 137 | kgid_t kgid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
| 139 | for (i = 0; i < group_info->ngroups; i++) { |
| 140 | if (get_user(group, grouplist+i)) |
| 141 | return -EFAULT; |
Eric W. Biederman | ae2975b | 2011-11-14 15:56:38 -0800 | [diff] [blame] | 142 | |
| 143 | kgid = make_kgid(user_ns, low2highgid(group)); |
| 144 | if (!gid_valid(kgid)) |
| 145 | return -EINVAL; |
| 146 | |
Alexey Dobriyan | 81243ea | 2016-10-07 17:03:12 -0700 | [diff] [blame] | 147 | group_info->gid[i] = kgid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
Heiko Carstens | 003d7ab | 2009-01-14 14:14:21 +0100 | [diff] [blame] | 153 | SYSCALL_DEFINE2(getgroups16, int, gidsetsize, old_gid_t __user *, grouplist) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | { |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 155 | const struct cred *cred = current_cred(); |
| 156 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
| 158 | if (gidsetsize < 0) |
| 159 | return -EINVAL; |
| 160 | |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 161 | i = cred->group_info->ngroups; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | if (gidsetsize) { |
| 163 | if (i > gidsetsize) { |
| 164 | i = -EINVAL; |
| 165 | goto out; |
| 166 | } |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 167 | if (groups16_to_user(grouplist, cred->group_info)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | i = -EFAULT; |
| 169 | goto out; |
| 170 | } |
| 171 | } |
| 172 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | return i; |
| 174 | } |
| 175 | |
Heiko Carstens | 003d7ab | 2009-01-14 14:14:21 +0100 | [diff] [blame] | 176 | SYSCALL_DEFINE2(setgroups16, int, gidsetsize, old_gid_t __user *, grouplist) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | { |
| 178 | struct group_info *group_info; |
| 179 | int retval; |
| 180 | |
Eric W. Biederman | 7ff4d90 | 2014-12-05 17:19:27 -0600 | [diff] [blame] | 181 | if (!may_setgroups()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | 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 Carstens | 003d7ab | 2009-01-14 14:14:21 +0100 | [diff] [blame] | 201 | SYSCALL_DEFINE0(getuid16) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | { |
Eric W. Biederman | a29c33f | 2012-02-07 18:51:01 -0800 | [diff] [blame] | 203 | return high2lowuid(from_kuid_munged(current_user_ns(), current_uid())); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Heiko Carstens | 003d7ab | 2009-01-14 14:14:21 +0100 | [diff] [blame] | 206 | SYSCALL_DEFINE0(geteuid16) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | { |
Eric W. Biederman | a29c33f | 2012-02-07 18:51:01 -0800 | [diff] [blame] | 208 | return high2lowuid(from_kuid_munged(current_user_ns(), current_euid())); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Heiko Carstens | 003d7ab | 2009-01-14 14:14:21 +0100 | [diff] [blame] | 211 | SYSCALL_DEFINE0(getgid16) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | { |
Eric W. Biederman | a29c33f | 2012-02-07 18:51:01 -0800 | [diff] [blame] | 213 | return high2lowgid(from_kgid_munged(current_user_ns(), current_gid())); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Heiko Carstens | 003d7ab | 2009-01-14 14:14:21 +0100 | [diff] [blame] | 216 | SYSCALL_DEFINE0(getegid16) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | { |
Eric W. Biederman | a29c33f | 2012-02-07 18:51:01 -0800 | [diff] [blame] | 218 | return high2lowgid(from_kgid_munged(current_user_ns(), current_egid())); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | } |