compat.c 24.0 KB
Newer Older
1
/*
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10
 * 32bit Socket syscall emulation. Based on arch/sparc64/kernel/sys_sparc32.c.
 *
 * Copyright (C) 2000		VA Linux Co
 * Copyright (C) 2000		Don Dugger <n0ano@valinux.com>
 * Copyright (C) 1999 		Arun Sharma <arun.sharma@intel.com>
 * Copyright (C) 1997,1998 	Jakub Jelinek (jj@sunsite.mff.cuni.cz)
 * Copyright (C) 1997 		David S. Miller (davem@caip.rutgers.edu)
 * Copyright (C) 2000		Hewlett-Packard Co.
 * Copyright (C) 2000		David Mosberger-Tang <davidm@hpl.hp.com>
11
 * Copyright (C) 2000,2001	Andi Kleen, SuSE Labs
L
Linus Torvalds 已提交
12 13 14
 */

#include <linux/kernel.h>
15
#include <linux/gfp.h>
L
Linus Torvalds 已提交
16 17 18 19 20 21 22 23 24
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/file.h>
#include <linux/icmpv6.h>
#include <linux/socket.h>
#include <linux/syscalls.h>
#include <linux/filter.h>
#include <linux/compat.h>
#include <linux/security.h>
25
#include <linux/export.h>
L
Linus Torvalds 已提交
26 27 28

#include <net/scm.h>
#include <net/sock.h>
29 30
#include <net/ip.h>
#include <net/ipv6.h>
L
Linus Torvalds 已提交
31 32 33
#include <asm/uaccess.h>
#include <net/compat.h>

34 35 36 37
ssize_t get_compat_msghdr(struct msghdr *kmsg,
			  struct compat_msghdr __user *umsg,
			  struct sockaddr __user **save_addr,
			  struct iovec **iov)
L
Linus Torvalds 已提交
38
{
39
	compat_uptr_t uaddr, uiov, tmp3;
A
Al Viro 已提交
40
	compat_size_t nr_segs;
41
	ssize_t err;
L
Linus Torvalds 已提交
42 43

	if (!access_ok(VERIFY_READ, umsg, sizeof(*umsg)) ||
44
	    __get_user(uaddr, &umsg->msg_name) ||
L
Linus Torvalds 已提交
45
	    __get_user(kmsg->msg_namelen, &umsg->msg_namelen) ||
46
	    __get_user(uiov, &umsg->msg_iov) ||
A
Al Viro 已提交
47
	    __get_user(nr_segs, &umsg->msg_iovlen) ||
L
Linus Torvalds 已提交
48 49 50 51
	    __get_user(tmp3, &umsg->msg_control) ||
	    __get_user(kmsg->msg_controllen, &umsg->msg_controllen) ||
	    __get_user(kmsg->msg_flags, &umsg->msg_flags))
		return -EFAULT;
52
	if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
53
		kmsg->msg_namelen = sizeof(struct sockaddr_storage);
L
Linus Torvalds 已提交
54 55
	kmsg->msg_control = compat_ptr(tmp3);

56 57
	if (save_addr)
		*save_addr = compat_ptr(uaddr);
L
Linus Torvalds 已提交
58

59 60 61 62 63
	if (uaddr && kmsg->msg_namelen) {
		if (!save_addr) {
			err = move_addr_to_kernel(compat_ptr(uaddr),
						  kmsg->msg_namelen,
						  kmsg->msg_name);
64
			if (err < 0)
L
Linus Torvalds 已提交
65 66
				return err;
		}
67
	} else {
68 69
		kmsg->msg_name = NULL;
		kmsg->msg_namelen = 0;
70
	}
L
Linus Torvalds 已提交
71

A
Al Viro 已提交
72
	if (nr_segs > UIO_MAXIOV)
73 74
		return -EMSGSIZE;

75
	err = compat_rw_copy_check_uvector(save_addr ? READ : WRITE,
A
Al Viro 已提交
76
					   compat_ptr(uiov), nr_segs,
77
					   UIO_FASTIOV, *iov, iov);
78
	if (err >= 0)
A
Al Viro 已提交
79 80
		iov_iter_init(&kmsg->msg_iter, save_addr ? READ : WRITE,
			      *iov, nr_segs, err);
81
	return err;
L
Linus Torvalds 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
}

/* Bleech... */
#define CMSG_COMPAT_ALIGN(len)	ALIGN((len), sizeof(s32))

#define CMSG_COMPAT_DATA(cmsg)				\
	((void __user *)((char __user *)(cmsg) + CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr))))
#define CMSG_COMPAT_SPACE(len)				\
	(CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)) + CMSG_COMPAT_ALIGN(len))
#define CMSG_COMPAT_LEN(len)				\
	(CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)) + (len))

