blob: e74cd3dae8b054433bd83e87475c89239653bab4 [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 <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>
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
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -070082 kmsg->msg_iocb = NULL;
Jens Axboe0a384ab2020-02-27 08:11:20 -070083 *ptr = msg.msg_iov;
84 *len = msg.msg_iovlen;
85 return 0;
86}
tadeusz.struk@intel.com0345f932015-03-19 12:31:25 -070087
Jens Axboe0a384ab2020-02-27 08:11:20 -070088int get_compat_msghdr(struct msghdr *kmsg,
89 struct compat_msghdr __user *umsg,
90 struct sockaddr __user **save_addr,
91 struct iovec **iov)
92{
93 compat_uptr_t ptr;
94 compat_size_t len;
95 ssize_t err;
96
97 err = __get_compat_msghdr(kmsg, umsg, save_addr, &ptr, &len);
98 if (err)
99 return err;
100
101 err = compat_import_iovec(save_addr ? READ : WRITE, compat_ptr(ptr),
102 len, UIO_FASTIOV, iov, &kmsg->msg_iter);
Jens Axboe87e5e6d2019-05-14 16:02:22 -0600103 return err < 0 ? err : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
106/* Bleech... */
107#define CMSG_COMPAT_ALIGN(len) ALIGN((len), sizeof(s32))
108
109#define CMSG_COMPAT_DATA(cmsg) \
yuan linyu1ff8ceb2017-01-03 20:42:17 +0800110 ((void __user *)((char __user *)(cmsg) + sizeof(struct compat_cmsghdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#define CMSG_COMPAT_SPACE(len) \
yuan linyu1ff8ceb2017-01-03 20:42:17 +0800112 (sizeof(struct compat_cmsghdr) + CMSG_COMPAT_ALIGN(len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#define CMSG_COMPAT_LEN(len) \
yuan linyu1ff8ceb2017-01-03 20:42:17 +0800114 (sizeof(struct compat_cmsghdr) + (len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116#define CMSG_COMPAT_FIRSTHDR(msg) \
117 (((msg)->msg_controllen) >= sizeof(struct compat_cmsghdr) ? \
118 (struct compat_cmsghdr __user *)((msg)->msg_control) : \
119 (struct compat_cmsghdr __user *)NULL)
120
121#define CMSG_COMPAT_OK(ucmlen, ucmsg, mhdr) \
122 ((ucmlen) >= sizeof(struct compat_cmsghdr) && \
123 (ucmlen) <= (unsigned long) \
124 ((mhdr)->msg_controllen - \
Christoph Hellwig1f466e12020-05-11 13:59:13 +0200125 ((char __user *)(ucmsg) - (char __user *)(mhdr)->msg_control_user)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127static inline struct compat_cmsghdr __user *cmsg_compat_nxthdr(struct msghdr *msg,
128 struct compat_cmsghdr __user *cmsg, int cmsg_len)
129{
130 char __user *ptr = (char __user *)cmsg + CMSG_COMPAT_ALIGN(cmsg_len);
131 if ((unsigned long)(ptr + 1 - (char __user *)msg->msg_control) >
132 msg->msg_controllen)
133 return NULL;
134 return (struct compat_cmsghdr __user *)ptr;
135}
136
137/* There is a lot of hair here because the alignment rules (and
138 * thus placement) of cmsg headers and length are different for
139 * 32-bit apps. -DaveM
140 */
Al Viro8920e8f2005-09-07 18:28:51 -0700141int cmsghdr_from_user_compat_to_kern(struct msghdr *kmsg, struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 unsigned char *stackbuf, int stackbuf_size)
143{
144 struct compat_cmsghdr __user *ucmsg;
145 struct cmsghdr *kcmsg, *kcmsg_base;
146 compat_size_t ucmlen;
147 __kernel_size_t kcmlen, tmp;
Al Viro8920e8f2005-09-07 18:28:51 -0700148 int err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
David S. Millerac4340f2017-01-04 13:24:19 -0500150 BUILD_BUG_ON(sizeof(struct compat_cmsghdr) !=
151 CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)));
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 kcmlen = 0;
154 kcmsg_base = kcmsg = (struct cmsghdr *)stackbuf;
155 ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700156 while (ucmsg != NULL) {
157 if (get_user(ucmlen, &ucmsg->cmsg_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return -EFAULT;
159
160 /* Catch bogons. */
161 if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg))
162 return -EINVAL;
163
yuan linyu1ff8ceb2017-01-03 20:42:17 +0800164 tmp = ((ucmlen - sizeof(*ucmsg)) + sizeof(struct cmsghdr));
Al Viro8920e8f2005-09-07 18:28:51 -0700165 tmp = CMSG_ALIGN(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 kcmlen += tmp;
167 ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen);
168 }
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700169 if (kcmlen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return -EINVAL;
171
172 /* The kcmlen holds the 64-bit version of the control length.
173 * It may not be modified as we do not stick it into the kmsg
174 * until we have successfully copied over all of the data
175 * from the user.
176 */
Al Viro8920e8f2005-09-07 18:28:51 -0700177 if (kcmlen > stackbuf_size)
178 kcmsg_base = kcmsg = sock_kmalloc(sk, kcmlen, GFP_KERNEL);
179 if (kcmsg == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return -ENOBUFS;
181
182 /* Now copy them over neatly. */
183 memset(kcmsg, 0, kcmlen);
184 ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700185 while (ucmsg != NULL) {
Al Viro547ce4c2020-05-31 02:06:55 +0100186 struct compat_cmsghdr cmsg;
187 if (copy_from_user(&cmsg, ucmsg, sizeof(cmsg)))
Al Viro8920e8f2005-09-07 18:28:51 -0700188 goto Efault;
Al Viro547ce4c2020-05-31 02:06:55 +0100189 if (!CMSG_COMPAT_OK(cmsg.cmsg_len, ucmsg, kmsg))
Al Viro8920e8f2005-09-07 18:28:51 -0700190 goto Einval;
Al Viro547ce4c2020-05-31 02:06:55 +0100191 tmp = ((cmsg.cmsg_len - sizeof(*ucmsg)) + sizeof(struct cmsghdr));
Al Viro8920e8f2005-09-07 18:28:51 -0700192 if ((char *)kcmsg_base + kcmlen - (char *)kcmsg < CMSG_ALIGN(tmp))
193 goto Einval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 kcmsg->cmsg_len = tmp;
Al Viro547ce4c2020-05-31 02:06:55 +0100195 kcmsg->cmsg_level = cmsg.cmsg_level;
196 kcmsg->cmsg_type = cmsg.cmsg_type;
Al Viro8920e8f2005-09-07 18:28:51 -0700197 tmp = CMSG_ALIGN(tmp);
Al Viro547ce4c2020-05-31 02:06:55 +0100198 if (copy_from_user(CMSG_DATA(kcmsg),
Al Viro8920e8f2005-09-07 18:28:51 -0700199 CMSG_COMPAT_DATA(ucmsg),
Al Viro547ce4c2020-05-31 02:06:55 +0100200 (cmsg.cmsg_len - sizeof(*ucmsg))))
Al Viro8920e8f2005-09-07 18:28:51 -0700201 goto Efault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 /* Advance. */
Al Viro8920e8f2005-09-07 18:28:51 -0700204 kcmsg = (struct cmsghdr *)((char *)kcmsg + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen);
206 }
207
Meng Xuc2a64bb2017-09-19 13:19:13 -0400208 /*
209 * check the length of messages copied in is the same as the
210 * what we get from the first loop
211 */
212 if ((char *)kcmsg - (char *)kcmsg_base != kcmlen)
213 goto Einval;
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /* Ok, looks like we made it. Hook it up and return success. */
216 kmsg->msg_control = kcmsg_base;
217 kmsg->msg_controllen = kcmlen;
218 return 0;
219
Al Viro8920e8f2005-09-07 18:28:51 -0700220Einval:
221 err = -EINVAL;
222Efault:
223 if (kcmsg_base != (struct cmsghdr *)stackbuf)
224 sock_kfree_s(sk, kcmsg_base, kcmlen);
225 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
228int put_cmsg_compat(struct msghdr *kmsg, int level, int type, int len, void *data)
229{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 struct compat_cmsghdr __user *cm = (struct compat_cmsghdr __user *) kmsg->msg_control;
231 struct compat_cmsghdr cmhdr;
Deepa Dinamani13c6ee22019-02-02 07:34:48 -0800232 struct old_timeval32 ctv;
233 struct old_timespec32 cts[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 int cmlen;
235
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700236 if (cm == NULL || kmsg->msg_controllen < sizeof(*cm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 kmsg->msg_flags |= MSG_CTRUNC;
238 return 0; /* XXX: return error? check spec. */
239 }
240
H. J. Luee4fa232012-02-19 17:50:46 -0800241 if (!COMPAT_USE_64BIT_TIME) {
Deepa Dinamani7f1bc6e2019-02-02 07:34:46 -0800242 if (level == SOL_SOCKET && type == SO_TIMESTAMP_OLD) {
Deepa Dinamani13c6ee22019-02-02 07:34:48 -0800243 struct __kernel_old_timeval *tv = (struct __kernel_old_timeval *)data;
H. J. Luee4fa232012-02-19 17:50:46 -0800244 ctv.tv_sec = tv->tv_sec;
245 ctv.tv_usec = tv->tv_usec;
246 data = &ctv;
247 len = sizeof(ctv);
Patrick Ohly20d49472009-02-12 05:03:38 +0000248 }
H. J. Luee4fa232012-02-19 17:50:46 -0800249 if (level == SOL_SOCKET &&
Deepa Dinamani7f1bc6e2019-02-02 07:34:46 -0800250 (type == SO_TIMESTAMPNS_OLD || type == SO_TIMESTAMPING_OLD)) {
251 int count = type == SO_TIMESTAMPNS_OLD ? 1 : 3;
H. J. Luee4fa232012-02-19 17:50:46 -0800252 int i;
Arnd Bergmanndf1b4ba2019-10-25 22:04:46 +0200253 struct __kernel_old_timespec *ts = data;
H. J. Luee4fa232012-02-19 17:50:46 -0800254 for (i = 0; i < count; i++) {
255 cts[i].tv_sec = ts[i].tv_sec;
256 cts[i].tv_nsec = ts[i].tv_nsec;
257 }
258 data = &cts;
259 len = sizeof(cts[0]) * count;
260 }
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +0900261 }
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 cmlen = CMSG_COMPAT_LEN(len);
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700264 if (kmsg->msg_controllen < cmlen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 kmsg->msg_flags |= MSG_CTRUNC;
266 cmlen = kmsg->msg_controllen;
267 }
268 cmhdr.cmsg_level = level;
269 cmhdr.cmsg_type = type;
270 cmhdr.cmsg_len = cmlen;
271
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700272 if (copy_to_user(cm, &cmhdr, sizeof cmhdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return -EFAULT;
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700274 if (copy_to_user(CMSG_COMPAT_DATA(cm), data, cmlen - sizeof(struct compat_cmsghdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return -EFAULT;
276 cmlen = CMSG_COMPAT_SPACE(len);
Wei Yongjun1ac70e72007-12-20 14:36:44 -0800277 if (kmsg->msg_controllen < cmlen)
278 cmlen = kmsg->msg_controllen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 kmsg->msg_control += cmlen;
280 kmsg->msg_controllen -= cmlen;
281 return 0;
282}
283
Kees Cookc0029de2020-06-09 16:11:29 -0700284static int scm_max_fds_compat(struct msghdr *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Kees Cookc0029de2020-06-09 16:11:29 -0700286 if (msg->msg_controllen <= sizeof(struct compat_cmsghdr))
287 return 0;
288 return (msg->msg_controllen - sizeof(struct compat_cmsghdr)) / sizeof(int);
289}
290
291void scm_detach_fds_compat(struct msghdr *msg, struct scm_cookie *scm)
292{
293 struct compat_cmsghdr __user *cm =
294 (struct compat_cmsghdr __user *)msg->msg_control;
295 unsigned int o_flags = (msg->msg_flags & MSG_CMSG_CLOEXEC) ? O_CLOEXEC : 0;
296 int fdmax = min_t(int, scm_max_fds_compat(msg), scm->fp->count);
297 int __user *cmsg_data = CMSG_USER_DATA(cm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 int err = 0, i;
299
Kees Cookc0029de2020-06-09 16:11:29 -0700300 for (i = 0; i < fdmax; i++) {
Kees Cook66590612020-06-10 08:20:05 -0700301 err = receive_fd_user(scm->fp->fp[i], cmsg_data + i, o_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (err)
303 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305
306 if (i > 0) {
307 int cmlen = CMSG_COMPAT_LEN(i * sizeof(int));
Kees Cookc0029de2020-06-09 16:11:29 -0700308
Miklos Szeredieffee6a2006-10-09 21:42:14 -0700309 err = put_user(SOL_SOCKET, &cm->cmsg_level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (!err)
311 err = put_user(SCM_RIGHTS, &cm->cmsg_type);
312 if (!err)
313 err = put_user(cmlen, &cm->cmsg_len);
314 if (!err) {
315 cmlen = CMSG_COMPAT_SPACE(i * sizeof(int));
Kees Cookc0029de2020-06-09 16:11:29 -0700316 if (msg->msg_controllen < cmlen)
317 cmlen = msg->msg_controllen;
318 msg->msg_control += cmlen;
319 msg->msg_controllen -= cmlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
321 }
Kees Cookc0029de2020-06-09 16:11:29 -0700322
323 if (i < scm->fp->count || (scm->fp->count && fdmax <= 0))
324 msg->msg_flags |= MSG_CTRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 /*
Kees Cookc0029de2020-06-09 16:11:29 -0700327 * All of the files that fit in the message have had their usage counts
328 * incremented, so we just free the list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 */
330 __scm_destroy(scm);
331}
332
Willem de Bruijn719c44d2016-06-07 12:06:34 -0400333/* allocate a 64-bit sock_fprog on the user stack for duration of syscall. */
334struct sock_fprog __user *get_compat_bpf_fprog(char __user *optval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 struct compat_sock_fprog __user *fprog32 = (struct compat_sock_fprog __user *)optval;
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +0900337 struct sock_fprog __user *kfprog = compat_alloc_user_space(sizeof(struct sock_fprog));
Al Virof8f8a722017-06-27 18:34:53 -0400338 struct compat_sock_fprog f32;
339 struct sock_fprog f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Al Virof8f8a722017-06-27 18:34:53 -0400341 if (copy_from_user(&f32, fprog32, sizeof(*fprog32)))
342 return NULL;
343 memset(&f, 0, sizeof(f));
344 f.len = f32.len;
345 f.filter = compat_ptr(f32.filter);
346 if (copy_to_user(kfprog, &f, sizeof(struct sock_fprog)))
Willem de Bruijn719c44d2016-06-07 12:06:34 -0400347 return NULL;
348
349 return kfprog;
350}
351EXPORT_SYMBOL_GPL(get_compat_bpf_fprog);
352
353static int do_set_attach_filter(struct socket *sock, int level, int optname,
354 char __user *optval, unsigned int optlen)
355{
356 struct sock_fprog __user *kfprog;
357
358 kfprog = get_compat_bpf_fprog(optval);
359 if (!kfprog)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return -EFAULT;
361
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800362 return sock_setsockopt(sock, level, optname, (char __user *)kfprog,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 sizeof(struct sock_fprog));
364}
365
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800366static int compat_sock_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700367 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800368{
Helge Deller19575982016-06-03 23:49:17 +0200369 if (optname == SO_ATTACH_FILTER ||
370 optname == SO_ATTACH_REUSEPORT_CBPF)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800371 return do_set_attach_filter(sock, level, optname,
372 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800373 return sock_setsockopt(sock, level, optname, optval, optlen);
374}
375
Dominik Brodowski73ee3ea2018-03-16 16:58:54 +0100376static int __compat_sys_setsockopt(int fd, int level, int optname,
377 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800379 int err;
Jann Horn52baf982019-02-20 22:34:54 +0100380 struct socket *sock;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800381
Jann Horn52baf982019-02-20 22:34:54 +0100382 if (optlen > INT_MAX)
383 return -EINVAL;
384
385 sock = sockfd_lookup(fd, &err);
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700386 if (sock) {
387 err = security_socket_setsockopt(sock, level, optname);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800388 if (err) {
389 sockfd_put(sock);
390 return err;
391 }
392
393 if (level == SOL_SOCKET)
394 err = compat_sock_setsockopt(sock, level,
395 optname, optval, optlen);
396 else if (sock->ops->compat_setsockopt)
397 err = sock->ops->compat_setsockopt(sock, level,
398 optname, optval, optlen);
399 else
400 err = sock->ops->setsockopt(sock, level,
401 optname, optval, optlen);
402 sockfd_put(sock);
403 }
404 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Dominik Brodowski73ee3ea2018-03-16 16:58:54 +0100407COMPAT_SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
408 char __user *, optval, unsigned int, optlen)
409{
410 return __compat_sys_setsockopt(fd, level, optname, optval, optlen);
411}
412
Dominik Brodowski8770cf42018-03-16 17:01:41 +0100413static int __compat_sys_getsockopt(int fd, int level, int optname,
414 char __user *optval,
415 int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800417 int err;
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700418 struct socket *sock = sockfd_lookup(fd, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700420 if (sock) {
421 err = security_socket_getsockopt(sock, level, optname);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800422 if (err) {
423 sockfd_put(sock);
424 return err;
425 }
426
427 if (level == SOL_SOCKET)
Arnd Bergmannfe0c72f2019-02-02 07:34:44 -0800428 err = sock_getsockopt(sock, level,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800429 optname, optval, optlen);
430 else if (sock->ops->compat_getsockopt)
431 err = sock->ops->compat_getsockopt(sock, level,
432 optname, optval, optlen);
433 else
434 err = sock->ops->getsockopt(sock, level,
435 optname, optval, optlen);
436 sockfd_put(sock);
437 }
438 return err;
439}
David L Stevensdae50292008-04-27 01:06:07 -0700440
Dominik Brodowski8770cf42018-03-16 17:01:41 +0100441COMPAT_SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
442 char __user *, optval, int __user *, optlen)
443{
444 return __compat_sys_getsockopt(fd, level, optname, optval, optlen);
445}
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447/* Argument list sizes for compat_sys_socketcall */
448#define AL(x) ((x) * sizeof(u32))
Anton Blanchard228e5482011-05-02 20:21:35 +0000449static unsigned char nas[21] = {
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700450 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
451 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
452 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
Anton Blanchard228e5482011-05-02 20:21:35 +0000453 AL(4), AL(5), AL(4)
Eric Dumazetc6d409c2010-06-03 20:03:40 -0700454};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455#undef AL
456
Dominik Brodowski6df35462018-03-16 17:07:03 +0100457static inline long __compat_sys_sendmsg(int fd,
458 struct compat_msghdr __user *msg,
459 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Dominik Brodowskie1834a32018-03-13 20:35:57 +0100461 return __sys_sendmsg(fd, (struct user_msghdr __user *)msg,
462 flags | MSG_CMSG_COMPAT, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
Dominik Brodowski6df35462018-03-16 17:07:03 +0100465COMPAT_SYSCALL_DEFINE3(sendmsg, int, fd, struct compat_msghdr __user *, msg,
466 unsigned int, flags)
467{
468 return __compat_sys_sendmsg(fd, msg, flags);
469}
470
471static inline long __compat_sys_sendmmsg(int fd,
472 struct compat_mmsghdr __user *mmsg,
473 unsigned int vlen, unsigned int flags)
Anton Blanchard228e5482011-05-02 20:21:35 +0000474{
475 return __sys_sendmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
Dominik Brodowskie1834a32018-03-13 20:35:57 +0100476 flags | MSG_CMSG_COMPAT, false);
Anton Blanchard228e5482011-05-02 20:21:35 +0000477}
478
Dominik Brodowski6df35462018-03-16 17:07:03 +0100479COMPAT_SYSCALL_DEFINE4(sendmmsg, int, fd, struct compat_mmsghdr __user *, mmsg,
480 unsigned int, vlen, unsigned int, flags)
481{
482 return __compat_sys_sendmmsg(fd, mmsg, vlen, flags);
483}
484
485static inline long __compat_sys_recvmsg(int fd,
486 struct compat_msghdr __user *msg,
487 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Dominik Brodowskie1834a32018-03-13 20:35:57 +0100489 return __sys_recvmsg(fd, (struct user_msghdr __user *)msg,
490 flags | MSG_CMSG_COMPAT, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
Dominik Brodowski6df35462018-03-16 17:07:03 +0100493COMPAT_SYSCALL_DEFINE3(recvmsg, int, fd, struct compat_msghdr __user *, msg,
494 unsigned int, flags)
495{
496 return __compat_sys_recvmsg(fd, msg, flags);
497}
498
Dominik Brodowskifd4e82f2018-03-16 16:48:34 +0100499static inline long __compat_sys_recvfrom(int fd, void __user *buf,
500 compat_size_t len, unsigned int flags,
501 struct sockaddr __user *addr,
502 int __user *addrlen)
503{
504 return __sys_recvfrom(fd, buf, len, flags | MSG_CMSG_COMPAT, addr,
505 addrlen);
506}
507
Heiko Carstens3a49a0f2014-03-04 17:09:57 +0100508COMPAT_SYSCALL_DEFINE4(recv, int, fd, void __user *, buf, compat_size_t, len, unsigned int, flags)
Johannes Berg1dacc762009-07-01 11:26:02 +0000509{
Dominik Brodowskifd4e82f2018-03-16 16:48:34 +0100510 return __compat_sys_recvfrom(fd, buf, len, flags, NULL, NULL);
Johannes Berg1dacc762009-07-01 11:26:02 +0000511}
512
Heiko Carstens3a49a0f2014-03-04 17:09:57 +0100513COMPAT_SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, buf, compat_size_t, len,
514 unsigned int, flags, struct sockaddr __user *, addr,
515 int __user *, addrlen)
Johannes Berg1dacc762009-07-01 11:26:02 +0000516{
Dominik Brodowskifd4e82f2018-03-16 16:48:34 +0100517 return __compat_sys_recvfrom(fd, buf, len, flags, addr, addrlen);
Johannes Berg1dacc762009-07-01 11:26:02 +0000518}
519
Arnd Bergmanne11d4282018-04-18 13:43:52 +0200520COMPAT_SYSCALL_DEFINE5(recvmmsg_time64, int, fd, struct compat_mmsghdr __user *, mmsg,
521 unsigned int, vlen, unsigned int, flags,
522 struct __kernel_timespec __user *, timeout)
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700523{
Arnd Bergmanne11d4282018-04-18 13:43:52 +0200524 return __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
525 flags | MSG_CMSG_COMPAT, timeout, NULL);
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700526}
527
Arnd Bergmanne11d4282018-04-18 13:43:52 +0200528#ifdef CONFIG_COMPAT_32BIT_TIME
Arnd Bergmann8dabe722019-01-07 00:33:08 +0100529COMPAT_SYSCALL_DEFINE5(recvmmsg_time32, int, fd, struct compat_mmsghdr __user *, mmsg,
Dominik Brodowski157b3342018-03-16 17:10:50 +0100530 unsigned int, vlen, unsigned int, flags,
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +0200531 struct old_timespec32 __user *, timeout)
Dominik Brodowski157b3342018-03-16 17:10:50 +0100532{
Arnd Bergmanne11d4282018-04-18 13:43:52 +0200533 return __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen,
534 flags | MSG_CMSG_COMPAT, NULL, timeout);
Dominik Brodowski157b3342018-03-16 17:10:50 +0100535}
Arnd Bergmanne11d4282018-04-18 13:43:52 +0200536#endif
Dominik Brodowski157b3342018-03-16 17:10:50 +0100537
Heiko Carstens361d93c2014-03-03 16:21:27 +0100538COMPAT_SYSCALL_DEFINE2(socketcall, int, call, u32 __user *, args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Richard Guy Briggs62bc3062017-01-17 11:07:15 -0500540 u32 a[AUDITSC_ARGS];
541 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 u32 a0, a1;
Richard Guy Briggs62bc3062017-01-17 11:07:15 -0500543 int ret;
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +0900544
Anton Blanchard228e5482011-05-02 20:21:35 +0000545 if (call < SYS_SOCKET || call > SYS_SENDMMSG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return -EINVAL;
Richard Guy Briggs62bc3062017-01-17 11:07:15 -0500547 len = nas[call];
548 if (len > sizeof(a))
549 return -EINVAL;
550
551 if (copy_from_user(a, args, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return -EFAULT;
Richard Guy Briggs62bc3062017-01-17 11:07:15 -0500553
554 ret = audit_socketcall_compat(len / sizeof(a[0]), a);
555 if (ret)
556 return ret;
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 a0 = a[0];
559 a1 = a[1];
YOSHIFUJI Hideaki4768fbc2007-02-09 23:25:31 +0900560
Stephen Hemmingere71a4782007-04-10 20:10:33 -0700561 switch (call) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 case SYS_SOCKET:
Dominik Brodowski9d6a15c2018-03-13 19:29:43 +0100563 ret = __sys_socket(a0, a1, a[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 break;
565 case SYS_BIND:
Dominik Brodowskia87d35d2018-03-13 19:33:09 +0100566 ret = __sys_bind(a0, compat_ptr(a1), a[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 break;
568 case SYS_CONNECT:
Dominik Brodowski1387c2c2018-03-13 19:35:09 +0100569 ret = __sys_connect(a0, compat_ptr(a1), a[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 break;
571 case SYS_LISTEN:
Dominik Brodowski25e290e2018-03-13 19:36:54 +0100572 ret = __sys_listen(a0, a1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 break;
574 case SYS_ACCEPT:
Dominik Brodowski4541e802018-03-13 19:24:23 +0100575 ret = __sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 break;
577 case SYS_GETSOCKNAME:
Dominik Brodowski8882a102018-03-13 19:43:14 +0100578 ret = __sys_getsockname(a0, compat_ptr(a1), compat_ptr(a[2]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 break;
580 case SYS_GETPEERNAME:
Dominik Brodowskib21c8f82018-03-13 19:47:00 +0100581 ret = __sys_getpeername(a0, compat_ptr(a1), compat_ptr(a[2]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 break;
583 case SYS_SOCKETPAIR:
Dominik Brodowski6debc8d2018-03-13 19:49:23 +0100584 ret = __sys_socketpair(a0, a1, a[2], compat_ptr(a[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 break;
586 case SYS_SEND:
Dominik Brodowskif3bf8962018-03-13 19:52:00 +0100587 ret = __sys_sendto(a0, compat_ptr(a1), a[2], a[3], NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 break;
589 case SYS_SENDTO:
Dominik Brodowski211b6342018-03-13 19:18:52 +0100590 ret = __sys_sendto(a0, compat_ptr(a1), a[2], a[3],
591 compat_ptr(a[4]), a[5]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 break;
593 case SYS_RECV:
Dominik Brodowskifd4e82f2018-03-16 16:48:34 +0100594 ret = __compat_sys_recvfrom(a0, compat_ptr(a1), a[2], a[3],
595 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 break;
597 case SYS_RECVFROM:
Dominik Brodowskifd4e82f2018-03-16 16:48:34 +0100598 ret = __compat_sys_recvfrom(a0, compat_ptr(a1), a[2], a[3],
599 compat_ptr(a[4]),
600 compat_ptr(a[5]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 break;
602 case SYS_SHUTDOWN:
Dominik Brodowski005a1ae2018-03-13 20:07:05 +0100603 ret = __sys_shutdown(a0, a1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 break;
605 case SYS_SETSOCKOPT:
Dominik Brodowski73ee3ea2018-03-16 16:58:54 +0100606 ret = __compat_sys_setsockopt(a0, a1, a[2],
607 compat_ptr(a[3]), a[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 break;
609 case SYS_GETSOCKOPT:
Dominik Brodowski8770cf42018-03-16 17:01:41 +0100610 ret = __compat_sys_getsockopt(a0, a1, a[2],
611 compat_ptr(a[3]),
612 compat_ptr(a[4]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 break;
614 case SYS_SENDMSG:
Dominik Brodowski6df35462018-03-16 17:07:03 +0100615 ret = __compat_sys_sendmsg(a0, compat_ptr(a1), a[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 break;
Anton Blanchard228e5482011-05-02 20:21:35 +0000617 case SYS_SENDMMSG:
Dominik Brodowski6df35462018-03-16 17:07:03 +0100618 ret = __compat_sys_sendmmsg(a0, compat_ptr(a1), a[2], a[3]);
Anton Blanchard228e5482011-05-02 20:21:35 +0000619 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 case SYS_RECVMSG:
Dominik Brodowski6df35462018-03-16 17:07:03 +0100621 ret = __compat_sys_recvmsg(a0, compat_ptr(a1), a[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 break;
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700623 case SYS_RECVMMSG:
Arnd Bergmanne11d4282018-04-18 13:43:52 +0200624 ret = __sys_recvmmsg(a0, compat_ptr(a1), a[2],
625 a[3] | MSG_CMSG_COMPAT, NULL,
626 compat_ptr(a[4]));
Arnaldo Carvalho de Meloa2e27252009-10-12 23:40:10 -0700627 break;
Ulrich Drepperde11def2008-11-19 15:36:14 -0800628 case SYS_ACCEPT4:
Dominik Brodowski4541e802018-03-13 19:24:23 +0100629 ret = __sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), a[3]);
Ulrich Drepperaaca0bd2008-07-23 21:29:20 -0700630 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 default:
632 ret = -EINVAL;
633 break;
634 }
635 return ret;
636}