xref: /openbmc/linux/net/core/scm.c (revision 1ff8cebf49ed9e9ca2ae44b5c4176aef9c21af9c)
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 
327c0f6ba6SLinus Torvalds #include <linux/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) ||
57d661684cSAndy Lutomirski 	     ns_capable(task_active_pid_ns(current)->user_ns, CAP_SYS_ADMIN)) &&
58b2e4f544SEric W. Biederman 	    ((uid_eq(uid, cred->uid)   || uid_eq(uid, cred->euid) ||
59c7b96acfSEric W. Biederman 	      uid_eq(uid, cred->suid)) || ns_capable(cred->user_ns, CAP_SETUID)) &&
60b2e4f544SEric W. Biederman 	    ((gid_eq(gid, cred->gid)   || gid_eq(gid, cred->egid) ||
61c7b96acfSEric W. Biederman 	      gid_eq(gid, cred->sgid)) || ns_capable(cred->user_ns, 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 
74*1ff8cebfSyuan linyu 	num = (cmsg->cmsg_len - 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;
90415e3d3eSHannes Frederic Sowa 		fpl->user = NULL;
911da177e4SLinus Torvalds 	}
921da177e4SLinus Torvalds 	fpp = &fpl->fp[fpl->count];
931da177e4SLinus Torvalds 
94bba14de9SEric Dumazet 	if (fpl->count + num > fpl->max)
951da177e4SLinus Torvalds 		return -EINVAL;
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 	/*
981da177e4SLinus Torvalds 	 *	Verify the descriptors and increment the usage count.
991da177e4SLinus Torvalds 	 */
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds 	for (i=0; i< num; i++)
1021da177e4SLinus Torvalds 	{
1031da177e4SLinus Torvalds 		int fd = fdp[i];
1041da177e4SLinus Torvalds 		struct file *file;
1051da177e4SLinus Torvalds 
106326be7b4SAl Viro 		if (fd < 0 || !(file = fget_raw(fd)))
1071da177e4SLinus Torvalds 			return -EBADF;
1081da177e4SLinus Torvalds 		*fpp++ = file;
1091da177e4SLinus Torvalds 		fpl->count++;
1101da177e4SLinus Torvalds 	}
111415e3d3eSHannes Frederic Sowa 
112415e3d3eSHannes Frederic Sowa 	if (!fpl->user)
113415e3d3eSHannes Frederic Sowa 		fpl->user = get_uid(current_user());
114415e3d3eSHannes Frederic Sowa 
1151da177e4SLinus Torvalds 	return num;
1161da177e4SLinus Torvalds }
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds void __scm_destroy(struct scm_cookie *scm)
1191da177e4SLinus Torvalds {
1201da177e4SLinus Torvalds 	struct scm_fp_list *fpl = scm->fp;
1211da177e4SLinus Torvalds 	int i;
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 	if (fpl) {
1241da177e4SLinus Torvalds 		scm->fp = NULL;
1251da177e4SLinus Torvalds 		for (i=fpl->count-1; i>=0; i--)
1261da177e4SLinus Torvalds 			fput(fpl->fp[i]);
127415e3d3eSHannes Frederic Sowa 		free_uid(fpl->user);
1281da177e4SLinus Torvalds 		kfree(fpl);
1291da177e4SLinus Torvalds 	}
1301da177e4SLinus Torvalds }
1319e34a5b5SEric Dumazet EXPORT_SYMBOL(__scm_destroy);
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)
1341da177e4SLinus Torvalds {
1351da177e4SLinus Torvalds 	struct cmsghdr *cmsg;
1361da177e4SLinus Torvalds 	int err;
1371da177e4SLinus Torvalds 
138f95b414eSGu Zheng 	for_each_cmsghdr(cmsg, msg) {
1391da177e4SLinus Torvalds 		err = -EINVAL;
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds 		/* Verify that cmsg_len is at least sizeof(struct cmsghdr) */
1421da177e4SLinus Torvalds 		/* The first check was omitted in <= 2.2.5. The reasoning was
1431da177e4SLinus Torvalds 		   that parser checks cmsg_len in any case, so that
1441da177e4SLinus Torvalds 		   additional check would be work duplication.
1451da177e4SLinus Torvalds 		   But if cmsg_level is not SOL_SOCKET, we do not check
1461da177e4SLinus Torvalds 		   for too short ancillary data object at all! Oops.
1471da177e4SLinus Torvalds 		   OK, let's add it...
1481da177e4SLinus Torvalds 		 */
1491da177e4SLinus Torvalds 		if (!CMSG_OK(msg, cmsg))
1501da177e4SLinus Torvalds 			goto error;
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 		if (cmsg->cmsg_level != SOL_SOCKET)
1531da177e4SLinus Torvalds 			continue;
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 		switch (cmsg->cmsg_type)
1561da177e4SLinus Torvalds 		{
1571da177e4SLinus Torvalds 		case SCM_RIGHTS:
15876dadd76SEric W. Biederman 			if (!sock->ops || sock->ops->family != PF_UNIX)
15976dadd76SEric W. Biederman 				goto error;
1601da177e4SLinus Torvalds 			err=scm_fp_copy(cmsg, &p->fp);
1611da177e4SLinus Torvalds 			if (err<0)
1621da177e4SLinus Torvalds 				goto error;
1631da177e4SLinus Torvalds 			break;
1641da177e4SLinus Torvalds 		case SCM_CREDENTIALS:
165b2e4f544SEric W. Biederman 		{
166dbe9a417SEric W. Biederman 			struct ucred creds;
167b2e4f544SEric W. Biederman 			kuid_t uid;
168b2e4f544SEric W. Biederman 			kgid_t gid;
1691da177e4SLinus Torvalds 			if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct ucred)))
1701da177e4SLinus Torvalds 				goto error;
171dbe9a417SEric W. Biederman 			memcpy(&creds, CMSG_DATA(cmsg), sizeof(struct ucred));
172dbe9a417SEric W. Biederman 			err = scm_check_creds(&creds);
1731da177e4SLinus Torvalds 			if (err)
1741da177e4SLinus Torvalds 				goto error;
175257b5358SEric W. Biederman 
176dbe9a417SEric W. Biederman 			p->creds.pid = creds.pid;
177dbe9a417SEric W. Biederman 			if (!p->pid || pid_vnr(p->pid) != creds.pid) {
178257b5358SEric W. Biederman 				struct pid *pid;
179257b5358SEric W. Biederman 				err = -ESRCH;
180dbe9a417SEric W. Biederman 				pid = find_get_pid(creds.pid);
181257b5358SEric W. Biederman 				if (!pid)
182257b5358SEric W. Biederman 					goto error;
183257b5358SEric W. Biederman 				put_pid(p->pid);
184257b5358SEric W. Biederman 				p->pid = pid;
185257b5358SEric W. Biederman 			}
186257b5358SEric W. Biederman 
187b2e4f544SEric W. Biederman 			err = -EINVAL;
188dbe9a417SEric W. Biederman 			uid = make_kuid(current_user_ns(), creds.uid);
189dbe9a417SEric W. Biederman 			gid = make_kgid(current_user_ns(), creds.gid);
190b2e4f544SEric W. Biederman 			if (!uid_valid(uid) || !gid_valid(gid))
191b2e4f544SEric W. Biederman 				goto error;
192b2e4f544SEric W. Biederman 
193dbe9a417SEric W. Biederman 			p->creds.uid = uid;
194dbe9a417SEric W. Biederman 			p->creds.gid = gid;
1951da177e4SLinus Torvalds 			break;
196b2e4f544SEric W. Biederman 		}
1971da177e4SLinus Torvalds 		default:
1981da177e4SLinus Torvalds 			goto error;
1991da177e4SLinus Torvalds 		}
2001da177e4SLinus Torvalds 	}
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	if (p->fp && !p->fp->count)
2031da177e4SLinus Torvalds 	{
2041da177e4SLinus Torvalds 		kfree(p->fp);
2051da177e4SLinus Torvalds 		p->fp = NULL;
2061da177e4SLinus Torvalds 	}
2071da177e4SLinus Torvalds 	return 0;
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds error:
2101da177e4SLinus Torvalds 	scm_destroy(p);
2111da177e4SLinus Torvalds 	return err;
2121da177e4SLinus Torvalds }
2139e34a5b5SEric Dumazet EXPORT_SYMBOL(__scm_send);
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds int put_cmsg(struct msghdr * msg, int level, int type, int len, void *data)
2161da177e4SLinus Torvalds {
217cfcabdccSStephen Hemminger 	struct cmsghdr __user *cm
218cfcabdccSStephen Hemminger 		= (__force struct cmsghdr __user *)msg->msg_control;
2191da177e4SLinus Torvalds 	struct cmsghdr cmhdr;
2201da177e4SLinus Torvalds 	int cmlen = CMSG_LEN(len);
2211da177e4SLinus Torvalds 	int err;
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 	if (MSG_CMSG_COMPAT & msg->msg_flags)
2241da177e4SLinus Torvalds 		return put_cmsg_compat(msg, level, type, len, data);
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds 	if (cm==NULL || msg->msg_controllen < sizeof(*cm)) {
2271da177e4SLinus Torvalds 		msg->msg_flags |= MSG_CTRUNC;
2281da177e4SLinus Torvalds 		return 0; /* XXX: return error? check spec. */
2291da177e4SLinus Torvalds 	}
2301da177e4SLinus Torvalds 	if (msg->msg_controllen < cmlen) {
2311da177e4SLinus Torvalds 		msg->msg_flags |= MSG_CTRUNC;
2321da177e4SLinus Torvalds 		cmlen = msg->msg_controllen;
2331da177e4SLinus Torvalds 	}
2341da177e4SLinus Torvalds 	cmhdr.cmsg_level = level;
2351da177e4SLinus Torvalds 	cmhdr.cmsg_type = type;
2361da177e4SLinus Torvalds 	cmhdr.cmsg_len = cmlen;
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 	err = -EFAULT;
2391da177e4SLinus Torvalds 	if (copy_to_user(cm, &cmhdr, sizeof cmhdr))
2401da177e4SLinus Torvalds 		goto out;
2411da177e4SLinus Torvalds 	if (copy_to_user(CMSG_DATA(cm), data, cmlen - sizeof(struct cmsghdr)))
2421da177e4SLinus Torvalds 		goto out;
2431da177e4SLinus Torvalds 	cmlen = CMSG_SPACE(len);
2441ac70e7aSWei Yongjun 	if (msg->msg_controllen < cmlen)
2451ac70e7aSWei Yongjun 		cmlen = msg->msg_controllen;
2461da177e4SLinus Torvalds 	msg->msg_control += cmlen;
2471da177e4SLinus Torvalds 	msg->msg_controllen -= cmlen;
2481da177e4SLinus Torvalds 	err = 0;
2491da177e4SLinus Torvalds out:
2501da177e4SLinus Torvalds 	return err;
2511da177e4SLinus Torvalds }
2529e34a5b5SEric Dumazet EXPORT_SYMBOL(put_cmsg);
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm)
2551da177e4SLinus Torvalds {
256cfcabdccSStephen Hemminger 	struct cmsghdr __user *cm
257cfcabdccSStephen Hemminger 		= (__force struct cmsghdr __user*)msg->msg_control;
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	int fdmax = 0;
2601da177e4SLinus Torvalds 	int fdnum = scm->fp->count;
2611da177e4SLinus Torvalds 	struct file **fp = scm->fp->fp;
2621da177e4SLinus Torvalds 	int __user *cmfptr;
2631da177e4SLinus Torvalds 	int err = 0, i;
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	if (MSG_CMSG_COMPAT & msg->msg_flags) {
2661da177e4SLinus Torvalds 		scm_detach_fds_compat(msg, scm);
2671da177e4SLinus Torvalds 		return;
2681da177e4SLinus Torvalds 	}
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	if (msg->msg_controllen > sizeof(struct cmsghdr))
2711da177e4SLinus Torvalds 		fdmax = ((msg->msg_controllen - sizeof(struct cmsghdr))
2721da177e4SLinus Torvalds 			 / sizeof(int));
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	if (fdnum < fdmax)
2751da177e4SLinus Torvalds 		fdmax = fdnum;
2761da177e4SLinus Torvalds 
277cfcabdccSStephen Hemminger 	for (i=0, cmfptr=(__force int __user *)CMSG_DATA(cm); i<fdmax;
278cfcabdccSStephen Hemminger 	     i++, cmfptr++)
2791da177e4SLinus Torvalds 	{
28048a87cc2SJohn Fastabend 		struct socket *sock;
2811da177e4SLinus Torvalds 		int new_fd;
2821da177e4SLinus Torvalds 		err = security_file_receive(fp[i]);
2831da177e4SLinus Torvalds 		if (err)
2841da177e4SLinus Torvalds 			break;
2854a19542eSUlrich Drepper 		err = get_unused_fd_flags(MSG_CMSG_CLOEXEC & msg->msg_flags
2864a19542eSUlrich Drepper 					  ? O_CLOEXEC : 0);
2871da177e4SLinus Torvalds 		if (err < 0)
2881da177e4SLinus Torvalds 			break;
2891da177e4SLinus Torvalds 		new_fd = err;
2901da177e4SLinus Torvalds 		err = put_user(new_fd, cmfptr);
2911da177e4SLinus Torvalds 		if (err) {
2921da177e4SLinus Torvalds 			put_unused_fd(new_fd);
2931da177e4SLinus Torvalds 			break;
2941da177e4SLinus Torvalds 		}
2951da177e4SLinus Torvalds 		/* Bump the usage count and install the file. */
29648a87cc2SJohn Fastabend 		sock = sock_from_file(fp[i], &err);
297d8429506SDaniel Wagner 		if (sock) {
2982a56a1feSTejun Heo 			sock_update_netprioidx(&sock->sk->sk_cgrp_data);
2992a56a1feSTejun Heo 			sock_update_classid(&sock->sk->sk_cgrp_data);
300d8429506SDaniel Wagner 		}
301cb0942b8SAl Viro 		fd_install(new_fd, get_file(fp[i]));
3021da177e4SLinus Torvalds 	}
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	if (i > 0)
3051da177e4SLinus Torvalds 	{
3061da177e4SLinus Torvalds 		int cmlen = CMSG_LEN(i*sizeof(int));
3071da177e4SLinus Torvalds 		err = put_user(SOL_SOCKET, &cm->cmsg_level);
3081da177e4SLinus Torvalds 		if (!err)
3091da177e4SLinus Torvalds 			err = put_user(SCM_RIGHTS, &cm->cmsg_type);
3101da177e4SLinus Torvalds 		if (!err)
3111da177e4SLinus Torvalds 			err = put_user(cmlen, &cm->cmsg_len);
3121da177e4SLinus Torvalds 		if (!err) {
3131da177e4SLinus Torvalds 			cmlen = CMSG_SPACE(i*sizeof(int));
3146900317fSDaniel Borkmann 			if (msg->msg_controllen < cmlen)
3156900317fSDaniel Borkmann 				cmlen = msg->msg_controllen;
3161da177e4SLinus Torvalds 			msg->msg_control += cmlen;
3171da177e4SLinus Torvalds 			msg->msg_controllen -= cmlen;
3181da177e4SLinus Torvalds 		}
3191da177e4SLinus Torvalds 	}
3201da177e4SLinus Torvalds 	if (i < fdnum || (fdnum && fdmax <= 0))
3211da177e4SLinus Torvalds 		msg->msg_flags |= MSG_CTRUNC;
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds 	/*
3241da177e4SLinus Torvalds 	 * All of the files that fit in the message have had their
3251da177e4SLinus Torvalds 	 * usage counts incremented, so we just free the list.
3261da177e4SLinus Torvalds 	 */
3271da177e4SLinus Torvalds 	__scm_destroy(scm);
3281da177e4SLinus Torvalds }
3299e34a5b5SEric Dumazet EXPORT_SYMBOL(scm_detach_fds);
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl)
3321da177e4SLinus Torvalds {
3331da177e4SLinus Torvalds 	struct scm_fp_list *new_fpl;
3341da177e4SLinus Torvalds 	int i;
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	if (!fpl)
3371da177e4SLinus Torvalds 		return NULL;
3381da177e4SLinus Torvalds 
339bba14de9SEric Dumazet 	new_fpl = kmemdup(fpl, offsetof(struct scm_fp_list, fp[fpl->count]),
340bba14de9SEric Dumazet 			  GFP_KERNEL);
3411da177e4SLinus Torvalds 	if (new_fpl) {
342bba14de9SEric Dumazet 		for (i = 0; i < fpl->count; i++)
3431da177e4SLinus Torvalds 			get_file(fpl->fp[i]);
344bba14de9SEric Dumazet 		new_fpl->max = new_fpl->count;
345415e3d3eSHannes Frederic Sowa 		new_fpl->user = get_uid(fpl->user);
3461da177e4SLinus Torvalds 	}
3471da177e4SLinus Torvalds 	return new_fpl;
3481da177e4SLinus Torvalds }
3491da177e4SLinus Torvalds EXPORT_SYMBOL(scm_fp_dup);
350