1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * 32bit Socket syscall emulation. Based on arch/sparc64/kernel/sys_sparc32.c.
4 *
5 * Copyright (C) 2000 VA Linux Co
6 * Copyright (C) 2000 Don Dugger <n0ano@valinux.com>
7 * Copyright (C) 1999 Arun Sharma <arun.sharma@intel.com>
8 * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
9 * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
10 * Copyright (C) 2000 Hewlett-Packard Co.
11 * Copyright (C) 2000 David Mosberger-Tang <davidm@hpl.hp.com>
12 * Copyright (C) 2000,2001 Andi Kleen, SuSE Labs
13 */
14
15 #include <linux/kernel.h>
16 #include <linux/gfp.h>
17 #include <linux/fs.h>
18 #include <linux/types.h>
19 #include <linux/file.h>
20 #include <linux/icmpv6.h>
21 #include <linux/socket.h>
22 #include <linux/syscalls.h>
23 #include <linux/filter.h>
24 #include <linux/compat.h>
25 #include <linux/security.h>
26 #include <linux/audit.h>
27 #include <linux/export.h>
28
29 #include <net/scm.h>
30 #include <net/sock.h>
31 #include <net/ip.h>
32 #include <net/ipv6.h>
33 #include <linux/uaccess.h>
34 #include <net/compat.h>
35
get_compat_msghdr(struct msghdr * kmsg,struct compat_msghdr __user * umsg,struct sockaddr __user ** save_addr,struct iovec ** iov)36 int get_compat_msghdr(struct msghdr *kmsg,
37 struct compat_msghdr __user *umsg,
38 struct sockaddr __user **save_addr,
39 struct iovec **iov)
40 {
41 struct compat_msghdr msg;
42 ssize_t err;
43
44 if (copy_from_user(&msg, umsg, sizeof(*umsg)))
45 return -EFAULT;
46
47 kmsg->msg_flags = msg.msg_flags;
48 kmsg->msg_namelen = msg.msg_namelen;
49
50 if (!msg.msg_name)
51 kmsg->msg_namelen = 0;
52
53 if (kmsg->msg_namelen < 0)
54 return -EINVAL;
55
56 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
57 kmsg->msg_namelen = sizeof(struct sockaddr_storage);
58
59 kmsg->msg_control = compat_ptr(msg.msg_control);
60 kmsg->msg_controllen = msg.msg_controllen;
61
62 if (save_addr)
63 *save_addr = compat_ptr(msg.msg_name);
64
65 if (msg.msg_name && kmsg->msg_namelen) {
66 if (!save_addr) {
67 err = move_addr_to_kernel(compat_ptr(msg.msg_name),
68 kmsg->msg_namelen,
69 kmsg->msg_name);
70 if (err < 0)
71 return err;
72 }
73 } else {
74 kmsg->msg_name = NULL;
75 kmsg->msg_namelen = 0;
76 }
77
78 if (msg.msg_iovlen > UIO_MAXIOV)
79 return -EMSGSIZE;
80
81 kmsg->msg_iocb = NULL;
82
83 err = compat_import_iovec(save_addr ? READ : WRITE,
84 compat_ptr(msg.msg_iov), msg.msg_iovlen,
85 UIO_FASTIOV, iov, &kmsg->msg_iter);
86 return err < 0 ? err : 0;
87 }
88
89 /* Bleech... */
90 #define CMSG_COMPAT_ALIGN(len) ALIGN((len), sizeof(s32))
91
92 #define CMSG_COMPAT_DATA(cmsg) \
93 ((void __user *)((char __user *)(cmsg) + sizeof(struct compat_cmsghdr)))
94 #define CMSG_COMPAT_SPACE(len) \
95 (sizeof(struct compat_cmsghdr) + CMSG_COMPAT_ALIGN(len))
96 #define CMSG_COMPAT_LEN(len) \
97 (sizeof(struct compat_cmsghdr) + (len))
98
99 #define CMSG_COMPAT_FIRSTHDR(msg) \
100 (((msg)->msg_controllen) >= sizeof(struct compat_cmsghdr) ? \
101 (struct compat_cmsghdr __user *)((msg)->msg_control) : \
102 (struct compat_cmsghdr __user *)NULL)
103
104 #define CMSG_COMPAT_OK(ucmlen, ucmsg, mhdr) \
105 ((ucmlen) >= sizeof(struct compat_cmsghdr) && \
106 (ucmlen) <= (unsigned long) \
107 ((mhdr)->msg_controllen - \
108 ((char *)(ucmsg) - (char *)(mhdr)->msg_control)))
109
cmsg_compat_nxthdr(struct msghdr * msg,struct compat_cmsghdr __user * cmsg,int cmsg_len)110 static inline struct compat_cmsghdr __user *cmsg_compat_nxthdr(struct msghdr *msg,
111 struct compat_cmsghdr __user *cmsg, int cmsg_len)
112 {
113 char __user *ptr = (char __user *)cmsg + CMSG_COMPAT_ALIGN(cmsg_len);
114 if ((unsigned long)(ptr + 1 - (char __user *)msg->msg_control) >
115 msg->msg_controllen)
116 return NULL;
117 return (struct compat_cmsghdr __user *)ptr;
118 }
119
120 /* There is a lot of hair here because the alignment rules (and
121 * thus placement) of cmsg headers and length are different for
122 * 32-bit apps. -DaveM
123 */
cmsghdr_from_user_compat_to_kern(struct msghdr * kmsg,struct sock * sk,unsigned char * stackbuf,int stackbuf_size)124 int cmsghdr_from_user_compat_to_kern(struct msghdr *kmsg, struct sock *sk,
125 unsigned char *stackbuf, int stackbuf_size)
126 {
127 struct compat_cmsghdr __user *ucmsg;
128 struct cmsghdr *kcmsg, *kcmsg_base;
129 compat_size_t ucmlen;
130 __kernel_size_t kcmlen, tmp;
131 int err = -EFAULT;
132
133 BUILD_BUG_ON(sizeof(struct compat_cmsghdr) !=
134 CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)));
135
136 kcmlen = 0;
137 kcmsg_base = kcmsg = (struct cmsghdr *)stackbuf;
138 ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg);
139 while (ucmsg != NULL) {
140 if (get_user(ucmlen, &ucmsg->cmsg_len))
141 return -EFAULT;
142
143 /* Catch bogons. */
144 if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg))
145 return -EINVAL;
146
147 tmp = ((ucmlen - sizeof(*ucmsg)) + sizeof(struct cmsghdr));
148 tmp = CMSG_ALIGN(tmp);
149 kcmlen += tmp;
150 ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen);
151 }
152 if (kcmlen == 0)
153 return -EINVAL;
154
155 /* The kcmlen holds the 64-bit version of the control length.
156 * It may not be modified as we do not stick it into the kmsg
157 * until we have successfully copied over all of the data
158 * from the user.
159 */
160 if (kcmlen > stackbuf_size)
161 kcmsg_base = kcmsg = sock_kmalloc(sk, kcmlen, GFP_KERNEL);
162 if (kcmsg == NULL)
163 return -ENOMEM;
164
165 /* Now copy them over neatly. */
166 memset(kcmsg, 0, kcmlen);
167 ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg);
168 while (ucmsg != NULL) {
169 if (__get_user(ucmlen, &ucmsg->cmsg_len))
170 goto Efault;
171 if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg))
172 goto Einval;
173 tmp = ((ucmlen - sizeof(*ucmsg)) + sizeof(struct cmsghdr));
174 if ((char *)kcmsg_base + kcmlen - (char *)kcmsg < CMSG_ALIGN(tmp))
175 goto Einval;
176 kcmsg->cmsg_len = tmp;
177 tmp = CMSG_ALIGN(tmp);
178 if (__get_user(kcmsg->cmsg_level, &ucmsg->cmsg_level) ||
179 __get_user(kcmsg->cmsg_type, &ucmsg->cmsg_type) ||
180 copy_from_user(CMSG_DATA(kcmsg),
181 CMSG_COMPAT_DATA(ucmsg),
182 (ucmlen - sizeof(*ucmsg))))
183 goto Efault;
184
185 /* Advance. */
186 kcmsg = (struct cmsghdr *)((char *)kcmsg + tmp);
187 ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen);
188 }
189
190 /*
191 * check the length of messages copied in is the same as the
192 * what we get from the first loop
193 */
194 if ((char *)kcmsg - (char *)kcmsg_base != kcmlen)
195 goto Einval;
196
197 /* Ok, looks like we made it. Hook it up and return success. */
198 kmsg->msg_control = kcmsg_base;
199 kmsg->msg_controllen = kcmlen;
200 return 0;
201
202 Einval:
203 err = -EINVAL;
204 Efault:
205 if (kcmsg_base != (struct cmsghdr *)stackbuf)
206 sock_kfree_s(sk, kcmsg_base, kcmlen);
207 return err;
208 }
209
put_cmsg_compat(struct msghdr * kmsg,int level,int type,int len,void * data)210 int put_cmsg_compat(struct msghdr *kmsg, int level, int type, int len, void *data)
211 {
212 struct compat_cmsghdr __user *cm = (struct compat_cmsghdr __user *) kmsg->msg_control;
213 struct compat_cmsghdr cmhdr;
214 struct old_timeval32 ctv;
215 struct old_timespec32 cts[3];
216 int cmlen;
217
218 if (cm == NULL || kmsg->msg_controllen < sizeof(*cm)) {
219 kmsg->msg_flags |= MSG_CTRUNC;
220 return 0; /* XXX: return error? check spec. */
221 }
222
223 if (!COMPAT_USE_64BIT_TIME) {
224 if (level == SOL_SOCKET && type == SO_TIMESTAMP_OLD) {
225 struct __kernel_old_timeval *tv = (struct __kernel_old_timeval *)data;
226 ctv.tv_sec = tv->tv_sec;
227 ctv.tv_usec = tv->tv_usec;
228 data = &ctv;
229 len = sizeof(ctv);
230 }
231 if (level == SOL_SOCKET &&
232 (type == SO_TIMESTAMPNS_OLD || type == SO_TIMESTAMPING_OLD)) {
233 int count = type == SO_TIMESTAMPNS_OLD ? 1 : 3;
234 int i;
235 struct timespec *ts = (struct timespec *)data;
236 for (i = 0; i < count; i++) {
237 cts[i].tv_sec = ts[i].tv_sec;
238 cts[i].tv_nsec = ts[i].tv_nsec;
239 }
240 data = &cts;
241 len = sizeof(cts[0]) * count;
242 }
243 }
244
245 cmlen = CMSG_COMPAT_LEN(len);
246 if (kmsg->msg_controllen < cmlen) {
247 kmsg->msg_flags |= MSG_CTRUNC;
248 cmlen = kmsg->msg_controllen;
249 }
250 cmhdr.cmsg_level = level;
251 cmhdr.cmsg_type = type;
252 cmhdr.cmsg_len = cmlen;
253
254 if (copy_to_user(cm, &cmhdr, sizeof cmhdr))
255 return -EFAULT;
256 if (copy_to_user(CMSG_COMPAT_DATA(cm), data, cmlen - sizeof(struct compat_cmsghdr)))
257 return -EFAULT;
258 cmlen = CMSG_COMPAT_SPACE(len);
259 if (kmsg->msg_controllen < cmlen)
260 cmlen = kmsg->msg_controllen;
261 kmsg->msg_control += cmlen;
262 kmsg->msg_controllen -= cmlen;
263 return 0;
264 }
265
scm_detach_fds_compat(struct msghdr * kmsg,struct scm_cookie * scm)266 void scm_detach_fds_compat(struct msghdr *kmsg, struct scm_cookie *scm)
267 {
268 struct compat_cmsghdr __user *cm = (struct compat_cmsghdr __user *) kmsg->msg_control;
269 int fdmax = (kmsg->msg_controllen - sizeof(struct compat_cmsghdr)) / sizeof(int);
270 int fdnum = scm->fp->count;
271 struct file **fp = scm->fp->fp;
272 int __user *cmfptr;
273 int err = 0, i;
274
275 if (fdnum < fdmax)
276 fdmax = fdnum;
277
278 for (i = 0, cmfptr = (int __user *) CMSG_COMPAT_DATA(cm); i < fdmax; i++, cmfptr++) {
279 int new_fd;
280 err = security_file_receive(fp[i]);
281 if (err)
282 break;
283 err = get_unused_fd_flags(MSG_CMSG_CLOEXEC & kmsg->msg_flags
284 ? O_CLOEXEC : 0);
285 if (err < 0)
286 break;
287 new_fd = err;
288 err = put_user(new_fd, cmfptr);
289 if (err) {
290 put_unused_fd(new_fd);
291 break;
292 }
293 /* Bump the usage count and install the file. */
294 __receive_sock(fp[i]);
295 fd_install(new_fd, get_file(fp[i]));
296 }
297
298 if (i > 0) {
299 int cmlen = CMSG_COMPAT_LEN(i * sizeof(int));
300 err = put_user(SOL_SOCKET, &cm->cmsg_level);
301 if (!err)
302 err = put_user(SCM_RIGHTS, &cm->cmsg_type);
303 if (!err)
304 err = put_user(cmlen, &cm->cmsg_len);
305 if (!err) {
306 cmlen = CMSG_COMPAT_SPACE(i * sizeof(int));
307 kmsg->msg_control += cmlen;
308 kmsg->msg_controllen -= cmlen;
309 }
310 }
311 if (i < fdnum)
312 kmsg->msg_flags |= MSG_CTRUNC;
313
314 /*
315 * All of the files that fit in the message have had their
316 * usage counts incremented, so we just free the list.
317 */
318 __scm_destroy(scm);
319 }
320
321 /* allocate a 64-bit sock_fprog on the user stack for duration of syscall. */
get_compat_bpf_fprog(char __user * optval)322 struct sock_fprog __user *get_compat_bpf_fprog(char __user *optval)
323 {
324 struct compat_sock_fprog __user *fprog32 = (struct compat_sock_fprog __user *)optval;
325 struct sock_fprog __user *kfprog = compat_alloc_user_space(sizeof(struct sock_fprog));
326 struct compat_sock_fprog f32;
327 struct sock_fprog f;
328
329 if (copy_from_user(&f32, fprog32, sizeof(*fprog32)))
330 return NULL;
331 memset(&f, 0, sizeof(f));
332 f.len = f32.len;
333 f.filter = compat_ptr(f32.filter);
334 if (copy_to_user(kfprog, &f, sizeof(struct sock_fprog)))
335 return NULL;
336
337 return kfprog;
338 }
339 EXPORT_SYMBOL_GPL(get_compat_bpf_fprog);
340
do_set_attach_filter(struct socket * sock,int level,int optname,char __user * optval,unsigned int optlen)341 static int do_set_attach_filter(struct socket *sock, int level, int optname,
342 char __user *optval, unsigned int optlen)
343 {
344 struct sock_fprog __user *kfprog;
345
346 kfprog = get_compat_bpf_fprog(optval);
347 if (!kfprog)
348 return -EFAULT;
349
350 return sock_setsockopt(sock, level, optname, (char __user *)kfprog,
351 sizeof(struct sock_fprog));
352 }
353
compat_sock_setsockopt(struct socket * sock,int level,int optname,char __user * optval,unsigned int optlen)354 static int compat_sock_setsockopt(struct socket *sock, int level, int optname,
355 char __user *optval, unsigned int optlen)
356 {
357 if (optname == SO_ATTACH_FILTER ||
358 optname == SO_ATTACH_REUSEPORT_CBPF)
359 return do_set_attach_filter(sock, level, optname,
360 optval, optlen);
361 return sock_setsockopt(sock, level, optname, optval, optlen);
362 }
363
__compat_sys_setsockopt(int fd,int level,int optname,char __user * optval,unsigned int optlen)364 static int __compat_sys_setsockopt(int fd, int level, int optname,
365 char __user *optval, unsigned int optlen)
366 {
367 int err;
368 struct socket *sock;
369
370 if (optlen > INT_MAX)
371 return -EINVAL;
372
373 sock = sockfd_lookup(fd, &err);
374 if (sock) {
375 err = security_socket_setsockopt(sock, level, optname);
376 if (err) {
377 sockfd_put(sock);
378 return err;
379 }
380
381 if (level == SOL_SOCKET)
382 err = compat_sock_setsockopt(sock, level,
383 optname, optval, optlen);
384 else if (sock->ops->compat_setsockopt)
385 err = sock->ops->compat_setsockopt(sock, level,
386 optname, optval, optlen);
387 else
388 err = sock->ops->setsockopt(sock, level,
389 optname, optval, optlen);
390 sockfd_put(sock);
391 }
392 return err;
393 }
394
COMPAT_SYSCALL_DEFINE5(setsockopt,int,fd,int,level,int,optname,char __user *,optval,unsigned int,optlen)395 COMPAT_SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
396 char __user *, optval, unsigned int, optlen)
397 {
398 return __compat_sys_setsockopt(fd, level, optname, optval, optlen);
399 }
400
__compat_sys_getsockopt(int fd,int level,int optname,char __user * optval,int __user * optlen)401 static int __compat_sys_getsockopt(int fd, int level, int optname,
402 char __user *optval,
403 int __user *optlen)
404 {
405 int err;
406 struct socket *sock = sockfd_lookup(fd, &err);
407
408 if (sock) {
409 err = security_socket_getsockopt(sock, level, optname);
410 if (err) {
411 sockfd_put(sock);
412 return err;
413 }
414
415 if (level == SOL_SOCKET)
416 err = sock_getsockopt(sock, level,
417 optname, optval, optlen);
418 else if (sock->ops->compat_getsockopt)
419 err = sock->ops->compat_getsockopt(sock, level,
420 optname, optval, optlen);
421 else
422 err = sock->ops->getsockopt(sock, level,
423 optname, optval, optlen);
424 sockfd_put(sock);
425 }
426 return err;
427 }
428
COMPAT_SYSCALL_DEFINE5(getsockopt,int,fd,int,level,int,optname,char __user *,optval,int __user *,optlen)429 COMPAT_SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
430 char __user *, optval, int __user *, optlen)
431 {
432 return __compat_sys_getsockopt(fd, level, optname, optval, optlen);
433 }
434
435 struct compat_group_req {
436 __u32 gr_interface;
437 struct __kernel_sockaddr_storage gr_group
438 __aligned(4);
439 } __packed;
440
441 struct compat_group_source_req {
442 __u32 gsr_interface;
443 struct __kernel_sockaddr_storage gsr_group
444 __aligned(4);
445 struct __kernel_sockaddr_storage gsr_source
446 __aligned(4);
447 } __packed;
448
449 struct compat_group_filter {
450 __u32 gf_interface;
451 struct __kernel_sockaddr_storage gf_group
452 __aligned(4);
453 __u32 gf_fmode;
454 __u32 gf_numsrc;
455 struct __kernel_sockaddr_storage gf_slist[1]
456 __aligned(4);
457 } __packed;
458
459 #define __COMPAT_GF0_SIZE (sizeof(struct compat_group_filter) - \
460 sizeof(struct __kernel_sockaddr_storage))
461
462
compat_mc_setsockopt(struct sock * sock,int level,int optname,char __user * optval,unsigned int optlen,int (* setsockopt)(struct sock *,int,int,char __user *,unsigned int))463 int compat_mc_setsockopt(struct sock *sock, int level, int optname,
464 char __user *optval, unsigned int optlen,
465 int (*setsockopt)(struct sock *, int, int, char __user *, unsigned int))
466 {
467 char __user *koptval = optval;
468 int koptlen = optlen;
469
470 switch (optname) {
471 case MCAST_JOIN_GROUP:
472 case MCAST_LEAVE_GROUP:
473 {
474 struct compat_group_req __user *gr32 = (void __user *)optval;
475 struct group_req __user *kgr =
476 compat_alloc_user_space(sizeof(struct group_req));
477 u32 interface;
478
479 if (!access_ok(gr32, sizeof(*gr32)) ||
480 !access_ok(kgr, sizeof(struct group_req)) ||
481 __get_user(interface, &gr32->gr_interface) ||
482 __put_user(interface, &kgr->gr_interface) ||
483 copy_in_user(&kgr->gr_group, &gr32->gr_group,
484 sizeof(kgr->gr_group)))
485 return -EFAULT;
486 koptval = (char __user *)kgr;
487 koptlen = sizeof(struct group_req);
488 break;
489 }
490 case MCAST_JOIN_SOURCE_GROUP:
491 case MCAST_LEAVE_SOURCE_GROUP:
492 case MCAST_BLOCK_SOURCE:
493 case MCAST_UNBLOCK_SOURCE:
494 {
495 struct compat_group_source_req __user *gsr32 = (void __user *)optval;
496 struct group_source_req __user *kgsr = compat_alloc_user_space(
497 sizeof(struct group_source_req));
498 u32 interface;
499
500 if (!access_ok(gsr32, sizeof(*gsr32)) ||
501 !access_ok(kgsr,
502 sizeof(struct group_source_req)) ||
503 __get_user(interface, &gsr32->gsr_interface) ||
504 __put_user(interface, &kgsr->gsr_interface) ||
505 copy_in_user(&kgsr->gsr_group, &gsr32->gsr_group,
506 sizeof(kgsr->gsr_group)) ||
507 copy_in_user(&kgsr->gsr_source, &gsr32->gsr_source,
508 sizeof(kgsr->gsr_source)))
509 return -EFAULT;
510 koptval = (char __user *)kgsr;
511 koptlen = sizeof(struct group_source_req);
512 break;
513 }
514 case MCAST_MSFILTER:
515 {
516 struct compat_group_filter __user *gf32 = (void __user *)optval;
517 struct group_filter __user *kgf;
518 u32 interface, fmode, numsrc;
519
520 if (!access_ok(gf32, __COMPAT_GF0_SIZE) ||
521 __get_user(interface, &gf32->gf_interface) ||
522 __get_user(fmode, &gf32->gf_fmode) ||
523 __get_user(numsrc, &gf32->gf_numsrc))
524 return -EFAULT;
525 koptlen = optlen + sizeof(struct group_filter) -
526 sizeof(struct compat_group_filter);
527 if (koptlen < GROUP_FILTER_SIZE(numsrc))
528 return -EINVAL;
529 kgf = compat_alloc_user_space(koptlen);
530 if (!access_ok(kgf, koptlen) ||
531 __put_user(interface, &kgf->gf_interface) ||
532 __put_user(fmode, &kgf->gf_fmode) ||
533 __put_user(numsrc, &kgf->gf_numsrc) ||
534 copy_in_user(&kgf->gf_group, &gf32->gf_group,
535 sizeof(kgf->gf_group)) ||
536 (numsrc && copy_in_user(kgf->gf_slist, gf32->gf_slist,
537 numsrc * sizeof(kgf->gf_slist[0]))))
538 return -EFAULT;
539 koptval = (char __user *)kgf;
540 break;
541 }
542
543 default:
544 break;
545 }
546 return setsockopt(sock, level, optname, koptval, koptlen);
547 }
548 EXPORT_SYMBOL(compat_mc_setsockopt);
549
compat_mc_getsockopt(struct sock * sock,int level,int optname,char __user * optval,int __user * optlen,int (* getsockopt)(struct sock *,int,int,char __user *,int __user *))550 int compat_mc_getsockopt(struct sock *sock, int level, int optname,
551 char __user *optval, int __user *optlen,
552 int (*getsockopt)(struct sock *, int, int, char __user *, int __user *))
553 {
554 struct compat_group_filter __user *gf32 = (void __user *)optval;
555 struct group_filter __user *kgf;
556 int __user *koptlen;
557 u32 interface, fmode, numsrc;
558 int klen, ulen, err;
559
560 if (optname != MCAST_MSFILTER)
561 return getsockopt(sock, level, optname, optval, optlen);
562
563 koptlen = compat_alloc_user_space(sizeof(*koptlen));
564 if (!access_ok(optlen, sizeof(*optlen)) ||
565 __get_user(ulen, optlen))
566 return -EFAULT;
567
568 /* adjust len for pad */
569 klen = ulen + sizeof(*kgf) - sizeof(*gf32);
570
571 if (klen < GROUP_FILTER_SIZE(0))
572 return -EINVAL;
573
574 if (!access_ok(koptlen, sizeof(*koptlen)) ||
575 __put_user(klen, koptlen))
576 return -EFAULT;
577
578 /* have to allow space for previous compat_alloc_user_space, too */
579 kgf = compat_alloc_user_space(klen+sizeof(*optlen));
580
581 if (!access_ok(gf32, __COMPAT_GF0_SIZE) ||
582 __get_user(interface, &gf32->gf_interface) ||
583 __get_user(fmode, &gf32->gf_fmode) ||
584 __get_user(numsrc, &gf32->gf_numsrc) ||
585 __put_user(interface, &kgf->gf_interface) ||
586 __put_user(fmode, &kgf->gf_fmode) ||
587 __put_user(numsrc, &kgf->gf_numsrc) ||
588 copy_in_user(&kgf->gf_group, &gf32->gf_group, sizeof(kgf->gf_group)))
589 return -EFAULT;
590
591 err = getsockopt(sock, level, optname, (char __user *)kgf, koptlen);
592 if (err)
593 return err;
594
595 if (!access_ok(koptlen, sizeof(*koptlen)) ||
596 __get_user(klen, koptlen))
597 return -EFAULT;
598
599 ulen = klen - (sizeof(*kgf)-sizeof(*gf32));
600
601 if (!access_ok(optlen, sizeof(*optlen)) ||
602 __put_user(ulen, optlen))
603 return -EFAULT;
604
605 if (!access_ok(kgf, klen) ||
606 !access_ok(gf32, ulen) ||
607 __get_user(interface, &kgf->gf_interface) ||
608 __get_user(fmode, &kgf->gf_fmode) ||
609 __get_user(numsrc, &kgf->gf_numsrc) ||
610 __put_user(interface, &gf32->gf_interface) ||
611 __put_user(fmode, &gf32->gf_fmode) ||
612 __put_user(numsrc, &gf32->gf_numsrc))
613 return -EFAULT;
614 if (numsrc) {
615 int copylen;
616
617 klen -= GROUP_FILTER_SIZE(0);
618 copylen = numsrc * sizeof(gf32->gf_slist[0]);
619 if (copylen > klen)
620 copylen = klen;
621 if (copy_in_user(gf32->gf_slist, kgf->gf_slist, copylen))
622 return -EFAULT;
623 }
624 return err;
625 }
626 EXPORT_SYMBOL(compat_mc_getsockopt);
627
628
629 /* Argument list sizes for compat_sys_socketcall */
630 #define AL(x) ((x) * sizeof(u32))
631 static unsigned char nas[21] = {
632 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
633 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
634 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
635 AL(4), AL(5), AL(4)
636 };
637 #undef AL
638
__compat_sys_sendmsg(int fd,struct compat_msghdr __user * msg,unsigned int flags)639 static inline long __compat_sys_sendmsg(int fd,
640 struct compat_msghdr __user *msg,
641 unsigned int flags)
642 {
643 return __sys_sendmsg(fd, (struct user_msghdr __user *)msg,
644 flags | MSG_CMSG_COMPAT, false);
645 }
646
COMPAT_SYSCALL_DEFINE3(sendmsg,int,fd,struct compat_msghdr __user *,msg,unsigned int,flags)647 COMPAT_SYSCALL_DEFINE3(sendmsg, int, fd, struct compat_msghdr __user *, msg,
648 unsigned int, flags)
649 {
650 return __compat_sys_sendmsg(fd, msg, flags);
651 }
652
__compat_sys_sendmmsg(int fd,struct compat_mmsghdr __user * mmsg,unsigned int vlen,unsigned int flags)653 static inline long __compat_sys_sendmmsg(int fd,
654 struct compat_mmsghdr __user *mmsg,
655 unsigned int vlen, unsigned int flags)
656 {
657 return __sys_sendmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
658 flags | MSG_CMSG_COMPAT, false);
659 }
660
COMPAT_SYSCALL_DEFINE4(sendmmsg,int,fd,struct compat_mmsghdr __user *,mmsg,unsigned int,vlen,unsigned int,flags)661 COMPAT_SYSCALL_DEFINE4(sendmmsg, int, fd, struct compat_mmsghdr __user *, mmsg,
662 unsigned int, vlen, unsigned int, flags)
663 {
664 return __compat_sys_sendmmsg(fd, mmsg, vlen, flags);
665 }
666
__compat_sys_recvmsg(int fd,struct compat_msghdr __user * msg,unsigned int flags)667 static inline long __compat_sys_recvmsg(int fd,
668 struct compat_msghdr __user *msg,
669 unsigned int flags)
670 {
671 return __sys_recvmsg(fd, (struct user_msghdr __user *)msg,
672 flags | MSG_CMSG_COMPAT, false);
673 }
674
COMPAT_SYSCALL_DEFINE3(recvmsg,int,fd,struct compat_msghdr __user *,msg,unsigned int,flags)675 COMPAT_SYSCALL_DEFINE3(recvmsg, int, fd, struct compat_msghdr __user *, msg,
676 unsigned int, flags)
677 {
678 return __compat_sys_recvmsg(fd, msg, flags);
679 }
680
__compat_sys_recvfrom(int fd,void __user * buf,compat_size_t len,unsigned int flags,struct sockaddr __user * addr,int __user * addrlen)681 static inline long __compat_sys_recvfrom(int fd, void __user *buf,
682 compat_size_t len, unsigned int flags,
683 struct sockaddr __user *addr,
684 int __user *addrlen)
685 {
686 return __sys_recvfrom(fd, buf, len, flags | MSG_CMSG_COMPAT, addr,
687 addrlen);
688 }
689
COMPAT_SYSCALL_DEFINE4(recv,int,fd,void __user *,buf,compat_size_t,len,unsigned int,flags)690 COMPAT_SYSCALL_DEFINE4(recv, int, fd, void __user *, buf, compat_size_t, len, unsigned int, flags)
691 {
692 return __compat_sys_recvfrom(fd, buf, len, flags, NULL, NULL);
693 }
694
COMPAT_SYSCALL_DEFINE6(recvfrom,int,fd,void __user *,buf,compat_size_t,len,unsigned int,flags,struct sockaddr __user *,addr,int __user *,addrlen)695 COMPAT_SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, buf, compat_size_t, len,
696 unsigned int, flags, struct sockaddr __user *, addr,
697 int __user *, addrlen)
698 {
699 return __compat_sys_recvfrom(fd, buf, len, flags, addr, addrlen);
700 }
701
COMPAT_SYSCALL_DEFINE5(recvmmsg_time64,int,fd,struct compat_mmsghdr __user *,mmsg,unsigned int,vlen,unsigned int,flags,struct __kernel_timespec __user *,timeout)702 COMPAT_SYSCALL_DEFINE5(recvmmsg_time64, int, fd, struct compat_mmsghdr __user *, mmsg,
703 unsigned int, vlen, unsigned int, flags,
704 struct __kernel_timespec __user *, timeout)
705 {
706 return __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
707 flags | MSG_CMSG_COMPAT, timeout, NULL);
708 }
709
710 #ifdef CONFIG_COMPAT_32BIT_TIME
COMPAT_SYSCALL_DEFINE5(recvmmsg_time32,int,fd,struct compat_mmsghdr __user *,mmsg,unsigned int,vlen,unsigned int,flags,struct old_timespec32 __user *,timeout)711 COMPAT_SYSCALL_DEFINE5(recvmmsg_time32, int, fd, struct compat_mmsghdr __user *, mmsg,
712 unsigned int, vlen, unsigned int, flags,
713 struct old_timespec32 __user *, timeout)
714 {
715 return __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
716 flags | MSG_CMSG_COMPAT, NULL, timeout);
717 }
718 #endif
719
COMPAT_SYSCALL_DEFINE2(socketcall,int,call,u32 __user *,args)720 COMPAT_SYSCALL_DEFINE2(socketcall, int, call, u32 __user *, args)
721 {
722 u32 a[AUDITSC_ARGS];
723 unsigned int len;
724 u32 a0, a1;
725 int ret;
726
727 if (call < SYS_SOCKET || call > SYS_SENDMMSG)
728 return -EINVAL;
729 len = nas[call];
730 if (len > sizeof(a))
731 return -EINVAL;
732
733 if (copy_from_user(a, args, len))
734 return -EFAULT;
735
736 ret = audit_socketcall_compat(len / sizeof(a[0]), a);
737 if (ret)
738 return ret;
739
740 a0 = a[0];
741 a1 = a[1];
742
743 switch (call) {
744 case SYS_SOCKET:
745 ret = __sys_socket(a0, a1, a[2]);
746 break;
747 case SYS_BIND:
748 ret = __sys_bind(a0, compat_ptr(a1), a[2]);
749 break;
750 case SYS_CONNECT:
751 ret = __sys_connect(a0, compat_ptr(a1), a[2]);
752 break;
753 case SYS_LISTEN:
754 ret = __sys_listen(a0, a1);
755 break;
756 case SYS_ACCEPT:
757 ret = __sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), 0);
758 break;
759 case SYS_GETSOCKNAME:
760 ret = __sys_getsockname(a0, compat_ptr(a1), compat_ptr(a[2]));
761 break;
762 case SYS_GETPEERNAME:
763 ret = __sys_getpeername(a0, compat_ptr(a1), compat_ptr(a[2]));
764 break;
765 case SYS_SOCKETPAIR:
766 ret = __sys_socketpair(a0, a1, a[2], compat_ptr(a[3]));
767 break;
768 case SYS_SEND:
769 ret = __sys_sendto(a0, compat_ptr(a1), a[2], a[3], NULL, 0);
770 break;
771 case SYS_SENDTO:
772 ret = __sys_sendto(a0, compat_ptr(a1), a[2], a[3],
773 compat_ptr(a[4]), a[5]);
774 break;
775 case SYS_RECV:
776 ret = __compat_sys_recvfrom(a0, compat_ptr(a1), a[2], a[3],
777 NULL, NULL);
778 break;
779 case SYS_RECVFROM:
780 ret = __compat_sys_recvfrom(a0, compat_ptr(a1), a[2], a[3],
781 compat_ptr(a[4]),
782 compat_ptr(a[5]));
783 break;
784 case SYS_SHUTDOWN:
785 ret = __sys_shutdown(a0, a1);
786 break;
787 case SYS_SETSOCKOPT:
788 ret = __compat_sys_setsockopt(a0, a1, a[2],
789 compat_ptr(a[3]), a[4]);
790 break;
791 case SYS_GETSOCKOPT:
792 ret = __compat_sys_getsockopt(a0, a1, a[2],
793 compat_ptr(a[3]),
794 compat_ptr(a[4]));
795 break;
796 case SYS_SENDMSG:
797 ret = __compat_sys_sendmsg(a0, compat_ptr(a1), a[2]);
798 break;
799 case SYS_SENDMMSG:
800 ret = __compat_sys_sendmmsg(a0, compat_ptr(a1), a[2], a[3]);
801 break;
802 case SYS_RECVMSG:
803 ret = __compat_sys_recvmsg(a0, compat_ptr(a1), a[2]);
804 break;
805 case SYS_RECVMMSG:
806 ret = __sys_recvmmsg(a0, compat_ptr(a1), a[2],
807 a[3] | MSG_CMSG_COMPAT, NULL,
808 compat_ptr(a[4]));
809 break;
810 case SYS_ACCEPT4:
811 ret = __sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), a[3]);
812 break;
813 default:
814 ret = -EINVAL;
815 break;
816 }
817 return ret;
818 }
819