linux/fs/compat.c
<<
>>
Prefs
   1/*
   2 *  linux/fs/compat.c
   3 *
   4 *  Kernel compatibililty routines for e.g. 32 bit syscall support
   5 *  on 64 bit kernels.
   6 *
   7 *  Copyright (C) 2002       Stephen Rothwell, IBM Corporation
   8 *  Copyright (C) 1997-2000  Jakub Jelinek  (jakub@redhat.com)
   9 *  Copyright (C) 1998       Eddie C. Dost  (ecd@skynet.be)
  10 *  Copyright (C) 2001,2002  Andi Kleen, SuSE Labs 
  11 *  Copyright (C) 2003       Pavel Machek (pavel@ucw.cz)
  12 *
  13 *  This program is free software; you can redistribute it and/or modify
  14 *  it under the terms of the GNU General Public License version 2 as
  15 *  published by the Free Software Foundation.
  16 */
  17
  18#include <linux/stddef.h>
  19#include <linux/kernel.h>
  20#include <linux/linkage.h>
  21#include <linux/compat.h>
  22#include <linux/errno.h>
  23#include <linux/time.h>
  24#include <linux/fs.h>
  25#include <linux/fcntl.h>
  26#include <linux/namei.h>
  27#include <linux/file.h>
  28#include <linux/fdtable.h>
  29#include <linux/vfs.h>
  30#include <linux/ioctl.h>
  31#include <linux/init.h>
  32#include <linux/ncp_mount.h>
  33#include <linux/nfs4_mount.h>
  34#include <linux/syscalls.h>
  35#include <linux/ctype.h>
  36#include <linux/dirent.h>
  37#include <linux/fsnotify.h>
  38#include <linux/highuid.h>
  39#include <linux/personality.h>
  40#include <linux/rwsem.h>
  41#include <linux/tsacct_kern.h>
  42#include <linux/security.h>
  43#include <linux/highmem.h>
  44#include <linux/signal.h>
  45#include <linux/poll.h>
  46#include <linux/mm.h>
  47#include <linux/fs_struct.h>
  48#include <linux/slab.h>
  49#include <linux/pagemap.h>
  50#include <linux/aio.h>
  51
  52#include <asm/uaccess.h>
  53#include <asm/mmu_context.h>
  54#include <asm/ioctls.h>
  55#include "internal.h"
  56
  57int compat_log = 1;
  58
  59int compat_printk(const char *fmt, ...)
  60{
  61        va_list ap;
  62        int ret;
  63        if (!compat_log)
  64                return 0;
  65        va_start(ap, fmt);
  66        ret = vprintk(fmt, ap);
  67        va_end(ap);
  68        return ret;
  69}
  70
  71/*
  72 * Not all architectures have sys_utime, so implement this in terms
  73 * of sys_utimes.
  74 */
  75asmlinkage long compat_sys_utime(const char __user *filename,
  76                                 struct compat_utimbuf __user *t)
  77{
  78        struct timespec tv[2];
  79
  80        if (t) {
  81                if (get_user(tv[0].tv_sec, &t->actime) ||
  82                    get_user(tv[1].tv_sec, &t->modtime))
  83                        return -EFAULT;
  84                tv[0].tv_nsec = 0;
  85                tv[1].tv_nsec = 0;
  86        }
  87        return do_utimes(AT_FDCWD, filename, t ? tv : NULL, 0);
  88}
  89
  90asmlinkage long compat_sys_utimensat(unsigned int dfd, const char __user *filename, struct compat_timespec __user *t, int flags)
  91{
  92        struct timespec tv[2];
  93
  94        if  (t) {
  95                if (get_compat_timespec(&tv[0], &t[0]) ||
  96                    get_compat_timespec(&tv[1], &t[1]))
  97                        return -EFAULT;
  98
  99                if (tv[0].tv_nsec == UTIME_OMIT && tv[1].tv_nsec == UTIME_OMIT)
 100                        return 0;
 101        }
 102        return do_utimes(dfd, filename, t ? tv : NULL, flags);
 103}
 104
 105asmlinkage long compat_sys_futimesat(unsigned int dfd, const char __user *filename, struct compat_timeval __user *t)
 106{
 107        struct timespec tv[2];
 108
 109        if (t) {
 110                if (get_user(tv[0].tv_sec, &t[0].tv_sec) ||
 111                    get_user(tv[0].tv_nsec, &t[0].tv_usec) ||
 112                    get_user(tv[1].tv_sec, &t[1].tv_sec) ||
 113                    get_user(tv[1].tv_nsec, &t[1].tv_usec))
 114                        return -EFAULT;
 115                if (tv[0].tv_nsec >= 1000000 || tv[0].tv_nsec < 0 ||
 116                    tv[1].tv_nsec >= 1000000 || tv[1].tv_nsec < 0)
 117                        return -EINVAL;
 118                tv[0].tv_nsec *= 1000;
 119                tv[1].tv_nsec *= 1000;
 120        }
 121        return do_utimes(dfd, filename, t ? tv : NULL, 0);
 122}
 123
 124asmlinkage long compat_sys_utimes(const char __user *filename, struct compat_timeval __user *t)
 125{
 126        return compat_sys_futimesat(AT_FDCWD, filename, t);
 127}
 128
 129static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
 130{
 131        struct compat_stat tmp;
 132
 133        if (!old_valid_dev(stat->dev) || !old_valid_dev(stat->rdev))
 134                return -EOVERFLOW;
 135
 136        memset(&tmp, 0, sizeof(tmp));
 137        tmp.st_dev = old_encode_dev(stat->dev);
 138        tmp.st_ino = stat->ino;
 139        if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
 140                return -EOVERFLOW;
 141        tmp.st_mode = stat->mode;
 142        tmp.st_nlink = stat->nlink;
 143        if (tmp.st_nlink != stat->nlink)
 144                return -EOVERFLOW;
 145        SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
 146        SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
 147        tmp.st_rdev = old_encode_dev(stat->rdev);
 148        if ((u64) stat->size > MAX_NON_LFS)
 149                return -EOVERFLOW;
 150        tmp.st_size = stat->size;
 151        tmp.st_atime = stat->atime.tv_sec;
 152        tmp.st_atime_nsec = stat->atime.tv_nsec;
 153        tmp.st_mtime = stat->mtime.tv_sec;
 154        tmp.st_mtime_nsec = stat->mtime.tv_nsec;
 155        tmp.st_ctime = stat->ctime.tv_sec;
 156        tmp.st_ctime_nsec = stat->ctime.tv_nsec;
 157        tmp.st_blocks = stat->blocks;
 158        tmp.st_blksize = stat->blksize;
 159        return copy_to_user(ubuf, &tmp, sizeof(tmp)) ? -EFAULT : 0;
 160}
 161
 162asmlinkage long compat_sys_newstat(const char __user * filename,
 163                struct compat_stat __user *statbuf)
 164{
 165        struct kstat stat;
 166        int error;
 167
 168        error = vfs_stat(filename, &stat);
 169        if (error)
 170                return error;
 171        return cp_compat_stat(&stat, statbuf);
 172}
 173
 174asmlinkage long compat_sys_newlstat(const char __user * filename,
 175                struct compat_stat __user *statbuf)
 176{
 177        struct kstat stat;
 178        int error;
 179
 180        error = vfs_lstat(filename, &stat);
 181        if (error)
 182                return error;
 183        return cp_compat_stat(&stat, statbuf);
 184}
 185
 186#ifndef __ARCH_WANT_STAT64
 187asmlinkage long compat_sys_newfstatat(unsigned int dfd,
 188                const char __user *filename,
 189                struct compat_stat __user *statbuf, int flag)
 190{
 191        struct kstat stat;
 192        int error;
 193
 194        error = vfs_fstatat(dfd, filename, &stat, flag);
 195        if (error)
 196                return error;
 197        return cp_compat_stat(&stat, statbuf);
 198}
 199#endif
 200
 201asmlinkage long compat_sys_newfstat(unsigned int fd,
 202                struct compat_stat __user * statbuf)
 203{
 204        struct kstat stat;
 205        int error = vfs_fstat(fd, &stat);
 206
 207        if (!error)
 208                error = cp_compat_stat(&stat, statbuf);
 209        return error;
 210}
 211
 212static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf)
 213{
 214        
 215        if (sizeof ubuf->f_blocks == 4) {
 216                if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail |
 217                     kbuf->f_bsize | kbuf->f_frsize) & 0xffffffff00000000ULL)
 218                        return -EOVERFLOW;
 219                /* f_files and f_ffree may be -1; it's okay
 220                 * to stuff that into 32 bits */
 221                if (kbuf->f_files != 0xffffffffffffffffULL
 222                 && (kbuf->f_files & 0xffffffff00000000ULL))
 223                        return -EOVERFLOW;
 224                if (kbuf->f_ffree != 0xffffffffffffffffULL
 225                 && (kbuf->f_ffree & 0xffffffff00000000ULL))
 226                        return -EOVERFLOW;
 227        }
 228        if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
 229            __put_user(kbuf->f_type, &ubuf->f_type) ||
 230            __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
 231            __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
 232            __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
 233            __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
 234            __put_user(kbuf->f_files, &ubuf->f_files) ||
 235            __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
 236            __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
 237            __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
 238            __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
 239            __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
 240            __put_user(kbuf->f_flags, &ubuf->f_flags) ||
 241            __clear_user(ubuf->f_spare, sizeof(ubuf->f_spare)))
 242                return -EFAULT;
 243        return 0;
 244}
 245
 246/*
 247 * The following statfs calls are copies of code from fs/statfs.c and
 248 * should be checked against those from time to time
 249 */
 250asmlinkage long compat_sys_statfs(const char __user *pathname, struct compat_statfs __user *buf)
 251{
 252        struct kstatfs tmp;
 253        int error = user_statfs(pathname, &tmp);
 254        if (!error)
 255                error = put_compat_statfs(buf, &tmp);
 256        return error;
 257}
 258
 259asmlinkage long compat_sys_fstatfs(unsigned int fd, struct compat_statfs __user *buf)
 260{
 261        struct kstatfs tmp;
 262        int error = fd_statfs(fd, &tmp);
 263        if (!error)
 264                error = put_compat_statfs(buf, &tmp);
 265        return error;
 266}
 267
 268static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf)
 269{
 270        if (sizeof(ubuf->f_bsize) == 4) {
 271                if ((kbuf->f_type | kbuf->f_bsize | kbuf->f_namelen |
 272                     kbuf->f_frsize | kbuf->f_flags) & 0xffffffff00000000ULL)
 273                        return -EOVERFLOW;
 274                /* f_files and f_ffree may be -1; it's okay
 275                 * to stuff that into 32 bits */
 276                if (kbuf->f_files != 0xffffffffffffffffULL
 277                 && (kbuf->f_files & 0xffffffff00000000ULL))
 278                        return -EOVERFLOW;
 279                if (kbuf->f_ffree != 0xffffffffffffffffULL
 280                 && (kbuf->f_ffree & 0xffffffff00000000ULL))
 281                        return -EOVERFLOW;
 282        }
 283        if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
 284            __put_user(kbuf->f_type, &ubuf->f_type) ||
 285            __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
 286            __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
 287            __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
 288            __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
 289            __put_user(kbuf->f_files, &ubuf->f_files) ||
 290            __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
 291            __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
 292            __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
 293            __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
 294            __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
 295            __put_user(kbuf->f_flags, &ubuf->f_flags) ||
 296            __clear_user(ubuf->f_spare, sizeof(ubuf->f_spare)))
 297                return -EFAULT;
 298        return 0;
 299}
 300
 301asmlinkage long compat_sys_statfs64(const char __user *pathname, compat_size_t sz, struct compat_statfs64 __user *buf)
 302{
 303        struct kstatfs tmp;
 304        int error;
 305
 306        if (sz != sizeof(*buf))
 307                return -EINVAL;
 308
 309        error = user_statfs(pathname, &tmp);
 310        if (!error)
 311                error = put_compat_statfs64(buf, &tmp);
 312        return error;
 313}
 314
 315asmlinkage long compat_sys_fstatfs64(unsigned int fd, compat_size_t sz, struct compat_statfs64 __user *buf)
 316{
 317        struct kstatfs tmp;
 318        int error;
 319
 320        if (sz != sizeof(*buf))
 321                return -EINVAL;
 322
 323        error = fd_statfs(fd, &tmp);
 324        if (!error)
 325                error = put_compat_statfs64(buf, &tmp);
 326        return error;
 327}
 328
 329/*
 330 * This is a copy of sys_ustat, just dealing with a structure layout.
 331 * Given how simple this syscall is that apporach is more maintainable
 332 * than the various conversion hacks.
 333 */
 334asmlinkage long compat_sys_ustat(unsigned dev, struct compat_ustat __user *u)
 335{
 336        struct compat_ustat tmp;
 337        struct kstatfs sbuf;
 338        int err = vfs_ustat(new_decode_dev(dev), &sbuf);
 339        if (err)
 340                return err;
 341
 342        memset(&tmp, 0, sizeof(struct compat_ustat));
 343        tmp.f_tfree = sbuf.f_bfree;
 344        tmp.f_tinode = sbuf.f_ffree;
 345        if (copy_to_user(u, &tmp, sizeof(struct compat_ustat)))
 346                return -EFAULT;
 347        return 0;
 348}
 349
 350static int get_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
 351{
 352        if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
 353            __get_user(kfl->l_type, &ufl->l_type) ||
 354            __get_user(kfl->l_whence, &ufl->l_whence) ||
 355            __get_user(kfl->l_start, &ufl->l_start) ||
 356            __get_user(kfl->l_len, &ufl->l_len) ||
 357            __get_user(kfl->l_pid, &ufl->l_pid))
 358                return -EFAULT;
 359        return 0;
 360}
 361
 362static int put_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
 363{
 364        if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
 365            __put_user(kfl->l_type, &ufl->l_type) ||
 366            __put_user(kfl->l_whence, &ufl->l_whence) ||
 367            __put_user(kfl->l_start, &ufl->l_start) ||
 368            __put_user(kfl->l_len, &ufl->l_len) ||
 369            __put_user(kfl->l_pid, &ufl->l_pid))
 370                return -EFAULT;
 371        return 0;
 372}
 373
 374#ifndef HAVE_ARCH_GET_COMPAT_FLOCK64
 375static int get_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
 376{
 377        if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
 378            __get_user(kfl->l_type, &ufl->l_type) ||
 379            __get_user(kfl->l_whence, &ufl->l_whence) ||
 380            __get_user(kfl->l_start, &ufl->l_start) ||
 381            __get_user(kfl->l_len, &ufl->l_len) ||
 382            __get_user(kfl->l_pid, &ufl->l_pid))
 383                return -EFAULT;
 384        return 0;
 385}
 386#endif
 387
 388#ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64
 389static int put_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
 390{
 391        if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
 392            __put_user(kfl->l_type, &ufl->l_type) ||
 393            __put_user(kfl->l_whence, &ufl->l_whence) ||
 394            __put_user(kfl->l_start, &ufl->l_start) ||
 395            __put_user(kfl->l_len, &ufl->l_len) ||
 396            __put_user(kfl->l_pid, &ufl->l_pid))
 397                return -EFAULT;
 398        return 0;
 399}
 400#endif
 401
 402static unsigned int
 403convert_fcntl_cmd(unsigned int cmd)
 404{
 405        switch (cmd) {
 406        case F_GETLK64:
 407                return F_GETLK;
 408        case F_SETLK64:
 409                return F_SETLK;
 410        case F_SETLKW64:
 411                return F_SETLKW;
 412        }
 413
 414        return cmd;
 415}
 416
 417asmlinkage long compat_sys_fcntl64(unsigned int fd, unsigned int cmd,
 418                unsigned long arg)
 419{
 420        mm_segment_t old_fs;
 421        struct flock f;
 422        long ret;
 423        unsigned int conv_cmd;
 424
 425        switch (cmd) {
 426        case F_GETLK:
 427        case F_SETLK:
 428        case F_SETLKW:
 429                ret = get_compat_flock(&f, compat_ptr(arg));
 430                if (ret != 0)
 431                        break;
 432                old_fs = get_fs();
 433                set_fs(KERNEL_DS);
 434                ret = sys_fcntl(fd, cmd, (unsigned long)&f);
 435                set_fs(old_fs);
 436                if (cmd == F_GETLK && ret == 0) {
 437                        /* GETLK was successful and we need to return the data...
 438                         * but it needs to fit in the compat structure.
 439                         * l_start shouldn't be too big, unless the original
 440                         * start + end is greater than COMPAT_OFF_T_MAX, in which
 441                         * case the app was asking for trouble, so we return
 442                         * -EOVERFLOW in that case.
 443                         * l_len could be too big, in which case we just truncate it,
 444                         * and only allow the app to see that part of the conflicting
 445                         * lock that might make sense to it anyway
 446                         */
 447
 448                        if (f.l_start > COMPAT_OFF_T_MAX)
 449                                ret = -EOVERFLOW;
 450                        if (f.l_len > COMPAT_OFF_T_MAX)
 451                                f.l_len = COMPAT_OFF_T_MAX;
 452                        if (ret == 0)
 453                                ret = put_compat_flock(&f, compat_ptr(arg));
 454                }
 455                break;
 456
 457        case F_GETLK64:
 458        case F_SETLK64:
 459        case F_SETLKW64:
 460        case F_OFD_GETLK:
 461        case F_OFD_SETLK:
 462        case F_OFD_SETLKW:
 463                ret = get_compat_flock64(&f, compat_ptr(arg));
 464                if (ret != 0)
 465                        break;
 466                old_fs = get_fs();
 467                set_fs(KERNEL_DS);
 468                conv_cmd = convert_fcntl_cmd(cmd);
 469                ret = sys_fcntl(fd, conv_cmd, (unsigned long)&f);
 470                set_fs(old_fs);
 471                if ((conv_cmd == F_GETLK || conv_cmd == F_OFD_GETLK) && ret == 0) {
 472                        /* need to return lock information - see above for commentary */
 473                        if (f.l_start > COMPAT_LOFF_T_MAX)
 474                                ret = -EOVERFLOW;
 475                        if (f.l_len > COMPAT_LOFF_T_MAX)
 476                                f.l_len = COMPAT_LOFF_T_MAX;
 477                        if (ret == 0)
 478                                ret = put_compat_flock64(&f, compat_ptr(arg));
 479                }
 480                break;
 481
 482        default:
 483                ret = sys_fcntl(fd, cmd, arg);
 484                break;
 485        }
 486        return ret;
 487}
 488
 489asmlinkage long compat_sys_fcntl(unsigned int fd, unsigned int cmd,
 490                unsigned long arg)
 491{
 492        switch (cmd) {
 493        case F_GETLK64:
 494        case F_SETLK64:
 495        case F_SETLKW64:
 496        case F_OFD_GETLK:
 497        case F_OFD_SETLK:
 498        case F_OFD_SETLKW:
 499                return -EINVAL;
 500        }
 501        return compat_sys_fcntl64(fd, cmd, arg);
 502}
 503
 504asmlinkage long
 505compat_sys_io_setup(unsigned nr_reqs, u32 __user *ctx32p)
 506{
 507        long ret;
 508        aio_context_t ctx64;
 509
 510        mm_segment_t oldfs = get_fs();
 511        if (unlikely(get_user(ctx64, ctx32p)))
 512                return -EFAULT;
 513
 514        set_fs(KERNEL_DS);
 515        /* The __user pointer cast is valid because of the set_fs() */
 516        ret = sys_io_setup(nr_reqs, (aio_context_t __user *) &ctx64);
 517        set_fs(oldfs);
 518        /* truncating is ok because it's a user address */
 519        if (!ret)
 520                ret = put_user((u32) ctx64, ctx32p);
 521        return ret;
 522}
 523
 524asmlinkage long
 525compat_sys_io_getevents(aio_context_t ctx_id,
 526                                 unsigned long min_nr,
 527                                 unsigned long nr,
 528                                 struct io_event __user *events,
 529                                 struct compat_timespec __user *timeout)
 530{
 531        long ret;
 532        struct timespec t;
 533        struct timespec __user *ut = NULL;
 534
 535        ret = -EFAULT;
 536        if (unlikely(!access_ok(VERIFY_WRITE, events, 
 537                                nr * sizeof(struct io_event))))
 538                goto out;
 539        if (timeout) {
 540                if (get_compat_timespec(&t, timeout))
 541                        goto out;
 542
 543                ut = compat_alloc_user_space(sizeof(*ut));
 544                if (copy_to_user(ut, &t, sizeof(t)) )
 545                        goto out;
 546        } 
 547        ret = sys_io_getevents(ctx_id, min_nr, nr, events, ut);
 548out:
 549        return ret;
 550}
 551
 552/* A write operation does a read from user space and vice versa */
 553#define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
 554
 555ssize_t compat_rw_copy_check_uvector(int type,
 556                const struct compat_iovec __user *uvector, unsigned long nr_segs,
 557                unsigned long fast_segs, struct iovec *fast_pointer,
 558                struct iovec **ret_pointer)
 559{
 560        compat_ssize_t tot_len;
 561        struct iovec *iov = *ret_pointer = fast_pointer;
 562        ssize_t ret = 0;
 563        int seg;
 564
 565        /*
 566         * SuS says "The readv() function *may* fail if the iovcnt argument
 567         * was less than or equal to 0, or greater than {IOV_MAX}.  Linux has
 568         * traditionally returned zero for zero segments, so...
 569         */
 570        if (nr_segs == 0)
 571                goto out;
 572
 573        ret = -EINVAL;
 574        if (nr_segs > UIO_MAXIOV || nr_segs < 0)
 575                goto out;
 576        if (nr_segs > fast_segs) {
 577                ret = -ENOMEM;
 578                iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
 579                if (iov == NULL)
 580                        goto out;
 581        }
 582        *ret_pointer = iov;
 583
 584        ret = -EFAULT;
 585        if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
 586                goto out;
 587
 588        /*
 589         * Single unix specification:
 590         * We should -EINVAL if an element length is not >= 0 and fitting an
 591         * ssize_t.
 592         *
 593         * In Linux, the total length is limited to MAX_RW_COUNT, there is
 594         * no overflow possibility.
 595         */
 596        tot_len = 0;
 597        ret = -EINVAL;
 598        for (seg = 0; seg < nr_segs; seg++) {
 599                compat_uptr_t buf;
 600                compat_ssize_t len;
 601
 602                if (__get_user(len, &uvector->iov_len) ||
 603                   __get_user(buf, &uvector->iov_base)) {
 604                        ret = -EFAULT;
 605                        goto out;
 606                }
 607                if (len < 0)    /* size_t not fitting in compat_ssize_t .. */
 608                        goto out;
 609                if (type >= 0 &&
 610                    !access_ok(vrfy_dir(type), compat_ptr(buf), len)) {
 611                        ret = -EFAULT;
 612                        goto out;
 613                }
 614                if (len > MAX_RW_COUNT - tot_len)
 615                        len = MAX_RW_COUNT - tot_len;
 616                tot_len += len;
 617                iov->iov_base = compat_ptr(buf);
 618                iov->iov_len = (compat_size_t) len;
 619                uvector++;
 620                iov++;
 621        }
 622        ret = tot_len;
 623
 624out:
 625        return ret;
 626}
 627
 628static inline long
 629copy_iocb(long nr, u32 __user *ptr32, struct iocb __user * __user *ptr64)
 630{
 631        compat_uptr_t uptr;
 632        int i;
 633
 634        for (i = 0; i < nr; ++i) {
 635                if (get_user(uptr, ptr32 + i))
 636                        return -EFAULT;
 637                if (put_user(compat_ptr(uptr), ptr64 + i))
 638                        return -EFAULT;
 639        }
 640        return 0;
 641}
 642
 643#define MAX_AIO_SUBMITS         (PAGE_SIZE/sizeof(struct iocb *))
 644
 645asmlinkage long
 646compat_sys_io_submit(aio_context_t ctx_id, int nr, u32 __user *iocb)
 647{
 648        struct iocb __user * __user *iocb64; 
 649        long ret;
 650
 651        if (unlikely(nr < 0))
 652                return -EINVAL;
 653
 654        if (nr > MAX_AIO_SUBMITS)
 655                nr = MAX_AIO_SUBMITS;
 656        
 657        iocb64 = compat_alloc_user_space(nr * sizeof(*iocb64));
 658        ret = copy_iocb(nr, iocb, iocb64);
 659        if (!ret)
 660                ret = do_io_submit(ctx_id, nr, iocb64, 1);
 661        return ret;
 662}
 663
 664struct compat_ncp_mount_data {
 665        compat_int_t version;
 666        compat_uint_t ncp_fd;
 667        __compat_uid_t mounted_uid;
 668        compat_pid_t wdog_pid;
 669        unsigned char mounted_vol[NCP_VOLNAME_LEN + 1];
 670        compat_uint_t time_out;
 671        compat_uint_t retry_count;
 672        compat_uint_t flags;
 673        __compat_uid_t uid;
 674        __compat_gid_t gid;
 675        compat_mode_t file_mode;
 676        compat_mode_t dir_mode;
 677};
 678
 679struct compat_ncp_mount_data_v4 {
 680        compat_int_t version;
 681        compat_ulong_t flags;
 682        compat_ulong_t mounted_uid;
 683        compat_long_t wdog_pid;
 684        compat_uint_t ncp_fd;
 685        compat_uint_t time_out;
 686        compat_uint_t retry_count;
 687        compat_ulong_t uid;
 688        compat_ulong_t gid;
 689        compat_ulong_t file_mode;
 690        compat_ulong_t dir_mode;
 691};
 692
 693static void *do_ncp_super_data_conv(void *raw_data)
 694{
 695        int version = *(unsigned int *)raw_data;
 696
 697        if (version == 3) {
 698                struct compat_ncp_mount_data *c_n = raw_data;
 699                struct ncp_mount_data *n = raw_data;
 700
 701                n->dir_mode = c_n->dir_mode;
 702                n->file_mode = c_n->file_mode;
 703                n->gid = c_n->gid;
 704                n->uid = c_n->uid;
 705                memmove (n->mounted_vol, c_n->mounted_vol, (sizeof (c_n->mounted_vol) + 3 * sizeof (unsigned int)));
 706                n->wdog_pid = c_n->wdog_pid;
 707                n->mounted_uid = c_n->mounted_uid;
 708        } else if (version == 4) {
 709                struct compat_ncp_mount_data_v4 *c_n = raw_data;
 710                struct ncp_mount_data_v4 *n = raw_data;
 711
 712                n->dir_mode = c_n->dir_mode;
 713                n->file_mode = c_n->file_mode;
 714                n->gid = c_n->gid;
 715                n->uid = c_n->uid;
 716                n->retry_count = c_n->retry_count;
 717                n->time_out = c_n->time_out;
 718                n->ncp_fd = c_n->ncp_fd;
 719                n->wdog_pid = c_n->wdog_pid;
 720                n->mounted_uid = c_n->mounted_uid;
 721                n->flags = c_n->flags;
 722        } else if (version != 5) {
 723                return NULL;
 724        }
 725
 726        return raw_data;
 727}
 728
 729
 730struct compat_nfs_string {
 731        compat_uint_t len;
 732        compat_uptr_t data;
 733};
 734
 735static inline void compat_nfs_string(struct nfs_string *dst,
 736                                     struct compat_nfs_string *src)
 737{
 738        dst->data = compat_ptr(src->data);
 739        dst->len = src->len;
 740}
 741
 742struct compat_nfs4_mount_data_v1 {
 743        compat_int_t version;
 744        compat_int_t flags;
 745        compat_int_t rsize;
 746        compat_int_t wsize;
 747        compat_int_t timeo;
 748        compat_int_t retrans;
 749        compat_int_t acregmin;
 750        compat_int_t acregmax;
 751        compat_int_t acdirmin;
 752        compat_int_t acdirmax;
 753        struct compat_nfs_string client_addr;
 754        struct compat_nfs_string mnt_path;
 755        struct compat_nfs_string hostname;
 756        compat_uint_t host_addrlen;
 757        compat_uptr_t host_addr;
 758        compat_int_t proto;
 759        compat_int_t auth_flavourlen;
 760        compat_uptr_t auth_flavours;
 761};
 762
 763static int do_nfs4_super_data_conv(void *raw_data)
 764{
 765        int version = *(compat_uint_t *) raw_data;
 766
 767        if (version == 1) {
 768                struct compat_nfs4_mount_data_v1 *raw = raw_data;
 769                struct nfs4_mount_data *real = raw_data;
 770
 771                /* copy the fields backwards */
 772                real->auth_flavours = compat_ptr(raw->auth_flavours);
 773                real->auth_flavourlen = raw->auth_flavourlen;
 774                real->proto = raw->proto;
 775                real->host_addr = compat_ptr(raw->host_addr);
 776                real->host_addrlen = raw->host_addrlen;
 777                compat_nfs_string(&real->hostname, &raw->hostname);
 778                compat_nfs_string(&real->mnt_path, &raw->mnt_path);
 779                compat_nfs_string(&real->client_addr, &raw->client_addr);
 780                real->acdirmax = raw->acdirmax;
 781                real->acdirmin = raw->acdirmin;
 782                real->acregmax = raw->acregmax;
 783                real->acregmin = raw->acregmin;
 784                real->retrans = raw->retrans;
 785                real->timeo = raw->timeo;
 786                real->wsize = raw->wsize;
 787                real->rsize = raw->rsize;
 788                real->flags = raw->flags;
 789                real->version = raw->version;
 790        }
 791
 792        return 0;
 793}
 794
 795#define NCPFS_NAME      "ncpfs"
 796#define NFS4_NAME       "nfs4"
 797
 798asmlinkage long compat_sys_mount(const char __user * dev_name,
 799                                 const char __user * dir_name,
 800                                 const char __user * type, unsigned long flags,
 801                                 const void __user * data)
 802{
 803        char *kernel_type;
 804        void *options;
 805        char *kernel_dev;
 806        int retval;
 807
 808        kernel_type = copy_mount_string(type);
 809        retval = PTR_ERR(kernel_type);
 810        if (IS_ERR(kernel_type))
 811                goto out;
 812
 813        kernel_dev = copy_mount_string(dev_name);
 814        retval = PTR_ERR(kernel_dev);
 815        if (IS_ERR(kernel_dev))
 816                goto out1;
 817
 818        options = copy_mount_options(data);
 819        retval = PTR_ERR(options);
 820        if (IS_ERR(options))
 821                goto out2;
 822
 823        if (kernel_type && options) {
 824                if (!strcmp(kernel_type, NCPFS_NAME)) {
 825                        do_ncp_super_data_conv(options);
 826                } else if (!strcmp(kernel_type, NFS4_NAME)) {
 827                        retval = -EINVAL;
 828                        if (do_nfs4_super_data_conv(options))
 829                                goto out3;
 830                }
 831        }
 832
 833        retval = do_mount(kernel_dev, dir_name, kernel_type, flags, options);
 834
 835 out3:
 836        kfree(options);
 837 out2:
 838        kfree(kernel_dev);
 839 out1:
 840        kfree(kernel_type);
 841 out:
 842        return retval;
 843}
 844
 845struct compat_old_linux_dirent {
 846        compat_ulong_t  d_ino;
 847        compat_ulong_t  d_offset;
 848        unsigned short  d_namlen;
 849        char            d_name[1];
 850};
 851
 852struct compat_readdir_callback {
 853        struct dir_context ctx;
 854        struct compat_old_linux_dirent __user *dirent;
 855        int result;
 856};
 857
 858static int compat_fillonedir(void *__buf, const char *name, int namlen,
 859                        loff_t offset, u64 ino, unsigned int d_type)
 860{
 861        struct compat_readdir_callback *buf = __buf;
 862        struct compat_old_linux_dirent __user *dirent;
 863        compat_ulong_t d_ino;
 864
 865        if (buf->result)
 866                return -EINVAL;
 867        d_ino = ino;
 868        if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
 869                buf->result = -EOVERFLOW;
 870                return -EOVERFLOW;
 871        }
 872        buf->result++;
 873        dirent = buf->dirent;
 874        if (!access_ok(VERIFY_WRITE, dirent,
 875                        (unsigned long)(dirent->d_name + namlen + 1) -
 876                                (unsigned long)dirent))
 877                goto efault;
 878        if (    __put_user(d_ino, &dirent->d_ino) ||
 879                __put_user(offset, &dirent->d_offset) ||
 880                __put_user(namlen, &dirent->d_namlen) ||
 881                __copy_to_user(dirent->d_name, name, namlen) ||
 882                __put_user(0, dirent->d_name + namlen))
 883                goto efault;
 884        return 0;
 885efault:
 886        buf->result = -EFAULT;
 887        return -EFAULT;
 888}
 889
 890asmlinkage long compat_sys_old_readdir(unsigned int fd,
 891        struct compat_old_linux_dirent __user *dirent, unsigned int count)
 892{
 893        int error;
 894        struct fd f = fdget(fd);
 895        struct compat_readdir_callback buf;
 896
 897        if (!f.file)
 898                return -EBADF;
 899
 900        buf.result = 0;
 901        buf.dirent = dirent;
 902        buf.ctx.actor = compat_fillonedir;
 903
 904        error = iterate_dir(f.file, &buf.ctx);
 905        if (buf.result)
 906                error = buf.result;
 907
 908        fdput(f);
 909        return error;
 910}
 911
 912struct compat_linux_dirent {
 913        compat_ulong_t  d_ino;
 914        compat_ulong_t  d_off;
 915        unsigned short  d_reclen;
 916        char            d_name[1];
 917};
 918
 919struct compat_getdents_callback {
 920        struct dir_context ctx;
 921        struct compat_linux_dirent __user *current_dir;
 922        struct compat_linux_dirent __user *previous;
 923        int count;
 924        int error;
 925};
 926
 927static int compat_filldir(void *__buf, const char *name, int namlen,
 928                loff_t offset, u64 ino, unsigned int d_type)
 929{
 930        struct compat_linux_dirent __user * dirent;
 931        struct compat_getdents_callback *buf = __buf;
 932        compat_ulong_t d_ino;
 933        int reclen = ALIGN(offsetof(struct compat_linux_dirent, d_name) +
 934                namlen + 2, sizeof(compat_long_t));
 935
 936        buf->error = -EINVAL;   /* only used if we fail.. */
 937        if (reclen > buf->count)
 938                return -EINVAL;
 939        d_ino = ino;
 940        if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
 941                buf->error = -EOVERFLOW;
 942                return -EOVERFLOW;
 943        }
 944        dirent = buf->previous;
 945        if (dirent) {
 946                if (__put_user(offset, &dirent->d_off))
 947                        goto efault;
 948        }
 949        dirent = buf->current_dir;
 950        if (__put_user(d_ino, &dirent->d_ino))
 951                goto efault;
 952        if (__put_user(reclen, &dirent->d_reclen))
 953                goto efault;
 954        if (copy_to_user(dirent->d_name, name, namlen))
 955                goto efault;
 956        if (__put_user(0, dirent->d_name + namlen))
 957                goto efault;
 958        if (__put_user(d_type, (char  __user *) dirent + reclen - 1))
 959                goto efault;
 960        buf->previous = dirent;
 961        dirent = (void __user *)dirent + reclen;
 962        buf->current_dir = dirent;
 963        buf->count -= reclen;
 964        return 0;
 965efault:
 966        buf->error = -EFAULT;
 967        return -EFAULT;
 968}
 969
 970asmlinkage long compat_sys_getdents(unsigned int fd,
 971                struct compat_linux_dirent __user *dirent, unsigned int count)
 972{
 973        struct fd f;
 974        struct compat_linux_dirent __user * lastdirent;
 975        struct compat_getdents_callback buf;
 976        int error;
 977
 978        if (!access_ok(VERIFY_WRITE, dirent, count))
 979                return -EFAULT;
 980
 981        f = fdget(fd);
 982        if (!f.file)
 983                return -EBADF;
 984
 985        buf.current_dir = dirent;
 986        buf.previous = NULL;
 987        buf.count = count;
 988        buf.error = 0;
 989        buf.ctx.actor = compat_filldir;
 990
 991        error = iterate_dir(f.file, &buf.ctx);
 992        if (error >= 0)
 993                error = buf.error;
 994        lastdirent = buf.previous;
 995        if (lastdirent) {
 996                if (put_user(buf.ctx.pos, &lastdirent->d_off))
 997                        error = -EFAULT;
 998                else
 999                        error = count - buf.count;
1000        }
1001        fdput(f);
1002        return error;
1003}
1004
1005#ifndef __ARCH_OMIT_COMPAT_SYS_GETDENTS64
1006
1007struct compat_getdents_callback64 {
1008        struct dir_context ctx;
1009        struct linux_dirent64 __user *current_dir;
1010        struct linux_dirent64 __user *previous;
1011        int count;
1012        int error;
1013};
1014
1015static int compat_filldir64(void * __buf, const char * name, int namlen, loff_t offset,
1016                     u64 ino, unsigned int d_type)
1017{
1018        struct linux_dirent64 __user *dirent;
1019        struct compat_getdents_callback64 *buf = __buf;
1020        int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1,
1021                sizeof(u64));
1022        u64 off;
1023
1024        buf->error = -EINVAL;   /* only used if we fail.. */
1025        if (reclen > buf->count)
1026                return -EINVAL;
1027        dirent = buf->previous;
1028
1029        if (dirent) {
1030                if (__put_user_unaligned(offset, &dirent->d_off))
1031                        goto efault;
1032        }
1033        dirent = buf->current_dir;
1034        if (__put_user_unaligned(ino, &dirent->d_ino))
1035                goto efault;
1036        off = 0;
1037        if (__put_user_unaligned(off, &dirent->d_off))
1038                goto efault;
1039        if (__put_user(reclen, &dirent->d_reclen))
1040                goto efault;
1041        if (__put_user(d_type, &dirent->d_type))
1042                goto efault;
1043        if (copy_to_user(dirent->d_name, name, namlen))
1044                goto efault;
1045        if (__put_user(0, dirent->d_name + namlen))
1046                goto efault;
1047        buf->previous = dirent;
1048        dirent = (void __user *)dirent + reclen;
1049        buf->current_dir = dirent;
1050        buf->count -= reclen;
1051        return 0;
1052efault:
1053        buf->error = -EFAULT;
1054        return -EFAULT;
1055}
1056
1057asmlinkage long compat_sys_getdents64(unsigned int fd,
1058                struct linux_dirent64 __user * dirent, unsigned int count)
1059{
1060        struct fd f;
1061        struct linux_dirent64 __user * lastdirent;
1062        struct compat_getdents_callback64 buf;
1063        int error;
1064
1065        if (!access_ok(VERIFY_WRITE, dirent, count))
1066                return -EFAULT;
1067
1068        f = fdget(fd);
1069        if (!f.file)
1070                return -EBADF;
1071
1072        buf.current_dir = dirent;
1073        buf.previous = NULL;
1074        buf.count = count;
1075        buf.error = 0;
1076        buf.ctx.actor = compat_filldir64;
1077
1078        error = iterate_dir(f.file, &buf.ctx);
1079        if (error >= 0)
1080                error = buf.error;
1081        lastdirent = buf.previous;
1082        if (lastdirent) {
1083                typeof(lastdirent->d_off) d_off = buf.ctx.pos;
1084                if (__put_user_unaligned(d_off, &lastdirent->d_off))
1085                        error = -EFAULT;
1086                else
1087                        error = count - buf.count;
1088        }
1089        fdput(f);
1090        return error;
1091}
1092#endif /* ! __ARCH_OMIT_COMPAT_SYS_GETDENTS64 */
1093
1094/*
1095 * Exactly like fs/open.c:sys_open(), except that it doesn't set the
1096 * O_LARGEFILE flag.
1097 */
1098COMPAT_SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
1099{
1100        return do_sys_open(AT_FDCWD, filename, flags, mode);
1101}
1102
1103/*
1104 * Exactly like fs/open.c:sys_openat(), except that it doesn't set the
1105 * O_LARGEFILE flag.
1106 */
1107COMPAT_SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags, umode_t, mode)
1108{
1109        return do_sys_open(dfd, filename, flags, mode);
1110}
1111
1112#define __COMPAT_NFDBITS       (8 * sizeof(compat_ulong_t))
1113
1114static int poll_select_copy_remaining(struct timespec *end_time, void __user *p,
1115                                      int timeval, int ret)
1116{
1117        struct timespec ts;
1118
1119        if (!p)
1120                return ret;
1121
1122        if (current->personality & STICKY_TIMEOUTS)
1123                goto sticky;
1124
1125        /* No update for zero timeout */
1126        if (!end_time->tv_sec && !end_time->tv_nsec)
1127                return ret;
1128
1129        ktime_get_ts(&ts);
1130        ts = timespec_sub(*end_time, ts);
1131        if (ts.tv_sec < 0)
1132                ts.tv_sec = ts.tv_nsec = 0;
1133
1134        if (timeval) {
1135                struct compat_timeval rtv;
1136
1137                rtv.tv_sec = ts.tv_sec;
1138                rtv.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
1139
1140                if (!copy_to_user(p, &rtv, sizeof(rtv)))
1141                        return ret;
1142        } else {
1143                struct compat_timespec rts;
1144
1145                rts.tv_sec = ts.tv_sec;
1146                rts.tv_nsec = ts.tv_nsec;
1147
1148                if (!copy_to_user(p, &rts, sizeof(rts)))
1149                        return ret;
1150        }
1151        /*
1152         * If an application puts its timeval in read-only memory, we
1153         * don't want the Linux-specific update to the timeval to
1154         * cause a fault after the select has completed
1155         * successfully. However, because we're not updating the
1156         * timeval, we can't restart the system call.
1157         */
1158
1159sticky:
1160        if (ret == -ERESTARTNOHAND)
1161                ret = -EINTR;
1162        return ret;
1163}
1164
1165/*
1166 * Ooo, nasty.  We need here to frob 32-bit unsigned longs to
1167 * 64-bit unsigned longs.
1168 */
1169static
1170int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1171                        unsigned long *fdset)
1172{
1173        nr = DIV_ROUND_UP(nr, __COMPAT_NFDBITS);
1174        if (ufdset) {
1175                unsigned long odd;
1176
1177                if (!access_ok(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t)))
1178                        return -EFAULT;
1179
1180                odd = nr & 1UL;
1181                nr &= ~1UL;
1182                while (nr) {
1183                        unsigned long h, l;
1184                        if (__get_user(l, ufdset) || __get_user(h, ufdset+1))
1185                                return -EFAULT;
1186                        ufdset += 2;
1187                        *fdset++ = h << 32 | l;
1188                        nr -= 2;
1189                }
1190                if (odd && __get_user(*fdset, ufdset))
1191                        return -EFAULT;
1192        } else {
1193                /* Tricky, must clear full unsigned long in the
1194                 * kernel fdset at the end, this makes sure that
1195                 * actually happens.
1196                 */
1197                memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t));
1198        }
1199        return 0;
1200}
1201
1202static
1203int compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1204                      unsigned long *fdset)
1205{
1206        unsigned long odd;
1207        nr = DIV_ROUND_UP(nr, __COMPAT_NFDBITS);
1208
1209        if (!ufdset)
1210                return 0;
1211
1212        odd = nr & 1UL;
1213        nr &= ~1UL;
1214        while (nr) {
1215                unsigned long h, l;
1216                l = *fdset++;
1217                h = l >> 32;
1218                if (__put_user(l, ufdset) || __put_user(h, ufdset+1))
1219                        return -EFAULT;
1220                ufdset += 2;
1221                nr -= 2;
1222        }
1223        if (odd && __put_user(*fdset, ufdset))
1224                return -EFAULT;
1225        return 0;
1226}
1227
1228
1229/*
1230 * This is a virtual copy of sys_select from fs/select.c and probably
1231 * should be compared to it from time to time
1232 */
1233
1234/*
1235 * We can actually return ERESTARTSYS instead of EINTR, but I'd
1236 * like to be certain this leads to no problems. So I return
1237 * EINTR just for safety.
1238 *
1239 * Update: ERESTARTSYS breaks at least the xview clock binary, so
1240 * I'm trying ERESTARTNOHAND which restart only when you want to.
1241 */
1242int compat_core_sys_select(int n, compat_ulong_t __user *inp,
1243        compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1244        struct timespec *end_time)
1245{
1246        fd_set_bits fds;
1247        void *bits;
1248        int size, max_fds, ret = -EINVAL;
1249        struct fdtable *fdt;
1250        long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
1251
1252        if (n < 0)
1253                goto out_nofds;
1254
1255        /* max_fds can increase, so grab it once to avoid race */
1256        rcu_read_lock();
1257        fdt = files_fdtable(current->files);
1258        max_fds = fdt->max_fds;
1259        rcu_read_unlock();
1260        if (n > max_fds)
1261                n = max_fds;
1262
1263        /*
1264         * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
1265         * since we used fdset we need to allocate memory in units of
1266         * long-words.
1267         */
1268        size = FDS_BYTES(n);
1269        bits = stack_fds;
1270        if (size > sizeof(stack_fds) / 6) {
1271                bits = kmalloc(6 * size, GFP_KERNEL);
1272                ret = -ENOMEM;
1273                if (!bits)
1274                        goto out_nofds;
1275        }
1276        fds.in      = (unsigned long *)  bits;
1277        fds.out     = (unsigned long *) (bits +   size);
1278        fds.ex      = (unsigned long *) (bits + 2*size);
1279        fds.res_in  = (unsigned long *) (bits + 3*size);
1280        fds.res_out = (unsigned long *) (bits + 4*size);
1281        fds.res_ex  = (unsigned long *) (bits + 5*size);
1282
1283        if ((ret = compat_get_fd_set(n, inp, fds.in)) ||
1284            (ret = compat_get_fd_set(n, outp, fds.out)) ||
1285            (ret = compat_get_fd_set(n, exp, fds.ex)))
1286                goto out;
1287        zero_fd_set(n, fds.res_in);
1288        zero_fd_set(n, fds.res_out);
1289        zero_fd_set(n, fds.res_ex);
1290
1291        ret = do_select(n, &fds, end_time);
1292
1293        if (ret < 0)
1294                goto out;
1295        if (!ret) {
1296                ret = -ERESTARTNOHAND;
1297                if (signal_pending(current))
1298                        goto out;
1299                ret = 0;
1300        }
1301
1302        if (compat_set_fd_set(n, inp, fds.res_in) ||
1303            compat_set_fd_set(n, outp, fds.res_out) ||
1304            compat_set_fd_set(n, exp, fds.res_ex))
1305                ret = -EFAULT;
1306out:
1307        if (bits != stack_fds)
1308                kfree(bits);
1309out_nofds:
1310        return ret;
1311}
1312
1313asmlinkage long compat_sys_select(int n, compat_ulong_t __user *inp,
1314        compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1315        struct compat_timeval __user *tvp)
1316{
1317        struct timespec end_time, *to = NULL;
1318        struct compat_timeval tv;
1319        int ret;
1320
1321        if (tvp) {
1322                if (copy_from_user(&tv, tvp, sizeof(tv)))
1323                        return -EFAULT;
1324
1325                to = &end_time;
1326                if (poll_select_set_timeout(to,
1327                                tv.tv_sec + (tv.tv_usec / USEC_PER_SEC),
1328                                (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC))
1329                        return -EINVAL;
1330        }
1331
1332        ret = compat_core_sys_select(n, inp, outp, exp, to);
1333        ret = poll_select_copy_remaining(&end_time, tvp, 1, ret);
1334
1335        return ret;
1336}
1337
1338struct compat_sel_arg_struct {
1339        compat_ulong_t n;
1340        compat_uptr_t inp;
1341        compat_uptr_t outp;
1342        compat_uptr_t exp;
1343        compat_uptr_t tvp;
1344};
1345
1346asmlinkage long compat_sys_old_select(struct compat_sel_arg_struct __user *arg)
1347{
1348        struct compat_sel_arg_struct a;
1349
1350        if (copy_from_user(&a, arg, sizeof(a)))
1351                return -EFAULT;
1352        return compat_sys_select(a.n, compat_ptr(a.inp), compat_ptr(a.outp),
1353                                 compat_ptr(a.exp), compat_ptr(a.tvp));
1354}
1355
1356static long do_compat_pselect(int n, compat_ulong_t __user *inp,
1357        compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1358        struct compat_timespec __user *tsp, compat_sigset_t __user *sigmask,
1359        compat_size_t sigsetsize)
1360{
1361        compat_sigset_t ss32;
1362        sigset_t ksigmask, sigsaved;
1363        struct compat_timespec ts;
1364        struct timespec end_time, *to = NULL;
1365        int ret;
1366
1367        if (tsp) {
1368                if (copy_from_user(&ts, tsp, sizeof(ts)))
1369                        return -EFAULT;
1370
1371                to = &end_time;
1372                if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
1373                        return -EINVAL;
1374        }
1375
1376        if (sigmask) {
1377                if (sigsetsize != sizeof(compat_sigset_t))
1378                        return -EINVAL;
1379                if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
1380                        return -EFAULT;
1381                sigset_from_compat(&ksigmask, &ss32);
1382
1383                sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
1384                sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1385        }
1386
1387        ret = compat_core_sys_select(n, inp, outp, exp, to);
1388        ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
1389
1390        if (ret == -ERESTARTNOHAND) {
1391                /*
1392                 * Don't restore the signal mask yet. Let do_signal() deliver
1393                 * the signal on the way back to userspace, before the signal
1394                 * mask is restored.
1395                 */
1396                if (sigmask) {
1397                        memcpy(&current->saved_sigmask, &sigsaved,
1398                                        sizeof(sigsaved));
1399                        set_restore_sigmask();
1400                }
1401        } else if (sigmask)
1402                sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1403
1404        return ret;
1405}
1406
1407asmlinkage long compat_sys_pselect6(int n, compat_ulong_t __user *inp,
1408        compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1409        struct compat_timespec __user *tsp, void __user *sig)
1410{
1411        compat_size_t sigsetsize = 0;
1412        compat_uptr_t up = 0;
1413
1414        if (sig) {
1415                if (!access_ok(VERIFY_READ, sig,
1416                                sizeof(compat_uptr_t)+sizeof(compat_size_t)) ||
1417                        __get_user(up, (compat_uptr_t __user *)sig) ||
1418                        __get_user(sigsetsize,
1419                                (compat_size_t __user *)(sig+sizeof(up))))
1420                        return -EFAULT;
1421        }
1422        return do_compat_pselect(n, inp, outp, exp, tsp, compat_ptr(up),
1423                                 sigsetsize);
1424}
1425
1426asmlinkage long compat_sys_ppoll(struct pollfd __user *ufds,
1427        unsigned int nfds, struct compat_timespec __user *tsp,
1428        const compat_sigset_t __user *sigmask, compat_size_t sigsetsize)
1429{
1430        compat_sigset_t ss32;
1431        sigset_t ksigmask, sigsaved;
1432        struct compat_timespec ts;
1433        struct timespec end_time, *to = NULL;
1434        int ret;
1435
1436        if (tsp) {
1437                if (copy_from_user(&ts, tsp, sizeof(ts)))
1438                        return -EFAULT;
1439
1440                to = &end_time;
1441                if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
1442                        return -EINVAL;
1443        }
1444
1445        if (sigmask) {
1446                if (sigsetsize != sizeof(compat_sigset_t))
1447                        return -EINVAL;
1448                if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
1449                        return -EFAULT;
1450                sigset_from_compat(&ksigmask, &ss32);
1451
1452                sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
1453                sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1454        }
1455
1456        ret = do_sys_poll(ufds, nfds, to);
1457
1458        /* We can restart this syscall, usually */
1459        if (ret == -EINTR) {
1460                /*
1461                 * Don't restore the signal mask yet. Let do_signal() deliver
1462                 * the signal on the way back to userspace, before the signal
1463                 * mask is restored.
1464                 */
1465                if (sigmask) {
1466                        memcpy(&current->saved_sigmask, &sigsaved,
1467                                sizeof(sigsaved));
1468                        set_restore_sigmask();
1469                }
1470                ret = -ERESTARTNOHAND;
1471        } else if (sigmask)
1472                sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1473
1474        ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
1475
1476        return ret;
1477}
1478
1479#ifdef CONFIG_FHANDLE
1480/*
1481 * Exactly like fs/open.c:sys_open_by_handle_at(), except that it
1482 * doesn't set the O_LARGEFILE flag.
1483 */
1484COMPAT_SYSCALL_DEFINE3(open_by_handle_at, int, mountdirfd,
1485                             struct file_handle __user *, handle, int, flags)
1486{
1487        return do_handle_open(mountdirfd, handle, flags);
1488}
1489#endif
1490