YOSHIFUJI Hideaki | 4768fbc | 2007-02-09 23:25:31 +0900 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * 32bit Socket syscall emulation. Based on arch/sparc64/kernel/sys_sparc32.c. |
| 3 | * |
| 4 | * Copyright (C) 2000 VA Linux Co |
| 5 | * Copyright (C) 2000 Don Dugger <n0ano@valinux.com> |
| 6 | * Copyright (C) 1999 Arun Sharma <arun.sharma@intel.com> |
| 7 | * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz) |
| 8 | * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu) |
| 9 | * Copyright (C) 2000 Hewlett-Packard Co. |
| 10 | * Copyright (C) 2000 David Mosberger-Tang <davidm@hpl.hp.com> |
YOSHIFUJI Hideaki | 4768fbc | 2007-02-09 23:25:31 +0900 | [diff] [blame] | 11 | * Copyright (C) 2000,2001 Andi Kleen, SuSE Labs |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/types.h> |
| 18 | #include <linux/file.h> |
| 19 | #include <linux/icmpv6.h> |
| 20 | #include <linux/socket.h> |
| 21 | #include <linux/syscalls.h> |
| 22 | #include <linux/filter.h> |
| 23 | #include <linux/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/security.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 25 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | #include <net/scm.h> |
| 28 | #include <net/sock.h> |
David L Stevens | dae5029 | 2008-04-27 01:06:07 -0700 | [diff] [blame] | 29 | #include <net/ip.h> |
| 30 | #include <net/ipv6.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <asm/uaccess.h> |
| 32 | #include <net/compat.h> |
| 33 | |
Al Viro | 08adb7d | 2014-11-10 20:23:13 -0500 | [diff] [blame] | 34 | ssize_t get_compat_msghdr(struct msghdr *kmsg, |
| 35 | struct compat_msghdr __user *umsg, |
| 36 | struct sockaddr __user **save_addr, |
| 37 | struct iovec **iov) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | { |
Al Viro | 08adb7d | 2014-11-10 20:23:13 -0500 | [diff] [blame] | 39 | compat_uptr_t uaddr, uiov, tmp3; |
Al Viro | c0371da | 2014-11-24 10:42:55 -0500 | [diff] [blame] | 40 | compat_size_t nr_segs; |
Al Viro | 08adb7d | 2014-11-10 20:23:13 -0500 | [diff] [blame] | 41 | ssize_t err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | if (!access_ok(VERIFY_READ, umsg, sizeof(*umsg)) || |
Al Viro | 08adb7d | 2014-11-10 20:23:13 -0500 | [diff] [blame] | 44 | __get_user(uaddr, &umsg->msg_name) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | __get_user(kmsg->msg_namelen, &umsg->msg_namelen) || |
Al Viro | 08adb7d | 2014-11-10 20:23:13 -0500 | [diff] [blame] | 46 | __get_user(uiov, &umsg->msg_iov) || |
Al Viro | c0371da | 2014-11-24 10:42:55 -0500 | [diff] [blame] | 47 | __get_user(nr_segs, &umsg->msg_iovlen) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | __get_user(tmp3, &umsg->msg_control) || |
| 49 | __get_user(kmsg->msg_controllen, &umsg->msg_controllen) || |
| 50 | __get_user(kmsg->msg_flags, &umsg->msg_flags)) |
| 51 | return -EFAULT; |
Dan Carpenter | 1661bf3 | 2013-10-03 00:27:20 +0300 | [diff] [blame] | 52 | if (kmsg->msg_namelen > sizeof(struct sockaddr_storage)) |
Dan Carpenter | db31c55 | 2013-11-27 15:40:21 +0300 | [diff] [blame] | 53 | kmsg->msg_namelen = sizeof(struct sockaddr_storage); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | kmsg->msg_control = compat_ptr(tmp3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Al Viro | 08adb7d | 2014-11-10 20:23:13 -0500 | [diff] [blame] | 56 | if (save_addr) |
| 57 | *save_addr = compat_ptr(uaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Al Viro | 08adb7d | 2014-11-10 20:23:13 -0500 | [diff] [blame] | 59 | if (uaddr && kmsg->msg_namelen) { |
| 60 | if (!save_addr) { |
| 61 | err = move_addr_to_kernel(compat_ptr(uaddr), |
| 62 | kmsg->msg_namelen, |
| 63 | kmsg->msg_name); |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 64 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | return err; |
| 66 | } |
Andrey Ryabinin | 40eea80 | 2014-07-26 21:26:58 +0400 | [diff] [blame] | 67 | } else { |
Al Viro | 08adb7d | 2014-11-10 20:23:13 -0500 | [diff] [blame] | 68 | kmsg->msg_name = NULL; |
| 69 | kmsg->msg_namelen = 0; |
Andrey Ryabinin | 40eea80 | 2014-07-26 21:26:58 +0400 | [diff] [blame] | 70 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Al Viro | c0371da | 2014-11-24 10:42:55 -0500 | [diff] [blame] | 72 | if (nr_segs > UIO_MAXIOV) |
Al Viro | 0844932 | 2014-11-09 22:33:45 -0500 | [diff] [blame] | 73 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
Al Viro | 08adb7d | 2014-11-10 20:23:13 -0500 | [diff] [blame] | 75 | err = compat_rw_copy_check_uvector(save_addr ? READ : WRITE, |
Al Viro | c0371da | 2014-11-24 10:42:55 -0500 | [diff] [blame] | 76 | compat_ptr(uiov), nr_segs, |
Al Viro | 08adb7d | 2014-11-10 20:23:13 -0500 | [diff] [blame] | 77 | UIO_FASTIOV, *iov, iov); |
Al Viro | 0844932 | 2014-11-09 22:33:45 -0500 | [diff] [blame] | 78 | if (err >= 0) |
Al Viro | c0371da | 2014-11-24 10:42:55 -0500 | [diff] [blame] | 79 | iov_iter_init(&kmsg->msg_iter, save_addr ? READ : WRITE, |
| 80 | *iov, nr_segs, err); |
Al Viro | 0844932 | 2014-11-09 22:33:45 -0500 | [diff] [blame] | 81 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /* Bleech... */ |
| 85 | #define CMSG_COMPAT_ALIGN(len) ALIGN((len), sizeof(s32)) |
| 86 | |
| 87 | #define CMSG_COMPAT_DATA(cmsg) \ |
| 88 | ((void __user *)((char __user *)(cmsg) + CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)))) |
| 89 | #define CMSG_COMPAT_SPACE(len) \ |
| 90 | (CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)) + CMSG_COMPAT_ALIGN(len)) |
| 91 | #define CMSG_COMPAT_LEN(len) \ |
| 92 | (CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)) + (len)) |
| 93 | |
| 94 | #define CMSG_COMPAT_FIRSTHDR(msg) \ |
| 95 | (((msg)->msg_controllen) >= sizeof(struct compat_cmsghdr) ? \ |
| 96 | (struct compat_cmsghdr __user *)((msg)->msg_control) : \ |
| 97 | (struct compat_cmsghdr __user *)NULL) |
| 98 | |
| 99 | #define CMSG_COMPAT_OK(ucmlen, ucmsg, mhdr) \ |
| 100 | ((ucmlen) >= sizeof(struct compat_cmsghdr) && \ |
| 101 | (ucmlen) <= (unsigned long) \ |
| 102 | ((mhdr)->msg_controllen - \ |
| 103 | ((char *)(ucmsg) - (char *)(mhdr)->msg_control))) |
| 104 | |
| 105 | static inline struct compat_cmsghdr __user *cmsg_compat_nxthdr(struct msghdr *msg, |
| 106 | struct compat_cmsghdr __user *cmsg, int cmsg_len) |
| 107 | { |
| 108 | char __user *ptr = (char __user *)cmsg + CMSG_COMPAT_ALIGN(cmsg_len); |
| 109 | if ((unsigned long)(ptr + 1 - (char __user *)msg->msg_control) > |
| 110 | msg->msg_controllen) |
| 111 | return NULL; |
| 112 | return (struct compat_cmsghdr __user *)ptr; |
| 113 | } |
| 114 | |
| 115 | /* There is a lot of hair here because the alignment rules (and |
| 116 | * thus placement) of cmsg headers and length are different for |
| 117 | * 32-bit apps. -DaveM |
| 118 | */ |
Al Viro | 8920e8f | 2005-09-07 18:28:51 -0700 | [diff] [blame] | 119 | 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] | 120 | unsigned char *stackbuf, int stackbuf_size) |
| 121 | { |
| 122 | struct compat_cmsghdr __user *ucmsg; |
| 123 | struct cmsghdr *kcmsg, *kcmsg_base; |
| 124 | compat_size_t ucmlen; |
| 125 | __kernel_size_t kcmlen, tmp; |
Al Viro | 8920e8f | 2005-09-07 18:28:51 -0700 | [diff] [blame] | 126 | int err = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
| 128 | kcmlen = 0; |
| 129 | kcmsg_base = kcmsg = (struct cmsghdr *)stackbuf; |
| 130 | ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg); |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 131 | while (ucmsg != NULL) { |
| 132 | if (get_user(ucmlen, &ucmsg->cmsg_len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | return -EFAULT; |
| 134 | |
| 135 | /* Catch bogons. */ |
| 136 | if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg)) |
| 137 | return -EINVAL; |
| 138 | |
| 139 | tmp = ((ucmlen - CMSG_COMPAT_ALIGN(sizeof(*ucmsg))) + |
| 140 | CMSG_ALIGN(sizeof(struct cmsghdr))); |
Al Viro | 8920e8f | 2005-09-07 18:28:51 -0700 | [diff] [blame] | 141 | tmp = CMSG_ALIGN(tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | kcmlen += tmp; |
| 143 | ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen); |
| 144 | } |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 145 | if (kcmlen == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | return -EINVAL; |
| 147 | |
| 148 | /* The kcmlen holds the 64-bit version of the control length. |
| 149 | * It may not be modified as we do not stick it into the kmsg |
| 150 | * until we have successfully copied over all of the data |
| 151 | * from the user. |
| 152 | */ |
Al Viro | 8920e8f | 2005-09-07 18:28:51 -0700 | [diff] [blame] | 153 | if (kcmlen > stackbuf_size) |
| 154 | kcmsg_base = kcmsg = sock_kmalloc(sk, kcmlen, GFP_KERNEL); |
| 155 | if (kcmsg == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | return -ENOBUFS; |
| 157 | |
| 158 | /* Now copy them over neatly. */ |
| 159 | memset(kcmsg, 0, kcmlen); |
| 160 | ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg); |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 161 | while (ucmsg != NULL) { |
Al Viro | 8920e8f | 2005-09-07 18:28:51 -0700 | [diff] [blame] | 162 | if (__get_user(ucmlen, &ucmsg->cmsg_len)) |
| 163 | goto Efault; |
| 164 | if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg)) |
| 165 | goto Einval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | tmp = ((ucmlen - CMSG_COMPAT_ALIGN(sizeof(*ucmsg))) + |
| 167 | CMSG_ALIGN(sizeof(struct cmsghdr))); |
Al Viro | 8920e8f | 2005-09-07 18:28:51 -0700 | [diff] [blame] | 168 | if ((char *)kcmsg_base + kcmlen - (char *)kcmsg < CMSG_ALIGN(tmp)) |
| 169 | goto Einval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | kcmsg->cmsg_len = tmp; |
Al Viro | 8920e8f | 2005-09-07 18:28:51 -0700 | [diff] [blame] | 171 | tmp = CMSG_ALIGN(tmp); |
| 172 | if (__get_user(kcmsg->cmsg_level, &ucmsg->cmsg_level) || |
| 173 | __get_user(kcmsg->cmsg_type, &ucmsg->cmsg_type) || |
| 174 | copy_from_user(CMSG_DATA(kcmsg), |
| 175 | CMSG_COMPAT_DATA(ucmsg), |
| 176 | (ucmlen - CMSG_COMPAT_ALIGN(sizeof(*ucmsg))))) |
| 177 | goto Efault; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
| 179 | /* Advance. */ |
Al Viro | 8920e8f | 2005-09-07 18:28:51 -0700 | [diff] [blame] | 180 | kcmsg = (struct cmsghdr *)((char *)kcmsg + tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen); |
| 182 | } |
| 183 | |
| 184 | /* Ok, looks like we made it. Hook it up and return success. */ |
| 185 | kmsg->msg_control = kcmsg_base; |
| 186 | kmsg->msg_controllen = kcmlen; |
| 187 | return 0; |
| 188 | |
Al Viro | 8920e8f | 2005-09-07 18:28:51 -0700 | [diff] [blame] | 189 | Einval: |
| 190 | err = -EINVAL; |
| 191 | Efault: |
| 192 | if (kcmsg_base != (struct cmsghdr *)stackbuf) |
| 193 | sock_kfree_s(sk, kcmsg_base, kcmlen); |
| 194 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | int put_cmsg_compat(struct msghdr *kmsg, int level, int type, int len, void *data) |
| 198 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | struct compat_cmsghdr __user *cm = (struct compat_cmsghdr __user *) kmsg->msg_control; |
| 200 | struct compat_cmsghdr cmhdr; |
Jesper Juhl | 8188104 | 2012-07-22 11:37:20 +0000 | [diff] [blame] | 201 | struct compat_timeval ctv; |
| 202 | struct compat_timespec cts[3]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | int cmlen; |
| 204 | |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 205 | if (cm == NULL || kmsg->msg_controllen < sizeof(*cm)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | kmsg->msg_flags |= MSG_CTRUNC; |
| 207 | return 0; /* XXX: return error? check spec. */ |
| 208 | } |
| 209 | |
H. J. Lu | ee4fa23 | 2012-02-19 17:50:46 -0800 | [diff] [blame] | 210 | if (!COMPAT_USE_64BIT_TIME) { |
H. J. Lu | ee4fa23 | 2012-02-19 17:50:46 -0800 | [diff] [blame] | 211 | if (level == SOL_SOCKET && type == SCM_TIMESTAMP) { |
| 212 | struct timeval *tv = (struct timeval *)data; |
| 213 | ctv.tv_sec = tv->tv_sec; |
| 214 | ctv.tv_usec = tv->tv_usec; |
| 215 | data = &ctv; |
| 216 | len = sizeof(ctv); |
Patrick Ohly | 20d4947 | 2009-02-12 05:03:38 +0000 | [diff] [blame] | 217 | } |
H. J. Lu | ee4fa23 | 2012-02-19 17:50:46 -0800 | [diff] [blame] | 218 | if (level == SOL_SOCKET && |
| 219 | (type == SCM_TIMESTAMPNS || type == SCM_TIMESTAMPING)) { |
| 220 | int count = type == SCM_TIMESTAMPNS ? 1 : 3; |
| 221 | int i; |
| 222 | struct timespec *ts = (struct timespec *)data; |
| 223 | for (i = 0; i < count; i++) { |
| 224 | cts[i].tv_sec = ts[i].tv_sec; |
| 225 | cts[i].tv_nsec = ts[i].tv_nsec; |
| 226 | } |
| 227 | data = &cts; |
| 228 | len = sizeof(cts[0]) * count; |
| 229 | } |
YOSHIFUJI Hideaki | 4768fbc | 2007-02-09 23:25:31 +0900 | [diff] [blame] | 230 | } |
| 231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | cmlen = CMSG_COMPAT_LEN(len); |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 233 | if (kmsg->msg_controllen < cmlen) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | kmsg->msg_flags |= MSG_CTRUNC; |
| 235 | cmlen = kmsg->msg_controllen; |
| 236 | } |
| 237 | cmhdr.cmsg_level = level; |
| 238 | cmhdr.cmsg_type = type; |
| 239 | cmhdr.cmsg_len = cmlen; |
| 240 | |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 241 | if (copy_to_user(cm, &cmhdr, sizeof cmhdr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | return -EFAULT; |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 243 | 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] | 244 | return -EFAULT; |
| 245 | cmlen = CMSG_COMPAT_SPACE(len); |
Wei Yongjun | 1ac70e7 | 2007-12-20 14:36:44 -0800 | [diff] [blame] | 246 | if (kmsg->msg_controllen < cmlen) |
| 247 | cmlen = kmsg->msg_controllen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | kmsg->msg_control += cmlen; |
| 249 | kmsg->msg_controllen -= cmlen; |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | void scm_detach_fds_compat(struct msghdr *kmsg, struct scm_cookie *scm) |
| 254 | { |
| 255 | struct compat_cmsghdr __user *cm = (struct compat_cmsghdr __user *) kmsg->msg_control; |
| 256 | int fdmax = (kmsg->msg_controllen - sizeof(struct compat_cmsghdr)) / sizeof(int); |
| 257 | int fdnum = scm->fp->count; |
| 258 | struct file **fp = scm->fp->fp; |
| 259 | int __user *cmfptr; |
| 260 | int err = 0, i; |
| 261 | |
| 262 | if (fdnum < fdmax) |
| 263 | fdmax = fdnum; |
| 264 | |
| 265 | for (i = 0, cmfptr = (int __user *) CMSG_COMPAT_DATA(cm); i < fdmax; i++, cmfptr++) { |
| 266 | int new_fd; |
| 267 | err = security_file_receive(fp[i]); |
| 268 | if (err) |
| 269 | break; |
Ulrich Drepper | 4a19542 | 2007-07-15 23:40:34 -0700 | [diff] [blame] | 270 | err = get_unused_fd_flags(MSG_CMSG_CLOEXEC & kmsg->msg_flags |
| 271 | ? O_CLOEXEC : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | if (err < 0) |
| 273 | break; |
| 274 | new_fd = err; |
| 275 | err = put_user(new_fd, cmfptr); |
| 276 | if (err) { |
| 277 | put_unused_fd(new_fd); |
| 278 | break; |
| 279 | } |
| 280 | /* Bump the usage count and install the file. */ |
Al Viro | cb0942b | 2012-08-27 14:48:26 -0400 | [diff] [blame] | 281 | fd_install(new_fd, get_file(fp[i])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | if (i > 0) { |
| 285 | int cmlen = CMSG_COMPAT_LEN(i * sizeof(int)); |
Miklos Szeredi | effee6a | 2006-10-09 21:42:14 -0700 | [diff] [blame] | 286 | err = put_user(SOL_SOCKET, &cm->cmsg_level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | if (!err) |
| 288 | err = put_user(SCM_RIGHTS, &cm->cmsg_type); |
| 289 | if (!err) |
| 290 | err = put_user(cmlen, &cm->cmsg_len); |
| 291 | if (!err) { |
| 292 | cmlen = CMSG_COMPAT_SPACE(i * sizeof(int)); |
| 293 | kmsg->msg_control += cmlen; |
| 294 | kmsg->msg_controllen -= cmlen; |
| 295 | } |
| 296 | } |
| 297 | if (i < fdnum) |
| 298 | kmsg->msg_flags |= MSG_CTRUNC; |
| 299 | |
| 300 | /* |
| 301 | * All of the files that fit in the message have had their |
| 302 | * usage counts incremented, so we just free the list. |
| 303 | */ |
| 304 | __scm_destroy(scm); |
| 305 | } |
| 306 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 307 | static int do_set_attach_filter(struct socket *sock, int level, int optname, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 308 | char __user *optval, unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | { |
| 310 | struct compat_sock_fprog __user *fprog32 = (struct compat_sock_fprog __user *)optval; |
YOSHIFUJI Hideaki | 4768fbc | 2007-02-09 23:25:31 +0900 | [diff] [blame] | 311 | struct sock_fprog __user *kfprog = compat_alloc_user_space(sizeof(struct sock_fprog)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | compat_uptr_t ptr; |
| 313 | u16 len; |
| 314 | |
| 315 | if (!access_ok(VERIFY_READ, fprog32, sizeof(*fprog32)) || |
| 316 | !access_ok(VERIFY_WRITE, kfprog, sizeof(struct sock_fprog)) || |
| 317 | __get_user(len, &fprog32->len) || |
| 318 | __get_user(ptr, &fprog32->filter) || |
| 319 | __put_user(len, &kfprog->len) || |
| 320 | __put_user(compat_ptr(ptr), &kfprog->filter)) |
| 321 | return -EFAULT; |
| 322 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 323 | return sock_setsockopt(sock, level, optname, (char __user *)kfprog, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | sizeof(struct sock_fprog)); |
| 325 | } |
| 326 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 327 | static int do_set_sock_timeout(struct socket *sock, int level, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 328 | int optname, char __user *optval, unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | { |
Eric Dumazet | c6d409c | 2010-06-03 20:03:40 -0700 | [diff] [blame] | 330 | struct compat_timeval __user *up = (struct compat_timeval __user *)optval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | struct timeval ktime; |
| 332 | mm_segment_t old_fs; |
| 333 | int err; |
| 334 | |
| 335 | if (optlen < sizeof(*up)) |
| 336 | return -EINVAL; |
| 337 | if (!access_ok(VERIFY_READ, up, sizeof(*up)) || |
| 338 | __get_user(ktime.tv_sec, &up->tv_sec) || |
| 339 | __get_user(ktime.tv_usec, &up->tv_usec)) |
| 340 | return -EFAULT; |
| 341 | old_fs = get_fs(); |
| 342 | set_fs(KERNEL_DS); |
Eric Dumazet | c6d409c | 2010-06-03 20:03:40 -0700 | [diff] [blame] | 343 | err = sock_setsockopt(sock, level, optname, (char *)&ktime, sizeof(ktime)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | set_fs(old_fs); |
| 345 | |
| 346 | return err; |
| 347 | } |
| 348 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 349 | 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] | 350 | char __user *optval, unsigned int optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 351 | { |
| 352 | if (optname == SO_ATTACH_FILTER) |
| 353 | return do_set_attach_filter(sock, level, optname, |
| 354 | optval, optlen); |
| 355 | if (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO) |
| 356 | return do_set_sock_timeout(sock, level, optname, optval, optlen); |
| 357 | |
| 358 | return sock_setsockopt(sock, level, optname, optval, optlen); |
| 359 | } |
| 360 | |
Heiko Carstens | 361d93c | 2014-03-03 16:21:27 +0100 | [diff] [blame] | 361 | COMPAT_SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname, |
| 362 | char __user *, optval, unsigned int, optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | { |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 364 | int err; |
Eric Dumazet | c6d409c | 2010-06-03 20:03:40 -0700 | [diff] [blame] | 365 | struct socket *sock = sockfd_lookup(fd, &err); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 366 | |
Eric Dumazet | c6d409c | 2010-06-03 20:03:40 -0700 | [diff] [blame] | 367 | if (sock) { |
| 368 | err = security_socket_setsockopt(sock, level, optname); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 369 | if (err) { |
| 370 | sockfd_put(sock); |
| 371 | return err; |
| 372 | } |
| 373 | |
| 374 | if (level == SOL_SOCKET) |
| 375 | err = compat_sock_setsockopt(sock, level, |
| 376 | optname, optval, optlen); |
| 377 | else if (sock->ops->compat_setsockopt) |
| 378 | err = sock->ops->compat_setsockopt(sock, level, |
| 379 | optname, optval, optlen); |
| 380 | else |
| 381 | err = sock->ops->setsockopt(sock, level, |
| 382 | optname, optval, optlen); |
| 383 | sockfd_put(sock); |
| 384 | } |
| 385 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 388 | static int do_get_sock_timeout(struct socket *sock, int level, int optname, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | char __user *optval, int __user *optlen) |
| 390 | { |
| 391 | struct compat_timeval __user *up; |
| 392 | struct timeval ktime; |
| 393 | mm_segment_t old_fs; |
| 394 | int len, err; |
| 395 | |
| 396 | up = (struct compat_timeval __user *) optval; |
| 397 | if (get_user(len, optlen)) |
| 398 | return -EFAULT; |
| 399 | if (len < sizeof(*up)) |
| 400 | return -EINVAL; |
| 401 | len = sizeof(ktime); |
| 402 | old_fs = get_fs(); |
| 403 | set_fs(KERNEL_DS); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 404 | err = sock_getsockopt(sock, level, optname, (char *) &ktime, &len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | set_fs(old_fs); |
| 406 | |
| 407 | if (!err) { |
| 408 | if (put_user(sizeof(*up), optlen) || |
| 409 | !access_ok(VERIFY_WRITE, up, sizeof(*up)) || |
| 410 | __put_user(ktime.tv_sec, &up->tv_sec) || |
| 411 | __put_user(ktime.tv_usec, &up->tv_usec)) |
| 412 | err = -EFAULT; |
| 413 | } |
| 414 | return err; |
| 415 | } |
| 416 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 417 | static int compat_sock_getsockopt(struct socket *sock, int level, int optname, |
| 418 | char __user *optval, int __user *optlen) |
| 419 | { |
| 420 | if (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO) |
| 421 | return do_get_sock_timeout(sock, level, optname, optval, optlen); |
| 422 | return sock_getsockopt(sock, level, optname, optval, optlen); |
| 423 | } |
| 424 | |
Shaun Pereira | f0ac261 | 2006-03-21 23:59:39 -0800 | [diff] [blame] | 425 | int compat_sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp) |
| 426 | { |
H. J. Lu | ee4fa23 | 2012-02-19 17:50:46 -0800 | [diff] [blame] | 427 | struct compat_timeval __user *ctv; |
| 428 | int err; |
Eric Dumazet | b7aa0bf | 2007-04-19 16:16:32 -0700 | [diff] [blame] | 429 | struct timeval tv; |
Shaun Pereira | f0ac261 | 2006-03-21 23:59:39 -0800 | [diff] [blame] | 430 | |
H. J. Lu | ee4fa23 | 2012-02-19 17:50:46 -0800 | [diff] [blame] | 431 | if (COMPAT_USE_64BIT_TIME) |
| 432 | return sock_get_timestamp(sk, userstamp); |
| 433 | |
| 434 | ctv = (struct compat_timeval __user *) userstamp; |
| 435 | err = -ENOENT; |
Shaun Pereira | f0ac261 | 2006-03-21 23:59:39 -0800 | [diff] [blame] | 436 | if (!sock_flag(sk, SOCK_TIMESTAMP)) |
Patrick Ohly | 20d4947 | 2009-02-12 05:03:38 +0000 | [diff] [blame] | 437 | sock_enable_timestamp(sk, SOCK_TIMESTAMP); |
Eric Dumazet | b7aa0bf | 2007-04-19 16:16:32 -0700 | [diff] [blame] | 438 | tv = ktime_to_timeval(sk->sk_stamp); |
| 439 | if (tv.tv_sec == -1) |
Shaun Pereira | f0ac261 | 2006-03-21 23:59:39 -0800 | [diff] [blame] | 440 | return err; |
Eric Dumazet | b7aa0bf | 2007-04-19 16:16:32 -0700 | [diff] [blame] | 441 | if (tv.tv_sec == 0) { |
| 442 | sk->sk_stamp = ktime_get_real(); |
| 443 | tv = ktime_to_timeval(sk->sk_stamp); |
| 444 | } |
| 445 | err = 0; |
| 446 | if (put_user(tv.tv_sec, &ctv->tv_sec) || |
| 447 | put_user(tv.tv_usec, &ctv->tv_usec)) |
Shaun Pereira | f0ac261 | 2006-03-21 23:59:39 -0800 | [diff] [blame] | 448 | err = -EFAULT; |
| 449 | return err; |
| 450 | } |
| 451 | EXPORT_SYMBOL(compat_sock_get_timestamp); |
| 452 | |
Eric Dumazet | ae40eb1 | 2007-03-18 17:33:16 -0700 | [diff] [blame] | 453 | int compat_sock_get_timestampns(struct sock *sk, struct timespec __user *userstamp) |
| 454 | { |
H. J. Lu | ee4fa23 | 2012-02-19 17:50:46 -0800 | [diff] [blame] | 455 | struct compat_timespec __user *ctv; |
| 456 | int err; |
Eric Dumazet | ae40eb1 | 2007-03-18 17:33:16 -0700 | [diff] [blame] | 457 | struct timespec ts; |
| 458 | |
H. J. Lu | ee4fa23 | 2012-02-19 17:50:46 -0800 | [diff] [blame] | 459 | if (COMPAT_USE_64BIT_TIME) |
| 460 | return sock_get_timestampns (sk, userstamp); |
| 461 | |
| 462 | ctv = (struct compat_timespec __user *) userstamp; |
| 463 | err = -ENOENT; |
Eric Dumazet | ae40eb1 | 2007-03-18 17:33:16 -0700 | [diff] [blame] | 464 | if (!sock_flag(sk, SOCK_TIMESTAMP)) |
Patrick Ohly | 20d4947 | 2009-02-12 05:03:38 +0000 | [diff] [blame] | 465 | sock_enable_timestamp(sk, SOCK_TIMESTAMP); |
Eric Dumazet | ae40eb1 | 2007-03-18 17:33:16 -0700 | [diff] [blame] | 466 | ts = ktime_to_timespec(sk->sk_stamp); |
| 467 | if (ts.tv_sec == -1) |
| 468 | return err; |
| 469 | if (ts.tv_sec == 0) { |
| 470 | sk->sk_stamp = ktime_get_real(); |
| 471 | ts = ktime_to_timespec(sk->sk_stamp); |
| 472 | } |
| 473 | err = 0; |
| 474 | if (put_user(ts.tv_sec, &ctv->tv_sec) || |
| 475 | put_user(ts.tv_nsec, &ctv->tv_nsec)) |
| 476 | err = -EFAULT; |
| 477 | return err; |
| 478 | } |
| 479 | EXPORT_SYMBOL(compat_sock_get_timestampns); |
| 480 | |
Heiko Carstens | 361d93c | 2014-03-03 16:21:27 +0100 | [diff] [blame] | 481 | COMPAT_SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname, |
| 482 | char __user *, optval, int __user *, optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | { |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 484 | int err; |
Eric Dumazet | c6d409c | 2010-06-03 20:03:40 -0700 | [diff] [blame] | 485 | struct socket *sock = sockfd_lookup(fd, &err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | |
Eric Dumazet | c6d409c | 2010-06-03 20:03:40 -0700 | [diff] [blame] | 487 | if (sock) { |
| 488 | err = security_socket_getsockopt(sock, level, optname); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 489 | if (err) { |
| 490 | sockfd_put(sock); |
| 491 | return err; |
| 492 | } |
| 493 | |
| 494 | if (level == SOL_SOCKET) |
| 495 | err = compat_sock_getsockopt(sock, level, |
| 496 | optname, optval, optlen); |
| 497 | else if (sock->ops->compat_getsockopt) |
| 498 | err = sock->ops->compat_getsockopt(sock, level, |
| 499 | optname, optval, optlen); |
| 500 | else |
| 501 | err = sock->ops->getsockopt(sock, level, |
| 502 | optname, optval, optlen); |
| 503 | sockfd_put(sock); |
| 504 | } |
| 505 | return err; |
| 506 | } |
David L Stevens | dae5029 | 2008-04-27 01:06:07 -0700 | [diff] [blame] | 507 | |
| 508 | struct compat_group_req { |
| 509 | __u32 gr_interface; |
| 510 | struct __kernel_sockaddr_storage gr_group |
| 511 | __attribute__ ((aligned(4))); |
Eric Dumazet | bc10502 | 2010-06-03 03:21:52 -0700 | [diff] [blame] | 512 | } __packed; |
David L Stevens | dae5029 | 2008-04-27 01:06:07 -0700 | [diff] [blame] | 513 | |
| 514 | struct compat_group_source_req { |
| 515 | __u32 gsr_interface; |
| 516 | struct __kernel_sockaddr_storage gsr_group |
| 517 | __attribute__ ((aligned(4))); |
| 518 | struct __kernel_sockaddr_storage gsr_source |
| 519 | __attribute__ ((aligned(4))); |
Eric Dumazet | bc10502 | 2010-06-03 03:21:52 -0700 | [diff] [blame] | 520 | } __packed; |
David L Stevens | dae5029 | 2008-04-27 01:06:07 -0700 | [diff] [blame] | 521 | |
| 522 | struct compat_group_filter { |
| 523 | __u32 gf_interface; |
| 524 | struct __kernel_sockaddr_storage gf_group |
| 525 | __attribute__ ((aligned(4))); |
| 526 | __u32 gf_fmode; |
| 527 | __u32 gf_numsrc; |
| 528 | struct __kernel_sockaddr_storage gf_slist[1] |
| 529 | __attribute__ ((aligned(4))); |
Eric Dumazet | bc10502 | 2010-06-03 03:21:52 -0700 | [diff] [blame] | 530 | } __packed; |
David L Stevens | dae5029 | 2008-04-27 01:06:07 -0700 | [diff] [blame] | 531 | |
David L Stevens | be666e0 | 2008-04-29 03:23:00 -0700 | [diff] [blame] | 532 | #define __COMPAT_GF0_SIZE (sizeof(struct compat_group_filter) - \ |
| 533 | sizeof(struct __kernel_sockaddr_storage)) |
| 534 | |
David L Stevens | dae5029 | 2008-04-27 01:06:07 -0700 | [diff] [blame] | 535 | |
| 536 | int compat_mc_setsockopt(struct sock *sock, int level, int optname, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 537 | char __user *optval, unsigned int optlen, |
Eric Dumazet | c6d409c | 2010-06-03 20:03:40 -0700 | [diff] [blame] | 538 | int (*setsockopt)(struct sock *, int, int, char __user *, unsigned int)) |
David L Stevens | dae5029 | 2008-04-27 01:06:07 -0700 | [diff] [blame] | 539 | { |
| 540 | char __user *koptval = optval; |
| 541 | int koptlen = optlen; |
| 542 | |
| 543 | switch (optname) { |
| 544 | case MCAST_JOIN_GROUP: |
| 545 | case MCAST_LEAVE_GROUP: |
| 546 | { |
| 547 | struct compat_group_req __user *gr32 = (void *)optval; |
| 548 | struct group_req __user *kgr = |
| 549 | compat_alloc_user_space(sizeof(struct group_req)); |
| 550 | u32 interface; |
| 551 | |
| 552 | if (!access_ok(VERIFY_READ, gr32, sizeof(*gr32)) || |
| 553 | !access_ok(VERIFY_WRITE, kgr, sizeof(struct group_req)) || |
| 554 | __get_user(interface, &gr32->gr_interface) || |
| 555 | __put_user(interface, &kgr->gr_interface) || |
| 556 | copy_in_user(&kgr->gr_group, &gr32->gr_group, |
| 557 | sizeof(kgr->gr_group))) |
| 558 | return -EFAULT; |
| 559 | koptval = (char __user *)kgr; |
| 560 | koptlen = sizeof(struct group_req); |
| 561 | break; |
| 562 | } |
| 563 | case MCAST_JOIN_SOURCE_GROUP: |
| 564 | case MCAST_LEAVE_SOURCE_GROUP: |
| 565 | case MCAST_BLOCK_SOURCE: |
| 566 | case MCAST_UNBLOCK_SOURCE: |
| 567 | { |
| 568 | struct compat_group_source_req __user *gsr32 = (void *)optval; |
David L Stevens | be666e0 | 2008-04-29 03:23:00 -0700 | [diff] [blame] | 569 | struct group_source_req __user *kgsr = compat_alloc_user_space( |
David L Stevens | dae5029 | 2008-04-27 01:06:07 -0700 | [diff] [blame] | 570 | sizeof(struct group_source_req)); |
| 571 | u32 interface; |
| 572 | |
| 573 | if (!access_ok(VERIFY_READ, gsr32, sizeof(*gsr32)) || |
| 574 | !access_ok(VERIFY_WRITE, kgsr, |
| 575 | sizeof(struct group_source_req)) || |
| 576 | __get_user(interface, &gsr32->gsr_interface) || |
| 577 | __put_user(interface, &kgsr->gsr_interface) || |
| 578 | copy_in_user(&kgsr->gsr_group, &gsr32->gsr_group, |
| 579 | sizeof(kgsr->gsr_group)) || |
| 580 | copy_in_user(&kgsr->gsr_source, &gsr32->gsr_source, |
| 581 | sizeof(kgsr->gsr_source))) |
| 582 | return -EFAULT; |
| 583 | koptval = (char __user *)kgsr; |
| 584 | koptlen = sizeof(struct group_source_req); |
| 585 | break; |
| 586 | } |
| 587 | case MCAST_MSFILTER: |
| 588 | { |
| 589 | struct compat_group_filter __user *gf32 = (void *)optval; |
David L Stevens | be666e0 | 2008-04-29 03:23:00 -0700 | [diff] [blame] | 590 | struct group_filter __user *kgf; |
David L Stevens | dae5029 | 2008-04-27 01:06:07 -0700 | [diff] [blame] | 591 | u32 interface, fmode, numsrc; |
| 592 | |
David L Stevens | be666e0 | 2008-04-29 03:23:00 -0700 | [diff] [blame] | 593 | if (!access_ok(VERIFY_READ, gf32, __COMPAT_GF0_SIZE) || |
David L Stevens | dae5029 | 2008-04-27 01:06:07 -0700 | [diff] [blame] | 594 | __get_user(interface, &gf32->gf_interface) || |
| 595 | __get_user(fmode, &gf32->gf_fmode) || |
| 596 | __get_user(numsrc, &gf32->gf_numsrc)) |
| 597 | return -EFAULT; |
| 598 | koptlen = optlen + sizeof(struct group_filter) - |
| 599 | sizeof(struct compat_group_filter); |
| 600 | if (koptlen < GROUP_FILTER_SIZE(numsrc)) |
| 601 | return -EINVAL; |
| 602 | kgf = compat_alloc_user_space(koptlen); |
| 603 | if (!access_ok(VERIFY_WRITE, kgf, koptlen) || |
| 604 | __put_user(interface, &kgf->gf_interface) || |
| 605 | __put_user(fmode, &kgf->gf_fmode) || |
| 606 | __put_user(numsrc, &kgf->gf_numsrc) || |
| 607 | copy_in_user(&kgf->gf_group, &gf32->gf_group, |
| 608 | sizeof(kgf->gf_group)) || |
David L Stevens | be666e0 | 2008-04-29 03:23:00 -0700 | [diff] [blame] | 609 | (numsrc && copy_in_user(kgf->gf_slist, gf32->gf_slist, |
David L Stevens | dae5029 | 2008-04-27 01:06:07 -0700 | [diff] [blame] | 610 | numsrc * sizeof(kgf->gf_slist[0])))) |
| 611 | return -EFAULT; |
| 612 | koptval = (char __user *)kgf; |
| 613 | break; |
| 614 | } |
| 615 | |
| 616 | default: |
| 617 | break; |
| 618 | } |
| 619 | return setsockopt(sock, level, optname, koptval, koptlen); |
| 620 | } |
David L Stevens | dae5029 | 2008-04-27 01:06:07 -0700 | [diff] [blame] | 621 | EXPORT_SYMBOL(compat_mc_setsockopt); |
| 622 | |
David L Stevens | 42908c6 | 2008-04-29 03:23:22 -0700 | [diff] [blame] | 623 | int compat_mc_getsockopt(struct sock *sock, int level, int optname, |
| 624 | char __user *optval, int __user *optlen, |
Eric Dumazet | c6d409c | 2010-06-03 20:03:40 -0700 | [diff] [blame] | 625 | int (*getsockopt)(struct sock *, int, int, char __user *, int __user *)) |
David L Stevens | 42908c6 | 2008-04-29 03:23:22 -0700 | [diff] [blame] | 626 | { |
| 627 | struct compat_group_filter __user *gf32 = (void *)optval; |
| 628 | struct group_filter __user *kgf; |
| 629 | int __user *koptlen; |
| 630 | u32 interface, fmode, numsrc; |
| 631 | int klen, ulen, err; |
| 632 | |
| 633 | if (optname != MCAST_MSFILTER) |
| 634 | return getsockopt(sock, level, optname, optval, optlen); |
| 635 | |
| 636 | koptlen = compat_alloc_user_space(sizeof(*koptlen)); |
| 637 | if (!access_ok(VERIFY_READ, optlen, sizeof(*optlen)) || |
| 638 | __get_user(ulen, optlen)) |
| 639 | return -EFAULT; |
| 640 | |
| 641 | /* adjust len for pad */ |
| 642 | klen = ulen + sizeof(*kgf) - sizeof(*gf32); |
| 643 | |
| 644 | if (klen < GROUP_FILTER_SIZE(0)) |
| 645 | return -EINVAL; |
| 646 | |
| 647 | if (!access_ok(VERIFY_WRITE, koptlen, sizeof(*koptlen)) || |
| 648 | __put_user(klen, koptlen)) |
| 649 | return -EFAULT; |
| 650 | |
| 651 | /* have to allow space for previous compat_alloc_user_space, too */ |
| 652 | kgf = compat_alloc_user_space(klen+sizeof(*optlen)); |
| 653 | |
| 654 | if (!access_ok(VERIFY_READ, gf32, __COMPAT_GF0_SIZE) || |
| 655 | __get_user(interface, &gf32->gf_interface) || |
| 656 | __get_user(fmode, &gf32->gf_fmode) || |
| 657 | __get_user(numsrc, &gf32->gf_numsrc) || |
| 658 | __put_user(interface, &kgf->gf_interface) || |
| 659 | __put_user(fmode, &kgf->gf_fmode) || |
| 660 | __put_user(numsrc, &kgf->gf_numsrc) || |
Eric Dumazet | c6d409c | 2010-06-03 20:03:40 -0700 | [diff] [blame] | 661 | copy_in_user(&kgf->gf_group, &gf32->gf_group, sizeof(kgf->gf_group))) |
David L Stevens | 42908c6 | 2008-04-29 03:23:22 -0700 | [diff] [blame] | 662 | return -EFAULT; |
| 663 | |
| 664 | err = getsockopt(sock, level, optname, (char __user *)kgf, koptlen); |
| 665 | if (err) |
| 666 | return err; |
| 667 | |
| 668 | if (!access_ok(VERIFY_READ, koptlen, sizeof(*koptlen)) || |
| 669 | __get_user(klen, koptlen)) |
| 670 | return -EFAULT; |
| 671 | |
| 672 | ulen = klen - (sizeof(*kgf)-sizeof(*gf32)); |
| 673 | |
| 674 | if (!access_ok(VERIFY_WRITE, optlen, sizeof(*optlen)) || |
| 675 | __put_user(ulen, optlen)) |
| 676 | return -EFAULT; |
| 677 | |
| 678 | if (!access_ok(VERIFY_READ, kgf, klen) || |
| 679 | !access_ok(VERIFY_WRITE, gf32, ulen) || |
| 680 | __get_user(interface, &kgf->gf_interface) || |
| 681 | __get_user(fmode, &kgf->gf_fmode) || |
| 682 | __get_user(numsrc, &kgf->gf_numsrc) || |
| 683 | __put_user(interface, &gf32->gf_interface) || |
| 684 | __put_user(fmode, &gf32->gf_fmode) || |
| 685 | __put_user(numsrc, &gf32->gf_numsrc)) |
| 686 | return -EFAULT; |
| 687 | if (numsrc) { |
| 688 | int copylen; |
| 689 | |
| 690 | klen -= GROUP_FILTER_SIZE(0); |
| 691 | copylen = numsrc * sizeof(gf32->gf_slist[0]); |
| 692 | if (copylen > klen) |
| 693 | copylen = klen; |
Eric Dumazet | c6d409c | 2010-06-03 20:03:40 -0700 | [diff] [blame] | 694 | if (copy_in_user(gf32->gf_slist, kgf->gf_slist, copylen)) |
David L Stevens | 42908c6 | 2008-04-29 03:23:22 -0700 | [diff] [blame] | 695 | return -EFAULT; |
| 696 | } |
| 697 | return err; |
| 698 | } |
David L Stevens | 42908c6 | 2008-04-29 03:23:22 -0700 | [diff] [blame] | 699 | EXPORT_SYMBOL(compat_mc_getsockopt); |
| 700 | |
David L Stevens | dae5029 | 2008-04-27 01:06:07 -0700 | [diff] [blame] | 701 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | /* Argument list sizes for compat_sys_socketcall */ |
| 703 | #define AL(x) ((x) * sizeof(u32)) |
Anton Blanchard | 228e548 | 2011-05-02 20:21:35 +0000 | [diff] [blame] | 704 | static unsigned char nas[21] = { |
Eric Dumazet | c6d409c | 2010-06-03 20:03:40 -0700 | [diff] [blame] | 705 | AL(0), AL(3), AL(3), AL(3), AL(2), AL(3), |
| 706 | AL(3), AL(3), AL(4), AL(4), AL(4), AL(6), |
| 707 | AL(6), AL(2), AL(5), AL(5), AL(3), AL(3), |
Anton Blanchard | 228e548 | 2011-05-02 20:21:35 +0000 | [diff] [blame] | 708 | AL(4), AL(5), AL(4) |
Eric Dumazet | c6d409c | 2010-06-03 20:03:40 -0700 | [diff] [blame] | 709 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | #undef AL |
| 711 | |
Heiko Carstens | 361d93c | 2014-03-03 16:21:27 +0100 | [diff] [blame] | 712 | COMPAT_SYSCALL_DEFINE3(sendmsg, int, fd, struct compat_msghdr __user *, msg, unsigned int, flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | { |
Andy Lutomirski | a7526eb | 2013-06-05 19:38:26 +0000 | [diff] [blame] | 714 | if (flags & MSG_CMSG_COMPAT) |
| 715 | return -EINVAL; |
Al Viro | 666547f | 2014-04-06 14:03:05 -0400 | [diff] [blame] | 716 | return __sys_sendmsg(fd, (struct user_msghdr __user *)msg, flags | MSG_CMSG_COMPAT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Heiko Carstens | 361d93c | 2014-03-03 16:21:27 +0100 | [diff] [blame] | 719 | COMPAT_SYSCALL_DEFINE4(sendmmsg, int, fd, struct compat_mmsghdr __user *, mmsg, |
| 720 | unsigned int, vlen, unsigned int, flags) |
Anton Blanchard | 228e548 | 2011-05-02 20:21:35 +0000 | [diff] [blame] | 721 | { |
Andy Lutomirski | a7526eb | 2013-06-05 19:38:26 +0000 | [diff] [blame] | 722 | if (flags & MSG_CMSG_COMPAT) |
| 723 | return -EINVAL; |
Anton Blanchard | 228e548 | 2011-05-02 20:21:35 +0000 | [diff] [blame] | 724 | return __sys_sendmmsg(fd, (struct mmsghdr __user *)mmsg, vlen, |
| 725 | flags | MSG_CMSG_COMPAT); |
| 726 | } |
| 727 | |
Heiko Carstens | 361d93c | 2014-03-03 16:21:27 +0100 | [diff] [blame] | 728 | COMPAT_SYSCALL_DEFINE3(recvmsg, int, fd, struct compat_msghdr __user *, msg, unsigned int, flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | { |
Andy Lutomirski | a7526eb | 2013-06-05 19:38:26 +0000 | [diff] [blame] | 730 | if (flags & MSG_CMSG_COMPAT) |
| 731 | return -EINVAL; |
Al Viro | 666547f | 2014-04-06 14:03:05 -0400 | [diff] [blame] | 732 | return __sys_recvmsg(fd, (struct user_msghdr __user *)msg, flags | MSG_CMSG_COMPAT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | } |
| 734 | |
Heiko Carstens | 3a49a0f | 2014-03-04 17:09:57 +0100 | [diff] [blame] | 735 | 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] | 736 | { |
| 737 | return sys_recv(fd, buf, len, flags | MSG_CMSG_COMPAT); |
| 738 | } |
| 739 | |
Heiko Carstens | 3a49a0f | 2014-03-04 17:09:57 +0100 | [diff] [blame] | 740 | COMPAT_SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, buf, compat_size_t, len, |
| 741 | unsigned int, flags, struct sockaddr __user *, addr, |
| 742 | int __user *, addrlen) |
Johannes Berg | 1dacc76 | 2009-07-01 11:26:02 +0000 | [diff] [blame] | 743 | { |
| 744 | return sys_recvfrom(fd, buf, len, flags | MSG_CMSG_COMPAT, addr, addrlen); |
| 745 | } |
| 746 | |
Heiko Carstens | 361d93c | 2014-03-03 16:21:27 +0100 | [diff] [blame] | 747 | COMPAT_SYSCALL_DEFINE5(recvmmsg, int, fd, struct compat_mmsghdr __user *, mmsg, |
| 748 | unsigned int, vlen, unsigned int, flags, |
| 749 | struct compat_timespec __user *, timeout) |
Arnaldo Carvalho de Melo | a2e2725 | 2009-10-12 23:40:10 -0700 | [diff] [blame] | 750 | { |
| 751 | int datagrams; |
| 752 | struct timespec ktspec; |
Arnaldo Carvalho de Melo | a2e2725 | 2009-10-12 23:40:10 -0700 | [diff] [blame] | 753 | |
Andy Lutomirski | a7526eb | 2013-06-05 19:38:26 +0000 | [diff] [blame] | 754 | if (flags & MSG_CMSG_COMPAT) |
| 755 | return -EINVAL; |
| 756 | |
Jean-Mickael Guerin | 5b23136 | 2009-12-01 07:52:16 +0000 | [diff] [blame] | 757 | if (timeout == NULL) |
| 758 | return __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen, |
| 759 | flags | MSG_CMSG_COMPAT, NULL); |
| 760 | |
PaX Team | 2def2ef | 2014-01-30 16:59:25 -0800 | [diff] [blame] | 761 | if (compat_get_timespec(&ktspec, timeout)) |
Arnaldo Carvalho de Melo | a2e2725 | 2009-10-12 23:40:10 -0700 | [diff] [blame] | 762 | return -EFAULT; |
| 763 | |
| 764 | datagrams = __sys_recvmmsg(fd, (struct mmsghdr __user *)mmsg, vlen, |
| 765 | flags | MSG_CMSG_COMPAT, &ktspec); |
PaX Team | 2def2ef | 2014-01-30 16:59:25 -0800 | [diff] [blame] | 766 | if (datagrams > 0 && compat_put_timespec(&ktspec, timeout)) |
Arnaldo Carvalho de Melo | a2e2725 | 2009-10-12 23:40:10 -0700 | [diff] [blame] | 767 | datagrams = -EFAULT; |
| 768 | |
| 769 | return datagrams; |
| 770 | } |
| 771 | |
Heiko Carstens | 361d93c | 2014-03-03 16:21:27 +0100 | [diff] [blame] | 772 | COMPAT_SYSCALL_DEFINE2(socketcall, int, call, u32 __user *, args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | { |
| 774 | int ret; |
| 775 | u32 a[6]; |
| 776 | u32 a0, a1; |
YOSHIFUJI Hideaki | 4768fbc | 2007-02-09 23:25:31 +0900 | [diff] [blame] | 777 | |
Anton Blanchard | 228e548 | 2011-05-02 20:21:35 +0000 | [diff] [blame] | 778 | if (call < SYS_SOCKET || call > SYS_SENDMMSG) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | return -EINVAL; |
| 780 | if (copy_from_user(a, args, nas[call])) |
| 781 | return -EFAULT; |
| 782 | a0 = a[0]; |
| 783 | a1 = a[1]; |
YOSHIFUJI Hideaki | 4768fbc | 2007-02-09 23:25:31 +0900 | [diff] [blame] | 784 | |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 785 | switch (call) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | case SYS_SOCKET: |
| 787 | ret = sys_socket(a0, a1, a[2]); |
| 788 | break; |
| 789 | case SYS_BIND: |
| 790 | ret = sys_bind(a0, compat_ptr(a1), a[2]); |
| 791 | break; |
| 792 | case SYS_CONNECT: |
| 793 | ret = sys_connect(a0, compat_ptr(a1), a[2]); |
| 794 | break; |
| 795 | case SYS_LISTEN: |
| 796 | ret = sys_listen(a0, a1); |
| 797 | break; |
| 798 | case SYS_ACCEPT: |
Ulrich Drepper | de11def | 2008-11-19 15:36:14 -0800 | [diff] [blame] | 799 | ret = sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | break; |
| 801 | case SYS_GETSOCKNAME: |
| 802 | ret = sys_getsockname(a0, compat_ptr(a1), compat_ptr(a[2])); |
| 803 | break; |
| 804 | case SYS_GETPEERNAME: |
| 805 | ret = sys_getpeername(a0, compat_ptr(a1), compat_ptr(a[2])); |
| 806 | break; |
| 807 | case SYS_SOCKETPAIR: |
| 808 | ret = sys_socketpair(a0, a1, a[2], compat_ptr(a[3])); |
| 809 | break; |
| 810 | case SYS_SEND: |
| 811 | ret = sys_send(a0, compat_ptr(a1), a[2], a[3]); |
| 812 | break; |
| 813 | case SYS_SENDTO: |
| 814 | ret = sys_sendto(a0, compat_ptr(a1), a[2], a[3], compat_ptr(a[4]), a[5]); |
| 815 | break; |
| 816 | case SYS_RECV: |
Johannes Berg | 1dacc76 | 2009-07-01 11:26:02 +0000 | [diff] [blame] | 817 | ret = compat_sys_recv(a0, compat_ptr(a1), a[2], a[3]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | break; |
| 819 | case SYS_RECVFROM: |
Johannes Berg | 1dacc76 | 2009-07-01 11:26:02 +0000 | [diff] [blame] | 820 | ret = compat_sys_recvfrom(a0, compat_ptr(a1), a[2], a[3], |
| 821 | compat_ptr(a[4]), compat_ptr(a[5])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | break; |
| 823 | case SYS_SHUTDOWN: |
Eric Dumazet | c6d409c | 2010-06-03 20:03:40 -0700 | [diff] [blame] | 824 | ret = sys_shutdown(a0, a1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | break; |
| 826 | case SYS_SETSOCKOPT: |
| 827 | ret = compat_sys_setsockopt(a0, a1, a[2], |
| 828 | compat_ptr(a[3]), a[4]); |
| 829 | break; |
| 830 | case SYS_GETSOCKOPT: |
| 831 | ret = compat_sys_getsockopt(a0, a1, a[2], |
| 832 | compat_ptr(a[3]), compat_ptr(a[4])); |
| 833 | break; |
| 834 | case SYS_SENDMSG: |
| 835 | ret = compat_sys_sendmsg(a0, compat_ptr(a1), a[2]); |
| 836 | break; |
Anton Blanchard | 228e548 | 2011-05-02 20:21:35 +0000 | [diff] [blame] | 837 | case SYS_SENDMMSG: |
| 838 | ret = compat_sys_sendmmsg(a0, compat_ptr(a1), a[2], a[3]); |
| 839 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | case SYS_RECVMSG: |
| 841 | ret = compat_sys_recvmsg(a0, compat_ptr(a1), a[2]); |
| 842 | break; |
Arnaldo Carvalho de Melo | a2e2725 | 2009-10-12 23:40:10 -0700 | [diff] [blame] | 843 | case SYS_RECVMMSG: |
| 844 | ret = compat_sys_recvmmsg(a0, compat_ptr(a1), a[2], a[3], |
| 845 | compat_ptr(a[4])); |
| 846 | break; |
Ulrich Drepper | de11def | 2008-11-19 15:36:14 -0800 | [diff] [blame] | 847 | case SYS_ACCEPT4: |
| 848 | 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] | 849 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | default: |
| 851 | ret = -EINVAL; |
| 852 | break; |
| 853 | } |
| 854 | return ret; |
| 855 | } |