#define CMSG_COMPAT_FIRSTHDR(msg)			\
	(((msg)->msg_controllen) >= sizeof(struct compat_cmsghdr) ?	\
	 (struct compat_cmsghdr __user *)((msg)->msg_control) :		\
	 (struct compat_cmsghdr __user *)NULL)

#define CMSG_COMPAT_OK(ucmlen, ucmsg, mhdr) \
	((ucmlen) >= sizeof(struct compat_cmsghdr) && \
	 (ucmlen) <= (unsigned long) \
	 ((mhdr)->msg_controllen - \
	  ((char *)(ucmsg) - (char *)(mhdr)->msg_control)))

static inline struct compat_cmsghdr __user *cmsg_compat_nxthdr(struct msghdr *msg,
		struct compat_cmsghdr __user *cmsg, int cmsg_len)
{
	char __user *ptr = (char __user *)cmsg + CMSG_COMPAT_ALIGN(cmsg_len);
	if ((unsigned long)(ptr + 1 - (char __user *)msg->msg_control) >
			msg->msg_controllen)
		return NULL;
	return (struct compat_cmsghdr __user *)ptr;
}

/* There is a lot of hair here because the alignment rules (and
 * thus placement) of cmsg headers and length are different for
 * 32-bit apps.  -DaveM
 */
A
Al Viro 已提交
119
int cmsghdr_from_user_compat_to_kern(struct msghdr *kmsg, struct sock *sk,
L
Linus Torvalds 已提交
120 121 122 123 124 125
			       unsigned char *stackbuf, int stackbuf_size)
{
	struct compat_cmsghdr __user *ucmsg;
	struct cmsghdr *kcmsg, *kcmsg_base;
	compat_size_t ucmlen;
	__kernel_size_t kcmlen, tmp;
A
Al Viro 已提交
126
	int err = -EFAULT;
L
Linus Torvalds 已提交
127 128 129 130

	kcmlen = 0;
	kcmsg_base = kcmsg = (struct cmsghdr *)stackbuf;
	ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg);
131 132
	while (ucmsg != NULL) {
		if (get_user(ucmlen, &ucmsg->cmsg_len))
L
Linus Torvalds 已提交
133 134 135 136 137 138 139 140
			return -EFAULT;

		/* Catch bogons. */
		if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg))
			return -EINVAL;

		tmp = ((ucmlen - CMSG_COMPAT_ALIGN(sizeof(*ucmsg))) +
		       CMSG_ALIGN(sizeof(struct cmsghdr)));
A
Al Viro 已提交
141
		tmp = CMSG_ALIGN(tmp);
L
Linus Torvalds 已提交
142 143 144
		kcmlen += tmp;
		ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen);
	}
145
	if (kcmlen == 0)
L
Linus Torvalds 已提交
146 147 148 149 150 151 152
		return -EINVAL;

	/* The kcmlen holds the 64-bit version of the control length.
	 * It may not be modified as we do not stick it into the kmsg
	 * until we have successfully copied over all of the data
	 * from the user.
	 */
A
Al Viro 已提交
153 154 155
	if (kcmlen > stackbuf_size)
		kcmsg_base = kcmsg = sock_kmalloc(sk, kcmlen, GFP_KERNEL);
	if (kcmsg == NULL)
L
Linus Torvalds 已提交
156 157 158 159 160
		return -ENOBUFS;

	/* Now copy them over neatly. */
	memset(kcmsg, 0, kcmlen);
	ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg);
161
	while (ucmsg != NULL) {
A
Al Viro 已提交
162 163 164 165
		if (__get_user(ucmlen, &ucmsg->cmsg_len))
			goto Efault;
		if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg))
			goto Einval;
L
Linus Torvalds 已提交
166 167
		tmp = ((ucmlen - CMSG_COMPAT_ALIGN(sizeof(*ucmsg))) +
		       CMSG_ALIGN(sizeof(struct cmsghdr)));
A
Al Viro 已提交
168 169
		if ((char *)kcmsg_base + kcmlen - (char *)kcmsg < CMSG_ALIGN(tmp))
			goto Einval;
L
Linus Torvalds 已提交
170
		kcmsg->cmsg_len = tmp;
A
Al Viro 已提交
171 172 173 174 175 176 177
		tmp = CMSG_ALIGN(tmp);
		if (__get_user(kcmsg->cmsg_level, &ucmsg->cmsg_level) ||
		    __get_user(kcmsg->cmsg_type, &ucmsg->cmsg_type) ||
		    copy_from_user(CMSG_DATA(kcmsg),
				   CMSG_COMPAT_DATA(ucmsg),
				   (ucmlen - CMSG_COMPAT_ALIGN(sizeof(*ucmsg)))))
			goto Efault;
L
Linus Torvalds 已提交
178 179

		/* Advance. */
A
Al Viro 已提交
180
		kcmsg = (struct cmsghdr *)((char *)kcmsg + tmp);
