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