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> 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 { 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; 811da177e4SLinus Torvalds } 821da177e4SLinus Torvalds fpp = &fpl->fp[fpl->count]; 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds if (fpl->count + num > SCM_MAX_FD) 851da177e4SLinus Torvalds return -EINVAL; 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds /* 881da177e4SLinus Torvalds * Verify the descriptors and increment the usage count. 891da177e4SLinus Torvalds */ 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds for (i=0; i< num; i++) 921da177e4SLinus Torvalds { 931da177e4SLinus Torvalds int fd = fdp[i]; 941da177e4SLinus Torvalds struct file *file; 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds if (fd < 0 || !(file = fget(fd))) 971da177e4SLinus Torvalds return -EBADF; 981da177e4SLinus Torvalds *fpp++ = file; 991da177e4SLinus Torvalds fpl->count++; 1001da177e4SLinus Torvalds } 1011da177e4SLinus Torvalds return num; 1021da177e4SLinus Torvalds } 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds void __scm_destroy(struct scm_cookie *scm) 1051da177e4SLinus Torvalds { 1061da177e4SLinus Torvalds struct scm_fp_list *fpl = scm->fp; 1071da177e4SLinus Torvalds int i; 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds if (fpl) { 1101da177e4SLinus Torvalds scm->fp = NULL; 111f8d570a4SDavid Miller if (current->scm_work_list) { 112f8d570a4SDavid Miller list_add_tail(&fpl->list, current->scm_work_list); 113f8d570a4SDavid Miller } else { 114f8d570a4SDavid Miller LIST_HEAD(work_list); 115f8d570a4SDavid Miller 116f8d570a4SDavid Miller current->scm_work_list = &work_list; 117f8d570a4SDavid Miller 118f8d570a4SDavid Miller list_add(&fpl->list, &work_list); 119f8d570a4SDavid Miller while (!list_empty(&work_list)) { 120f8d570a4SDavid Miller fpl = list_first_entry(&work_list, struct scm_fp_list, list); 121f8d570a4SDavid Miller 122f8d570a4SDavid Miller list_del(&fpl->list); 1231da177e4SLinus Torvalds for (i=fpl->count-1; i>=0; i--) 1241da177e4SLinus Torvalds fput(fpl->fp[i]); 1251da177e4SLinus Torvalds kfree(fpl); 1261da177e4SLinus Torvalds } 127f8d570a4SDavid Miller 128f8d570a4SDavid Miller current->scm_work_list = NULL; 129f8d570a4SDavid Miller } 130f8d570a4SDavid Miller } 1311da177e4SLinus Torvalds } 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 1381da177e4SLinus Torvalds for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) 1391da177e4SLinus Torvalds { 1401da177e4SLinus Torvalds err = -EINVAL; 1411da177e4SLinus Torvalds 1421da177e4SLinus Torvalds /* Verify that cmsg_len is at least sizeof(struct cmsghdr) */ 1431da177e4SLinus Torvalds /* The first check was omitted in <= 2.2.5. The reasoning was 1441da177e4SLinus Torvalds that parser checks cmsg_len in any case, so that 1451da177e4SLinus Torvalds additional check would be work duplication. 1461da177e4SLinus Torvalds But if cmsg_level is not SOL_SOCKET, we do not check 1471da177e4SLinus Torvalds for too short ancillary data object at all! Oops. 1481da177e4SLinus Torvalds OK, let's add it... 1491da177e4SLinus Torvalds */ 1501da177e4SLinus Torvalds if (!CMSG_OK(msg, cmsg)) 1511da177e4SLinus Torvalds goto error; 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds if (cmsg->cmsg_level != SOL_SOCKET) 1541da177e4SLinus Torvalds continue; 1551da177e4SLinus Torvalds 1561da177e4SLinus Torvalds switch (cmsg->cmsg_type) 1571da177e4SLinus Torvalds { 1581da177e4SLinus Torvalds case SCM_RIGHTS: 159*76dadd76SEric W. Biederman if (!sock->ops || sock->ops->family != PF_UNIX) 160*76dadd76SEric W. Biederman goto error; 1611da177e4SLinus Torvalds err=scm_fp_copy(cmsg, &p->fp); 1621da177e4SLinus Torvalds if (err<0) 1631da177e4SLinus Torvalds goto error; 1641da177e4SLinus Torvalds break; 1651da177e4SLinus Torvalds case SCM_CREDENTIALS: 1661da177e4SLinus Torvalds if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct ucred))) 1671da177e4SLinus Torvalds goto error; 1681da177e4SLinus Torvalds memcpy(&p->creds, CMSG_DATA(cmsg), sizeof(struct ucred)); 1691da177e4SLinus Torvalds err = scm_check_creds(&p->creds); 1701da177e4SLinus Torvalds if (err) 1711da177e4SLinus Torvalds goto error; 1721da177e4SLinus Torvalds break; 1731da177e4SLinus Torvalds default: 1741da177e4SLinus Torvalds goto error; 1751da177e4SLinus Torvalds } 1761da177e4SLinus Torvalds } 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds if (p->fp && !p->fp->count) 1791da177e4SLinus Torvalds { 1801da177e4SLinus Torvalds kfree(p->fp); 1811da177e4SLinus Torvalds p->fp = NULL; 1821da177e4SLinus Torvalds } 1831da177e4SLinus Torvalds return 0; 1841da177e4SLinus Torvalds 1851da177e4SLinus Torvalds error: 1861da177e4SLinus Torvalds scm_destroy(p); 1871da177e4SLinus Torvalds return err; 1881da177e4SLinus Torvalds } 1891da177e4SLinus Torvalds 1901da177e4SLinus Torvalds int put_cmsg(struct msghdr * msg, int level, int type, int len, void *data) 1911da177e4SLinus Torvalds { 192cfcabdccSStephen Hemminger struct cmsghdr __user *cm 193cfcabdccSStephen Hemminger = (__force struct cmsghdr __user *)msg->msg_control; 1941da177e4SLinus Torvalds struct cmsghdr cmhdr; 1951da177e4SLinus Torvalds int cmlen = CMSG_LEN(len); 1961da177e4SLinus Torvalds int err; 1971da177e4SLinus Torvalds 1981da177e4SLinus Torvalds if (MSG_CMSG_COMPAT & msg->msg_flags) 1991da177e4SLinus Torvalds return put_cmsg_compat(msg, level, type, len, data); 2001da177e4SLinus Torvalds 2011da177e4SLinus Torvalds if (cm==NULL || msg->msg_controllen < sizeof(*cm)) { 2021da177e4SLinus Torvalds msg->msg_flags |= MSG_CTRUNC; 2031da177e4SLinus Torvalds return 0; /* XXX: return error? check spec. */ 2041da177e4SLinus Torvalds } 2051da177e4SLinus Torvalds if (msg->msg_controllen < cmlen) { 2061da177e4SLinus Torvalds msg->msg_flags |= MSG_CTRUNC; 2071da177e4SLinus Torvalds cmlen = msg->msg_controllen; 2081da177e4SLinus Torvalds } 2091da177e4SLinus Torvalds cmhdr.cmsg_level = level; 2101da177e4SLinus Torvalds cmhdr.cmsg_type = type; 2111da177e4SLinus Torvalds cmhdr.cmsg_len = cmlen; 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds err = -EFAULT; 2141da177e4SLinus Torvalds if (copy_to_user(cm, &cmhdr, sizeof cmhdr)) 2151da177e4SLinus Torvalds goto out; 2161da177e4SLinus Torvalds if (copy_to_user(CMSG_DATA(cm), data, cmlen - sizeof(struct cmsghdr))) 2171da177e4SLinus Torvalds goto out; 2181da177e4SLinus Torvalds cmlen = CMSG_SPACE(len); 2191ac70e7aSWei Yongjun if (msg->msg_controllen < cmlen) 2201ac70e7aSWei Yongjun cmlen = msg->msg_controllen; 2211da177e4SLinus Torvalds msg->msg_control += cmlen; 2221da177e4SLinus Torvalds msg->msg_controllen -= cmlen; 2231da177e4SLinus Torvalds err = 0; 2241da177e4SLinus Torvalds out: 2251da177e4SLinus Torvalds return err; 2261da177e4SLinus Torvalds } 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm) 2291da177e4SLinus Torvalds { 230cfcabdccSStephen Hemminger struct cmsghdr __user *cm 231cfcabdccSStephen Hemminger = (__force struct cmsghdr __user*)msg->msg_control; 2321da177e4SLinus Torvalds 2331da177e4SLinus Torvalds int fdmax = 0; 2341da177e4SLinus Torvalds int fdnum = scm->fp->count; 2351da177e4SLinus Torvalds struct file **fp = scm->fp->fp; 2361da177e4SLinus Torvalds int __user *cmfptr; 2371da177e4SLinus Torvalds int err = 0, i; 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds if (MSG_CMSG_COMPAT & msg->msg_flags) { 2401da177e4SLinus Torvalds scm_detach_fds_compat(msg, scm); 2411da177e4SLinus Torvalds return; 2421da177e4SLinus Torvalds } 2431da177e4SLinus Torvalds 2441da177e4SLinus Torvalds if (msg->msg_controllen > sizeof(struct cmsghdr)) 2451da177e4SLinus Torvalds fdmax = ((msg->msg_controllen - sizeof(struct cmsghdr)) 2461da177e4SLinus Torvalds / sizeof(int)); 2471da177e4SLinus Torvalds 2481da177e4SLinus Torvalds if (fdnum < fdmax) 2491da177e4SLinus Torvalds fdmax = fdnum; 2501da177e4SLinus Torvalds 251cfcabdccSStephen Hemminger for (i=0, cmfptr=(__force int __user *)CMSG_DATA(cm); i<fdmax; 252cfcabdccSStephen Hemminger i++, cmfptr++) 2531da177e4SLinus Torvalds { 2541da177e4SLinus Torvalds int new_fd; 2551da177e4SLinus Torvalds err = security_file_receive(fp[i]); 2561da177e4SLinus Torvalds if (err) 2571da177e4SLinus Torvalds break; 2584a19542eSUlrich Drepper err = get_unused_fd_flags(MSG_CMSG_CLOEXEC & msg->msg_flags 2594a19542eSUlrich Drepper ? O_CLOEXEC : 0); 2601da177e4SLinus Torvalds if (err < 0) 2611da177e4SLinus Torvalds break; 2621da177e4SLinus Torvalds new_fd = err; 2631da177e4SLinus Torvalds err = put_user(new_fd, cmfptr); 2641da177e4SLinus Torvalds if (err) { 2651da177e4SLinus Torvalds put_unused_fd(new_fd); 2661da177e4SLinus Torvalds break; 2671da177e4SLinus Torvalds } 2681da177e4SLinus Torvalds /* Bump the usage count and install the file. */ 2691da177e4SLinus Torvalds get_file(fp[i]); 2701da177e4SLinus Torvalds fd_install(new_fd, fp[i]); 2711da177e4SLinus Torvalds } 2721da177e4SLinus Torvalds 2731da177e4SLinus Torvalds if (i > 0) 2741da177e4SLinus Torvalds { 2751da177e4SLinus Torvalds int cmlen = CMSG_LEN(i*sizeof(int)); 2761da177e4SLinus Torvalds err = put_user(SOL_SOCKET, &cm->cmsg_level); 2771da177e4SLinus Torvalds if (!err) 2781da177e4SLinus Torvalds err = put_user(SCM_RIGHTS, &cm->cmsg_type); 2791da177e4SLinus Torvalds if (!err) 2801da177e4SLinus Torvalds err = put_user(cmlen, &cm->cmsg_len); 2811da177e4SLinus Torvalds if (!err) { 2821da177e4SLinus Torvalds cmlen = CMSG_SPACE(i*sizeof(int)); 2831da177e4SLinus Torvalds msg->msg_control += cmlen; 2841da177e4SLinus Torvalds msg->msg_controllen -= cmlen; 2851da177e4SLinus Torvalds } 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds if (i < fdnum || (fdnum && fdmax <= 0)) 2881da177e4SLinus Torvalds msg->msg_flags |= MSG_CTRUNC; 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds /* 2911da177e4SLinus Torvalds * All of the files that fit in the message have had their 2921da177e4SLinus Torvalds * usage counts incremented, so we just free the list. 2931da177e4SLinus Torvalds */ 2941da177e4SLinus Torvalds __scm_destroy(scm); 2951da177e4SLinus Torvalds } 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl) 2981da177e4SLinus Torvalds { 2991da177e4SLinus Torvalds struct scm_fp_list *new_fpl; 3001da177e4SLinus Torvalds int i; 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds if (!fpl) 3031da177e4SLinus Torvalds return NULL; 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds new_fpl = kmalloc(sizeof(*fpl), GFP_KERNEL); 3061da177e4SLinus Torvalds if (new_fpl) { 3071da177e4SLinus Torvalds for (i=fpl->count-1; i>=0; i--) 3081da177e4SLinus Torvalds get_file(fpl->fp[i]); 3091da177e4SLinus Torvalds memcpy(new_fpl, fpl, sizeof(*fpl)); 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds return new_fpl; 3121da177e4SLinus Torvalds } 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds EXPORT_SYMBOL(__scm_destroy); 3151da177e4SLinus Torvalds EXPORT_SYMBOL(__scm_send); 3161da177e4SLinus Torvalds EXPORT_SYMBOL(put_cmsg); 3171da177e4SLinus Torvalds EXPORT_SYMBOL(scm_detach_fds); 3181da177e4SLinus Torvalds EXPORT_SYMBOL(scm_fp_dup); 319