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/kernel.h>
19 #include <linux/linkage.h>
20 #include <linux/compat.h>
21 #include <linux/errno.h>
22 #include <linux/time.h>
24 #include <linux/fcntl.h>
25 #include <linux/namei.h>
26 #include <linux/file.h>
27 #include <linux/vfs.h>
28 #include <linux/ioctl.h>
29 #include <linux/init.h>
30 #include <linux/smb.h>
31 #include <linux/smb_mount.h>
32 #include <linux/ncp_mount.h>
33 #include <linux/nfs4_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/fsnotify.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>
46 #include <linux/tsacct_kern.h>
47 #include <linux/security.h>
48 #include <linux/highmem.h>
49 #include <linux/poll.h>
51 #include <linux/eventpoll.h>
53 #include <asm/uaccess.h>
54 #include <asm/mmu_context.h>
55 #include <asm/ioctls.h>
60 int compat_printk(const char *fmt
, ...)
67 ret
= vprintk(fmt
, ap
);
72 #include "read_write.h"
75 * Not all architectures have sys_utime, so implement this in terms
78 asmlinkage
long compat_sys_utime(char __user
*filename
, struct compat_utimbuf __user
*t
)
80 struct timespec tv
[2];
83 if (get_user(tv
[0].tv_sec
, &t
->actime
) ||
84 get_user(tv
[1].tv_sec
, &t
->modtime
))
89 return do_utimes(AT_FDCWD
, filename
, t
? tv
: NULL
, 0);
92 asmlinkage
long compat_sys_utimensat(unsigned int dfd
, char __user
*filename
, struct compat_timespec __user
*t
, int flags
)
94 struct timespec tv
[2];
97 if (get_compat_timespec(&tv
[0], &t
[0]) ||
98 get_compat_timespec(&tv
[1], &t
[1]))
101 if ((tv
[0].tv_nsec
== UTIME_OMIT
|| tv
[0].tv_nsec
== UTIME_NOW
)
102 && tv
[0].tv_sec
!= 0)
104 if ((tv
[1].tv_nsec
== UTIME_OMIT
|| tv
[1].tv_nsec
== UTIME_NOW
)
105 && tv
[1].tv_sec
!= 0)
108 if (tv
[0].tv_nsec
== UTIME_OMIT
&& tv
[1].tv_nsec
== UTIME_OMIT
)
111 return do_utimes(dfd
, filename
, t
? tv
: NULL
, flags
);
114 asmlinkage
long compat_sys_futimesat(unsigned int dfd
, char __user
*filename
, struct compat_timeval __user
*t
)
116 struct timespec tv
[2];
119 if (get_user(tv
[0].tv_sec
, &t
[0].tv_sec
) ||
120 get_user(tv
[0].tv_nsec
, &t
[0].tv_usec
) ||
121 get_user(tv
[1].tv_sec
, &t
[1].tv_sec
) ||
122 get_user(tv
[1].tv_nsec
, &t
[1].tv_usec
))
124 if (tv
[0].tv_nsec
>= 1000000 || tv
[0].tv_nsec
< 0 ||
125 tv
[1].tv_nsec
>= 1000000 || tv
[1].tv_nsec
< 0)
127 tv
[0].tv_nsec
*= 1000;
128 tv
[1].tv_nsec
*= 1000;
130 return do_utimes(dfd
, filename
, t
? tv
: NULL
, 0);
133 asmlinkage
long compat_sys_utimes(char __user
*filename
, struct compat_timeval __user
*t
)
135 return compat_sys_futimesat(AT_FDCWD
, filename
, t
);
138 asmlinkage
long compat_sys_newstat(char __user
* filename
,
139 struct compat_stat __user
*statbuf
)
142 int error
= vfs_stat_fd(AT_FDCWD
, filename
, &stat
);
145 error
= cp_compat_stat(&stat
, statbuf
);
149 asmlinkage
long compat_sys_newlstat(char __user
* filename
,
150 struct compat_stat __user
*statbuf
)
153 int error
= vfs_lstat_fd(AT_FDCWD
, filename
, &stat
);
156 error
= cp_compat_stat(&stat
, statbuf
);
160 #ifndef __ARCH_WANT_STAT64
161 asmlinkage
long compat_sys_newfstatat(unsigned int dfd
, char __user
*filename
,
162 struct compat_stat __user
*statbuf
, int flag
)
167 if ((flag
& ~AT_SYMLINK_NOFOLLOW
) != 0)
170 if (flag
& AT_SYMLINK_NOFOLLOW
)
171 error
= vfs_lstat_fd(dfd
, filename
, &stat
);
173 error
= vfs_stat_fd(dfd
, filename
, &stat
);
176 error
= cp_compat_stat(&stat
, statbuf
);
183 asmlinkage
long compat_sys_newfstat(unsigned int fd
,
184 struct compat_stat __user
* statbuf
)
187 int error
= vfs_fstat(fd
, &stat
);
190 error
= cp_compat_stat(&stat
, statbuf
);
194 static int put_compat_statfs(struct compat_statfs __user
*ubuf
, struct kstatfs
*kbuf
)
197 if (sizeof ubuf
->f_blocks
== 4) {
198 if ((kbuf
->f_blocks
| kbuf
->f_bfree
| kbuf
->f_bavail
) &
199 0xffffffff00000000ULL
)
201 /* f_files and f_ffree may be -1; it's okay
202 * to stuff that into 32 bits */
203 if (kbuf
->f_files
!= 0xffffffffffffffffULL
204 && (kbuf
->f_files
& 0xffffffff00000000ULL
))
206 if (kbuf
->f_ffree
!= 0xffffffffffffffffULL
207 && (kbuf
->f_ffree
& 0xffffffff00000000ULL
))
210 if (!access_ok(VERIFY_WRITE
, ubuf
, sizeof(*ubuf
)) ||
211 __put_user(kbuf
->f_type
, &ubuf
->f_type
) ||
212 __put_user(kbuf
->f_bsize
, &ubuf
->f_bsize
) ||
213 __put_user(kbuf
->f_blocks
, &ubuf
->f_blocks
) ||
214 __put_user(kbuf
->f_bfree
, &ubuf
->f_bfree
) ||
215 __put_user(kbuf
->f_bavail
, &ubuf
->f_bavail
) ||
216 __put_user(kbuf
->f_files
, &ubuf
->f_files
) ||
217 __put_user(kbuf
->f_ffree
, &ubuf
->f_ffree
) ||
218 __put_user(kbuf
->f_namelen
, &ubuf
->f_namelen
) ||
219 __put_user(kbuf
->f_fsid
.val
[0], &ubuf
->f_fsid
.val
[0]) ||
220 __put_user(kbuf
->f_fsid
.val
[1], &ubuf
->f_fsid
.val
[1]) ||
221 __put_user(kbuf
->f_frsize
, &ubuf
->f_frsize
) ||
222 __put_user(0, &ubuf
->f_spare
[0]) ||
223 __put_user(0, &ubuf
->f_spare
[1]) ||
224 __put_user(0, &ubuf
->f_spare
[2]) ||
225 __put_user(0, &ubuf
->f_spare
[3]) ||
226 __put_user(0, &ubuf
->f_spare
[4]))
232 * The following statfs calls are copies of code from fs/open.c and
233 * should be checked against those from time to time
235 asmlinkage
long compat_sys_statfs(const char __user
*path
, struct compat_statfs __user
*buf
)
240 error
= user_path_walk(path
, &nd
);
243 error
= vfs_statfs(nd
.dentry
, &tmp
);
245 error
= put_compat_statfs(buf
, &tmp
);
251 asmlinkage
long compat_sys_fstatfs(unsigned int fd
, struct compat_statfs __user
*buf
)
261 error
= vfs_statfs(file
->f_path
.dentry
, &tmp
);
263 error
= put_compat_statfs(buf
, &tmp
);
269 static int put_compat_statfs64(struct compat_statfs64 __user
*ubuf
, struct kstatfs
*kbuf
)
271 if (sizeof ubuf
->f_blocks
== 4) {
272 if ((kbuf
->f_blocks
| kbuf
->f_bfree
| kbuf
->f_bavail
) &
273 0xffffffff00000000ULL
)
275 /* f_files and f_ffree may be -1; it's okay
276 * to stuff that into 32 bits */
277 if (kbuf
->f_files
!= 0xffffffffffffffffULL
278 && (kbuf
->f_files
& 0xffffffff00000000ULL
))
280 if (kbuf
->f_ffree
!= 0xffffffffffffffffULL
281 && (kbuf
->f_ffree
& 0xffffffff00000000ULL
))
284 if (!access_ok(VERIFY_WRITE
, ubuf
, sizeof(*ubuf
)) ||
285 __put_user(kbuf
->f_type
, &ubuf
->f_type
) ||
286 __put_user(kbuf
->f_bsize
, &ubuf
->f_bsize
) ||
287 __put_user(kbuf
->f_blocks
, &ubuf
->f_blocks
) ||
288 __put_user(kbuf
->f_bfree
, &ubuf
->f_bfree
) ||
289 __put_user(kbuf
->f_bavail
, &ubuf
->f_bavail
) ||
290 __put_user(kbuf
->f_files
, &ubuf
->f_files
) ||
291 __put_user(kbuf
->f_ffree
, &ubuf
->f_ffree
) ||
292 __put_user(kbuf
->f_namelen
, &ubuf
->f_namelen
) ||
293 __put_user(kbuf
->f_fsid
.val
[0], &ubuf
->f_fsid
.val
[0]) ||
294 __put_user(kbuf
->f_fsid
.val
[1], &ubuf
->f_fsid
.val
[1]) ||
295 __put_user(kbuf
->f_frsize
, &ubuf
->f_frsize
))
300 asmlinkage
long compat_sys_statfs64(const char __user
*path
, compat_size_t sz
, struct compat_statfs64 __user
*buf
)
305 if (sz
!= sizeof(*buf
))
308 error
= user_path_walk(path
, &nd
);
311 error
= vfs_statfs(nd
.dentry
, &tmp
);
313 error
= put_compat_statfs64(buf
, &tmp
);
319 asmlinkage
long compat_sys_fstatfs64(unsigned int fd
, compat_size_t sz
, struct compat_statfs64 __user
*buf
)
325 if (sz
!= sizeof(*buf
))
332 error
= vfs_statfs(file
->f_path
.dentry
, &tmp
);
334 error
= put_compat_statfs64(buf
, &tmp
);
340 static int get_compat_flock(struct flock
*kfl
, struct compat_flock __user
*ufl
)
342 if (!access_ok(VERIFY_READ
, ufl
, sizeof(*ufl
)) ||
343 __get_user(kfl
->l_type
, &ufl
->l_type
) ||
344 __get_user(kfl
->l_whence
, &ufl
->l_whence
) ||
345 __get_user(kfl
->l_start
, &ufl
->l_start
) ||
346 __get_user(kfl
->l_len
, &ufl
->l_len
) ||
347 __get_user(kfl
->l_pid
, &ufl
->l_pid
))
352 static int put_compat_flock(struct flock
*kfl
, struct compat_flock __user
*ufl
)
354 if (!access_ok(VERIFY_WRITE
, ufl
, sizeof(*ufl
)) ||
355 __put_user(kfl
->l_type
, &ufl
->l_type
) ||
356 __put_user(kfl
->l_whence
, &ufl
->l_whence
) ||
357 __put_user(kfl
->l_start
, &ufl
->l_start
) ||
358 __put_user(kfl
->l_len
, &ufl
->l_len
) ||
359 __put_user(kfl
->l_pid
, &ufl
->l_pid
))
364 #ifndef HAVE_ARCH_GET_COMPAT_FLOCK64
365 static int get_compat_flock64(struct flock
*kfl
, struct compat_flock64 __user
*ufl
)
367 if (!access_ok(VERIFY_READ
, ufl
, sizeof(*ufl
)) ||
368 __get_user(kfl
->l_type
, &ufl
->l_type
) ||
369 __get_user(kfl
->l_whence
, &ufl
->l_whence
) ||
370 __get_user(kfl
->l_start
, &ufl
->l_start
) ||
371 __get_user(kfl
->l_len
, &ufl
->l_len
) ||
372 __get_user(kfl
->l_pid
, &ufl
->l_pid
))
378 #ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64
379 static int put_compat_flock64(struct flock
*kfl
, struct compat_flock64 __user
*ufl
)
381 if (!access_ok(VERIFY_WRITE
, ufl
, sizeof(*ufl
)) ||
382 __put_user(kfl
->l_type
, &ufl
->l_type
) ||
383 __put_user(kfl
->l_whence
, &ufl
->l_whence
) ||
384 __put_user(kfl
->l_start
, &ufl
->l_start
) ||
385 __put_user(kfl
->l_len
, &ufl
->l_len
) ||
386 __put_user(kfl
->l_pid
, &ufl
->l_pid
))
392 asmlinkage
long compat_sys_fcntl64(unsigned int fd
, unsigned int cmd
,
403 ret
= get_compat_flock(&f
, compat_ptr(arg
));
408 ret
= sys_fcntl(fd
, cmd
, (unsigned long)&f
);
410 if (cmd
== F_GETLK
&& ret
== 0) {
411 /* GETLK was successfule and we need to return the data...
412 * but it needs to fit in the compat structure.
413 * l_start shouldn't be too big, unless the original
414 * start + end is greater than COMPAT_OFF_T_MAX, in which
415 * case the app was asking for trouble, so we return
416 * -EOVERFLOW in that case.
417 * l_len could be too big, in which case we just truncate it,
418 * and only allow the app to see that part of the conflicting
419 * lock that might make sense to it anyway
422 if (f
.l_start
> COMPAT_OFF_T_MAX
)
424 if (f
.l_len
> COMPAT_OFF_T_MAX
)
425 f
.l_len
= COMPAT_OFF_T_MAX
;
427 ret
= put_compat_flock(&f
, compat_ptr(arg
));
434 ret
= get_compat_flock64(&f
, compat_ptr(arg
));
439 ret
= sys_fcntl(fd
, (cmd
== F_GETLK64
) ? F_GETLK
:
440 ((cmd
== F_SETLK64
) ? F_SETLK
: F_SETLKW
),
443 if (cmd
== F_GETLK64
&& ret
== 0) {
444 /* need to return lock information - see above for commentary */
445 if (f
.l_start
> COMPAT_LOFF_T_MAX
)
447 if (f
.l_len
> COMPAT_LOFF_T_MAX
)
448 f
.l_len
= COMPAT_LOFF_T_MAX
;
450 ret
= put_compat_flock64(&f
, compat_ptr(arg
));
455 ret
= sys_fcntl(fd
, cmd
, arg
);
461 asmlinkage
long compat_sys_fcntl(unsigned int fd
, unsigned int cmd
,
464 if ((cmd
== F_GETLK64
) || (cmd
== F_SETLK64
) || (cmd
== F_SETLKW64
))
466 return compat_sys_fcntl64(fd
, cmd
, arg
);
470 compat_sys_io_setup(unsigned nr_reqs
, u32 __user
*ctx32p
)
475 mm_segment_t oldfs
= get_fs();
476 if (unlikely(get_user(ctx64
, ctx32p
)))
480 /* The __user pointer cast is valid because of the set_fs() */
481 ret
= sys_io_setup(nr_reqs
, (aio_context_t __user
*) &ctx64
);
483 /* truncating is ok because it's a user address */
485 ret
= put_user((u32
) ctx64
, ctx32p
);
490 compat_sys_io_getevents(aio_context_t ctx_id
,
491 unsigned long min_nr
,
493 struct io_event __user
*events
,
494 struct compat_timespec __user
*timeout
)
498 struct timespec __user
*ut
= NULL
;
501 if (unlikely(!access_ok(VERIFY_WRITE
, events
,
502 nr
* sizeof(struct io_event
))))
505 if (get_compat_timespec(&t
, timeout
))
508 ut
= compat_alloc_user_space(sizeof(*ut
));
509 if (copy_to_user(ut
, &t
, sizeof(t
)) )
512 ret
= sys_io_getevents(ctx_id
, min_nr
, nr
, events
, ut
);
518 copy_iocb(long nr
, u32 __user
*ptr32
, struct iocb __user
* __user
*ptr64
)
523 for (i
= 0; i
< nr
; ++i
) {
524 if (get_user(uptr
, ptr32
+ i
))
526 if (put_user(compat_ptr(uptr
), ptr64
+ i
))
532 #define MAX_AIO_SUBMITS (PAGE_SIZE/sizeof(struct iocb *))
535 compat_sys_io_submit(aio_context_t ctx_id
, int nr
, u32 __user
*iocb
)
537 struct iocb __user
* __user
*iocb64
;
540 if (unlikely(nr
< 0))
543 if (nr
> MAX_AIO_SUBMITS
)
544 nr
= MAX_AIO_SUBMITS
;
546 iocb64
= compat_alloc_user_space(nr
* sizeof(*iocb64
));
547 ret
= copy_iocb(nr
, iocb
, iocb64
);
549 ret
= sys_io_submit(ctx_id
, nr
, iocb64
);
553 struct compat_ncp_mount_data
{
554 compat_int_t version
;
555 compat_uint_t ncp_fd
;
556 __compat_uid_t mounted_uid
;
557 compat_pid_t wdog_pid
;
558 unsigned char mounted_vol
[NCP_VOLNAME_LEN
+ 1];
559 compat_uint_t time_out
;
560 compat_uint_t retry_count
;
564 compat_mode_t file_mode
;
565 compat_mode_t dir_mode
;
568 struct compat_ncp_mount_data_v4
{
569 compat_int_t version
;
570 compat_ulong_t flags
;
571 compat_ulong_t mounted_uid
;
572 compat_long_t wdog_pid
;
573 compat_uint_t ncp_fd
;
574 compat_uint_t time_out
;
575 compat_uint_t retry_count
;
578 compat_ulong_t file_mode
;
579 compat_ulong_t dir_mode
;
582 static void *do_ncp_super_data_conv(void *raw_data
)
584 int version
= *(unsigned int *)raw_data
;
587 struct compat_ncp_mount_data
*c_n
= raw_data
;
588 struct ncp_mount_data
*n
= raw_data
;
590 n
->dir_mode
= c_n
->dir_mode
;
591 n
->file_mode
= c_n
->file_mode
;
594 memmove (n
->mounted_vol
, c_n
->mounted_vol
, (sizeof (c_n
->mounted_vol
) + 3 * sizeof (unsigned int)));
595 n
->wdog_pid
= c_n
->wdog_pid
;
596 n
->mounted_uid
= c_n
->mounted_uid
;
597 } else if (version
== 4) {
598 struct compat_ncp_mount_data_v4
*c_n
= raw_data
;
599 struct ncp_mount_data_v4
*n
= raw_data
;
601 n
->dir_mode
= c_n
->dir_mode
;
602 n
->file_mode
= c_n
->file_mode
;
605 n
->retry_count
= c_n
->retry_count
;
606 n
->time_out
= c_n
->time_out
;
607 n
->ncp_fd
= c_n
->ncp_fd
;
608 n
->wdog_pid
= c_n
->wdog_pid
;
609 n
->mounted_uid
= c_n
->mounted_uid
;
610 n
->flags
= c_n
->flags
;
611 } else if (version
!= 5) {
618 struct compat_smb_mount_data
{
619 compat_int_t version
;
620 __compat_uid_t mounted_uid
;
623 compat_mode_t file_mode
;
624 compat_mode_t dir_mode
;
627 static void *do_smb_super_data_conv(void *raw_data
)
629 struct smb_mount_data
*s
= raw_data
;
630 struct compat_smb_mount_data
*c_s
= raw_data
;
632 if (c_s
->version
!= SMB_MOUNT_OLDVERSION
)
634 s
->dir_mode
= c_s
->dir_mode
;
635 s
->file_mode
= c_s
->file_mode
;
638 s
->mounted_uid
= c_s
->mounted_uid
;
643 struct compat_nfs_string
{
648 static inline void compat_nfs_string(struct nfs_string
*dst
,
649 struct compat_nfs_string
*src
)
651 dst
->data
= compat_ptr(src
->data
);
655 struct compat_nfs4_mount_data_v1
{
656 compat_int_t version
;
661 compat_int_t retrans
;
662 compat_int_t acregmin
;
663 compat_int_t acregmax
;
664 compat_int_t acdirmin
;
665 compat_int_t acdirmax
;
666 struct compat_nfs_string client_addr
;
667 struct compat_nfs_string mnt_path
;
668 struct compat_nfs_string hostname
;
669 compat_uint_t host_addrlen
;
670 compat_uptr_t host_addr
;
672 compat_int_t auth_flavourlen
;
673 compat_uptr_t auth_flavours
;
676 static int do_nfs4_super_data_conv(void *raw_data
)
678 int version
= *(compat_uint_t
*) raw_data
;
681 struct compat_nfs4_mount_data_v1
*raw
= raw_data
;
682 struct nfs4_mount_data
*real
= raw_data
;
684 /* copy the fields backwards */
685 real
->auth_flavours
= compat_ptr(raw
->auth_flavours
);
686 real
->auth_flavourlen
= raw
->auth_flavourlen
;
687 real
->proto
= raw
->proto
;
688 real
->host_addr
= compat_ptr(raw
->host_addr
);
689 real
->host_addrlen
= raw
->host_addrlen
;
690 compat_nfs_string(&real
->hostname
, &raw
->hostname
);
691 compat_nfs_string(&real
->mnt_path
, &raw
->mnt_path
);
692 compat_nfs_string(&real
->client_addr
, &raw
->client_addr
);
693 real
->acdirmax
= raw
->acdirmax
;
694 real
->acdirmin
= raw
->acdirmin
;
695 real
->acregmax
= raw
->acregmax
;
696 real
->acregmin
= raw
->acregmin
;
697 real
->retrans
= raw
->retrans
;
698 real
->timeo
= raw
->timeo
;
699 real
->wsize
= raw
->wsize
;
700 real
->rsize
= raw
->rsize
;
701 real
->flags
= raw
->flags
;
702 real
->version
= raw
->version
;
711 #define SMBFS_NAME "smbfs"
712 #define NCPFS_NAME "ncpfs"
713 #define NFS4_NAME "nfs4"
715 asmlinkage
long compat_sys_mount(char __user
* dev_name
, char __user
* dir_name
,
716 char __user
* type
, unsigned long flags
,
719 unsigned long type_page
;
720 unsigned long data_page
;
721 unsigned long dev_page
;
725 retval
= copy_mount_options (type
, &type_page
);
729 dir_page
= getname(dir_name
);
730 retval
= PTR_ERR(dir_page
);
731 if (IS_ERR(dir_page
))
734 retval
= copy_mount_options (dev_name
, &dev_page
);
738 retval
= copy_mount_options (data
, &data_page
);
744 if (type_page
&& data_page
) {
745 if (!strcmp((char *)type_page
, SMBFS_NAME
)) {
746 do_smb_super_data_conv((void *)data_page
);
747 } else if (!strcmp((char *)type_page
, NCPFS_NAME
)) {
748 do_ncp_super_data_conv((void *)data_page
);
749 } else if (!strcmp((char *)type_page
, NFS4_NAME
)) {
750 if (do_nfs4_super_data_conv((void *) data_page
))
756 retval
= do_mount((char*)dev_page
, dir_page
, (char*)type_page
,
757 flags
, (void*)data_page
);
761 free_page(data_page
);
767 free_page(type_page
);
772 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char __user *) (de)))
774 struct compat_old_linux_dirent
{
775 compat_ulong_t d_ino
;
776 compat_ulong_t d_offset
;
777 unsigned short d_namlen
;
781 struct compat_readdir_callback
{
782 struct compat_old_linux_dirent __user
*dirent
;
786 static int compat_fillonedir(void *__buf
, const char *name
, int namlen
,
787 loff_t offset
, u64 ino
, unsigned int d_type
)
789 struct compat_readdir_callback
*buf
= __buf
;
790 struct compat_old_linux_dirent __user
*dirent
;
791 compat_ulong_t d_ino
;
796 if (sizeof(d_ino
) < sizeof(ino
) && d_ino
!= ino
)
799 dirent
= buf
->dirent
;
800 if (!access_ok(VERIFY_WRITE
, dirent
,
801 (unsigned long)(dirent
->d_name
+ namlen
+ 1) -
802 (unsigned long)dirent
))
804 if ( __put_user(d_ino
, &dirent
->d_ino
) ||
805 __put_user(offset
, &dirent
->d_offset
) ||
806 __put_user(namlen
, &dirent
->d_namlen
) ||
807 __copy_to_user(dirent
->d_name
, name
, namlen
) ||
808 __put_user(0, dirent
->d_name
+ namlen
))
812 buf
->result
= -EFAULT
;
816 asmlinkage
long compat_sys_old_readdir(unsigned int fd
,
817 struct compat_old_linux_dirent __user
*dirent
, unsigned int count
)
821 struct compat_readdir_callback buf
;
831 error
= vfs_readdir(file
, compat_fillonedir
, &buf
);
840 struct compat_linux_dirent
{
841 compat_ulong_t d_ino
;
842 compat_ulong_t d_off
;
843 unsigned short d_reclen
;
847 struct compat_getdents_callback
{
848 struct compat_linux_dirent __user
*current_dir
;
849 struct compat_linux_dirent __user
*previous
;
854 static int compat_filldir(void *__buf
, const char *name
, int namlen
,
855 loff_t offset
, u64 ino
, unsigned int d_type
)
857 struct compat_linux_dirent __user
* dirent
;
858 struct compat_getdents_callback
*buf
= __buf
;
859 compat_ulong_t d_ino
;
860 int reclen
= ALIGN(NAME_OFFSET(dirent
) + namlen
+ 2, sizeof(compat_long_t
));
862 buf
->error
= -EINVAL
; /* only used if we fail.. */
863 if (reclen
> buf
->count
)
866 if (sizeof(d_ino
) < sizeof(ino
) && d_ino
!= ino
)
868 dirent
= buf
->previous
;
870 if (__put_user(offset
, &dirent
->d_off
))
873 dirent
= buf
->current_dir
;
874 if (__put_user(d_ino
, &dirent
->d_ino
))
876 if (__put_user(reclen
, &dirent
->d_reclen
))
878 if (copy_to_user(dirent
->d_name
, name
, namlen
))
880 if (__put_user(0, dirent
->d_name
+ namlen
))
882 if (__put_user(d_type
, (char __user
*) dirent
+ reclen
- 1))
884 buf
->previous
= dirent
;
885 dirent
= (void __user
*)dirent
+ reclen
;
886 buf
->current_dir
= dirent
;
887 buf
->count
-= reclen
;
890 buf
->error
= -EFAULT
;
894 asmlinkage
long compat_sys_getdents(unsigned int fd
,
895 struct compat_linux_dirent __user
*dirent
, unsigned int count
)
898 struct compat_linux_dirent __user
* lastdirent
;
899 struct compat_getdents_callback buf
;
903 if (!access_ok(VERIFY_WRITE
, dirent
, count
))
911 buf
.current_dir
= dirent
;
916 error
= vfs_readdir(file
, compat_filldir
, &buf
);
920 lastdirent
= buf
.previous
;
922 if (put_user(file
->f_pos
, &lastdirent
->d_off
))
925 error
= count
- buf
.count
;
934 #ifndef __ARCH_OMIT_COMPAT_SYS_GETDENTS64
936 struct compat_getdents_callback64
{
937 struct linux_dirent64 __user
*current_dir
;
938 struct linux_dirent64 __user
*previous
;
943 static int compat_filldir64(void * __buf
, const char * name
, int namlen
, loff_t offset
,
944 u64 ino
, unsigned int d_type
)
946 struct linux_dirent64 __user
*dirent
;
947 struct compat_getdents_callback64
*buf
= __buf
;
948 int jj
= NAME_OFFSET(dirent
);
949 int reclen
= ALIGN(jj
+ namlen
+ 1, sizeof(u64
));
952 buf
->error
= -EINVAL
; /* only used if we fail.. */
953 if (reclen
> buf
->count
)
955 dirent
= buf
->previous
;
958 if (__put_user_unaligned(offset
, &dirent
->d_off
))
961 dirent
= buf
->current_dir
;
962 if (__put_user_unaligned(ino
, &dirent
->d_ino
))
965 if (__put_user_unaligned(off
, &dirent
->d_off
))
967 if (__put_user(reclen
, &dirent
->d_reclen
))
969 if (__put_user(d_type
, &dirent
->d_type
))
971 if (copy_to_user(dirent
->d_name
, name
, namlen
))
973 if (__put_user(0, dirent
->d_name
+ namlen
))
975 buf
->previous
= dirent
;
976 dirent
= (void __user
*)dirent
+ reclen
;
977 buf
->current_dir
= dirent
;
978 buf
->count
-= reclen
;
981 buf
->error
= -EFAULT
;
985 asmlinkage
long compat_sys_getdents64(unsigned int fd
,
986 struct linux_dirent64 __user
* dirent
, unsigned int count
)
989 struct linux_dirent64 __user
* lastdirent
;
990 struct compat_getdents_callback64 buf
;
994 if (!access_ok(VERIFY_WRITE
, dirent
, count
))
1002 buf
.current_dir
= dirent
;
1003 buf
.previous
= NULL
;
1007 error
= vfs_readdir(file
, compat_filldir64
, &buf
);
1011 lastdirent
= buf
.previous
;
1013 typeof(lastdirent
->d_off
) d_off
= file
->f_pos
;
1015 if (__put_user_unaligned(d_off
, &lastdirent
->d_off
))
1017 error
= count
- buf
.count
;
1025 #endif /* ! __ARCH_OMIT_COMPAT_SYS_GETDENTS64 */
1027 static ssize_t
compat_do_readv_writev(int type
, struct file
*file
,
1028 const struct compat_iovec __user
*uvector
,
1029 unsigned long nr_segs
, loff_t
*pos
)
1031 compat_ssize_t tot_len
;
1032 struct iovec iovstack
[UIO_FASTIOV
];
1033 struct iovec
*iov
=iovstack
, *vector
;
1040 * SuS says "The readv() function *may* fail if the iovcnt argument
1041 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
1042 * traditionally returned zero for zero segments, so...
1049 * First get the "struct iovec" from user memory and
1050 * verify all the pointers
1053 if ((nr_segs
> UIO_MAXIOV
) || (nr_segs
<= 0))
1057 if (nr_segs
> UIO_FASTIOV
) {
1059 iov
= kmalloc(nr_segs
*sizeof(struct iovec
), GFP_KERNEL
);
1064 if (!access_ok(VERIFY_READ
, uvector
, nr_segs
*sizeof(*uvector
)))
1068 * Single unix specification:
1069 * We should -EINVAL if an element length is not >= 0 and fitting an
1070 * ssize_t. The total length is fitting an ssize_t
1072 * Be careful here because iov_len is a size_t not an ssize_t
1077 for (seg
= 0 ; seg
< nr_segs
; seg
++) {
1078 compat_ssize_t tmp
= tot_len
;
1082 if (__get_user(len
, &uvector
->iov_len
) ||
1083 __get_user(buf
, &uvector
->iov_base
)) {
1087 if (len
< 0) /* size_t not fitting an compat_ssize_t .. */
1090 if (tot_len
< tmp
) /* maths overflow on the compat_ssize_t */
1092 vector
->iov_base
= compat_ptr(buf
);
1093 vector
->iov_len
= (compat_size_t
) len
;
1102 ret
= rw_verify_area(type
, file
, pos
, tot_len
);
1106 ret
= security_file_permission(file
, type
== READ
? MAY_READ
:MAY_WRITE
);
1112 fn
= file
->f_op
->read
;
1113 fnv
= file
->f_op
->aio_read
;
1115 fn
= (io_fn_t
)file
->f_op
->write
;
1116 fnv
= file
->f_op
->aio_write
;
1120 ret
= do_sync_readv_writev(file
, iov
, nr_segs
, tot_len
,
1123 ret
= do_loop_readv_writev(file
, iov
, nr_segs
, pos
, fn
);
1126 if (iov
!= iovstack
)
1128 if ((ret
+ (type
== READ
)) > 0) {
1129 struct dentry
*dentry
= file
->f_path
.dentry
;
1131 fsnotify_access(dentry
);
1133 fsnotify_modify(dentry
);
1139 compat_sys_readv(unsigned long fd
, const struct compat_iovec __user
*vec
, unsigned long vlen
)
1142 ssize_t ret
= -EBADF
;
1148 if (!(file
->f_mode
& FMODE_READ
))
1152 if (!file
->f_op
|| (!file
->f_op
->aio_read
&& !file
->f_op
->read
))
1155 ret
= compat_do_readv_writev(READ
, file
, vec
, vlen
, &file
->f_pos
);
1163 compat_sys_writev(unsigned long fd
, const struct compat_iovec __user
*vec
, unsigned long vlen
)
1166 ssize_t ret
= -EBADF
;
1171 if (!(file
->f_mode
& FMODE_WRITE
))
1175 if (!file
->f_op
|| (!file
->f_op
->aio_write
&& !file
->f_op
->write
))
1178 ret
= compat_do_readv_writev(WRITE
, file
, vec
, vlen
, &file
->f_pos
);
1186 compat_sys_vmsplice(int fd
, const struct compat_iovec __user
*iov32
,
1187 unsigned int nr_segs
, unsigned int flags
)
1190 struct iovec __user
*iov
;
1191 if (nr_segs
> UIO_MAXIOV
)
1193 iov
= compat_alloc_user_space(nr_segs
* sizeof(struct iovec
));
1194 for (i
= 0; i
< nr_segs
; i
++) {
1195 struct compat_iovec v
;
1196 if (get_user(v
.iov_base
, &iov32
[i
].iov_base
) ||
1197 get_user(v
.iov_len
, &iov32
[i
].iov_len
) ||
1198 put_user(compat_ptr(v
.iov_base
), &iov
[i
].iov_base
) ||
1199 put_user(v
.iov_len
, &iov
[i
].iov_len
))
1202 return sys_vmsplice(fd
, iov
, nr_segs
, flags
);
1206 * Exactly like fs/open.c:sys_open(), except that it doesn't set the
1210 compat_sys_open(const char __user
*filename
, int flags
, int mode
)
1212 return do_sys_open(AT_FDCWD
, filename
, flags
, mode
);
1216 * Exactly like fs/open.c:sys_openat(), except that it doesn't set the
1220 compat_sys_openat(unsigned int dfd
, const char __user
*filename
, int flags
, int mode
)
1222 return do_sys_open(dfd
, filename
, flags
, mode
);
1226 * compat_count() counts the number of arguments/envelopes. It is basically
1227 * a copy of count() from fs/exec.c, except that it works with 32 bit argv
1228 * and envp pointers.
1230 static int compat_count(compat_uptr_t __user
*argv
, int max
)
1238 if (get_user(p
, argv
))
1251 * compat_copy_strings() is basically a copy of copy_strings() from fs/exec.c
1252 * except that it works with 32 bit argv and envp pointers.
1254 static int compat_copy_strings(int argc
, compat_uptr_t __user
*argv
,
1255 struct linux_binprm
*bprm
)
1257 struct page
*kmapped_page
= NULL
;
1261 while (argc
-- > 0) {
1266 if (get_user(str
, argv
+argc
) ||
1267 !(len
= strnlen_user(compat_ptr(str
), bprm
->p
))) {
1272 if (bprm
->p
< len
) {
1278 /* XXX: add architecture specific overflow check here. */
1283 int offset
, bytes_to_copy
;
1286 offset
= pos
% PAGE_SIZE
;
1288 page
= bprm
->page
[i
];
1291 page
= alloc_page(GFP_HIGHUSER
);
1292 bprm
->page
[i
] = page
;
1300 if (page
!= kmapped_page
) {
1302 kunmap(kmapped_page
);
1303 kmapped_page
= page
;
1304 kaddr
= kmap(kmapped_page
);
1307 memset(kaddr
, 0, offset
);
1308 bytes_to_copy
= PAGE_SIZE
- offset
;
1309 if (bytes_to_copy
> len
) {
1310 bytes_to_copy
= len
;
1312 memset(kaddr
+offset
+len
, 0,
1313 PAGE_SIZE
-offset
-len
);
1315 err
= copy_from_user(kaddr
+offset
, compat_ptr(str
),
1322 pos
+= bytes_to_copy
;
1323 str
+= bytes_to_copy
;
1324 len
-= bytes_to_copy
;
1330 kunmap(kmapped_page
);
1336 #define free_arg_pages(bprm) do { } while (0)
1340 static inline void free_arg_pages(struct linux_binprm
*bprm
)
1344 for (i
= 0; i
< MAX_ARG_PAGES
; i
++) {
1346 __free_page(bprm
->page
[i
]);
1347 bprm
->page
[i
] = NULL
;
1351 #endif /* CONFIG_MMU */
1354 * compat_do_execve() is mostly a copy of do_execve(), with the exception
1355 * that it processes 32 bit argv and envp pointers.
1357 int compat_do_execve(char * filename
,
1358 compat_uptr_t __user
*argv
,
1359 compat_uptr_t __user
*envp
,
1360 struct pt_regs
* regs
)
1362 struct linux_binprm
*bprm
;
1368 bprm
= kzalloc(sizeof(*bprm
), GFP_KERNEL
);
1372 file
= open_exec(filename
);
1373 retval
= PTR_ERR(file
);
1379 bprm
->p
= PAGE_SIZE
*MAX_ARG_PAGES
-sizeof(void *);
1381 bprm
->filename
= filename
;
1382 bprm
->interp
= filename
;
1383 bprm
->mm
= mm_alloc();
1388 retval
= init_new_context(current
, bprm
->mm
);
1392 bprm
->argc
= compat_count(argv
, bprm
->p
/ sizeof(compat_uptr_t
));
1393 if ((retval
= bprm
->argc
) < 0)
1396 bprm
->envc
= compat_count(envp
, bprm
->p
/ sizeof(compat_uptr_t
));
1397 if ((retval
= bprm
->envc
) < 0)
1400 retval
= security_bprm_alloc(bprm
);
1404 retval
= prepare_binprm(bprm
);
1408 retval
= copy_strings_kernel(1, &bprm
->filename
, bprm
);
1412 bprm
->exec
= bprm
->p
;
1413 retval
= compat_copy_strings(bprm
->envc
, envp
, bprm
);
1417 retval
= compat_copy_strings(bprm
->argc
, argv
, bprm
);
1421 retval
= search_binary_handler(bprm
, regs
);
1423 free_arg_pages(bprm
);
1425 /* execve success */
1426 security_bprm_free(bprm
);
1427 acct_update_integrals(current
);
1433 /* Something went wrong, return the inode and free the argument pages*/
1434 for (i
= 0 ; i
< MAX_ARG_PAGES
; i
++) {
1435 struct page
* page
= bprm
->page
[i
];
1441 security_bprm_free(bprm
);
1449 allow_write_access(bprm
->file
);
1460 #define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t))
1463 * Ooo, nasty. We need here to frob 32-bit unsigned longs to
1464 * 64-bit unsigned longs.
1467 int compat_get_fd_set(unsigned long nr
, compat_ulong_t __user
*ufdset
,
1468 unsigned long *fdset
)
1470 nr
= DIV_ROUND_UP(nr
, __COMPAT_NFDBITS
);
1474 if (!access_ok(VERIFY_WRITE
, ufdset
, nr
*sizeof(compat_ulong_t
)))
1481 if (__get_user(l
, ufdset
) || __get_user(h
, ufdset
+1))
1484 *fdset
++ = h
<< 32 | l
;
1487 if (odd
&& __get_user(*fdset
, ufdset
))
1490 /* Tricky, must clear full unsigned long in the
1491 * kernel fdset at the end, this makes sure that
1494 memset(fdset
, 0, ((nr
+ 1) & ~1)*sizeof(compat_ulong_t
));
1500 int compat_set_fd_set(unsigned long nr
, compat_ulong_t __user
*ufdset
,
1501 unsigned long *fdset
)
1504 nr
= DIV_ROUND_UP(nr
, __COMPAT_NFDBITS
);
1515 if (__put_user(l
, ufdset
) || __put_user(h
, ufdset
+1))
1520 if (odd
&& __put_user(*fdset
, ufdset
))
1527 * This is a virtual copy of sys_select from fs/select.c and probably
1528 * should be compared to it from time to time
1532 * We can actually return ERESTARTSYS instead of EINTR, but I'd
1533 * like to be certain this leads to no problems. So I return
1534 * EINTR just for safety.
1536 * Update: ERESTARTSYS breaks at least the xview clock binary, so
1537 * I'm trying ERESTARTNOHAND which restart only when you want to.
1539 #define MAX_SELECT_SECONDS \
1540 ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
1542 int compat_core_sys_select(int n
, compat_ulong_t __user
*inp
,
1543 compat_ulong_t __user
*outp
, compat_ulong_t __user
*exp
, s64
*timeout
)
1547 int size
, max_fds
, ret
= -EINVAL
;
1548 struct fdtable
*fdt
;
1553 /* max_fds can increase, so grab it once to avoid race */
1555 fdt
= files_fdtable(current
->files
);
1556 max_fds
= fdt
->max_fds
;
1562 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
1563 * since we used fdset we need to allocate memory in units of
1567 size
= FDS_BYTES(n
);
1568 bits
= kmalloc(6 * size
, GFP_KERNEL
);
1571 fds
.in
= (unsigned long *) bits
;
1572 fds
.out
= (unsigned long *) (bits
+ size
);
1573 fds
.ex
= (unsigned long *) (bits
+ 2*size
);
1574 fds
.res_in
= (unsigned long *) (bits
+ 3*size
);
1575 fds
.res_out
= (unsigned long *) (bits
+ 4*size
);
1576 fds
.res_ex
= (unsigned long *) (bits
+ 5*size
);
1578 if ((ret
= compat_get_fd_set(n
, inp
, fds
.in
)) ||
1579 (ret
= compat_get_fd_set(n
, outp
, fds
.out
)) ||
1580 (ret
= compat_get_fd_set(n
, exp
, fds
.ex
)))
1582 zero_fd_set(n
, fds
.res_in
);
1583 zero_fd_set(n
, fds
.res_out
);
1584 zero_fd_set(n
, fds
.res_ex
);
1586 ret
= do_select(n
, &fds
, timeout
);
1591 ret
= -ERESTARTNOHAND
;
1592 if (signal_pending(current
))
1597 if (compat_set_fd_set(n
, inp
, fds
.res_in
) ||
1598 compat_set_fd_set(n
, outp
, fds
.res_out
) ||
1599 compat_set_fd_set(n
, exp
, fds
.res_ex
))
1607 asmlinkage
long compat_sys_select(int n
, compat_ulong_t __user
*inp
,
1608 compat_ulong_t __user
*outp
, compat_ulong_t __user
*exp
,
1609 struct compat_timeval __user
*tvp
)
1612 struct compat_timeval tv
;
1616 if (copy_from_user(&tv
, tvp
, sizeof(tv
)))
1619 if (tv
.tv_sec
< 0 || tv
.tv_usec
< 0)
1622 /* Cast to u64 to make GCC stop complaining */
1623 if ((u64
)tv
.tv_sec
>= (u64
)MAX_INT64_SECONDS
)
1624 timeout
= -1; /* infinite */
1626 timeout
= DIV_ROUND_UP(tv
.tv_usec
, 1000000/HZ
);
1627 timeout
+= tv
.tv_sec
* HZ
;
1631 ret
= compat_core_sys_select(n
, inp
, outp
, exp
, &timeout
);
1634 struct compat_timeval rtv
;
1636 if (current
->personality
& STICKY_TIMEOUTS
)
1638 rtv
.tv_usec
= jiffies_to_usecs(do_div((*(u64
*)&timeout
), HZ
));
1639 rtv
.tv_sec
= timeout
;
1640 if (compat_timeval_compare(&rtv
, &tv
) >= 0)
1642 if (copy_to_user(tvp
, &rtv
, sizeof(rtv
))) {
1645 * If an application puts its timeval in read-only
1646 * memory, we don't want the Linux-specific update to
1647 * the timeval to cause a fault after the select has
1648 * completed successfully. However, because we're not
1649 * updating the timeval, we can't restart the system
1652 if (ret
== -ERESTARTNOHAND
)
1660 #ifdef TIF_RESTORE_SIGMASK
1661 asmlinkage
long compat_sys_pselect7(int n
, compat_ulong_t __user
*inp
,
1662 compat_ulong_t __user
*outp
, compat_ulong_t __user
*exp
,
1663 struct compat_timespec __user
*tsp
, compat_sigset_t __user
*sigmask
,
1664 compat_size_t sigsetsize
)
1666 compat_sigset_t ss32
;
1667 sigset_t ksigmask
, sigsaved
;
1668 s64 timeout
= MAX_SCHEDULE_TIMEOUT
;
1669 struct compat_timespec ts
;
1673 if (copy_from_user(&ts
, tsp
, sizeof(ts
)))
1676 if (ts
.tv_sec
< 0 || ts
.tv_nsec
< 0)
1681 if (sigsetsize
!= sizeof(compat_sigset_t
))
1683 if (copy_from_user(&ss32
, sigmask
, sizeof(ss32
)))
1685 sigset_from_compat(&ksigmask
, &ss32
);
1687 sigdelsetmask(&ksigmask
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
1688 sigprocmask(SIG_SETMASK
, &ksigmask
, &sigsaved
);
1693 if ((unsigned long)ts
.tv_sec
< MAX_SELECT_SECONDS
) {
1694 timeout
= DIV_ROUND_UP(ts
.tv_nsec
, 1000000000/HZ
);
1695 timeout
+= ts
.tv_sec
* (unsigned long)HZ
;
1699 ts
.tv_sec
-= MAX_SELECT_SECONDS
;
1700 timeout
= MAX_SELECT_SECONDS
* HZ
;
1704 ret
= compat_core_sys_select(n
, inp
, outp
, exp
, &timeout
);
1706 } while (!ret
&& !timeout
&& tsp
&& (ts
.tv_sec
|| ts
.tv_nsec
));
1709 struct compat_timespec rts
;
1711 if (current
->personality
& STICKY_TIMEOUTS
)
1714 rts
.tv_sec
= timeout
/ HZ
;
1715 rts
.tv_nsec
= (timeout
% HZ
) * (NSEC_PER_SEC
/HZ
);
1716 if (rts
.tv_nsec
>= NSEC_PER_SEC
) {
1718 rts
.tv_nsec
-= NSEC_PER_SEC
;
1720 if (compat_timespec_compare(&rts
, &ts
) >= 0)
1722 if (copy_to_user(tsp
, &rts
, sizeof(rts
))) {
1725 * If an application puts its timeval in read-only
1726 * memory, we don't want the Linux-specific update to
1727 * the timeval to cause a fault after the select has
1728 * completed successfully. However, because we're not
1729 * updating the timeval, we can't restart the system
1732 if (ret
== -ERESTARTNOHAND
)
1737 if (ret
== -ERESTARTNOHAND
) {
1739 * Don't restore the signal mask yet. Let do_signal() deliver
1740 * the signal on the way back to userspace, before the signal
1744 memcpy(¤t
->saved_sigmask
, &sigsaved
,
1746 set_thread_flag(TIF_RESTORE_SIGMASK
);
1749 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
1754 asmlinkage
long compat_sys_pselect6(int n
, compat_ulong_t __user
*inp
,
1755 compat_ulong_t __user
*outp
, compat_ulong_t __user
*exp
,
1756 struct compat_timespec __user
*tsp
, void __user
*sig
)
1758 compat_size_t sigsetsize
= 0;
1759 compat_uptr_t up
= 0;
1762 if (!access_ok(VERIFY_READ
, sig
,
1763 sizeof(compat_uptr_t
)+sizeof(compat_size_t
)) ||
1764 __get_user(up
, (compat_uptr_t __user
*)sig
) ||
1765 __get_user(sigsetsize
,
1766 (compat_size_t __user
*)(sig
+sizeof(up
))))
1769 return compat_sys_pselect7(n
, inp
, outp
, exp
, tsp
, compat_ptr(up
),
1773 asmlinkage
long compat_sys_ppoll(struct pollfd __user
*ufds
,
1774 unsigned int nfds
, struct compat_timespec __user
*tsp
,
1775 const compat_sigset_t __user
*sigmask
, compat_size_t sigsetsize
)
1777 compat_sigset_t ss32
;
1778 sigset_t ksigmask
, sigsaved
;
1779 struct compat_timespec ts
;
1784 if (copy_from_user(&ts
, tsp
, sizeof(ts
)))
1787 /* We assume that ts.tv_sec is always lower than
1788 the number of seconds that can be expressed in
1789 an s64. Otherwise the compiler bitches at us */
1790 timeout
= DIV_ROUND_UP(ts
.tv_nsec
, 1000000000/HZ
);
1791 timeout
+= ts
.tv_sec
* HZ
;
1795 if (sigsetsize
!= sizeof(compat_sigset_t
))
1797 if (copy_from_user(&ss32
, sigmask
, sizeof(ss32
)))
1799 sigset_from_compat(&ksigmask
, &ss32
);
1801 sigdelsetmask(&ksigmask
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
1802 sigprocmask(SIG_SETMASK
, &ksigmask
, &sigsaved
);
1805 ret
= do_sys_poll(ufds
, nfds
, &timeout
);
1807 /* We can restart this syscall, usually */
1808 if (ret
== -EINTR
) {
1810 * Don't restore the signal mask yet. Let do_signal() deliver
1811 * the signal on the way back to userspace, before the signal
1815 memcpy(¤t
->saved_sigmask
, &sigsaved
,
1817 set_thread_flag(TIF_RESTORE_SIGMASK
);
1819 ret
= -ERESTARTNOHAND
;
1821 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
1823 if (tsp
&& timeout
>= 0) {
1824 struct compat_timespec rts
;
1826 if (current
->personality
& STICKY_TIMEOUTS
)
1828 /* Yes, we know it's actually an s64, but it's also positive. */
1829 rts
.tv_nsec
= jiffies_to_usecs(do_div((*(u64
*)&timeout
), HZ
)) *
1831 rts
.tv_sec
= timeout
;
1832 if (compat_timespec_compare(&rts
, &ts
) >= 0)
1834 if (copy_to_user(tsp
, &rts
, sizeof(rts
))) {
1837 * If an application puts its timeval in read-only
1838 * memory, we don't want the Linux-specific update to
1839 * the timeval to cause a fault after the select has
1840 * completed successfully. However, because we're not
1841 * updating the timeval, we can't restart the system
1844 if (ret
== -ERESTARTNOHAND
&& timeout
>= 0)
1851 #endif /* TIF_RESTORE_SIGMASK */
1853 #if defined(CONFIG_NFSD) || defined(CONFIG_NFSD_MODULE)
1854 /* Stuff for NFS server syscalls... */
1855 struct compat_nfsctl_svc
{
1860 struct compat_nfsctl_client
{
1861 s8 cl32_ident
[NFSCLNT_IDMAX
+1];
1863 struct in_addr cl32_addrlist
[NFSCLNT_ADDRMAX
];
1866 u8 cl32_fhkey
[NFSCLNT_KEYMAX
];
1869 struct compat_nfsctl_export
{
1870 char ex32_client
[NFSCLNT_IDMAX
+1];
1871 char ex32_path
[NFS_MAXPATHLEN
+1];
1872 compat_dev_t ex32_dev
;
1873 compat_ino_t ex32_ino
;
1874 compat_int_t ex32_flags
;
1875 __compat_uid_t ex32_anon_uid
;
1876 __compat_gid_t ex32_anon_gid
;
1879 struct compat_nfsctl_fdparm
{
1880 struct sockaddr gd32_addr
;
1881 s8 gd32_path
[NFS_MAXPATHLEN
+1];
1882 compat_int_t gd32_version
;
1885 struct compat_nfsctl_fsparm
{
1886 struct sockaddr gd32_addr
;
1887 s8 gd32_path
[NFS_MAXPATHLEN
+1];
1888 compat_int_t gd32_maxlen
;
1891 struct compat_nfsctl_arg
{
1892 compat_int_t ca32_version
; /* safeguard */
1894 struct compat_nfsctl_svc u32_svc
;
1895 struct compat_nfsctl_client u32_client
;
1896 struct compat_nfsctl_export u32_export
;
1897 struct compat_nfsctl_fdparm u32_getfd
;
1898 struct compat_nfsctl_fsparm u32_getfs
;
1900 #define ca32_svc u.u32_svc
1901 #define ca32_client u.u32_client
1902 #define ca32_export u.u32_export
1903 #define ca32_getfd u.u32_getfd
1904 #define ca32_getfs u.u32_getfs
1907 union compat_nfsctl_res
{
1908 __u8 cr32_getfh
[NFS_FHSIZE
];
1909 struct knfsd_fh cr32_getfs
;
1912 static int compat_nfs_svc_trans(struct nfsctl_arg
*karg
,
1913 struct compat_nfsctl_arg __user
*arg
)
1915 if (!access_ok(VERIFY_READ
, &arg
->ca32_svc
, sizeof(arg
->ca32_svc
)) ||
1916 get_user(karg
->ca_version
, &arg
->ca32_version
) ||
1917 __get_user(karg
->ca_svc
.svc_port
, &arg
->ca32_svc
.svc32_port
) ||
1918 __get_user(karg
->ca_svc
.svc_nthreads
,
1919 &arg
->ca32_svc
.svc32_nthreads
))
1924 static int compat_nfs_clnt_trans(struct nfsctl_arg
*karg
,
1925 struct compat_nfsctl_arg __user
*arg
)
1927 if (!access_ok(VERIFY_READ
, &arg
->ca32_client
,
1928 sizeof(arg
->ca32_client
)) ||
1929 get_user(karg
->ca_version
, &arg
->ca32_version
) ||
1930 __copy_from_user(&karg
->ca_client
.cl_ident
[0],
1931 &arg
->ca32_client
.cl32_ident
[0],
1933 __get_user(karg
->ca_client
.cl_naddr
,
1934 &arg
->ca32_client
.cl32_naddr
) ||
1935 __copy_from_user(&karg
->ca_client
.cl_addrlist
[0],
1936 &arg
->ca32_client
.cl32_addrlist
[0],
1937 (sizeof(struct in_addr
) * NFSCLNT_ADDRMAX
)) ||
1938 __get_user(karg
->ca_client
.cl_fhkeytype
,
1939 &arg
->ca32_client
.cl32_fhkeytype
) ||
1940 __get_user(karg
->ca_client
.cl_fhkeylen
,
1941 &arg
->ca32_client
.cl32_fhkeylen
) ||
1942 __copy_from_user(&karg
->ca_client
.cl_fhkey
[0],
1943 &arg
->ca32_client
.cl32_fhkey
[0],
1950 static int compat_nfs_exp_trans(struct nfsctl_arg
*karg
,
1951 struct compat_nfsctl_arg __user
*arg
)
1953 if (!access_ok(VERIFY_READ
, &arg
->ca32_export
,
1954 sizeof(arg
->ca32_export
)) ||
1955 get_user(karg
->ca_version
, &arg
->ca32_version
) ||
1956 __copy_from_user(&karg
->ca_export
.ex_client
[0],
1957 &arg
->ca32_export
.ex32_client
[0],
1959 __copy_from_user(&karg
->ca_export
.ex_path
[0],
1960 &arg
->ca32_export
.ex32_path
[0],
1962 __get_user(karg
->ca_export
.ex_dev
,
1963 &arg
->ca32_export
.ex32_dev
) ||
1964 __get_user(karg
->ca_export
.ex_ino
,
1965 &arg
->ca32_export
.ex32_ino
) ||
1966 __get_user(karg
->ca_export
.ex_flags
,
1967 &arg
->ca32_export
.ex32_flags
) ||
1968 __get_user(karg
->ca_export
.ex_anon_uid
,
1969 &arg
->ca32_export
.ex32_anon_uid
) ||
1970 __get_user(karg
->ca_export
.ex_anon_gid
,
1971 &arg
->ca32_export
.ex32_anon_gid
))
1973 SET_UID(karg
->ca_export
.ex_anon_uid
, karg
->ca_export
.ex_anon_uid
);
1974 SET_GID(karg
->ca_export
.ex_anon_gid
, karg
->ca_export
.ex_anon_gid
);
1979 static int compat_nfs_getfd_trans(struct nfsctl_arg
*karg
,
1980 struct compat_nfsctl_arg __user
*arg
)
1982 if (!access_ok(VERIFY_READ
, &arg
->ca32_getfd
,
1983 sizeof(arg
->ca32_getfd
)) ||
1984 get_user(karg
->ca_version
, &arg
->ca32_version
) ||
1985 __copy_from_user(&karg
->ca_getfd
.gd_addr
,
1986 &arg
->ca32_getfd
.gd32_addr
,
1987 (sizeof(struct sockaddr
))) ||
1988 __copy_from_user(&karg
->ca_getfd
.gd_path
,
1989 &arg
->ca32_getfd
.gd32_path
,
1990 (NFS_MAXPATHLEN
+1)) ||
1991 __get_user(karg
->ca_getfd
.gd_version
,
1992 &arg
->ca32_getfd
.gd32_version
))
1998 static int compat_nfs_getfs_trans(struct nfsctl_arg
*karg
,
1999 struct compat_nfsctl_arg __user
*arg
)
2001 if (!access_ok(VERIFY_READ
,&arg
->ca32_getfs
,sizeof(arg
->ca32_getfs
)) ||
2002 get_user(karg
->ca_version
, &arg
->ca32_version
) ||
2003 __copy_from_user(&karg
->ca_getfs
.gd_addr
,
2004 &arg
->ca32_getfs
.gd32_addr
,
2005 (sizeof(struct sockaddr
))) ||
2006 __copy_from_user(&karg
->ca_getfs
.gd_path
,
2007 &arg
->ca32_getfs
.gd32_path
,
2008 (NFS_MAXPATHLEN
+1)) ||
2009 __get_user(karg
->ca_getfs
.gd_maxlen
,
2010 &arg
->ca32_getfs
.gd32_maxlen
))
2016 /* This really doesn't need translations, we are only passing
2017 * back a union which contains opaque nfs file handle data.
2019 static int compat_nfs_getfh_res_trans(union nfsctl_res
*kres
,
2020 union compat_nfsctl_res __user
*res
)
2024 err
= copy_to_user(res
, kres
, sizeof(*res
));
2026 return (err
) ? -EFAULT
: 0;
2029 asmlinkage
long compat_sys_nfsservctl(int cmd
,
2030 struct compat_nfsctl_arg __user
*arg
,
2031 union compat_nfsctl_res __user
*res
)
2033 struct nfsctl_arg
*karg
;
2034 union nfsctl_res
*kres
;
2038 karg
= kmalloc(sizeof(*karg
), GFP_USER
);
2039 kres
= kmalloc(sizeof(*kres
), GFP_USER
);
2040 if(!karg
|| !kres
) {
2047 err
= compat_nfs_svc_trans(karg
, arg
);
2050 case NFSCTL_ADDCLIENT
:
2051 err
= compat_nfs_clnt_trans(karg
, arg
);
2054 case NFSCTL_DELCLIENT
:
2055 err
= compat_nfs_clnt_trans(karg
, arg
);
2059 case NFSCTL_UNEXPORT
:
2060 err
= compat_nfs_exp_trans(karg
, arg
);
2064 err
= compat_nfs_getfd_trans(karg
, arg
);
2068 err
= compat_nfs_getfs_trans(karg
, arg
);
2081 /* The __user pointer casts are valid because of the set_fs() */
2082 err
= sys_nfsservctl(cmd
, (void __user
*) karg
, (void __user
*) kres
);
2088 if((cmd
== NFSCTL_GETFD
) ||
2089 (cmd
== NFSCTL_GETFS
))
2090 err
= compat_nfs_getfh_res_trans(kres
, res
);
2098 long asmlinkage
compat_sys_nfsservctl(int cmd
, void *notused
, void *notused2
)
2100 return sys_ni_syscall();
2106 #ifdef CONFIG_HAS_COMPAT_EPOLL_EVENT
2107 asmlinkage
long compat_sys_epoll_ctl(int epfd
, int op
, int fd
,
2108 struct compat_epoll_event __user
*event
)
2111 struct compat_epoll_event user
;
2112 struct epoll_event __user
*kernel
= NULL
;
2115 if (copy_from_user(&user
, event
, sizeof(user
)))
2117 kernel
= compat_alloc_user_space(sizeof(struct epoll_event
));
2118 err
|= __put_user(user
.events
, &kernel
->events
);
2119 err
|= __put_user(user
.data
, &kernel
->data
);
2122 return err
? err
: sys_epoll_ctl(epfd
, op
, fd
, kernel
);
2126 asmlinkage
long compat_sys_epoll_wait(int epfd
,
2127 struct compat_epoll_event __user
*events
,
2128 int maxevents
, int timeout
)
2130 long i
, ret
, err
= 0;
2131 struct epoll_event __user
*kbuf
;
2132 struct epoll_event ev
;
2134 if ((maxevents
<= 0) ||
2135 (maxevents
> (INT_MAX
/ sizeof(struct epoll_event
))))
2137 kbuf
= compat_alloc_user_space(sizeof(struct epoll_event
) * maxevents
);
2138 ret
= sys_epoll_wait(epfd
, kbuf
, maxevents
, timeout
);
2139 for (i
= 0; i
< ret
; i
++) {
2140 err
|= __get_user(ev
.events
, &kbuf
[i
].events
);
2141 err
|= __get_user(ev
.data
, &kbuf
[i
].data
);
2142 err
|= __put_user(ev
.events
, &events
->events
);
2143 err
|= __put_user_unaligned(ev
.data
, &events
->data
);
2147 return err
? -EFAULT
: ret
;
2149 #endif /* CONFIG_HAS_COMPAT_EPOLL_EVENT */
2151 #ifdef TIF_RESTORE_SIGMASK
2152 asmlinkage
long compat_sys_epoll_pwait(int epfd
,
2153 struct compat_epoll_event __user
*events
,
2154 int maxevents
, int timeout
,
2155 const compat_sigset_t __user
*sigmask
,
2156 compat_size_t sigsetsize
)
2159 compat_sigset_t csigmask
;
2160 sigset_t ksigmask
, sigsaved
;
2163 * If the caller wants a certain signal mask to be set during the wait,
2167 if (sigsetsize
!= sizeof(compat_sigset_t
))
2169 if (copy_from_user(&csigmask
, sigmask
, sizeof(csigmask
)))
2171 sigset_from_compat(&ksigmask
, &csigmask
);
2172 sigdelsetmask(&ksigmask
, sigmask(SIGKILL
) | sigmask(SIGSTOP
));
2173 sigprocmask(SIG_SETMASK
, &ksigmask
, &sigsaved
);
2176 #ifdef CONFIG_HAS_COMPAT_EPOLL_EVENT
2177 err
= compat_sys_epoll_wait(epfd
, events
, maxevents
, timeout
);
2179 err
= sys_epoll_wait(epfd
, events
, maxevents
, timeout
);
2183 * If we changed the signal mask, we need to restore the original one.
2184 * In case we've got a signal while waiting, we do not restore the
2185 * signal mask yet, and we allow do_signal() to deliver the signal on
2186 * the way back to userspace, before the signal mask is restored.
2189 if (err
== -EINTR
) {
2190 memcpy(¤t
->saved_sigmask
, &sigsaved
,
2192 set_thread_flag(TIF_RESTORE_SIGMASK
);
2194 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
2199 #endif /* TIF_RESTORE_SIGMASK */
2201 #endif /* CONFIG_EPOLL */