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