xref: /linux/net/compat.c (revision 288440de9e5fdb4a3ff73864850f080c1250fc81)
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 
36 int __get_compat_msghdr(struct msghdr *kmsg,
37 			struct compat_msghdr *msg,
38 			struct sockaddr __user **save_addr)
39 {
40 	ssize_t err;
41 
42 	kmsg->msg_flags = msg->msg_flags;
43 	kmsg->msg_namelen = msg->msg_namelen;
44 
45 	if (!msg->msg_name)
46 		kmsg->msg_namelen = 0;
47 
48 	if (kmsg->msg_namelen < 0)
49 		return -EINVAL;
50 
51 	if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
52 		kmsg->msg_namelen = sizeof(struct sockaddr_storage);
53 
54 	kmsg->msg_control_is_user = true;
55 	kmsg->msg_control_user = compat_ptr(msg->msg_control);
56 	kmsg->msg_controllen = msg->msg_controllen;
57 
58 	if (save_addr)
59 		*save_addr = compat_ptr(msg->msg_name);
60 
61 	if (msg->msg_name && kmsg->msg_namelen) {
62 		if (!save_addr) {
63 			err = move_addr_to_kernel(compat_ptr(msg->msg_name),
64 						  kmsg->msg_namelen,
65 						  kmsg->msg_name);
66 			if (err < 0)
67 				return err;
68 		}
69 	} else {
70 		kmsg->msg_name = NULL;
71 		kmsg->msg_namelen = 0;
72 	}
73 
74 	if (msg->msg_iovlen > UIO_MAXIOV)
75 		return -EMSGSIZE;
76 
77 	kmsg->msg_iocb = NULL;
78 	kmsg->msg_ubuf = NULL;
79 	return 0;
80 }
81 
82 int get_compat_msghdr(struct msghdr *kmsg,
83 		      struct compat_msghdr __user *umsg,
84 		      struct sockaddr __user **save_addr,
85 		      struct iovec **iov)
86 {
87 	struct compat_msghdr msg;
88 	ssize_t err;
89 
90 	if (copy_from_user(&msg, umsg, sizeof(*umsg)))
91 		return -EFAULT;
92 
93 	err = __get_compat_msghdr(kmsg, &msg, save_addr);
94 	if (err)
95 		return err;
96 
97 	err = import_iovec(save_addr ? READ : WRITE, compat_ptr(msg.msg_iov), msg.msg_iovlen,
98 			   UIO_FASTIOV, iov, &kmsg->msg_iter);
99 	return err < 0 ? err : 0;
100 }
101 
102 /* Bleech... */
103 #define CMSG_COMPAT_ALIGN(len)	ALIGN((len), sizeof(s32))
104 
105 #define CMSG_COMPAT_DATA(cmsg)				\
106 	((void __user *)((char __user *)(cmsg) + sizeof(struct compat_cmsghdr)))
107 #define CMSG_COMPAT_SPACE(len)				\
108 	(sizeof(struct compat_cmsghdr) + CMSG_COMPAT_ALIGN(len))
109 #define CMSG_COMPAT_LEN(len)				\
110 	(sizeof(struct compat_cmsghdr) + (len))
111 
112 #define CMSG_COMPAT_FIRSTHDR(msg)			\
113 	(((msg)->msg_controllen) >= sizeof(struct compat_cmsghdr) ?	\
114 	 (struct compat_cmsghdr __user *)((msg)->msg_control) :		\
115 	 (struct compat_cmsghdr __user *)NULL)
116 
117 #define CMSG_COMPAT_OK(ucmlen, ucmsg, mhdr) \
118 	((ucmlen) >= sizeof(struct compat_cmsghdr) && \
119 	 (ucmlen) <= (unsigned long) \
120 	 ((mhdr)->msg_controllen - \
121 	  ((char __user *)(ucmsg) - (char __user *)(mhdr)->msg_control_user)))
122 
123 static inline struct compat_cmsghdr __user *cmsg_compat_nxthdr(struct msghdr *msg,
124 		struct compat_cmsghdr __user *cmsg, int cmsg_len)
125 {
126 	char __user *ptr = (char __user *)cmsg + CMSG_COMPAT_ALIGN(cmsg_len);
127 	if ((unsigned long)(ptr + 1 - (char __user *)msg->msg_control) >
128 			msg->msg_controllen)
129 		return NULL;
130 	return (struct compat_cmsghdr __user *)ptr;
131 }
132 
133 /* There is a lot of hair here because the alignment rules (and
134  * thus placement) of cmsg headers and length are different for
135  * 32-bit apps.  -DaveM
136  */
137 int cmsghdr_from_user_compat_to_kern(struct msghdr *kmsg, struct sock *sk,
138 			       unsigned char *stackbuf, int stackbuf_size)
139 {
140 	struct compat_cmsghdr __user *ucmsg;
141 	struct cmsghdr *kcmsg, *kcmsg_base;
142 	compat_size_t ucmlen;
143 	__kernel_size_t kcmlen, tmp;
144 	int err = -EFAULT;
145 
146 	BUILD_BUG_ON(sizeof(struct compat_cmsghdr) !=
147 		     CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)));
148 
149 	kcmlen = 0;
150 	kcmsg_base = kcmsg = (struct cmsghdr *)stackbuf;
151 	ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg);
152 	while (ucmsg != NULL) {
153 		if (get_user(ucmlen, &ucmsg->cmsg_len))
154 			return -EFAULT;
155 
156 		/* Catch bogons. */
157 		if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg))
158 			return -EINVAL;
159 
160 		tmp = ((ucmlen - sizeof(*ucmsg)) + sizeof(struct cmsghdr));
161 		tmp = CMSG_ALIGN(tmp);
162 		kcmlen += tmp;
163 		ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen);
164 	}
165 	if (kcmlen == 0)
166 		return -EINVAL;
167 
168 	/* The kcmlen holds the 64-bit version of the control length.
169 	 * It may not be modified as we do not stick it into the kmsg
170 	 * until we have successfully copied over all of the data
171 	 * from the user.
172 	 */
173 	if (kcmlen > stackbuf_size)
174 		kcmsg_base = kcmsg = sock_kmalloc(sk, kcmlen, GFP_KERNEL);
175 	if (kcmsg == NULL)
176 		return -ENOMEM;
177 
178 	/* Now copy them over neatly. */
179 	memset(kcmsg, 0, kcmlen);
180 	ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg);
181 	while (ucmsg != NULL) {
182 		struct compat_cmsghdr cmsg;
183 		if (copy_from_user(&cmsg, ucmsg, sizeof(cmsg)))
184 			goto Efault;
185 		if (!CMSG_COMPAT_OK(cmsg.cmsg_len, ucmsg, kmsg))
186 			goto Einval;
187 		tmp = ((cmsg.cmsg_len - sizeof(*ucmsg)) + sizeof(struct cmsghdr));
188 		if ((char *)kcmsg_base + kcmlen - (char *)kcmsg < CMSG_ALIGN(tmp))
189 			goto Einval;
190 		kcmsg->cmsg_len = tmp;
191 		kcmsg->cmsg_level = cmsg.cmsg_level;
192 		kcmsg->cmsg_type = cmsg.cmsg_type;
193 		tmp = CMSG_ALIGN(tmp);
194 		if (copy_from_user(CMSG_DATA(kcmsg),
195 				   CMSG_COMPAT_DATA(ucmsg),
196 				   (cmsg.cmsg_len - sizeof(*ucmsg))))
197 			goto Efault;
198 
199 		/* Advance. */
200 		kcmsg = (struct cmsghdr *)((char *)kcmsg + tmp);
201 		ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, cmsg.cmsg_len);
202 	}
203 
204 	/*
205 	 * check the length of messages copied in is the same as the
206 	 * what we get from the first loop
207 	 */
208 	if ((char *)kcmsg - (char *)kcmsg_base != kcmlen)
209 		goto Einval;
210 
211 	/* Ok, looks like we made it.  Hook it up and return success. */
212 	kmsg->msg_control = kcmsg_base;
213 	kmsg->msg_controllen = kcmlen;
214 	return 0;
215 
216 Einval:
217 	err = -EINVAL;
218 Efault:
219 	if (kcmsg_base != (struct cmsghdr *)stackbuf)
220 		sock_kfree_s(sk, kcmsg_base, kcmlen);
221 	return err;
222 }
223 
224 int put_cmsg_compat(struct msghdr *kmsg, int level, int type, int len, void *data)
225 {
226 	struct compat_cmsghdr __user *cm = (struct compat_cmsghdr __user *) kmsg->msg_control;
227 	struct compat_cmsghdr cmhdr;
228 	struct old_timeval32 ctv;
229 	struct old_timespec32 cts[3];
230 	int cmlen;
231 
232 	if (cm == NULL || kmsg->msg_controllen < sizeof(*cm)) {
233 		kmsg->msg_flags |= MSG_CTRUNC;
234 		return 0; /* XXX: return error? check spec. */
235 	}
236 
237 	if (!COMPAT_USE_64BIT_TIME) {
238 		if (level == SOL_SOCKET && type == SO_TIMESTAMP_OLD) {
239 			struct __kernel_old_timeval *tv = (struct __kernel_old_timeval *)data;
240 			ctv.tv_sec = tv->tv_sec;
241 			ctv.tv_usec = tv->tv_usec;
242 			data = &ctv;
243 			len = sizeof(ctv);
244 		}
245 		if (level == SOL_SOCKET &&
246 		    (type == SO_TIMESTAMPNS_OLD || type == SO_TIMESTAMPING_OLD)) {
247 			int count = type == SO_TIMESTAMPNS_OLD ? 1 : 3;
248 			int i;
249 			struct __kernel_old_timespec *ts = data;
250 			for (i = 0; i < count; i++) {
251 				cts[i].tv_sec = ts[i].tv_sec;
252 				cts[i].tv_nsec = ts[i].tv_nsec;
253 			}
254 			data = &cts;
255 			len = sizeof(cts[0]) * count;
256 		}
257 	}
258 
259 	cmlen = CMSG_COMPAT_LEN(len);
260 	if (kmsg->msg_controllen < cmlen) {
261 		kmsg->msg_flags |= MSG_CTRUNC;
262 		cmlen = kmsg->msg_controllen;
263 	}
264 	cmhdr.cmsg_level = level;
265 	cmhdr.cmsg_type = type;
266 	cmhdr.cmsg_len = cmlen;
267 
268 	if (copy_to_user(cm, &cmhdr, sizeof cmhdr))
269 		return -EFAULT;
270 	if (copy_to_user(CMSG_COMPAT_DATA(cm), data, cmlen - sizeof(struct compat_cmsghdr)))
271 		return -EFAULT;
272 	cmlen = CMSG_COMPAT_SPACE(len);
273 	if (kmsg->msg_controllen < cmlen)
274 		cmlen = kmsg->msg_controllen;
275 	kmsg->msg_control += cmlen;
276 	kmsg->msg_controllen -= cmlen;
277 	return 0;
278 }
279 
280 static int scm_max_fds_compat(struct msghdr *msg)
281 {
282 	if (msg->msg_controllen <= sizeof(struct compat_cmsghdr))
283 		return 0;
284 	return (msg->msg_controllen - sizeof(struct compat_cmsghdr)) / sizeof(int);
285 }
286 
287 void scm_detach_fds_compat(struct msghdr *msg, struct scm_cookie *scm)
288 {
289 	struct compat_cmsghdr __user *cm =
290 		(struct compat_cmsghdr __user *)msg->msg_control;
291 	unsigned int o_flags = (msg->msg_flags & MSG_CMSG_CLOEXEC) ? O_CLOEXEC : 0;
292 	int fdmax = min_t(int, scm_max_fds_compat(msg), scm->fp->count);
293 	int __user *cmsg_data = CMSG_COMPAT_DATA(cm);
294 	int err = 0, i;
295 
296 	for (i = 0; i < fdmax; i++) {
297 		err = receive_fd_user(scm->fp->fp[i], cmsg_data + i, o_flags);
298 		if (err < 0)
299 			break;
300 	}
301 
302 	if (i > 0) {
303 		int cmlen = CMSG_COMPAT_LEN(i * sizeof(int));
304 
305 		err = put_user(SOL_SOCKET, &cm->cmsg_level);
306 		if (!err)
307 			err = put_user(SCM_RIGHTS, &cm->cmsg_type);
308 		if (!err)
309 			err = put_user(cmlen, &cm->cmsg_len);
310 		if (!err) {
311 			cmlen = CMSG_COMPAT_SPACE(i * sizeof(int));
312 			if (msg->msg_controllen < cmlen)
313 				cmlen = msg->msg_controllen;
314 			msg->msg_control += cmlen;
315 			msg->msg_controllen -= cmlen;
316 		}
317 	}
318 
319 	if (i < scm->fp->count || (scm->fp->count && fdmax <= 0))
320 		msg->msg_flags |= MSG_CTRUNC;
321 
322 	/*
323 	 * All of the files that fit in the message have had their usage counts
324 	 * incremented, so we just free the list.
325 	 */
326 	__scm_destroy(scm);
327 }
328 
329 /* Argument list sizes for compat_sys_socketcall */
330 #define AL(x) ((x) * sizeof(u32))
331 static unsigned char nas[21] = {
332 	AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
333 	AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
334 	AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
335 	AL(4), AL(5), AL(4)
336 };
337 #undef AL
338 
339 static inline long __compat_sys_sendmsg(int fd,
340 					struct compat_msghdr __user *msg,
341 					unsigned int flags)
342 {
343 	return __sys_sendmsg(fd, (struct user_msghdr __user *)msg,
344 			     flags | MSG_CMSG_COMPAT, false);
345 }
346 
347 COMPAT_SYSCALL_DEFINE3(sendmsg, int, fd, struct compat_msghdr __user *, msg,
348 		       unsigned int, flags)
349 {
350 	return __compat_sys_sendmsg(fd, msg, flags);
351 }
352 
353 static inline long __compat_sys_sendmmsg(int fd,
354 					 struct compat_mmsghdr __user *mmsg,
355 					 unsigned int vlen, unsigned int flags)
356 {
357 	return __sys_sendmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
358 			      flags | MSG_CMSG_COMPAT, false);
359 }
360 
361 COMPAT_SYSCALL_DEFINE4(sendmmsg, int, fd, struct compat_mmsghdr __user *, mmsg,
362 		       unsigned int, vlen, unsigned int, flags)
363 {
364 	return __compat_sys_sendmmsg(fd, mmsg, vlen, flags);
365 }
366 
367 static inline long __compat_sys_recvmsg(int fd,
368 					struct compat_msghdr __user *msg,
369 					unsigned int flags)
370 {
371 	return __sys_recvmsg(fd, (struct user_msghdr __user *)msg,
372 			     flags | MSG_CMSG_COMPAT, false);
373 }
374 
375 COMPAT_SYSCALL_DEFINE3(recvmsg, int, fd, struct compat_msghdr __user *, msg,
376 		       unsigned int, flags)
377 {
378 	return __compat_sys_recvmsg(fd, msg, flags);
379 }
380 
381 static inline long __compat_sys_recvfrom(int fd, void __user *buf,
382 					 compat_size_t len, unsigned int flags,
383 					 struct sockaddr __user *addr,
384 					 int __user *addrlen)
385 {
386 	return __sys_recvfrom(fd, buf, len, flags | MSG_CMSG_COMPAT, addr,
387 			      addrlen);
388 }
389 
390 COMPAT_SYSCALL_DEFINE4(recv, int, fd, void __user *, buf, compat_size_t, len, unsigned int, flags)
391 {
392 	return __compat_sys_recvfrom(fd, buf, len, flags, NULL, NULL);
393 }
394 
395 COMPAT_SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, buf, compat_size_t, len,
396 		       unsigned int, flags, struct sockaddr __user *, addr,
397 		       int __user *, addrlen)
398 {
399 	return __compat_sys_recvfrom(fd, buf, len, flags, addr, addrlen);
400 }
401 
402 COMPAT_SYSCALL_DEFINE5(recvmmsg_time64, int, fd, struct compat_mmsghdr __user *, mmsg,
403 		       unsigned int, vlen, unsigned int, flags,
404 		       struct __kernel_timespec __user *, timeout)
405 {
406 	return __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
407 			      flags | MSG_CMSG_COMPAT, timeout, NULL);
408 }
409 
410 #ifdef CONFIG_COMPAT_32BIT_TIME
411 COMPAT_SYSCALL_DEFINE5(recvmmsg_time32, int, fd, struct compat_mmsghdr __user *, mmsg,
412 		       unsigned int, vlen, unsigned int, flags,
413 		       struct old_timespec32 __user *, timeout)
414 {
415 	return __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
416 			      flags | MSG_CMSG_COMPAT, NULL, timeout);
417 }
418 #endif
419 
420 COMPAT_SYSCALL_DEFINE2(socketcall, int, call, u32 __user *, args)
421 {
422 	u32 a[AUDITSC_ARGS];
423 	unsigned int len;
424 	u32 a0, a1;
425 	int ret;
426 
427 	if (call < SYS_SOCKET || call > SYS_SENDMMSG)
428 		return -EINVAL;
429 	len = nas[call];
430 	if (len > sizeof(a))
431 		return -EINVAL;
432 
433 	if (copy_from_user(a, args, len))
434 		return -EFAULT;
435 
436 	ret = audit_socketcall_compat(len / sizeof(a[0]), a);
437 	if (ret)
438 		return ret;
439 
440 	a0 = a[0];
441 	a1 = a[1];
442 
443 	switch (call) {
444 	case SYS_SOCKET:
445 		ret = __sys_socket(a0, a1, a[2]);
446 		break;
447 	case SYS_BIND:
448 		ret = __sys_bind(a0, compat_ptr(a1), a[2]);
449 		break;
450 	case SYS_CONNECT:
451 		ret = __sys_connect(a0, compat_ptr(a1), a[2]);
452 		break;
453 	case SYS_LISTEN:
454 		ret = __sys_listen(a0, a1);
455 		break;
456 	case SYS_ACCEPT:
457 		ret = __sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), 0);
458 		break;
459 	case SYS_GETSOCKNAME:
460 		ret = __sys_getsockname(a0, compat_ptr(a1), compat_ptr(a[2]));
461 		break;
462 	case SYS_GETPEERNAME:
463 		ret = __sys_getpeername(a0, compat_ptr(a1), compat_ptr(a[2]));
464 		break;
465 	case SYS_SOCKETPAIR:
466 		ret = __sys_socketpair(a0, a1, a[2], compat_ptr(a[3]));
467 		break;
468 	case SYS_SEND:
469 		ret = __sys_sendto(a0, compat_ptr(a1), a[2], a[3], NULL, 0);
470 		break;
471 	case SYS_SENDTO:
472 		ret = __sys_sendto(a0, compat_ptr(a1), a[2], a[3],
473 				   compat_ptr(a[4]), a[5]);
474 		break;
475 	case SYS_RECV:
476 		ret = __compat_sys_recvfrom(a0, compat_ptr(a1), a[2], a[3],
477 					    NULL, NULL);
478 		break;
479 	case SYS_RECVFROM:
480 		ret = __compat_sys_recvfrom(a0, compat_ptr(a1), a[2], a[3],
481 					    compat_ptr(a[4]),
482 					    compat_ptr(a[5]));
483 		break;
484 	case SYS_SHUTDOWN:
485 		ret = __sys_shutdown(a0, a1);
486 		break;
487 	case SYS_SETSOCKOPT:
488 		ret = __sys_setsockopt(a0, a1, a[2], compat_ptr(a[3]), a[4]);
489 		break;
490 	case SYS_GETSOCKOPT:
491 		ret = __sys_getsockopt(a0, a1, a[2], compat_ptr(a[3]),
492 				       compat_ptr(a[4]));
493 		break;
494 	case SYS_SENDMSG:
495 		ret = __compat_sys_sendmsg(a0, compat_ptr(a1), a[2]);
496 		break;
497 	case SYS_SENDMMSG:
498 		ret = __compat_sys_sendmmsg(a0, compat_ptr(a1), a[2], a[3]);
499 		break;
500 	case SYS_RECVMSG:
501 		ret = __compat_sys_recvmsg(a0, compat_ptr(a1), a[2]);
502 		break;
503 	case SYS_RECVMMSG:
504 		ret = __sys_recvmmsg(a0, compat_ptr(a1), a[2],
505 				     a[3] | MSG_CMSG_COMPAT, NULL,
506 				     compat_ptr(a[4]));
507 		break;
508 	case SYS_ACCEPT4:
509 		ret = __sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), a[3]);
510 		break;
511 	default:
512 		ret = -EINVAL;
513 		break;
514 	}
515 	return ret;
516 }
517