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