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> |
| 15 | #include <linux/fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/types.h> |
| 17 | #include <linux/file.h> |
| 18 | #include <linux/icmpv6.h> |
| 19 | #include <linux/socket.h> |
| 20 | #include <linux/syscalls.h> |
| 21 | #include <linux/filter.h> |
| 22 | #include <linux/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/security.h> |
| 24 | |
| 25 | #include <net/scm.h> |
| 26 | #include <net/sock.h> |
| 27 | #include <asm/uaccess.h> |
| 28 | #include <net/compat.h> |
| 29 | |
| 30 | static inline int iov_from_user_compat_to_kern(struct iovec *kiov, |
| 31 | struct compat_iovec __user *uiov32, |
| 32 | int niov) |
| 33 | { |
| 34 | int tot_len = 0; |
| 35 | |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 36 | while (niov > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | compat_uptr_t buf; |
| 38 | compat_size_t len; |
| 39 | |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 40 | if (get_user(len, &uiov32->iov_len) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | get_user(buf, &uiov32->iov_base)) { |
| 42 | tot_len = -EFAULT; |
| 43 | break; |
| 44 | } |
| 45 | tot_len += len; |
| 46 | kiov->iov_base = compat_ptr(buf); |
| 47 | kiov->iov_len = (__kernel_size_t) len; |
| 48 | uiov32++; |
| 49 | kiov++; |
| 50 | niov--; |
| 51 | } |
| 52 | return tot_len; |
| 53 | } |
| 54 | |
| 55 | int get_compat_msghdr(struct msghdr *kmsg, struct compat_msghdr __user *umsg) |
| 56 | { |
| 57 | compat_uptr_t tmp1, tmp2, tmp3; |
| 58 | |
| 59 | if (!access_ok(VERIFY_READ, umsg, sizeof(*umsg)) || |
| 60 | __get_user(tmp1, &umsg->msg_name) || |
| 61 | __get_user(kmsg->msg_namelen, &umsg->msg_namelen) || |
| 62 | __get_user(tmp2, &umsg->msg_iov) || |
| 63 | __get_user(kmsg->msg_iovlen, &umsg->msg_iovlen) || |
| 64 | __get_user(tmp3, &umsg->msg_control) || |
| 65 | __get_user(kmsg->msg_controllen, &umsg->msg_controllen) || |
| 66 | __get_user(kmsg->msg_flags, &umsg->msg_flags)) |
| 67 | return -EFAULT; |
| 68 | kmsg->msg_name = compat_ptr(tmp1); |
| 69 | kmsg->msg_iov = compat_ptr(tmp2); |
| 70 | kmsg->msg_control = compat_ptr(tmp3); |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | /* I've named the args so it is easy to tell whose space the pointers are in. */ |
| 75 | int verify_compat_iovec(struct msghdr *kern_msg, struct iovec *kern_iov, |
| 76 | char *kern_address, int mode) |
| 77 | { |
| 78 | int tot_len; |
| 79 | |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 80 | if (kern_msg->msg_namelen) { |
| 81 | if (mode==VERIFY_READ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | int err = move_addr_to_kernel(kern_msg->msg_name, |
| 83 | kern_msg->msg_namelen, |
| 84 | kern_address); |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 85 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | return err; |
| 87 | } |
| 88 | kern_msg->msg_name = kern_address; |
| 89 | } else |
| 90 | kern_msg->msg_name = NULL; |
| 91 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | tot_len = iov_from_user_compat_to_kern(kern_iov, |
| 93 | (struct compat_iovec __user *)kern_msg->msg_iov, |
| 94 | kern_msg->msg_iovlen); |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 95 | if (tot_len >= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | kern_msg->msg_iov = kern_iov; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
| 98 | return tot_len; |
| 99 | } |
| 100 | |
| 101 | /* Bleech... */ |
| 102 | #define CMSG_COMPAT_ALIGN(len) ALIGN((len), sizeof(s32)) |
| 103 | |
| 104 | #define CMSG_COMPAT_DATA(cmsg) \ |
| 105 | ((void __user *)((char __user *)(cmsg) + CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)))) |
| 106 | #define CMSG_COMPAT_SPACE(len) \ |
| 107 | (CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)) + CMSG_COMPAT_ALIGN(len)) |
| 108 | #define CMSG_COMPAT_LEN(len) \ |
| 109 | (CMSG_COMPAT_ALIGN(sizeof(struct compat_cmsghdr)) + (len)) |
| 110 | |
| 111 | #define CMSG_COMPAT_FIRSTHDR(msg) \ |
| 112 | (((msg)->msg_controllen) >= sizeof(struct compat_cmsghdr) ? \ |
| 113 | (struct compat_cmsghdr __user *)((msg)->msg_control) : \ |
| 114 | (struct compat_cmsghdr __user *)NULL) |
| 115 | |
| 116 | #define CMSG_COMPAT_OK(ucmlen, ucmsg, mhdr) \ |
| 117 | ((ucmlen) >= sizeof(struct compat_cmsghdr) && \ |
| 118 | (ucmlen) <= (unsigned long) \ |
| 119 | ((mhdr)->msg_controllen - \ |
| 120 | ((char *)(ucmsg) - (char *)(mhdr)->msg_control))) |
| 121 | |
| 122 | static inline struct compat_cmsghdr __user *cmsg_compat_nxthdr(struct msghdr *msg, |
| 123 | struct compat_cmsghdr __user *cmsg, int cmsg_len) |
| 124 | { |
| 125 | char __user *ptr = (char __user *)cmsg + CMSG_COMPAT_ALIGN(cmsg_len); |
| 126 | if ((unsigned long)(ptr + 1 - (char __user *)msg->msg_control) > |
| 127 | msg->msg_controllen) |
| 128 | return NULL; |
| 129 | return (struct compat_cmsghdr __user *)ptr; |
| 130 | } |
| 131 | |
| 132 | /* There is a lot of hair here because the alignment rules (and |
| 133 | * thus placement) of cmsg headers and length are different for |
| 134 | * 32-bit apps. -DaveM |
| 135 | */ |
Al Viro | 8920e8f | 2005-09-07 18:28:51 -0700 | [diff] [blame] | 136 | 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] | 137 | unsigned char *stackbuf, int stackbuf_size) |
| 138 | { |
| 139 | struct compat_cmsghdr __user *ucmsg; |
| 140 | struct cmsghdr *kcmsg, *kcmsg_base; |
| 141 | compat_size_t ucmlen; |
| 142 | __kernel_size_t kcmlen, tmp; |
Al Viro | 8920e8f | 2005-09-07 18:28:51 -0700 | [diff] [blame] | 143 | int err = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
| 145 | kcmlen = 0; |
| 146 | kcmsg_base = kcmsg = (struct cmsghdr *)stackbuf; |
| 147 | ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg); |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 148 | while (ucmsg != NULL) { |
| 149 | if (get_user(ucmlen, &ucmsg->cmsg_len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | return -EFAULT; |
| 151 | |
| 152 | /* Catch bogons. */ |
| 153 | if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg)) |
| 154 | return -EINVAL; |
| 155 | |
| 156 | tmp = ((ucmlen - CMSG_COMPAT_ALIGN(sizeof(*ucmsg))) + |
| 157 | CMSG_ALIGN(sizeof(struct cmsghdr))); |
Al Viro | 8920e8f | 2005-09-07 18:28:51 -0700 | [diff] [blame] | 158 | tmp = CMSG_ALIGN(tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | kcmlen += tmp; |
| 160 | ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen); |
| 161 | } |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 162 | if (kcmlen == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | return -EINVAL; |
| 164 | |
| 165 | /* The kcmlen holds the 64-bit version of the control length. |
| 166 | * It may not be modified as we do not stick it into the kmsg |
| 167 | * until we have successfully copied over all of the data |
| 168 | * from the user. |
| 169 | */ |
Al Viro | 8920e8f | 2005-09-07 18:28:51 -0700 | [diff] [blame] | 170 | if (kcmlen > stackbuf_size) |
| 171 | kcmsg_base = kcmsg = sock_kmalloc(sk, kcmlen, GFP_KERNEL); |
| 172 | if (kcmsg == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | return -ENOBUFS; |
| 174 | |
| 175 | /* Now copy them over neatly. */ |
| 176 | memset(kcmsg, 0, kcmlen); |
| 177 | ucmsg = CMSG_COMPAT_FIRSTHDR(kmsg); |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 178 | while (ucmsg != NULL) { |
Al Viro | 8920e8f | 2005-09-07 18:28:51 -0700 | [diff] [blame] | 179 | if (__get_user(ucmlen, &ucmsg->cmsg_len)) |
| 180 | goto Efault; |
| 181 | if (!CMSG_COMPAT_OK(ucmlen, ucmsg, kmsg)) |
| 182 | goto Einval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | tmp = ((ucmlen - CMSG_COMPAT_ALIGN(sizeof(*ucmsg))) + |
| 184 | CMSG_ALIGN(sizeof(struct cmsghdr))); |
Al Viro | 8920e8f | 2005-09-07 18:28:51 -0700 | [diff] [blame] | 185 | if ((char *)kcmsg_base + kcmlen - (char *)kcmsg < CMSG_ALIGN(tmp)) |
| 186 | goto Einval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | kcmsg->cmsg_len = tmp; |
Al Viro | 8920e8f | 2005-09-07 18:28:51 -0700 | [diff] [blame] | 188 | tmp = CMSG_ALIGN(tmp); |
| 189 | if (__get_user(kcmsg->cmsg_level, &ucmsg->cmsg_level) || |
| 190 | __get_user(kcmsg->cmsg_type, &ucmsg->cmsg_type) || |
| 191 | copy_from_user(CMSG_DATA(kcmsg), |
| 192 | CMSG_COMPAT_DATA(ucmsg), |
| 193 | (ucmlen - CMSG_COMPAT_ALIGN(sizeof(*ucmsg))))) |
| 194 | goto Efault; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
| 196 | /* Advance. */ |
Al Viro | 8920e8f | 2005-09-07 18:28:51 -0700 | [diff] [blame] | 197 | kcmsg = (struct cmsghdr *)((char *)kcmsg + tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | ucmsg = cmsg_compat_nxthdr(kmsg, ucmsg, ucmlen); |
| 199 | } |
| 200 | |
| 201 | /* Ok, looks like we made it. Hook it up and return success. */ |
| 202 | kmsg->msg_control = kcmsg_base; |
| 203 | kmsg->msg_controllen = kcmlen; |
| 204 | return 0; |
| 205 | |
Al Viro | 8920e8f | 2005-09-07 18:28:51 -0700 | [diff] [blame] | 206 | Einval: |
| 207 | err = -EINVAL; |
| 208 | Efault: |
| 209 | if (kcmsg_base != (struct cmsghdr *)stackbuf) |
| 210 | sock_kfree_s(sk, kcmsg_base, kcmlen); |
| 211 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | int put_cmsg_compat(struct msghdr *kmsg, int level, int type, int len, void *data) |
| 215 | { |
| 216 | struct compat_timeval ctv; |
Eric Dumazet | 92f37fd | 2007-03-25 22:14:49 -0700 | [diff] [blame] | 217 | struct compat_timespec cts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | struct compat_cmsghdr __user *cm = (struct compat_cmsghdr __user *) kmsg->msg_control; |
| 219 | struct compat_cmsghdr cmhdr; |
| 220 | int cmlen; |
| 221 | |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 222 | if (cm == NULL || kmsg->msg_controllen < sizeof(*cm)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | kmsg->msg_flags |= MSG_CTRUNC; |
| 224 | return 0; /* XXX: return error? check spec. */ |
| 225 | } |
| 226 | |
YOSHIFUJI Hideaki | 4768fbc | 2007-02-09 23:25:31 +0900 | [diff] [blame] | 227 | if (level == SOL_SOCKET && type == SO_TIMESTAMP) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | struct timeval *tv = (struct timeval *)data; |
| 229 | ctv.tv_sec = tv->tv_sec; |
| 230 | ctv.tv_usec = tv->tv_usec; |
| 231 | data = &ctv; |
Eric Dumazet | 92f37fd | 2007-03-25 22:14:49 -0700 | [diff] [blame] | 232 | len = sizeof(ctv); |
| 233 | } |
| 234 | if (level == SOL_SOCKET && type == SO_TIMESTAMPNS) { |
| 235 | struct timespec *ts = (struct timespec *)data; |
| 236 | cts.tv_sec = ts->tv_sec; |
| 237 | cts.tv_nsec = ts->tv_nsec; |
| 238 | data = &cts; |
| 239 | len = sizeof(cts); |
YOSHIFUJI Hideaki | 4768fbc | 2007-02-09 23:25:31 +0900 | [diff] [blame] | 240 | } |
| 241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | cmlen = CMSG_COMPAT_LEN(len); |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 243 | if (kmsg->msg_controllen < cmlen) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | kmsg->msg_flags |= MSG_CTRUNC; |
| 245 | cmlen = kmsg->msg_controllen; |
| 246 | } |
| 247 | cmhdr.cmsg_level = level; |
| 248 | cmhdr.cmsg_type = type; |
| 249 | cmhdr.cmsg_len = cmlen; |
| 250 | |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 251 | if (copy_to_user(cm, &cmhdr, sizeof cmhdr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | return -EFAULT; |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 253 | 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] | 254 | return -EFAULT; |
| 255 | cmlen = CMSG_COMPAT_SPACE(len); |
Wei Yongjun | 1ac70e7 | 2007-12-20 14:36:44 -0800 | [diff] [blame] | 256 | if (kmsg->msg_controllen < cmlen) |
| 257 | cmlen = kmsg->msg_controllen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | kmsg->msg_control += cmlen; |
| 259 | kmsg->msg_controllen -= cmlen; |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | void scm_detach_fds_compat(struct msghdr *kmsg, struct scm_cookie *scm) |
| 264 | { |
| 265 | struct compat_cmsghdr __user *cm = (struct compat_cmsghdr __user *) kmsg->msg_control; |
| 266 | int fdmax = (kmsg->msg_controllen - sizeof(struct compat_cmsghdr)) / sizeof(int); |
| 267 | int fdnum = scm->fp->count; |
| 268 | struct file **fp = scm->fp->fp; |
| 269 | int __user *cmfptr; |
| 270 | int err = 0, i; |
| 271 | |
| 272 | if (fdnum < fdmax) |
| 273 | fdmax = fdnum; |
| 274 | |
| 275 | for (i = 0, cmfptr = (int __user *) CMSG_COMPAT_DATA(cm); i < fdmax; i++, cmfptr++) { |
| 276 | int new_fd; |
| 277 | err = security_file_receive(fp[i]); |
| 278 | if (err) |
| 279 | break; |
Ulrich Drepper | 4a19542 | 2007-07-15 23:40:34 -0700 | [diff] [blame] | 280 | err = get_unused_fd_flags(MSG_CMSG_CLOEXEC & kmsg->msg_flags |
| 281 | ? O_CLOEXEC : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | if (err < 0) |
| 283 | break; |
| 284 | new_fd = err; |
| 285 | err = put_user(new_fd, cmfptr); |
| 286 | if (err) { |
| 287 | put_unused_fd(new_fd); |
| 288 | break; |
| 289 | } |
| 290 | /* Bump the usage count and install the file. */ |
| 291 | get_file(fp[i]); |
| 292 | fd_install(new_fd, fp[i]); |
| 293 | } |
| 294 | |
| 295 | if (i > 0) { |
| 296 | int cmlen = CMSG_COMPAT_LEN(i * sizeof(int)); |
Miklos Szeredi | effee6a | 2006-10-09 21:42:14 -0700 | [diff] [blame] | 297 | err = put_user(SOL_SOCKET, &cm->cmsg_level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | if (!err) |
| 299 | err = put_user(SCM_RIGHTS, &cm->cmsg_type); |
| 300 | if (!err) |
| 301 | err = put_user(cmlen, &cm->cmsg_len); |
| 302 | if (!err) { |
| 303 | cmlen = CMSG_COMPAT_SPACE(i * sizeof(int)); |
| 304 | kmsg->msg_control += cmlen; |
| 305 | kmsg->msg_controllen -= cmlen; |
| 306 | } |
| 307 | } |
| 308 | if (i < fdnum) |
| 309 | kmsg->msg_flags |= MSG_CTRUNC; |
| 310 | |
| 311 | /* |
| 312 | * All of the files that fit in the message have had their |
| 313 | * usage counts incremented, so we just free the list. |
| 314 | */ |
| 315 | __scm_destroy(scm); |
| 316 | } |
| 317 | |
| 318 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | * A struct sock_filter is architecture independent. |
| 320 | */ |
| 321 | struct compat_sock_fprog { |
| 322 | u16 len; |
| 323 | compat_uptr_t filter; /* struct sock_filter * */ |
| 324 | }; |
| 325 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 326 | static int do_set_attach_filter(struct socket *sock, int level, int optname, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | char __user *optval, int optlen) |
| 328 | { |
| 329 | struct compat_sock_fprog __user *fprog32 = (struct compat_sock_fprog __user *)optval; |
YOSHIFUJI Hideaki | 4768fbc | 2007-02-09 23:25:31 +0900 | [diff] [blame] | 330 | 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] | 331 | compat_uptr_t ptr; |
| 332 | u16 len; |
| 333 | |
| 334 | if (!access_ok(VERIFY_READ, fprog32, sizeof(*fprog32)) || |
| 335 | !access_ok(VERIFY_WRITE, kfprog, sizeof(struct sock_fprog)) || |
| 336 | __get_user(len, &fprog32->len) || |
| 337 | __get_user(ptr, &fprog32->filter) || |
| 338 | __put_user(len, &kfprog->len) || |
| 339 | __put_user(compat_ptr(ptr), &kfprog->filter)) |
| 340 | return -EFAULT; |
| 341 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 342 | return sock_setsockopt(sock, level, optname, (char __user *)kfprog, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | sizeof(struct sock_fprog)); |
| 344 | } |
| 345 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 346 | static int do_set_sock_timeout(struct socket *sock, int level, |
| 347 | int optname, char __user *optval, int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | { |
| 349 | struct compat_timeval __user *up = (struct compat_timeval __user *) optval; |
| 350 | struct timeval ktime; |
| 351 | mm_segment_t old_fs; |
| 352 | int err; |
| 353 | |
| 354 | if (optlen < sizeof(*up)) |
| 355 | return -EINVAL; |
| 356 | if (!access_ok(VERIFY_READ, up, sizeof(*up)) || |
| 357 | __get_user(ktime.tv_sec, &up->tv_sec) || |
| 358 | __get_user(ktime.tv_usec, &up->tv_usec)) |
| 359 | return -EFAULT; |
| 360 | old_fs = get_fs(); |
| 361 | set_fs(KERNEL_DS); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 362 | err = sock_setsockopt(sock, level, optname, (char *) &ktime, sizeof(ktime)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | set_fs(old_fs); |
| 364 | |
| 365 | return err; |
| 366 | } |
| 367 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 368 | static int compat_sock_setsockopt(struct socket *sock, int level, int optname, |
| 369 | char __user *optval, int optlen) |
| 370 | { |
| 371 | if (optname == SO_ATTACH_FILTER) |
| 372 | return do_set_attach_filter(sock, level, optname, |
| 373 | optval, optlen); |
| 374 | if (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO) |
| 375 | return do_set_sock_timeout(sock, level, optname, optval, optlen); |
| 376 | |
| 377 | return sock_setsockopt(sock, level, optname, optval, optlen); |
| 378 | } |
| 379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | asmlinkage long compat_sys_setsockopt(int fd, int level, int optname, |
| 381 | char __user *optval, int optlen) |
| 382 | { |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 383 | int err; |
| 384 | struct socket *sock; |
| 385 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 386 | if (optlen < 0) |
| 387 | return -EINVAL; |
| 388 | |
| 389 | if ((sock = sockfd_lookup(fd, &err))!=NULL) |
| 390 | { |
| 391 | err = security_socket_setsockopt(sock,level,optname); |
| 392 | if (err) { |
| 393 | sockfd_put(sock); |
| 394 | return err; |
| 395 | } |
| 396 | |
| 397 | if (level == SOL_SOCKET) |
| 398 | err = compat_sock_setsockopt(sock, level, |
| 399 | optname, optval, optlen); |
| 400 | else if (sock->ops->compat_setsockopt) |
| 401 | err = sock->ops->compat_setsockopt(sock, level, |
| 402 | optname, optval, optlen); |
| 403 | else |
| 404 | err = sock->ops->setsockopt(sock, level, |
| 405 | optname, optval, optlen); |
| 406 | sockfd_put(sock); |
| 407 | } |
| 408 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 411 | 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] | 412 | char __user *optval, int __user *optlen) |
| 413 | { |
| 414 | struct compat_timeval __user *up; |
| 415 | struct timeval ktime; |
| 416 | mm_segment_t old_fs; |
| 417 | int len, err; |
| 418 | |
| 419 | up = (struct compat_timeval __user *) optval; |
| 420 | if (get_user(len, optlen)) |
| 421 | return -EFAULT; |
| 422 | if (len < sizeof(*up)) |
| 423 | return -EINVAL; |
| 424 | len = sizeof(ktime); |
| 425 | old_fs = get_fs(); |
| 426 | set_fs(KERNEL_DS); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 427 | err = sock_getsockopt(sock, level, optname, (char *) &ktime, &len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | set_fs(old_fs); |
| 429 | |
| 430 | if (!err) { |
| 431 | if (put_user(sizeof(*up), optlen) || |
| 432 | !access_ok(VERIFY_WRITE, up, sizeof(*up)) || |
| 433 | __put_user(ktime.tv_sec, &up->tv_sec) || |
| 434 | __put_user(ktime.tv_usec, &up->tv_usec)) |
| 435 | err = -EFAULT; |
| 436 | } |
| 437 | return err; |
| 438 | } |
| 439 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 440 | static int compat_sock_getsockopt(struct socket *sock, int level, int optname, |
| 441 | char __user *optval, int __user *optlen) |
| 442 | { |
| 443 | if (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO) |
| 444 | return do_get_sock_timeout(sock, level, optname, optval, optlen); |
| 445 | return sock_getsockopt(sock, level, optname, optval, optlen); |
| 446 | } |
| 447 | |
Shaun Pereira | f0ac261 | 2006-03-21 23:59:39 -0800 | [diff] [blame] | 448 | int compat_sock_get_timestamp(struct sock *sk, struct timeval __user *userstamp) |
| 449 | { |
| 450 | struct compat_timeval __user *ctv = |
| 451 | (struct compat_timeval __user*) userstamp; |
| 452 | int err = -ENOENT; |
Eric Dumazet | b7aa0bf | 2007-04-19 16:16:32 -0700 | [diff] [blame] | 453 | struct timeval tv; |
Shaun Pereira | f0ac261 | 2006-03-21 23:59:39 -0800 | [diff] [blame] | 454 | |
| 455 | if (!sock_flag(sk, SOCK_TIMESTAMP)) |
| 456 | sock_enable_timestamp(sk); |
Eric Dumazet | b7aa0bf | 2007-04-19 16:16:32 -0700 | [diff] [blame] | 457 | tv = ktime_to_timeval(sk->sk_stamp); |
| 458 | if (tv.tv_sec == -1) |
Shaun Pereira | f0ac261 | 2006-03-21 23:59:39 -0800 | [diff] [blame] | 459 | return err; |
Eric Dumazet | b7aa0bf | 2007-04-19 16:16:32 -0700 | [diff] [blame] | 460 | if (tv.tv_sec == 0) { |
| 461 | sk->sk_stamp = ktime_get_real(); |
| 462 | tv = ktime_to_timeval(sk->sk_stamp); |
| 463 | } |
| 464 | err = 0; |
| 465 | if (put_user(tv.tv_sec, &ctv->tv_sec) || |
| 466 | put_user(tv.tv_usec, &ctv->tv_usec)) |
Shaun Pereira | f0ac261 | 2006-03-21 23:59:39 -0800 | [diff] [blame] | 467 | err = -EFAULT; |
| 468 | return err; |
| 469 | } |
| 470 | EXPORT_SYMBOL(compat_sock_get_timestamp); |
| 471 | |
Eric Dumazet | ae40eb1 | 2007-03-18 17:33:16 -0700 | [diff] [blame] | 472 | int compat_sock_get_timestampns(struct sock *sk, struct timespec __user *userstamp) |
| 473 | { |
| 474 | struct compat_timespec __user *ctv = |
| 475 | (struct compat_timespec __user*) userstamp; |
| 476 | int err = -ENOENT; |
| 477 | struct timespec ts; |
| 478 | |
| 479 | if (!sock_flag(sk, SOCK_TIMESTAMP)) |
| 480 | sock_enable_timestamp(sk); |
| 481 | ts = ktime_to_timespec(sk->sk_stamp); |
| 482 | if (ts.tv_sec == -1) |
| 483 | return err; |
| 484 | if (ts.tv_sec == 0) { |
| 485 | sk->sk_stamp = ktime_get_real(); |
| 486 | ts = ktime_to_timespec(sk->sk_stamp); |
| 487 | } |
| 488 | err = 0; |
| 489 | if (put_user(ts.tv_sec, &ctv->tv_sec) || |
| 490 | put_user(ts.tv_nsec, &ctv->tv_nsec)) |
| 491 | err = -EFAULT; |
| 492 | return err; |
| 493 | } |
| 494 | EXPORT_SYMBOL(compat_sock_get_timestampns); |
| 495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | asmlinkage long compat_sys_getsockopt(int fd, int level, int optname, |
| 497 | char __user *optval, int __user *optlen) |
| 498 | { |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 499 | int err; |
| 500 | struct socket *sock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 502 | if ((sock = sockfd_lookup(fd, &err))!=NULL) |
| 503 | { |
| 504 | err = security_socket_getsockopt(sock, level, |
| 505 | optname); |
| 506 | if (err) { |
| 507 | sockfd_put(sock); |
| 508 | return err; |
| 509 | } |
| 510 | |
| 511 | if (level == SOL_SOCKET) |
| 512 | err = compat_sock_getsockopt(sock, level, |
| 513 | optname, optval, optlen); |
| 514 | else if (sock->ops->compat_getsockopt) |
| 515 | err = sock->ops->compat_getsockopt(sock, level, |
| 516 | optname, optval, optlen); |
| 517 | else |
| 518 | err = sock->ops->getsockopt(sock, level, |
| 519 | optname, optval, optlen); |
| 520 | sockfd_put(sock); |
| 521 | } |
| 522 | return err; |
| 523 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | /* Argument list sizes for compat_sys_socketcall */ |
| 525 | #define AL(x) ((x) * sizeof(u32)) |
| 526 | static unsigned char nas[18]={AL(0),AL(3),AL(3),AL(3),AL(2),AL(3), |
| 527 | AL(3),AL(3),AL(4),AL(4),AL(4),AL(6), |
| 528 | AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)}; |
| 529 | #undef AL |
| 530 | |
| 531 | asmlinkage long compat_sys_sendmsg(int fd, struct compat_msghdr __user *msg, unsigned flags) |
| 532 | { |
| 533 | return sys_sendmsg(fd, (struct msghdr __user *)msg, flags | MSG_CMSG_COMPAT); |
| 534 | } |
| 535 | |
| 536 | asmlinkage long compat_sys_recvmsg(int fd, struct compat_msghdr __user *msg, unsigned int flags) |
| 537 | { |
| 538 | return sys_recvmsg(fd, (struct msghdr __user *)msg, flags | MSG_CMSG_COMPAT); |
| 539 | } |
| 540 | |
| 541 | asmlinkage long compat_sys_socketcall(int call, u32 __user *args) |
| 542 | { |
| 543 | int ret; |
| 544 | u32 a[6]; |
| 545 | u32 a0, a1; |
YOSHIFUJI Hideaki | 4768fbc | 2007-02-09 23:25:31 +0900 | [diff] [blame] | 546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | if (call < SYS_SOCKET || call > SYS_RECVMSG) |
| 548 | return -EINVAL; |
| 549 | if (copy_from_user(a, args, nas[call])) |
| 550 | return -EFAULT; |
| 551 | a0 = a[0]; |
| 552 | a1 = a[1]; |
YOSHIFUJI Hideaki | 4768fbc | 2007-02-09 23:25:31 +0900 | [diff] [blame] | 553 | |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 554 | switch (call) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | case SYS_SOCKET: |
| 556 | ret = sys_socket(a0, a1, a[2]); |
| 557 | break; |
| 558 | case SYS_BIND: |
| 559 | ret = sys_bind(a0, compat_ptr(a1), a[2]); |
| 560 | break; |
| 561 | case SYS_CONNECT: |
| 562 | ret = sys_connect(a0, compat_ptr(a1), a[2]); |
| 563 | break; |
| 564 | case SYS_LISTEN: |
| 565 | ret = sys_listen(a0, a1); |
| 566 | break; |
| 567 | case SYS_ACCEPT: |
| 568 | ret = sys_accept(a0, compat_ptr(a1), compat_ptr(a[2])); |
| 569 | break; |
| 570 | case SYS_GETSOCKNAME: |
| 571 | ret = sys_getsockname(a0, compat_ptr(a1), compat_ptr(a[2])); |
| 572 | break; |
| 573 | case SYS_GETPEERNAME: |
| 574 | ret = sys_getpeername(a0, compat_ptr(a1), compat_ptr(a[2])); |
| 575 | break; |
| 576 | case SYS_SOCKETPAIR: |
| 577 | ret = sys_socketpair(a0, a1, a[2], compat_ptr(a[3])); |
| 578 | break; |
| 579 | case SYS_SEND: |
| 580 | ret = sys_send(a0, compat_ptr(a1), a[2], a[3]); |
| 581 | break; |
| 582 | case SYS_SENDTO: |
| 583 | ret = sys_sendto(a0, compat_ptr(a1), a[2], a[3], compat_ptr(a[4]), a[5]); |
| 584 | break; |
| 585 | case SYS_RECV: |
| 586 | ret = sys_recv(a0, compat_ptr(a1), a[2], a[3]); |
| 587 | break; |
| 588 | case SYS_RECVFROM: |
| 589 | ret = sys_recvfrom(a0, compat_ptr(a1), a[2], a[3], compat_ptr(a[4]), compat_ptr(a[5])); |
| 590 | break; |
| 591 | case SYS_SHUTDOWN: |
| 592 | ret = sys_shutdown(a0,a1); |
| 593 | break; |
| 594 | case SYS_SETSOCKOPT: |
| 595 | ret = compat_sys_setsockopt(a0, a1, a[2], |
| 596 | compat_ptr(a[3]), a[4]); |
| 597 | break; |
| 598 | case SYS_GETSOCKOPT: |
| 599 | ret = compat_sys_getsockopt(a0, a1, a[2], |
| 600 | compat_ptr(a[3]), compat_ptr(a[4])); |
| 601 | break; |
| 602 | case SYS_SENDMSG: |
| 603 | ret = compat_sys_sendmsg(a0, compat_ptr(a1), a[2]); |
| 604 | break; |
| 605 | case SYS_RECVMSG: |
| 606 | ret = compat_sys_recvmsg(a0, compat_ptr(a1), a[2]); |
| 607 | break; |
| 608 | default: |
| 609 | ret = -EINVAL; |
| 610 | break; |
| 611 | } |
| 612 | return ret; |
| 613 | } |