L
Linus Torvalds 已提交
181 182 183 184 185 186 187 188
		ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen);
	}

	/* Ok, looks like we made it.  Hook it up and return success. */
	kmsg->msg_control = kcmsg_base;
	kmsg->msg_controllen = kcmlen;
	return 0;

A
Al Viro 已提交
189 190 191 192 193 194
Einval:
	err = -EINVAL;
Efault:
	if (kcmsg_base != (struct cmsghdr *)stackbuf)
		sock_kfree_s(sk, kcmsg_base, kcmlen);
	return err;
L
Linus Torvalds 已提交
195 196 197 198 199 200
}

int put_cmsg_compat(struct msghdr *kmsg, int level, int type, int len, void *data)
{
	struct compat_cmsghdr __user *cm = (struct compat_cmsghdr __user *) kmsg->msg_control;
	struct compat_cmsghdr cmhdr;
201 202
	struct compat_timeval ctv;
	struct compat_timespec cts[3];
L
Linus Torvalds 已提交
203 204
	int cmlen;

205
	if (cm == NULL || kmsg->msg_controllen < sizeof(*cm)) {
L
Linus Torvalds 已提交
206 207 208 209
		kmsg->msg_flags |= MSG_CTRUNC;
		return 0; /* XXX: return error? check spec. */
	}

210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
	if (!COMPAT_USE_64BIT_TIME) {
		if (level == SOL_SOCKET && type == SCM_TIMESTAMP) {
			struct timeval *tv = (struct timeval *)data;
			ctv.tv_sec = tv->tv_sec;
			ctv.tv_usec = tv->tv_usec;
			data = &ctv;
			len = sizeof(ctv);
		}
		if (level == SOL_SOCKET &&
		    (type == SCM_TIMESTAMPNS || type == SCM_TIMESTAMPING)) {
			int count = type == SCM_TIMESTAMPNS ? 1 : 3;
			int i;
			struct timespec *ts = (struct timespec *)data;
			for (i = 0; i < count; i++) {
				cts[i].tv_sec = ts[i].tv_sec;
				cts[i].tv_nsec = ts[i].tv_nsec;
			}
			data = &cts;
			len = sizeof(cts[0]) * count;
229
		}
230 231
	}

L
Linus Torvalds 已提交
232
	cmlen = CMSG_COMPAT_LEN(len);
233
	if (kmsg->msg_controllen < cmlen) {
L
Linus Torvalds 已提交
234 235 236 237 238 239 240
		kmsg->msg_flags |= MSG_CTRUNC;
		cmlen = kmsg->msg_controllen;
	}
	cmhdr.cmsg_level = level;
	cmhdr.cmsg_type = type;
	cmhdr.cmsg_len = cmlen;

241
	if (copy_to_user(cm, &cmhdr, sizeof cmhdr))
L
Linus Torvalds 已提交
242
		return -EFAULT;
243
	if (copy_to_user(CMSG_COMPAT_DATA(cm), data, cmlen - sizeof(struct compat_cmsghdr)))
L
Linus Torvalds 已提交
244 245
		return -EFAULT;
	cmlen = CMSG_COMPAT_SPACE(len);
246 247
	if (kmsg->msg_controllen < cmlen)
		cmlen = kmsg->msg_controllen;
L
Linus Torvalds 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
	kmsg->msg_control += cmlen;
	kmsg->msg_controllen -= cmlen;
	return 0;
}

void scm_detach_fds_compat(struct msghdr *kmsg, struct scm_cookie *scm)
{
	struct compat_cmsghdr __user *cm = (struct compat_cmsghdr __user *) kmsg->msg_control;
	int fdmax = (kmsg->msg_controllen - sizeof(struct compat_cmsghdr)) / sizeof(int);
	int fdnum = scm->fp->count;
	struct file **fp = scm->fp->fp;
	int __user *cmfptr;
	int err = 0, i;

	if (fdnum < fdmax)
		fdmax = fdnum;

	for (i = 0, cmfptr = (int __user *) CMSG_COMPAT_DATA(cm); i < fdmax; i++, cmfptr++) {
		int new_fd;
		err = security_file_receive(fp[i]);
		if (err)
			break;
U
Ulrich Drepper 已提交
270 271
		err = get_unused_fd_flags(MSG_CMSG_CLOEXEC & kmsg->msg_flags
					  ? O_CLOEXEC : 0);
L
Linus Torvalds 已提交
272 273 274 275 276 277 278 279 280
		if (err < 0)
			break;
		new_fd = err;
		err = put_user(new_fd, cmfptr);
		if (err) {
			put_unused_fd(new_fd);
			break;
		}
		/* Bump the usage count and install the file. */
A
Al Viro 已提交
281
		fd_install(new_fd, get_file(fp[i]));
L
Linus Torvalds 已提交
282 283 284 285
	}

	if (i > 0) {
		int cmlen = CMSG_COMPAT_LEN(i * sizeof(int));
286
		err = put_user(SOL_SOCKET, &cm->cmsg_level);
L
Linus Torvalds 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
		if (!err)
			err = put_user(SCM_RIGHTS, &cm->cmsg_type);
		if (!err)
			err = put_user(cmlen, &cm->cmsg_len);
		if (!err) {
			cmlen = CMSG_COMPAT_SPACE(i * sizeof(int));
			kmsg->msg_control += cmlen;
			kmsg->msg_controllen -= cmlen;
		}
	}
	if (i < fdnum)
		kmsg->msg_flags |= MSG_CTRUNC;

	/*
	 * All of the files that fit in the message have had their
	 * usage counts incremented, so we just free the list.
	 */
	__scm_destroy(scm);
}

