scm.c 8.5 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9
/* scm.c - Socket level control messages processing.
 *
 * Author:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
 *              Alignment and value checking mods by Craig Metz
 */

#include <linux/module.h>
#include <linux/signal.h>
10
#include <linux/capability.h>
L
Linus Torvalds 已提交
11 12
#include <linux/errno.h>
#include <linux/sched.h>
13
#include <linux/sched/user.h>
L
Linus Torvalds 已提交
14 15 16 17 18 19 20 21 22 23
#include <linux/mm.h>
#include <linux/kernel.h>
#include <linux/stat.h>
#include <linux/socket.h>
#include <linux/file.h>
#include <linux/fcntl.h>
#include <linux/net.h>
#include <linux/interrupt.h>
#include <linux/netdevice.h>
#include <linux/security.h>
24
#include <linux/pid_namespace.h>
25 26
#include <linux/pid.h>
#include <linux/nsproxy.h>
27
#include <linux/slab.h>
D
Deepa Dinamani 已提交
28
#include <linux/errqueue.h>
L
Linus Torvalds 已提交
29

30
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
31 32 33 34 35 36

#include <net/protocol.h>
#include <linux/skbuff.h>
#include <net/sock.h>
#include <net/compat.h>
#include <net/scm.h>
37
#include <net/cls_cgroup.h>
L
Linus Torvalds 已提交
38 39 40


/*
41
 *	Only allow a user to send credentials, that they could set with
L
Linus Torvalds 已提交
42 43 44 45 46
 *	setu(g)id.
 */

static __inline__ int scm_check_creds(struct ucred *creds)
{
47
	const struct cred *cred = current_cred();
48 49 50 51 52
	kuid_t uid = make_kuid(cred->user_ns, creds->uid);
	kgid_t gid = make_kgid(cred->user_ns, creds->gid);

	if (!uid_valid(uid) || !gid_valid(gid))
		return -EINVAL;
53

54
	if ((creds->pid == task_tgid_vnr(current) ||
55
	     ns_capable(task_active_pid_ns(current)->user_ns, CAP_SYS_ADMIN)) &&
56
	    ((uid_eq(uid, cred->uid)   || uid_eq(uid, cred->euid) ||
57
	      uid_eq(uid, cred->suid)) || ns_capable(cred->user_ns, CAP_SETUID)) &&
58
	    ((gid_eq(gid, cred->gid)   || gid_eq(gid, cred->egid) ||
59
	      gid_eq(gid, cred->sgid)) || ns_capable(cred->user_ns, CAP_SETGID))) {
L
Linus Torvalds 已提交
60 61 62 63 64 65 66 67 68 69 70 71
	       return 0;
	}
	return -EPERM;
}

static int scm_fp_copy(struct cmsghdr *cmsg, struct scm_fp_list **fplp)
{
	int *fdp = (int*)CMSG_DATA(cmsg);
	struct scm_fp_list *fpl = *fplp;
	struct file **fpp;
	int i, num;

72
	num = (cmsg->cmsg_len - sizeof(struct cmsghdr))/sizeof(int);
L
Linus Torvalds 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86

	if (num <= 0)
		return 0;

	if (num > SCM_MAX_FD)
		return -EINVAL;

	if (!fpl)
	{
		fpl = kmalloc(sizeof(struct scm_fp_list), GFP_KERNEL);
		if (!fpl)
			return -ENOMEM;
		*fplp = fpl;
		fpl->count = 0;
E
Eric Dumazet 已提交
87
		fpl->max = SCM_MAX_FD;
88
		fpl->user = NULL;
L
Linus Torvalds 已提交
89 90 91
	}
	fpp = &fpl->fp[fpl->count];

E
Eric Dumazet 已提交
92
	if (fpl->count + num > fpl->max)
L
Linus Torvalds 已提交
93
		return -EINVAL;
94

L
Linus Torvalds 已提交
95 96 97
	/*
	 *	Verify the descriptors and increment the usage count.
	 */
98

L
Linus Torvalds 已提交
99 100 101 102 103
	for (i=0; i< num; i++)
	{
		int fd = fdp[i];
		struct file *file;

104
		if (fd < 0 || !(file = fget_raw(fd)))
L
Linus Torvalds 已提交
105 106 107 108
			return -EBADF;
		*fpp++ = file;
		fpl->count++;
	}
109 110 111 112

	if (!fpl->user)
		fpl->user = get_uid(current_user());

L
Linus Torvalds 已提交
113 114 115 116 117 118 119 120 121 122
	return num;
}

void __scm_destroy(struct scm_cookie *scm)
{
	struct scm_fp_list *fpl = scm->fp;
	int i;

	if (fpl) {
		scm->fp = NULL;
A
Al Viro 已提交
123 124
		for (i=fpl->count-1; i>=0; i--)
			fput(fpl->fp[i]);
125
		free_uid(fpl->user);
A
Al Viro 已提交
126
		kfree(fpl);
L
Linus Torvalds 已提交
127 128
	}
}
E
Eric Dumazet 已提交
129
EXPORT_SYMBOL(__scm_destroy);
L
Linus Torvalds 已提交
130 131 132 133 134 135

