uid16.c 5.01 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
Linus Torvalds's avatar
Linus Torvalds committed
2 3 4 5 6 7 8 9 10 11
/*
 *	Wrapper functions for 16bit uid back compatibility. All nicely tied
 *	together in the faint hope we can take the out in five years time.
 */

#include <linux/mm.h>
#include <linux/mman.h>
#include <linux/notifier.h>
#include <linux/reboot.h>
#include <linux/prctl.h>
12
#include <linux/capability.h>
Linus Torvalds's avatar
Linus Torvalds committed
13 14 15
#include <linux/init.h>
#include <linux/highuid.h>
#include <linux/security.h>
16
#include <linux/cred.h>
Linus Torvalds's avatar
Linus Torvalds committed
17 18
#include <linux/syscalls.h>

19
#include <linux/uaccess.h>
Linus Torvalds's avatar
Linus Torvalds committed
20

21
SYSCALL_DEFINE3(chown16, const char __user *, filename, old_uid_t, user, old_gid_t, group)
Linus Torvalds's avatar
Linus Torvalds committed
22
{
23
	return sys_chown(filename, low2highuid(user), low2highgid(group));
Linus Torvalds's avatar
Linus Torvalds committed
24 25
}

26
SYSCALL_DEFINE3(lchown16, const char __user *, filename, old_uid_t, user, old_gid_t, group)
Linus Torvalds's avatar
Linus Torvalds committed
27
{
28
	return sys_lchown(filename, low2highuid(user), low2highgid(group));
Linus Torvalds's avatar
Linus Torvalds committed
29 30
}

31
SYSCALL_DEFINE3(fchown16, unsigned int, fd, old_uid_t, user, old_gid_t, group)
Linus Torvalds's avatar
Linus Torvalds committed
32
{
33
	return sys_fchown(fd, low2highuid(user), low2highgid(group));
Linus Torvalds's avatar
Linus Torvalds committed
34 35
}

36
SYSCALL_DEFINE2(setregid16, old_gid_t, rgid, old_gid_t, egid)
Linus Torvalds's avatar
Linus Torvalds committed
37
{
38
	return sys_setregid(low2highgid(rgid), low2highgid(egid));
Linus Torvalds's avatar
Linus Torvalds committed
39 40
}

41
SYSCALL_DEFINE1(setgid16, old_gid_t, gid)
Linus Torvalds's avatar
Linus Torvalds committed
42
{
43
	return sys_setgid(low2highgid(gid));
Linus Torvalds's avatar
Linus Torvalds committed
44 45
}

46
SYSCALL_DEFINE2(setreuid16, old_uid_t, ruid, old_uid_t, euid)
Linus Torvalds's avatar
Linus Torvalds committed
47
{
48
	return sys_setreuid(low2highuid(ruid), low2highuid(euid));
Linus Torvalds's avatar
Linus Torvalds committed
49 50
}

51
SYSCALL_DEFINE1(setuid16, old_uid_t, uid)
Linus Torvalds's avatar
Linus Torvalds committed
52
{
53
	return sys_setuid(low2highuid(uid));
Linus Torvalds's avatar
Linus Torvalds committed
54 55
}

56
SYSCALL_DEFINE3(setresuid16, old_uid_t, ruid, old_uid_t, euid, old_uid_t, suid)
Linus Torvalds's avatar
Linus Torvalds committed
57
{
58
	return sys_setresuid(low2highuid(ruid), low2highuid(euid),
59
				 low2highuid(suid));
Linus Torvalds's avatar
Linus Torvalds committed
60 61
}

62
SYSCALL_DEFINE3(getresuid16, old_uid_t __user *, ruidp, old_uid_t __user *, euidp, old_uid_t __user *, suidp)
Linus Torvalds's avatar
Linus Torvalds committed
63
{
64
	const struct cred *cred = current_cred();
Linus Torvalds's avatar
Linus Torvalds committed
65
	int retval;
66
	old_uid_t ruid, euid, suid;
Linus Torvalds's avatar
Linus Torvalds committed
67

68 69 70 71 72 73 74
	ruid = high2lowuid(from_kuid_munged(cred->user_ns, cred->uid));
	euid = high2lowuid(from_kuid_munged(cred->user_ns, cred->euid));
	suid = high2lowuid(from_kuid_munged(cred->user_ns, cred->suid));

	if (!(retval   = put_user(ruid, ruidp)) &&
	    !(retval   = put_user(euid, euidp)))
		retval = put_user(suid, suidp);
Linus Torvalds's avatar
Linus Torvalds committed
75 76 77 78

	return retval;
}

79
SYSCALL_DEFINE3(setresgid16, old_gid_t, rgid, old_gid_t, egid, old_gid_t, sgid)
Linus Torvalds's avatar
Linus Torvalds committed
80
{
81
	return sys_setresgid(low2highgid(rgid), low2highgid(egid),
82
				 low2highgid(sgid));
Linus Torvalds's avatar
Linus Torvalds committed
83 84
}

85

86
SYSCALL_DEFINE3(getresgid16, old_gid_t __user *, rgidp, old_gid_t __user *, egidp, old_gid_t __user *, sgidp)
Linus Torvalds's avatar
Linus Torvalds committed
87
{
88
	const struct cred *cred = current_cred();
Linus Torvalds's avatar
Linus Torvalds committed
89
	int retval;
90 91 92 93 94
	old_gid_t rgid, egid, sgid;

	rgid = high2lowgid(from_kgid_munged(cred->user_ns, cred->gid));
	egid = high2lowgid(from_kgid_munged(cred->user_ns, cred->egid));
	sgid = high2lowgid(from_kgid_munged(cred->user_ns, cred->sgid));
Linus Torvalds's avatar
Linus Torvalds committed
95

96 97 98
	if (!(retval   = put_user(rgid, rgidp)) &&
	    !(retval   = put_user(egid, egidp)))
		retval = put_user(sgid, sgidp);
Linus Torvalds's avatar
Linus Torvalds committed
99 100 101 102

	return retval;
}