307
static int do_set_attach_filter(struct socket *sock, int level, int optname,
308
				char __user *optval, unsigned int optlen)
L
Linus Torvalds 已提交
309 310
{
	struct compat_sock_fprog __user *fprog32 = (struct compat_sock_fprog __user *)optval;
311
	struct sock_fprog __user *kfprog = compat_alloc_user_space(sizeof(struct sock_fprog));
L
Linus Torvalds 已提交
312 313 314 315 316 317 318 319 320 321 322
	compat_uptr_t ptr;
	u16 len;

	if (!access_ok(VERIFY_READ, fprog32, sizeof(*fprog32)) ||
	    !access_ok(VERIFY_WRITE, kfprog, sizeof(struct sock_fprog)) ||
	    __get_user(len, &fprog32->len) ||
	    __get_user(ptr, &fprog32->filter) ||
	    __put_user(len, &kfprog->len) ||
	    __put_user(compat_ptr(ptr), &kfprog->filter))
		return -EFAULT;

323
	return sock_setsockopt(sock, level, optname, (char __user *)kfprog,
L
Linus Torvalds 已提交
324 325 326
			      sizeof(struct sock_fprog));
}

327
static int do_set_sock_timeout(struct socket *sock, int level,
328
		int optname, char __user *optval, unsigned int optlen)
L
Linus Torvalds 已提交
329
{
330
	struct compat_timeval __user *up = (struct compat_timeval __user *)optval;
L
Linus Torvalds 已提交
331 332 333 334 335 336 337 338 339 340 341 342
	struct timeval ktime;
	mm_segment_t old_fs;
	int err;

	if (optlen < sizeof(*up))
		return -EINVAL;
	if (!access_ok(VERIFY_READ, up, sizeof(*up)) ||
	    __get_user(ktime.tv_sec, &up->tv_sec) ||
	    __get_user(ktime.tv_usec, &up->tv_usec))
		return -EFAULT;
	old_fs = get_fs();
	set_fs(KERNEL_DS);
343
	err = sock_setsockopt(sock, level, optname, (char *)&ktime, sizeof(ktime));
L
Linus Torvalds 已提交
344 345 346 347 348
	set_fs(old_fs);

	return err;
}

349
static int compat_sock_setsockopt(struct socket *sock, int level, int optname,
350
				char __user *optval, unsigned int optlen)
351 352 353 354 355 356 357 358 359 360
{
	if (optname == SO_ATTACH_FILTER)
		return do_set_attach_filter(sock, level, optname,
					    optval, optlen);
	if (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO)
		return do_set_sock_timeout(sock, level, optname, optval, optlen);

	return sock_setsockopt(sock, level, optname, optval, optlen);
}

361 362
COMPAT_SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
		       char __user *, optval, unsigned int, optlen)
L
Linus Torvalds 已提交
363
{
364
	int err;
365
	struct socket *sock = sockfd_lookup(fd, &err);
366

367 368
	if (sock) {
		err = security_socket_setsockopt(sock, level, optname);
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
		if (err) {
			sockfd_put(sock);
			return err;
		}

		if (level == SOL_SOCKET)
			err = compat_sock_setsockopt(sock, level,
					optname, optval, optlen);
		else if (sock->ops->compat_setsockopt)
			err = sock->ops->compat_setsockopt(sock, level,
					optname, optval, optlen);
		else
			err = sock->ops->setsockopt(sock, level,
					optname, optval, optlen);
		sockfd_put(sock);
	}
	return err;
L
Linus Torvalds 已提交
386 387
}

388
static int do_get_sock_timeout(struct socket *sock, int level, int optname,
L
Linus Torvalds 已提交
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
		char __user *optval, int __user *optlen)
{
	struct compat_timeval __user *up;
	struct timeval ktime;
	mm_segment_t old_fs;
	int len, err;

	up = (struct compat_timeval __user *) optval;
	if (get_user(len, optlen))
		return -EFAULT;
	if (len < sizeof(*up))
		return -EINVAL;
	len = sizeof(ktime);
	old_fs = get_fs();
	set_fs(KERNEL_DS);
404
	err = sock_getsockopt(sock, level, optname, (char *) &ktime, &len);
L
Linus Torvalds 已提交
405 406 407 408 409 410 411 412 413 414 415 416
	set_fs(old_fs);

	if (!err) {
		if (put_user(sizeof(*up), optlen) ||
		    !access_ok(VERIFY_WRITE, up, sizeof(*up)) ||
		    __put_user(ktime.tv_sec, &up->tv_sec) ||
		    __put_user(ktime.tv_usec, &up->tv_usec))
			err = -EFAULT;
	}
	return err;
}

