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> 27*b488893aSPavel Emelyanov #include <linux/pid.h> 28*b488893aSPavel Emelyanov #include <linux/nsproxy.h> 291da177e4SLinus Torvalds 301da177e4SLinus Torvalds #include <asm/system.h> 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 { 47*b488893aSPavel Emelyanov if ((creds->pid == task_tgid_vnr(current) || capable(CAP_SYS_ADMIN)) && 481da177e4SLinus Torvalds ((creds->uid == current->uid || creds->uid == current->euid || 491da177e4SLinus Torvalds creds->uid == current->suid) || capable(CAP_SETUID)) && 501da177e4SLinus Torvalds ((creds->gid == current->gid || creds->gid == current->egid || 511da177e4SLinus Torvalds creds->gid == current->sgid) || capable(CAP_SETGID))) { 521da177e4SLinus Torvalds return 0; 531da177e4SLinus Torvalds } 541da177e4SLinus Torvalds return -EPERM; 551da177e4SLinus Torvalds } 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds static int scm_fp_copy(struct cmsghdr *cmsg, struct scm_fp_list **fplp) 581da177e4SLinus Torvalds { 591da177e4SLinus Torvalds int *fdp = (int*)CMSG_DATA(cmsg); 601da177e4SLinus Torvalds struct scm_fp_list *fpl = *fplp; 611da177e4SLinus Torvalds struct file **fpp; 621da177e4SLinus Torvalds int i, num; 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds num = (cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr)))/sizeof(int); 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds if (num <= 0) 671da177e4SLinus Torvalds return 0; 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds if (num > SCM_MAX_FD) 701da177e4SLinus Torvalds return -EINVAL; 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds if (!fpl) 731da177e4SLinus Torvalds { 741da177e4SLinus Torvalds fpl = kmalloc(sizeof(struct scm_fp_list), GFP_KERNEL); 751da177e4SLinus Torvalds if (!fpl) 761da177e4SLinus Torvalds return -ENOMEM; 771da177e4SLinus Torvalds *fplp = fpl; 781da177e4SLinus Torvalds fpl->count = 0; 791da177e4SLinus Torvalds } 801da177e4SLinus Torvalds fpp = &fpl->fp[fpl->count]; 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds if (fpl->count + num > SCM_MAX_FD) 831da177e4SLinus Torvalds return -EINVAL; 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds /* 861da177e4SLinus Torvalds * Verify the descriptors and increment the usage count. 871da177e4SLinus Torvalds */ 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds for (i=0; i< num; i++) 901da177e4SLinus Torvalds { 911da177e4SLinus Torvalds int fd = fdp[i]; 921da177e4SLinus Torvalds struct file *file; 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds if (fd < 0 || !(file = fget(fd))) 951da177e4SLinus Torvalds return -EBADF; 961da177e4SLinus Torvalds *fpp++ = file; 971da177e4SLinus Torvalds fpl->count++; 981da177e4SLinus Torvalds } 991da177e4SLinus Torvalds return num; 1001da177e4SLinus Torvalds } 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds void __scm_destroy(struct scm_cookie *scm) 1031da177e4SLinus Torvalds { 1041da177e4SLinus Torvalds struct scm_fp_list *fpl = scm->fp; 1051da177e4SLinus Torvalds int i; 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds if (fpl) { 1081da177e4SLinus Torvalds scm->fp = NULL; 1091da177e4SLinus Torvalds for (i=fpl->count-1; i>=0; i--) 1101da177e4SLinus Torvalds fput(fpl->fp[i]); 1111da177e4SLinus Torvalds kfree(fpl); 1121da177e4SLinus Torvalds } 1131da177e4SLinus Torvalds } 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p) 1161da177e4SLinus Torvalds { 1171da177e4SLinus Torvalds struct cmsghdr *cmsg; 1181da177e4SLinus Torvalds int err; 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) 1211da177e4SLinus Torvalds { 1221da177e4SLinus Torvalds err = -EINVAL; 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds /* Verify that cmsg_len is at least sizeof(struct cmsghdr) */ 1251da177e4SLinus Torvalds /* The first check was omitted in <= 2.2.5. The reasoning was 1261da177e4SLinus Torvalds that parser checks cmsg_len in any case, so that 1271da177e4SLinus Torvalds additional check would be work duplication. 1281da177e4SLinus Torvalds But if cmsg_level is not SOL_SOCKET, we do not check 1291da177e4SLinus Torvalds for too short ancillary data object at all! Oops. 1301da177e4SLinus Torvalds OK, let's add it... 1311da177e4SLinus Torvalds */ 1321da177e4SLinus Torvalds if (!CMSG_OK(msg, cmsg)) 1331da177e4SLinus Torvalds goto error; 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds if (cmsg->cmsg_level != SOL_SOCKET) 1361da177e4SLinus Torvalds continue; 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds switch (cmsg->cmsg_type) 1391da177e4SLinus Torvalds { 1401da177e4SLinus Torvalds case SCM_RIGHTS: 1411da177e4SLinus Torvalds err=scm_fp_copy(cmsg, &p->fp); 1421da177e4SLinus Torvalds if (err<0) 1431da177e4SLinus Torvalds goto error; 1441da177e4SLinus Torvalds break; 1451da177e4SLinus Torvalds case SCM_CREDENTIALS: 1461da177e4SLinus Torvalds if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct ucred))) 1471da177e4SLinus Torvalds goto error; 1481da177e4SLinus Torvalds memcpy(&p->creds, CMSG_DATA(cmsg), sizeof(struct ucred)); 1491da177e4SLinus Torvalds err = scm_check_creds(&p->creds); 1501da177e4SLinus Torvalds if (err) 1511da177e4SLinus Torvalds goto error; 1521da177e4SLinus Torvalds break; 1531da177e4SLinus Torvalds default: 1541da177e4SLinus Torvalds goto error; 1551da177e4SLinus Torvalds } 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds if (p->fp && !p->fp->count) 1591da177e4SLinus Torvalds { 1601da177e4SLinus Torvalds kfree(p->fp); 1611da177e4SLinus Torvalds p->fp = NULL; 1621da177e4SLinus Torvalds } 1631da177e4SLinus Torvalds return 0; 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds error: 1661da177e4SLinus Torvalds scm_destroy(p); 1671da177e4SLinus Torvalds return err; 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds int put_cmsg(struct msghdr * msg, int level, int type, int len, void *data) 1711da177e4SLinus Torvalds { 172cfcabdccSStephen Hemminger struct cmsghdr __user *cm 173cfcabdccSStephen Hemminger = (__force struct cmsghdr __user *)msg->msg_control; 1741da177e4SLinus Torvalds struct cmsghdr cmhdr; 1751da177e4SLinus Torvalds int cmlen = CMSG_LEN(len); 1761da177e4SLinus Torvalds int err; 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds if (MSG_CMSG_COMPAT & msg->msg_flags) 1791da177e4SLinus Torvalds return put_cmsg_compat(msg, level, type, len, data); 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds if (cm==NULL || msg->msg_controllen < sizeof(*cm)) { 1821da177e4SLinus Torvalds msg->msg_flags |= MSG_CTRUNC; 1831da177e4SLinus Torvalds return 0; /* XXX: return error? check spec. */ 1841da177e4SLinus Torvalds } 1851da177e4SLinus Torvalds if (msg->msg_controllen < cmlen) { 1861da177e4SLinus Torvalds msg->msg_flags |= MSG_CTRUNC; 1871da177e4SLinus Torvalds cmlen = msg->msg_controllen; 1881da177e4SLinus Torvalds } 1891da177e4SLinus Torvalds cmhdr.cmsg_level = level; 1901da177e4SLinus Torvalds cmhdr.cmsg_type = type; 1911da177e4SLinus Torvalds cmhdr.cmsg_len = cmlen; 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds err = -EFAULT; 1941da177e4SLinus Torvalds if (copy_to_user(cm, &cmhdr, sizeof cmhdr)) 1951da177e4SLinus Torvalds goto out; 1961da177e4SLinus Torvalds if (copy_to_user(CMSG_DATA(cm), data, cmlen - sizeof(struct cmsghdr))) 1971da177e4SLinus Torvalds goto out; 1981da177e4SLinus Torvalds cmlen = CMSG_SPACE(len); 1991da177e4SLinus Torvalds msg->msg_control += cmlen; 2001da177e4SLinus Torvalds msg->msg_controllen -= cmlen; 2011da177e4SLinus Torvalds err = 0; 2021da177e4SLinus Torvalds out: 2031da177e4SLinus Torvalds return err; 2041da177e4SLinus Torvalds } 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm) 2071da177e4SLinus Torvalds { 208cfcabdccSStephen Hemminger struct cmsghdr __user *cm 209cfcabdccSStephen Hemminger = (__force struct cmsghdr __user*)msg->msg_control; 2101da177e4SLinus Torvalds 2111da177e4SLinus Torvalds int fdmax = 0; 2121da177e4SLinus Torvalds int fdnum = scm->fp->count; 2131da177e4SLinus Torvalds struct file **fp = scm->fp->fp; 2141da177e4SLinus Torvalds int __user *cmfptr; 2151da177e4SLinus Torvalds int err = 0, i; 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds if (MSG_CMSG_COMPAT & msg->msg_flags) { 2181da177e4SLinus Torvalds scm_detach_fds_compat(msg, scm); 2191da177e4SLinus Torvalds return; 2201da177e4SLinus Torvalds } 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds if (msg->msg_controllen > sizeof(struct cmsghdr)) 2231da177e4SLinus Torvalds fdmax = ((msg->msg_controllen - sizeof(struct cmsghdr)) 2241da177e4SLinus Torvalds / sizeof(int)); 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds if (fdnum < fdmax) 2271da177e4SLinus Torvalds fdmax = fdnum; 2281da177e4SLinus Torvalds 229cfcabdccSStephen Hemminger for (i=0, cmfptr=(__force int __user *)CMSG_DATA(cm); i<fdmax; 230cfcabdccSStephen Hemminger i++, cmfptr++) 2311da177e4SLinus Torvalds { 2321da177e4SLinus Torvalds int new_fd; 2331da177e4SLinus Torvalds err = security_file_receive(fp[i]); 2341da177e4SLinus Torvalds if (err) 2351da177e4SLinus Torvalds break; 2364a19542eSUlrich Drepper err = get_unused_fd_flags(MSG_CMSG_CLOEXEC & msg->msg_flags 2374a19542eSUlrich Drepper ? O_CLOEXEC : 0); 2381da177e4SLinus Torvalds if (err < 0) 2391da177e4SLinus Torvalds break; 2401da177e4SLinus Torvalds new_fd = err; 2411da177e4SLinus Torvalds err = put_user(new_fd, cmfptr); 2421da177e4SLinus Torvalds if (err) { 2431da177e4SLinus Torvalds put_unused_fd(new_fd); 2441da177e4SLinus Torvalds break; 2451da177e4SLinus Torvalds } 2461da177e4SLinus Torvalds /* Bump the usage count and install the file. */ 2471da177e4SLinus Torvalds get_file(fp[i]); 2481da177e4SLinus Torvalds fd_install(new_fd, fp[i]); 2491da177e4SLinus Torvalds } 2501da177e4SLinus Torvalds 2511da177e4SLinus Torvalds if (i > 0) 2521da177e4SLinus Torvalds { 2531da177e4SLinus Torvalds int cmlen = CMSG_LEN(i*sizeof(int)); 2541da177e4SLinus Torvalds err = put_user(SOL_SOCKET, &cm->cmsg_level); 2551da177e4SLinus Torvalds if (!err) 2561da177e4SLinus Torvalds err = put_user(SCM_RIGHTS, &cm->cmsg_type); 2571da177e4SLinus Torvalds if (!err) 2581da177e4SLinus Torvalds err = put_user(cmlen, &cm->cmsg_len); 2591da177e4SLinus Torvalds if (!err) { 2601da177e4SLinus Torvalds cmlen = CMSG_SPACE(i*sizeof(int)); 2611da177e4SLinus Torvalds msg->msg_control += cmlen; 2621da177e4SLinus Torvalds msg->msg_controllen -= cmlen; 2631da177e4SLinus Torvalds } 2641da177e4SLinus Torvalds } 2651da177e4SLinus Torvalds if (i < fdnum || (fdnum && fdmax <= 0)) 2661da177e4SLinus Torvalds msg->msg_flags |= MSG_CTRUNC; 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds /* 2691da177e4SLinus Torvalds * All of the files that fit in the message have had their 2701da177e4SLinus Torvalds * usage counts incremented, so we just free the list. 2711da177e4SLinus Torvalds */ 2721da177e4SLinus Torvalds __scm_destroy(scm); 2731da177e4SLinus Torvalds } 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl) 2761da177e4SLinus Torvalds { 2771da177e4SLinus Torvalds struct scm_fp_list *new_fpl; 2781da177e4SLinus Torvalds int i; 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds if (!fpl) 2811da177e4SLinus Torvalds return NULL; 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds new_fpl = kmalloc(sizeof(*fpl), GFP_KERNEL); 2841da177e4SLinus Torvalds if (new_fpl) { 2851da177e4SLinus Torvalds for (i=fpl->count-1; i>=0; i--) 2861da177e4SLinus Torvalds get_file(fpl->fp[i]); 2871da177e4SLinus Torvalds memcpy(new_fpl, fpl, sizeof(*fpl)); 2881da177e4SLinus Torvalds } 2891da177e4SLinus Torvalds return new_fpl; 2901da177e4SLinus Torvalds } 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds EXPORT_SYMBOL(__scm_destroy); 2931da177e4SLinus Torvalds EXPORT_SYMBOL(__scm_send); 2941da177e4SLinus Torvalds EXPORT_SYMBOL(put_cmsg); 2951da177e4SLinus Torvalds EXPORT_SYMBOL(scm_detach_fds); 2961da177e4SLinus Torvalds EXPORT_SYMBOL(scm_fp_dup); 297