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;
78 kmsg->msg_ubuf = NULL;
82 int get_compat_msghdr(struct msghdr *kmsg,
83 struct compat_msghdr __user *umsg,
84 struct sockaddr __user **save_addr,
87 struct compat_msghdr msg;
90 if (copy_from_user(&msg, umsg, sizeof(*umsg)))
93 err = __get_compat_msghdr(kmsg, &msg, save_addr);
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;
103 #define CMSG_COMPAT_ALIGN(len) ALIGN((len), sizeof(s32))
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))
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)
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)))
123 static inline struct compat_cmsghdr __user *cmsg_compat_nxthdr(struct msghdr *msg,
124 struct compat_cmsghdr __user *cmsg, int cmsg_len)
126 char __user *ptr = (char __user *)cmsg + CMSG_COMPAT_ALIGN(cmsg_len);
127 if ((unsigned long)(ptr + 1 - (char __user *)msg->msg_control) >
130 return (struct compat_cmsghdr __user *)ptr;
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
137 int cmsghdr_from_user_compat_to_kern(struct msghdr *kmsg, struct sock *sk,
138 unsigned char *stackbuf, int stackbuf_size)
140 struct compat_cmsghdr __user *ucmsg;
141 struct cmsghdr *kcmsg, *kcmsg_base;
142 compat_size_t ucmlen;
143 __kernel_size_t kcmlen, tmp;
146 BUILD_BUG_ON(sizeof(struct compat_cmsghdr) !=
147 CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)));
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))
157 if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg))
160 tmp = ((ucmlen - sizeof(*ucmsg)) + sizeof(struct cmsghdr));
161 tmp = CMSG_ALIGN(tmp);
163 ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen);
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
173 if (kcmlen > stackbuf_size)
174 kcmsg_base = kcmsg = sock_kmalloc(sk, kcmlen, GFP_KERNEL);
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)))
185 if (!CMSG_COMPAT_OK(cmsg.cmsg_len, ucmsg, kmsg))
187 tmp = ((cmsg.cmsg_len - sizeof(*ucmsg)) + sizeof(struct cmsghdr));
188 if ((char *)kcmsg_base + kcmlen - (char *)kcmsg < CMSG_ALIGN(tmp))
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))))
200 kcmsg = (struct cmsghdr *)((char *)kcmsg + tmp);
201 ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, cmsg.cmsg_len);
205 * check the length of messages copied in is the same as the
206 * what we get from the first loop
208 if ((char *)kcmsg - (char *)kcmsg_base != kcmlen)
211 /* Ok, looks like we made it. Hook it up and return success. */
212 kmsg->msg_control = kcmsg_base;
213 kmsg->msg_controllen = kcmlen;
219 if (kcmsg_base != (struct cmsghdr *)stackbuf)
220 sock_kfree_s(sk, kcmsg_base, kcmlen);
224 int put_cmsg_compat(struct msghdr *kmsg, int level, int type, int len, void *data)
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];
232 if (cm == NULL || kmsg->msg_controllen < sizeof(*cm)) {
233 kmsg->msg_flags |= MSG_CTRUNC;
234 return 0; /* XXX: return error? check spec. */
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;
245 if (level == SOL_SOCKET &&
246 (type == SO_TIMESTAMPNS_OLD || type == SO_TIMESTAMPING_OLD)) {
247 int count = type == SO_TIMESTAMPNS_OLD ? 1 : 3;
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;
255 len = sizeof(cts[0]) * count;
259 cmlen = CMSG_COMPAT_LEN(len);
260 if (kmsg->msg_controllen < cmlen) {
261 kmsg->msg_flags |= MSG_CTRUNC;
262 cmlen = kmsg->msg_controllen;
264 cmhdr.cmsg_level = level;
265 cmhdr.cmsg_type = type;
266 cmhdr.cmsg_len = cmlen;
268 if (copy_to_user(cm, &cmhdr, sizeof cmhdr))
270 if (copy_to_user(CMSG_COMPAT_DATA(cm), data, cmlen - sizeof(struct compat_cmsghdr)))
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;
280 static int scm_max_fds_compat(struct msghdr *msg)
282 if (msg->msg_controllen <= sizeof(struct compat_cmsghdr))
284 return (msg->msg_controllen - sizeof(struct compat_cmsghdr)) / sizeof(int);
287 void scm_detach_fds_compat(struct msghdr *msg, struct scm_cookie *scm)
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);
296 for (i = 0; i < fdmax; i++) {
297 err = receive_fd_user(scm->fp->fp[i], cmsg_data + i, o_flags);
303 int cmlen = CMSG_COMPAT_LEN(i * sizeof(int));
305 err = put_user(SOL_SOCKET, &cm->cmsg_level);
307 err = put_user(SCM_RIGHTS, &cm->cmsg_type);
309 err = put_user(cmlen, &cm->cmsg_len);
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;
319 if (i < scm->fp->count || (scm->fp->count && fdmax <= 0))
320 msg->msg_flags |= MSG_CTRUNC;
323 * All of the files that fit in the message have had their usage counts
324 * incremented, so we just free the list.
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),
339 static inline long __compat_sys_sendmsg(int fd,
340 struct compat_msghdr __user *msg,
343 return __sys_sendmsg(fd, (struct user_msghdr __user *)msg,
344 flags | MSG_CMSG_COMPAT, false);
347 COMPAT_SYSCALL_DEFINE3(sendmsg, int, fd, struct compat_msghdr __user *, msg,
350 return __compat_sys_sendmsg(fd, msg, flags);
353 static inline long __compat_sys_sendmmsg(int fd,
354 struct compat_mmsghdr __user *mmsg,
355 unsigned int vlen, unsigned int flags)
357 return __sys_sendmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
358 flags | MSG_CMSG_COMPAT, false);
361 COMPAT_SYSCALL_DEFINE4(sendmmsg, int, fd, struct compat_mmsghdr __user *, mmsg,
362 unsigned int, vlen, unsigned int, flags)
364 return __compat_sys_sendmmsg(fd, mmsg, vlen, flags);
367 static inline long __compat_sys_recvmsg(int fd,
368 struct compat_msghdr __user *msg,
371 return __sys_recvmsg(fd, (struct user_msghdr __user *)msg,
372 flags | MSG_CMSG_COMPAT, false);
375 COMPAT_SYSCALL_DEFINE3(recvmsg, int, fd, struct compat_msghdr __user *, msg,
378 return __compat_sys_recvmsg(fd, msg, flags);
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,
386 return __sys_recvfrom(fd, buf, len, flags | MSG_CMSG_COMPAT, addr,
390 COMPAT_SYSCALL_DEFINE4(recv, int, fd, void __user *, buf, compat_size_t, len, unsigned int, flags)
392 return __compat_sys_recvfrom(fd, buf, len, flags, NULL, NULL);
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)
399 return __compat_sys_recvfrom(fd, buf, len, flags, addr, addrlen);
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)
406 return __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
407 flags | MSG_CMSG_COMPAT, timeout, NULL);
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)
415 return __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
416 flags | MSG_CMSG_COMPAT, NULL, timeout);
420 COMPAT_SYSCALL_DEFINE2(socketcall, int, call, u32 __user *, args)
427 if (call < SYS_SOCKET || call > SYS_SENDMMSG)
433 if (copy_from_user(a, args, len))
436 ret = audit_socketcall_compat(len / sizeof(a[0]), a);
445 ret = __sys_socket(a0, a1, a[2]);
448 ret = __sys_bind(a0, compat_ptr(a1), a[2]);
451 ret = __sys_connect(a0, compat_ptr(a1), a[2]);
454 ret = __sys_listen(a0, a1);
457 ret = __sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), 0);
459 case SYS_GETSOCKNAME:
460 ret = __sys_getsockname(a0, compat_ptr(a1), compat_ptr(a[2]));
462 case SYS_GETPEERNAME:
463 ret = __sys_getpeername(a0, compat_ptr(a1), compat_ptr(a[2]));
466 ret = __sys_socketpair(a0, a1, a[2], compat_ptr(a[3]));
469 ret = __sys_sendto(a0, compat_ptr(a1), a[2], a[3], NULL, 0);
472 ret = __sys_sendto(a0, compat_ptr(a1), a[2], a[3],
473 compat_ptr(a[4]), a[5]);
476 ret = __compat_sys_recvfrom(a0, compat_ptr(a1), a[2], a[3],
480 ret = __compat_sys_recvfrom(a0, compat_ptr(a1), a[2], a[3],
485 ret = __sys_shutdown(a0, a1);
488 ret = __sys_setsockopt(a0, a1, a[2], compat_ptr(a[3]), a[4]);
491 ret = __sys_getsockopt(a0, a1, a[2], compat_ptr(a[3]),
495 ret = __compat_sys_sendmsg(a0, compat_ptr(a1), a[2]);
498 ret = __compat_sys_sendmmsg(a0, compat_ptr(a1), a[2], a[3]);
501 ret = __compat_sys_recvmsg(a0, compat_ptr(a1), a[2]);
504 ret = __sys_recvmmsg(a0, compat_ptr(a1), a[2],
505 a[3] | MSG_CMSG_COMPAT, NULL,
509 ret = __sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), a[3]);