417
static int compat_sock_getsockopt(struct socket *sock, int level, int optname,
L
Linus Torvalds 已提交
418 419
				char __user *optval, int __user *optlen)
{
420 421 422
	if (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO)
		return do_get_sock_timeout(sock, level, optname, optval, optlen);
	return sock_getsockopt(sock, level, optname, optval, optlen);
L
Linus Torvalds 已提交
423 424
}

425 426
int compat_sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp)
{
427 428
	struct compat_timeval __user *ctv;
	int err;
429
	struct timeval tv;
430

431 432 433 434 435
	if (COMPAT_USE_64BIT_TIME)
		return sock_get_timestamp(sk, userstamp);

	ctv = (struct compat_timeval __user *) userstamp;
	err = -ENOENT;
436
	if (!sock_flag(sk, SOCK_TIMESTAMP))
437
		sock_enable_timestamp(sk, SOCK_TIMESTAMP);
438 439
	tv = ktime_to_timeval(sk->sk_stamp);
	if (tv.tv_sec == -1)
440
		return err;
441 442 443 444 445 446 447
	if (tv.tv_sec == 0) {
		sk->sk_stamp = ktime_get_real();
		tv = ktime_to_timeval(sk->sk_stamp);
	}
	err = 0;
	if (put_user(tv.tv_sec, &ctv->tv_sec) ||
			put_user(tv.tv_usec, &ctv->tv_usec))
448 449 450 451 452
		err = -EFAULT;
	return err;
}
EXPORT_SYMBOL(compat_sock_get_timestamp);

453 454
int compat_sock_get_timestampns(struct sock *sk, struct timespec __user *userstamp)
{
455 456
	struct compat_timespec __user *ctv;
	int err;
457 458
	struct timespec ts;

459 460 461 462 463
	if (COMPAT_USE_64BIT_TIME)
		return sock_get_timestampns (sk, userstamp);

	ctv = (struct compat_timespec __user *) userstamp;
	err = -ENOENT;
464
	if (!sock_flag(sk, SOCK_TIMESTAMP))
465
		sock_enable_timestamp(sk, SOCK_TIMESTAMP);
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
	ts = ktime_to_timespec(sk->sk_stamp);
	if (ts.tv_sec == -1)
		return err;
	if (ts.tv_sec == 0) {
		sk->sk_stamp = ktime_get_real();
		ts = ktime_to_timespec(sk->sk_stamp);
	}
	err = 0;
	if (put_user(ts.tv_sec, &ctv->tv_sec) ||
			put_user(ts.tv_nsec, &ctv->tv_nsec))
		err = -EFAULT;
	return err;
}
EXPORT_SYMBOL(compat_sock_get_timestampns);

481 482
COMPAT_SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
		       char __user *, optval, int __user *, optlen)
483 484
{
	int err;
485
	struct socket *sock = sockfd_lookup(fd, &err);
486

487 488
	if (sock) {
		err = security_socket_getsockopt(sock, level, optname);
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
		if (err) {
			sockfd_put(sock);
			return err;
		}

		if (level == SOL_SOCKET)
			err = compat_sock_getsockopt(sock, level,
					optname, optval, optlen);
		else if (sock->ops->compat_getsockopt)
			err = sock->ops->compat_getsockopt(sock, level,
					optname, optval, optlen);
		else
			err = sock->ops->getsockopt(sock, level,
					optname, optval, optlen);
		sockfd_put(sock);
	}
	return err;
}
507 508 509 510 511

struct compat_group_req {
	__u32				 gr_interface;
	struct __kernel_sockaddr_storage gr_group
		__attribute__ ((aligned(4)));
E
Eric Dumazet 已提交
512
} __packed;
513 514 515 516 517 518 519

struct compat_group_source_req {
	__u32				 gsr_interface;
	struct __kernel_sockaddr_storage gsr_group
		__attribute__ ((aligned(4)));
	struct __kernel_sockaddr_storage gsr_source
		__attribute__ ((aligned(4)));
E
Eric Dumazet 已提交
520
} __packed;
521 522 523 524 525 526 527 528 529

struct compat_group_filter {
	__u32				 gf_interface;
	struct __kernel_sockaddr_storage gf_group
		__attribute__ ((aligned(4)));
	__u32				 gf_fmode;
	__u32				 gf_numsrc;
	struct __kernel_sockaddr_storage gf_slist[1]
		__attribute__ ((aligned(4)));
E
Eric Dumazet 已提交
530
} __packed;
531

