scm.c 7.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/* scm.c - Socket level control messages processing.
 *
 * Author:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
 *              Alignment and value checking mods by Craig Metz
 *
 *		This program is free software; you can redistribute it and/or
 *		modify it under the terms of the GNU General Public License
 *		as published by the Free Software Foundation; either version
 *		2 of the License, or (at your option) any later version.
 */

#include <linux/module.h>
#include <linux/signal.h>
14
#include <linux/capability.h>
L
Linus Torvalds 已提交
15 16
#include <linux/errno.h>
#include <linux/sched.h>
17
#include <linux/sched/user.h>
L
Linus Torvalds 已提交
18 19 20 21 22 23 24 25 26 27
#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>
28
#include <linux/pid_namespace.h>
29 30
#include <linux/pid.h>
#include <linux/nsproxy.h>
31
#include <linux/slab.h>
L
Linus Torvalds 已提交
32

33
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
34 35 36 37 38 39

#include <net/protocol.h>
#include <linux/skbuff.h>
#include <net/sock.h>
#include <net/compat.h>
#include <net/scm.h>
40
#include <net/cls_cgroup.h>
L
Linus Torvalds 已提交
41 42 43


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

static __inline__ int scm_check_creds(struct ucred *creds)
{
50
	const struct cred *cred = current_cred();
51 52 53 54 55
	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;
56

57
	if ((creds->pid == task_tgid_vnr(current) ||
58
	     ns_capable(task_active_pid_ns(current)->user_ns, CAP_SYS_ADMIN)) &&
59
	    ((uid_eq(uid, cred->uid)   || uid_eq(uid, cred->euid) ||
60
	      uid_eq(uid, cred->suid)) || ns_capable(cred->user_ns, CAP_SETUID)) &&
61
	    ((gid_eq(gid, cred->gid)   || gid_eq(gid, cred->egid) ||
62
	      gid_eq(gid, cred->sgid)) || ns_capable(cred->user_ns, CAP_SETGID))) {
L
Linus Torvalds 已提交
63 64 65 66 67 68 69 70 71 72 73 74
	       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;

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

	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 已提交
90
		fpl->max = SCM_MAX_FD;
91
		fpl->user = NULL;
L
Linus Torvalds 已提交
92 93 94
	}
	fpp = &fpl->fp[fpl->count];

E
Eric Dumazet 已提交
95
	if (fpl->count + num > fpl->max)
L
Linus Torvalds 已提交
96
		return -EINVAL;
97

L
Linus Torvalds 已提交
98 99 100
	/*
	 *	Verify the descriptors and increment the usage count.
	 */
101

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

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

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

L
Linus Torvalds 已提交
116 117 118 119 120 121 122 123 124 125
	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 已提交
126 127
		for (i=fpl->count-1; i>=0; i--)
			fput(fpl->fp[i]);
128
		free_uid(fpl->user);
A
Al Viro 已提交
129
		kfree(fpl);
L
Linus Torvalds 已提交
130 131
	}
}
E
Eric Dumazet 已提交
132
EXPORT_SYMBOL(__scm_destroy);
L
Linus Torvalds 已提交
133 134 135 136 137 138

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

139
	for_each_cmsghdr(cmsg, msg) {
L
Linus Torvalds 已提交
140 141 142 143 144 145
		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.
146
		   But if cmsg_level is not SOL_SOCKET, we do not check
L
Linus Torvalds 已提交
147 148 149 150 151 152 153 154 155 156 157 158
		   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:
159 160
			if (!sock->ops || sock->ops->family != PF_UNIX)
				goto error;
L
Linus Torvalds 已提交
161 162 163 164 165
			err=scm_fp_copy(cmsg, &p->fp);
			if (err<0)
				goto error;
			break;
		case SCM_CREDENTIALS:
166
		{
167
			struct ucred creds;
168 169
			kuid_t uid;
			kgid_t gid;
L
Linus Torvalds 已提交
170 171
			if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct ucred)))
				goto error;
172 173
			memcpy(&creds, CMSG_DATA(cmsg), sizeof(struct ucred));
			err = scm_check_creds(&creds);
L
Linus Torvalds 已提交
174 175
			if (err)
				goto error;
176

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

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

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

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

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