103
SYSCALL_DEFINE1(setfsuid16, old_uid_t, uid)
Linus Torvalds's avatar
Linus Torvalds committed
104
{
105
	return sys_setfsuid(low2highuid(uid));
Linus Torvalds's avatar
Linus Torvalds committed
106 107
}

108
SYSCALL_DEFINE1(setfsgid16, old_gid_t, gid)
Linus Torvalds's avatar
Linus Torvalds committed
109
{
110
	return sys_setfsgid(low2highgid(gid));
Linus Torvalds's avatar
Linus Torvalds committed
111 112 113 114 115
}

static int groups16_to_user(old_gid_t __user *grouplist,
    struct group_info *group_info)
{
116
	struct user_namespace *user_ns = current_user_ns();
Linus Torvalds's avatar
Linus Torvalds committed
117 118
	int i;
	old_gid_t group;
119
	kgid_t kgid;
Linus Torvalds's avatar
Linus Torvalds committed
120 121

	for (i = 0; i < group_info->ngroups; i++) {
122
		kgid = group_info->gid[i];
123
		group = high2lowgid(from_kgid_munged(user_ns, kgid));
Linus Torvalds's avatar
Linus Torvalds committed
124 125 126 127 128 129 130 131 132 133
		if (put_user(group, grouplist+i))
			return -EFAULT;
	}

	return 0;
}

static int groups16_from_user(struct group_info *group_info,
    old_gid_t __user *grouplist)
{
134
	struct user_namespace *user_ns = current_user_ns();
Linus Torvalds's avatar
Linus Torvalds committed
135 136
	int i;
	old_gid_t group;
137
	kgid_t kgid;
Linus Torvalds's avatar
Linus Torvalds committed
138 139 140 141

	for (i = 0; i < group_info->ngroups; i++) {
		if (get_user(group, grouplist+i))
			return  -EFAULT;
142 143 144 145 146

		kgid = make_kgid(user_ns, low2highgid(group));
		if (!gid_valid(kgid))
			return -EINVAL;

147
		group_info->gid[i] = kgid;
Linus Torvalds's avatar
Linus Torvalds committed
148 149 150 151 152
	}

	return 0;
}

153
SYSCALL_DEFINE2(getgroups16, int, gidsetsize, old_gid_t __user *, grouplist)
Linus Torvalds's avatar
Linus Torvalds committed
154
{
155 156
	const struct cred *cred = current_cred();
	int i;
Linus Torvalds's avatar
Linus Torvalds committed
157 158 159 160

	if (gidsetsize < 0)
		return -EINVAL;

161
	i = cred->group_info->ngroups;
Linus Torvalds's avatar
Linus Torvalds committed
162 163 164 165 166
	if (gidsetsize) {
		if (i > gidsetsize) {
			i = -EINVAL;
			goto out;
		}
167
		if (groups16_to_user(grouplist, cred->group_info)) {
Linus Torvalds's avatar
Linus Torvalds committed
168 169 170 171 172 173 174 175
			i = -EFAULT;
			goto out;
		}
	}
out:
	return i;
}

176
SYSCALL_DEFINE2(setgroups16, int, gidsetsize, old_gid_t __user *, grouplist)
Linus Torvalds's avatar
Linus Torvalds committed
177 178 179 180
{
	struct group_info *group_info;
	int retval;

181
	if (!may_setgroups())
Linus Torvalds's avatar
Linus Torvalds committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
		return -EPERM;
	if ((unsigned)gidsetsize > NGROUPS_MAX)
		return -EINVAL;

	group_info = groups_alloc(gidsetsize);
	if (!group_info)
		return -ENOMEM;
	retval = groups16_from_user(group_info, grouplist);
	if (retval) {
		put_group_info(group_info);
		return retval;
	}

	retval = set_current_groups(group_info);
	put_group_info(group_info);

	return retval;
}

201
SYSCALL_DEFINE0(getuid16)
Linus Torvalds's avatar
Linus Torvalds committed
202
{
203
	return high2lowuid(from_kuid_munged(current_user_ns(), current_uid()));
Linus Torvalds's avatar
Linus Torvalds committed
204 205
}

206
SYSCALL_DEFINE0(geteuid16)
Linus Torvalds's avatar
Linus Torvalds committed
207
{
208
	return high2lowuid(from_kuid_munged(current_user_ns(), current_euid()));
Linus Torvalds's avatar
Linus Torvalds committed
209 210
}

211
SYSCALL_DEFINE0(getgid16)
Linus Torvalds's avatar
Linus Torvalds committed
212
{
213
	return high2lowgid(from_kgid_munged(current_user_ns(), current_gid()));
Linus Torvalds's avatar
Linus Torvalds committed
214 215
}

216
SYSCALL_DEFINE0(getegid16)
Linus Torvalds's avatar
Linus Torvalds committed
217
{
218
	return high2lowgid(from_kgid_munged(current_user_ns(), current_egid()));
Linus Torvalds's avatar
Linus Torvalds committed
219
}