532 533 534
#define __COMPAT_GF0_SIZE (sizeof(struct compat_group_filter) - \
			sizeof(struct __kernel_sockaddr_storage))

535 536

int compat_mc_setsockopt(struct sock *sock, int level, int optname,
537
	char __user *optval, unsigned int optlen,
538
	int (*setsockopt)(struct sock *, int, int, char __user *, unsigned int))
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
{
	char __user	*koptval = optval;
	int		koptlen = optlen;

	switch (optname) {
	case MCAST_JOIN_GROUP:
	case MCAST_LEAVE_GROUP:
	{
		struct compat_group_req __user *gr32 = (void *)optval;
		struct group_req __user *kgr =
			compat_alloc_user_space(sizeof(struct group_req));
		u32 interface;

		if (!access_ok(VERIFY_READ, gr32, sizeof(*gr32)) ||
		    !access_ok(VERIFY_WRITE, kgr, sizeof(struct group_req)) ||
		    __get_user(interface, &gr32->gr_interface) ||
		    __put_user(interface, &kgr->gr_interface) ||
		    copy_in_user(&kgr->gr_group, &gr32->gr_group,
				sizeof(kgr->gr_group)))
			return -EFAULT;
		koptval = (char __user *)kgr;
		koptlen = sizeof(struct group_req);
		break;
	}
	case MCAST_JOIN_SOURCE_GROUP:
	case MCAST_LEAVE_SOURCE_GROUP:
	case MCAST_BLOCK_SOURCE:
	case MCAST_UNBLOCK_SOURCE:
	{
		struct compat_group_source_req __user *gsr32 = (void *)optval;
569
		struct group_source_req __user *kgsr = compat_alloc_user_space(
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
			sizeof(struct group_source_req));
		u32 interface;

		if (!access_ok(VERIFY_READ, gsr32, sizeof(*gsr32)) ||
		    !access_ok(VERIFY_WRITE, kgsr,
			sizeof(struct group_source_req)) ||
		    __get_user(interface, &gsr32->gsr_interface) ||
		    __put_user(interface, &kgsr->gsr_interface) ||
		    copy_in_user(&kgsr->gsr_group, &gsr32->gsr_group,
				sizeof(kgsr->gsr_group)) ||
		    copy_in_user(&kgsr->gsr_source, &gsr32->gsr_source,
				sizeof(kgsr->gsr_source)))
			return -EFAULT;
		koptval = (char __user *)kgsr;
		koptlen = sizeof(struct group_source_req);
		break;
	}
	case MCAST_MSFILTER:
	{
		struct compat_group_filter __user *gf32 = (void *)optval;
590
		struct group_filter __user *kgf;
591 592
		u32 interface, fmode, numsrc;

593
		if (!access_ok(VERIFY_READ, gf32, __COMPAT_GF0_SIZE) ||
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
		    __get_user(interface, &gf32->gf_interface) ||
		    __get_user(fmode, &gf32->gf_fmode) ||
		    __get_user(numsrc, &gf32->gf_numsrc))
			return -EFAULT;
		koptlen = optlen + sizeof(struct group_filter) -
				sizeof(struct compat_group_filter);
		if (koptlen < GROUP_FILTER_SIZE(numsrc))
			return -EINVAL;
		kgf = compat_alloc_user_space(koptlen);
		if (!access_ok(VERIFY_WRITE, kgf, koptlen) ||
		    __put_user(interface, &kgf->gf_interface) ||
		    __put_user(fmode, &kgf->gf_fmode) ||
		    __put_user(numsrc, &kgf->gf_numsrc) ||
		    copy_in_user(&kgf->gf_group, &gf32->gf_group,
				sizeof(kgf->gf_group)) ||
609
		    (numsrc && copy_in_user(kgf->gf_slist, gf32->gf_slist,
610 611 612 613 614 615 616 617 618 619 620 621 622
				numsrc * sizeof(kgf->gf_slist[0]))))
			return -EFAULT;
		koptval = (char __user *)kgf;
		break;
	}

	default:
		break;
	}
	return setsockopt(sock, level, optname, koptval, koptlen);
}
EXPORT_SYMBOL(compat_mc_setsockopt);