int put_cmsg(struct msghdr * msg, int level, int type, int len, void *data)
{
S
Stephen Hemminger 已提交
218 219
	struct cmsghdr __user *cm
		= (__force struct cmsghdr __user *)msg->msg_control;
L
Linus Torvalds 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
	struct cmsghdr cmhdr;
	int cmlen = CMSG_LEN(len);
	int err;

	if (MSG_CMSG_COMPAT & msg->msg_flags)
		return put_cmsg_compat(msg, level, type, len, data);

	if (cm==NULL || msg->msg_controllen < sizeof(*cm)) {
		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;
	}
	cmhdr.cmsg_level = level;
	cmhdr.cmsg_type = type;
	cmhdr.cmsg_len = cmlen;

	err = -EFAULT;
	if (copy_to_user(cm, &cmhdr, sizeof cmhdr))
241
		goto out;
L
Linus Torvalds 已提交
242 243 244
	if (copy_to_user(CMSG_DATA(cm), data, cmlen - sizeof(struct cmsghdr)))
		goto out;
	cmlen = CMSG_SPACE(len);
245 246
	if (msg->msg_controllen < cmlen)
		cmlen = msg->msg_controllen;
L
Linus Torvalds 已提交
247 248 249 250 251 252
	msg->msg_control += cmlen;
	msg->msg_controllen -= cmlen;
	err = 0;
out:
	return err;
}
E
Eric Dumazet 已提交
253
EXPORT_SYMBOL(put_cmsg);
L
Linus Torvalds 已提交
254 255 256

void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm)
{
S
Stephen Hemminger 已提交
257 258
	struct cmsghdr __user *cm
		= (__force struct cmsghdr __user*)msg->msg_control;
L
Linus Torvalds 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

	int fdmax = 0;
	int fdnum = scm->fp->count;
	struct file **fp = scm->fp->fp;
	int __user *cmfptr;
	int err = 0, i;

	if (MSG_CMSG_COMPAT & msg->msg_flags) {
		scm_detach_fds_compat(msg, scm);
		return;
	}

	if (msg->msg_controllen > sizeof(struct cmsghdr))
		fdmax = ((msg->msg_controllen - sizeof(struct cmsghdr))
			 / sizeof(int));

	if (fdnum < fdmax)
		fdmax = fdnum;

S
Stephen Hemminger 已提交
278 279
	for (i=0, cmfptr=(__force int __user *)CMSG_DATA(cm); i<fdmax;
	     i++, cmfptr++)
L
Linus Torvalds 已提交
280
	{
281
		struct socket *sock;
L
Linus Torvalds 已提交
282 283 284 285
		int new_fd;
		err = security_file_receive(fp[i]);
		if (err)
			break;
U
Ulrich Drepper 已提交
286 287
		err = get_unused_fd_flags(MSG_CMSG_CLOEXEC & msg->msg_flags
					  ? O_CLOEXEC : 0);
L
Linus Torvalds 已提交
288 289 290 291 292 293 294 295 296
		if (err < 0)
			break;
		new_fd = err;
		err = put_user(new_fd, cmfptr);
		if (err) {
			put_unused_fd(new_fd);
			break;
		}
		/* Bump the usage count and install the file. */
297
		sock = sock_from_file(fp[i], &err);
298
		if (sock) {
299 300
			sock_update_netprioidx(&sock->sk->sk_cgrp_data);
			sock_update_classid(&sock->sk->sk_cgrp_data);
301
		}
A
Al Viro 已提交
302
		fd_install(new_fd, get_file(fp[i]));
L
Linus Torvalds 已提交
303 304 305 306 307
	}

	if (i > 0)
	{
		int cmlen = CMSG_LEN(i*sizeof(int));
308
		err = put_user(SOL_SOCKET, &cm->cmsg_level);
L
Linus Torvalds 已提交
309 310 311 312 313 314
		if (!err)
			err = put_user(SCM_RIGHTS, &cm->cmsg_type);
		if (!err)
			err = put_user(cmlen, &cm->cmsg_len);
		if (!err) {
			cmlen = CMSG_SPACE(i*sizeof(int));
315 316
			if (msg->msg_controllen < cmlen)
				cmlen = msg->msg_controllen;
L
Linus Torvalds 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329
			msg->msg_control += cmlen;
			msg->msg_controllen -= cmlen;
		}
	}
	if (i < fdnum || (fdnum && fdmax <= 0))
		msg->msg_flags |= MSG_CTRUNC;

	/*
	 * All of the files that fit in the message have had their
	 * usage counts incremented, so we just free the list.
	 */
	__scm_destroy(scm);
}
E
Eric Dumazet 已提交
330
EXPORT_SYMBOL(scm_detach_fds);
L
Linus Torvalds 已提交
331 332 333 334 335 336 337 338 339

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 已提交
340 341
	new_fpl = kmemdup(fpl, offsetof(struct scm_fp_list, fp[fpl->count]),
			  GFP_KERNEL);
L
Linus Torvalds 已提交
342
	if (new_fpl) {
E
Eric Dumazet 已提交
343
		for (i = 0; i < fpl->count; i++)
L
Linus Torvalds 已提交
344
			get_file(fpl->fp[i]);
E
Eric Dumazet 已提交
345
		new_fpl->max = new_fpl->count;
346
		new_fpl->user = get_uid(fpl->user);
L
Linus Torvalds 已提交
347 348 349 350
	}
	return new_fpl;
}
EXPORT_SYMBOL(scm_fp_dup);