1 // SPDX-License-Identifier: GPL-2.0-only
3 * 32bit Socket syscall emulation. Based on arch/sparc64/kernel/sys_sparc32.c.
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
15 #include <linux/kernel.h>
16 #include <linux/gfp.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>
33 #include <linux/uaccess.h>
34 #include <net/compat.h>
36 int __get_compat_msghdr(struct msghdr *kmsg,
37 struct compat_msghdr *msg,
38 struct sockaddr __user **save_addr)
42 kmsg->msg_flags = msg->msg_flags;
43 kmsg->msg_namelen = msg->msg_namelen;
46 kmsg->msg_namelen = 0;
48 if (kmsg->msg_namelen < 0)
51 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
52 kmsg->msg_namelen = sizeof(struct sockaddr_storage);
54 kmsg->msg_control_is_user = true;
55 kmsg->msg_control_user = compat_ptr(msg->msg_control);
56 kmsg->msg_controllen = msg->msg_controllen;
59 *save_addr = compat_ptr(msg->msg_name);
61 if (msg->msg_name && kmsg->msg_namelen) {
63 err = move_addr_to_kernel(compat_ptr(msg->msg_name),
70 kmsg->msg_name = NULL;
71 kmsg->msg_namelen = 0;
74 if (msg->msg_iovlen > UIO_MAXIOV)
77 kmsg->msg_iocb = NULL;
81 int get_compat_msghdr(struct msghdr *kmsg,
82 struct compat_msghdr __user *umsg,
83 struct sockaddr __user **save_addr,
86 struct compat_msghdr msg;
89 if (copy_from_user(&msg, umsg, sizeof(*umsg)))
92 err = __get_compat_msghdr(kmsg, &msg, save_addr);
96 err = import_iovec(save_addr ? READ : WRITE, compat_ptr(msg.msg_iov), msg.msg_iovlen,
97 UIO_FASTIOV, iov, &kmsg->msg_iter);
98 return err < 0 ? err : 0;
102 #define CMSG_COMPAT_ALIGN(len) ALIGN((len), sizeof(s32))
104 #define CMSG_COMPAT_DATA(cmsg) \
105 ((void __user *)((char __user *)(cmsg) + sizeof(struct compat_cmsghdr)))
106 #define CMSG_COMPAT_SPACE(len) \
107 (sizeof(struct compat_cmsghdr) + CMSG_COMPAT_ALIGN(len))
108 #define CMSG_COMPAT_LEN(len) \
109 (sizeof(struct compat_cmsghdr) + (len))
111 #define CMSG_COMPAT_FIRSTHDR(msg) \
112 (((msg)->msg_controllen) >= sizeof(struct compat_cmsghdr) ? \
113 (struct compat_cmsghdr __user *)((msg)->msg_control) : \
114 (struct compat_cmsghdr __user *)NULL)
116 #define CMSG_COMPAT_OK(ucmlen, ucmsg, mhdr) \
117 ((ucmlen) >= sizeof(struct compat_cmsghdr) && \
118 (ucmlen) <= (unsigned long) \
119 ((mhdr)->msg_controllen - \
120 ((char __user *)(ucmsg) - (char __user *)(mhdr)->msg_control_user)))
122 static inline struct compat_cmsghdr __user *cmsg_compat_nxthdr(struct msghdr *msg,
123 struct compat_cmsghdr __user *cmsg, int cmsg_len)
125 char __user *ptr = (char __user *)cmsg + CMSG_COMPAT_ALIGN(cmsg_len);
126 if ((unsigned long)(ptr + 1 - (char __user *)msg->msg_control) >
129 return (struct compat_cmsghdr __user *)ptr;
132 /* There is a lot of hair here because the alignment rules (and
133 * thus placement) of cmsg headers and length are different for
134 * 32-bit apps. -DaveM
136 int cmsghdr_from_user_compat_to_kern(struct msghdr *kmsg, struct sock *sk,
137 unsigned char *stackbuf, int stackbuf_size)
139 struct compat_cmsghdr __user *ucmsg;
140 struct cmsghdr *kcmsg, *kcmsg_base;
141 compat_size_t ucmlen;
142 __kernel_size_t kcmlen, tmp;
145 BUILD_BUG_ON(sizeof(struct compat_cmsghdr) !=
146 CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)));
149 kcmsg_base = kcmsg = (struct cmsghdr *)stackbuf;
150 ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg);
151 while (ucmsg != NULL) {
152 if (get_user(ucmlen, &ucmsg->cmsg_len))
156 if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg))
159 tmp = ((ucmlen - sizeof(*ucmsg)) + sizeof(struct cmsghdr));
160 tmp = CMSG_ALIGN(tmp);
162 ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen);
167 /* The kcmlen holds the 64-bit version of the control length.
168 * It may not be modified as we do not stick it into the kmsg
169 * until we have successfully copied over all of the data
172 if (kcmlen > stackbuf_size)
173 kcmsg_base = kcmsg = sock_kmalloc(sk, kcmlen, GFP_KERNEL);
177 /* Now copy them over neatly. */
178 memset(kcmsg, 0, kcmlen);
179 ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg);
180 while (ucmsg != NULL) {
181 struct compat_cmsghdr cmsg;
182 if (copy_from_user(&cmsg, ucmsg, sizeof(cmsg)))
184 if (!CMSG_COMPAT_OK(cmsg.cmsg_len, ucmsg, kmsg))
186 tmp = ((cmsg.cmsg_len - sizeof(*ucmsg)) + sizeof(struct cmsghdr));
187 if ((char *)kcmsg_base + kcmlen - (char *)kcmsg < CMSG_ALIGN(tmp))
189 kcmsg->cmsg_len = tmp;
190 kcmsg->cmsg_level = cmsg.cmsg_level;
191 kcmsg->cmsg_type = cmsg.cmsg_type;
192 tmp = CMSG_ALIGN(tmp);
193 if (copy_from_user(CMSG_DATA(kcmsg),
194 CMSG_COMPAT_DATA(ucmsg),
195 (cmsg.cmsg_len - sizeof(*ucmsg))))
199 kcmsg = (struct cmsghdr *)((char *)kcmsg + tmp);
200 ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, cmsg.cmsg_len);
204 * check the length of messages copied in is the same as the
205 * what we get from the first loop
207 if ((char *)kcmsg - (char *)kcmsg_base != kcmlen)
210 /* Ok, looks like we made it. Hook it up and return success. */
211 kmsg->msg_control = kcmsg_base;
212 kmsg->msg_controllen = kcmlen;
218 if (kcmsg_base != (struct cmsghdr *)stackbuf)
219 sock_kfree_s(sk, kcmsg_base, kcmlen);
223 int put_cmsg_compat(struct msghdr *kmsg, int level, int type, int len, void *data)
225 struct compat_cmsghdr __user *cm = (struct compat_cmsghdr __user *) kmsg->msg_control;
226 struct compat_cmsghdr cmhdr;
227 struct old_timeval32 ctv;
228 struct old_timespec32 cts[3];
231 if (cm == NULL || kmsg->msg_controllen < sizeof(*cm)) {
232 kmsg->msg_flags |= MSG_CTRUNC;
233 return 0; /* XXX: return error? check spec. */
236 if (!COMPAT_USE_64BIT_TIME) {
237 if (level == SOL_SOCKET && type == SO_TIMESTAMP_OLD) {
238 struct __kernel_old_timeval *tv = (struct __kernel_old_timeval *)data;
239 ctv.tv_sec = tv->tv_sec;
240 ctv.tv_usec = tv->tv_usec;
244 if (level == SOL_SOCKET &&
245 (type == SO_TIMESTAMPNS_OLD || type == SO_TIMESTAMPING_OLD)) {
246 int count = type == SO_TIMESTAMPNS_OLD ? 1 : 3;
248 struct __kernel_old_timespec *ts = data;
249 for (i = 0; i < count; i++) {
250 cts[i].tv_sec = ts[i].tv_sec;
251 cts[i].tv_nsec = ts[i].tv_nsec;
254 len = sizeof(cts[0]) * count;
258 cmlen = CMSG_COMPAT_LEN(len);
259 if (kmsg->msg_controllen < cmlen) {
260 kmsg->msg_flags |= MSG_CTRUNC;
261 cmlen = kmsg->msg_controllen;
263 cmhdr.cmsg_level = level;
264 cmhdr.cmsg_type = type;
265 cmhdr.cmsg_len = cmlen;
267 if (copy_to_user(cm, &cmhdr, sizeof cmhdr))
269 if (copy_to_user(CMSG_COMPAT_DATA(cm), data, cmlen - sizeof(struct compat_cmsghdr)))
271 cmlen = CMSG_COMPAT_SPACE(len);
272 if (kmsg->msg_controllen < cmlen)
273 cmlen = kmsg->msg_controllen;
274 kmsg->msg_control += cmlen;
275 kmsg->msg_controllen -= cmlen;
279 static int scm_max_fds_compat(struct msghdr *msg)
281 if (msg->msg_controllen <= sizeof(struct compat_cmsghdr))
283 return (msg->msg_controllen - sizeof(struct compat_cmsghdr)) / sizeof(int);
286 void scm_detach_fds_compat(struct msghdr *msg, struct scm_cookie *scm)
288 struct compat_cmsghdr __user *cm =
289 (struct compat_cmsghdr __user *)msg->msg_control;
290 unsigned int o_flags = (msg->msg_flags & MSG_CMSG_CLOEXEC) ? O_CLOEXEC : 0;
291 int fdmax = min_t(int, scm_max_fds_compat(msg), scm->fp->count);
292 int __user *cmsg_data = CMSG_COMPAT_DATA(cm);
295 for (i = 0; i < fdmax; i++) {
296 err = receive_fd_user(scm->fp->fp[i], cmsg_data + i, o_flags);
302 int cmlen = CMSG_COMPAT_LEN(i * sizeof(int));
304 err = put_user(SOL_SOCKET, &cm->cmsg_level);
306 err = put_user(SCM_RIGHTS, &cm->cmsg_type);
308 err = put_user(cmlen, &cm->cmsg_len);
310 cmlen = CMSG_COMPAT_SPACE(i * sizeof(int));
311 if (msg->msg_controllen < cmlen)
312 cmlen = msg->msg_controllen;
313 msg->msg_control += cmlen;
314 msg->msg_controllen -= cmlen;
318 if (i < scm->fp->count || (scm->fp->count && fdmax <= 0))
319 msg->msg_flags |= MSG_CTRUNC;
322 * All of the files that fit in the message have had their usage counts
323 * incremented, so we just free the list.
328 /* Argument list sizes for compat_sys_socketcall */
329 #define AL(x) ((x) * sizeof(u32))
330 static unsigned char nas[21] = {
331 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
332 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
333 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
338 static inline long __compat_sys_sendmsg(int fd,
339 struct compat_msghdr __user *msg,
342 return __sys_sendmsg(fd, (struct user_msghdr __user *)msg,
343 flags | MSG_CMSG_COMPAT, false);
346 COMPAT_SYSCALL_DEFINE3(sendmsg, int, fd, struct compat_msghdr __user *, msg,
349 return __compat_sys_sendmsg(fd, msg, flags);
352 static inline long __compat_sys_sendmmsg(int fd,
353 struct compat_mmsghdr __user *mmsg,
354 unsigned int vlen, unsigned int flags)
356 return __sys_sendmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
357 flags | MSG_CMSG_COMPAT, false);
360 COMPAT_SYSCALL_DEFINE4(sendmmsg, int, fd, struct compat_mmsghdr __user *, mmsg,
361 unsigned int, vlen, unsigned int, flags)
363 return __compat_sys_sendmmsg(fd, mmsg, vlen, flags);
366 static inline long __compat_sys_recvmsg(int fd,
367 struct compat_msghdr __user *msg,
370 return __sys_recvmsg(fd, (struct user_msghdr __user *)msg,
371 flags | MSG_CMSG_COMPAT, false);
374 COMPAT_SYSCALL_DEFINE3(recvmsg, int, fd, struct compat_msghdr __user *, msg,
377 return __compat_sys_recvmsg(fd, msg, flags);
380 static inline long __compat_sys_recvfrom(int fd, void __user *buf,
381 compat_size_t len, unsigned int flags,
382 struct sockaddr __user *addr,
385 return __sys_recvfrom(fd, buf, len, flags | MSG_CMSG_COMPAT, addr,
389 COMPAT_SYSCALL_DEFINE4(recv, int, fd, void __user *, buf, compat_size_t, len, unsigned int, flags)
391 return __compat_sys_recvfrom(fd, buf, len, flags, NULL, NULL);
394 COMPAT_SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, buf, compat_size_t, len,
395 unsigned int, flags, struct sockaddr __user *, addr,
396 int __user *, addrlen)
398 return __compat_sys_recvfrom(fd, buf, len, flags, addr, addrlen);
401 COMPAT_SYSCALL_DEFINE5(recvmmsg_time64, int, fd, struct compat_mmsghdr __user *, mmsg,
402 unsigned int, vlen, unsigned int, flags,
403 struct __kernel_timespec __user *, timeout)
405 return __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
406 flags | MSG_CMSG_COMPAT, timeout, NULL);
409 #ifdef CONFIG_COMPAT_32BIT_TIME
410 COMPAT_SYSCALL_DEFINE5(recvmmsg_time32, int, fd, struct compat_mmsghdr __user *, mmsg,
411 unsigned int, vlen, unsigned int, flags,
412 struct old_timespec32 __user *, timeout)
414 return __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
415 flags | MSG_CMSG_COMPAT, NULL, timeout);
419 COMPAT_SYSCALL_DEFINE2(socketcall, int, call, u32 __user *, args)
426 if (call < SYS_SOCKET || call > SYS_SENDMMSG)
432 if (copy_from_user(a, args, len))
435 ret = audit_socketcall_compat(len / sizeof(a[0]), a);
444 ret = __sys_socket(a0, a1, a[2]);
447 ret = __sys_bind(a0, compat_ptr(a1), a[2]);
450 ret = __sys_connect(a0, compat_ptr(a1), a[2]);
453 ret = __sys_listen(a0, a1);
456 ret = __sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), 0);
458 case SYS_GETSOCKNAME:
459 ret = __sys_getsockname(a0, compat_ptr(a1), compat_ptr(a[2]));
461 case SYS_GETPEERNAME:
462 ret = __sys_getpeername(a0, compat_ptr(a1), compat_ptr(a[2]));
465 ret = __sys_socketpair(a0, a1, a[2], compat_ptr(a[3]));
468 ret = __sys_sendto(a0, compat_ptr(a1), a[2], a[3], NULL, 0);
471 ret = __sys_sendto(a0, compat_ptr(a1), a[2], a[3],
472 compat_ptr(a[4]), a[5]);
475 ret = __compat_sys_recvfrom(a0, compat_ptr(a1), a[2], a[3],
479 ret = __compat_sys_recvfrom(a0, compat_ptr(a1), a[2], a[3],
484 ret = __sys_shutdown(a0, a1);
487 ret = __sys_setsockopt(a0, a1, a[2], compat_ptr(a[3]), a[4]);
490 ret = __sys_getsockopt(a0, a1, a[2], compat_ptr(a[3]),
494 ret = __compat_sys_sendmsg(a0, compat_ptr(a1), a[2]);
497 ret = __compat_sys_sendmmsg(a0, compat_ptr(a1), a[2], a[3]);
500 ret = __compat_sys_recvmsg(a0, compat_ptr(a1), a[2]);
503 ret = __sys_recvmmsg(a0, compat_ptr(a1), a[2],
504 a[3] | MSG_CMSG_COMPAT, NULL,
508 ret = __sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), a[3]);