623 624
int compat_mc_getsockopt(struct sock *sock, int level, int optname,
	char __user *optval, int __user *optlen,
625
	int (*getsockopt)(struct sock *, int, int, char __user *, int __user *))
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
{
	struct compat_group_filter __user *gf32 = (void *)optval;
	struct group_filter __user *kgf;
	int __user	*koptlen;
	u32 interface, fmode, numsrc;
	int klen, ulen, err;

	if (optname != MCAST_MSFILTER)
		return getsockopt(sock, level, optname, optval, optlen);

	koptlen = compat_alloc_user_space(sizeof(*koptlen));
	if (!access_ok(VERIFY_READ, optlen, sizeof(*optlen)) ||
	    __get_user(ulen, optlen))
		return -EFAULT;

	/* adjust len for pad */
	klen = ulen + sizeof(*kgf) - sizeof(*gf32);

	if (klen < GROUP_FILTER_SIZE(0))
		return -EINVAL;

	if (!access_ok(VERIFY_WRITE, koptlen, sizeof(*koptlen)) ||
	    __put_user(klen, koptlen))
		return -EFAULT;

	/* have to allow space for previous compat_alloc_user_space, too */
	kgf = compat_alloc_user_space(klen+sizeof(*optlen));

	if (!access_ok(VERIFY_READ, gf32, __COMPAT_GF0_SIZE) ||
	    __get_user(interface, &gf32->gf_interface) ||
	    __get_user(fmode, &gf32->gf_fmode) ||
	    __get_user(numsrc, &gf32->gf_numsrc) ||
	    __put_user(interface, &kgf->gf_interface) ||
	    __put_user(fmode, &kgf->gf_fmode) ||
	    __put_user(numsrc, &kgf->gf_numsrc) ||
661
	    copy_in_user(&kgf->gf_group, &gf32->gf_group, sizeof(kgf->gf_group)))
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
		return -EFAULT;

	err = getsockopt(sock, level, optname, (char __user *)kgf, koptlen);
	if (err)
		return err;

	if (!access_ok(VERIFY_READ, koptlen, sizeof(*koptlen)) ||
	    __get_user(klen, koptlen))
		return -EFAULT;

	ulen = klen - (sizeof(*kgf)-sizeof(*gf32));

	if (!access_ok(VERIFY_WRITE, optlen, sizeof(*optlen)) ||
	    __put_user(ulen, optlen))
		return -EFAULT;

	if (!access_ok(VERIFY_READ, kgf, klen) ||
	    !access_ok(VERIFY_WRITE, gf32, ulen) ||
	    __get_user(interface, &kgf->gf_interface) ||
	    __get_user(fmode, &kgf->gf_fmode) ||
	    __get_user(numsrc, &kgf->gf_numsrc) ||
	    __put_user(interface, &gf32->gf_interface) ||
	    __put_user(fmode, &gf32->gf_fmode) ||
	    __put_user(numsrc, &gf32->gf_numsrc))
		return -EFAULT;
	if (numsrc) {
		int copylen;

		klen -= GROUP_FILTER_SIZE(0);
		copylen = numsrc * sizeof(gf32->gf_slist[0]);
		if (copylen > klen)
			copylen = klen;
694
		if (copy_in_user(gf32->gf_slist, kgf->gf_slist, copylen))
695 696 697 698 699 700
			return -EFAULT;
	}
	return err;
}
EXPORT_SYMBOL(compat_mc_getsockopt);

701

L
Linus Torvalds 已提交
702 703
/* Argument list sizes for compat_sys_socketcall */
#define AL(x) ((x) * sizeof(u32))
704
static unsigned char nas[21] = {
705 706 707
	AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
	AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
	AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
708
	AL(4), AL(5), AL(4)
709
};
L
Linus Torvalds 已提交
710 711
#undef AL

712
COMPAT_SYSCALL_DEFINE3(sendmsg, int, fd, struct compat_msghdr __user *, msg, unsigned int, flags)
L
Linus Torvalds 已提交
713
{
714
	return __sys_sendmsg(fd, (struct user_msghdr __user *)msg, flags | MSG_CMSG_COMPAT);
L
Linus Torvalds 已提交
715 716
}

717 718
COMPAT_SYSCALL_DEFINE4(sendmmsg, int, fd, struct compat_mmsghdr __user *, mmsg,
		       unsigned int, vlen, unsigned int, flags)
719 720 721 722 723
{
	return __sys_sendmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
			      flags | MSG_CMSG_COMPAT);
}

724
COMPAT_SYSCALL_DEFINE3(recvmsg, int, fd, struct compat_msghdr __user *, msg, unsigned int, flags)
L
Linus Torvalds 已提交
725
{
726
	return __sys_recvmsg(fd, (struct user_msghdr __user *)msg, flags | MSG_CMSG_COMPAT);
L
Linus Torvalds 已提交
727 728
}

729
COMPAT_SYSCALL_DEFINE4(recv, int, fd, void __user *, buf, compat_size_t, len, unsigned int, flags)
730 731 732 733
{
	return sys_recv(fd, buf, len, flags | MSG_CMSG_COMPAT);
}

734 735 736
COMPAT_SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, buf, compat_size_t, len,
		       unsigned int, flags, struct sockaddr __user *, addr,
		       int __user *, addrlen)
737 738 739 740
{
	return sys_recvfrom(fd, buf, len, flags | MSG_CMSG_COMPAT, addr, addrlen);
}

