12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /* scm.c - Socket level control messages processing.
31da177e4SLinus Torvalds *
41da177e4SLinus Torvalds * Author: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
51da177e4SLinus Torvalds * Alignment and value checking mods by Craig Metz
61da177e4SLinus Torvalds */
71da177e4SLinus Torvalds
81da177e4SLinus Torvalds #include <linux/module.h>
91da177e4SLinus Torvalds #include <linux/signal.h>
104fc268d2SRandy Dunlap #include <linux/capability.h>
111da177e4SLinus Torvalds #include <linux/errno.h>
121da177e4SLinus Torvalds #include <linux/sched.h>
138703e8a4SIngo Molnar #include <linux/sched/user.h>
141da177e4SLinus Torvalds #include <linux/mm.h>
151da177e4SLinus Torvalds #include <linux/kernel.h>
161da177e4SLinus Torvalds #include <linux/stat.h>
171da177e4SLinus Torvalds #include <linux/socket.h>
181da177e4SLinus Torvalds #include <linux/file.h>
191da177e4SLinus Torvalds #include <linux/fcntl.h>
201da177e4SLinus Torvalds #include <linux/net.h>
211da177e4SLinus Torvalds #include <linux/interrupt.h>
221da177e4SLinus Torvalds #include <linux/netdevice.h>
231da177e4SLinus Torvalds #include <linux/security.h>
2492f28d97SEric W. Biederman #include <linux/pid_namespace.h>
25b488893aSPavel Emelyanov #include <linux/pid.h>
26b488893aSPavel Emelyanov #include <linux/nsproxy.h>
275a0e3ad6STejun Heo #include <linux/slab.h>
289718475eSDeepa Dinamani #include <linux/errqueue.h>
29705318a9SPavel Begunkov #include <linux/io_uring.h>
301da177e4SLinus Torvalds
317c0f6ba6SLinus Torvalds #include <linux/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>
38d8429506SDaniel Wagner #include <net/cls_cgroup.h>
39d9f21b36SKuniyuki Iwashima #include <net/af_unix.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
scm_check_creds(struct ucred * creds)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
scm_fp_copy(struct cmsghdr * cmsg,struct scm_fp_list ** fplp)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
741ff8cebfSyuan 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 {
842c6ad20bSVasily Averin fpl = kmalloc(sizeof(struct scm_fp_list), GFP_KERNEL_ACCOUNT);
851da177e4SLinus Torvalds if (!fpl)
861da177e4SLinus Torvalds return -ENOMEM;
871da177e4SLinus Torvalds *fplp = fpl;
881da177e4SLinus Torvalds fpl->count = 0;
89d9f21b36SKuniyuki Iwashima fpl->count_unix = 0;
90bba14de9SEric Dumazet fpl->max = SCM_MAX_FD;
91415e3d3eSHannes Frederic Sowa fpl->user = NULL;
921fbfdfaaSKuniyuki Iwashima #if IS_ENABLED(CONFIG_UNIX)
9342f298c0SKuniyuki Iwashima fpl->inflight = false;
94*7172dc93SKuniyuki Iwashima fpl->dead = false;
9529b64e35SKuniyuki Iwashima fpl->edges = NULL;
961fbfdfaaSKuniyuki Iwashima INIT_LIST_HEAD(&fpl->vertices);
971fbfdfaaSKuniyuki Iwashima #endif
981da177e4SLinus Torvalds }
991da177e4SLinus Torvalds fpp = &fpl->fp[fpl->count];
1001da177e4SLinus Torvalds
101bba14de9SEric Dumazet if (fpl->count + num > fpl->max)
1021da177e4SLinus Torvalds return -EINVAL;
1031da177e4SLinus Torvalds
1041da177e4SLinus Torvalds /*
1051da177e4SLinus Torvalds * Verify the descriptors and increment the usage count.
1061da177e4SLinus Torvalds */
1071da177e4SLinus Torvalds
1081da177e4SLinus Torvalds for (i=0; i< num; i++)
1091da177e4SLinus Torvalds {
1101da177e4SLinus Torvalds int fd = fdp[i];
1111da177e4SLinus Torvalds struct file *file;
1121da177e4SLinus Torvalds
113326be7b4SAl Viro if (fd < 0 || !(file = fget_raw(fd)))
1141da177e4SLinus Torvalds return -EBADF;
115705318a9SPavel Begunkov /* don't allow io_uring files */
116a4104821SJens Axboe if (io_is_uring_fops(file)) {
117705318a9SPavel Begunkov fput(file);
118705318a9SPavel Begunkov return -EINVAL;
119705318a9SPavel Begunkov }
120d9f21b36SKuniyuki Iwashima if (unix_get_socket(file))
121d9f21b36SKuniyuki Iwashima fpl->count_unix++;
122d9f21b36SKuniyuki Iwashima
1231da177e4SLinus Torvalds *fpp++ = file;
1241da177e4SLinus Torvalds fpl->count++;
1251da177e4SLinus Torvalds }
126415e3d3eSHannes Frederic Sowa
127415e3d3eSHannes Frederic Sowa if (!fpl->user)
128415e3d3eSHannes Frederic Sowa fpl->user = get_uid(current_user());
129415e3d3eSHannes Frederic Sowa
1301da177e4SLinus Torvalds return num;
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds
__scm_destroy(struct scm_cookie * scm)1331da177e4SLinus Torvalds void __scm_destroy(struct scm_cookie *scm)
1341da177e4SLinus Torvalds {
1351da177e4SLinus Torvalds struct scm_fp_list *fpl = scm->fp;
1361da177e4SLinus Torvalds int i;
1371da177e4SLinus Torvalds
1381da177e4SLinus Torvalds if (fpl) {
1391da177e4SLinus Torvalds scm->fp = NULL;
1401da177e4SLinus Torvalds for (i=fpl->count-1; i>=0; i--)
1411da177e4SLinus Torvalds fput(fpl->fp[i]);
142415e3d3eSHannes Frederic Sowa free_uid(fpl->user);
1431da177e4SLinus Torvalds kfree(fpl);
1441da177e4SLinus Torvalds }
1451da177e4SLinus Torvalds }
1469e34a5b5SEric Dumazet EXPORT_SYMBOL(__scm_destroy);
1471da177e4SLinus Torvalds
__scm_send(struct socket * sock,struct msghdr * msg,struct scm_cookie * p)1481da177e4SLinus Torvalds int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)
1491da177e4SLinus Torvalds {
1501ded5e5aSEric Dumazet const struct proto_ops *ops = READ_ONCE(sock->ops);
1511da177e4SLinus Torvalds struct cmsghdr *cmsg;
1521da177e4SLinus Torvalds int err;
1531da177e4SLinus Torvalds
154f95b414eSGu Zheng for_each_cmsghdr(cmsg, msg) {
1551da177e4SLinus Torvalds err = -EINVAL;
1561da177e4SLinus Torvalds
1571da177e4SLinus Torvalds /* Verify that cmsg_len is at least sizeof(struct cmsghdr) */
1581da177e4SLinus Torvalds /* The first check was omitted in <= 2.2.5. The reasoning was
1591da177e4SLinus Torvalds that parser checks cmsg_len in any case, so that
1601da177e4SLinus Torvalds additional check would be work duplication.
1611da177e4SLinus Torvalds But if cmsg_level is not SOL_SOCKET, we do not check
1621da177e4SLinus Torvalds for too short ancillary data object at all! Oops.
1631da177e4SLinus Torvalds OK, let's add it...
1641da177e4SLinus Torvalds */
1651da177e4SLinus Torvalds if (!CMSG_OK(msg, cmsg))
1661da177e4SLinus Torvalds goto error;
1671da177e4SLinus Torvalds
1681da177e4SLinus Torvalds if (cmsg->cmsg_level != SOL_SOCKET)
1691da177e4SLinus Torvalds continue;
1701da177e4SLinus Torvalds
1711da177e4SLinus Torvalds switch (cmsg->cmsg_type)
1721da177e4SLinus Torvalds {
1731da177e4SLinus Torvalds case SCM_RIGHTS:
1741ded5e5aSEric Dumazet if (!ops || ops->family != PF_UNIX)
17576dadd76SEric W. Biederman goto error;
1761da177e4SLinus Torvalds err=scm_fp_copy(cmsg, &p->fp);
1771da177e4SLinus Torvalds if (err<0)
1781da177e4SLinus Torvalds goto error;
1791da177e4SLinus Torvalds break;
1801da177e4SLinus Torvalds case SCM_CREDENTIALS:
181b2e4f544SEric W. Biederman {
182dbe9a417SEric W. Biederman struct ucred creds;
183b2e4f544SEric W. Biederman kuid_t uid;
184b2e4f544SEric W. Biederman kgid_t gid;
1851da177e4SLinus Torvalds if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct ucred)))
1861da177e4SLinus Torvalds goto error;
187dbe9a417SEric W. Biederman memcpy(&creds, CMSG_DATA(cmsg), sizeof(struct ucred));
188dbe9a417SEric W. Biederman err = scm_check_creds(&creds);
1891da177e4SLinus Torvalds if (err)
1901da177e4SLinus Torvalds goto error;
191257b5358SEric W. Biederman
192dbe9a417SEric W. Biederman p->creds.pid = creds.pid;
193dbe9a417SEric W. Biederman if (!p->pid || pid_vnr(p->pid) != creds.pid) {
194257b5358SEric W. Biederman struct pid *pid;
195257b5358SEric W. Biederman err = -ESRCH;
196dbe9a417SEric W. Biederman pid = find_get_pid(creds.pid);
197257b5358SEric W. Biederman if (!pid)
198257b5358SEric W. Biederman goto error;
199257b5358SEric W. Biederman put_pid(p->pid);
200257b5358SEric W. Biederman p->pid = pid;
201257b5358SEric W. Biederman }
202257b5358SEric W. Biederman
203b2e4f544SEric W. Biederman err = -EINVAL;
204dbe9a417SEric W. Biederman uid = make_kuid(current_user_ns(), creds.uid);
205dbe9a417SEric W. Biederman gid = make_kgid(current_user_ns(), creds.gid);
206b2e4f544SEric W. Biederman if (!uid_valid(uid) || !gid_valid(gid))
207b2e4f544SEric W. Biederman goto error;
208b2e4f544SEric W. Biederman
209dbe9a417SEric W. Biederman p->creds.uid = uid;
210dbe9a417SEric W. Biederman p->creds.gid = gid;
2111da177e4SLinus Torvalds break;
212b2e4f544SEric W. Biederman }
2131da177e4SLinus Torvalds default:
2141da177e4SLinus Torvalds goto error;
2151da177e4SLinus Torvalds }
2161da177e4SLinus Torvalds }
2171da177e4SLinus Torvalds
2181da177e4SLinus Torvalds if (p->fp && !p->fp->count)
2191da177e4SLinus Torvalds {
2201da177e4SLinus Torvalds kfree(p->fp);
2211da177e4SLinus Torvalds p->fp = NULL;
2221da177e4SLinus Torvalds }
2231da177e4SLinus Torvalds return 0;
2241da177e4SLinus Torvalds
2251da177e4SLinus Torvalds error:
2261da177e4SLinus Torvalds scm_destroy(p);
2271da177e4SLinus Torvalds return err;
2281da177e4SLinus Torvalds }
2299e34a5b5SEric Dumazet EXPORT_SYMBOL(__scm_send);
2301da177e4SLinus Torvalds
put_cmsg(struct msghdr * msg,int level,int type,int len,void * data)2311da177e4SLinus Torvalds int put_cmsg(struct msghdr * msg, int level, int type, int len, void *data)
2321da177e4SLinus Torvalds {
2331da177e4SLinus Torvalds int cmlen = CMSG_LEN(len);
2341da177e4SLinus Torvalds
2351f466e1fSChristoph Hellwig if (msg->msg_flags & MSG_CMSG_COMPAT)
2361da177e4SLinus Torvalds return put_cmsg_compat(msg, level, type, len, data);
2371da177e4SLinus Torvalds
2381f466e1fSChristoph Hellwig if (!msg->msg_control || msg->msg_controllen < sizeof(struct cmsghdr)) {
2391da177e4SLinus Torvalds msg->msg_flags |= MSG_CTRUNC;
2401da177e4SLinus Torvalds return 0; /* XXX: return error? check spec. */
2411da177e4SLinus Torvalds }
2421da177e4SLinus Torvalds if (msg->msg_controllen < cmlen) {
2431da177e4SLinus Torvalds msg->msg_flags |= MSG_CTRUNC;
2441da177e4SLinus Torvalds cmlen = msg->msg_controllen;
2451da177e4SLinus Torvalds }
2461f466e1fSChristoph Hellwig
2471f466e1fSChristoph Hellwig if (msg->msg_control_is_user) {
2481f466e1fSChristoph Hellwig struct cmsghdr __user *cm = msg->msg_control_user;
2491f466e1fSChristoph Hellwig
2505f1eb1ffSEric Dumazet check_object_size(data, cmlen - sizeof(*cm), true);
2515f1eb1ffSEric Dumazet
25238ebcf50SEric Dumazet if (!user_write_access_begin(cm, cmlen))
25338ebcf50SEric Dumazet goto efault;
25438ebcf50SEric Dumazet
255e7ad33faSEric Dumazet unsafe_put_user(cmlen, &cm->cmsg_len, efault_end);
25638ebcf50SEric Dumazet unsafe_put_user(level, &cm->cmsg_level, efault_end);
25738ebcf50SEric Dumazet unsafe_put_user(type, &cm->cmsg_type, efault_end);
25838ebcf50SEric Dumazet unsafe_copy_to_user(CMSG_USER_DATA(cm), data,
25938ebcf50SEric Dumazet cmlen - sizeof(*cm), efault_end);
26038ebcf50SEric Dumazet user_write_access_end();
2611f466e1fSChristoph Hellwig } else {
2621f466e1fSChristoph Hellwig struct cmsghdr *cm = msg->msg_control;
2631da177e4SLinus Torvalds
2641f466e1fSChristoph Hellwig cm->cmsg_level = level;
2651f466e1fSChristoph Hellwig cm->cmsg_type = type;
2661f466e1fSChristoph Hellwig cm->cmsg_len = cmlen;
2671f466e1fSChristoph Hellwig memcpy(CMSG_DATA(cm), data, cmlen - sizeof(*cm));
2681f466e1fSChristoph Hellwig }
2691f466e1fSChristoph Hellwig
2701f466e1fSChristoph Hellwig cmlen = min(CMSG_SPACE(len), msg->msg_controllen);
271c39ef213SKevin Brodsky if (msg->msg_control_is_user)
272c39ef213SKevin Brodsky msg->msg_control_user += cmlen;
273c39ef213SKevin Brodsky else
2741da177e4SLinus Torvalds msg->msg_control += cmlen;
2751da177e4SLinus Torvalds msg->msg_controllen -= cmlen;
2761f466e1fSChristoph Hellwig return 0;
27738ebcf50SEric Dumazet
27838ebcf50SEric Dumazet efault_end:
27938ebcf50SEric Dumazet user_write_access_end();
28038ebcf50SEric Dumazet efault:
28138ebcf50SEric Dumazet return -EFAULT;
2821da177e4SLinus Torvalds }
2839e34a5b5SEric Dumazet EXPORT_SYMBOL(put_cmsg);
2841da177e4SLinus Torvalds
put_cmsg_scm_timestamping64(struct msghdr * msg,struct scm_timestamping_internal * tss_internal)2859718475eSDeepa Dinamani void put_cmsg_scm_timestamping64(struct msghdr *msg, struct scm_timestamping_internal *tss_internal)
2869718475eSDeepa Dinamani {
2879718475eSDeepa Dinamani struct scm_timestamping64 tss;
2889718475eSDeepa Dinamani int i;
2899718475eSDeepa Dinamani
2909718475eSDeepa Dinamani for (i = 0; i < ARRAY_SIZE(tss.ts); i++) {
2919718475eSDeepa Dinamani tss.ts[i].tv_sec = tss_internal->ts[i].tv_sec;
2929718475eSDeepa Dinamani tss.ts[i].tv_nsec = tss_internal->ts[i].tv_nsec;
2939718475eSDeepa Dinamani }
2949718475eSDeepa Dinamani
2959718475eSDeepa Dinamani put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPING_NEW, sizeof(tss), &tss);
2969718475eSDeepa Dinamani }
2979718475eSDeepa Dinamani EXPORT_SYMBOL(put_cmsg_scm_timestamping64);
2989718475eSDeepa Dinamani
put_cmsg_scm_timestamping(struct msghdr * msg,struct scm_timestamping_internal * tss_internal)2999718475eSDeepa Dinamani void put_cmsg_scm_timestamping(struct msghdr *msg, struct scm_timestamping_internal *tss_internal)
3009718475eSDeepa Dinamani {
3019718475eSDeepa Dinamani struct scm_timestamping tss;
3029718475eSDeepa Dinamani int i;
3039718475eSDeepa Dinamani
3040309f98fSArnd Bergmann for (i = 0; i < ARRAY_SIZE(tss.ts); i++) {
3050309f98fSArnd Bergmann tss.ts[i].tv_sec = tss_internal->ts[i].tv_sec;
3060309f98fSArnd Bergmann tss.ts[i].tv_nsec = tss_internal->ts[i].tv_nsec;
3070309f98fSArnd Bergmann }
3089718475eSDeepa Dinamani
3099718475eSDeepa Dinamani put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPING_OLD, sizeof(tss), &tss);
3109718475eSDeepa Dinamani }
3119718475eSDeepa Dinamani EXPORT_SYMBOL(put_cmsg_scm_timestamping);
3129718475eSDeepa Dinamani
scm_max_fds(struct msghdr * msg)3132618d530SChristoph Hellwig static int scm_max_fds(struct msghdr *msg)
3141da177e4SLinus Torvalds {
3152618d530SChristoph Hellwig if (msg->msg_controllen <= sizeof(struct cmsghdr))
3162618d530SChristoph Hellwig return 0;
3172618d530SChristoph Hellwig return (msg->msg_controllen - sizeof(struct cmsghdr)) / sizeof(int);
3182618d530SChristoph Hellwig }
3192618d530SChristoph Hellwig
scm_detach_fds(struct msghdr * msg,struct scm_cookie * scm)3202618d530SChristoph Hellwig void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm)
3212618d530SChristoph Hellwig {
322c0029de5SKees Cook struct cmsghdr __user *cm =
323c39ef213SKevin Brodsky (__force struct cmsghdr __user *)msg->msg_control_user;
324c0029de5SKees Cook unsigned int o_flags = (msg->msg_flags & MSG_CMSG_CLOEXEC) ? O_CLOEXEC : 0;
3252618d530SChristoph Hellwig int fdmax = min_t(int, scm_max_fds(msg), scm->fp->count);
3262618d530SChristoph Hellwig int __user *cmsg_data = CMSG_USER_DATA(cm);
3272618d530SChristoph Hellwig int err = 0, i;
3282618d530SChristoph Hellwig
329c0029de5SKees Cook /* no use for FD passing from kernel space callers */
330c0029de5SKees Cook if (WARN_ON_ONCE(!msg->msg_control_is_user))
331c0029de5SKees Cook return;
332c0029de5SKees Cook
3332618d530SChristoph Hellwig if (msg->msg_flags & MSG_CMSG_COMPAT) {
3342618d530SChristoph Hellwig scm_detach_fds_compat(msg, scm);
3352618d530SChristoph Hellwig return;
3362618d530SChristoph Hellwig }
3372618d530SChristoph Hellwig
3382618d530SChristoph Hellwig for (i = 0; i < fdmax; i++) {
339eac9189cSChristian Brauner err = scm_recv_one_fd(scm->fp->fp[i], cmsg_data + i, o_flags);
340deefa7f3SKees Cook if (err < 0)
3412618d530SChristoph Hellwig break;
3422618d530SChristoph Hellwig }
3432618d530SChristoph Hellwig
3442618d530SChristoph Hellwig if (i > 0) {
3451da177e4SLinus Torvalds int cmlen = CMSG_LEN(i * sizeof(int));
3462618d530SChristoph Hellwig
3471da177e4SLinus Torvalds err = put_user(SOL_SOCKET, &cm->cmsg_level);
3481da177e4SLinus Torvalds if (!err)
3491da177e4SLinus Torvalds err = put_user(SCM_RIGHTS, &cm->cmsg_type);
3501da177e4SLinus Torvalds if (!err)
3511da177e4SLinus Torvalds err = put_user(cmlen, &cm->cmsg_len);
3521da177e4SLinus Torvalds if (!err) {
3531da177e4SLinus Torvalds cmlen = CMSG_SPACE(i * sizeof(int));
3546900317fSDaniel Borkmann if (msg->msg_controllen < cmlen)
3556900317fSDaniel Borkmann cmlen = msg->msg_controllen;
356c39ef213SKevin Brodsky msg->msg_control_user += cmlen;
3571da177e4SLinus Torvalds msg->msg_controllen -= cmlen;
3581da177e4SLinus Torvalds }
3591da177e4SLinus Torvalds }
3602618d530SChristoph Hellwig
3612618d530SChristoph Hellwig if (i < scm->fp->count || (scm->fp->count && fdmax <= 0))
3621da177e4SLinus Torvalds msg->msg_flags |= MSG_CTRUNC;
3631da177e4SLinus Torvalds
3641da177e4SLinus Torvalds /*
3652618d530SChristoph Hellwig * All of the files that fit in the message have had their usage counts
3662618d530SChristoph Hellwig * incremented, so we just free the list.
3671da177e4SLinus Torvalds */
3681da177e4SLinus Torvalds __scm_destroy(scm);
3691da177e4SLinus Torvalds }
3709e34a5b5SEric Dumazet EXPORT_SYMBOL(scm_detach_fds);
3711da177e4SLinus Torvalds
scm_fp_dup(struct scm_fp_list * fpl)3721da177e4SLinus Torvalds struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl)
3731da177e4SLinus Torvalds {
3741da177e4SLinus Torvalds struct scm_fp_list *new_fpl;
3751da177e4SLinus Torvalds int i;
3761da177e4SLinus Torvalds
3771da177e4SLinus Torvalds if (!fpl)
3781da177e4SLinus Torvalds return NULL;
3791da177e4SLinus Torvalds
380bba14de9SEric Dumazet new_fpl = kmemdup(fpl, offsetof(struct scm_fp_list, fp[fpl->count]),
3812c6ad20bSVasily Averin GFP_KERNEL_ACCOUNT);
3821da177e4SLinus Torvalds if (new_fpl) {
383bba14de9SEric Dumazet for (i = 0; i < fpl->count; i++)
3841da177e4SLinus Torvalds get_file(fpl->fp[i]);
3851fbfdfaaSKuniyuki Iwashima
386bba14de9SEric Dumazet new_fpl->max = new_fpl->count;
387415e3d3eSHannes Frederic Sowa new_fpl->user = get_uid(fpl->user);
3881fbfdfaaSKuniyuki Iwashima #if IS_ENABLED(CONFIG_UNIX)
38942f298c0SKuniyuki Iwashima new_fpl->inflight = false;
39029b64e35SKuniyuki Iwashima new_fpl->edges = NULL;
3911fbfdfaaSKuniyuki Iwashima INIT_LIST_HEAD(&new_fpl->vertices);
3921fbfdfaaSKuniyuki Iwashima #endif
3931da177e4SLinus Torvalds }
3941da177e4SLinus Torvalds return new_fpl;
3951da177e4SLinus Torvalds }
3961da177e4SLinus Torvalds EXPORT_SYMBOL(scm_fp_dup);
397