Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-next
[firefly-linux-kernel-4.4.55.git] / net / core / scm.c
1 /* scm.c - Socket level control messages processing.
2  *
3  * Author:      Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
4  *              Alignment and value checking mods by Craig Metz
5  *
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.
10  */
11
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/mm.h>
18 #include <linux/kernel.h>
19 #include <linux/stat.h>
20 #include <linux/socket.h>
21 #include <linux/file.h>
22 #include <linux/fcntl.h>
23 #include <linux/net.h>
24 #include <linux/interrupt.h>
25 #include <linux/netdevice.h>
26 #include <linux/security.h>
27 #include <linux/pid.h>
28 #include <linux/nsproxy.h>
29 #include <linux/slab.h>
30
31 #include <asm/uaccess.h>
32
33 #include <net/protocol.h>
34 #include <linux/skbuff.h>
35 #include <net/sock.h>
36 #include <net/compat.h>
37 #include <net/scm.h>
38
39
40 /*
41  *      Only allow a user to send credentials, that they could set with
42  *      setu(g)id.
43  */
44
45 static __inline__ int scm_check_creds(struct ucred *creds)
46 {
47         const struct cred *cred = current_cred();
48         kuid_t uid = make_kuid(cred->user_ns, creds->uid);
49         kgid_t gid = make_kgid(cred->user_ns, creds->gid);
50
51         if (!uid_valid(uid) || !gid_valid(gid))
52                 return -EINVAL;
53
54         if ((creds->pid == task_tgid_vnr(current) || capable(CAP_SYS_ADMIN)) &&
55             ((uid_eq(uid, cred->uid)   || uid_eq(uid, cred->euid) ||
56               uid_eq(uid, cred->suid)) || capable(CAP_SETUID)) &&
57             ((gid_eq(gid, cred->gid)   || gid_eq(gid, cred->egid) ||
58               gid_eq(gid, cred->sgid)) || capable(CAP_SETGID))) {
59                return 0;
60         }
61         return -EPERM;
62 }
63
64 static int scm_fp_copy(struct cmsghdr *cmsg, struct scm_fp_list **fplp)
65 {
66         int *fdp = (int*)CMSG_DATA(cmsg);
67         struct scm_fp_list *fpl = *fplp;
68         struct file **fpp;
69         int i, num;
70
71         num = (cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr)))/sizeof(int);
72
73         if (num <= 0)
74                 return 0;
75
76         if (num > SCM_MAX_FD)
77                 return -EINVAL;
78
79         if (!fpl)
80         {
81                 fpl = kmalloc(sizeof(struct scm_fp_list), GFP_KERNEL);
82                 if (!fpl)
83                         return -ENOMEM;
84                 *fplp = fpl;
85                 fpl->count = 0;
86                 fpl->max = SCM_MAX_FD;
87         }
88         fpp = &fpl->fp[fpl->count];
89
90         if (fpl->count + num > fpl->max)
91                 return -EINVAL;
92
93         /*
94          *      Verify the descriptors and increment the usage count.
95          */
96
97         for (i=0; i< num; i++)
98         {
99                 int fd = fdp[i];
100                 struct file *file;
101
102                 if (fd < 0 || !(file = fget_raw(fd)))
103                         return -EBADF;
104                 *fpp++ = file;
105                 fpl->count++;
106         }
107         return num;
108 }
109
110 void __scm_destroy(struct scm_cookie *scm)
111 {
112         struct scm_fp_list *fpl = scm->fp;
113         int i;
114
115         if (fpl) {
116                 scm->fp = NULL;
117                 for (i=fpl->count-1; i>=0; i--)
118                         fput(fpl->fp[i]);
119                 kfree(fpl);
120         }
121 }
122 EXPORT_SYMBOL(__scm_destroy);
123
124 int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)
125 {
126         struct cmsghdr *cmsg;
127         int err;
128
129         for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg))
130         {
131                 err = -EINVAL;
132
133                 /* Verify that cmsg_len is at least sizeof(struct cmsghdr) */
134                 /* The first check was omitted in <= 2.2.5. The reasoning was
135                    that parser checks cmsg_len in any case, so that
136                    additional check would be work duplication.
137                    But if cmsg_level is not SOL_SOCKET, we do not check
138                    for too short ancillary data object at all! Oops.
139                    OK, let's add it...
140                  */
141                 if (!CMSG_OK(msg, cmsg))
142                         goto error;
143
144                 if (cmsg->cmsg_level != SOL_SOCKET)
145                         continue;
146
147                 switch (cmsg->cmsg_type)
148                 {
149                 case SCM_RIGHTS:
150                         if (!sock->ops || sock->ops->family != PF_UNIX)
151                                 goto error;
152                         err=scm_fp_copy(cmsg, &p->fp);
153                         if (err<0)
154                                 goto error;
155                         break;
156                 case SCM_CREDENTIALS:
157                 {
158                         kuid_t uid;
159                         kgid_t gid;
160                         if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct ucred)))
161                                 goto error;
162                         memcpy(&p->creds, CMSG_DATA(cmsg), sizeof(struct ucred));
163                         err = scm_check_creds(&p->creds);
164                         if (err)
165                                 goto error;
166
167                         if (!p->pid || pid_vnr(p->pid) != p->creds.pid) {
168                                 struct pid *pid;
169                                 err = -ESRCH;
170                                 pid = find_get_pid(p->creds.pid);
171                                 if (!pid)
172                                         goto error;
173                                 put_pid(p->pid);
174                                 p->pid = pid;
175                         }
176
177                         err = -EINVAL;
178                         uid = make_kuid(current_user_ns(), p->creds.uid);
179                         gid = make_kgid(current_user_ns(), p->creds.gid);
180                         if (!uid_valid(uid) || !gid_valid(gid))
181                                 goto error;
182
183                         if (!p->cred ||
184                             !uid_eq(p->cred->euid, uid) ||
185                             !gid_eq(p->cred->egid, gid)) {
186                                 struct cred *cred;
187                                 err = -ENOMEM;
188                                 cred = prepare_creds();
189                                 if (!cred)
190                                         goto error;
191
192                                 cred->uid = cred->euid = uid;
193                                 cred->gid = cred->egid = gid;
194                                 if (p->cred)
195                                         put_cred(p->cred);
196                                 p->cred = cred;
197                         }
198                         break;
199                 }
200                 default:
201                         goto error;
202                 }
203         }
204
205         if (p->fp && !p->fp->count)
206         {
207                 kfree(p->fp);
208                 p->fp = NULL;
209         }
210         return 0;
211
212 error:
213         scm_destroy(p);
214         return err;
215 }
216 EXPORT_SYMBOL(__scm_send);
217
218 int put_cmsg(struct msghdr * msg, int level, int type, int len, void *data)
219 {
220         struct cmsghdr __user *cm
221                 = (__force struct cmsghdr __user *)msg->msg_control;
222         struct cmsghdr cmhdr;
223         int cmlen = CMSG_LEN(len);
224         int err;
225
226         if (MSG_CMSG_COMPAT & msg->msg_flags)
227                 return put_cmsg_compat(msg, level, type, len, data);
228
229         if (cm==NULL || msg->msg_controllen < sizeof(*cm)) {
230                 msg->msg_flags |= MSG_CTRUNC;
231                 return 0; /* XXX: return error? check spec. */
232         }
233         if (msg->msg_controllen < cmlen) {
234                 msg->msg_flags |= MSG_CTRUNC;
235                 cmlen = msg->msg_controllen;
236         }
237         cmhdr.cmsg_level = level;
238         cmhdr.cmsg_type = type;
239         cmhdr.cmsg_len = cmlen;
240
241         err = -EFAULT;
242         if (copy_to_user(cm, &cmhdr, sizeof cmhdr))
243                 goto out;
244         if (copy_to_user(CMSG_DATA(cm), data, cmlen - sizeof(struct cmsghdr)))
245                 goto out;
246         cmlen = CMSG_SPACE(len);
247         if (msg->msg_controllen < cmlen)
248                 cmlen = msg->msg_controllen;
249         msg->msg_control += cmlen;
250         msg->msg_controllen -= cmlen;
251         err = 0;
252 out:
253         return err;
254 }
255 EXPORT_SYMBOL(put_cmsg);
256
257 void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm)
258 {
259         struct cmsghdr __user *cm
260                 = (__force struct cmsghdr __user*)msg->msg_control;
261
262         int fdmax = 0;
263         int fdnum = scm->fp->count;
264         struct file **fp = scm->fp->fp;
265         int __user *cmfptr;
266         int err = 0, i;
267
268         if (MSG_CMSG_COMPAT & msg->msg_flags) {
269                 scm_detach_fds_compat(msg, scm);
270                 return;
271         }
272
273         if (msg->msg_controllen > sizeof(struct cmsghdr))
274                 fdmax = ((msg->msg_controllen - sizeof(struct cmsghdr))
275                          / sizeof(int));
276
277         if (fdnum < fdmax)
278                 fdmax = fdnum;
279
280         for (i=0, cmfptr=(__force int __user *)CMSG_DATA(cm); i<fdmax;
281              i++, cmfptr++)
282         {
283                 struct socket *sock;
284                 int new_fd;
285                 err = security_file_receive(fp[i]);
286                 if (err)
287                         break;
288                 err = get_unused_fd_flags(MSG_CMSG_CLOEXEC & msg->msg_flags
289                                           ? O_CLOEXEC : 0);
290                 if (err < 0)
291                         break;
292                 new_fd = err;
293                 err = put_user(new_fd, cmfptr);
294                 if (err) {
295                         put_unused_fd(new_fd);
296                         break;
297                 }
298                 /* Bump the usage count and install the file. */
299                 get_file(fp[i]);
300                 sock = sock_from_file(fp[i], &err);
301                 if (sock)
302                         sock_update_netprioidx(sock->sk, current);
303                 fd_install(new_fd, fp[i]);
304         }
305
306         if (i > 0)
307         {
308                 int cmlen = CMSG_LEN(i*sizeof(int));
309                 err = put_user(SOL_SOCKET, &cm->cmsg_level);
310                 if (!err)
311                         err = put_user(SCM_RIGHTS, &cm->cmsg_type);
312                 if (!err)
313                         err = put_user(cmlen, &cm->cmsg_len);
314                 if (!err) {
315                         cmlen = CMSG_SPACE(i*sizeof(int));
316                         msg->msg_control += cmlen;
317                         msg->msg_controllen -= cmlen;
318                 }
319         }
320         if (i < fdnum || (fdnum && fdmax <= 0))
321                 msg->msg_flags |= MSG_CTRUNC;
322
323         /*
324          * All of the files that fit in the message have had their
325          * usage counts incremented, so we just free the list.
326          */
327         __scm_destroy(scm);
328 }
329 EXPORT_SYMBOL(scm_detach_fds);
330
331 struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl)
332 {
333         struct scm_fp_list *new_fpl;
334         int i;
335
336         if (!fpl)
337                 return NULL;
338
339         new_fpl = kmemdup(fpl, offsetof(struct scm_fp_list, fp[fpl->count]),
340                           GFP_KERNEL);
341         if (new_fpl) {
342                 for (i = 0; i < fpl->count; i++)
343                         get_file(fpl->fp[i]);
344                 new_fpl->max = new_fpl->count;
345         }
346         return new_fpl;
347 }
348 EXPORT_SYMBOL(scm_fp_dup);