int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)
{
	struct cmsghdr *cmsg;
	int err;

136
	for_each_cmsghdr(cmsg, msg) {
L
Linus Torvalds 已提交
137 138 139 140 141 142
		err = -EINVAL;

		/* Verify that cmsg_len is at least sizeof(struct cmsghdr) */
		/* The first check was omitted in <= 2.2.5. The reasoning was
		   that parser checks cmsg_len in any case, so that
		   additional check would be work duplication.
143
		   But if cmsg_level is not SOL_SOCKET, we do not check
L
Linus Torvalds 已提交
144 145 146 147 148 149 150 151 152 153 154 155
		   for too short ancillary data object at all! Oops.
		   OK, let's add it...
		 */
		if (!CMSG_OK(msg, cmsg))
			goto error;

		if (cmsg->cmsg_level != SOL_SOCKET)
			continue;

		switch (cmsg->cmsg_type)
		{
		case SCM_RIGHTS:
156 157
			if (!sock->ops || sock->ops->family != PF_UNIX)
				goto error;
L
Linus Torvalds 已提交
158 159 160 161 162
			err=scm_fp_copy(cmsg, &p->fp);
			if (err<0)
				goto error;
			break;
		case SCM_CREDENTIALS:
163
		{
164
			struct ucred creds;
165 166
			kuid_t uid;
			kgid_t gid;
L
Linus Torvalds 已提交
167 168
			if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct ucred)))
				goto error;
169 170
			memcpy(&creds, CMSG_DATA(cmsg), sizeof(struct ucred));
			err = scm_check_creds(&creds);
L
Linus Torvalds 已提交
171 172
			if (err)
				goto error;
173

174 175
			p->creds.pid = creds.pid;
			if (!p->pid || pid_vnr(p->pid) != creds.pid) {
176 177
				struct pid *pid;
				err = -ESRCH;
178
				pid = find_get_pid(creds.pid);
179 180 181 182 183 184
				if (!pid)
					goto error;
				put_pid(p->pid);
				p->pid = pid;
			}

185
			err = -EINVAL;
186 187
			uid = make_kuid(current_user_ns(), creds.uid);
			gid = make_kgid(current_user_ns(), creds.gid);
188 189 190
			if (!uid_valid(uid) || !gid_valid(gid))
				goto error;

191 192
			p->creds.uid = uid;
			p->creds.gid = gid;
L
Linus Torvalds 已提交
193
			break;
194
		}
L
Linus Torvalds 已提交
195 196 197 198 199 200 201 202 203 204 205
		default:
			goto error;
		}
	}

	if (p->fp && !p->fp->count)
	{
		kfree(p->fp);
		p->fp = NULL;
	}
	return 0;
206

L
Linus Torvalds 已提交
207 208 209 210
error:
	scm_destroy(p);
	return err;
}
E
Eric Dumazet 已提交
211
EXPORT_SYMBOL(__scm_send);
L
Linus Torvalds 已提交
212 213 214 215 216

int put_cmsg(struct msghdr * msg, int level, int type, int len, void *data)
{
	int cmlen = CMSG_LEN(len);

217
	if (msg->msg_flags & MSG_CMSG_COMPAT)
L
Linus Torvalds 已提交
218 219
		return put_cmsg_compat(msg, level, type, len, data);

220
	if (!msg->msg_control || msg->msg_controllen < sizeof(struct cmsghdr)) {
L
Linus Torvalds 已提交
221 222 223 224 225 226 227
		msg->msg_flags |= MSG_CTRUNC;
		return 0; /* XXX: return error? check spec. */
	}
	if (msg->msg_controllen < cmlen) {
		msg->msg_flags |= MSG_CTRUNC;
		cmlen = msg->msg_controllen;
	}
228 229 230

	if (msg->msg_control_is_user) {
		struct cmsghdr __user *cm = msg->msg_control_user;
E
Eric Dumazet 已提交
231 232 233 234

		if (!user_write_access_begin(cm, cmlen))
			goto efault;

E
Eric Dumazet 已提交
235
		unsafe_put_user(cmlen, &cm->cmsg_len, efault_end);
E
Eric Dumazet 已提交
236 237 238 239 240
		unsafe_put_user(level, &cm->cmsg_level, efault_end);
		unsafe_put_user(type, &cm->cmsg_type, efault_end);
		unsafe_copy_to_user(CMSG_USER_DATA(cm), data,
				    cmlen - sizeof(*cm), efault_end);
		user_write_access_end();
241 242 243 244 245 246 247 248 249 250
	} else {
		struct cmsghdr *cm = msg->msg_control;

		cm->cmsg_level = level;
		cm->cmsg_type = type;
		cm->cmsg_len = cmlen;
		memcpy(CMSG_DATA(cm), data, cmlen - sizeof(*cm));
	}

	cmlen = min(CMSG_SPACE(len), msg->msg_controllen);
L
Linus Torvalds 已提交
251 252
	msg->msg_control += cmlen;
	msg->msg_controllen -= cmlen;
253
	return 0;
E
Eric Dumazet 已提交
254 255 256 257 258

efault_end:
	user_write_access_end();
efault:
	return -EFAULT;
L
Linus Torvalds 已提交
259
}
E
Eric Dumazet 已提交
260
EXPORT_SYMBOL(put_cmsg);
L
Linus Torvalds 已提交
261

