xref: /linux/net/core/scm.c (revision 6ffd46410248ee39b46c2cdafb79791c2e618932)
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>
2792f28d97SEric W. Biederman #include <linux/pid_namespace.h>
28b488893aSPavel Emelyanov #include <linux/pid.h>
29b488893aSPavel Emelyanov #include <linux/nsproxy.h>
305a0e3ad6STejun Heo #include <linux/slab.h>
311da177e4SLinus Torvalds 
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>
39d8429506SDaniel Wagner #include <net/cls_cgroup.h>
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds /*
431da177e4SLinus Torvalds  *	Only allow a user to send credentials, that they could set with
441da177e4SLinus Torvalds  *	setu(g)id.
451da177e4SLinus Torvalds  */
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds static __inline__ int scm_check_creds(struct ucred *creds)
481da177e4SLinus Torvalds {
4986a264abSDavid Howells 	const struct cred *cred = current_cred();
50b2e4f544SEric W. Biederman 	kuid_t uid = make_kuid(cred->user_ns, creds->uid);
51b2e4f544SEric W. Biederman 	kgid_t gid = make_kgid(cred->user_ns, creds->gid);
52b2e4f544SEric W. Biederman 
53b2e4f544SEric W. Biederman 	if (!uid_valid(uid) || !gid_valid(gid))
54b2e4f544SEric W. Biederman 		return -EINVAL;
55b6dff3ecSDavid Howells 
5692f28d97SEric W. Biederman 	if ((creds->pid == task_tgid_vnr(current) ||
5792f28d97SEric W. Biederman 	     ns_capable(current->nsproxy->pid_ns->user_ns, CAP_SYS_ADMIN)) &&
58b2e4f544SEric W. Biederman 	    ((uid_eq(uid, cred->uid)   || uid_eq(uid, cred->euid) ||
5900f70de0SEric W. Biederman 	      uid_eq(uid, cred->suid)) || nsown_capable(CAP_SETUID)) &&
60b2e4f544SEric W. Biederman 	    ((gid_eq(gid, cred->gid)   || gid_eq(gid, cred->egid) ||
6100f70de0SEric W. Biederman 	      gid_eq(gid, cred->sgid)) || nsown_capable(CAP_SETGID))) {
621da177e4SLinus Torvalds 	       return 0;
631da177e4SLinus Torvalds 	}
641da177e4SLinus Torvalds 	return -EPERM;
651da177e4SLinus Torvalds }
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds static int scm_fp_copy(struct cmsghdr *cmsg, struct scm_fp_list **fplp)
681da177e4SLinus Torvalds {
691da177e4SLinus Torvalds 	int *fdp = (int*)CMSG_DATA(cmsg);
701da177e4SLinus Torvalds 	struct scm_fp_list *fpl = *fplp;
711da177e4SLinus Torvalds 	struct file **fpp;
721da177e4SLinus Torvalds 	int i, num;
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds 	num = (cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr)))/sizeof(int);
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds 	if (num <= 0)
771da177e4SLinus Torvalds 		return 0;
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds 	if (num > SCM_MAX_FD)
801da177e4SLinus Torvalds 		return -EINVAL;
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds 	if (!fpl)
831da177e4SLinus Torvalds 	{
841da177e4SLinus Torvalds 		fpl = kmalloc(sizeof(struct scm_fp_list), GFP_KERNEL);
851da177e4SLinus Torvalds 		if (!fpl)
861da177e4SLinus Torvalds 			return -ENOMEM;
871da177e4SLinus Torvalds 		*fplp = fpl;
881da177e4SLinus Torvalds 		fpl->count = 0;
89bba14de9SEric Dumazet 		fpl->max = SCM_MAX_FD;
901da177e4SLinus Torvalds 	}
911da177e4SLinus Torvalds 	fpp = &fpl->fp[fpl->count];
921da177e4SLinus Torvalds 
93bba14de9SEric Dumazet 	if (fpl->count + num > fpl->max)
941da177e4SLinus Torvalds 		return -EINVAL;
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds 	/*
971da177e4SLinus Torvalds 	 *	Verify the descriptors and increment the usage count.
981da177e4SLinus Torvalds 	 */
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds 	for (i=0; i< num; i++)
1011da177e4SLinus Torvalds 	{
1021da177e4SLinus Torvalds 		int fd = fdp[i];
1031da177e4SLinus Torvalds 		struct file *file;
1041da177e4SLinus Torvalds 
105326be7b4SAl Viro 		if (fd < 0 || !(file = fget_raw(fd)))
1061da177e4SLinus Torvalds 			return -EBADF;
1071da177e4SLinus Torvalds 		*fpp++ = file;
1081da177e4SLinus Torvalds 		fpl->count++;
1091da177e4SLinus Torvalds 	}
1101da177e4SLinus Torvalds 	return num;
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds void __scm_destroy(struct scm_cookie *scm)
1141da177e4SLinus Torvalds {
1151da177e4SLinus Torvalds 	struct scm_fp_list *fpl = scm->fp;
1161da177e4SLinus Torvalds 	int i;
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 	if (fpl) {
1191da177e4SLinus Torvalds 		scm->fp = NULL;
1201da177e4SLinus Torvalds 		for (i=fpl->count-1; i>=0; i--)
1211da177e4SLinus Torvalds 			fput(fpl->fp[i]);
1221da177e4SLinus Torvalds 		kfree(fpl);
1231da177e4SLinus Torvalds 	}
1241da177e4SLinus Torvalds }
1259e34a5b5SEric Dumazet EXPORT_SYMBOL(__scm_destroy);
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)
1281da177e4SLinus Torvalds {
1291da177e4SLinus Torvalds 	struct cmsghdr *cmsg;
1301da177e4SLinus Torvalds 	int err;
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 	for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg))
1331da177e4SLinus Torvalds 	{
1341da177e4SLinus Torvalds 		err = -EINVAL;
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 		/* Verify that cmsg_len is at least sizeof(struct cmsghdr) */
1371da177e4SLinus Torvalds 		/* The first check was omitted in <= 2.2.5. The reasoning was
1381da177e4SLinus Torvalds 		   that parser checks cmsg_len in any case, so that
1391da177e4SLinus Torvalds 		   additional check would be work duplication.
1401da177e4SLinus Torvalds 		   But if cmsg_level is not SOL_SOCKET, we do not check
1411da177e4SLinus Torvalds 		   for too short ancillary data object at all! Oops.
1421da177e4SLinus Torvalds 		   OK, let's add it...
1431da177e4SLinus Torvalds 		 */
1441da177e4SLinus Torvalds 		if (!CMSG_OK(msg, cmsg))
1451da177e4SLinus Torvalds 			goto error;
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds 		if (cmsg->cmsg_level != SOL_SOCKET)
1481da177e4SLinus Torvalds 			continue;
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds 		switch (cmsg->cmsg_type)
1511da177e4SLinus Torvalds 		{
1521da177e4SLinus Torvalds 		case SCM_RIGHTS:
15376dadd76SEric W. Biederman 			if (!sock->ops || sock->ops->family != PF_UNIX)
15476dadd76SEric W. Biederman 				goto error;
1551da177e4SLinus Torvalds 			err=scm_fp_copy(cmsg, &p->fp);
1561da177e4SLinus Torvalds 			if (err<0)
1571da177e4SLinus Torvalds 				goto error;
1581da177e4SLinus Torvalds 			break;
1591da177e4SLinus Torvalds 		case SCM_CREDENTIALS:
160b2e4f544SEric W. Biederman 		{
161dbe9a417SEric W. Biederman 			struct ucred creds;
162b2e4f544SEric W. Biederman 			kuid_t uid;
163b2e4f544SEric W. Biederman 			kgid_t gid;
1641da177e4SLinus Torvalds 			if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct ucred)))
1651da177e4SLinus Torvalds 				goto error;
166dbe9a417SEric W. Biederman 			memcpy(&creds, CMSG_DATA(cmsg), sizeof(struct ucred));
167dbe9a417SEric W. Biederman 			err = scm_check_creds(&creds);
1681da177e4SLinus Torvalds 			if (err)
1691da177e4SLinus Torvalds 				goto error;
170257b5358SEric W. Biederman 
171dbe9a417SEric W. Biederman 			p->creds.pid = creds.pid;
172dbe9a417SEric W. Biederman 			if (!p->pid || pid_vnr(p->pid) != creds.pid) {
173257b5358SEric W. Biederman 				struct pid *pid;
174257b5358SEric W. Biederman 				err = -ESRCH;
175dbe9a417SEric W. Biederman 				pid = find_get_pid(creds.pid);
176257b5358SEric W. Biederman 				if (!pid)
177257b5358SEric W. Biederman 					goto error;
178257b5358SEric W. Biederman 				put_pid(p->pid);
179257b5358SEric W. Biederman 				p->pid = pid;
180257b5358SEric W. Biederman 			}
181257b5358SEric W. Biederman 
182b2e4f544SEric W. Biederman 			err = -EINVAL;
183dbe9a417SEric W. Biederman 			uid = make_kuid(current_user_ns(), creds.uid);
184dbe9a417SEric W. Biederman 			gid = make_kgid(current_user_ns(), creds.gid);
185b2e4f544SEric W. Biederman 			if (!uid_valid(uid) || !gid_valid(gid))
186b2e4f544SEric W. Biederman 				goto error;
187b2e4f544SEric W. Biederman 
188dbe9a417SEric W. Biederman 			p->creds.uid = uid;
189dbe9a417SEric W. Biederman 			p->creds.gid = gid;
1901da177e4SLinus Torvalds 			break;
191b2e4f544SEric W. Biederman 		}
1921da177e4SLinus Torvalds 		default:
1931da177e4SLinus Torvalds 			goto error;
1941da177e4SLinus Torvalds 		}
1951da177e4SLinus Torvalds 	}
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	if (p->fp && !p->fp->count)
1981da177e4SLinus Torvalds 	{
1991da177e4SLinus Torvalds 		kfree(p->fp);
2001da177e4SLinus Torvalds 		p->fp = NULL;
2011da177e4SLinus Torvalds 	}
2021da177e4SLinus Torvalds 	return 0;
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds error:
2051da177e4SLinus Torvalds 	scm_destroy(p);
2061da177e4SLinus Torvalds 	return err;
2071da177e4SLinus Torvalds }
2089e34a5b5SEric Dumazet EXPORT_SYMBOL(__scm_send);
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds int put_cmsg(struct msghdr * msg, int level, int type, int len, void *data)
2111da177e4SLinus Torvalds {
212cfcabdccSStephen Hemminger 	struct cmsghdr __user *cm
213cfcabdccSStephen Hemminger 		= (__force struct cmsghdr __user *)msg->msg_control;
2141da177e4SLinus Torvalds 	struct cmsghdr cmhdr;
2151da177e4SLinus Torvalds 	int cmlen = CMSG_LEN(len);
2161da177e4SLinus Torvalds 	int err;
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 	if (MSG_CMSG_COMPAT & msg->msg_flags)
2191da177e4SLinus Torvalds 		return put_cmsg_compat(msg, level, type, len, data);
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	if (cm==NULL || msg->msg_controllen < sizeof(*cm)) {
2221da177e4SLinus Torvalds 		msg->msg_flags |= MSG_CTRUNC;
2231da177e4SLinus Torvalds 		return 0; /* XXX: return error? check spec. */
2241da177e4SLinus Torvalds 	}
2251da177e4SLinus Torvalds 	if (msg->msg_controllen < cmlen) {
2261da177e4SLinus Torvalds 		msg->msg_flags |= MSG_CTRUNC;
2271da177e4SLinus Torvalds 		cmlen = msg->msg_controllen;
2281da177e4SLinus Torvalds 	}
2291da177e4SLinus Torvalds 	cmhdr.cmsg_level = level;
2301da177e4SLinus Torvalds 	cmhdr.cmsg_type = type;
2311da177e4SLinus Torvalds 	cmhdr.cmsg_len = cmlen;
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 	err = -EFAULT;
2341da177e4SLinus Torvalds 	if (copy_to_user(cm, &cmhdr, sizeof cmhdr))
2351da177e4SLinus Torvalds 		goto out;
2361da177e4SLinus Torvalds 	if (copy_to_user(CMSG_DATA(cm), data, cmlen - sizeof(struct cmsghdr)))
2371da177e4SLinus Torvalds 		goto out;
2381da177e4SLinus Torvalds 	cmlen = CMSG_SPACE(len);
2391ac70e7aSWei Yongjun 	if (msg->msg_controllen < cmlen)
2401ac70e7aSWei Yongjun 		cmlen = msg->msg_controllen;
2411da177e4SLinus Torvalds 	msg->msg_control += cmlen;
2421da177e4SLinus Torvalds 	msg->msg_controllen -= cmlen;
2431da177e4SLinus Torvalds 	err = 0;
2441da177e4SLinus Torvalds out:
2451da177e4SLinus Torvalds 	return err;
2461da177e4SLinus Torvalds }
2479e34a5b5SEric Dumazet EXPORT_SYMBOL(put_cmsg);
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm)
2501da177e4SLinus Torvalds {
251cfcabdccSStephen Hemminger 	struct cmsghdr __user *cm
252cfcabdccSStephen Hemminger 		= (__force struct cmsghdr __user*)msg->msg_control;
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	int fdmax = 0;
2551da177e4SLinus Torvalds 	int fdnum = scm->fp->count;
2561da177e4SLinus Torvalds 	struct file **fp = scm->fp->fp;
2571da177e4SLinus Torvalds 	int __user *cmfptr;
2581da177e4SLinus Torvalds 	int err = 0, i;
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	if (MSG_CMSG_COMPAT & msg->msg_flags) {
2611da177e4SLinus Torvalds 		scm_detach_fds_compat(msg, scm);
2621da177e4SLinus Torvalds 		return;
2631da177e4SLinus Torvalds 	}
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	if (msg->msg_controllen > sizeof(struct cmsghdr))
2661da177e4SLinus Torvalds 		fdmax = ((msg->msg_controllen - sizeof(struct cmsghdr))
2671da177e4SLinus Torvalds 			 / sizeof(int));
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	if (fdnum < fdmax)
2701da177e4SLinus Torvalds 		fdmax = fdnum;
2711da177e4SLinus Torvalds 
272cfcabdccSStephen Hemminger 	for (i=0, cmfptr=(__force int __user *)CMSG_DATA(cm); i<fdmax;
273cfcabdccSStephen Hemminger 	     i++, cmfptr++)
2741da177e4SLinus Torvalds 	{
27548a87cc2SJohn Fastabend 		struct socket *sock;
2761da177e4SLinus Torvalds 		int new_fd;
2771da177e4SLinus Torvalds 		err = security_file_receive(fp[i]);
2781da177e4SLinus Torvalds 		if (err)
2791da177e4SLinus Torvalds 			break;
2804a19542eSUlrich Drepper 		err = get_unused_fd_flags(MSG_CMSG_CLOEXEC & msg->msg_flags
2814a19542eSUlrich Drepper 					  ? O_CLOEXEC : 0);
2821da177e4SLinus Torvalds 		if (err < 0)
2831da177e4SLinus Torvalds 			break;
2841da177e4SLinus Torvalds 		new_fd = err;
2851da177e4SLinus Torvalds 		err = put_user(new_fd, cmfptr);
2861da177e4SLinus Torvalds 		if (err) {
2871da177e4SLinus Torvalds 			put_unused_fd(new_fd);
2881da177e4SLinus Torvalds 			break;
2891da177e4SLinus Torvalds 		}
2901da177e4SLinus Torvalds 		/* Bump the usage count and install the file. */
29148a87cc2SJohn Fastabend 		sock = sock_from_file(fp[i], &err);
292d8429506SDaniel Wagner 		if (sock) {
293*6ffd4641SZefan Li 			sock_update_netprioidx(sock->sk);
294211d2f97SZefan Li 			sock_update_classid(sock->sk);
295d8429506SDaniel Wagner 		}
296cb0942b8SAl Viro 		fd_install(new_fd, get_file(fp[i]));
2971da177e4SLinus Torvalds 	}
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	if (i > 0)
3001da177e4SLinus Torvalds 	{
3011da177e4SLinus Torvalds 		int cmlen = CMSG_LEN(i*sizeof(int));
3021da177e4SLinus Torvalds 		err = put_user(SOL_SOCKET, &cm->cmsg_level);
3031da177e4SLinus Torvalds 		if (!err)
3041da177e4SLinus Torvalds 			err = put_user(SCM_RIGHTS, &cm->cmsg_type);
3051da177e4SLinus Torvalds 		if (!err)
3061da177e4SLinus Torvalds 			err = put_user(cmlen, &cm->cmsg_len);
3071da177e4SLinus Torvalds 		if (!err) {
3081da177e4SLinus Torvalds 			cmlen = CMSG_SPACE(i*sizeof(int));
3091da177e4SLinus Torvalds 			msg->msg_control += cmlen;
3101da177e4SLinus Torvalds 			msg->msg_controllen -= cmlen;
3111da177e4SLinus Torvalds 		}
3121da177e4SLinus Torvalds 	}
3131da177e4SLinus Torvalds 	if (i < fdnum || (fdnum && fdmax <= 0))
3141da177e4SLinus Torvalds 		msg->msg_flags |= MSG_CTRUNC;
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds 	/*
3171da177e4SLinus Torvalds 	 * All of the files that fit in the message have had their
3181da177e4SLinus Torvalds 	 * usage counts incremented, so we just free the list.
3191da177e4SLinus Torvalds 	 */
3201da177e4SLinus Torvalds 	__scm_destroy(scm);
3211da177e4SLinus Torvalds }
3229e34a5b5SEric Dumazet EXPORT_SYMBOL(scm_detach_fds);
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl)
3251da177e4SLinus Torvalds {
3261da177e4SLinus Torvalds 	struct scm_fp_list *new_fpl;
3271da177e4SLinus Torvalds 	int i;
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 	if (!fpl)
3301da177e4SLinus Torvalds 		return NULL;
3311da177e4SLinus Torvalds 
332bba14de9SEric Dumazet 	new_fpl = kmemdup(fpl, offsetof(struct scm_fp_list, fp[fpl->count]),
333bba14de9SEric Dumazet 			  GFP_KERNEL);
3341da177e4SLinus Torvalds 	if (new_fpl) {
335bba14de9SEric Dumazet 		for (i = 0; i < fpl->count; i++)
3361da177e4SLinus Torvalds 			get_file(fpl->fp[i]);
337bba14de9SEric Dumazet 		new_fpl->max = new_fpl->count;
3381da177e4SLinus Torvalds 	}
3391da177e4SLinus Torvalds 	return new_fpl;
3401da177e4SLinus Torvalds }
3411da177e4SLinus Torvalds EXPORT_SYMBOL(scm_fp_dup);
342