Skip to content
Snippets Groups Projects
compat.c 55.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • Linus Torvalds's avatar
    Linus Torvalds committed
    
    	buf->error = -EINVAL;	/* only used if we fail.. */
    	if (reclen > buf->count)
    		return -EINVAL;
    	dirent = buf->previous;
    
    	if (dirent) {
    		if (__put_user_unaligned(offset, &dirent->d_off))
    			goto efault;
    	}
    	dirent = buf->current_dir;
    	if (__put_user_unaligned(ino, &dirent->d_ino))
    		goto efault;
    	off = 0;
    	if (__put_user_unaligned(off, &dirent->d_off))
    		goto efault;
    	if (__put_user(reclen, &dirent->d_reclen))
    		goto efault;
    	if (__put_user(d_type, &dirent->d_type))
    		goto efault;
    	if (copy_to_user(dirent->d_name, name, namlen))
    		goto efault;
    	if (__put_user(0, dirent->d_name + namlen))
    		goto efault;
    	buf->previous = dirent;
    	dirent = (void __user *)dirent + reclen;
    	buf->current_dir = dirent;
    	buf->count -= reclen;
    	return 0;
    efault:
    	buf->error = -EFAULT;
    	return -EFAULT;
    }
    
    asmlinkage long compat_sys_getdents64(unsigned int fd,
    		struct linux_dirent64 __user * dirent, unsigned int count)
    {
    	struct file * file;
    	struct linux_dirent64 __user * lastdirent;
    	struct compat_getdents_callback64 buf;
    	int error;
    
    	error = -EFAULT;
    	if (!access_ok(VERIFY_WRITE, dirent, count))
    		goto out;
    
    	error = -EBADF;
    	file = fget(fd);
    	if (!file)
    		goto out;
    
    	buf.current_dir = dirent;
    	buf.previous = NULL;
    	buf.count = count;
    	buf.error = 0;
    
    	error = vfs_readdir(file, compat_filldir64, &buf);
    
    	if (error >= 0)
    		error = buf.error;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	lastdirent = buf.previous;
    	if (lastdirent) {
    		typeof(lastdirent->d_off) d_off = file->f_pos;
    
    		if (__put_user_unaligned(d_off, &lastdirent->d_off))
    
    			error = -EFAULT;
    		else
    			error = count - buf.count;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    	fput(file);
    out:
    	return error;
    }
    #endif /* ! __ARCH_OMIT_COMPAT_SYS_GETDENTS64 */
    
    static ssize_t compat_do_readv_writev(int type, struct file *file,
    			       const struct compat_iovec __user *uvector,
    			       unsigned long nr_segs, loff_t *pos)
    {
    	compat_ssize_t tot_len;
    	struct iovec iovstack[UIO_FASTIOV];
    	struct iovec *iov=iovstack, *vector;
    	ssize_t ret;
    	int seg;
    	io_fn_t fn;
    	iov_fn_t fnv;
    
    	/*
    	 * SuS says "The readv() function *may* fail if the iovcnt argument
    	 * was less than or equal to 0, or greater than {IOV_MAX}.  Linux has
    	 * traditionally returned zero for zero segments, so...
    	 */
    	ret = 0;
    	if (nr_segs == 0)
    		goto out;
    
    	/*
    	 * First get the "struct iovec" from user memory and
    	 * verify all the pointers
    	 */
    	ret = -EINVAL;
    	if ((nr_segs > UIO_MAXIOV) || (nr_segs <= 0))
    		goto out;
    	if (!file->f_op)
    		goto out;
    	if (nr_segs > UIO_FASTIOV) {
    		ret = -ENOMEM;
    		iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
    		if (!iov)
    			goto out;
    	}
    	ret = -EFAULT;
    	if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
    		goto out;
    
    	/*
    	 * Single unix specification:
    	 * We should -EINVAL if an element length is not >= 0 and fitting an
    	 * ssize_t.  The total length is fitting an ssize_t
    	 *
    	 * Be careful here because iov_len is a size_t not an ssize_t
    	 */
    	tot_len = 0;
    	vector = iov;
    	ret = -EINVAL;
    	for (seg = 0 ; seg < nr_segs; seg++) {
    		compat_ssize_t tmp = tot_len;
    		compat_ssize_t len;
    		compat_uptr_t buf;
    
    		if (__get_user(len, &uvector->iov_len) ||
    		    __get_user(buf, &uvector->iov_base)) {
    			ret = -EFAULT;
    			goto out;
    		}
    		if (len < 0)	/* size_t not fitting an compat_ssize_t .. */
    			goto out;
    		tot_len += len;
    		if (tot_len < tmp) /* maths overflow on the compat_ssize_t */
    			goto out;
    		vector->iov_base = compat_ptr(buf);
    		vector->iov_len = (compat_size_t) len;
    		uvector++;
    		vector++;
    	}
    	if (tot_len == 0) {
    		ret = 0;
    		goto out;
    	}
    
    	ret = rw_verify_area(type, file, pos, tot_len);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		goto out;
    
    	fnv = NULL;
    	if (type == READ) {
    		fn = file->f_op->read;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	} else {
    		fn = (io_fn_t)file->f_op->write;
    
    	if (fnv)
    		ret = do_sync_readv_writev(file, iov, nr_segs, tot_len,
    						pos, fnv);
    	else
    		ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    out:
    	if (iov != iovstack)
    		kfree(iov);
    
    Robert Love's avatar
    Robert Love committed
    	if ((ret + (type == READ)) > 0) {
    
    		struct dentry *dentry = file->f_path.dentry;
    
    Robert Love's avatar
    Robert Love committed
    		if (type == READ)
    			fsnotify_access(dentry);
    		else
    			fsnotify_modify(dentry);
    	}
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return ret;
    }
    
    
    static size_t compat_readv(struct file *file,
    			   const struct compat_iovec __user *vec,
    			   unsigned long vlen, loff_t *pos)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	ssize_t ret = -EBADF;
    
    	if (!(file->f_mode & FMODE_READ))
    		goto out;
    
    	ret = -EINVAL;
    
    	if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		goto out;
    
    
    	ret = compat_do_readv_writev(READ, file, vec, vlen, pos);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    out:
    
    	if (ret > 0)
    		add_rchar(current, ret);
    	inc_syscr(current);
    
    	return ret;
    }
    
    asmlinkage ssize_t
    compat_sys_readv(unsigned long fd, const struct compat_iovec __user *vec,
    		 unsigned long vlen)
    {
    	struct file *file;
    
    	if (!file)
    		return -EBADF;
    	ret = compat_readv(file, vec, vlen, &file->f_pos);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return ret;
    }
    
    
    asmlinkage ssize_t
    compat_sys_preadv(unsigned long fd, const struct compat_iovec __user *vec,
    
    		  unsigned long vlen, u32 pos_low, u32 pos_high)
    
    {
    	loff_t pos = ((loff_t)pos_high << 32) | pos_low;
    	struct file *file;
    
    	ssize_t ret;
    
    	if (pos < 0)
    		return -EINVAL;
    
    	if (!file)
    		return -EBADF;
    	ret = compat_readv(file, vec, vlen, &pos);
    
    static size_t compat_writev(struct file *file,
    			    const struct compat_iovec __user *vec,
    			    unsigned long vlen, loff_t *pos)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	ssize_t ret = -EBADF;
    
    	if (!(file->f_mode & FMODE_WRITE))
    		goto out;
    
    	ret = -EINVAL;
    
    	if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		goto out;
    
    
    	ret = compat_do_readv_writev(WRITE, file, vec, vlen, pos);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    out:
    
    	if (ret > 0)
    		add_wchar(current, ret);
    	inc_syscw(current);
    
    	return ret;
    }
    
    asmlinkage ssize_t
    compat_sys_writev(unsigned long fd, const struct compat_iovec __user *vec,
    		  unsigned long vlen)
    {
    	struct file *file;
    
    	if (!file)
    		return -EBADF;
    	ret = compat_writev(file, vec, vlen, &file->f_pos);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return ret;
    }
    
    
    asmlinkage ssize_t
    compat_sys_pwritev(unsigned long fd, const struct compat_iovec __user *vec,
    
    		   unsigned long vlen, u32 pos_low, u32 pos_high)
    
    {
    	loff_t pos = ((loff_t)pos_high << 32) | pos_low;
    	struct file *file;
    
    	ssize_t ret;
    
    	if (pos < 0)
    		return -EINVAL;
    
    	if (!file)
    		return -EBADF;
    	ret = compat_writev(file, vec, vlen, &pos);
    
    asmlinkage long
    compat_sys_vmsplice(int fd, const struct compat_iovec __user *iov32,
    		    unsigned int nr_segs, unsigned int flags)
    {
    	unsigned i;
    
    	struct iovec __user *iov;
    
    	if (nr_segs > UIO_MAXIOV)
    
    		return -EINVAL;
    	iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec));
    	for (i = 0; i < nr_segs; i++) {
    		struct compat_iovec v;
    		if (get_user(v.iov_base, &iov32[i].iov_base) ||
    		    get_user(v.iov_len, &iov32[i].iov_len) ||
    		    put_user(compat_ptr(v.iov_base), &iov[i].iov_base) ||
    		    put_user(v.iov_len, &iov[i].iov_len))
    			return -EFAULT;
    	}
    	return sys_vmsplice(fd, iov, nr_segs, flags);
    }
    
    
    /*
     * Exactly like fs/open.c:sys_open(), except that it doesn't set the
     * O_LARGEFILE flag.
     */
    asmlinkage long
    compat_sys_open(const char __user *filename, int flags, int mode)
    {
    
    	return do_sys_open(AT_FDCWD, filename, flags, mode);
    }
    
    /*
     * Exactly like fs/open.c:sys_openat(), except that it doesn't set the
     * O_LARGEFILE flag.
     */
    asmlinkage long
    
    compat_sys_openat(unsigned int dfd, const char __user *filename, int flags, int mode)
    
    {
    	return do_sys_open(dfd, filename, flags, mode);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /*
     * compat_count() counts the number of arguments/envelopes. It is basically
     * a copy of count() from fs/exec.c, except that it works with 32 bit argv
     * and envp pointers.
     */
    static int compat_count(compat_uptr_t __user *argv, int max)
    {
    	int i = 0;
    
    	if (argv != NULL) {
    		for (;;) {
    			compat_uptr_t p;
    
    			if (get_user(p, argv))
    				return -EFAULT;
    			if (!p)
    				break;
    			argv++;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				return -E2BIG;
    		}
    	}
    	return i;
    }
    
    /*
     * compat_copy_strings() is basically a copy of copy_strings() from fs/exec.c
     * except that it works with 32 bit argv and envp pointers.
     */
    static int compat_copy_strings(int argc, compat_uptr_t __user *argv,
    				struct linux_binprm *bprm)
    {
    	struct page *kmapped_page = NULL;
    	char *kaddr = NULL;
    
    	unsigned long kpos = 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	int ret;
    
    	while (argc-- > 0) {
    		compat_uptr_t str;
    		int len;
    		unsigned long pos;
    
    		if (get_user(str, argv+argc) ||
    
    		    !(len = strnlen_user(compat_ptr(str), MAX_ARG_STRLEN))) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			ret = -EFAULT;
    			goto out;
    		}
    
    
    		if (len > MAX_ARG_STRLEN) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			ret = -E2BIG;
    			goto out;
    		}
    
    
    		/* We're going to work our way backwords. */
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		pos = bprm->p;
    
    		str += len;
    		bprm->p -= len;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    		while (len > 0) {
    			int offset, bytes_to_copy;
    
    			offset = pos % PAGE_SIZE;
    
    			if (offset == 0)
    				offset = PAGE_SIZE;
    
    			bytes_to_copy = offset;
    			if (bytes_to_copy > len)
    				bytes_to_copy = len;
    
    			offset -= bytes_to_copy;
    			pos -= bytes_to_copy;
    			str -= bytes_to_copy;
    			len -= bytes_to_copy;
    
    			if (!kmapped_page || kpos != (pos & PAGE_MASK)) {
    				struct page *page;
    
    #ifdef CONFIG_STACK_GROWSUP
    				ret = expand_stack_downwards(bprm->vma, pos);
    				if (ret < 0) {
    					/* We've exceed the stack rlimit. */
    					ret = -E2BIG;
    					goto out;
    				}
    #endif
    				ret = get_user_pages(current, bprm->mm, pos,
    						     1, 1, 1, &page, NULL);
    				if (ret <= 0) {
    					/* We've exceed the stack rlimit. */
    					ret = -E2BIG;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    					goto out;
    				}
    
    
    				if (kmapped_page) {
    					flush_kernel_dcache_page(kmapped_page);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    					kunmap(kmapped_page);
    
    					put_page(kmapped_page);
    				}
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				kmapped_page = page;
    				kaddr = kmap(kmapped_page);
    
    				kpos = pos & PAGE_MASK;
    				flush_cache_page(bprm->vma, kpos,
    						 page_to_pfn(kmapped_page));
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			}
    
    			if (copy_from_user(kaddr+offset, compat_ptr(str),
    						bytes_to_copy)) {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				ret = -EFAULT;
    				goto out;
    			}
    		}
    	}
    	ret = 0;
    out:
    
    	if (kmapped_page) {
    		flush_kernel_dcache_page(kmapped_page);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		kunmap(kmapped_page);
    
    		put_page(kmapped_page);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    
    	return ret;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    }
    
    /*
     * compat_do_execve() is mostly a copy of do_execve(), with the exception
     * that it processes 32 bit argv and envp pointers.
     */
    int compat_do_execve(char * filename,
    	compat_uptr_t __user *argv,
    	compat_uptr_t __user *envp,
    	struct pt_regs * regs)
    {
    	struct linux_binprm *bprm;
    	struct file *file;
    
    	struct files_struct *displaced;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	int retval;
    
    
    	retval = unshare_files(&displaced);
    	if (retval)
    		goto out_ret;
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	retval = -ENOMEM;
    
    	bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (!bprm)
    
    		goto out_files;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	retval = prepare_bprm_creds(bprm);
    	if (retval)
    
    
    	retval = check_unsafe_exec(bprm);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	file = open_exec(filename);
    	retval = PTR_ERR(file);
    	if (IS_ERR(file))
    
    		goto out_unmark;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	sched_exec();
    
    	bprm->file = file;
    	bprm->filename = filename;
    	bprm->interp = filename;
    
    
    	retval = bprm_mm_init(bprm);
    	if (retval)
    		goto out_file;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	bprm->argc = compat_count(argv, MAX_ARG_STRINGS);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if ((retval = bprm->argc) < 0)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	bprm->envc = compat_count(envp, MAX_ARG_STRINGS);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if ((retval = bprm->envc) < 0)
    		goto out;
    
    	retval = prepare_binprm(bprm);
    	if (retval < 0)
    		goto out;
    
    	retval = copy_strings_kernel(1, &bprm->filename, bprm);
    	if (retval < 0)
    		goto out;
    
    	bprm->exec = bprm->p;
    	retval = compat_copy_strings(bprm->envc, envp, bprm);
    	if (retval < 0)
    		goto out;
    
    	retval = compat_copy_strings(bprm->argc, argv, bprm);
    	if (retval < 0)
    		goto out;
    
    	retval = search_binary_handler(bprm, regs);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	current->fs->in_exec = 0;
    
    	current->in_execve = 0;
    
    	acct_update_integrals(current);
    	free_bprm(bprm);
    
    	if (displaced)
    		put_files_struct(displaced);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (bprm->mm)
    
    		mmput(bprm->mm);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    out_file:
    	if (bprm->file) {
    		allow_write_access(bprm->file);
    		fput(bprm->file);
    	}
    
    
    out_unmark:
    
    	if (clear_in_exec)
    		current->fs->in_exec = 0;
    
    	current->in_execve = 0;
    
    	free_bprm(bprm);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    out_files:
    	if (displaced)
    		reset_files_struct(displaced);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    out_ret:
    	return retval;
    }
    
    #define __COMPAT_NFDBITS       (8 * sizeof(compat_ulong_t))
    
    
    static int poll_select_copy_remaining(struct timespec *end_time, void __user *p,
    				      int timeval, int ret)
    {
    	struct timespec ts;
    
    	if (!p)
    		return ret;
    
    	if (current->personality & STICKY_TIMEOUTS)
    		goto sticky;
    
    	/* No update for zero timeout */
    	if (!end_time->tv_sec && !end_time->tv_nsec)
    		return ret;
    
    	ktime_get_ts(&ts);
    	ts = timespec_sub(*end_time, ts);
    	if (ts.tv_sec < 0)
    		ts.tv_sec = ts.tv_nsec = 0;
    
    	if (timeval) {
    		struct compat_timeval rtv;
    
    		rtv.tv_sec = ts.tv_sec;
    		rtv.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
    
    		if (!copy_to_user(p, &rtv, sizeof(rtv)))
    			return ret;
    	} else {
    		struct compat_timespec rts;
    
    		rts.tv_sec = ts.tv_sec;
    		rts.tv_nsec = ts.tv_nsec;
    
    		if (!copy_to_user(p, &rts, sizeof(rts)))
    			return ret;
    	}
    	/*
    	 * If an application puts its timeval in read-only memory, we
    	 * don't want the Linux-specific update to the timeval to
    	 * cause a fault after the select has completed
    	 * successfully. However, because we're not updating the
    	 * timeval, we can't restart the system call.
    	 */
    
    sticky:
    	if (ret == -ERESTARTNOHAND)
    		ret = -EINTR;
    	return ret;
    }
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /*
     * Ooo, nasty.  We need here to frob 32-bit unsigned longs to
     * 64-bit unsigned longs.
     */
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
    			unsigned long *fdset)
    {
    
    	nr = DIV_ROUND_UP(nr, __COMPAT_NFDBITS);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (ufdset) {
    		unsigned long odd;
    
    		if (!access_ok(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t)))
    			return -EFAULT;
    
    		odd = nr & 1UL;
    		nr &= ~1UL;
    		while (nr) {
    			unsigned long h, l;
    
    			if (__get_user(l, ufdset) || __get_user(h, ufdset+1))
    				return -EFAULT;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			ufdset += 2;
    			*fdset++ = h << 32 | l;
    			nr -= 2;
    		}
    
    		if (odd && __get_user(*fdset, ufdset))
    			return -EFAULT;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	} else {
    		/* Tricky, must clear full unsigned long in the
    		 * kernel fdset at the end, this makes sure that
    		 * actually happens.
    		 */
    		memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t));
    	}
    	return 0;
    }
    
    
    int compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
    		      unsigned long *fdset)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	unsigned long odd;
    
    	nr = DIV_ROUND_UP(nr, __COMPAT_NFDBITS);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	if (!ufdset)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	odd = nr & 1UL;
    	nr &= ~1UL;
    	while (nr) {
    		unsigned long h, l;
    		l = *fdset++;
    		h = l >> 32;
    
    		if (__put_user(l, ufdset) || __put_user(h, ufdset+1))
    			return -EFAULT;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		ufdset += 2;
    		nr -= 2;
    	}
    
    	if (odd && __put_user(*fdset, ufdset))
    		return -EFAULT;
    	return 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    }
    
    
    /*
     * This is a virtual copy of sys_select from fs/select.c and probably
     * should be compared to it from time to time
     */
    
    /*
     * We can actually return ERESTARTSYS instead of EINTR, but I'd
     * like to be certain this leads to no problems. So I return
     * EINTR just for safety.
     *
     * Update: ERESTARTSYS breaks at least the xview clock binary, so
     * I'm trying ERESTARTNOHAND which restart only when you want to.
     */
    #define MAX_SELECT_SECONDS \
    	((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
    
    
    int compat_core_sys_select(int n, compat_ulong_t __user *inp,
    
    	compat_ulong_t __user *outp, compat_ulong_t __user *exp,
    	struct timespec *end_time)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	fd_set_bits fds;
    
    	int size, max_fds, ret = -EINVAL;
    
    	long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	if (n < 0)
    		goto out_nofds;
    
    
    	/* max_fds can increase, so grab it once to avoid race */
    
    	rcu_read_lock();
    
    	fdt = files_fdtable(current->files);
    
    	max_fds = fdt->max_fds;
    
    	rcu_read_unlock();
    
    	if (n > max_fds)
    		n = max_fds;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	/*
    	 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
    	 * since we used fdset we need to allocate memory in units of
    	 * long-words.
    	 */
    	size = FDS_BYTES(n);
    
    	bits = stack_fds;
    	if (size > sizeof(stack_fds) / 6) {
    		bits = kmalloc(6 * size, GFP_KERNEL);
    		ret = -ENOMEM;
    		if (!bits)
    			goto out_nofds;
    	}
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	fds.in      = (unsigned long *)  bits;
    	fds.out     = (unsigned long *) (bits +   size);
    	fds.ex      = (unsigned long *) (bits + 2*size);
    	fds.res_in  = (unsigned long *) (bits + 3*size);
    	fds.res_out = (unsigned long *) (bits + 4*size);
    	fds.res_ex  = (unsigned long *) (bits + 5*size);
    
    	if ((ret = compat_get_fd_set(n, inp, fds.in)) ||
    	    (ret = compat_get_fd_set(n, outp, fds.out)) ||
    	    (ret = compat_get_fd_set(n, exp, fds.ex)))
    		goto out;
    	zero_fd_set(n, fds.res_in);
    	zero_fd_set(n, fds.res_out);
    	zero_fd_set(n, fds.res_ex);
    
    
    	ret = do_select(n, &fds, end_time);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	if (ret < 0)
    		goto out;
    	if (!ret) {
    		ret = -ERESTARTNOHAND;
    		if (signal_pending(current))
    			goto out;
    		ret = 0;
    	}
    
    
    	if (compat_set_fd_set(n, inp, fds.res_in) ||
    	    compat_set_fd_set(n, outp, fds.res_out) ||
    	    compat_set_fd_set(n, exp, fds.res_ex))
    		ret = -EFAULT;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    out:
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    out_nofds:
    	return ret;
    }
    
    
    asmlinkage long compat_sys_select(int n, compat_ulong_t __user *inp,
    	compat_ulong_t __user *outp, compat_ulong_t __user *exp,
    	struct compat_timeval __user *tvp)
    {
    
    	struct timespec end_time, *to = NULL;
    
    	struct compat_timeval tv;
    	int ret;
    
    	if (tvp) {
    		if (copy_from_user(&tv, tvp, sizeof(tv)))
    			return -EFAULT;
    
    
    		if (poll_select_set_timeout(to,
    				tv.tv_sec + (tv.tv_usec / USEC_PER_SEC),
    				(tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC))
    
    	ret = compat_core_sys_select(n, inp, outp, exp, to);
    	ret = poll_select_copy_remaining(&end_time, tvp, 1, ret);
    
    struct compat_sel_arg_struct {
    	compat_ulong_t n;
    	compat_uptr_t inp;
    	compat_uptr_t outp;
    	compat_uptr_t exp;
    	compat_uptr_t tvp;
    };
    
    asmlinkage long compat_sys_old_select(struct compat_sel_arg_struct __user *arg)
    {
    	struct compat_sel_arg_struct a;
    
    	if (copy_from_user(&a, arg, sizeof(a)))
    		return -EFAULT;
    	return compat_sys_select(a.n, compat_ptr(a.inp), compat_ptr(a.outp),
    				 compat_ptr(a.exp), compat_ptr(a.tvp));
    }
    
    
    #ifdef HAVE_SET_RESTORE_SIGMASK
    
    static long do_compat_pselect(int n, compat_ulong_t __user *inp,
    
    	compat_ulong_t __user *outp, compat_ulong_t __user *exp,
    	struct compat_timespec __user *tsp, compat_sigset_t __user *sigmask,
    	compat_size_t sigsetsize)
    {
    	compat_sigset_t ss32;
    	sigset_t ksigmask, sigsaved;
    	struct compat_timespec ts;
    
    	struct timespec end_time, *to = NULL;
    
    	int ret;
    
    	if (tsp) {
    		if (copy_from_user(&ts, tsp, sizeof(ts)))
    			return -EFAULT;
    
    
    		to = &end_time;
    		if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
    
    			return -EINVAL;
    	}
    
    	if (sigmask) {
    		if (sigsetsize != sizeof(compat_sigset_t))
    			return -EINVAL;
    		if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
    			return -EFAULT;
    		sigset_from_compat(&ksigmask, &ss32);
    
    		sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
    		sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
    	}
    
    
    	ret = compat_core_sys_select(n, inp, outp, exp, to);
    	ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
    
    
    	if (ret == -ERESTARTNOHAND) {
    		/*
    		 * Don't restore the signal mask yet. Let do_signal() deliver
    		 * the signal on the way back to userspace, before the signal
    		 * mask is restored.
    		 */
    		if (sigmask) {
    			memcpy(&current->saved_sigmask, &sigsaved,
    					sizeof(sigsaved));
    
    			set_restore_sigmask();
    
    		}
    	} else if (sigmask)
    		sigprocmask(SIG_SETMASK, &sigsaved, NULL);
    
    	return ret;
    }
    
    asmlinkage long compat_sys_pselect6(int n, compat_ulong_t __user *inp,
    	compat_ulong_t __user *outp, compat_ulong_t __user *exp,
    	struct compat_timespec __user *tsp, void __user *sig)
    {
    	compat_size_t sigsetsize = 0;
    	compat_uptr_t up = 0;
    
    	if (sig) {
    		if (!access_ok(VERIFY_READ, sig,
    				sizeof(compat_uptr_t)+sizeof(compat_size_t)) ||
    		    	__get_user(up, (compat_uptr_t __user *)sig) ||
    		    	__get_user(sigsetsize,
    				(compat_size_t __user *)(sig+sizeof(up))))
    			return -EFAULT;
    	}
    
    	return do_compat_pselect(n, inp, outp, exp, tsp, compat_ptr(up),
    				 sigsetsize);
    
    }
    
    asmlinkage long compat_sys_ppoll(struct pollfd __user *ufds,
    	unsigned int nfds, struct compat_timespec __user *tsp,
    	const compat_sigset_t __user *sigmask, compat_size_t sigsetsize)
    {
    	compat_sigset_t ss32;
    	sigset_t ksigmask, sigsaved;
    	struct compat_timespec ts;
    
    	struct timespec end_time, *to = NULL;
    
    	int ret;
    
    	if (tsp) {
    		if (copy_from_user(&ts, tsp, sizeof(ts)))
    			return -EFAULT;
    
    
    		to = &end_time;
    		if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
    			return -EINVAL;
    
    		if (sigsetsize != sizeof(compat_sigset_t))
    
    			return -EINVAL;
    		if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
    			return -EFAULT;
    		sigset_from_compat(&ksigmask, &ss32);
    
    		sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
    		sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
    	}
    
    
    	ret = do_sys_poll(ufds, nfds, to);
    
    
    	/* We can restart this syscall, usually */
    	if (ret == -EINTR) {
    		/*
    		 * Don't restore the signal mask yet. Let do_signal() deliver
    		 * the signal on the way back to userspace, before the signal
    		 * mask is restored.
    		 */
    		if (sigmask) {
    			memcpy(&current->saved_sigmask, &sigsaved,
    				sizeof(sigsaved));
    
    			set_restore_sigmask();
    
    		}
    		ret = -ERESTARTNOHAND;
    	} else if (sigmask)
    		sigprocmask(SIG_SETMASK, &sigsaved, NULL);
    
    
    	ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
    
    #endif /* HAVE_SET_RESTORE_SIGMASK */
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    #if defined(CONFIG_NFSD) || defined(CONFIG_NFSD_MODULE)
    /* Stuff for NFS server syscalls... */
    struct compat_nfsctl_svc {
    	u16			svc32_port;
    	s32			svc32_nthreads;
    };
    
    struct compat_nfsctl_client {
    	s8			cl32_ident[NFSCLNT_IDMAX+1];
    	s32			cl32_naddr;
    	struct in_addr		cl32_addrlist[NFSCLNT_ADDRMAX];
    	s32			cl32_fhkeytype;
    	s32			cl32_fhkeylen;
    	u8			cl32_fhkey[NFSCLNT_KEYMAX];
    };
    
    struct compat_nfsctl_export {
    	char		ex32_client[NFSCLNT_IDMAX+1];
    	char		ex32_path[NFS_MAXPATHLEN+1];
    	compat_dev_t	ex32_dev;
    	compat_ino_t	ex32_ino;
    	compat_int_t	ex32_flags;
    
    	__compat_uid_t	ex32_anon_uid;
    	__compat_gid_t	ex32_anon_gid;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    };
    
    struct compat_nfsctl_fdparm {
    	struct sockaddr		gd32_addr;
    	s8			gd32_path[NFS_MAXPATHLEN+1];
    	compat_int_t		gd32_version;
    };
    
    struct compat_nfsctl_fsparm {
    	struct sockaddr		gd32_addr;
    	s8			gd32_path[NFS_MAXPATHLEN+1];
    	compat_int_t		gd32_maxlen;
    };
    
    struct compat_nfsctl_arg {
    	compat_int_t		ca32_version;	/* safeguard */
    	union {
    		struct compat_nfsctl_svc	u32_svc;
    		struct compat_nfsctl_client	u32_client;
    		struct compat_nfsctl_export	u32_export;
    		struct compat_nfsctl_fdparm	u32_getfd;
    		struct compat_nfsctl_fsparm	u32_getfs;
    	} u;
    #define ca32_svc	u.u32_svc
    #define ca32_client	u.u32_client
    #define ca32_export	u.u32_export
    #define ca32_getfd	u.u32_getfd
    #define ca32_getfs	u.u32_getfs
    };
    
    union compat_nfsctl_res {
    	__u8			cr32_getfh[NFS_FHSIZE];
    	struct knfsd_fh		cr32_getfs;
    };
    
    
    static int compat_nfs_svc_trans(struct nfsctl_arg *karg,
    				struct compat_nfsctl_arg __user *arg)