Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
YOSHIFUJI Hideaki | 4768fbc | 2007-02-09 23:25:31 +0900 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * 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 Hideaki | 4768fbc | 2007-02-09 23:25:31 +0900 | [diff] [blame] | 12 | * Copyright (C) 2000,2001 Andi Kleen, SuSE Labs |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/security.h> |
Richard Guy Briggs | 62bc306 | 2017-01-17 11:07:15 -0500 | [diff] [blame] | 26 | #include <linux/audit.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 27 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | #include <net/scm.h> |
| 30 | #include <net/sock.h> |
David L Stevens | dae5029 | 2008-04-27 01:06:07 -0700 | [diff] [blame] | 31 | #include <net/ip.h> |
| 32 | #include <net/ipv6.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 33 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <net/compat.h> |
| 35 | |
Jens Axboe | 0a384ab | 2020-02-27 08:11:20 -0700 | [diff] [blame] | 36 | int __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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | { |
Al Viro | 5da028a | 2017-06-27 18:24:21 -0400 | [diff] [blame] | 41 | struct compat_msghdr msg; |
Al Viro | 08adb7d | 2014-11-10 20:23:13 -0500 | [diff] [blame] | 42 | ssize_t err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Al Viro | 5da028a | 2017-06-27 18:24:21 -0400 | [diff] [blame] | 44 | if (copy_from_user(&msg, umsg, sizeof(*umsg))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | return -EFAULT; |
Catalin Marinas | 91edd09 | 2015-03-20 16:48:13 +0000 | [diff] [blame] | 46 | |
Al Viro | 5da028a | 2017-06-27 18:24:21 -0400 | [diff] [blame] | 47 | kmsg->msg_flags = msg.msg_flags; |
| 48 | kmsg->msg_namelen = msg.msg_namelen; |
| 49 | |
| 50 | if (!msg.msg_name) |
Catalin Marinas | 91edd09 | 2015-03-20 16:48:13 +0000 | [diff] [blame] | 51 | kmsg->msg_namelen = 0; |
| 52 | |
| 53 | if (kmsg->msg_namelen < 0) |
| 54 | return -EINVAL; |
| 55 | |
Dan Carpenter | 1661bf3 | 2013-10-03 00:27:20 +0300 | [diff] [blame] | 56 | if (kmsg->msg_namelen > sizeof(struct sockaddr_storage)) |
Dan Carpenter | db31c55 | 2013-11-27 15:40:21 +0300 | [diff] [blame] | 57 | kmsg->msg_namelen = sizeof(struct sockaddr_storage); |
Al Viro | 5da028a | 2017-06-27 18:24:21 -0400 | [diff] [blame] | 58 | |
Christoph Hellwig | 1f466e1 | 2020-05-11 13:59:13 +0200 | [diff] [blame] | 59 | kmsg->msg_control_is_user = true; |
| 60 | kmsg->msg_control_user = compat_ptr(msg.msg_control); |
Al Viro | 5da028a | 2017-06-27 18:24:21 -0400 | [diff] [blame] | 61 | kmsg->msg_controllen = msg.msg_controllen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Al Viro | 08adb7d | 2014-11-10 20:23:13 -0500 | [diff] [blame] | 63 | if (save_addr) |
Al Viro | 5da028a | 2017-06-27 18:24:21 -0400 | [diff] [blame] | 64 | *save_addr = compat_ptr(msg.msg_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
Al Viro | 5da028a | 2017-06-27 18:24:21 -0400 | [diff] [blame] | 66 | if (msg.msg_name && kmsg->msg_namelen) { |
Al Viro | 08adb7d | 2014-11-10 20:23:13 -0500 | [diff] [blame] | 67 | if (!save_addr) { |
Al Viro | 5da028a | 2017-06-27 18:24:21 -0400 | [diff] [blame] | 68 | err = move_addr_to_kernel(compat_ptr(msg.msg_name), |
Al Viro | 08adb7d | 2014-11-10 20:23:13 -0500 | [diff] [blame] | 69 | kmsg->msg_namelen, |
| 70 | kmsg->msg_name); |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 71 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | return err; |
| 73 | } |
Andrey Ryabinin | 40eea80 | 2014-07-26 21:26:58 +0400 | [diff] [blame] | 74 | } else { |
Al Viro | 08adb7d | 2014-11-10 20:23:13 -0500 | [diff] [blame] | 75 | kmsg->msg_name = NULL; |
| 76 | kmsg->msg_namelen = 0; |
Andrey Ryabinin | 40eea80 | 2014-07-26 21:26:58 +0400 | [diff] [blame] | 77 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Al Viro | 5da028a | 2017-06-27 18:24:21 -0400 | [diff] [blame] | 79 | if (msg.msg_iovlen > UIO_MAXIOV) |
Al Viro | 0844932 | 2014-11-09 22:33:45 -0500 | [diff] [blame] | 80 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
tadeusz.struk@intel.com | 0345f93 | 2015-03-19 12:31:25 -0700 | [diff] [blame] | 82 | kmsg->msg_iocb = NULL; |
Jens Axboe | 0a384ab | 2020-02-27 08:11:20 -0700 | [diff] [blame] | 83 | *ptr = msg.msg_iov; |
| 84 | *len = msg.msg_iovlen; |
| 85 | return 0; |
| 86 | } |
tadeusz.struk@intel.com | 0345f93 | 2015-03-19 12:31:25 -0700 | [diff] [blame] | 87 | |
Jens Axboe | 0a384ab | 2020-02-27 08:11:20 -0700 | [diff] [blame] | 88 | int 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 Axboe | 87e5e6d | 2019-05-14 16:02:22 -0600 | [diff] [blame] | 103 | return err < 0 ? err : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /* Bleech... */ |
| 107 | #define CMSG_COMPAT_ALIGN(len) ALIGN((len), sizeof(s32)) |
| 108 | |
| 109 | #define CMSG_COMPAT_DATA(cmsg) \ |
yuan linyu | 1ff8ceb | 2017-01-03 20:42:17 +0800 | [diff] [blame] | 110 | ((void __user *)((char __user *)(cmsg) + sizeof(struct compat_cmsghdr))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | #define CMSG_COMPAT_SPACE(len) \ |
yuan linyu | 1ff8ceb | 2017-01-03 20:42:17 +0800 | [diff] [blame] | 112 | (sizeof(struct compat_cmsghdr) + CMSG_COMPAT_ALIGN(len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | #define CMSG_COMPAT_LEN(len) \ |
yuan linyu | 1ff8ceb | 2017-01-03 20:42:17 +0800 | [diff] [blame] | 114 | (sizeof(struct compat_cmsghdr) + (len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 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 Hellwig | 1f466e1 | 2020-05-11 13:59:13 +0200 | [diff] [blame] | 125 | ((char __user *)(ucmsg) - (char __user *)(mhdr)->msg_control_user))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
| 127 | static 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 Viro | 8920e8f | 2005-09-07 18:28:51 -0700 | [diff] [blame] | 141 | int cmsghdr_from_user_compat_to_kern(struct msghdr *kmsg, struct sock *sk, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | 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 Viro | 8920e8f | 2005-09-07 18:28:51 -0700 | [diff] [blame] | 148 | int err = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
David S. Miller | ac4340f | 2017-01-04 13:24:19 -0500 | [diff] [blame] | 150 | BUILD_BUG_ON(sizeof(struct compat_cmsghdr) != |
| 151 | CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr))); |
| 152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | kcmlen = 0; |
| 154 | kcmsg_base = kcmsg = (struct cmsghdr *)stackbuf; |
| 155 | ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg); |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 156 | while (ucmsg != NULL) { |
| 157 | if (get_user(ucmlen, &ucmsg->cmsg_len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | return -EFAULT; |
| 159 | |
| 160 | /* Catch bogons. */ |
| 161 | if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg)) |
| 162 | return -EINVAL; |
| 163 | |
yuan linyu | 1ff8ceb | 2017-01-03 20:42:17 +0800 | [diff] [blame] | 164 | tmp = ((ucmlen - sizeof(*ucmsg)) + sizeof(struct cmsghdr)); |
Al Viro | 8920e8f | 2005-09-07 18:28:51 -0700 | [diff] [blame] | 165 | tmp = CMSG_ALIGN(tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | kcmlen += tmp; |
| 167 | ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen); |
| 168 | } |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 169 | if (kcmlen == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | 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 Viro | 8920e8f | 2005-09-07 18:28:51 -0700 | [diff] [blame] | 177 | if (kcmlen > stackbuf_size) |
| 178 | kcmsg_base = kcmsg = sock_kmalloc(sk, kcmlen, GFP_KERNEL); |
| 179 | if (kcmsg == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | return -ENOBUFS; |
| 181 | |
| 182 | /* Now copy them over neatly. */ |
| 183 | memset(kcmsg, 0, kcmlen); |
| 184 | ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg); |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 185 | while (ucmsg != NULL) { |
Al Viro | 547ce4c | 2020-05-31 02:06:55 +0100 | [diff] [blame] | 186 | struct compat_cmsghdr cmsg; |
| 187 | if (copy_from_user(&cmsg, ucmsg, sizeof(cmsg))) |
Al Viro | 8920e8f | 2005-09-07 18:28:51 -0700 | [diff] [blame] | 188 | goto Efault; |
Al Viro | 547ce4c | 2020-05-31 02:06:55 +0100 | [diff] [blame] | 189 | if (!CMSG_COMPAT_OK(cmsg.cmsg_len, ucmsg, kmsg)) |
Al Viro | 8920e8f | 2005-09-07 18:28:51 -0700 | [diff] [blame] | 190 | goto Einval; |
Al Viro | 547ce4c | 2020-05-31 02:06:55 +0100 | [diff] [blame] | 191 | tmp = ((cmsg.cmsg_len - sizeof(*ucmsg)) + sizeof(struct cmsghdr)); |
Al Viro | 8920e8f | 2005-09-07 18:28:51 -0700 | [diff] [blame] | 192 | if ((char *)kcmsg_base + kcmlen - (char *)kcmsg < CMSG_ALIGN(tmp)) |
| 193 | goto Einval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | kcmsg->cmsg_len = tmp; |
Al Viro | 547ce4c | 2020-05-31 02:06:55 +0100 | [diff] [blame] | 195 | kcmsg->cmsg_level = cmsg.cmsg_level; |
| 196 | kcmsg->cmsg_type = cmsg.cmsg_type; |
Al Viro | 8920e8f | 2005-09-07 18:28:51 -0700 | [diff] [blame] | 197 | tmp = CMSG_ALIGN(tmp); |
Al Viro | 547ce4c | 2020-05-31 02:06:55 +0100 | [diff] [blame] | 198 | if (copy_from_user(CMSG_DATA(kcmsg), |
Al Viro | 8920e8f | 2005-09-07 18:28:51 -0700 | [diff] [blame] | 199 | CMSG_COMPAT_DATA(ucmsg), |
Al Viro | 547ce4c | 2020-05-31 02:06:55 +0100 | [diff] [blame] | 200 | (cmsg.cmsg_len - sizeof(*ucmsg)))) |
Al Viro | 8920e8f | 2005-09-07 18:28:51 -0700 | [diff] [blame] | 201 | goto Efault; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
| 203 | /* Advance. */ |
Al Viro | 8920e8f | 2005-09-07 18:28:51 -0700 | [diff] [blame] | 204 | kcmsg = (struct cmsghdr *)((char *)kcmsg + tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen); |
| 206 | } |
| 207 | |
Meng Xu | c2a64bb | 2017-09-19 13:19:13 -0400 | [diff] [blame] | 208 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | /* 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 Viro | 8920e8f | 2005-09-07 18:28:51 -0700 | [diff] [blame] | 220 | Einval: |
| 221 | err = -EINVAL; |
| 222 | Efault: |
| 223 | if (kcmsg_base != (struct cmsghdr *)stackbuf) |
| 224 | sock_kfree_s(sk, kcmsg_base, kcmlen); |
| 225 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | int put_cmsg_compat(struct msghdr *kmsg, int level, int type, int len, void *data) |
| 229 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | struct compat_cmsghdr __user *cm = (struct compat_cmsghdr __user *) kmsg->msg_control; |
| 231 | struct compat_cmsghdr cmhdr; |
Deepa Dinamani | 13c6ee2 | 2019-02-02 07:34:48 -0800 | [diff] [blame] | 232 | struct old_timeval32 ctv; |
| 233 | struct old_timespec32 cts[3]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | int cmlen; |
| 235 | |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 236 | if (cm == NULL || kmsg->msg_controllen < sizeof(*cm)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | kmsg->msg_flags |= MSG_CTRUNC; |
| 238 | return 0; /* XXX: return error? check spec. */ |
| 239 | } |
| 240 | |
H. J. Lu | ee4fa23 | 2012-02-19 17:50:46 -0800 | [diff] [blame] | 241 | if (!COMPAT_USE_64BIT_TIME) { |
Deepa Dinamani | 7f1bc6e | 2019-02-02 07:34:46 -0800 | [diff] [blame] | 242 | if (level == SOL_SOCKET && type == SO_TIMESTAMP_OLD) { |
Deepa Dinamani | 13c6ee2 | 2019-02-02 07:34:48 -0800 | [diff] [blame] | 243 | struct __kernel_old_timeval *tv = (struct __kernel_old_timeval *)data; |
H. J. Lu | ee4fa23 | 2012-02-19 17:50:46 -0800 | [diff] [blame] | 244 | ctv.tv_sec = tv->tv_sec; |
| 245 | ctv.tv_usec = tv->tv_usec; |
| 246 | data = &ctv; |
| 247 | len = sizeof(ctv); |
Patrick Ohly | 20d4947 | 2009-02-12 05:03:38 +0000 | [diff] [blame] | 248 | } |
H. J. Lu | ee4fa23 | 2012-02-19 17:50:46 -0800 | [diff] [blame] | 249 | if (level == SOL_SOCKET && |
Deepa Dinamani | 7f1bc6e | 2019-02-02 07:34:46 -0800 | [diff] [blame] | 250 | (type == SO_TIMESTAMPNS_OLD || type == SO_TIMESTAMPING_OLD)) { |
| 251 | int count = type == SO_TIMESTAMPNS_OLD ? 1 : 3; |
H. J. Lu | ee4fa23 | 2012-02-19 17:50:46 -0800 | [diff] [blame] | 252 | int i; |
Arnd Bergmann | df1b4ba | 2019-10-25 22:04:46 +0200 | [diff] [blame] | 253 | struct __kernel_old_timespec *ts = data; |
H. J. Lu | ee4fa23 | 2012-02-19 17:50:46 -0800 | [diff] [blame] | 254 | 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 Hideaki | 4768fbc | 2007-02-09 23:25:31 +0900 | [diff] [blame] | 261 | } |
| 262 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | cmlen = CMSG_COMPAT_LEN(len); |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 264 | if (kmsg->msg_controllen < cmlen) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | 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 Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 272 | if (copy_to_user(cm, &cmhdr, sizeof cmhdr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | return -EFAULT; |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 274 | if (copy_to_user(CMSG_COMPAT_DATA(cm), data, cmlen - sizeof(struct compat_cmsghdr))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | return -EFAULT; |
| 276 | cmlen = CMSG_COMPAT_SPACE(len); |
Wei Yongjun | 1ac70e7 | 2007-12-20 14:36:44 -0800 | [diff] [blame] | 277 | if (kmsg->msg_controllen < cmlen) |
| 278 | cmlen = kmsg->msg_controllen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | kmsg->msg_control += cmlen; |
| 280 | kmsg->msg_controllen -= cmlen; |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | void scm_detach_fds_compat(struct msghdr *kmsg, struct scm_cookie *scm) |
| 285 | { |
| 286 | struct compat_cmsghdr __user *cm = (struct compat_cmsghdr __user *) kmsg->msg_control; |
| 287 | int fdmax = (kmsg->msg_controllen - sizeof(struct compat_cmsghdr)) / sizeof(int); |
| 288 | int fdnum = scm->fp->count; |
| 289 | struct file **fp = scm->fp->fp; |
| 290 | int __user *cmfptr; |
| 291 | int err = 0, i; |
| 292 | |
| 293 | if (fdnum < fdmax) |
| 294 | fdmax = fdnum; |
| 295 | |
| 296 | for (i = 0, cmfptr = (int __user *) CMSG_COMPAT_DATA(cm); i < fdmax; i++, cmfptr++) { |
| 297 | int new_fd; |
| 298 | err = security_file_receive(fp[i]); |
| 299 | if (err) |
| 300 | break; |
Ulrich Drepper | 4a19542 | 2007-07-15 23:40:34 -0700 | [diff] [blame] | 301 | err = get_unused_fd_flags(MSG_CMSG_CLOEXEC & kmsg->msg_flags |
| 302 | ? O_CLOEXEC : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | if (err < 0) |
| 304 | break; |
| 305 | new_fd = err; |
| 306 | err = put_user(new_fd, cmfptr); |
| 307 | if (err) { |
| 308 | put_unused_fd(new_fd); |
| 309 | break; |
| 310 | } |
| 311 | /* Bump the usage count and install the file. */ |
Al Viro | cb0942b | 2012-08-27 14:48:26 -0400 | [diff] [blame] | 312 | fd_install(new_fd, get_file(fp[i])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | if (i > 0) { |
| 316 | int cmlen = CMSG_COMPAT_LEN(i * sizeof(int)); |
Miklos Szeredi | effee6a | 2006-10-09 21:42:14 -0700 | [diff] [blame] | 317 | err = put_user(SOL_SOCKET, &cm->cmsg_level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | if (!err) |
| 319 | err = put_user(SCM_RIGHTS, &cm->cmsg_type); |
| 320 | if (!err) |
| 321 | err = put_user(cmlen, &cm->cmsg_len); |
| 322 | if (!err) { |
| 323 | cmlen = CMSG_COMPAT_SPACE(i * sizeof(int)); |
| 324 | kmsg->msg_control += cmlen; |
| 325 | kmsg->msg_controllen -= cmlen; |
| 326 | } |
| 327 | } |
| 328 | if (i < fdnum) |
| 329 | kmsg->msg_flags |= MSG_CTRUNC; |
| 330 | |
| 331 | /* |
| 332 | * All of the files that fit in the message have had their |
| 333 | * usage counts incremented, so we just free the list. |
| 334 | */ |
| 335 | __scm_destroy(scm); |
| 336 | } |
| 337 | |
Willem de Bruijn | 719c44d | 2016-06-07 12:06:34 -0400 | [diff] [blame] | 338 | /* allocate a 64-bit sock_fprog on the user stack for duration of syscall. */ |
| 339 | struct sock_fprog __user *get_compat_bpf_fprog(char __user *optval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | { |
| 341 | struct compat_sock_fprog __user *fprog32 = (struct compat_sock_fprog __user *)optval; |
YOSHIFUJI Hideaki | 4768fbc | 2007-02-09 23:25:31 +0900 | [diff] [blame] | 342 | struct sock_fprog __user *kfprog = compat_alloc_user_space(sizeof(struct sock_fprog)); |
Al Viro | f8f8a72 | 2017-06-27 18:34:53 -0400 | [diff] [blame] | 343 | struct compat_sock_fprog f32; |
| 344 | struct sock_fprog f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
Al Viro | f8f8a72 | 2017-06-27 18:34:53 -0400 | [diff] [blame] | 346 | if (copy_from_user(&f32, fprog32, sizeof(*fprog32))) |
| 347 | return NULL; |
| 348 | memset(&f, 0, sizeof(f)); |
| 349 | f.len = f32.len; |
| 350 | f.filter = compat_ptr(f32.filter); |
| 351 | if (copy_to_user(kfprog, &f, sizeof(struct sock_fprog))) |
Willem de Bruijn | 719c44d | 2016-06-07 12:06:34 -0400 | [diff] [blame] | 352 | return NULL; |
| 353 | |
| 354 | return kfprog; |
| 355 | } |
| 356 | EXPORT_SYMBOL_GPL(get_compat_bpf_fprog); |
| 357 | |
| 358 | static int do_set_attach_filter(struct socket *sock, int level, int optname, |
| 359 | char __user *optval, unsigned int optlen) |
| 360 | { |
| 361 | struct sock_fprog __user *kfprog; |
| 362 | |
| 363 | kfprog = get_compat_bpf_fprog(optval); |
| 364 | if (!kfprog) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | return -EFAULT; |
| 366 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 367 | return sock_setsockopt(sock, level, optname, (char __user *)kfprog, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | sizeof(struct sock_fprog)); |
| 369 | } |
| 370 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 371 | static int compat_sock_setsockopt(struct socket *sock, int level, int optname, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 372 | char __user *optval, unsigned int optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 373 | { |
Helge Deller | 1957598 | 2016-06-03 23:49:17 +0200 | [diff] [blame] | 374 | if (optname == SO_ATTACH_FILTER || |
| 375 | optname == SO_ATTACH_REUSEPORT_CBPF) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 376 | return do_set_attach_filter(sock, level, optname, |
| 377 | optval, optlen); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 378 | return sock_setsockopt(sock, level, optname, optval, optlen); |
| 379 | } |
| 380 | |
Dominik Brodowski | 73ee3ea | 2018-03-16 16:58:54 +0100 | [diff] [blame] | 381 | static int __compat_sys_setsockopt(int fd, int level, int optname, |
| 382 | char __user *optval, unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | { |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 384 | int err; |
Jann Horn | 52baf98 | 2019-02-20 22:34:54 +0100 | [diff] [blame] | 385 | struct socket *sock; |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 386 | |
Jann Horn | 52baf98 | 2019-02-20 22:34:54 +0100 | [diff] [blame] | 387 | if (optlen > INT_MAX) |
| 388 | return -EINVAL; |
| 389 | |
| 390 | sock = sockfd_lookup(fd, &err); |
Eric Dumazet | c6d409c | 2010-06-03 20:03:40 -0700 | [diff] [blame] | 391 | if (sock) { |
| 392 | err = security_socket_setsockopt(sock, level, optname); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 393 | if (err) { |
| 394 | sockfd_put(sock); |
| 395 | return err; |
| 396 | } |
| 397 | |
| 398 | if (level == SOL_SOCKET) |
| 399 | err = compat_sock_setsockopt(sock, level, |
| 400 | optname, optval, optlen); |
| 401 | else if (sock->ops->compat_setsockopt) |
| 402 | err = sock->ops->compat_setsockopt(sock, level, |
| 403 | optname, optval, optlen); |
| 404 | else |
| 405 | err = sock->ops->setsockopt(sock, level, |
| 406 | optname, optval, optlen); |
| 407 | sockfd_put(sock); |
| 408 | } |
| 409 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Dominik Brodowski | 73ee3ea | 2018-03-16 16:58:54 +0100 | [diff] [blame] | 412 | COMPAT_SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname, |
| 413 | char __user *, optval, unsigned int, optlen) |
| 414 | { |
| 415 | return __compat_sys_setsockopt(fd, level, optname, optval, optlen); |
| 416 | } |
| 417 | |
Dominik Brodowski | 8770cf4 | 2018-03-16 17:01:41 +0100 | [diff] [blame] | 418 | static int __compat_sys_getsockopt(int fd, int level, int optname, |
| 419 | char __user *optval, |
| 420 | int __user *optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | { |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 422 | int err; |
Eric Dumazet | c6d409c | 2010-06-03 20:03:40 -0700 | [diff] [blame] | 423 | struct socket *sock = sockfd_lookup(fd, &err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | |
Eric Dumazet | c6d409c | 2010-06-03 20:03:40 -0700 | [diff] [blame] | 425 | if (sock) { |
| 426 | err = security_socket_getsockopt(sock, level, optname); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 427 | if (err) { |
| 428 | sockfd_put(sock); |
| 429 | return err; |
| 430 | } |
| 431 | |
| 432 | if (level == SOL_SOCKET) |
Arnd Bergmann | fe0c72f | 2019-02-02 07:34:44 -0800 | [diff] [blame] | 433 | err = sock_getsockopt(sock, level, |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 434 | optname, optval, optlen); |
| 435 | else if (sock->ops->compat_getsockopt) |
| 436 | err = sock->ops->compat_getsockopt(sock, level, |
| 437 | optname, optval, optlen); |
| 438 | else |
| 439 | err = sock->ops->getsockopt(sock, level, |
| 440 | optname, optval, optlen); |
| 441 | sockfd_put(sock); |
| 442 | } |
| 443 | return err; |
| 444 | } |
David L Stevens | dae5029 | 2008-04-27 01:06:07 -0700 | [diff] [blame] | 445 | |
Dominik Brodowski | 8770cf4 | 2018-03-16 17:01:41 +0100 | [diff] [blame] | 446 | COMPAT_SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname, |
| 447 | char __user *, optval, int __user *, optlen) |
| 448 | { |
| 449 | return __compat_sys_getsockopt(fd, level, optname, optval, optlen); |
| 450 | } |
| 451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | /* Argument list sizes for compat_sys_socketcall */ |
| 453 | #define AL(x) ((x) * sizeof(u32)) |
Anton Blanchard | 228e548 | 2011-05-02 20:21:35 +0000 | [diff] [blame] | 454 | static unsigned char nas[21] = { |
Eric Dumazet | c6d409c | 2010-06-03 20:03:40 -0700 | [diff] [blame] | 455 | AL(0), AL(3), AL(3), AL(3), AL(2), AL(3), |
| 456 | AL(3), AL(3), AL(4), AL(4), AL(4), AL(6), |
| 457 | AL(6), AL(2), AL(5), AL(5), AL(3), AL(3), |
Anton Blanchard | 228e548 | 2011-05-02 20:21:35 +0000 | [diff] [blame] | 458 | AL(4), AL(5), AL(4) |
Eric Dumazet | c6d409c | 2010-06-03 20:03:40 -0700 | [diff] [blame] | 459 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | #undef AL |
| 461 | |
Dominik Brodowski | 6df3546 | 2018-03-16 17:07:03 +0100 | [diff] [blame] | 462 | static inline long __compat_sys_sendmsg(int fd, |
| 463 | struct compat_msghdr __user *msg, |
| 464 | unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | { |
Dominik Brodowski | e1834a3 | 2018-03-13 20:35:57 +0100 | [diff] [blame] | 466 | return __sys_sendmsg(fd, (struct user_msghdr __user *)msg, |
| 467 | flags | MSG_CMSG_COMPAT, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Dominik Brodowski | 6df3546 | 2018-03-16 17:07:03 +0100 | [diff] [blame] | 470 | COMPAT_SYSCALL_DEFINE3(sendmsg, int, fd, struct compat_msghdr __user *, msg, |
| 471 | unsigned int, flags) |
| 472 | { |
| 473 | return __compat_sys_sendmsg(fd, msg, flags); |
| 474 | } |
| 475 | |
| 476 | static inline long __compat_sys_sendmmsg(int fd, |
| 477 | struct compat_mmsghdr __user *mmsg, |
| 478 | unsigned int vlen, unsigned int flags) |
Anton Blanchard | 228e548 | 2011-05-02 20:21:35 +0000 | [diff] [blame] | 479 | { |
| 480 | return __sys_sendmmsg(fd, (struct mmsghdr __user *)mmsg, vlen, |
Dominik Brodowski | e1834a3 | 2018-03-13 20:35:57 +0100 | [diff] [blame] | 481 | flags | MSG_CMSG_COMPAT, false); |
Anton Blanchard | 228e548 | 2011-05-02 20:21:35 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Dominik Brodowski | 6df3546 | 2018-03-16 17:07:03 +0100 | [diff] [blame] | 484 | COMPAT_SYSCALL_DEFINE4(sendmmsg, int, fd, struct compat_mmsghdr __user *, mmsg, |
| 485 | unsigned int, vlen, unsigned int, flags) |
| 486 | { |
| 487 | return __compat_sys_sendmmsg(fd, mmsg, vlen, flags); |
| 488 | } |
| 489 | |
| 490 | static inline long __compat_sys_recvmsg(int fd, |
| 491 | struct compat_msghdr __user *msg, |
| 492 | unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | { |
Dominik Brodowski | e1834a3 | 2018-03-13 20:35:57 +0100 | [diff] [blame] | 494 | return __sys_recvmsg(fd, (struct user_msghdr __user *)msg, |
| 495 | flags | MSG_CMSG_COMPAT, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Dominik Brodowski | 6df3546 | 2018-03-16 17:07:03 +0100 | [diff] [blame] | 498 | COMPAT_SYSCALL_DEFINE3(recvmsg, int, fd, struct compat_msghdr __user *, msg, |
| 499 | unsigned int, flags) |
| 500 | { |
| 501 | return __compat_sys_recvmsg(fd, msg, flags); |
| 502 | } |
| 503 | |
Dominik Brodowski | fd4e82f | 2018-03-16 16:48:34 +0100 | [diff] [blame] | 504 | static inline long __compat_sys_recvfrom(int fd, void __user *buf, |
| 505 | compat_size_t len, unsigned int flags, |
| 506 | struct sockaddr __user *addr, |
| 507 | int __user *addrlen) |
| 508 | { |
| 509 | return __sys_recvfrom(fd, buf, len, flags | MSG_CMSG_COMPAT, addr, |
| 510 | addrlen); |
| 511 | } |
| 512 | |
Heiko Carstens | 3a49a0f | 2014-03-04 17:09:57 +0100 | [diff] [blame] | 513 | COMPAT_SYSCALL_DEFINE4(recv, int, fd, void __user *, buf, compat_size_t, len, unsigned int, flags) |
Johannes Berg | 1dacc76 | 2009-07-01 11:26:02 +0000 | [diff] [blame] | 514 | { |
Dominik Brodowski | fd4e82f | 2018-03-16 16:48:34 +0100 | [diff] [blame] | 515 | return __compat_sys_recvfrom(fd, buf, len, flags, NULL, NULL); |
Johannes Berg | 1dacc76 | 2009-07-01 11:26:02 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Heiko Carstens | 3a49a0f | 2014-03-04 17:09:57 +0100 | [diff] [blame] | 518 | COMPAT_SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, buf, compat_size_t, len, |
| 519 | unsigned int, flags, struct sockaddr __user *, addr, |
| 520 | int __user *, addrlen) |
Johannes Berg | 1dacc76 | 2009-07-01 11:26:02 +0000 | [diff] [blame] | 521 | { |
Dominik Brodowski | fd4e82f | 2018-03-16 16:48:34 +0100 | [diff] [blame] | 522 | return __compat_sys_recvfrom(fd, buf, len, flags, addr, addrlen); |
Johannes Berg | 1dacc76 | 2009-07-01 11:26:02 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Arnd Bergmann | e11d428 | 2018-04-18 13:43:52 +0200 | [diff] [blame] | 525 | COMPAT_SYSCALL_DEFINE5(recvmmsg_time64, int, fd, struct compat_mmsghdr __user *, mmsg, |
| 526 | unsigned int, vlen, unsigned int, flags, |
| 527 | struct __kernel_timespec __user *, timeout) |
Arnaldo Carvalho de Melo | a2e2725 | 2009-10-12 23:40:10 -0700 | [diff] [blame] | 528 | { |
Arnd Bergmann | e11d428 | 2018-04-18 13:43:52 +0200 | [diff] [blame] | 529 | return __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen, |
| 530 | flags | MSG_CMSG_COMPAT, timeout, NULL); |
Arnaldo Carvalho de Melo | a2e2725 | 2009-10-12 23:40:10 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Arnd Bergmann | e11d428 | 2018-04-18 13:43:52 +0200 | [diff] [blame] | 533 | #ifdef CONFIG_COMPAT_32BIT_TIME |
Arnd Bergmann | 8dabe72 | 2019-01-07 00:33:08 +0100 | [diff] [blame] | 534 | COMPAT_SYSCALL_DEFINE5(recvmmsg_time32, int, fd, struct compat_mmsghdr __user *, mmsg, |
Dominik Brodowski | 157b334 | 2018-03-16 17:10:50 +0100 | [diff] [blame] | 535 | unsigned int, vlen, unsigned int, flags, |
Arnd Bergmann | 9afc5ee | 2018-07-13 12:52:28 +0200 | [diff] [blame] | 536 | struct old_timespec32 __user *, timeout) |
Dominik Brodowski | 157b334 | 2018-03-16 17:10:50 +0100 | [diff] [blame] | 537 | { |
Arnd Bergmann | e11d428 | 2018-04-18 13:43:52 +0200 | [diff] [blame] | 538 | return __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen, |
| 539 | flags | MSG_CMSG_COMPAT, NULL, timeout); |
Dominik Brodowski | 157b334 | 2018-03-16 17:10:50 +0100 | [diff] [blame] | 540 | } |
Arnd Bergmann | e11d428 | 2018-04-18 13:43:52 +0200 | [diff] [blame] | 541 | #endif |
Dominik Brodowski | 157b334 | 2018-03-16 17:10:50 +0100 | [diff] [blame] | 542 | |
Heiko Carstens | 361d93c | 2014-03-03 16:21:27 +0100 | [diff] [blame] | 543 | COMPAT_SYSCALL_DEFINE2(socketcall, int, call, u32 __user *, args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | { |
Richard Guy Briggs | 62bc306 | 2017-01-17 11:07:15 -0500 | [diff] [blame] | 545 | u32 a[AUDITSC_ARGS]; |
| 546 | unsigned int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | u32 a0, a1; |
Richard Guy Briggs | 62bc306 | 2017-01-17 11:07:15 -0500 | [diff] [blame] | 548 | int ret; |
YOSHIFUJI Hideaki | 4768fbc | 2007-02-09 23:25:31 +0900 | [diff] [blame] | 549 | |
Anton Blanchard | 228e548 | 2011-05-02 20:21:35 +0000 | [diff] [blame] | 550 | if (call < SYS_SOCKET || call > SYS_SENDMMSG) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | return -EINVAL; |
Richard Guy Briggs | 62bc306 | 2017-01-17 11:07:15 -0500 | [diff] [blame] | 552 | len = nas[call]; |
| 553 | if (len > sizeof(a)) |
| 554 | return -EINVAL; |
| 555 | |
| 556 | if (copy_from_user(a, args, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | return -EFAULT; |
Richard Guy Briggs | 62bc306 | 2017-01-17 11:07:15 -0500 | [diff] [blame] | 558 | |
| 559 | ret = audit_socketcall_compat(len / sizeof(a[0]), a); |
| 560 | if (ret) |
| 561 | return ret; |
| 562 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | a0 = a[0]; |
| 564 | a1 = a[1]; |
YOSHIFUJI Hideaki | 4768fbc | 2007-02-09 23:25:31 +0900 | [diff] [blame] | 565 | |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 566 | switch (call) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | case SYS_SOCKET: |
Dominik Brodowski | 9d6a15c | 2018-03-13 19:29:43 +0100 | [diff] [blame] | 568 | ret = __sys_socket(a0, a1, a[2]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | break; |
| 570 | case SYS_BIND: |
Dominik Brodowski | a87d35d | 2018-03-13 19:33:09 +0100 | [diff] [blame] | 571 | ret = __sys_bind(a0, compat_ptr(a1), a[2]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | break; |
| 573 | case SYS_CONNECT: |
Dominik Brodowski | 1387c2c | 2018-03-13 19:35:09 +0100 | [diff] [blame] | 574 | ret = __sys_connect(a0, compat_ptr(a1), a[2]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | break; |
| 576 | case SYS_LISTEN: |
Dominik Brodowski | 25e290e | 2018-03-13 19:36:54 +0100 | [diff] [blame] | 577 | ret = __sys_listen(a0, a1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | break; |
| 579 | case SYS_ACCEPT: |
Dominik Brodowski | 4541e80 | 2018-03-13 19:24:23 +0100 | [diff] [blame] | 580 | ret = __sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | break; |
| 582 | case SYS_GETSOCKNAME: |
Dominik Brodowski | 8882a10 | 2018-03-13 19:43:14 +0100 | [diff] [blame] | 583 | ret = __sys_getsockname(a0, compat_ptr(a1), compat_ptr(a[2])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | break; |
| 585 | case SYS_GETPEERNAME: |
Dominik Brodowski | b21c8f8 | 2018-03-13 19:47:00 +0100 | [diff] [blame] | 586 | ret = __sys_getpeername(a0, compat_ptr(a1), compat_ptr(a[2])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | break; |
| 588 | case SYS_SOCKETPAIR: |
Dominik Brodowski | 6debc8d | 2018-03-13 19:49:23 +0100 | [diff] [blame] | 589 | ret = __sys_socketpair(a0, a1, a[2], compat_ptr(a[3])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | break; |
| 591 | case SYS_SEND: |
Dominik Brodowski | f3bf896 | 2018-03-13 19:52:00 +0100 | [diff] [blame] | 592 | ret = __sys_sendto(a0, compat_ptr(a1), a[2], a[3], NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | break; |
| 594 | case SYS_SENDTO: |
Dominik Brodowski | 211b634 | 2018-03-13 19:18:52 +0100 | [diff] [blame] | 595 | ret = __sys_sendto(a0, compat_ptr(a1), a[2], a[3], |
| 596 | compat_ptr(a[4]), a[5]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | break; |
| 598 | case SYS_RECV: |
Dominik Brodowski | fd4e82f | 2018-03-16 16:48:34 +0100 | [diff] [blame] | 599 | ret = __compat_sys_recvfrom(a0, compat_ptr(a1), a[2], a[3], |
| 600 | NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | break; |
| 602 | case SYS_RECVFROM: |
Dominik Brodowski | fd4e82f | 2018-03-16 16:48:34 +0100 | [diff] [blame] | 603 | ret = __compat_sys_recvfrom(a0, compat_ptr(a1), a[2], a[3], |
| 604 | compat_ptr(a[4]), |
| 605 | compat_ptr(a[5])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | break; |
| 607 | case SYS_SHUTDOWN: |
Dominik Brodowski | 005a1ae | 2018-03-13 20:07:05 +0100 | [diff] [blame] | 608 | ret = __sys_shutdown(a0, a1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | break; |
| 610 | case SYS_SETSOCKOPT: |
Dominik Brodowski | 73ee3ea | 2018-03-16 16:58:54 +0100 | [diff] [blame] | 611 | ret = __compat_sys_setsockopt(a0, a1, a[2], |
| 612 | compat_ptr(a[3]), a[4]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | break; |
| 614 | case SYS_GETSOCKOPT: |
Dominik Brodowski | 8770cf4 | 2018-03-16 17:01:41 +0100 | [diff] [blame] | 615 | ret = __compat_sys_getsockopt(a0, a1, a[2], |
| 616 | compat_ptr(a[3]), |
| 617 | compat_ptr(a[4])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | break; |
| 619 | case SYS_SENDMSG: |
Dominik Brodowski | 6df3546 | 2018-03-16 17:07:03 +0100 | [diff] [blame] | 620 | ret = __compat_sys_sendmsg(a0, compat_ptr(a1), a[2]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | break; |
Anton Blanchard | 228e548 | 2011-05-02 20:21:35 +0000 | [diff] [blame] | 622 | case SYS_SENDMMSG: |
Dominik Brodowski | 6df3546 | 2018-03-16 17:07:03 +0100 | [diff] [blame] | 623 | ret = __compat_sys_sendmmsg(a0, compat_ptr(a1), a[2], a[3]); |
Anton Blanchard | 228e548 | 2011-05-02 20:21:35 +0000 | [diff] [blame] | 624 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | case SYS_RECVMSG: |
Dominik Brodowski | 6df3546 | 2018-03-16 17:07:03 +0100 | [diff] [blame] | 626 | ret = __compat_sys_recvmsg(a0, compat_ptr(a1), a[2]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | break; |
Arnaldo Carvalho de Melo | a2e2725 | 2009-10-12 23:40:10 -0700 | [diff] [blame] | 628 | case SYS_RECVMMSG: |
Arnd Bergmann | e11d428 | 2018-04-18 13:43:52 +0200 | [diff] [blame] | 629 | ret = __sys_recvmmsg(a0, compat_ptr(a1), a[2], |
| 630 | a[3] | MSG_CMSG_COMPAT, NULL, |
| 631 | compat_ptr(a[4])); |
Arnaldo Carvalho de Melo | a2e2725 | 2009-10-12 23:40:10 -0700 | [diff] [blame] | 632 | break; |
Ulrich Drepper | de11def | 2008-11-19 15:36:14 -0800 | [diff] [blame] | 633 | case SYS_ACCEPT4: |
Dominik Brodowski | 4541e80 | 2018-03-13 19:24:23 +0100 | [diff] [blame] | 634 | ret = __sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), a[3]); |
Ulrich Drepper | aaca0bd | 2008-07-23 21:29:20 -0700 | [diff] [blame] | 635 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | default: |
| 637 | ret = -EINVAL; |
| 638 | break; |
| 639 | } |
| 640 | return ret; |
| 641 | } |