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