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_get_inq = 0;
56 kmsg->msg_control_user = compat_ptr(msg->msg_control);
57 kmsg->msg_controllen = msg->msg_controllen;
60 *save_addr = compat_ptr(msg->msg_name);
62 if (msg->msg_name && kmsg->msg_namelen) {
64 err = move_addr_to_kernel(compat_ptr(msg->msg_name),
71 kmsg->msg_name = NULL;
72 kmsg->msg_namelen = 0;
75 if (msg->msg_iovlen > UIO_MAXIOV)
78 kmsg->msg_iocb = NULL;
79 kmsg->msg_ubuf = NULL;
83 int get_compat_msghdr(struct msghdr *kmsg,
84 struct compat_msghdr __user *umsg,
85 struct sockaddr __user **save_addr,
88 struct compat_msghdr msg;
91 if (copy_from_user(&msg, umsg, sizeof(*umsg)))
94 err = __get_compat_msghdr(kmsg, &msg, save_addr);
98 err = import_iovec(save_addr ? READ : WRITE, compat_ptr(msg.msg_iov), msg.msg_iovlen,
99 UIO_FASTIOV, iov, &kmsg->msg_iter);
100 return err < 0 ? err : 0;
104 #define CMSG_COMPAT_ALIGN(len) ALIGN((len), sizeof(s32))
106 #define CMSG_COMPAT_DATA(cmsg) \
107 ((void __user *)((char __user *)(cmsg) + sizeof(struct compat_cmsghdr)))
108 #define CMSG_COMPAT_SPACE(len) \
109 (sizeof(struct compat_cmsghdr) + CMSG_COMPAT_ALIGN(len))
110 #define CMSG_COMPAT_LEN(len) \
111 (sizeof(struct compat_cmsghdr) + (len))
113 #define CMSG_COMPAT_FIRSTHDR(msg) \
114 (((msg)->msg_controllen) >= sizeof(struct compat_cmsghdr) ? \
115 (struct compat_cmsghdr __user *)((msg)->msg_control) : \
116 (struct compat_cmsghdr __user *)NULL)
118 #define CMSG_COMPAT_OK(ucmlen, ucmsg, mhdr) \
119 ((ucmlen) >= sizeof(struct compat_cmsghdr) && \
120 (ucmlen) <= (unsigned long) \
121 ((mhdr)->msg_controllen - \
122 ((char __user *)(ucmsg) - (char __user *)(mhdr)->msg_control_user)))
124 static inline struct compat_cmsghdr __user *cmsg_compat_nxthdr(struct msghdr *msg,
125 struct compat_cmsghdr __user *cmsg, int cmsg_len)
127 char __user *ptr = (char __user *)cmsg + CMSG_COMPAT_ALIGN(cmsg_len);
128 if ((unsigned long)(ptr + 1 - (char __user *)msg->msg_control) >
131 return (struct compat_cmsghdr __user *)ptr;
134 /* There is a lot of hair here because the alignment rules (and
135 * thus placement) of cmsg headers and length are different for
136 * 32-bit apps. -DaveM
138 int cmsghdr_from_user_compat_to_kern(struct msghdr *kmsg, struct sock *sk,
139 unsigned char *stackbuf, int stackbuf_size)
141 struct compat_cmsghdr __user *ucmsg;
142 struct cmsghdr *kcmsg, *kcmsg_base;
143 compat_size_t ucmlen;
144 __kernel_size_t kcmlen, tmp;
147 BUILD_BUG_ON(sizeof(struct compat_cmsghdr) !=
148 CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)));
151 kcmsg_base = kcmsg = (struct cmsghdr *)stackbuf;
152 ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg);
153 while (ucmsg != NULL) {
154 if (get_user(ucmlen, &ucmsg->cmsg_len))
158 if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg))
161 tmp = ((ucmlen - sizeof(*ucmsg)) + sizeof(struct cmsghdr));
162 tmp = CMSG_ALIGN(tmp);
164 ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen);
169 /* The kcmlen holds the 64-bit version of the control length.
170 * It may not be modified as we do not stick it into the kmsg
171 * until we have successfully copied over all of the data
174 if (kcmlen > stackbuf_size)
175 kcmsg_base = kcmsg = sock_kmalloc(sk, kcmlen, GFP_KERNEL);
179 /* Now copy them over neatly. */
180 memset(kcmsg, 0, kcmlen);
181 ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg);
182 while (ucmsg != NULL) {
183 struct compat_cmsghdr cmsg;
184 if (copy_from_user(&cmsg, ucmsg, sizeof(cmsg)))
186 if (!CMSG_COMPAT_OK(cmsg.cmsg_len, ucmsg, kmsg))
188 tmp = ((cmsg.cmsg_len - sizeof(*ucmsg)) + sizeof(struct cmsghdr));
189 if ((char *)kcmsg_base + kcmlen - (char *)kcmsg < CMSG_ALIGN(tmp))
191 kcmsg->cmsg_len = tmp;
192 kcmsg->cmsg_level = cmsg.cmsg_level;
193 kcmsg->cmsg_type = cmsg.cmsg_type;
194 tmp = CMSG_ALIGN(tmp);
195 if (copy_from_user(CMSG_DATA(kcmsg),
196 CMSG_COMPAT_DATA(ucmsg),
197 (cmsg.cmsg_len - sizeof(*ucmsg))))
201 kcmsg = (struct cmsghdr *)((char *)kcmsg + tmp);
202 ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, cmsg.cmsg_len);
206 * check the length of messages copied in is the same as the
207 * what we get from the first loop
209 if ((char *)kcmsg - (char *)kcmsg_base != kcmlen)
212 /* Ok, looks like we made it. Hook it up and return success. */
213 kmsg->msg_control = kcmsg_base;
214 kmsg->msg_controllen = kcmlen;
220 if (kcmsg_base != (struct cmsghdr *)stackbuf)
221 sock_kfree_s(sk, kcmsg_base, kcmlen);
225 int put_cmsg_compat(struct msghdr *kmsg, int level, int type, int len, void *data)
227 struct compat_cmsghdr __user *cm = (struct compat_cmsghdr __user *) kmsg->msg_control;
228 struct compat_cmsghdr cmhdr;
229 struct old_timeval32 ctv;
230 struct old_timespec32 cts[3];
233 if (cm == NULL || kmsg->msg_controllen < sizeof(*cm)) {
234 kmsg->msg_flags |= MSG_CTRUNC;
235 return 0; /* XXX: return error? check spec. */
238 if (!COMPAT_USE_64BIT_TIME) {
239 if (level == SOL_SOCKET && type == SO_TIMESTAMP_OLD) {
240 struct __kernel_old_timeval *tv = (struct __kernel_old_timeval *)data;
241 ctv.tv_sec = tv->tv_sec;
242 ctv.tv_usec = tv->tv_usec;
246 if (level == SOL_SOCKET &&
247 (type == SO_TIMESTAMPNS_OLD || type == SO_TIMESTAMPING_OLD)) {
248 int count = type == SO_TIMESTAMPNS_OLD ? 1 : 3;
250 struct __kernel_old_timespec *ts = data;
251 for (i = 0; i < count; i++) {
252 cts[i].tv_sec = ts[i].tv_sec;
253 cts[i].tv_nsec = ts[i].tv_nsec;
256 len = sizeof(cts[0]) * count;
260 cmlen = CMSG_COMPAT_LEN(len);
261 if (kmsg->msg_controllen < cmlen) {
262 kmsg->msg_flags |= MSG_CTRUNC;
263 cmlen = kmsg->msg_controllen;
265 cmhdr.cmsg_level = level;
266 cmhdr.cmsg_type = type;
267 cmhdr.cmsg_len = cmlen;
269 if (copy_to_user(cm, &cmhdr, sizeof cmhdr))
271 if (copy_to_user(CMSG_COMPAT_DATA(cm), data, cmlen - sizeof(struct compat_cmsghdr)))
273 cmlen = CMSG_COMPAT_SPACE(len);
274 if (kmsg->msg_controllen < cmlen)
275 cmlen = kmsg->msg_controllen;
276 kmsg->msg_control += cmlen;
277 kmsg->msg_controllen -= cmlen;
281 static int scm_max_fds_compat(struct msghdr *msg)
283 if (msg->msg_controllen <= sizeof(struct compat_cmsghdr))
285 return (msg->msg_controllen - sizeof(struct compat_cmsghdr)) / sizeof(int);
288 void scm_detach_fds_compat(struct msghdr *msg, struct scm_cookie *scm)
290 struct compat_cmsghdr __user *cm =
291 (struct compat_cmsghdr __user *)msg->msg_control;
292 unsigned int o_flags = (msg->msg_flags & MSG_CMSG_CLOEXEC) ? O_CLOEXEC : 0;
293 int fdmax = min_t(int, scm_max_fds_compat(msg), scm->fp->count);
294 int __user *cmsg_data = CMSG_COMPAT_DATA(cm);
297 for (i = 0; i < fdmax; i++) {
298 err = receive_fd_user(scm->fp->fp[i], cmsg_data + i, o_flags);
304 int cmlen = CMSG_COMPAT_LEN(i * sizeof(int));
306 err = put_user(SOL_SOCKET, &cm->cmsg_level);
308 err = put_user(SCM_RIGHTS, &cm->cmsg_type);
310 err = put_user(cmlen, &cm->cmsg_len);
312 cmlen = CMSG_COMPAT_SPACE(i * sizeof(int));
313 if (msg->msg_controllen < cmlen)
314 cmlen = msg->msg_controllen;
315 msg->msg_control += cmlen;
316 msg->msg_controllen -= cmlen;
320 if (i < scm->fp->count || (scm->fp->count && fdmax <= 0))
321 msg->msg_flags |= MSG_CTRUNC;
324 * All of the files that fit in the message have had their usage counts
325 * incremented, so we just free the list.
330 /* Argument list sizes for compat_sys_socketcall */
331 #define AL(x) ((x) * sizeof(u32))
332 static unsigned char nas[21] = {
333 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
334 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
335 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
340 static inline long __compat_sys_sendmsg(int fd,
341 struct compat_msghdr __user *msg,
344 return __sys_sendmsg(fd, (struct user_msghdr __user *)msg,
345 flags | MSG_CMSG_COMPAT, false);
348 COMPAT_SYSCALL_DEFINE3(sendmsg, int, fd, struct compat_msghdr __user *, msg,
351 return __compat_sys_sendmsg(fd, msg, flags);
354 static inline long __compat_sys_sendmmsg(int fd,
355 struct compat_mmsghdr __user *mmsg,
356 unsigned int vlen, unsigned int flags)
358 return __sys_sendmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
359 flags | MSG_CMSG_COMPAT, false);
362 COMPAT_SYSCALL_DEFINE4(sendmmsg, int, fd, struct compat_mmsghdr __user *, mmsg,
363 unsigned int, vlen, unsigned int, flags)
365 return __compat_sys_sendmmsg(fd, mmsg, vlen, flags);
368 static inline long __compat_sys_recvmsg(int fd,
369 struct compat_msghdr __user *msg,
372 return __sys_recvmsg(fd, (struct user_msghdr __user *)msg,
373 flags | MSG_CMSG_COMPAT, false);
376 COMPAT_SYSCALL_DEFINE3(recvmsg, int, fd, struct compat_msghdr __user *, msg,
379 return __compat_sys_recvmsg(fd, msg, flags);
382 static inline long __compat_sys_recvfrom(int fd, void __user *buf,
383 compat_size_t len, unsigned int flags,
384 struct sockaddr __user *addr,
387 return __sys_recvfrom(fd, buf, len, flags | MSG_CMSG_COMPAT, addr,
391 COMPAT_SYSCALL_DEFINE4(recv, int, fd, void __user *, buf, compat_size_t, len, unsigned int, flags)
393 return __compat_sys_recvfrom(fd, buf, len, flags, NULL, NULL);
396 COMPAT_SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, buf, compat_size_t, len,
397 unsigned int, flags, struct sockaddr __user *, addr,
398 int __user *, addrlen)
400 return __compat_sys_recvfrom(fd, buf, len, flags, addr, addrlen);
403 COMPAT_SYSCALL_DEFINE5(recvmmsg_time64, int, fd, struct compat_mmsghdr __user *, mmsg,
404 unsigned int, vlen, unsigned int, flags,
405 struct __kernel_timespec __user *, timeout)
407 return __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
408 flags | MSG_CMSG_COMPAT, timeout, NULL);
411 #ifdef CONFIG_COMPAT_32BIT_TIME
412 COMPAT_SYSCALL_DEFINE5(recvmmsg_time32, int, fd, struct compat_mmsghdr __user *, mmsg,
413 unsigned int, vlen, unsigned int, flags,
414 struct old_timespec32 __user *, timeout)
416 return __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
417 flags | MSG_CMSG_COMPAT, NULL, timeout);
421 COMPAT_SYSCALL_DEFINE2(socketcall, int, call, u32 __user *, args)
428 if (call < SYS_SOCKET || call > SYS_SENDMMSG)
434 if (copy_from_user(a, args, len))
437 ret = audit_socketcall_compat(len / sizeof(a[0]), a);
446 ret = __sys_socket(a0, a1, a[2]);
449 ret = __sys_bind(a0, compat_ptr(a1), a[2]);
452 ret = __sys_connect(a0, compat_ptr(a1), a[2]);
455 ret = __sys_listen(a0, a1);
458 ret = __sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), 0);
460 case SYS_GETSOCKNAME:
461 ret = __sys_getsockname(a0, compat_ptr(a1), compat_ptr(a[2]));
463 case SYS_GETPEERNAME:
464 ret = __sys_getpeername(a0, compat_ptr(a1), compat_ptr(a[2]));
467 ret = __sys_socketpair(a0, a1, a[2], compat_ptr(a[3]));
470 ret = __sys_sendto(a0, compat_ptr(a1), a[2], a[3], NULL, 0);
473 ret = __sys_sendto(a0, compat_ptr(a1), a[2], a[3],
474 compat_ptr(a[4]), a[5]);
477 ret = __compat_sys_recvfrom(a0, compat_ptr(a1), a[2], a[3],
481 ret = __compat_sys_recvfrom(a0, compat_ptr(a1), a[2], a[3],
486 ret = __sys_shutdown(a0, a1);
489 ret = __sys_setsockopt(a0, a1, a[2], compat_ptr(a[3]), a[4]);
492 ret = __sys_getsockopt(a0, a1, a[2], compat_ptr(a[3]),
496 ret = __compat_sys_sendmsg(a0, compat_ptr(a1), a[2]);
499 ret = __compat_sys_sendmmsg(a0, compat_ptr(a1), a[2], a[3]);
502 ret = __compat_sys_recvmsg(a0, compat_ptr(a1), a[2]);
505 ret = __sys_recvmmsg(a0, compat_ptr(a1), a[2],
506 a[3] | MSG_CMSG_COMPAT, NULL,
510 ret = __sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), a[3]);