741 742 743
COMPAT_SYSCALL_DEFINE5(recvmmsg, int, fd, struct compat_mmsghdr __user *, mmsg,
		       unsigned int, vlen, unsigned int, flags,
		       struct compat_timespec __user *, timeout)
744 745 746 747
{
	int datagrams;
	struct timespec ktspec;

748 749 750 751
	if (timeout == NULL)
		return __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
				      flags | MSG_CMSG_COMPAT, NULL);

752
	if (compat_get_timespec(&ktspec, timeout))
753 754 755 756
		return -EFAULT;

	datagrams = __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
				   flags | MSG_CMSG_COMPAT, &ktspec);
757
	if (datagrams > 0 && compat_put_timespec(&ktspec, timeout))
758 759 760 761 762
		datagrams = -EFAULT;

	return datagrams;
}

763
COMPAT_SYSCALL_DEFINE2(socketcall, int, call, u32 __user *, args)
L
Linus Torvalds 已提交
764 765 766 767
{
	int ret;
	u32 a[6];
	u32 a0, a1;
768

769
	if (call < SYS_SOCKET || call > SYS_SENDMMSG)
L
Linus Torvalds 已提交
770 771 772 773 774
		return -EINVAL;
	if (copy_from_user(a, args, nas[call]))
		return -EFAULT;
	a0 = a[0];
	a1 = a[1];
775

776
	switch (call) {
L
Linus Torvalds 已提交
777 778 779 780 781 782 783 784 785 786 787 788 789
	case SYS_SOCKET:
		ret = sys_socket(a0, a1, a[2]);
		break;
	case SYS_BIND:
		ret = sys_bind(a0, compat_ptr(a1), a[2]);
		break;
	case SYS_CONNECT:
		ret = sys_connect(a0, compat_ptr(a1), a[2]);
		break;
	case SYS_LISTEN:
		ret = sys_listen(a0, a1);
		break;
	case SYS_ACCEPT:
U
Ulrich Drepper 已提交
790
		ret = sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), 0);
L
Linus Torvalds 已提交
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
		break;
	case SYS_GETSOCKNAME:
		ret = sys_getsockname(a0, compat_ptr(a1), compat_ptr(a[2]));
		break;
	case SYS_GETPEERNAME:
		ret = sys_getpeername(a0, compat_ptr(a1), compat_ptr(a[2]));
		break;
	case SYS_SOCKETPAIR:
		ret = sys_socketpair(a0, a1, a[2], compat_ptr(a[3]));
		break;
	case SYS_SEND:
		ret = sys_send(a0, compat_ptr(a1), a[2], a[3]);
		break;
	case SYS_SENDTO:
		ret = sys_sendto(a0, compat_ptr(a1), a[2], a[3], compat_ptr(a[4]), a[5]);
		break;
	case SYS_RECV:
808
		ret = compat_sys_recv(a0, compat_ptr(a1), a[2], a[3]);
L
Linus Torvalds 已提交
809 810
		break;
	case SYS_RECVFROM:
811 812
		ret = compat_sys_recvfrom(a0, compat_ptr(a1), a[2], a[3],
					  compat_ptr(a[4]), compat_ptr(a[5]));
L
Linus Torvalds 已提交
813 814
		break;
	case SYS_SHUTDOWN:
815
		ret = sys_shutdown(a0, a1);
L
Linus Torvalds 已提交
816 817 818 819 820 821 822 823 824 825 826 827
		break;
	case SYS_SETSOCKOPT:
		ret = compat_sys_setsockopt(a0, a1, a[2],
				compat_ptr(a[3]), a[4]);
		break;
	case SYS_GETSOCKOPT:
		ret = compat_sys_getsockopt(a0, a1, a[2],
				compat_ptr(a[3]), compat_ptr(a[4]));
		break;
	case SYS_SENDMSG:
		ret = compat_sys_sendmsg(a0, compat_ptr(a1), a[2]);
		break;
828 829 830
	case SYS_SENDMMSG:
		ret = compat_sys_sendmmsg(a0, compat_ptr(a1), a[2], a[3]);
		break;
L
Linus Torvalds 已提交
831 832 833
	case SYS_RECVMSG:
		ret = compat_sys_recvmsg(a0, compat_ptr(a1), a[2]);
		break;
834 835 836 837
	case SYS_RECVMMSG:
		ret = compat_sys_recvmmsg(a0, compat_ptr(a1), a[2], a[3],
					  compat_ptr(a[4]));
		break;
U
Ulrich Drepper 已提交
838 839
	case SYS_ACCEPT4:
		ret = sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), a[3]);
U
Ulrich Drepper 已提交
840
		break;
L
Linus Torvalds 已提交
841 842 843 844 845 846
	default:
		ret = -EINVAL;
		break;
	}
	return ret;
}