D
Deepa Dinamani 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
void put_cmsg_scm_timestamping64(struct msghdr *msg, struct scm_timestamping_internal *tss_internal)
{
	struct scm_timestamping64 tss;
	int i;

	for (i = 0; i < ARRAY_SIZE(tss.ts); i++) {
		tss.ts[i].tv_sec = tss_internal->ts[i].tv_sec;
		tss.ts[i].tv_nsec = tss_internal->ts[i].tv_nsec;
	}

	put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPING_NEW, sizeof(tss), &tss);
}
EXPORT_SYMBOL(put_cmsg_scm_timestamping64);

void put_cmsg_scm_timestamping(struct msghdr *msg, struct scm_timestamping_internal *tss_internal)
{
	struct scm_timestamping tss;
	int i;

281 282 283 284
	for (i = 0; i < ARRAY_SIZE(tss.ts); i++) {
		tss.ts[i].tv_sec = tss_internal->ts[i].tv_sec;
		tss.ts[i].tv_nsec = tss_internal->ts[i].tv_nsec;
	}
D
Deepa Dinamani 已提交
285 286 287 288 289

	put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPING_OLD, sizeof(tss), &tss);
}
EXPORT_SYMBOL(put_cmsg_scm_timestamping);

290 291 292 293 294 295 296
static int scm_max_fds(struct msghdr *msg)
{
	if (msg->msg_controllen <= sizeof(struct cmsghdr))
		return 0;
	return (msg->msg_controllen - sizeof(struct cmsghdr)) / sizeof(int);
}

L
Linus Torvalds 已提交
297 298
void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm)
{
299 300 301
	struct cmsghdr __user *cm =
		(__force struct cmsghdr __user *)msg->msg_control;
	unsigned int o_flags = (msg->msg_flags & MSG_CMSG_CLOEXEC) ? O_CLOEXEC : 0;
302 303
	int fdmax = min_t(int, scm_max_fds(msg), scm->fp->count);
	int __user *cmsg_data = CMSG_USER_DATA(cm);
L
Linus Torvalds 已提交
304 305
	int err = 0, i;

306 307 308 309
	/* no use for FD passing from kernel space callers */
	if (WARN_ON_ONCE(!msg->msg_control_is_user))
		return;

310
	if (msg->msg_flags & MSG_CMSG_COMPAT) {
L
Linus Torvalds 已提交
311 312 313 314
		scm_detach_fds_compat(msg, scm);
		return;
	}

315
	for (i = 0; i < fdmax; i++) {
316
		err = receive_fd_user(scm->fp->fp[i], cmsg_data + i, o_flags);
317
		if (err < 0)
L
Linus Torvalds 已提交
318 319 320
			break;
	}

321
	if (i > 0) {
322 323
		int cmlen = CMSG_LEN(i * sizeof(int));

324
		err = put_user(SOL_SOCKET, &cm->cmsg_level);
L
Linus Torvalds 已提交
325 326 327 328 329
		if (!err)
			err = put_user(SCM_RIGHTS, &cm->cmsg_type);
		if (!err)
			err = put_user(cmlen, &cm->cmsg_len);
		if (!err) {
330
			cmlen = CMSG_SPACE(i * sizeof(int));
331 332
			if (msg->msg_controllen < cmlen)
				cmlen = msg->msg_controllen;
L
Linus Torvalds 已提交
333 334 335 336
			msg->msg_control += cmlen;
			msg->msg_controllen -= cmlen;
		}
	}
337 338

	if (i < scm->fp->count || (scm->fp->count && fdmax <= 0))
L
Linus Torvalds 已提交
339 340 341
		msg->msg_flags |= MSG_CTRUNC;

	/*
342 343
	 * All of the files that fit in the message have had their usage counts
	 * incremented, so we just free the list.
L
Linus Torvalds 已提交
344 345 346
	 */
	__scm_destroy(scm);
}
E
Eric Dumazet 已提交
347
EXPORT_SYMBOL(scm_detach_fds);
L
Linus Torvalds 已提交
348 349 350 351 352 353 354 355 356

struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl)
{
	struct scm_fp_list *new_fpl;
	int i;

	if (!fpl)
		return NULL;

E
Eric Dumazet 已提交
357 358
	new_fpl = kmemdup(fpl, offsetof(struct scm_fp_list, fp[fpl->count]),
			  GFP_KERNEL);
L
Linus Torvalds 已提交
359
	if (new_fpl) {
E
Eric Dumazet 已提交
360
		for (i = 0; i < fpl->count; i++)
L
Linus Torvalds 已提交
361
			get_file(fpl->fp[i]);
E
Eric Dumazet 已提交
362
		new_fpl->max = new_fpl->count;
363
		new_fpl->user = get_uid(fpl->user);
L
Linus Torvalds 已提交
364 365 366 367
	}
	return new_fpl;
}
EXPORT_SYMBOL(scm_fp_dup);