blob: 6cd2e7683dd06be7cd67ce377ab30c8486482cb5 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +09002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * 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 <[email protected]>
7 * Copyright (C) 1999 Arun Sharma <[email protected]>
8 * Copyright (C) 1997,1998 Jakub Jelinek ([email protected])
9 * Copyright (C) 1997 David S. Miller ([email protected])
10 * Copyright (C) 2000 Hewlett-Packard Co.
11 * Copyright (C) 2000 David Mosberger-Tang <[email protected]>
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +090012 * Copyright (C) 2000,2001 Andi Kleen, SuSE Labs
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
14
15#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/security.h>
Richard Guy Briggs62bc3062017-01-17 11:07:15 -050026#include <linux/audit.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040027#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include <net/scm.h>
30#include <net/sock.h>
David L Stevensdae50292008-04-27 01:06:07 -070031#include <net/ip.h>
32#include <net/ipv6.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080033#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <net/compat.h>
35
Jens Axboe0a384ab2020-02-27 08:11:20 -070036int __get_compat_msghdr(struct msghdr *kmsg,
37 struct compat_msghdr __user *umsg,
38 struct sockaddr __user **save_addr,
39 compat_uptr_t *ptr, compat_size_t *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Al Viro5da028a2017-06-27 18:24:21 -040041 struct compat_msghdr msg;
Al Viro08adb7d2014-11-10 20:23:13 -050042 ssize_t err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Al Viro5da028a2017-06-27 18:24:21 -040044 if (copy_from_user(&msg, umsg, sizeof(*umsg)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 return -EFAULT;
Catalin Marinas91edd092015-03-20 16:48:13 +000046
Al Viro5da028a2017-06-27 18:24:21 -040047 kmsg->msg_flags = msg.msg_flags;
48 kmsg->msg_namelen = msg.msg_namelen;
49
50 if (!msg.msg_name)
Catalin Marinas91edd092015-03-20 16:48:13 +000051 kmsg->msg_namelen = 0;
52
53 if (kmsg->msg_namelen < 0)
54 return -EINVAL;
55
Dan Carpenter1661bf32013-10-03 00:27:20 +030056 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
Dan Carpenterdb31c552013-11-27 15:40:21 +030057 kmsg->msg_namelen = sizeof(struct sockaddr_storage);
Al Viro5da028a2017-06-27 18:24:21 -040058
Christoph Hellwig1f466e12020-05-11 13:59:13 +020059 kmsg->msg_control_is_user = true;
60 kmsg->msg_control_user = compat_ptr(msg.msg_control);
Al Viro5da028a2017-06-27 18:24:21 -040061 kmsg->msg_controllen = msg.msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Al Viro08adb7d2014-11-10 20:23:13 -050063 if (save_addr)
Al Viro5da028a2017-06-27 18:24:21 -040064 *save_addr = compat_ptr(msg.msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Al Viro5da028a2017-06-27 18:24:21 -040066 if (msg.msg_name && kmsg->msg_namelen) {
Al Viro08adb7d2014-11-10 20:23:13 -050067 if (!save_addr) {
Al Viro5da028a2017-06-27 18:24:21 -040068 err = move_addr_to_kernel(compat_ptr(msg.msg_name),
Al Viro08adb7d2014-11-10 20:23:13 -050069 kmsg->msg_namelen,
70 kmsg->msg_name);
Stephen Hemmingere71a4782007-04-10 20:10:33 -070071 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return err;
73 }
Andrey Ryabinin40eea802014-07-26 21:26:58 +040074 } else {
Al Viro08adb7d2014-11-10 20:23:13 -050075 kmsg->msg_name = NULL;
76 kmsg->msg_namelen = 0;
Andrey Ryabinin40eea802014-07-26 21:26:58 +040077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Al Viro5da028a2017-06-27 18:24:21 -040079 if (msg.msg_iovlen > UIO_MAXIOV)
Al Viro08449322014-11-09 22:33:45 -050080 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
[email protected]0345f932015-03-19 12:31:25 -070082 kmsg->msg_iocb = NULL;
Pavel Begunkov7c701d92022-07-12 21:52:29 +010083 kmsg->msg_ubuf = NULL;
Jens Axboe0a384ab2020-02-27 08:11:20 -070084 *ptr = msg.msg_iov;
85 *len = msg.msg_iovlen;
86 return 0;
87}
[email protected]0345f932015-03-19 12:31:25 -070088
Jens Axboe0a384ab2020-02-27 08:11:20 -070089int get_compat_msghdr(struct msghdr *kmsg,
90 struct compat_msghdr __user *umsg,
91 struct sockaddr __user **save_addr,
92 struct iovec **iov)
93{
94 compat_uptr_t ptr;
95 compat_size_t len;
96 ssize_t err;
97
98 err = __get_compat_msghdr(kmsg, umsg, save_addr, &ptr, &len);
99 if (err)
100 return err;
101
Christoph Hellwig89cd35c2020-09-25 06:51:41 +0200102 err = import_iovec(save_addr ? READ : WRITE, compat_ptr(ptr), len,
103 UIO_FASTIOV, iov, &kmsg->msg_iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -0600104 return err < 0 ? err : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
107/* Bleech... */
108#define CMSG_COMPAT_ALIGN(len) ALIGN((len), sizeof(s32))
109
110#define CMSG_COMPAT_DATA(cmsg) \
yuan linyu1ff8ceb2017-01-03 20:42:17 +0800111 ((void __user *)((char __user *)(cmsg) + sizeof(struct compat_cmsghdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#define CMSG_COMPAT_SPACE(len) \
yuan linyu1ff8ceb2017-01-03 20:42:17 +0800113 (sizeof(struct compat_cmsghdr) + CMSG_COMPAT_ALIGN(len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#define CMSG_COMPAT_LEN(len) \
yuan linyu1ff8ceb2017-01-03 20:42:17 +0800115 (sizeof(struct compat_cmsghdr) + (len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117#define CMSG_COMPAT_FIRSTHDR(msg) \
118 (((msg)->msg_controllen) >= sizeof(struct compat_cmsghdr) ? \
119 (struct compat_cmsghdr __user *)((msg)->msg_control) : \
120 (struct compat_cmsghdr __user *)NULL)
121
122#define CMSG_COMPAT_OK(ucmlen, ucmsg, mhdr) \
123 ((ucmlen) >= sizeof(struct compat_cmsghdr) && \
124 (ucmlen) <= (unsigned long) \
125 ((mhdr)->msg_controllen - \
Christoph Hellwig1f466e12020-05-11 13:59:13 +0200126 ((char __user *)(ucmsg) - (char __user *)(mhdr)->msg_control_user)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128static inline struct compat_cmsghdr __user *cmsg_compat_nxthdr(struct msghdr *msg,
129 struct compat_cmsghdr __user *cmsg, int cmsg_len)
130{
131 char __user *ptr = (char __user *)cmsg + CMSG_COMPAT_ALIGN(cmsg_len);
132 if ((unsigned long)(ptr + 1 - (char __user *)msg->msg_control) >
133 msg->msg_controllen)
134 return NULL;
135 return (struct compat_cmsghdr __user *)ptr;
136}
137
138/* There is a lot of hair here because the alignment rules (and
139 * thus placement) of cmsg headers and length are different for
140 * 32-bit apps. -DaveM
141 */
Al Viro8920e8f2005-09-07 18:28:51 -0700142int cmsghdr_from_user_compat_to_kern(struct msghdr *kmsg, struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 unsigned char *stackbuf, int stackbuf_size)
144{
145 struct compat_cmsghdr __user *ucmsg;
146 struct cmsghdr *kcmsg, *kcmsg_base;
147 compat_size_t ucmlen;
148 __kernel_size_t kcmlen, tmp;
Al Viro8920e8f2005-09-07 18:28:51 -0700149 int err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
David S. Millerac4340f2017-01-04 13:24:19 -0500151 BUILD_BUG_ON(sizeof(struct compat_cmsghdr) !=
152 CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)));
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 kcmlen = 0;
155 kcmsg_base = kcmsg = (struct cmsghdr *)stackbuf;
156 ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700157 while (ucmsg != NULL) {
158 if (get_user(ucmlen, &ucmsg->cmsg_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return -EFAULT;
160
161 /* Catch bogons. */
162 if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg))
163 return -EINVAL;
164
yuan linyu1ff8ceb2017-01-03 20:42:17 +0800165 tmp = ((ucmlen - sizeof(*ucmsg)) + sizeof(struct cmsghdr));
Al Viro8920e8f2005-09-07 18:28:51 -0700166 tmp = CMSG_ALIGN(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 kcmlen += tmp;
168 ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen);
169 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700170 if (kcmlen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return -EINVAL;
172
173 /* The kcmlen holds the 64-bit version of the control length.
174 * It may not be modified as we do not stick it into the kmsg
175 * until we have successfully copied over all of the data
176 * from the user.
177 */
Al Viro8920e8f2005-09-07 18:28:51 -0700178 if (kcmlen > stackbuf_size)
179 kcmsg_base = kcmsg = sock_kmalloc(sk, kcmlen, GFP_KERNEL);
180 if (kcmsg == NULL)
Zheng Yongjun49251cd2021-06-02 22:06:40 +0800181 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 /* Now copy them over neatly. */
184 memset(kcmsg, 0, kcmlen);
185 ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700186 while (ucmsg != NULL) {
Al Viro547ce4c2020-05-31 02:06:55 +0100187 struct compat_cmsghdr cmsg;
188 if (copy_from_user(&cmsg, ucmsg, sizeof(cmsg)))
Al Viro8920e8f2005-09-07 18:28:51 -0700189 goto Efault;
Al Viro547ce4c2020-05-31 02:06:55 +0100190 if (!CMSG_COMPAT_OK(cmsg.cmsg_len, ucmsg, kmsg))
Al Viro8920e8f2005-09-07 18:28:51 -0700191 goto Einval;
Al Viro547ce4c2020-05-31 02:06:55 +0100192 tmp = ((cmsg.cmsg_len - sizeof(*ucmsg)) + sizeof(struct cmsghdr));
Al Viro8920e8f2005-09-07 18:28:51 -0700193 if ((char *)kcmsg_base + kcmlen - (char *)kcmsg < CMSG_ALIGN(tmp))
194 goto Einval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 kcmsg->cmsg_len = tmp;
Al Viro547ce4c2020-05-31 02:06:55 +0100196 kcmsg->cmsg_level = cmsg.cmsg_level;
197 kcmsg->cmsg_type = cmsg.cmsg_type;
Al Viro8920e8f2005-09-07 18:28:51 -0700198 tmp = CMSG_ALIGN(tmp);
Al Viro547ce4c2020-05-31 02:06:55 +0100199 if (copy_from_user(CMSG_DATA(kcmsg),
Al Viro8920e8f2005-09-07 18:28:51 -0700200 CMSG_COMPAT_DATA(ucmsg),
Al Viro547ce4c2020-05-31 02:06:55 +0100201 (cmsg.cmsg_len - sizeof(*ucmsg))))
Al Viro8920e8f2005-09-07 18:28:51 -0700202 goto Efault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 /* Advance. */
Al Viro8920e8f2005-09-07 18:28:51 -0700205 kcmsg = (struct cmsghdr *)((char *)kcmsg + tmp);
Al Viro181964e2020-07-27 19:22:20 +0100206 ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, cmsg.cmsg_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208
Meng Xuc2a64bb2017-09-19 13:19:13 -0400209 /*
210 * check the length of messages copied in is the same as the
211 * what we get from the first loop
212 */
213 if ((char *)kcmsg - (char *)kcmsg_base != kcmlen)
214 goto Einval;
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 /* Ok, looks like we made it. Hook it up and return success. */
217 kmsg->msg_control = kcmsg_base;
218 kmsg->msg_controllen = kcmlen;
219 return 0;
220
Al Viro8920e8f2005-09-07 18:28:51 -0700221Einval:
222 err = -EINVAL;
223Efault:
224 if (kcmsg_base != (struct cmsghdr *)stackbuf)
225 sock_kfree_s(sk, kcmsg_base, kcmlen);
226 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
229int put_cmsg_compat(struct msghdr *kmsg, int level, int type, int len, void *data)
230{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 struct compat_cmsghdr __user *cm = (struct compat_cmsghdr __user *) kmsg->msg_control;
232 struct compat_cmsghdr cmhdr;
Deepa Dinamani13c6ee22019-02-02 07:34:48 -0800233 struct old_timeval32 ctv;
234 struct old_timespec32 cts[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 int cmlen;
236
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700237 if (cm == NULL || kmsg->msg_controllen < sizeof(*cm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 kmsg->msg_flags |= MSG_CTRUNC;
239 return 0; /* XXX: return error? check spec. */
240 }
241
H. J. Luee4fa232012-02-19 17:50:46 -0800242 if (!COMPAT_USE_64BIT_TIME) {
Deepa Dinamani7f1bc6e2019-02-02 07:34:46 -0800243 if (level == SOL_SOCKET && type == SO_TIMESTAMP_OLD) {
Deepa Dinamani13c6ee22019-02-02 07:34:48 -0800244 struct __kernel_old_timeval *tv = (struct __kernel_old_timeval *)data;
H. J. Luee4fa232012-02-19 17:50:46 -0800245 ctv.tv_sec = tv->tv_sec;
246 ctv.tv_usec = tv->tv_usec;
247 data = &ctv;
248 len = sizeof(ctv);
Patrick Ohly20d49472009-02-12 05:03:38 +0000249 }
H. J. Luee4fa232012-02-19 17:50:46 -0800250 if (level == SOL_SOCKET &&
Deepa Dinamani7f1bc6e2019-02-02 07:34:46 -0800251 (type == SO_TIMESTAMPNS_OLD || type == SO_TIMESTAMPING_OLD)) {
252 int count = type == SO_TIMESTAMPNS_OLD ? 1 : 3;
H. J. Luee4fa232012-02-19 17:50:46 -0800253 int i;
Arnd Bergmanndf1b4ba2019-10-25 22:04:46 +0200254 struct __kernel_old_timespec *ts = data;
H. J. Luee4fa232012-02-19 17:50:46 -0800255 for (i = 0; i < count; i++) {
256 cts[i].tv_sec = ts[i].tv_sec;
257 cts[i].tv_nsec = ts[i].tv_nsec;
258 }
259 data = &cts;
260 len = sizeof(cts[0]) * count;
261 }
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +0900262 }
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 cmlen = CMSG_COMPAT_LEN(len);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700265 if (kmsg->msg_controllen < cmlen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 kmsg->msg_flags |= MSG_CTRUNC;
267 cmlen = kmsg->msg_controllen;
268 }
269 cmhdr.cmsg_level = level;
270 cmhdr.cmsg_type = type;
271 cmhdr.cmsg_len = cmlen;
272
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700273 if (copy_to_user(cm, &cmhdr, sizeof cmhdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return -EFAULT;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700275 if (copy_to_user(CMSG_COMPAT_DATA(cm), data, cmlen - sizeof(struct compat_cmsghdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return -EFAULT;
277 cmlen = CMSG_COMPAT_SPACE(len);
Wei Yongjun1ac70e72007-12-20 14:36:44 -0800278 if (kmsg->msg_controllen < cmlen)
279 cmlen = kmsg->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 kmsg->msg_control += cmlen;
281 kmsg->msg_controllen -= cmlen;
282 return 0;
283}
284
Kees Cookc0029de2020-06-09 16:11:29 -0700285static int scm_max_fds_compat(struct msghdr *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Kees Cookc0029de2020-06-09 16:11:29 -0700287 if (msg->msg_controllen <= sizeof(struct compat_cmsghdr))
288 return 0;
289 return (msg->msg_controllen - sizeof(struct compat_cmsghdr)) / sizeof(int);
290}
291
292void scm_detach_fds_compat(struct msghdr *msg, struct scm_cookie *scm)
293{
294 struct compat_cmsghdr __user *cm =
295 (struct compat_cmsghdr __user *)msg->msg_control;
296 unsigned int o_flags = (msg->msg_flags & MSG_CMSG_CLOEXEC) ? O_CLOEXEC : 0;
297 int fdmax = min_t(int, scm_max_fds_compat(msg), scm->fp->count);
Kees Cook16b89f62020-08-07 10:53:54 -0700298 int __user *cmsg_data = CMSG_COMPAT_DATA(cm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 int err = 0, i;
300
Kees Cookc0029de2020-06-09 16:11:29 -0700301 for (i = 0; i < fdmax; i++) {
Kees Cook66590612020-06-10 08:20:05 -0700302 err = receive_fd_user(scm->fp->fp[i], cmsg_data + i, o_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (err < 0)
304 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
306
307 if (i > 0) {
308 int cmlen = CMSG_COMPAT_LEN(i * sizeof(int));
Kees Cookc0029de2020-06-09 16:11:29 -0700309
Miklos Szeredieffee6a2006-10-09 21:42:14 -0700310 err = put_user(SOL_SOCKET, &cm->cmsg_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (!err)
312 err = put_user(SCM_RIGHTS, &cm->cmsg_type);
313 if (!err)
314 err = put_user(cmlen, &cm->cmsg_len);
315 if (!err) {
316 cmlen = CMSG_COMPAT_SPACE(i * sizeof(int));
Kees Cookc0029de2020-06-09 16:11:29 -0700317 if (msg->msg_controllen < cmlen)
318 cmlen = msg->msg_controllen;
319 msg->msg_control += cmlen;
320 msg->msg_controllen -= cmlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322 }
Kees Cookc0029de2020-06-09 16:11:29 -0700323
324 if (i < scm->fp->count || (scm->fp->count && fdmax <= 0))
325 msg->msg_flags |= MSG_CTRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 /*
Kees Cookc0029de2020-06-09 16:11:29 -0700328 * All of the files that fit in the message have had their usage counts
329 * incremented, so we just free the list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 */
331 __scm_destroy(scm);
332}
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334/* Argument list sizes for compat_sys_socketcall */
335#define AL(x) ((x) * sizeof(u32))
Anton Blanchard228e5482011-05-02 20:21:35 +0000336static unsigned char nas[21] = {
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700337 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
338 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
339 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
Anton Blanchard228e5482011-05-02 20:21:35 +0000340 AL(4), AL(5), AL(4)
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700341};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342#undef AL
343
Dominik Brodowski6df35462018-03-16 17:07:03 +0100344static inline long __compat_sys_sendmsg(int fd,
345 struct compat_msghdr __user *msg,
346 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Dominik Brodowskie1834a32018-03-13 20:35:57 +0100348 return __sys_sendmsg(fd, (struct user_msghdr __user *)msg,
349 flags | MSG_CMSG_COMPAT, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
Dominik Brodowski6df35462018-03-16 17:07:03 +0100352COMPAT_SYSCALL_DEFINE3(sendmsg, int, fd, struct compat_msghdr __user *, msg,
353 unsigned int, flags)
354{
355 return __compat_sys_sendmsg(fd, msg, flags);
356}
357
358static inline long __compat_sys_sendmmsg(int fd,
359 struct compat_mmsghdr __user *mmsg,
360 unsigned int vlen, unsigned int flags)
Anton Blanchard228e5482011-05-02 20:21:35 +0000361{
362 return __sys_sendmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
Dominik Brodowskie1834a32018-03-13 20:35:57 +0100363 flags | MSG_CMSG_COMPAT, false);
Anton Blanchard228e5482011-05-02 20:21:35 +0000364}
365
Dominik Brodowski6df35462018-03-16 17:07:03 +0100366COMPAT_SYSCALL_DEFINE4(sendmmsg, int, fd, struct compat_mmsghdr __user *, mmsg,
367 unsigned int, vlen, unsigned int, flags)
368{
369 return __compat_sys_sendmmsg(fd, mmsg, vlen, flags);
370}
371
372static inline long __compat_sys_recvmsg(int fd,
373 struct compat_msghdr __user *msg,
374 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Dominik Brodowskie1834a32018-03-13 20:35:57 +0100376 return __sys_recvmsg(fd, (struct user_msghdr __user *)msg,
377 flags | MSG_CMSG_COMPAT, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
Dominik Brodowski6df35462018-03-16 17:07:03 +0100380COMPAT_SYSCALL_DEFINE3(recvmsg, int, fd, struct compat_msghdr __user *, msg,
381 unsigned int, flags)
382{
383 return __compat_sys_recvmsg(fd, msg, flags);
384}
385
Dominik Brodowskifd4e82f2018-03-16 16:48:34 +0100386static inline long __compat_sys_recvfrom(int fd, void __user *buf,
387 compat_size_t len, unsigned int flags,
388 struct sockaddr __user *addr,
389 int __user *addrlen)
390{
391 return __sys_recvfrom(fd, buf, len, flags | MSG_CMSG_COMPAT, addr,
392 addrlen);
393}
394
Heiko Carstens3a49a0f2014-03-04 17:09:57 +0100395COMPAT_SYSCALL_DEFINE4(recv, int, fd, void __user *, buf, compat_size_t, len, unsigned int, flags)
Johannes Berg1dacc762009-07-01 11:26:02 +0000396{
Dominik Brodowskifd4e82f2018-03-16 16:48:34 +0100397 return __compat_sys_recvfrom(fd, buf, len, flags, NULL, NULL);
Johannes Berg1dacc762009-07-01 11:26:02 +0000398}
399
Heiko Carstens3a49a0f2014-03-04 17:09:57 +0100400COMPAT_SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, buf, compat_size_t, len,
401 unsigned int, flags, struct sockaddr __user *, addr,
402 int __user *, addrlen)
Johannes Berg1dacc762009-07-01 11:26:02 +0000403{
Dominik Brodowskifd4e82f2018-03-16 16:48:34 +0100404 return __compat_sys_recvfrom(fd, buf, len, flags, addr, addrlen);
Johannes Berg1dacc762009-07-01 11:26:02 +0000405}
406
Arnd Bergmanne11d4282018-04-18 13:43:52 +0200407COMPAT_SYSCALL_DEFINE5(recvmmsg_time64, int, fd, struct compat_mmsghdr __user *, mmsg,
408 unsigned int, vlen, unsigned int, flags,
409 struct __kernel_timespec __user *, timeout)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700410{
Arnd Bergmanne11d4282018-04-18 13:43:52 +0200411 return __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
412 flags | MSG_CMSG_COMPAT, timeout, NULL);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700413}
414
Arnd Bergmanne11d4282018-04-18 13:43:52 +0200415#ifdef CONFIG_COMPAT_32BIT_TIME
Arnd Bergmann8dabe722019-01-07 00:33:08 +0100416COMPAT_SYSCALL_DEFINE5(recvmmsg_time32, int, fd, struct compat_mmsghdr __user *, mmsg,
Dominik Brodowski157b3342018-03-16 17:10:50 +0100417 unsigned int, vlen, unsigned int, flags,
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +0200418 struct old_timespec32 __user *, timeout)
Dominik Brodowski157b3342018-03-16 17:10:50 +0100419{
Arnd Bergmanne11d4282018-04-18 13:43:52 +0200420 return __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
421 flags | MSG_CMSG_COMPAT, NULL, timeout);
Dominik Brodowski157b3342018-03-16 17:10:50 +0100422}
Arnd Bergmanne11d4282018-04-18 13:43:52 +0200423#endif
Dominik Brodowski157b3342018-03-16 17:10:50 +0100424
Heiko Carstens361d93c2014-03-03 16:21:27 +0100425COMPAT_SYSCALL_DEFINE2(socketcall, int, call, u32 __user *, args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Richard Guy Briggs62bc3062017-01-17 11:07:15 -0500427 u32 a[AUDITSC_ARGS];
428 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 u32 a0, a1;
Richard Guy Briggs62bc3062017-01-17 11:07:15 -0500430 int ret;
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +0900431
Anton Blanchard228e5482011-05-02 20:21:35 +0000432 if (call < SYS_SOCKET || call > SYS_SENDMMSG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return -EINVAL;
Richard Guy Briggs62bc3062017-01-17 11:07:15 -0500434 len = nas[call];
435 if (len > sizeof(a))
436 return -EINVAL;
437
438 if (copy_from_user(a, args, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return -EFAULT;
Richard Guy Briggs62bc3062017-01-17 11:07:15 -0500440
441 ret = audit_socketcall_compat(len / sizeof(a[0]), a);
442 if (ret)
443 return ret;
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 a0 = a[0];
446 a1 = a[1];
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +0900447
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700448 switch (call) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 case SYS_SOCKET:
Dominik Brodowski9d6a15c2018-03-13 19:29:43 +0100450 ret = __sys_socket(a0, a1, a[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 break;
452 case SYS_BIND:
Dominik Brodowskia87d35d2018-03-13 19:33:09 +0100453 ret = __sys_bind(a0, compat_ptr(a1), a[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 break;
455 case SYS_CONNECT:
Dominik Brodowski1387c2c2018-03-13 19:35:09 +0100456 ret = __sys_connect(a0, compat_ptr(a1), a[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 break;
458 case SYS_LISTEN:
Dominik Brodowski25e290e2018-03-13 19:36:54 +0100459 ret = __sys_listen(a0, a1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 break;
461 case SYS_ACCEPT:
Dominik Brodowski4541e802018-03-13 19:24:23 +0100462 ret = __sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 break;
464 case SYS_GETSOCKNAME:
Dominik Brodowski8882a102018-03-13 19:43:14 +0100465 ret = __sys_getsockname(a0, compat_ptr(a1), compat_ptr(a[2]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 break;
467 case SYS_GETPEERNAME:
Dominik Brodowskib21c8f82018-03-13 19:47:00 +0100468 ret = __sys_getpeername(a0, compat_ptr(a1), compat_ptr(a[2]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 break;
470 case SYS_SOCKETPAIR:
Dominik Brodowski6debc8d2018-03-13 19:49:23 +0100471 ret = __sys_socketpair(a0, a1, a[2], compat_ptr(a[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 break;
473 case SYS_SEND:
Dominik Brodowskif3bf8962018-03-13 19:52:00 +0100474 ret = __sys_sendto(a0, compat_ptr(a1), a[2], a[3], NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 break;
476 case SYS_SENDTO:
Dominik Brodowski211b6342018-03-13 19:18:52 +0100477 ret = __sys_sendto(a0, compat_ptr(a1), a[2], a[3],
478 compat_ptr(a[4]), a[5]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 break;
480 case SYS_RECV:
Dominik Brodowskifd4e82f2018-03-16 16:48:34 +0100481 ret = __compat_sys_recvfrom(a0, compat_ptr(a1), a[2], a[3],
482 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 break;
484 case SYS_RECVFROM:
Dominik Brodowskifd4e82f2018-03-16 16:48:34 +0100485 ret = __compat_sys_recvfrom(a0, compat_ptr(a1), a[2], a[3],
486 compat_ptr(a[4]),
487 compat_ptr(a[5]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 break;
489 case SYS_SHUTDOWN:
Dominik Brodowski005a1ae2018-03-13 20:07:05 +0100490 ret = __sys_shutdown(a0, a1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 break;
492 case SYS_SETSOCKOPT:
Christoph Hellwig55db9c02020-07-17 08:23:15 +0200493 ret = __sys_setsockopt(a0, a1, a[2], compat_ptr(a[3]), a[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 break;
495 case SYS_GETSOCKOPT:
Christoph Hellwig55db9c02020-07-17 08:23:15 +0200496 ret = __sys_getsockopt(a0, a1, a[2], compat_ptr(a[3]),
497 compat_ptr(a[4]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 break;
499 case SYS_SENDMSG:
Dominik Brodowski6df35462018-03-16 17:07:03 +0100500 ret = __compat_sys_sendmsg(a0, compat_ptr(a1), a[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 break;
Anton Blanchard228e5482011-05-02 20:21:35 +0000502 case SYS_SENDMMSG:
Dominik Brodowski6df35462018-03-16 17:07:03 +0100503 ret = __compat_sys_sendmmsg(a0, compat_ptr(a1), a[2], a[3]);
Anton Blanchard228e5482011-05-02 20:21:35 +0000504 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 case SYS_RECVMSG:
Dominik Brodowski6df35462018-03-16 17:07:03 +0100506 ret = __compat_sys_recvmsg(a0, compat_ptr(a1), a[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700508 case SYS_RECVMMSG:
Arnd Bergmanne11d4282018-04-18 13:43:52 +0200509 ret = __sys_recvmmsg(a0, compat_ptr(a1), a[2],
510 a[3] | MSG_CMSG_COMPAT, NULL,
511 compat_ptr(a[4]));
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700512 break;
Ulrich Drepperde11def2008-11-19 15:36:14 -0800513 case SYS_ACCEPT4:
Dominik Brodowski4541e802018-03-13 19:24:23 +0100514 ret = __sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), a[3]);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -0700515 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 default:
517 ret = -EINVAL;
518 break;
519 }
520 return ret;
521}