xref: /linux/net/core/scm.c (revision 48a87cc26c13b68f6cce4e9d769fcb17a6b3e4b8)
11da177e4SLinus Torvalds /* scm.c - Socket level control messages processing.
21da177e4SLinus Torvalds  *
31da177e4SLinus Torvalds  * Author:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
41da177e4SLinus Torvalds  *              Alignment and value checking mods by Craig Metz
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
71da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
81da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
91da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #include <linux/module.h>
131da177e4SLinus Torvalds #include <linux/signal.h>
144fc268d2SRandy Dunlap #include <linux/capability.h>
151da177e4SLinus Torvalds #include <linux/errno.h>
161da177e4SLinus Torvalds #include <linux/sched.h>
171da177e4SLinus Torvalds #include <linux/mm.h>
181da177e4SLinus Torvalds #include <linux/kernel.h>
191da177e4SLinus Torvalds #include <linux/stat.h>
201da177e4SLinus Torvalds #include <linux/socket.h>
211da177e4SLinus Torvalds #include <linux/file.h>
221da177e4SLinus Torvalds #include <linux/fcntl.h>
231da177e4SLinus Torvalds #include <linux/net.h>
241da177e4SLinus Torvalds #include <linux/interrupt.h>
251da177e4SLinus Torvalds #include <linux/netdevice.h>
261da177e4SLinus Torvalds #include <linux/security.h>
27b488893aSPavel Emelyanov #include <linux/pid.h>
28b488893aSPavel Emelyanov #include <linux/nsproxy.h>
295a0e3ad6STejun Heo #include <linux/slab.h>
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds #include <asm/uaccess.h>
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds #include <net/protocol.h>
341da177e4SLinus Torvalds #include <linux/skbuff.h>
351da177e4SLinus Torvalds #include <net/sock.h>
361da177e4SLinus Torvalds #include <net/compat.h>
371da177e4SLinus Torvalds #include <net/scm.h>
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds /*
411da177e4SLinus Torvalds  *	Only allow a user to send credentials, that they could set with
421da177e4SLinus Torvalds  *	setu(g)id.
431da177e4SLinus Torvalds  */
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds static __inline__ int scm_check_creds(struct ucred *creds)
461da177e4SLinus Torvalds {
4786a264abSDavid Howells 	const struct cred *cred = current_cred();
48b6dff3ecSDavid Howells 
49b488893aSPavel Emelyanov 	if ((creds->pid == task_tgid_vnr(current) || capable(CAP_SYS_ADMIN)) &&
50b6dff3ecSDavid Howells 	    ((creds->uid == cred->uid   || creds->uid == cred->euid ||
51b6dff3ecSDavid Howells 	      creds->uid == cred->suid) || capable(CAP_SETUID)) &&
52b6dff3ecSDavid Howells 	    ((creds->gid == cred->gid   || creds->gid == cred->egid ||
53b6dff3ecSDavid Howells 	      creds->gid == cred->sgid) || capable(CAP_SETGID))) {
541da177e4SLinus Torvalds 	       return 0;
551da177e4SLinus Torvalds 	}
561da177e4SLinus Torvalds 	return -EPERM;
571da177e4SLinus Torvalds }
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds static int scm_fp_copy(struct cmsghdr *cmsg, struct scm_fp_list **fplp)
601da177e4SLinus Torvalds {
611da177e4SLinus Torvalds 	int *fdp = (int*)CMSG_DATA(cmsg);
621da177e4SLinus Torvalds 	struct scm_fp_list *fpl = *fplp;
631da177e4SLinus Torvalds 	struct file **fpp;
641da177e4SLinus Torvalds 	int i, num;
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds 	num = (cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr)))/sizeof(int);
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds 	if (num <= 0)
691da177e4SLinus Torvalds 		return 0;
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds 	if (num > SCM_MAX_FD)
721da177e4SLinus Torvalds 		return -EINVAL;
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds 	if (!fpl)
751da177e4SLinus Torvalds 	{
761da177e4SLinus Torvalds 		fpl = kmalloc(sizeof(struct scm_fp_list), GFP_KERNEL);
771da177e4SLinus Torvalds 		if (!fpl)
781da177e4SLinus Torvalds 			return -ENOMEM;
791da177e4SLinus Torvalds 		*fplp = fpl;
801da177e4SLinus Torvalds 		fpl->count = 0;
81bba14de9SEric Dumazet 		fpl->max = SCM_MAX_FD;
821da177e4SLinus Torvalds 	}
831da177e4SLinus Torvalds 	fpp = &fpl->fp[fpl->count];
841da177e4SLinus Torvalds 
85bba14de9SEric Dumazet 	if (fpl->count + num > fpl->max)
861da177e4SLinus Torvalds 		return -EINVAL;
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds 	/*
891da177e4SLinus Torvalds 	 *	Verify the descriptors and increment the usage count.
901da177e4SLinus Torvalds 	 */
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds 	for (i=0; i< num; i++)
931da177e4SLinus Torvalds 	{
941da177e4SLinus Torvalds 		int fd = fdp[i];
951da177e4SLinus Torvalds 		struct file *file;
961da177e4SLinus Torvalds 
97326be7b4SAl Viro 		if (fd < 0 || !(file = fget_raw(fd)))
981da177e4SLinus Torvalds 			return -EBADF;
991da177e4SLinus Torvalds 		*fpp++ = file;
1001da177e4SLinus Torvalds 		fpl->count++;
1011da177e4SLinus Torvalds 	}
1021da177e4SLinus Torvalds 	return num;
1031da177e4SLinus Torvalds }
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds void __scm_destroy(struct scm_cookie *scm)
1061da177e4SLinus Torvalds {
1071da177e4SLinus Torvalds 	struct scm_fp_list *fpl = scm->fp;
1081da177e4SLinus Torvalds 	int i;
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds 	if (fpl) {
1111da177e4SLinus Torvalds 		scm->fp = NULL;
1121da177e4SLinus Torvalds 		for (i=fpl->count-1; i>=0; i--)
1131da177e4SLinus Torvalds 			fput(fpl->fp[i]);
1141da177e4SLinus Torvalds 		kfree(fpl);
1151da177e4SLinus Torvalds 	}
1161da177e4SLinus Torvalds }
1179e34a5b5SEric Dumazet EXPORT_SYMBOL(__scm_destroy);
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)
1201da177e4SLinus Torvalds {
1211da177e4SLinus Torvalds 	struct cmsghdr *cmsg;
1221da177e4SLinus Torvalds 	int err;
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 	for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg))
1251da177e4SLinus Torvalds 	{
1261da177e4SLinus Torvalds 		err = -EINVAL;
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds 		/* Verify that cmsg_len is at least sizeof(struct cmsghdr) */
1291da177e4SLinus Torvalds 		/* The first check was omitted in <= 2.2.5. The reasoning was
1301da177e4SLinus Torvalds 		   that parser checks cmsg_len in any case, so that
1311da177e4SLinus Torvalds 		   additional check would be work duplication.
1321da177e4SLinus Torvalds 		   But if cmsg_level is not SOL_SOCKET, we do not check
1331da177e4SLinus Torvalds 		   for too short ancillary data object at all! Oops.
1341da177e4SLinus Torvalds 		   OK, let's add it...
1351da177e4SLinus Torvalds 		 */
1361da177e4SLinus Torvalds 		if (!CMSG_OK(msg, cmsg))
1371da177e4SLinus Torvalds 			goto error;
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 		if (cmsg->cmsg_level != SOL_SOCKET)
1401da177e4SLinus Torvalds 			continue;
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 		switch (cmsg->cmsg_type)
1431da177e4SLinus Torvalds 		{
1441da177e4SLinus Torvalds 		case SCM_RIGHTS:
14576dadd76SEric W. Biederman 			if (!sock->ops || sock->ops->family != PF_UNIX)
14676dadd76SEric W. Biederman 				goto error;
1471da177e4SLinus Torvalds 			err=scm_fp_copy(cmsg, &p->fp);
1481da177e4SLinus Torvalds 			if (err<0)
1491da177e4SLinus Torvalds 				goto error;
1501da177e4SLinus Torvalds 			break;
1511da177e4SLinus Torvalds 		case SCM_CREDENTIALS:
1521da177e4SLinus Torvalds 			if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct ucred)))
1531da177e4SLinus Torvalds 				goto error;
1541da177e4SLinus Torvalds 			memcpy(&p->creds, CMSG_DATA(cmsg), sizeof(struct ucred));
1551da177e4SLinus Torvalds 			err = scm_check_creds(&p->creds);
1561da177e4SLinus Torvalds 			if (err)
1571da177e4SLinus Torvalds 				goto error;
158257b5358SEric W. Biederman 
15916e57262SEric Dumazet 			if (!p->pid || pid_vnr(p->pid) != p->creds.pid) {
160257b5358SEric W. Biederman 				struct pid *pid;
161257b5358SEric W. Biederman 				err = -ESRCH;
162257b5358SEric W. Biederman 				pid = find_get_pid(p->creds.pid);
163257b5358SEric W. Biederman 				if (!pid)
164257b5358SEric W. Biederman 					goto error;
165257b5358SEric W. Biederman 				put_pid(p->pid);
166257b5358SEric W. Biederman 				p->pid = pid;
167257b5358SEric W. Biederman 			}
168257b5358SEric W. Biederman 
16916e57262SEric Dumazet 			if (!p->cred ||
17016e57262SEric Dumazet 			    (p->cred->euid != p->creds.uid) ||
171257b5358SEric W. Biederman 			    (p->cred->egid != p->creds.gid)) {
172257b5358SEric W. Biederman 				struct cred *cred;
173257b5358SEric W. Biederman 				err = -ENOMEM;
174257b5358SEric W. Biederman 				cred = prepare_creds();
175257b5358SEric W. Biederman 				if (!cred)
176257b5358SEric W. Biederman 					goto error;
177257b5358SEric W. Biederman 
178257b5358SEric W. Biederman 				cred->uid = cred->euid = p->creds.uid;
179e33f7a9fSTim Chen 				cred->gid = cred->egid = p->creds.gid;
18016e57262SEric Dumazet 				if (p->cred)
181257b5358SEric W. Biederman 					put_cred(p->cred);
182257b5358SEric W. Biederman 				p->cred = cred;
183257b5358SEric W. Biederman 			}
1841da177e4SLinus Torvalds 			break;
1851da177e4SLinus Torvalds 		default:
1861da177e4SLinus Torvalds 			goto error;
1871da177e4SLinus Torvalds 		}
1881da177e4SLinus Torvalds 	}
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds 	if (p->fp && !p->fp->count)
1911da177e4SLinus Torvalds 	{
1921da177e4SLinus Torvalds 		kfree(p->fp);
1931da177e4SLinus Torvalds 		p->fp = NULL;
1941da177e4SLinus Torvalds 	}
1951da177e4SLinus Torvalds 	return 0;
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds error:
1981da177e4SLinus Torvalds 	scm_destroy(p);
1991da177e4SLinus Torvalds 	return err;
2001da177e4SLinus Torvalds }
2019e34a5b5SEric Dumazet EXPORT_SYMBOL(__scm_send);
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds int put_cmsg(struct msghdr * msg, int level, int type, int len, void *data)
2041da177e4SLinus Torvalds {
205cfcabdccSStephen Hemminger 	struct cmsghdr __user *cm
206cfcabdccSStephen Hemminger 		= (__force struct cmsghdr __user *)msg->msg_control;
2071da177e4SLinus Torvalds 	struct cmsghdr cmhdr;
2081da177e4SLinus Torvalds 	int cmlen = CMSG_LEN(len);
2091da177e4SLinus Torvalds 	int err;
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	if (MSG_CMSG_COMPAT & msg->msg_flags)
2121da177e4SLinus Torvalds 		return put_cmsg_compat(msg, level, type, len, data);
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	if (cm==NULL || msg->msg_controllen < sizeof(*cm)) {
2151da177e4SLinus Torvalds 		msg->msg_flags |= MSG_CTRUNC;
2161da177e4SLinus Torvalds 		return 0; /* XXX: return error? check spec. */
2171da177e4SLinus Torvalds 	}
2181da177e4SLinus Torvalds 	if (msg->msg_controllen < cmlen) {
2191da177e4SLinus Torvalds 		msg->msg_flags |= MSG_CTRUNC;
2201da177e4SLinus Torvalds 		cmlen = msg->msg_controllen;
2211da177e4SLinus Torvalds 	}
2221da177e4SLinus Torvalds 	cmhdr.cmsg_level = level;
2231da177e4SLinus Torvalds 	cmhdr.cmsg_type = type;
2241da177e4SLinus Torvalds 	cmhdr.cmsg_len = cmlen;
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds 	err = -EFAULT;
2271da177e4SLinus Torvalds 	if (copy_to_user(cm, &cmhdr, sizeof cmhdr))
2281da177e4SLinus Torvalds 		goto out;
2291da177e4SLinus Torvalds 	if (copy_to_user(CMSG_DATA(cm), data, cmlen - sizeof(struct cmsghdr)))
2301da177e4SLinus Torvalds 		goto out;
2311da177e4SLinus Torvalds 	cmlen = CMSG_SPACE(len);
2321ac70e7aSWei Yongjun 	if (msg->msg_controllen < cmlen)
2331ac70e7aSWei Yongjun 		cmlen = msg->msg_controllen;
2341da177e4SLinus Torvalds 	msg->msg_control += cmlen;
2351da177e4SLinus Torvalds 	msg->msg_controllen -= cmlen;
2361da177e4SLinus Torvalds 	err = 0;
2371da177e4SLinus Torvalds out:
2381da177e4SLinus Torvalds 	return err;
2391da177e4SLinus Torvalds }
2409e34a5b5SEric Dumazet EXPORT_SYMBOL(put_cmsg);
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm)
2431da177e4SLinus Torvalds {
244cfcabdccSStephen Hemminger 	struct cmsghdr __user *cm
245cfcabdccSStephen Hemminger 		= (__force struct cmsghdr __user*)msg->msg_control;
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds 	int fdmax = 0;
2481da177e4SLinus Torvalds 	int fdnum = scm->fp->count;
2491da177e4SLinus Torvalds 	struct file **fp = scm->fp->fp;
2501da177e4SLinus Torvalds 	int __user *cmfptr;
2511da177e4SLinus Torvalds 	int err = 0, i;
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 	if (MSG_CMSG_COMPAT & msg->msg_flags) {
2541da177e4SLinus Torvalds 		scm_detach_fds_compat(msg, scm);
2551da177e4SLinus Torvalds 		return;
2561da177e4SLinus Torvalds 	}
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	if (msg->msg_controllen > sizeof(struct cmsghdr))
2591da177e4SLinus Torvalds 		fdmax = ((msg->msg_controllen - sizeof(struct cmsghdr))
2601da177e4SLinus Torvalds 			 / sizeof(int));
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	if (fdnum < fdmax)
2631da177e4SLinus Torvalds 		fdmax = fdnum;
2641da177e4SLinus Torvalds 
265cfcabdccSStephen Hemminger 	for (i=0, cmfptr=(__force int __user *)CMSG_DATA(cm); i<fdmax;
266cfcabdccSStephen Hemminger 	     i++, cmfptr++)
2671da177e4SLinus Torvalds 	{
268*48a87cc2SJohn Fastabend 		struct socket *sock;
2691da177e4SLinus Torvalds 		int new_fd;
2701da177e4SLinus Torvalds 		err = security_file_receive(fp[i]);
2711da177e4SLinus Torvalds 		if (err)
2721da177e4SLinus Torvalds 			break;
2734a19542eSUlrich Drepper 		err = get_unused_fd_flags(MSG_CMSG_CLOEXEC & msg->msg_flags
2744a19542eSUlrich Drepper 					  ? O_CLOEXEC : 0);
2751da177e4SLinus Torvalds 		if (err < 0)
2761da177e4SLinus Torvalds 			break;
2771da177e4SLinus Torvalds 		new_fd = err;
2781da177e4SLinus Torvalds 		err = put_user(new_fd, cmfptr);
2791da177e4SLinus Torvalds 		if (err) {
2801da177e4SLinus Torvalds 			put_unused_fd(new_fd);
2811da177e4SLinus Torvalds 			break;
2821da177e4SLinus Torvalds 		}
2831da177e4SLinus Torvalds 		/* Bump the usage count and install the file. */
2841da177e4SLinus Torvalds 		get_file(fp[i]);
285*48a87cc2SJohn Fastabend 		sock = sock_from_file(fp[i], &err);
286*48a87cc2SJohn Fastabend 		if (sock)
287*48a87cc2SJohn Fastabend 			sock_update_netprioidx(sock->sk, current);
2881da177e4SLinus Torvalds 		fd_install(new_fd, fp[i]);
2891da177e4SLinus Torvalds 	}
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	if (i > 0)
2921da177e4SLinus Torvalds 	{
2931da177e4SLinus Torvalds 		int cmlen = CMSG_LEN(i*sizeof(int));
2941da177e4SLinus Torvalds 		err = put_user(SOL_SOCKET, &cm->cmsg_level);
2951da177e4SLinus Torvalds 		if (!err)
2961da177e4SLinus Torvalds 			err = put_user(SCM_RIGHTS, &cm->cmsg_type);
2971da177e4SLinus Torvalds 		if (!err)
2981da177e4SLinus Torvalds 			err = put_user(cmlen, &cm->cmsg_len);
2991da177e4SLinus Torvalds 		if (!err) {
3001da177e4SLinus Torvalds 			cmlen = CMSG_SPACE(i*sizeof(int));
3011da177e4SLinus Torvalds 			msg->msg_control += cmlen;
3021da177e4SLinus Torvalds 			msg->msg_controllen -= cmlen;
3031da177e4SLinus Torvalds 		}
3041da177e4SLinus Torvalds 	}
3051da177e4SLinus Torvalds 	if (i < fdnum || (fdnum && fdmax <= 0))
3061da177e4SLinus Torvalds 		msg->msg_flags |= MSG_CTRUNC;
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	/*
3091da177e4SLinus Torvalds 	 * All of the files that fit in the message have had their
3101da177e4SLinus Torvalds 	 * usage counts incremented, so we just free the list.
3111da177e4SLinus Torvalds 	 */
3121da177e4SLinus Torvalds 	__scm_destroy(scm);
3131da177e4SLinus Torvalds }
3149e34a5b5SEric Dumazet EXPORT_SYMBOL(scm_detach_fds);
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl)
3171da177e4SLinus Torvalds {
3181da177e4SLinus Torvalds 	struct scm_fp_list *new_fpl;
3191da177e4SLinus Torvalds 	int i;
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	if (!fpl)
3221da177e4SLinus Torvalds 		return NULL;
3231da177e4SLinus Torvalds 
324bba14de9SEric Dumazet 	new_fpl = kmemdup(fpl, offsetof(struct scm_fp_list, fp[fpl->count]),
325bba14de9SEric Dumazet 			  GFP_KERNEL);
3261da177e4SLinus Torvalds 	if (new_fpl) {
327bba14de9SEric Dumazet 		for (i = 0; i < fpl->count; i++)
3281da177e4SLinus Torvalds 			get_file(fpl->fp[i]);
329bba14de9SEric Dumazet 		new_fpl->max = new_fpl->count;
3301da177e4SLinus Torvalds 	}
3311da177e4SLinus Torvalds 	return new_fpl;
3321da177e4SLinus Torvalds }
3331da177e4SLinus Torvalds EXPORT_SYMBOL(scm_fp_dup);
334