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/ioctl.h>
29 #include <linux/init.h>
30 #include <linux/sockios.h> /* for SIOCDEVPRIVATE */
31 #include <linux/smb.h>
32 #include <linux/smb_mount.h>
33 #include <linux/ncp_mount.h>
34 #include <linux/smp_lock.h>
35 #include <linux/syscalls.h>
36 #include <linux/ctype.h>
37 #include <linux/module.h>
38 #include <linux/dirent.h>
39 #include <linux/dnotify.h>
40 #include <linux/highuid.h>
41 #include <linux/sunrpc/svc.h>
42 #include <linux/nfsd/nfsd.h>
43 #include <linux/nfsd/syscall.h>
44 #include <linux/personality.h>
45 #include <linux/rwsem.h>
47 #include <net/sock.h> /* siocdevprivate_ioctl */
49 #include <asm/uaccess.h>
50 #include <asm/mmu_context.h>
51 #include <asm/ioctls.h>
54 * Not all architectures have sys_utime, so implement this in terms
57 asmlinkage long compat_sys_utime(char __user *filename, struct compat_utimbuf __user *t)
62 if (get_user(tv[0].tv_sec, &t->actime) ||
63 get_user(tv[1].tv_sec, &t->modtime))
68 return do_utimes(filename, t ? tv : NULL);
71 asmlinkage long compat_sys_utimes(char __user *filename, struct compat_timeval __user *t)
76 if (get_user(tv[0].tv_sec, &t[0].tv_sec) ||
77 get_user(tv[0].tv_usec, &t[0].tv_usec) ||
78 get_user(tv[1].tv_sec, &t[1].tv_sec) ||
79 get_user(tv[1].tv_usec, &t[1].tv_usec))
82 return do_utimes(filename, t ? tv : NULL);
85 asmlinkage long compat_sys_newstat(char __user * filename,
86 struct compat_stat __user *statbuf)
89 int error = vfs_stat(filename, &stat);
92 error = cp_compat_stat(&stat, statbuf);
96 asmlinkage long compat_sys_newlstat(char __user * filename,
97 struct compat_stat __user *statbuf)
100 int error = vfs_lstat(filename, &stat);
103 error = cp_compat_stat(&stat, statbuf);
107 asmlinkage long compat_sys_newfstat(unsigned int fd,
108 struct compat_stat __user * statbuf)
111 int error = vfs_fstat(fd, &stat);
114 error = cp_compat_stat(&stat, statbuf);
118 static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf)
121 if (sizeof ubuf->f_blocks == 4) {
122 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail) &
123 0xffffffff00000000ULL)
125 /* f_files and f_ffree may be -1; it's okay
126 * to stuff that into 32 bits */
127 if (kbuf->f_files != 0xffffffffffffffffULL
128 && (kbuf->f_files & 0xffffffff00000000ULL))
130 if (kbuf->f_ffree != 0xffffffffffffffffULL
131 && (kbuf->f_ffree & 0xffffffff00000000ULL))
134 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
135 __put_user(kbuf->f_type, &ubuf->f_type) ||
136 __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
137 __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
138 __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
139 __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
140 __put_user(kbuf->f_files, &ubuf->f_files) ||
141 __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
142 __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
143 __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
144 __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
145 __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
146 __put_user(0, &ubuf->f_spare[0]) ||
147 __put_user(0, &ubuf->f_spare[1]) ||
148 __put_user(0, &ubuf->f_spare[2]) ||
149 __put_user(0, &ubuf->f_spare[3]) ||
150 __put_user(0, &ubuf->f_spare[4]))
156 * The following statfs calls are copies of code from fs/open.c and
157 * should be checked against those from time to time
159 asmlinkage long compat_sys_statfs(const char __user *path, struct compat_statfs __user *buf)
164 error = user_path_walk(path, &nd);
167 error = vfs_statfs(nd.dentry->d_inode->i_sb, &tmp);
168 if (!error && put_compat_statfs(buf, &tmp))
175 asmlinkage long compat_sys_fstatfs(unsigned int fd, struct compat_statfs __user *buf)
185 error = vfs_statfs(file->f_dentry->d_inode->i_sb, &tmp);
186 if (!error && put_compat_statfs(buf, &tmp))
193 static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf)
195 if (sizeof ubuf->f_blocks == 4) {
196 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail) &
197 0xffffffff00000000ULL)
199 /* f_files and f_ffree may be -1; it's okay
200 * to stuff that into 32 bits */
201 if (kbuf->f_files != 0xffffffffffffffffULL
202 && (kbuf->f_files & 0xffffffff00000000ULL))
204 if (kbuf->f_ffree != 0xffffffffffffffffULL
205 && (kbuf->f_ffree & 0xffffffff00000000ULL))
208 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
209 __put_user(kbuf->f_type, &ubuf->f_type) ||
210 __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
211 __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
212 __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
213 __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
214 __put_user(kbuf->f_files, &ubuf->f_files) ||
215 __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
216 __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
217 __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
218 __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
219 __put_user(kbuf->f_frsize, &ubuf->f_frsize))
224 asmlinkage long compat_sys_statfs64(const char __user *path, compat_size_t sz, struct compat_statfs64 __user *buf)
229 if (sz != sizeof(*buf))
232 error = user_path_walk(path, &nd);
235 error = vfs_statfs(nd.dentry->d_inode->i_sb, &tmp);
236 if (!error && put_compat_statfs64(buf, &tmp))
243 asmlinkage long compat_sys_fstatfs64(unsigned int fd, compat_size_t sz, struct compat_statfs64 __user *buf)
249 if (sz != sizeof(*buf))
256 error = vfs_statfs(file->f_dentry->d_inode->i_sb, &tmp);
257 if (!error && put_compat_statfs64(buf, &tmp))
264 /* ioctl32 stuff, used by sparc64, parisc, s390x, ppc64, x86_64, MIPS */
266 #define IOCTL_HASHSIZE 256
267 static struct ioctl_trans *ioctl32_hash_table[IOCTL_HASHSIZE];
268 static DECLARE_RWSEM(ioctl32_sem);
270 extern struct ioctl_trans ioctl_start[];
271 extern int ioctl_table_size;
273 static inline unsigned long ioctl32_hash(unsigned long cmd)
275 return (((cmd >> 6) ^ (cmd >> 4) ^ cmd)) % IOCTL_HASHSIZE;
278 static void ioctl32_insert_translation(struct ioctl_trans *trans)
281 struct ioctl_trans *t;
283 hash = ioctl32_hash (trans->cmd);
284 if (!ioctl32_hash_table[hash])
285 ioctl32_hash_table[hash] = trans;
287 t = ioctl32_hash_table[hash];
295 static int __init init_sys32_ioctl(void)
299 for (i = 0; i < ioctl_table_size; i++) {
300 if (ioctl_start[i].next != 0) {
301 printk("ioctl translation %d bad\n",i);
305 ioctl32_insert_translation(&ioctl_start[i]);
310 __initcall(init_sys32_ioctl);
312 int register_ioctl32_conversion(unsigned int cmd,
313 ioctl_trans_handler_t handler)
315 struct ioctl_trans *t;
316 struct ioctl_trans *new_t;
317 unsigned long hash = ioctl32_hash(cmd);
319 new_t = kmalloc(sizeof(*new_t), GFP_KERNEL);
323 down_write(&ioctl32_sem);
324 for (t = ioctl32_hash_table[hash]; t; t = t->next) {
326 printk(KERN_ERR "Trying to register duplicated ioctl32 "
327 "handler %x\n", cmd);
328 up_write(&ioctl32_sem);
335 new_t->handler = handler;
336 ioctl32_insert_translation(new_t);
338 up_write(&ioctl32_sem);
341 EXPORT_SYMBOL(register_ioctl32_conversion);
343 static inline int builtin_ioctl(struct ioctl_trans *t)
345 return t >= ioctl_start && t < (ioctl_start + ioctl_table_size);
349 This function cannot unregister duplicate ioctls, because they are not
351 When they happen we need to extend the prototype to pass the handler too. */
353 int unregister_ioctl32_conversion(unsigned int cmd)
355 unsigned long hash = ioctl32_hash(cmd);
356 struct ioctl_trans *t, *t1;
358 down_write(&ioctl32_sem);
360 t = ioctl32_hash_table[hash];
362 up_write(&ioctl32_sem);
367 if (builtin_ioctl(t)) {
368 printk("%p tried to unregister builtin ioctl %x\n",
369 __builtin_return_address(0), cmd);
371 ioctl32_hash_table[hash] = t->next;
372 up_write(&ioctl32_sem);
379 if (t1->cmd == cmd) {
380 if (builtin_ioctl(t1)) {
381 printk("%p tried to unregister builtin "
383 __builtin_return_address(0), cmd);
387 up_write(&ioctl32_sem);
394 printk(KERN_ERR "Trying to free unknown 32bit ioctl handler %x\n",
397 up_write(&ioctl32_sem);
400 EXPORT_SYMBOL(unregister_ioctl32_conversion);
402 static void compat_ioctl_error(struct file *filp, unsigned int fd,
403 unsigned int cmd, unsigned long arg)
409 /* find the name of the device. */
410 path = (char *)__get_free_page(GFP_KERNEL);
412 fn = d_path(filp->f_dentry, filp->f_vfsmnt, path, PAGE_SIZE);
417 sprintf(buf,"'%c'", (cmd>>24) & 0x3f);
418 if (!isprint(buf[1]))
419 sprintf(buf, "%02x", buf[1]);
420 printk("ioctl32(%s:%d): Unknown cmd fd(%d) "
421 "cmd(%08x){%s} arg(%08x) on %s\n",
422 current->comm, current->pid,
423 (int)fd, (unsigned int)cmd, buf,
424 (unsigned int)arg, fn);
427 free_page((unsigned long)path);
430 asmlinkage long compat_sys_ioctl(unsigned int fd, unsigned int cmd,
435 struct ioctl_trans *t;
438 filp = fget_light(fd, &fput_needed);
442 /* RED-PEN how should LSM module know it's handling 32bit? */
443 error = security_file_ioctl(filp, cmd, arg);
448 * To allow the compat_ioctl handlers to be self contained
449 * we need to check the common ioctls here first.
450 * Just handle them with the standard handlers below.
463 if (S_ISREG(filp->f_dentry->d_inode->i_mode))
468 if (filp->f_op && filp->f_op->compat_ioctl) {
469 error = filp->f_op->compat_ioctl(filp, cmd, arg);
470 if (error != -ENOIOCTLCMD)
475 (!filp->f_op->ioctl && !filp->f_op->unlocked_ioctl))
480 /* When register_ioctl32_conversion is finally gone remove
482 down_read(&ioctl32_sem);
483 for (t = ioctl32_hash_table[ioctl32_hash(cmd)]; t; t = t->next) {
487 up_read(&ioctl32_sem);
489 if (S_ISSOCK(filp->f_dentry->d_inode->i_mode) &&
490 cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
491 error = siocdevprivate_ioctl(fd, cmd, arg);
496 compat_ioctl_error(filp, fd, cmd, arg);
505 error = t->handler(fd, cmd, arg, filp);
507 up_read(&ioctl32_sem);
511 up_read(&ioctl32_sem);
513 error = vfs_ioctl(filp, fd, cmd, arg);
515 fput_light(filp, fput_needed);
520 static int get_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
522 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
523 __get_user(kfl->l_type, &ufl->l_type) ||
524 __get_user(kfl->l_whence, &ufl->l_whence) ||
525 __get_user(kfl->l_start, &ufl->l_start) ||
526 __get_user(kfl->l_len, &ufl->l_len) ||
527 __get_user(kfl->l_pid, &ufl->l_pid))
532 static int put_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
534 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
535 __put_user(kfl->l_type, &ufl->l_type) ||
536 __put_user(kfl->l_whence, &ufl->l_whence) ||
537 __put_user(kfl->l_start, &ufl->l_start) ||
538 __put_user(kfl->l_len, &ufl->l_len) ||
539 __put_user(kfl->l_pid, &ufl->l_pid))
544 #ifndef HAVE_ARCH_GET_COMPAT_FLOCK64
545 static int get_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
547 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
548 __get_user(kfl->l_type, &ufl->l_type) ||
549 __get_user(kfl->l_whence, &ufl->l_whence) ||
550 __get_user(kfl->l_start, &ufl->l_start) ||
551 __get_user(kfl->l_len, &ufl->l_len) ||
552 __get_user(kfl->l_pid, &ufl->l_pid))
558 #ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64
559 static int put_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
561 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
562 __put_user(kfl->l_type, &ufl->l_type) ||
563 __put_user(kfl->l_whence, &ufl->l_whence) ||
564 __put_user(kfl->l_start, &ufl->l_start) ||
565 __put_user(kfl->l_len, &ufl->l_len) ||
566 __put_user(kfl->l_pid, &ufl->l_pid))
572 asmlinkage long compat_sys_fcntl64(unsigned int fd, unsigned int cmd,
583 ret = get_compat_flock(&f, compat_ptr(arg));
588 ret = sys_fcntl(fd, cmd, (unsigned long)&f);
590 if (cmd == F_GETLK && ret == 0) {
591 if ((f.l_start >= COMPAT_OFF_T_MAX) ||
592 ((f.l_start + f.l_len) > COMPAT_OFF_T_MAX))
595 ret = put_compat_flock(&f, compat_ptr(arg));
602 ret = get_compat_flock64(&f, compat_ptr(arg));
607 ret = sys_fcntl(fd, (cmd == F_GETLK64) ? F_GETLK :
608 ((cmd == F_SETLK64) ? F_SETLK : F_SETLKW),
611 if (cmd == F_GETLK64 && ret == 0) {
612 if ((f.l_start >= COMPAT_LOFF_T_MAX) ||
613 ((f.l_start + f.l_len) > COMPAT_LOFF_T_MAX))
616 ret = put_compat_flock64(&f, compat_ptr(arg));
621 ret = sys_fcntl(fd, cmd, arg);
627 asmlinkage long compat_sys_fcntl(unsigned int fd, unsigned int cmd,
630 if ((cmd == F_GETLK64) || (cmd == F_SETLK64) || (cmd == F_SETLKW64))
632 return compat_sys_fcntl64(fd, cmd, arg);
636 compat_sys_io_setup(unsigned nr_reqs, u32 __user *ctx32p)
641 mm_segment_t oldfs = get_fs();
642 if (unlikely(get_user(ctx64, ctx32p)))
646 /* The __user pointer cast is valid because of the set_fs() */
647 ret = sys_io_setup(nr_reqs, (aio_context_t __user *) &ctx64);
649 /* truncating is ok because it's a user address */
651 ret = put_user((u32) ctx64, ctx32p);
656 compat_sys_io_getevents(aio_context_t ctx_id,
657 unsigned long min_nr,
659 struct io_event __user *events,
660 struct compat_timespec __user *timeout)
664 struct timespec __user *ut = NULL;
667 if (unlikely(!access_ok(VERIFY_WRITE, events,
668 nr * sizeof(struct io_event))))
671 if (get_compat_timespec(&t, timeout))
674 ut = compat_alloc_user_space(sizeof(*ut));
675 if (copy_to_user(ut, &t, sizeof(t)) )
678 ret = sys_io_getevents(ctx_id, min_nr, nr, events, ut);
684 copy_iocb(long nr, u32 __user *ptr32, struct iocb __user * __user *ptr64)
689 for (i = 0; i < nr; ++i) {
690 if (get_user(uptr, ptr32 + i))
692 if (put_user(compat_ptr(uptr), ptr64 + i))
698 #define MAX_AIO_SUBMITS (PAGE_SIZE/sizeof(struct iocb *))
701 compat_sys_io_submit(aio_context_t ctx_id, int nr, u32 __user *iocb)
703 struct iocb __user * __user *iocb64;
706 if (unlikely(nr < 0))
709 if (nr > MAX_AIO_SUBMITS)
710 nr = MAX_AIO_SUBMITS;
712 iocb64 = compat_alloc_user_space(nr * sizeof(*iocb64));
713 ret = copy_iocb(nr, iocb, iocb64);
715 ret = sys_io_submit(ctx_id, nr, iocb64);
719 struct compat_ncp_mount_data {
720 compat_int_t version;
721 compat_uint_t ncp_fd;
722 compat_uid_t mounted_uid;
723 compat_pid_t wdog_pid;
724 unsigned char mounted_vol[NCP_VOLNAME_LEN + 1];
725 compat_uint_t time_out;
726 compat_uint_t retry_count;
730 compat_mode_t file_mode;
731 compat_mode_t dir_mode;
734 struct compat_ncp_mount_data_v4 {
735 compat_int_t version;
736 compat_ulong_t flags;
737 compat_ulong_t mounted_uid;
738 compat_long_t wdog_pid;
739 compat_uint_t ncp_fd;
740 compat_uint_t time_out;
741 compat_uint_t retry_count;
744 compat_ulong_t file_mode;
745 compat_ulong_t dir_mode;
748 static void *do_ncp_super_data_conv(void *raw_data)
750 int version = *(unsigned int *)raw_data;
753 struct compat_ncp_mount_data *c_n = raw_data;
754 struct ncp_mount_data *n = raw_data;
756 n->dir_mode = c_n->dir_mode;
757 n->file_mode = c_n->file_mode;
760 memmove (n->mounted_vol, c_n->mounted_vol, (sizeof (c_n->mounted_vol) + 3 * sizeof (unsigned int)));
761 n->wdog_pid = c_n->wdog_pid;
762 n->mounted_uid = c_n->mounted_uid;
763 } else if (version == 4) {
764 struct compat_ncp_mount_data_v4 *c_n = raw_data;
765 struct ncp_mount_data_v4 *n = raw_data;
767 n->dir_mode = c_n->dir_mode;
768 n->file_mode = c_n->file_mode;
771 n->retry_count = c_n->retry_count;
772 n->time_out = c_n->time_out;
773 n->ncp_fd = c_n->ncp_fd;
774 n->wdog_pid = c_n->wdog_pid;
775 n->mounted_uid = c_n->mounted_uid;
776 n->flags = c_n->flags;
777 } else if (version != 5) {
784 struct compat_smb_mount_data {
785 compat_int_t version;
786 compat_uid_t mounted_uid;
789 compat_mode_t file_mode;
790 compat_mode_t dir_mode;
793 static void *do_smb_super_data_conv(void *raw_data)
795 struct smb_mount_data *s = raw_data;
796 struct compat_smb_mount_data *c_s = raw_data;
798 if (c_s->version != SMB_MOUNT_OLDVERSION)
800 s->dir_mode = c_s->dir_mode;
801 s->file_mode = c_s->file_mode;
804 s->mounted_uid = c_s->mounted_uid;
809 extern int copy_mount_options (const void __user *, unsigned long *);
811 #define SMBFS_NAME "smbfs"
812 #define NCPFS_NAME "ncpfs"
814 asmlinkage long compat_sys_mount(char __user * dev_name, char __user * dir_name,
815 char __user * type, unsigned long flags,
818 unsigned long type_page;
819 unsigned long data_page;
820 unsigned long dev_page;
824 retval = copy_mount_options (type, &type_page);
828 dir_page = getname(dir_name);
829 retval = PTR_ERR(dir_page);
830 if (IS_ERR(dir_page))
833 retval = copy_mount_options (dev_name, &dev_page);
837 retval = copy_mount_options (data, &data_page);
844 if (!strcmp((char *)type_page, SMBFS_NAME)) {
845 do_smb_super_data_conv((void *)data_page);
846 } else if (!strcmp((char *)type_page, NCPFS_NAME)) {
847 do_ncp_super_data_conv((void *)data_page);
852 retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
853 flags, (void*)data_page);
856 free_page(data_page);
862 free_page(type_page);
867 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char __user *) (de)))
868 #define COMPAT_ROUND_UP(x) (((x)+sizeof(compat_long_t)-1) & \
869 ~(sizeof(compat_long_t)-1))
871 struct compat_old_linux_dirent {
872 compat_ulong_t d_ino;
873 compat_ulong_t d_offset;
874 unsigned short d_namlen;
878 struct compat_readdir_callback {
879 struct compat_old_linux_dirent __user *dirent;
883 static int compat_fillonedir(void *__buf, const char *name, int namlen,
884 loff_t offset, ino_t ino, unsigned int d_type)
886 struct compat_readdir_callback *buf = __buf;
887 struct compat_old_linux_dirent __user *dirent;
892 dirent = buf->dirent;
893 if (!access_ok(VERIFY_WRITE, dirent,
894 (unsigned long)(dirent->d_name + namlen + 1) -
895 (unsigned long)dirent))
897 if ( __put_user(ino, &dirent->d_ino) ||
898 __put_user(offset, &dirent->d_offset) ||
899 __put_user(namlen, &dirent->d_namlen) ||
900 __copy_to_user(dirent->d_name, name, namlen) ||
901 __put_user(0, dirent->d_name + namlen))
905 buf->result = -EFAULT;
909 asmlinkage long compat_sys_old_readdir(unsigned int fd,
910 struct compat_old_linux_dirent __user *dirent, unsigned int count)
914 struct compat_readdir_callback buf;
924 error = vfs_readdir(file, compat_fillonedir, &buf);
933 struct compat_linux_dirent {
934 compat_ulong_t d_ino;
935 compat_ulong_t d_off;
936 unsigned short d_reclen;
940 struct compat_getdents_callback {
941 struct compat_linux_dirent __user *current_dir;
942 struct compat_linux_dirent __user *previous;
947 static int compat_filldir(void *__buf, const char *name, int namlen,
948 loff_t offset, ino_t ino, unsigned int d_type)
950 struct compat_linux_dirent __user * dirent;
951 struct compat_getdents_callback *buf = __buf;
952 int reclen = COMPAT_ROUND_UP(NAME_OFFSET(dirent) + namlen + 2);
954 buf->error = -EINVAL; /* only used if we fail.. */
955 if (reclen > buf->count)
957 dirent = buf->previous;
959 if (__put_user(offset, &dirent->d_off))
962 dirent = buf->current_dir;
963 if (__put_user(ino, &dirent->d_ino))
965 if (__put_user(reclen, &dirent->d_reclen))
967 if (copy_to_user(dirent->d_name, name, namlen))
969 if (__put_user(0, dirent->d_name + namlen))
971 if (__put_user(d_type, (char __user *) dirent + reclen - 1))
973 buf->previous = dirent;
974 dirent = (void __user *)dirent + reclen;
975 buf->current_dir = dirent;
976 buf->count -= reclen;
979 buf->error = -EFAULT;
983 asmlinkage long compat_sys_getdents(unsigned int fd,
984 struct compat_linux_dirent __user *dirent, unsigned int count)
987 struct compat_linux_dirent __user * lastdirent;
988 struct compat_getdents_callback buf;
992 if (!access_ok(VERIFY_WRITE, dirent, count))
1000 buf.current_dir = dirent;
1001 buf.previous = NULL;
1005 error = vfs_readdir(file, compat_filldir, &buf);
1009 lastdirent = buf.previous;
1011 if (put_user(file->f_pos, &lastdirent->d_off))
1014 error = count - buf.count;
1023 #ifndef __ARCH_OMIT_COMPAT_SYS_GETDENTS64
1024 #define COMPAT_ROUND_UP64(x) (((x)+sizeof(u64)-1) & ~(sizeof(u64)-1))
1026 struct compat_getdents_callback64 {
1027 struct linux_dirent64 __user *current_dir;
1028 struct linux_dirent64 __user *previous;
1033 static int compat_filldir64(void * __buf, const char * name, int namlen, loff_t offset,
1034 ino_t ino, unsigned int d_type)
1036 struct linux_dirent64 __user *dirent;
1037 struct compat_getdents_callback64 *buf = __buf;
1038 int jj = NAME_OFFSET(dirent);
1039 int reclen = COMPAT_ROUND_UP64(jj + namlen + 1);
1042 buf->error = -EINVAL; /* only used if we fail.. */
1043 if (reclen > buf->count)
1045 dirent = buf->previous;
1048 if (__put_user_unaligned(offset, &dirent->d_off))
1051 dirent = buf->current_dir;
1052 if (__put_user_unaligned(ino, &dirent->d_ino))
1055 if (__put_user_unaligned(off, &dirent->d_off))
1057 if (__put_user(reclen, &dirent->d_reclen))
1059 if (__put_user(d_type, &dirent->d_type))
1061 if (copy_to_user(dirent->d_name, name, namlen))
1063 if (__put_user(0, dirent->d_name + namlen))
1065 buf->previous = dirent;
1066 dirent = (void __user *)dirent + reclen;
1067 buf->current_dir = dirent;
1068 buf->count -= reclen;
1071 buf->error = -EFAULT;
1075 asmlinkage long compat_sys_getdents64(unsigned int fd,
1076 struct linux_dirent64 __user * dirent, unsigned int count)
1079 struct linux_dirent64 __user * lastdirent;
1080 struct compat_getdents_callback64 buf;
1084 if (!access_ok(VERIFY_WRITE, dirent, count))
1092 buf.current_dir = dirent;
1093 buf.previous = NULL;
1097 error = vfs_readdir(file, compat_filldir64, &buf);
1101 lastdirent = buf.previous;
1103 typeof(lastdirent->d_off) d_off = file->f_pos;
1104 __put_user_unaligned(d_off, &lastdirent->d_off);
1105 error = count - buf.count;
1113 #endif /* ! __ARCH_OMIT_COMPAT_SYS_GETDENTS64 */
1115 static ssize_t compat_do_readv_writev(int type, struct file *file,
1116 const struct compat_iovec __user *uvector,
1117 unsigned long nr_segs, loff_t *pos)
1119 typedef ssize_t (*io_fn_t)(struct file *, char __user *, size_t, loff_t *);
1120 typedef ssize_t (*iov_fn_t)(struct file *, const struct iovec *, unsigned long, loff_t *);
1122 compat_ssize_t tot_len;
1123 struct iovec iovstack[UIO_FASTIOV];
1124 struct iovec *iov=iovstack, *vector;
1131 * SuS says "The readv() function *may* fail if the iovcnt argument
1132 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
1133 * traditionally returned zero for zero segments, so...
1140 * First get the "struct iovec" from user memory and
1141 * verify all the pointers
1144 if ((nr_segs > UIO_MAXIOV) || (nr_segs <= 0))
1148 if (nr_segs > UIO_FASTIOV) {
1150 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
1155 if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
1159 * Single unix specification:
1160 * We should -EINVAL if an element length is not >= 0 and fitting an
1161 * ssize_t. The total length is fitting an ssize_t
1163 * Be careful here because iov_len is a size_t not an ssize_t
1168 for (seg = 0 ; seg < nr_segs; seg++) {
1169 compat_ssize_t tmp = tot_len;
1173 if (__get_user(len, &uvector->iov_len) ||
1174 __get_user(buf, &uvector->iov_base)) {
1178 if (len < 0) /* size_t not fitting an compat_ssize_t .. */
1181 if (tot_len < tmp) /* maths overflow on the compat_ssize_t */
1183 vector->iov_base = compat_ptr(buf);
1184 vector->iov_len = (compat_size_t) len;
1193 ret = rw_verify_area(type, file, pos, tot_len);
1199 fn = file->f_op->read;
1200 fnv = file->f_op->readv;
1202 fn = (io_fn_t)file->f_op->write;
1203 fnv = file->f_op->writev;
1206 ret = fnv(file, iov, nr_segs, pos);
1210 /* Do it by hand, with file-ops */
1213 while (nr_segs > 0) {
1218 base = vector->iov_base;
1219 len = vector->iov_len;
1223 nr = fn(file, base, len, pos);
1234 if (iov != iovstack)
1236 if ((ret + (type == READ)) > 0)
1237 dnotify_parent(file->f_dentry,
1238 (type == READ) ? DN_ACCESS : DN_MODIFY);
1243 compat_sys_readv(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
1246 ssize_t ret = -EBADF;
1252 if (!(file->f_mode & FMODE_READ))
1256 if (!file->f_op || (!file->f_op->readv && !file->f_op->read))
1259 ret = compat_do_readv_writev(READ, file, vec, vlen, &file->f_pos);
1267 compat_sys_writev(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen)
1270 ssize_t ret = -EBADF;
1275 if (!(file->f_mode & FMODE_WRITE))
1279 if (!file->f_op || (!file->f_op->writev && !file->f_op->write))
1282 ret = compat_do_readv_writev(WRITE, file, vec, vlen, &file->f_pos);
1290 * compat_count() counts the number of arguments/envelopes. It is basically
1291 * a copy of count() from fs/exec.c, except that it works with 32 bit argv
1292 * and envp pointers.
1294 static int compat_count(compat_uptr_t __user *argv, int max)
1302 if (get_user(p, argv))
1315 * compat_copy_strings() is basically a copy of copy_strings() from fs/exec.c
1316 * except that it works with 32 bit argv and envp pointers.
1318 static int compat_copy_strings(int argc, compat_uptr_t __user *argv,
1319 struct linux_binprm *bprm)
1321 struct page *kmapped_page = NULL;
1325 while (argc-- > 0) {
1330 if (get_user(str, argv+argc) ||
1331 !(len = strnlen_user(compat_ptr(str), bprm->p))) {
1336 if (bprm->p < len) {
1342 /* XXX: add architecture specific overflow check here. */
1347 int offset, bytes_to_copy;
1350 offset = pos % PAGE_SIZE;
1352 page = bprm->page[i];
1355 page = alloc_page(GFP_HIGHUSER);
1356 bprm->page[i] = page;
1364 if (page != kmapped_page) {
1366 kunmap(kmapped_page);
1367 kmapped_page = page;
1368 kaddr = kmap(kmapped_page);
1371 memset(kaddr, 0, offset);
1372 bytes_to_copy = PAGE_SIZE - offset;
1373 if (bytes_to_copy > len) {
1374 bytes_to_copy = len;
1376 memset(kaddr+offset+len, 0,
1377 PAGE_SIZE-offset-len);
1379 err = copy_from_user(kaddr+offset, compat_ptr(str),
1386 pos += bytes_to_copy;
1387 str += bytes_to_copy;
1388 len -= bytes_to_copy;
1394 kunmap(kmapped_page);
1400 #define free_arg_pages(bprm) do { } while (0)
1404 static inline void free_arg_pages(struct linux_binprm *bprm)
1408 for (i = 0; i < MAX_ARG_PAGES; i++) {
1410 __free_page(bprm->page[i]);
1411 bprm->page[i] = NULL;
1415 #endif /* CONFIG_MMU */
1418 * compat_do_execve() is mostly a copy of do_execve(), with the exception
1419 * that it processes 32 bit argv and envp pointers.
1421 int compat_do_execve(char * filename,
1422 compat_uptr_t __user *argv,
1423 compat_uptr_t __user *envp,
1424 struct pt_regs * regs)
1426 struct linux_binprm *bprm;
1432 bprm = kmalloc(sizeof(*bprm), GFP_KERNEL);
1435 memset(bprm, 0, sizeof(*bprm));
1437 file = open_exec(filename);
1438 retval = PTR_ERR(file);
1444 bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *);
1446 bprm->filename = filename;
1447 bprm->interp = filename;
1448 bprm->mm = mm_alloc();
1453 retval = init_new_context(current, bprm->mm);
1457 bprm->argc = compat_count(argv, bprm->p / sizeof(compat_uptr_t));
1458 if ((retval = bprm->argc) < 0)
1461 bprm->envc = compat_count(envp, bprm->p / sizeof(compat_uptr_t));
1462 if ((retval = bprm->envc) < 0)
1465 retval = security_bprm_alloc(bprm);
1469 retval = prepare_binprm(bprm);
1473 retval = copy_strings_kernel(1, &bprm->filename, bprm);
1477 bprm->exec = bprm->p;
1478 retval = compat_copy_strings(bprm->envc, envp, bprm);
1482 retval = compat_copy_strings(bprm->argc, argv, bprm);
1486 retval = search_binary_handler(bprm, regs);
1488 free_arg_pages(bprm);
1490 /* execve success */
1491 security_bprm_free(bprm);
1497 /* Something went wrong, return the inode and free the argument pages*/
1498 for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
1499 struct page * page = bprm->page[i];
1505 security_bprm_free(bprm);
1513 allow_write_access(bprm->file);
1524 #define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t))
1526 #define ROUND_UP(x,y) (((x)+(y)-1)/(y))
1529 * Ooo, nasty. We need here to frob 32-bit unsigned longs to
1530 * 64-bit unsigned longs.
1533 int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1534 unsigned long *fdset)
1536 nr = ROUND_UP(nr, __COMPAT_NFDBITS);
1540 if (!access_ok(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t)))
1547 __get_user(l, ufdset);
1548 __get_user(h, ufdset+1);
1550 *fdset++ = h << 32 | l;
1554 __get_user(*fdset, ufdset);
1556 /* Tricky, must clear full unsigned long in the
1557 * kernel fdset at the end, this makes sure that
1560 memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t));
1566 void compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1567 unsigned long *fdset)
1570 nr = ROUND_UP(nr, __COMPAT_NFDBITS);
1581 __put_user(l, ufdset);
1582 __put_user(h, ufdset+1);
1587 __put_user(*fdset, ufdset);
1592 * This is a virtual copy of sys_select from fs/select.c and probably
1593 * should be compared to it from time to time
1595 static void *select_bits_alloc(int size)
1597 return kmalloc(6 * size, GFP_KERNEL);
1600 static void select_bits_free(void *bits, int size)
1606 * We can actually return ERESTARTSYS instead of EINTR, but I'd
1607 * like to be certain this leads to no problems. So I return
1608 * EINTR just for safety.
1610 * Update: ERESTARTSYS breaks at least the xview clock binary, so
1611 * I'm trying ERESTARTNOHAND which restart only when you want to.
1613 #define MAX_SELECT_SECONDS \
1614 ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
1617 compat_sys_select(int n, compat_ulong_t __user *inp, compat_ulong_t __user *outp,
1618 compat_ulong_t __user *exp, struct compat_timeval __user *tvp)
1623 int size, max_fdset, ret = -EINVAL;
1625 timeout = MAX_SCHEDULE_TIMEOUT;
1629 if (!access_ok(VERIFY_READ, tvp, sizeof(*tvp))
1630 || __get_user(sec, &tvp->tv_sec)
1631 || __get_user(usec, &tvp->tv_usec)) {
1636 if (sec < 0 || usec < 0)
1639 if ((unsigned long) sec < MAX_SELECT_SECONDS) {
1640 timeout = ROUND_UP(usec, 1000000/HZ);
1641 timeout += sec * (unsigned long) HZ;
1648 /* max_fdset can increase, so grab it once to avoid race */
1649 max_fdset = current->files->max_fdset;
1654 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
1655 * since we used fdset we need to allocate memory in units of
1659 size = FDS_BYTES(n);
1660 bits = select_bits_alloc(size);
1663 fds.in = (unsigned long *) bits;
1664 fds.out = (unsigned long *) (bits + size);
1665 fds.ex = (unsigned long *) (bits + 2*size);
1666 fds.res_in = (unsigned long *) (bits + 3*size);
1667 fds.res_out = (unsigned long *) (bits + 4*size);
1668 fds.res_ex = (unsigned long *) (bits + 5*size);
1670 if ((ret = compat_get_fd_set(n, inp, fds.in)) ||
1671 (ret = compat_get_fd_set(n, outp, fds.out)) ||
1672 (ret = compat_get_fd_set(n, exp, fds.ex)))
1674 zero_fd_set(n, fds.res_in);
1675 zero_fd_set(n, fds.res_out);
1676 zero_fd_set(n, fds.res_ex);
1678 ret = do_select(n, &fds, &timeout);
1680 if (tvp && !(current->personality & STICKY_TIMEOUTS)) {
1681 time_t sec = 0, usec = 0;
1684 usec = timeout % HZ;
1685 usec *= (1000000/HZ);
1687 if (put_user(sec, &tvp->tv_sec) ||
1688 put_user(usec, &tvp->tv_usec))
1695 ret = -ERESTARTNOHAND;
1696 if (signal_pending(current))
1701 compat_set_fd_set(n, inp, fds.res_in);
1702 compat_set_fd_set(n, outp, fds.res_out);
1703 compat_set_fd_set(n, exp, fds.res_ex);
1706 select_bits_free(bits, size);
1711 #if defined(CONFIG_NFSD) || defined(CONFIG_NFSD_MODULE)
1712 /* Stuff for NFS server syscalls... */
1713 struct compat_nfsctl_svc {
1718 struct compat_nfsctl_client {
1719 s8 cl32_ident[NFSCLNT_IDMAX+1];
1721 struct in_addr cl32_addrlist[NFSCLNT_ADDRMAX];
1724 u8 cl32_fhkey[NFSCLNT_KEYMAX];
1727 struct compat_nfsctl_export {
1728 char ex32_client[NFSCLNT_IDMAX+1];
1729 char ex32_path[NFS_MAXPATHLEN+1];
1730 compat_dev_t ex32_dev;
1731 compat_ino_t ex32_ino;
1732 compat_int_t ex32_flags;
1733 compat_uid_t ex32_anon_uid;
1734 compat_gid_t ex32_anon_gid;
1737 struct compat_nfsctl_fdparm {
1738 struct sockaddr gd32_addr;
1739 s8 gd32_path[NFS_MAXPATHLEN+1];
1740 compat_int_t gd32_version;
1743 struct compat_nfsctl_fsparm {
1744 struct sockaddr gd32_addr;
1745 s8 gd32_path[NFS_MAXPATHLEN+1];
1746 compat_int_t gd32_maxlen;
1749 struct compat_nfsctl_arg {
1750 compat_int_t ca32_version; /* safeguard */
1752 struct compat_nfsctl_svc u32_svc;
1753 struct compat_nfsctl_client u32_client;
1754 struct compat_nfsctl_export u32_export;
1755 struct compat_nfsctl_fdparm u32_getfd;
1756 struct compat_nfsctl_fsparm u32_getfs;
1758 #define ca32_svc u.u32_svc
1759 #define ca32_client u.u32_client
1760 #define ca32_export u.u32_export
1761 #define ca32_getfd u.u32_getfd
1762 #define ca32_getfs u.u32_getfs
1765 union compat_nfsctl_res {
1766 __u8 cr32_getfh[NFS_FHSIZE];
1767 struct knfsd_fh cr32_getfs;
1770 static int compat_nfs_svc_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1774 err = access_ok(VERIFY_READ, &arg->ca32_svc, sizeof(arg->ca32_svc));
1775 err |= get_user(karg->ca_version, &arg->ca32_version);
1776 err |= __get_user(karg->ca_svc.svc_port, &arg->ca32_svc.svc32_port);
1777 err |= __get_user(karg->ca_svc.svc_nthreads, &arg->ca32_svc.svc32_nthreads);
1778 return (err) ? -EFAULT : 0;
1781 static int compat_nfs_clnt_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1785 err = access_ok(VERIFY_READ, &arg->ca32_client, sizeof(arg->ca32_client));
1786 err |= get_user(karg->ca_version, &arg->ca32_version);
1787 err |= __copy_from_user(&karg->ca_client.cl_ident[0],
1788 &arg->ca32_client.cl32_ident[0],
1790 err |= __get_user(karg->ca_client.cl_naddr, &arg->ca32_client.cl32_naddr);
1791 err |= __copy_from_user(&karg->ca_client.cl_addrlist[0],
1792 &arg->ca32_client.cl32_addrlist[0],
1793 (sizeof(struct in_addr) * NFSCLNT_ADDRMAX));
1794 err |= __get_user(karg->ca_client.cl_fhkeytype,
1795 &arg->ca32_client.cl32_fhkeytype);
1796 err |= __get_user(karg->ca_client.cl_fhkeylen,
1797 &arg->ca32_client.cl32_fhkeylen);
1798 err |= __copy_from_user(&karg->ca_client.cl_fhkey[0],
1799 &arg->ca32_client.cl32_fhkey[0],
1802 return (err) ? -EFAULT : 0;
1805 static int compat_nfs_exp_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1809 err = access_ok(VERIFY_READ, &arg->ca32_export, sizeof(arg->ca32_export));
1810 err |= get_user(karg->ca_version, &arg->ca32_version);
1811 err |= __copy_from_user(&karg->ca_export.ex_client[0],
1812 &arg->ca32_export.ex32_client[0],
1814 err |= __copy_from_user(&karg->ca_export.ex_path[0],
1815 &arg->ca32_export.ex32_path[0],
1817 err |= __get_user(karg->ca_export.ex_dev,
1818 &arg->ca32_export.ex32_dev);
1819 err |= __get_user(karg->ca_export.ex_ino,
1820 &arg->ca32_export.ex32_ino);
1821 err |= __get_user(karg->ca_export.ex_flags,
1822 &arg->ca32_export.ex32_flags);
1823 err |= __get_user(karg->ca_export.ex_anon_uid,
1824 &arg->ca32_export.ex32_anon_uid);
1825 err |= __get_user(karg->ca_export.ex_anon_gid,
1826 &arg->ca32_export.ex32_anon_gid);
1827 SET_UID(karg->ca_export.ex_anon_uid, karg->ca_export.ex_anon_uid);
1828 SET_GID(karg->ca_export.ex_anon_gid, karg->ca_export.ex_anon_gid);
1830 return (err) ? -EFAULT : 0;
1833 static int compat_nfs_getfd_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1837 err = access_ok(VERIFY_READ, &arg->ca32_getfd, sizeof(arg->ca32_getfd));
1838 err |= get_user(karg->ca_version, &arg->ca32_version);
1839 err |= __copy_from_user(&karg->ca_getfd.gd_addr,
1840 &arg->ca32_getfd.gd32_addr,
1841 (sizeof(struct sockaddr)));
1842 err |= __copy_from_user(&karg->ca_getfd.gd_path,
1843 &arg->ca32_getfd.gd32_path,
1844 (NFS_MAXPATHLEN+1));
1845 err |= __get_user(karg->ca_getfd.gd_version,
1846 &arg->ca32_getfd.gd32_version);
1848 return (err) ? -EFAULT : 0;
1851 static int compat_nfs_getfs_trans(struct nfsctl_arg *karg, struct compat_nfsctl_arg __user *arg)
1855 err = access_ok(VERIFY_READ, &arg->ca32_getfs, sizeof(arg->ca32_getfs));
1856 err |= get_user(karg->ca_version, &arg->ca32_version);
1857 err |= __copy_from_user(&karg->ca_getfs.gd_addr,
1858 &arg->ca32_getfs.gd32_addr,
1859 (sizeof(struct sockaddr)));
1860 err |= __copy_from_user(&karg->ca_getfs.gd_path,
1861 &arg->ca32_getfs.gd32_path,
1862 (NFS_MAXPATHLEN+1));
1863 err |= __get_user(karg->ca_getfs.gd_maxlen,
1864 &arg->ca32_getfs.gd32_maxlen);
1866 return (err) ? -EFAULT : 0;
1869 /* This really doesn't need translations, we are only passing
1870 * back a union which contains opaque nfs file handle data.
1872 static int compat_nfs_getfh_res_trans(union nfsctl_res *kres, union compat_nfsctl_res __user *res)
1876 err = copy_to_user(res, kres, sizeof(*res));
1878 return (err) ? -EFAULT : 0;
1881 asmlinkage long compat_sys_nfsservctl(int cmd, struct compat_nfsctl_arg __user *arg,
1882 union compat_nfsctl_res __user *res)
1884 struct nfsctl_arg *karg;
1885 union nfsctl_res *kres;
1889 karg = kmalloc(sizeof(*karg), GFP_USER);
1890 kres = kmalloc(sizeof(*kres), GFP_USER);
1891 if(!karg || !kres) {
1898 err = compat_nfs_svc_trans(karg, arg);
1901 case NFSCTL_ADDCLIENT:
1902 err = compat_nfs_clnt_trans(karg, arg);
1905 case NFSCTL_DELCLIENT:
1906 err = compat_nfs_clnt_trans(karg, arg);
1910 case NFSCTL_UNEXPORT:
1911 err = compat_nfs_exp_trans(karg, arg);
1915 err = compat_nfs_getfd_trans(karg, arg);
1919 err = compat_nfs_getfs_trans(karg, arg);
1929 /* The __user pointer casts are valid because of the set_fs() */
1930 err = sys_nfsservctl(cmd, (void __user *) karg, (void __user *) kres);
1936 if((cmd == NFSCTL_GETFD) ||
1937 (cmd == NFSCTL_GETFS))
1938 err = compat_nfs_getfh_res_trans(kres, res);
1946 long asmlinkage compat_sys_nfsservctl(int cmd, void *notused, void *notused2)
1948 return sys_ni_syscall();