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