xref: /openbmc/linux/net/core/scm.c (revision 5a0e3ad6af8660be21ca98a971cd00f331318c05)
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>
29*5a0e3ad6STejun Heo #include <linux/slab.h>
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds #include <asm/system.h>
321da177e4SLinus Torvalds #include <asm/uaccess.h>
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds #include <net/protocol.h>
351da177e4SLinus Torvalds #include <linux/skbuff.h>
361da177e4SLinus Torvalds #include <net/sock.h>
371da177e4SLinus Torvalds #include <net/compat.h>
381da177e4SLinus Torvalds #include <net/scm.h>
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds /*
421da177e4SLinus Torvalds  *	Only allow a user to send credentials, that they could set with
431da177e4SLinus Torvalds  *	setu(g)id.
441da177e4SLinus Torvalds  */
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds static __inline__ int scm_check_creds(struct ucred *creds)
471da177e4SLinus Torvalds {
4886a264abSDavid Howells 	const struct cred *cred = current_cred();
49b6dff3ecSDavid Howells 
50b488893aSPavel Emelyanov 	if ((creds->pid == task_tgid_vnr(current) || capable(CAP_SYS_ADMIN)) &&
51b6dff3ecSDavid Howells 	    ((creds->uid == cred->uid   || creds->uid == cred->euid ||
52b6dff3ecSDavid Howells 	      creds->uid == cred->suid) || capable(CAP_SETUID)) &&
53b6dff3ecSDavid Howells 	    ((creds->gid == cred->gid   || creds->gid == cred->egid ||
54b6dff3ecSDavid Howells 	      creds->gid == cred->sgid) || capable(CAP_SETGID))) {
551da177e4SLinus Torvalds 	       return 0;
561da177e4SLinus Torvalds 	}
571da177e4SLinus Torvalds 	return -EPERM;
581da177e4SLinus Torvalds }
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds static int scm_fp_copy(struct cmsghdr *cmsg, struct scm_fp_list **fplp)
611da177e4SLinus Torvalds {
621da177e4SLinus Torvalds 	int *fdp = (int*)CMSG_DATA(cmsg);
631da177e4SLinus Torvalds 	struct scm_fp_list *fpl = *fplp;
641da177e4SLinus Torvalds 	struct file **fpp;
651da177e4SLinus Torvalds 	int i, num;
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds 	num = (cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr)))/sizeof(int);
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds 	if (num <= 0)
701da177e4SLinus Torvalds 		return 0;
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds 	if (num > SCM_MAX_FD)
731da177e4SLinus Torvalds 		return -EINVAL;
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds 	if (!fpl)
761da177e4SLinus Torvalds 	{
771da177e4SLinus Torvalds 		fpl = kmalloc(sizeof(struct scm_fp_list), GFP_KERNEL);
781da177e4SLinus Torvalds 		if (!fpl)
791da177e4SLinus Torvalds 			return -ENOMEM;
801da177e4SLinus Torvalds 		*fplp = fpl;
811da177e4SLinus Torvalds 		fpl->count = 0;
821da177e4SLinus Torvalds 	}
831da177e4SLinus Torvalds 	fpp = &fpl->fp[fpl->count];
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds 	if (fpl->count + num > SCM_MAX_FD)
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 
971da177e4SLinus Torvalds 		if (fd < 0 || !(file = fget(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;
112f8d570a4SDavid Miller 		if (current->scm_work_list) {
113f8d570a4SDavid Miller 			list_add_tail(&fpl->list, current->scm_work_list);
114f8d570a4SDavid Miller 		} else {
115f8d570a4SDavid Miller 			LIST_HEAD(work_list);
116f8d570a4SDavid Miller 
117f8d570a4SDavid Miller 			current->scm_work_list = &work_list;
118f8d570a4SDavid Miller 
119f8d570a4SDavid Miller 			list_add(&fpl->list, &work_list);
120f8d570a4SDavid Miller 			while (!list_empty(&work_list)) {
121f8d570a4SDavid Miller 				fpl = list_first_entry(&work_list, struct scm_fp_list, list);
122f8d570a4SDavid Miller 
123f8d570a4SDavid Miller 				list_del(&fpl->list);
1241da177e4SLinus Torvalds 				for (i=fpl->count-1; i>=0; i--)
1251da177e4SLinus Torvalds 					fput(fpl->fp[i]);
1261da177e4SLinus Torvalds 				kfree(fpl);
1271da177e4SLinus Torvalds 			}
128f8d570a4SDavid Miller 
129f8d570a4SDavid Miller 			current->scm_work_list = NULL;
130f8d570a4SDavid Miller 		}
131f8d570a4SDavid Miller 	}
1321da177e4SLinus Torvalds }
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)
1351da177e4SLinus Torvalds {
1361da177e4SLinus Torvalds 	struct cmsghdr *cmsg;
1371da177e4SLinus Torvalds 	int err;
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 	for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg))
1401da177e4SLinus Torvalds 	{
1411da177e4SLinus Torvalds 		err = -EINVAL;
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 		/* Verify that cmsg_len is at least sizeof(struct cmsghdr) */
1441da177e4SLinus Torvalds 		/* The first check was omitted in <= 2.2.5. The reasoning was
1451da177e4SLinus Torvalds 		   that parser checks cmsg_len in any case, so that
1461da177e4SLinus Torvalds 		   additional check would be work duplication.
1471da177e4SLinus Torvalds 		   But if cmsg_level is not SOL_SOCKET, we do not check
1481da177e4SLinus Torvalds 		   for too short ancillary data object at all! Oops.
1491da177e4SLinus Torvalds 		   OK, let's add it...
1501da177e4SLinus Torvalds 		 */
1511da177e4SLinus Torvalds 		if (!CMSG_OK(msg, cmsg))
1521da177e4SLinus Torvalds 			goto error;
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 		if (cmsg->cmsg_level != SOL_SOCKET)
1551da177e4SLinus Torvalds 			continue;
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 		switch (cmsg->cmsg_type)
1581da177e4SLinus Torvalds 		{
1591da177e4SLinus Torvalds 		case SCM_RIGHTS:
16076dadd76SEric W. Biederman 			if (!sock->ops || sock->ops->family != PF_UNIX)
16176dadd76SEric W. Biederman 				goto error;
1621da177e4SLinus Torvalds 			err=scm_fp_copy(cmsg, &p->fp);
1631da177e4SLinus Torvalds 			if (err<0)
1641da177e4SLinus Torvalds 				goto error;
1651da177e4SLinus Torvalds 			break;
1661da177e4SLinus Torvalds 		case SCM_CREDENTIALS:
1671da177e4SLinus Torvalds 			if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct ucred)))
1681da177e4SLinus Torvalds 				goto error;
1691da177e4SLinus Torvalds 			memcpy(&p->creds, CMSG_DATA(cmsg), sizeof(struct ucred));
1701da177e4SLinus Torvalds 			err = scm_check_creds(&p->creds);
1711da177e4SLinus Torvalds 			if (err)
1721da177e4SLinus Torvalds 				goto error;
1731da177e4SLinus Torvalds 			break;
1741da177e4SLinus Torvalds 		default:
1751da177e4SLinus Torvalds 			goto error;
1761da177e4SLinus Torvalds 		}
1771da177e4SLinus Torvalds 	}
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	if (p->fp && !p->fp->count)
1801da177e4SLinus Torvalds 	{
1811da177e4SLinus Torvalds 		kfree(p->fp);
1821da177e4SLinus Torvalds 		p->fp = NULL;
1831da177e4SLinus Torvalds 	}
1841da177e4SLinus Torvalds 	return 0;
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds error:
1871da177e4SLinus Torvalds 	scm_destroy(p);
1881da177e4SLinus Torvalds 	return err;
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds int put_cmsg(struct msghdr * msg, int level, int type, int len, void *data)
1921da177e4SLinus Torvalds {
193cfcabdccSStephen Hemminger 	struct cmsghdr __user *cm
194cfcabdccSStephen Hemminger 		= (__force struct cmsghdr __user *)msg->msg_control;
1951da177e4SLinus Torvalds 	struct cmsghdr cmhdr;
1961da177e4SLinus Torvalds 	int cmlen = CMSG_LEN(len);
1971da177e4SLinus Torvalds 	int err;
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 	if (MSG_CMSG_COMPAT & msg->msg_flags)
2001da177e4SLinus Torvalds 		return put_cmsg_compat(msg, level, type, len, data);
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	if (cm==NULL || msg->msg_controllen < sizeof(*cm)) {
2031da177e4SLinus Torvalds 		msg->msg_flags |= MSG_CTRUNC;
2041da177e4SLinus Torvalds 		return 0; /* XXX: return error? check spec. */
2051da177e4SLinus Torvalds 	}
2061da177e4SLinus Torvalds 	if (msg->msg_controllen < cmlen) {
2071da177e4SLinus Torvalds 		msg->msg_flags |= MSG_CTRUNC;
2081da177e4SLinus Torvalds 		cmlen = msg->msg_controllen;
2091da177e4SLinus Torvalds 	}
2101da177e4SLinus Torvalds 	cmhdr.cmsg_level = level;
2111da177e4SLinus Torvalds 	cmhdr.cmsg_type = type;
2121da177e4SLinus Torvalds 	cmhdr.cmsg_len = cmlen;
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	err = -EFAULT;
2151da177e4SLinus Torvalds 	if (copy_to_user(cm, &cmhdr, sizeof cmhdr))
2161da177e4SLinus Torvalds 		goto out;
2171da177e4SLinus Torvalds 	if (copy_to_user(CMSG_DATA(cm), data, cmlen - sizeof(struct cmsghdr)))
2181da177e4SLinus Torvalds 		goto out;
2191da177e4SLinus Torvalds 	cmlen = CMSG_SPACE(len);
2201ac70e7aSWei Yongjun 	if (msg->msg_controllen < cmlen)
2211ac70e7aSWei Yongjun 		cmlen = msg->msg_controllen;
2221da177e4SLinus Torvalds 	msg->msg_control += cmlen;
2231da177e4SLinus Torvalds 	msg->msg_controllen -= cmlen;
2241da177e4SLinus Torvalds 	err = 0;
2251da177e4SLinus Torvalds out:
2261da177e4SLinus Torvalds 	return err;
2271da177e4SLinus Torvalds }
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm)
2301da177e4SLinus Torvalds {
231cfcabdccSStephen Hemminger 	struct cmsghdr __user *cm
232cfcabdccSStephen Hemminger 		= (__force struct cmsghdr __user*)msg->msg_control;
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 	int fdmax = 0;
2351da177e4SLinus Torvalds 	int fdnum = scm->fp->count;
2361da177e4SLinus Torvalds 	struct file **fp = scm->fp->fp;
2371da177e4SLinus Torvalds 	int __user *cmfptr;
2381da177e4SLinus Torvalds 	int err = 0, i;
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	if (MSG_CMSG_COMPAT & msg->msg_flags) {
2411da177e4SLinus Torvalds 		scm_detach_fds_compat(msg, scm);
2421da177e4SLinus Torvalds 		return;
2431da177e4SLinus Torvalds 	}
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds 	if (msg->msg_controllen > sizeof(struct cmsghdr))
2461da177e4SLinus Torvalds 		fdmax = ((msg->msg_controllen - sizeof(struct cmsghdr))
2471da177e4SLinus Torvalds 			 / sizeof(int));
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 	if (fdnum < fdmax)
2501da177e4SLinus Torvalds 		fdmax = fdnum;
2511da177e4SLinus Torvalds 
252cfcabdccSStephen Hemminger 	for (i=0, cmfptr=(__force int __user *)CMSG_DATA(cm); i<fdmax;
253cfcabdccSStephen Hemminger 	     i++, cmfptr++)
2541da177e4SLinus Torvalds 	{
2551da177e4SLinus Torvalds 		int new_fd;
2561da177e4SLinus Torvalds 		err = security_file_receive(fp[i]);
2571da177e4SLinus Torvalds 		if (err)
2581da177e4SLinus Torvalds 			break;
2594a19542eSUlrich Drepper 		err = get_unused_fd_flags(MSG_CMSG_CLOEXEC & msg->msg_flags
2604a19542eSUlrich Drepper 					  ? O_CLOEXEC : 0);
2611da177e4SLinus Torvalds 		if (err < 0)
2621da177e4SLinus Torvalds 			break;
2631da177e4SLinus Torvalds 		new_fd = err;
2641da177e4SLinus Torvalds 		err = put_user(new_fd, cmfptr);
2651da177e4SLinus Torvalds 		if (err) {
2661da177e4SLinus Torvalds 			put_unused_fd(new_fd);
2671da177e4SLinus Torvalds 			break;
2681da177e4SLinus Torvalds 		}
2691da177e4SLinus Torvalds 		/* Bump the usage count and install the file. */
2701da177e4SLinus Torvalds 		get_file(fp[i]);
2711da177e4SLinus Torvalds 		fd_install(new_fd, fp[i]);
2721da177e4SLinus Torvalds 	}
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	if (i > 0)
2751da177e4SLinus Torvalds 	{
2761da177e4SLinus Torvalds 		int cmlen = CMSG_LEN(i*sizeof(int));
2771da177e4SLinus Torvalds 		err = put_user(SOL_SOCKET, &cm->cmsg_level);
2781da177e4SLinus Torvalds 		if (!err)
2791da177e4SLinus Torvalds 			err = put_user(SCM_RIGHTS, &cm->cmsg_type);
2801da177e4SLinus Torvalds 		if (!err)
2811da177e4SLinus Torvalds 			err = put_user(cmlen, &cm->cmsg_len);
2821da177e4SLinus Torvalds 		if (!err) {
2831da177e4SLinus Torvalds 			cmlen = CMSG_SPACE(i*sizeof(int));
2841da177e4SLinus Torvalds 			msg->msg_control += cmlen;
2851da177e4SLinus Torvalds 			msg->msg_controllen -= cmlen;
2861da177e4SLinus Torvalds 		}
2871da177e4SLinus Torvalds 	}
2881da177e4SLinus Torvalds 	if (i < fdnum || (fdnum && fdmax <= 0))
2891da177e4SLinus Torvalds 		msg->msg_flags |= MSG_CTRUNC;
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	/*
2921da177e4SLinus Torvalds 	 * All of the files that fit in the message have had their
2931da177e4SLinus Torvalds 	 * usage counts incremented, so we just free the list.
2941da177e4SLinus Torvalds 	 */
2951da177e4SLinus Torvalds 	__scm_destroy(scm);
2961da177e4SLinus Torvalds }
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl)
2991da177e4SLinus Torvalds {
3001da177e4SLinus Torvalds 	struct scm_fp_list *new_fpl;
3011da177e4SLinus Torvalds 	int i;
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	if (!fpl)
3041da177e4SLinus Torvalds 		return NULL;
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	new_fpl = kmalloc(sizeof(*fpl), GFP_KERNEL);
3071da177e4SLinus Torvalds 	if (new_fpl) {
3081da177e4SLinus Torvalds 		for (i=fpl->count-1; i>=0; i--)
3091da177e4SLinus Torvalds 			get_file(fpl->fp[i]);
3101da177e4SLinus Torvalds 		memcpy(new_fpl, fpl, sizeof(*fpl));
3111da177e4SLinus Torvalds 	}
3121da177e4SLinus Torvalds 	return new_fpl;
3131da177e4SLinus Torvalds }
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds EXPORT_SYMBOL(__scm_destroy);
3161da177e4SLinus Torvalds EXPORT_SYMBOL(__scm_send);
3171da177e4SLinus Torvalds EXPORT_SYMBOL(put_cmsg);
3181da177e4SLinus Torvalds EXPORT_SYMBOL(scm_detach_fds);
3191da177e4SLinus Torvalds EXPORT_SYMBOL(scm_fp_dup);
320