1 /* scm.c - Socket level control messages processing.
4 * Alignment and value checking mods by Craig Metz
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/module.h>
13 #include <linux/signal.h>
14 #include <linux/capability.h>
15 #include <linux/errno.h>
16 #include <linux/sched.h>
17 #include <linux/sched/user.h>
19 #include <linux/kernel.h>
20 #include <linux/stat.h>
21 #include <linux/socket.h>
22 #include <linux/file.h>
23 #include <linux/fcntl.h>
24 #include <linux/net.h>
25 #include <linux/interrupt.h>
26 #include <linux/netdevice.h>
27 #include <linux/security.h>
28 #include <linux/pid_namespace.h>
29 #include <linux/pid.h>
30 #include <linux/nsproxy.h>
31 #include <linux/slab.h>
32 #include <linux/errqueue.h>
34 #include <linux/uaccess.h>
36 #include <net/protocol.h>
37 #include <linux/skbuff.h>
39 #include <net/compat.h>
41 #include <net/cls_cgroup.h>
45 * Only allow a user to send credentials, that they could set with
49 static __inline__ int scm_check_creds(struct ucred *creds)
51 const struct cred *cred = current_cred();
52 kuid_t uid = make_kuid(cred->user_ns, creds->uid);
53 kgid_t gid = make_kgid(cred->user_ns, creds->gid);
55 if (!uid_valid(uid) || !gid_valid(gid))
58 if ((creds->pid == task_tgid_vnr(current) ||
59 ns_capable(task_active_pid_ns(current)->user_ns, CAP_SYS_ADMIN)) &&
60 ((uid_eq(uid, cred->uid) || uid_eq(uid, cred->euid) ||
61 uid_eq(uid, cred->suid)) || ns_capable(cred->user_ns, CAP_SETUID)) &&
62 ((gid_eq(gid, cred->gid) || gid_eq(gid, cred->egid) ||
63 gid_eq(gid, cred->sgid)) || ns_capable(cred->user_ns, CAP_SETGID))) {
69 static int scm_fp_copy(struct cmsghdr *cmsg, struct scm_fp_list **fplp)
71 int *fdp = (int*)CMSG_DATA(cmsg);
72 struct scm_fp_list *fpl = *fplp;
76 num = (cmsg->cmsg_len - sizeof(struct cmsghdr))/sizeof(int);
86 fpl = kmalloc(sizeof(struct scm_fp_list), GFP_KERNEL);
91 fpl->max = SCM_MAX_FD;
94 fpp = &fpl->fp[fpl->count];
96 if (fpl->count + num > fpl->max)
100 * Verify the descriptors and increment the usage count.
103 for (i=0; i< num; i++)
108 if (fd < 0 || !(file = fget_raw(fd)))
115 fpl->user = get_uid(current_user());
120 void __scm_destroy(struct scm_cookie *scm)
122 struct scm_fp_list *fpl = scm->fp;
127 for (i=fpl->count-1; i>=0; i--)
133 EXPORT_SYMBOL(__scm_destroy);
135 int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)
137 struct cmsghdr *cmsg;
140 for_each_cmsghdr(cmsg, msg) {
143 /* Verify that cmsg_len is at least sizeof(struct cmsghdr) */
144 /* The first check was omitted in <= 2.2.5. The reasoning was
145 that parser checks cmsg_len in any case, so that
146 additional check would be work duplication.
147 But if cmsg_level is not SOL_SOCKET, we do not check
148 for too short ancillary data object at all! Oops.
151 if (!CMSG_OK(msg, cmsg))
154 if (cmsg->cmsg_level != SOL_SOCKET)
157 switch (cmsg->cmsg_type)
160 if (!sock->ops || sock->ops->family != PF_UNIX)
162 err=scm_fp_copy(cmsg, &p->fp);
166 case SCM_CREDENTIALS:
171 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct ucred)))
173 memcpy(&creds, CMSG_DATA(cmsg), sizeof(struct ucred));
174 err = scm_check_creds(&creds);
178 p->creds.pid = creds.pid;
179 if (!p->pid || pid_vnr(p->pid) != creds.pid) {
182 pid = find_get_pid(creds.pid);
190 uid = make_kuid(current_user_ns(), creds.uid);
191 gid = make_kgid(current_user_ns(), creds.gid);
192 if (!uid_valid(uid) || !gid_valid(gid))
204 if (p->fp && !p->fp->count)
215 EXPORT_SYMBOL(__scm_send);
217 int put_cmsg(struct msghdr * msg, int level, int type, int len, void *data)
219 struct cmsghdr __user *cm
220 = (__force struct cmsghdr __user *)msg->msg_control;
221 struct cmsghdr cmhdr;
222 int cmlen = CMSG_LEN(len);
225 if (MSG_CMSG_COMPAT & msg->msg_flags)
226 return put_cmsg_compat(msg, level, type, len, data);
228 if (cm==NULL || msg->msg_controllen < sizeof(*cm)) {
229 msg->msg_flags |= MSG_CTRUNC;
230 return 0; /* XXX: return error? check spec. */
232 if (msg->msg_controllen < cmlen) {
233 msg->msg_flags |= MSG_CTRUNC;
234 cmlen = msg->msg_controllen;
236 cmhdr.cmsg_level = level;
237 cmhdr.cmsg_type = type;
238 cmhdr.cmsg_len = cmlen;
241 if (copy_to_user(cm, &cmhdr, sizeof cmhdr))
243 if (copy_to_user(CMSG_DATA(cm), data, cmlen - sizeof(struct cmsghdr)))
245 cmlen = CMSG_SPACE(len);
246 if (msg->msg_controllen < cmlen)
247 cmlen = msg->msg_controllen;
248 msg->msg_control += cmlen;
249 msg->msg_controllen -= cmlen;
254 EXPORT_SYMBOL(put_cmsg);
256 void put_cmsg_scm_timestamping64(struct msghdr *msg, struct scm_timestamping_internal *tss_internal)
258 struct scm_timestamping64 tss;
261 for (i = 0; i < ARRAY_SIZE(tss.ts); i++) {
262 tss.ts[i].tv_sec = tss_internal->ts[i].tv_sec;
263 tss.ts[i].tv_nsec = tss_internal->ts[i].tv_nsec;
266 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPING_NEW, sizeof(tss), &tss);
268 EXPORT_SYMBOL(put_cmsg_scm_timestamping64);
270 void put_cmsg_scm_timestamping(struct msghdr *msg, struct scm_timestamping_internal *tss_internal)
272 struct scm_timestamping tss;
275 for (i = 0; i < ARRAY_SIZE(tss.ts); i++)
276 tss.ts[i] = timespec64_to_timespec(tss_internal->ts[i]);
278 put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPING_OLD, sizeof(tss), &tss);
280 EXPORT_SYMBOL(put_cmsg_scm_timestamping);
282 void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm)
284 struct cmsghdr __user *cm
285 = (__force struct cmsghdr __user*)msg->msg_control;
288 int fdnum = scm->fp->count;
289 struct file **fp = scm->fp->fp;
293 if (MSG_CMSG_COMPAT & msg->msg_flags) {
294 scm_detach_fds_compat(msg, scm);
298 if (msg->msg_controllen > sizeof(struct cmsghdr))
299 fdmax = ((msg->msg_controllen - sizeof(struct cmsghdr))
305 for (i=0, cmfptr=(__force int __user *)CMSG_DATA(cm); i<fdmax;
310 err = security_file_receive(fp[i]);
313 err = get_unused_fd_flags(MSG_CMSG_CLOEXEC & msg->msg_flags
318 err = put_user(new_fd, cmfptr);
320 put_unused_fd(new_fd);
323 /* Bump the usage count and install the file. */
324 sock = sock_from_file(fp[i], &err);
326 sock_update_netprioidx(&sock->sk->sk_cgrp_data);
327 sock_update_classid(&sock->sk->sk_cgrp_data);
329 fd_install(new_fd, get_file(fp[i]));
334 int cmlen = CMSG_LEN(i*sizeof(int));
335 err = put_user(SOL_SOCKET, &cm->cmsg_level);
337 err = put_user(SCM_RIGHTS, &cm->cmsg_type);
339 err = put_user(cmlen, &cm->cmsg_len);
341 cmlen = CMSG_SPACE(i*sizeof(int));
342 if (msg->msg_controllen < cmlen)
343 cmlen = msg->msg_controllen;
344 msg->msg_control += cmlen;
345 msg->msg_controllen -= cmlen;
348 if (i < fdnum || (fdnum && fdmax <= 0))
349 msg->msg_flags |= MSG_CTRUNC;
352 * All of the files that fit in the message have had their
353 * usage counts incremented, so we just free the list.
357 EXPORT_SYMBOL(scm_detach_fds);
359 struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl)
361 struct scm_fp_list *new_fpl;
367 new_fpl = kmemdup(fpl, offsetof(struct scm_fp_list, fp[fpl->count]),
370 for (i = 0; i < fpl->count; i++)
371 get_file(fpl->fp[i]);
372 new_fpl->max = new_fpl->count;
373 new_fpl->user = get_uid(fpl->user);
377 EXPORT_SYMBOL(scm_fp_dup);