4 * Kernel compatibililty routines for e.g. 32 bit syscall support
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@suse.cz)
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.
18 #include <linux/linkage.h>
19 #include <linux/compat.h>
20 #include <linux/errno.h>
21 #include <linux/time.h>
23 #include <linux/fcntl.h>
24 #include <linux/namei.h>
25 #include <linux/file.h>
26 #include <linux/vfs.h>
27 #include <linux/ioctl32.h>
28 #include <linux/init.h>
29 #include <linux/sockios.h> /* for SIOCDEVPRIVATE */
30 #include <linux/smb.h>
31 #include <linux/smb_mount.h>
32 #include <linux/ncp_mount.h>
33 #include <linux/smp_lock.h>
34 #include <linux/syscalls.h>
35 #include <linux/ctype.h>
36 #include <linux/module.h>
37 #include <linux/dnotify.h>
38 #include <linux/highuid.h>
39 #include <linux/sunrpc/svc.h>
40 #include <linux/nfsd/nfsd.h>
41 #include <linux/nfsd/syscall.h>
42 #include <linux/personality.h>
44 #include <net/sock.h> /* siocdevprivate_ioctl */
46 #include <asm/uaccess.h>
47 #include <asm/mmu_context.h>
50 * Not all architectures have sys_utime, so implement this in terms
53 asmlinkage long compat_sys_utime(char __user *filename, struct compat_utimbuf __user *t)
58 if (get_user(tv[0].tv_sec, &t->actime) ||
59 get_user(tv[1].tv_sec, &t->modtime))
64 return do_utimes(filename, t ? tv : NULL);
67 asmlinkage long compat_sys_utimes(char __user *filename, struct compat_timeval __user *t)
72 if (get_user(tv[0].tv_sec, &t[0].tv_sec) ||
73 get_user(tv[0].tv_usec, &t[0].tv_usec) ||
74 get_user(tv[1].tv_sec, &t[1].tv_sec) ||
75 get_user(tv[1].tv_usec, &t[1].tv_usec))
78 return do_utimes(filename, t ? tv : NULL);
81 asmlinkage long compat_sys_newstat(char __user * filename,
82 struct compat_stat __user *statbuf)
85 int error = vfs_stat(filename, &stat);
88 error = cp_compat_stat(&stat, statbuf);
92 asmlinkage long compat_sys_newlstat(char __user * filename,
93 struct compat_stat __user *statbuf)
96 int error = vfs_lstat(filename, &stat);
99 error = cp_compat_stat(&stat, statbuf);
103 asmlinkage long compat_sys_newfstat(unsigned int fd,
104 struct compat_stat __user * statbuf)
107 int error = vfs_fstat(fd, &stat);
110 error = cp_compat_stat(&stat, statbuf);
114 static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf)
117 if (sizeof ubuf->f_blocks == 4) {
118 if ((kbuf->f_blocks | kbuf->f_bfree |
119 kbuf->f_bavail | kbuf->f_files | kbuf->f_ffree) &
120 0xffffffff00000000ULL)
123 if (verify_area(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
124 __put_user(kbuf->f_type, &ubuf->f_type) ||
125 __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
126 __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
127 __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
128 __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
129 __put_user(kbuf->f_files, &ubuf->f_files) ||
130 __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
131 __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
132 __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
133 __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
134 __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
135 __put_user(0, &ubuf->f_spare[0]) ||
136 __put_user(0, &ubuf->f_spare[1]) ||
137 __put_user(0, &ubuf->f_spare[2]) ||
138 __put_user(0, &ubuf->f_spare[3]) ||
139 __put_user(0, &ubuf->f_spare[4]))
145 * The following statfs calls are copies of code from fs/open.c and
146 * should be checked against those from time to time
148 asmlinkage long compat_sys_statfs(const char __user *path, struct compat_statfs __user *buf)
153 error = user_path_walk(path, &nd);
156 error = vfs_statfs(nd.dentry->d_inode->i_sb, &tmp);
157 if (!error && put_compat_statfs(buf, &tmp))
164 asmlinkage long compat_sys_fstatfs(unsigned int fd, struct compat_statfs __user *buf)
174 error = vfs_statfs(file->f_dentry->d_inode->i_sb, &tmp);
175 if (!error && put_compat_statfs(buf, &tmp))
182 static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf)
184 if (sizeof ubuf->f_blocks == 4) {
185 if ((kbuf->f_blocks | kbuf->f_bfree |
186 kbuf->f_bavail | kbuf->f_files | kbuf->f_ffree) &
187 0xffffffff00000000ULL)
190 if (verify_area(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
191 __put_user(kbuf->f_type, &ubuf->f_type) ||
192 __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
193 __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
194 __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
195 __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
196 __put_user(kbuf->f_files, &ubuf->f_files) ||
197 __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
198 __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
199 __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
200 __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
201 __put_user(kbuf->f_frsize, &ubuf->f_frsize))
206 asmlinkage long compat_statfs64(const char __user *path, compat_size_t sz, struct compat_statfs64 __user *buf)
211 if (sz != sizeof(*buf))
214 error = user_path_walk(path, &nd);
217 error = vfs_statfs(nd.dentry->d_inode->i_sb, &tmp);
218 if (!error && put_compat_statfs64(buf, &tmp))
225 asmlinkage long compat_fstatfs64(unsigned int fd, compat_size_t sz, struct compat_statfs64 __user *buf)
231 if (sz != sizeof(*buf))
238 error = vfs_statfs(file->f_dentry->d_inode->i_sb, &tmp);
239 if (!error && put_compat_statfs64(buf, &tmp))
246 /* ioctl32 stuff, used by sparc64, parisc, s390x, ppc64, x86_64, MIPS */
248 #define IOCTL_HASHSIZE 256
249 struct ioctl_trans *ioctl32_hash_table[IOCTL_HASHSIZE];
251 extern struct ioctl_trans ioctl_start[];
252 extern int ioctl_table_size;
254 static inline unsigned long ioctl32_hash(unsigned long cmd)
256 return (((cmd >> 6) ^ (cmd >> 4) ^ cmd)) % IOCTL_HASHSIZE;
259 static void ioctl32_insert_translation(struct ioctl_trans *trans)
262 struct ioctl_trans *t;
264 hash = ioctl32_hash (trans->cmd);
265 if (!ioctl32_hash_table[hash])
266 ioctl32_hash_table[hash] = trans;
268 t = ioctl32_hash_table[hash];
276 static int __init init_sys32_ioctl(void)
280 for (i = 0; i < ioctl_table_size; i++) {
281 if (ioctl_start[i].next != 0) {
282 printk("ioctl translation %d bad\n",i);
286 ioctl32_insert_translation(&ioctl_start[i]);
291 __initcall(init_sys32_ioctl);
293 int register_ioctl32_conversion(unsigned int cmd, int (*handler)(unsigned int,
294 unsigned int, unsigned long, struct file *))
296 struct ioctl_trans *t;
297 struct ioctl_trans *new_t;
298 unsigned long hash = ioctl32_hash(cmd);
300 new_t = kmalloc(sizeof(*new_t), GFP_KERNEL);
305 for (t = ioctl32_hash_table[hash]; t; t = t->next) {
307 printk(KERN_ERR "Trying to register duplicated ioctl32 "
308 "handler %x\n", cmd);
316 new_t->handler = handler;
317 ioctl32_insert_translation(new_t);
322 EXPORT_SYMBOL(register_ioctl32_conversion);
324 static inline int builtin_ioctl(struct ioctl_trans *t)
326 return t >= ioctl_start && t < (ioctl_start + ioctl_table_size);
330 This function cannot unregister duplicate ioctls, because they are not
332 When they happen we need to extend the prototype to pass the handler too. */
334 int unregister_ioctl32_conversion(unsigned int cmd)
336 unsigned long hash = ioctl32_hash(cmd);
337 struct ioctl_trans *t, *t1;
341 t = ioctl32_hash_table[hash];
348 if (builtin_ioctl(t)) {
349 printk("%p tried to unregister builtin ioctl %x\n",
350 __builtin_return_address(0), cmd);
352 ioctl32_hash_table[hash] = t->next;
360 if (t1->cmd == cmd) {
361 if (builtin_ioctl(t1)) {
362 printk("%p tried to unregister builtin "
364 __builtin_return_address(0), cmd);
375 printk(KERN_ERR "Trying to free unknown 32bit ioctl handler %x\n",
381 EXPORT_SYMBOL(unregister_ioctl32_conversion);
383 asmlinkage long compat_sys_ioctl(unsigned int fd, unsigned int cmd,
388 struct ioctl_trans *t;
394 if (!filp->f_op || !filp->f_op->ioctl) {
395 error = sys_ioctl (fd, cmd, arg);
401 t = ioctl32_hash_table[ioctl32_hash (cmd)];
403 while (t && t->cmd != cmd)
407 error = t->handler(fd, cmd, arg, filp);
411 error = sys_ioctl(fd, cmd, arg);
415 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
416 error = siocdevprivate_ioctl(fd, cmd, arg);
424 path = (char *)__get_free_page(GFP_KERNEL);
426 /* find the name of the device. */
428 fn = d_path(filp->f_dentry,
429 filp->f_vfsmnt, path,
433 sprintf(buf,"'%c'", (cmd>>24) & 0x3f);
434 if (!isprint(buf[1]))
435 sprintf(buf, "%02x", buf[1]);
436 printk("ioctl32(%s:%d): Unknown cmd fd(%d) "
437 "cmd(%08x){%s} arg(%08x) on %s\n",
438 current->comm, current->pid,
439 (int)fd, (unsigned int)cmd, buf,
440 (unsigned int)arg, fn);
442 free_page((unsigned long)path);
453 static int get_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
455 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
456 __get_user(kfl->l_type, &ufl->l_type) ||
457 __get_user(kfl->l_whence, &ufl->l_whence) ||
458 __get_user(kfl->l_start, &ufl->l_start) ||
459 __get_user(kfl->l_len, &ufl->l_len) ||
460 __get_user(kfl->l_pid, &ufl->l_pid))
465 static int put_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
467 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
468 __put_user(kfl->l_type, &ufl->l_type) ||
469 __put_user(kfl->l_whence, &ufl->l_whence) ||
470 __put_user(kfl->l_start, &ufl->l_start) ||
471 __put_user(kfl->l_len, &ufl->l_len) ||
472 __put_user(kfl->l_pid, &ufl->l_pid))
477 #ifndef HAVE_ARCH_GET_COMPAT_FLOCK64
478 static int get_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
480 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
481 __get_user(kfl->l_type, &ufl->l_type) ||
482 __get_user(kfl->l_whence, &ufl->l_whence) ||
483 __get_user(kfl->l_start, &ufl->l_start) ||
484 __get_user(kfl->l_len, &ufl->l_len) ||
485 __get_user(kfl->l_pid, &ufl->l_pid))
491 #ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64
492 static int put_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
494 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
495 __put_user(kfl->l_type, &ufl->l_type) ||
496 __put_user(kfl->l_whence, &ufl->l_whence) ||
497 __put_user(kfl->l_start, &ufl->l_start) ||
498 __put_user(kfl->l_len, &ufl->l_len) ||
499 __put_user(kfl->l_pid, &ufl->l_pid))
505 asmlinkage long compat_sys_fcntl64(unsigned int fd, unsigned int cmd,
516 ret = get_compat_flock(&f, compat_ptr(arg));
521 ret = sys_fcntl(fd, cmd, (unsigned long)&f);
523 if ((cmd == F_GETLK) && (ret == 0)) {
524 if ((f.l_start >= COMPAT_OFF_T_MAX) ||
525 ((f.l_start + f.l_len) >= COMPAT_OFF_T_MAX))
528 ret = put_compat_flock(&f, compat_ptr(arg));
535 ret = get_compat_flock64(&f, compat_ptr(arg));
540 ret = sys_fcntl(fd, (cmd == F_GETLK64) ? F_GETLK :
541 ((cmd == F_SETLK64) ? F_SETLK : F_SETLKW),
544 if ((cmd == F_GETLK64) && (ret == 0)) {
545 if ((f.l_start >= COMPAT_LOFF_T_MAX) ||
546 ((f.l_start + f.l_len) >= COMPAT_LOFF_T_MAX))
549 ret = put_compat_flock64(&f, compat_ptr(arg));
554 ret = sys_fcntl(fd, cmd, arg);
560 asmlinkage long compat_sys_fcntl(unsigned int fd, unsigned int cmd,
563 if ((cmd == F_GETLK64) || (cmd == F_SETLK64) || (cmd == F_SETLKW64))
565 return compat_sys_fcntl64(fd, cmd, arg);
569 compat_sys_io_setup(unsigned nr_reqs, u32 __user *ctx32p)
574 mm_segment_t oldfs = get_fs();
575 if (unlikely(get_user(ctx64, ctx32p)))
579 /* The __user pointer cast is valid because of the set_fs() */
580 ret = sys_io_setup(nr_reqs, (aio_context_t __user *) &ctx64);
582 /* truncating is ok because it's a user address */
584 ret = put_user((u32) ctx64, ctx32p);
589 compat_sys_io_getevents(aio_context_t ctx_id,
590 unsigned long min_nr,
592 struct io_event __user *events,
593 struct compat_timespec __user *timeout)
597 struct timespec __user *ut = NULL;
600 if (unlikely(!access_ok(VERIFY_WRITE, events,
601 nr * sizeof(struct io_event))))
604 if (get_compat_timespec(&t, timeout))
607 ut = compat_alloc_user_space(sizeof(*ut));
608 if (copy_to_user(ut, &t, sizeof(t)) )
611 ret = sys_io_getevents(ctx_id, min_nr, nr, events, ut);
617 copy_iocb(long nr, u32 __user *ptr32, struct iocb __user * __user *ptr64)
622 for (i = 0; i < nr; ++i) {
623 if (get_user(uptr, ptr32 + i))
625 if (put_user(compat_ptr(uptr), ptr64 + i))
631 #define MAX_AIO_SUBMITS (PAGE_SIZE/sizeof(struct iocb *))
634 compat_sys_io_submit(aio_context_t ctx_id, int nr, u32 __user *iocb)
636 struct iocb __user * __user *iocb64;
639 if (unlikely(nr < 0))
642 if (nr > MAX_AIO_SUBMITS)
643 nr = MAX_AIO_SUBMITS;
645 iocb64 = compat_alloc_user_space(nr * sizeof(*iocb64));
646 ret = copy_iocb(nr, iocb, iocb64);
648 ret = sys_io_submit(ctx_id, nr, iocb64);
652 struct compat_ncp_mount_data {
653 compat_int_t version;
654 compat_uint_t ncp_fd;
655 compat_uid_t mounted_uid;
656 compat_pid_t wdog_pid;
657 unsigned char mounted_vol[NCP_VOLNAME_LEN + 1];
658 compat_uint_t time_out;
659 compat_uint_t retry_count;
663 compat_mode_t file_mode;
664 compat_mode_t dir_mode;
667 struct compat_ncp_mount_data_v4 {
668 compat_int_t version;
669 compat_ulong_t flags;
670 compat_ulong_t mounted_uid;
671 compat_long_t wdog_pid;
672 compat_uint_t ncp_fd;
673 compat_uint_t time_out;
674 compat_uint_t retry_count;
677 compat_ulong_t file_mode;
678 compat_ulong_t dir_mode;
681 static void *do_ncp_super_data_conv(void *raw_data)
683 int version = *(unsigned int *)raw_data;
686 struct compat_ncp_mount_data *c_n = raw_data;
687 struct ncp_mount_data *n = raw_data;
689 n->dir_mode = c_n->dir_mode;
690 n->file_mode = c_n->file_mode;
693 memmove (n->mounted_vol, c_n->mounted_vol, (sizeof (c_n->mounted_vol) + 3 * sizeof (unsigned int)));
694 n->wdog_pid = c_n->wdog_pid;
695 n->mounted_uid = c_n->mounted_uid;
696 } else if (version == 4) {
697 struct compat_ncp_mount_data_v4 *c_n = raw_data;
698 struct ncp_mount_data_v4 *n = raw_data;
700 n->dir_mode = c_n->dir_mode;
701 n->file_mode = c_n->file_mode;
704 n->retry_count = c_n->retry_count;
705 n->time_out = c_n->time_out;
706 n->ncp_fd = c_n->ncp_fd;
707 n->wdog_pid = c_n->wdog_pid;
708 n->mounted_uid = c_n->mounted_uid;
709 n->flags = c_n->flags;
710 } else if (version != 5) {
717 struct compat_smb_mount_data {
718 compat_int_t version;
719 compat_uid_t mounted_uid;
722 compat_mode_t file_mode;
723 compat_mode_t dir_mode;
726 static void *do_smb_super_data_conv(void *raw_data)
728 struct smb_mount_data *s = raw_data;
729 struct compat_smb_mount_data *c_s = raw_data;
731 if (c_s->version != SMB_MOUNT_OLDVERSION)
733 s->dir_mode = c_s->dir_mode;
734 s->file_mode = c_s->file_mode;
737 s->mounted_uid = c_s->mounted_uid;
742 extern int copy_mount_options (const void __user *, unsigned long *);
744 #define SMBFS_NAME "smbfs"
745 #define NCPFS_NAME "ncpfs"
747 asmlinkage long compat_sys_mount(char __user * dev_name, char __user * dir_name,
748 char __user * type, unsigned long flags,
751 unsigned long type_page;
752 unsigned long data_page;
753 unsigned long dev_page;
757 retval = copy_mount_options (type, &type_page);
761 dir_page = getname(dir_name);
762 retval = PTR_ERR(dir_page);
763 if (IS_ERR(dir_page))
766 retval = copy_mount_options (dev_name, &dev_page);
770 retval = copy_mount_options (data, &data_page);
777 if (!strcmp((char *)type_page, SMBFS_NAME)) {
778 do_smb_super_data_conv((void *)data_page);
779 } else if (!strcmp((char *)type_page, NCPFS_NAME)) {
780 do_ncp_super_data_conv((void *)data_page);
785 retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
786 flags, (void*)data_page);
789 free_page(data_page);
795 free_page(type_page);
800 static ssize_t compat_do_readv_writev(int type, struct file *file,
801 const struct compat_iovec __user *uvector,
802 unsigned long nr_segs, loff_t *pos)
804 typedef ssize_t (*io_fn_t)(struct file *, char __user *, size_t, loff_t *);
805 typedef ssize_t (*iov_fn_t)(struct file *, const struct iovec *, unsigned long, loff_t *);
807 compat_ssize_t tot_len;
808 struct iovec iovstack[UIO_FASTIOV];
809 struct iovec *iov=iovstack, *vector;
817 * SuS says "The readv() function *may* fail if the iovcnt argument
818 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
819 * traditionally returned zero for zero segments, so...
826 * First get the "struct iovec" from user memory and
827 * verify all the pointers
830 if ((nr_segs > UIO_MAXIOV) || (nr_segs <= 0))
834 if (nr_segs > UIO_FASTIOV) {
836 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
841 if (verify_area(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
845 * Single unix specification:
846 * We should -EINVAL if an element length is not >= 0 and fitting an
847 * ssize_t. The total length is fitting an ssize_t
849 * Be careful here because iov_len is a size_t not an ssize_t
854 for (seg = 0 ; seg < nr_segs; seg++) {
855 compat_ssize_t tmp = tot_len;
859 if (__get_user(len, &uvector->iov_len) ||
860 __get_user(buf, &uvector->iov_base)) {
864 if (len < 0) /* size_t not fitting an compat_ssize_t .. */
867 if (tot_len < tmp) /* maths overflow on the compat_ssize_t */
869 vector->iov_base = compat_ptr(buf);
870 vector->iov_len = (compat_size_t) len;
879 inode = file->f_dentry->d_inode;
880 /* VERIFY_WRITE actually means a read, as we write to user space */
881 ret = locks_verify_area((type == READ
882 ? FLOCK_VERIFY_READ : FLOCK_VERIFY_WRITE),
883 inode, file, *pos, tot_len);
889 fn = file->f_op->read;
890 fnv = file->f_op->readv;
892 fn = (io_fn_t)file->f_op->write;
893 fnv = file->f_op->writev;
896 ret = fnv(file, iov, nr_segs, pos);
900 /* Do it by hand, with file-ops */
903 while (nr_segs > 0) {
908 base = vector->iov_base;
909 len = vector->iov_len;
913 nr = fn(file, base, len, pos);
926 if ((ret + (type == READ)) > 0)
927 dnotify_parent(file->f_dentry,
928 (type == READ) ? DN_ACCESS : DN_MODIFY);
933 compat_sys_readv(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
936 ssize_t ret = -EBADF;
942 if (!(file->f_mode & FMODE_READ))
946 if (!file->f_op || (!file->f_op->readv && !file->f_op->read))
949 ret = compat_do_readv_writev(READ, file, vec, vlen, &file->f_pos);
957 compat_sys_writev(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
960 ssize_t ret = -EBADF;
965 if (!(file->f_mode & FMODE_WRITE))
969 if (!file->f_op || (!file->f_op->writev && !file->f_op->write))
972 ret = compat_do_readv_writev(WRITE, file, vec, vlen, &file->f_pos);
980 * compat_count() counts the number of arguments/envelopes. It is basically
981 * a copy of count() from fs/exec.c, except that it works with 32 bit argv
984 static int compat_count(compat_uptr_t __user *argv, int max)
992 if (get_user(p, argv))
1005 * compat_copy_strings() is basically a copy of copy_strings() from fs/exec.c
1006 * except that it works with 32 bit argv and envp pointers.
1008 static int compat_copy_strings(int argc, compat_uptr_t __user *argv,
1009 struct linux_binprm *bprm)
1011 struct page *kmapped_page = NULL;
1015 while (argc-- > 0) {
1020 if (get_user(str, argv+argc) ||
1021 !(len = strnlen_user(compat_ptr(str), bprm->p))) {
1026 if (bprm->p < len) {
1032 /* XXX: add architecture specific overflow check here. */
1037 int offset, bytes_to_copy;
1040 offset = pos % PAGE_SIZE;
1042 page = bprm->page[i];
1045 page = alloc_page(GFP_HIGHUSER);
1046 bprm->page[i] = page;
1054 if (page != kmapped_page) {
1056 kunmap(kmapped_page);
1057 kmapped_page = page;
1058 kaddr = kmap(kmapped_page);
1061 memset(kaddr, 0, offset);
1062 bytes_to_copy = PAGE_SIZE - offset;
1063 if (bytes_to_copy > len) {
1064 bytes_to_copy = len;
1066 memset(kaddr+offset+len, 0,
1067 PAGE_SIZE-offset-len);
1069 err = copy_from_user(kaddr+offset, compat_ptr(str),
1076 pos += bytes_to_copy;
1077 str += bytes_to_copy;
1078 len -= bytes_to_copy;
1084 kunmap(kmapped_page);
1090 #define free_arg_pages(bprm) do { } while (0)
1094 static inline void free_arg_pages(struct linux_binprm *bprm)
1098 for (i = 0; i < MAX_ARG_PAGES; i++) {
1100 __free_page(bprm->page[i]);
1101 bprm->page[i] = NULL;
1105 #endif /* CONFIG_MMU */
1108 * compat_do_execve() is mostly a copy of do_execve(), with the exception
1109 * that it processes 32 bit argv and envp pointers.
1111 int compat_do_execve(char * filename,
1112 compat_uptr_t __user *argv,
1113 compat_uptr_t __user *envp,
1114 struct pt_regs * regs)
1116 struct linux_binprm bprm;
1121 sched_balance_exec();
1123 file = open_exec(filename);
1125 retval = PTR_ERR(file);
1129 bprm.p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1130 memset(bprm.page, 0, MAX_ARG_PAGES*sizeof(bprm.page[0]));
1133 bprm.filename = filename;
1134 bprm.interp = filename;
1138 bprm.security = NULL;
1139 bprm.mm = mm_alloc();
1144 retval = init_new_context(current, bprm.mm);
1148 bprm.argc = compat_count(argv, bprm.p / sizeof(compat_uptr_t));
1149 if ((retval = bprm.argc) < 0)
1152 bprm.envc = compat_count(envp, bprm.p / sizeof(compat_uptr_t));
1153 if ((retval = bprm.envc) < 0)
1156 retval = security_bprm_alloc(&bprm);
1160 retval = prepare_binprm(&bprm);
1164 retval = copy_strings_kernel(1, &bprm.filename, &bprm);
1169 retval = compat_copy_strings(bprm.envc, envp, &bprm);
1173 retval = compat_copy_strings(bprm.argc, argv, &bprm);
1177 retval = search_binary_handler(&bprm,regs);
1179 free_arg_pages(&bprm);
1181 /* execve success */
1182 security_bprm_free(&bprm);
1187 /* Something went wrong, return the inode and free the argument pages*/
1188 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1189 struct page * page = bprm.page[i];
1195 security_bprm_free(&bprm);
1203 allow_write_access(bprm.file);
1210 #define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t))
1212 #define ROUND_UP(x,y) (((x)+(y)-1)/(y))
1215 * Ooo, nasty. We need here to frob 32-bit unsigned longs to
1216 * 64-bit unsigned longs.
1219 int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1220 unsigned long *fdset)
1222 nr = ROUND_UP(nr, __COMPAT_NFDBITS);
1226 if (verify_area(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t)))
1233 __get_user(l, ufdset);
1234 __get_user(h, ufdset+1);
1236 *fdset++ = h << 32 | l;
1240 __get_user(*fdset, ufdset);
1242 /* Tricky, must clear full unsigned long in the
1243 * kernel fdset at the end, this makes sure that
1246 memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t));
1252 void compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1253 unsigned long *fdset)
1256 nr = ROUND_UP(nr, __COMPAT_NFDBITS);
1267 __put_user(l, ufdset);
1268 __put_user(h, ufdset+1);
1273 __put_user(*fdset, ufdset);
1278 * This is a virtual copy of sys_select from fs/select.c and probably
1279 * should be compared to it from time to time
1281 static void *select_bits_alloc(int size)
1283 return kmalloc(6 * size, GFP_KERNEL);
1286 static void select_bits_free(void *bits, int size)
1292 * We can actually return ERESTARTSYS instead of EINTR, but I'd
1293 * like to be certain this leads to no problems. So I return
1294 * EINTR just for safety.
1296 * Update: ERESTARTSYS breaks at least the xview clock binary, so
1297 * I'm trying ERESTARTNOHAND which restart only when you want to.
1299 #define MAX_SELECT_SECONDS \
1300 ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
1303 compat_sys_select(int n, compat_ulong_t __user *inp, compat_ulong_t __user *outp,
1304 compat_ulong_t __user *exp, struct compat_timeval __user *tvp)
1309 int ret, size, max_fdset;
1311 timeout = MAX_SCHEDULE_TIMEOUT;
1315 if ((ret = verify_area(VERIFY_READ, tvp, sizeof(*tvp)))
1316 || (ret = __get_user(sec, &tvp->tv_sec))
1317 || (ret = __get_user(usec, &tvp->tv_usec)))
1321 if (sec < 0 || usec < 0)
1324 if ((unsigned long) sec < MAX_SELECT_SECONDS) {
1325 timeout = ROUND_UP(usec, 1000000/HZ);
1326 timeout += sec * (unsigned long) HZ;
1334 /* max_fdset can increase, so grab it once to avoid race */
1335 max_fdset = current->files->max_fdset;
1340 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
1341 * since we used fdset we need to allocate memory in units of
1345 size = FDS_BYTES(n);
1346 bits = select_bits_alloc(size);
1349 fds.in = (unsigned long *) bits;
1350 fds.out = (unsigned long *) (bits + size);
1351 fds.ex = (unsigned long *) (bits + 2*size);
1352 fds.res_in = (unsigned long *) (bits + 3*size);
1353 fds.res_out = (unsigned long *) (bits + 4*size);
1354 fds.res_ex = (unsigned long *) (bits + 5*size);
1356 if ((ret = compat_get_fd_set(n, inp, fds.in)) ||
1357 (ret = compat_get_fd_set(n, outp, fds.out)) ||
1358 (ret = compat_get_fd_set(n, exp, fds.ex)))
1360 zero_fd_set(n, fds.res_in);
1361 zero_fd_set(n, fds.res_out);
1362 zero_fd_set(n, fds.res_ex);
1364 ret = do_select(n, &fds, &timeout);
1366 if (tvp && !(current->personality & STICKY_TIMEOUTS)) {
1367 time_t sec = 0, usec = 0;
1370 usec = timeout % HZ;
1371 usec *= (1000000/HZ);
1373 if (put_user(sec, &tvp->tv_sec) ||
1374 put_user(usec, &tvp->tv_usec))
1381 ret = -ERESTARTNOHAND;
1382 if (signal_pending(current))
1387 compat_set_fd_set(n, inp, fds.res_in);
1388 compat_set_fd_set(n, outp, fds.res_out);
1389 compat_set_fd_set(n, exp, fds.res_ex);
1392 select_bits_free(bits, size);
1397 #if defined(CONFIG_NFSD) || defined(CONFIG_NFSD_MODULE)
1398 /* Stuff for NFS server syscalls... */
1399 struct compat_nfsctl_svc {
1404 struct compat_nfsctl_client {
1405 s8 cl32_ident[NFSCLNT_IDMAX+1];
1407 struct in_addr cl32_addrlist[NFSCLNT_ADDRMAX];
1410 u8 cl32_fhkey[NFSCLNT_KEYMAX];
1413 struct compat_nfsctl_export {
1414 char ex32_client[NFSCLNT_IDMAX+1];
1415 char ex32_path[NFS_MAXPATHLEN+1];
1416 compat_dev_t ex32_dev;
1417 compat_ino_t ex32_ino;
1418 compat_int_t ex32_flags;
1419 compat_uid_t ex32_anon_uid;
1420 compat_gid_t ex32_anon_gid;
1423 struct compat_nfsctl_fdparm {
1424 struct sockaddr gd32_addr;
1425 s8 gd32_path[NFS_MAXPATHLEN+1];
1426 compat_int_t gd32_version;
1429 struct compat_nfsctl_fsparm {
1430 struct sockaddr gd32_addr;
1431 s8 gd32_path[NFS_MAXPATHLEN+1];
1432 compat_int_t gd32_maxlen;
1435 struct compat_nfsctl_arg {
1436 compat_int_t ca32_version; /* safeguard */
1438 struct compat_nfsctl_svc u32_svc;
1439 struct compat_nfsctl_client u32_client;
1440 struct compat_nfsctl_export u32_export;
1441 struct compat_nfsctl_fdparm u32_getfd;
1442 struct compat_nfsctl_fsparm u32_getfs;
1444 #define ca32_svc u.u32_svc
1445 #define ca32_client u.u32_client
1446 #define ca32_export u.u32_export
1447 #define ca32_getfd u.u32_getfd
1448 #define ca32_getfs u.u32_getfs
1451 union compat_nfsctl_res {
1452 __u8 cr32_getfh[NFS_FHSIZE];
1453 struct knfsd_fh cr32_getfs;
1456 static int compat_nfs_svc_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1460 err = access_ok(VERIFY_READ, &arg->ca32_svc, sizeof(arg->ca32_svc));
1461 err |= get_user(karg->ca_version, &arg->ca32_version);
1462 err |= __get_user(karg->ca_svc.svc_port, &arg->ca32_svc.svc32_port);
1463 err |= __get_user(karg->ca_svc.svc_nthreads, &arg->ca32_svc.svc32_nthreads);
1464 return (err) ? -EFAULT : 0;
1467 static int compat_nfs_clnt_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1471 err = access_ok(VERIFY_READ, &arg->ca32_client, sizeof(arg->ca32_client));
1472 err |= get_user(karg->ca_version, &arg->ca32_version);
1473 err |= __copy_from_user(&karg->ca_client.cl_ident[0],
1474 &arg->ca32_client.cl32_ident[0],
1476 err |= __get_user(karg->ca_client.cl_naddr, &arg->ca32_client.cl32_naddr);
1477 err |= __copy_from_user(&karg->ca_client.cl_addrlist[0],
1478 &arg->ca32_client.cl32_addrlist[0],
1479 (sizeof(struct in_addr) * NFSCLNT_ADDRMAX));
1480 err |= __get_user(karg->ca_client.cl_fhkeytype,
1481 &arg->ca32_client.cl32_fhkeytype);
1482 err |= __get_user(karg->ca_client.cl_fhkeylen,
1483 &arg->ca32_client.cl32_fhkeylen);
1484 err |= __copy_from_user(&karg->ca_client.cl_fhkey[0],
1485 &arg->ca32_client.cl32_fhkey[0],
1488 return (err) ? -EFAULT : 0;
1491 static int compat_nfs_exp_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1495 err = access_ok(VERIFY_READ, &arg->ca32_export, sizeof(arg->ca32_export));
1496 err |= get_user(karg->ca_version, &arg->ca32_version);
1497 err |= __copy_from_user(&karg->ca_export.ex_client[0],
1498 &arg->ca32_export.ex32_client[0],
1500 err |= __copy_from_user(&karg->ca_export.ex_path[0],
1501 &arg->ca32_export.ex32_path[0],
1503 err |= __get_user(karg->ca_export.ex_dev,
1504 &arg->ca32_export.ex32_dev);
1505 err |= __get_user(karg->ca_export.ex_ino,
1506 &arg->ca32_export.ex32_ino);
1507 err |= __get_user(karg->ca_export.ex_flags,
1508 &arg->ca32_export.ex32_flags);
1509 err |= __get_user(karg->ca_export.ex_anon_uid,
1510 &arg->ca32_export.ex32_anon_uid);
1511 err |= __get_user(karg->ca_export.ex_anon_gid,
1512 &arg->ca32_export.ex32_anon_gid);
1513 SET_UID(karg->ca_export.ex_anon_uid, karg->ca_export.ex_anon_uid);
1514 SET_GID(karg->ca_export.ex_anon_gid, karg->ca_export.ex_anon_gid);
1516 return (err) ? -EFAULT : 0;
1519 static int compat_nfs_getfd_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1523 err = access_ok(VERIFY_READ, &arg->ca32_getfd, sizeof(arg->ca32_getfd));
1524 err |= get_user(karg->ca_version, &arg->ca32_version);
1525 err |= __copy_from_user(&karg->ca_getfd.gd_addr,
1526 &arg->ca32_getfd.gd32_addr,
1527 (sizeof(struct sockaddr)));
1528 err |= __copy_from_user(&karg->ca_getfd.gd_path,
1529 &arg->ca32_getfd.gd32_path,
1530 (NFS_MAXPATHLEN+1));
1531 err |= __get_user(karg->ca_getfd.gd_version,
1532 &arg->ca32_getfd.gd32_version);
1534 return (err) ? -EFAULT : 0;
1537 static int compat_nfs_getfs_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1541 err = access_ok(VERIFY_READ, &arg->ca32_getfs, sizeof(arg->ca32_getfs));
1542 err |= get_user(karg->ca_version, &arg->ca32_version);
1543 err |= __copy_from_user(&karg->ca_getfs.gd_addr,
1544 &arg->ca32_getfs.gd32_addr,
1545 (sizeof(struct sockaddr)));
1546 err |= __copy_from_user(&karg->ca_getfs.gd_path,
1547 &arg->ca32_getfs.gd32_path,
1548 (NFS_MAXPATHLEN+1));
1549 err |= __get_user(karg->ca_getfs.gd_maxlen,
1550 &arg->ca32_getfs.gd32_maxlen);
1552 return (err) ? -EFAULT : 0;
1555 /* This really doesn't need translations, we are only passing
1556 * back a union which contains opaque nfs file handle data.
1558 static int compat_nfs_getfh_res_trans(union nfsctl_res *kres, union compat_nfsctl_res __user *res)
1562 err = copy_to_user(res, kres, sizeof(*res));
1564 return (err) ? -EFAULT : 0;
1567 asmlinkage long compat_sys_nfsservctl(int cmd, struct compat_nfsctl_arg __user *arg,
1568 union compat_nfsctl_res __user *res)
1570 struct nfsctl_arg *karg;
1571 union nfsctl_res *kres;
1575 karg = kmalloc(sizeof(*karg), GFP_USER);
1576 kres = kmalloc(sizeof(*kres), GFP_USER);
1577 if(!karg || !kres) {
1584 err = compat_nfs_svc_trans(karg, arg);
1587 case NFSCTL_ADDCLIENT:
1588 err = compat_nfs_clnt_trans(karg, arg);
1591 case NFSCTL_DELCLIENT:
1592 err = compat_nfs_clnt_trans(karg, arg);
1596 case NFSCTL_UNEXPORT:
1597 err = compat_nfs_exp_trans(karg, arg);
1601 err = compat_nfs_getfd_trans(karg, arg);
1605 err = compat_nfs_getfs_trans(karg, arg);
1615 /* The __user pointer casts are valid because of the set_fs() */
1616 err = sys_nfsservctl(cmd, (void __user *) karg, (void __user *) kres);
1622 if((cmd == NFSCTL_GETFD) ||
1623 (cmd == NFSCTL_GETFS))
1624 err = compat_nfs_getfh_res_trans(kres, res);
1632 long asmlinkage compat_sys_nfsservctl(int cmd, void *notused, void *notused2)
1634 return sys_ni_syscall();