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
)
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
);
92 asmlinkage
long compat_sys_futimesat(unsigned int dfd
, char __user
*filename
, struct compat_timeval __user
*t
)
97 if (get_user(tv
[0].tv_sec
, &t
[0].tv_sec
) ||
98 get_user(tv
[0].tv_usec
, &t
[0].tv_usec
) ||
99 get_user(tv
[1].tv_sec
, &t
[1].tv_sec
) ||
100 get_user(tv
[1].tv_usec
, &t
[1].tv_usec
))
103 return do_utimes(dfd
, filename
, t
? tv
: NULL
);
106 asmlinkage
long compat_sys_utimes(char __user
*filename
, struct compat_timeval __user
*t
)
108 return compat_sys_futimesat(AT_FDCWD
, filename
, t
);
111 asmlinkage
long compat_sys_newstat(char __user
* filename
,
112 struct compat_stat __user
*statbuf
)
115 int error
= vfs_stat_fd(AT_FDCWD
, filename
, &stat
);
118 error
= cp_compat_stat(&stat
, statbuf
);
122 asmlinkage
long compat_sys_newlstat(char __user
* filename
,
123 struct compat_stat __user
*statbuf
)
126 int error
= vfs_lstat_fd(AT_FDCWD
, filename
, &stat
);
129 error
= cp_compat_stat(&stat
, statbuf
);
133 #ifndef __ARCH_WANT_STAT64
134 asmlinkage
long compat_sys_newfstatat(unsigned int dfd
, char __user
*filename
,
135 struct compat_stat __user
*statbuf
, int flag
)
140 if ((flag
& ~AT_SYMLINK_NOFOLLOW
) != 0)
143 if (flag
& AT_SYMLINK_NOFOLLOW
)
144 error
= vfs_lstat_fd(dfd
, filename
, &stat
);
146 error
= vfs_stat_fd(dfd
, filename
, &stat
);
149 error
= cp_compat_stat(&stat
, statbuf
);
156 asmlinkage
long compat_sys_newfstat(unsigned int fd
,
157 struct compat_stat __user
* statbuf
)
160 int error
= vfs_fstat(fd
, &stat
);
163 error
= cp_compat_stat(&stat
, statbuf
);
167 static int put_compat_statfs(struct compat_statfs __user
*ubuf
, struct kstatfs
*kbuf
)
170 if (sizeof ubuf
->f_blocks
== 4) {
171 if ((kbuf
->f_blocks
| kbuf
->f_bfree
| kbuf
->f_bavail
) &
172 0xffffffff00000000ULL
)
174 /* f_files and f_ffree may be -1; it's okay
175 * to stuff that into 32 bits */
176 if (kbuf
->f_files
!= 0xffffffffffffffffULL
177 && (kbuf
->f_files
& 0xffffffff00000000ULL
))
179 if (kbuf
->f_ffree
!= 0xffffffffffffffffULL
180 && (kbuf
->f_ffree
& 0xffffffff00000000ULL
))
183 if (!access_ok(VERIFY_WRITE
, ubuf
, sizeof(*ubuf
)) ||
184 __put_user(kbuf
->f_type
, &ubuf
->f_type
) ||
185 __put_user(kbuf
->f_bsize
, &ubuf
->f_bsize
) ||
186 __put_user(kbuf
->f_blocks
, &ubuf
->f_blocks
) ||
187 __put_user(kbuf
->f_bfree
, &ubuf
->f_bfree
) ||
188 __put_user(kbuf
->f_bavail
, &ubuf
->f_bavail
) ||
189 __put_user(kbuf
->f_files
, &ubuf
->f_files
) ||
190 __put_user(kbuf
->f_ffree
, &ubuf
->f_ffree
) ||
191 __put_user(kbuf
->f_namelen
, &ubuf
->f_namelen
) ||
192 __put_user(kbuf
->f_fsid
.val
[0], &ubuf
->f_fsid
.val
[0]) ||
193 __put_user(kbuf
->f_fsid
.val
[1], &ubuf
->f_fsid
.val
[1]) ||
194 __put_user(kbuf
->f_frsize
, &ubuf
->f_frsize
) ||
195 __put_user(0, &ubuf
->f_spare
[0]) ||
196 __put_user(0, &ubuf
->f_spare
[1]) ||
197 __put_user(0, &ubuf
->f_spare
[2]) ||
198 __put_user(0, &ubuf
->f_spare
[3]) ||
199 __put_user(0, &ubuf
->f_spare
[4]))
205 * The following statfs calls are copies of code from fs/open.c and
206 * should be checked against those from time to time
208 asmlinkage
long compat_sys_statfs(const char __user
*path
, struct compat_statfs __user
*buf
)
213 error
= user_path_walk(path
, &nd
);
216 error
= vfs_statfs(nd
.dentry
, &tmp
);
218 error
= put_compat_statfs(buf
, &tmp
);
224 asmlinkage
long compat_sys_fstatfs(unsigned int fd
, struct compat_statfs __user
*buf
)
234 error
= vfs_statfs(file
->f_path
.dentry
, &tmp
);
236 error
= put_compat_statfs(buf
, &tmp
);
242 static int put_compat_statfs64(struct compat_statfs64 __user
*ubuf
, struct kstatfs
*kbuf
)
244 if (sizeof ubuf
->f_blocks
== 4) {
245 if ((kbuf
->f_blocks
| kbuf
->f_bfree
| kbuf
->f_bavail
) &
246 0xffffffff00000000ULL
)
248 /* f_files and f_ffree may be -1; it's okay
249 * to stuff that into 32 bits */
250 if (kbuf
->f_files
!= 0xffffffffffffffffULL
251 && (kbuf
->f_files
& 0xffffffff00000000ULL
))
253 if (kbuf
->f_ffree
!= 0xffffffffffffffffULL
254 && (kbuf
->f_ffree
& 0xffffffff00000000ULL
))
257 if (!access_ok(VERIFY_WRITE
, ubuf
, sizeof(*ubuf
)) ||
258 __put_user(kbuf
->f_type
, &ubuf
->f_type
) ||
259 __put_user(kbuf
->f_bsize
, &ubuf
->f_bsize
) ||
260 __put_user(kbuf
->f_blocks
, &ubuf
->f_blocks
) ||
261 __put_user(kbuf
->f_bfree
, &ubuf
->f_bfree
) ||
262 __put_user(kbuf
->f_bavail
, &ubuf
->f_bavail
) ||
263 __put_user(kbuf
->f_files
, &ubuf
->f_files
) ||
264 __put_user(kbuf
->f_ffree
, &ubuf
->f_ffree
) ||
265 __put_user(kbuf
->f_namelen
, &ubuf
->f_namelen
) ||
266 __put_user(kbuf
->f_fsid
.val
[0], &ubuf
->f_fsid
.val
[0]) ||
267 __put_user(kbuf
->f_fsid
.val
[1], &ubuf
->f_fsid
.val
[1]) ||
268 __put_user(kbuf
->f_frsize
, &ubuf
->f_frsize
))
273 asmlinkage
long compat_sys_statfs64(const char __user
*path
, compat_size_t sz
, struct compat_statfs64 __user
*buf
)
278 if (sz
!= sizeof(*buf
))
281 error
= user_path_walk(path
, &nd
);
284 error
= vfs_statfs(nd
.dentry
, &tmp
);
286 error
= put_compat_statfs64(buf
, &tmp
);
292 asmlinkage
long compat_sys_fstatfs64(unsigned int fd
, compat_size_t sz
, struct compat_statfs64 __user
*buf
)
298 if (sz
!= sizeof(*buf
))
305 error
= vfs_statfs(file
->f_path
.dentry
, &tmp
);
307 error
= put_compat_statfs64(buf
, &tmp
);
313 static int get_compat_flock(struct flock
*kfl
, struct compat_flock __user
*ufl
)
315 if (!access_ok(VERIFY_READ
, ufl
, sizeof(*ufl
)) ||
316 __get_user(kfl
->l_type
, &ufl
->l_type
) ||
317 __get_user(kfl
->l_whence
, &ufl
->l_whence
) ||
318 __get_user(kfl
->l_start
, &ufl
->l_start
) ||
319 __get_user(kfl
->l_len
, &ufl
->l_len
) ||
320 __get_user(kfl
->l_pid
, &ufl
->l_pid
))
325 static int put_compat_flock(struct flock
*kfl
, struct compat_flock __user
*ufl
)
327 if (!access_ok(VERIFY_WRITE
, ufl
, sizeof(*ufl
)) ||
328 __put_user(kfl
->l_type
, &ufl
->l_type
) ||
329 __put_user(kfl
->l_whence
, &ufl
->l_whence
) ||
330 __put_user(kfl
->l_start
, &ufl
->l_start
) ||
331 __put_user(kfl
->l_len
, &ufl
->l_len
) ||
332 __put_user(kfl
->l_pid
, &ufl
->l_pid
))
337 #ifndef HAVE_ARCH_GET_COMPAT_FLOCK64
338 static int get_compat_flock64(struct flock
*kfl
, struct compat_flock64 __user
*ufl
)
340 if (!access_ok(VERIFY_READ
, ufl
, sizeof(*ufl
)) ||
341 __get_user(kfl
->l_type
, &ufl
->l_type
) ||
342 __get_user(kfl
->l_whence
, &ufl
->l_whence
) ||
343 __get_user(kfl
->l_start
, &ufl
->l_start
) ||
344 __get_user(kfl
->l_len
, &ufl
->l_len
) ||
345 __get_user(kfl
->l_pid
, &ufl
->l_pid
))
351 #ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64
352 static int put_compat_flock64(struct flock
*kfl
, struct compat_flock64 __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
))
365 asmlinkage
long compat_sys_fcntl64(unsigned int fd
, unsigned int cmd
,
376 ret
= get_compat_flock(&f
, compat_ptr(arg
));
381 ret
= sys_fcntl(fd
, cmd
, (unsigned long)&f
);
383 if (cmd
== F_GETLK
&& ret
== 0) {
384 /* GETLK was successfule and we need to return the data...
385 * but it needs to fit in the compat structure.
386 * l_start shouldn't be too big, unless the original
387 * start + end is greater than COMPAT_OFF_T_MAX, in which
388 * case the app was asking for trouble, so we return
389 * -EOVERFLOW in that case.
390 * l_len could be too big, in which case we just truncate it,
391 * and only allow the app to see that part of the conflicting
392 * lock that might make sense to it anyway
395 if (f
.l_start
> COMPAT_OFF_T_MAX
)
397 if (f
.l_len
> COMPAT_OFF_T_MAX
)
398 f
.l_len
= COMPAT_OFF_T_MAX
;
400 ret
= put_compat_flock(&f
, compat_ptr(arg
));
407 ret
= get_compat_flock64(&f
, compat_ptr(arg
));
412 ret
= sys_fcntl(fd
, (cmd
== F_GETLK64
) ? F_GETLK
:
413 ((cmd
== F_SETLK64
) ? F_SETLK
: F_SETLKW
),
416 if (cmd
== F_GETLK64
&& ret
== 0) {
417 /* need to return lock information - see above for commentary */
418 if (f
.l_start
> COMPAT_LOFF_T_MAX
)
420 if (f
.l_len
> COMPAT_LOFF_T_MAX
)
421 f
.l_len
= COMPAT_LOFF_T_MAX
;
423 ret
= put_compat_flock64(&f
, compat_ptr(arg
));
428 ret
= sys_fcntl(fd
, cmd
, arg
);
434 asmlinkage
long compat_sys_fcntl(unsigned int fd
, unsigned int cmd
,
437 if ((cmd
== F_GETLK64
) || (cmd
== F_SETLK64
) || (cmd
== F_SETLKW64
))
439 return compat_sys_fcntl64(fd
, cmd
, arg
);
443 compat_sys_io_setup(unsigned nr_reqs
, u32 __user
*ctx32p
)
448 mm_segment_t oldfs
= get_fs();
449 if (unlikely(get_user(ctx64
, ctx32p
)))
453 /* The __user pointer cast is valid because of the set_fs() */
454 ret
= sys_io_setup(nr_reqs
, (aio_context_t __user
*) &ctx64
);
456 /* truncating is ok because it's a user address */
458 ret
= put_user((u32
) ctx64
, ctx32p
);
463 compat_sys_io_getevents(aio_context_t ctx_id
,
464 unsigned long min_nr
,
466 struct io_event __user
*events
,
467 struct compat_timespec __user
*timeout
)
471 struct timespec __user
*ut
= NULL
;
474 if (unlikely(!access_ok(VERIFY_WRITE
, events
,
475 nr
* sizeof(struct io_event
))))
478 if (get_compat_timespec(&t
, timeout
))
481 ut
= compat_alloc_user_space(sizeof(*ut
));
482 if (copy_to_user(ut
, &t
, sizeof(t
)) )
485 ret
= sys_io_getevents(ctx_id
, min_nr
, nr
, events
, ut
);
491 copy_iocb(long nr
, u32 __user
*ptr32
, struct iocb __user
* __user
*ptr64
)
496 for (i
= 0; i
< nr
; ++i
) {
497 if (get_user(uptr
, ptr32
+ i
))
499 if (put_user(compat_ptr(uptr
), ptr64
+ i
))
505 #define MAX_AIO_SUBMITS (PAGE_SIZE/sizeof(struct iocb *))
508 compat_sys_io_submit(aio_context_t ctx_id
, int nr
, u32 __user
*iocb
)
510 struct iocb __user
* __user
*iocb64
;
513 if (unlikely(nr
< 0))
516 if (nr
> MAX_AIO_SUBMITS
)
517 nr
= MAX_AIO_SUBMITS
;
519 iocb64
= compat_alloc_user_space(nr
* sizeof(*iocb64
));
520 ret
= copy_iocb(nr
, iocb
, iocb64
);
522 ret
= sys_io_submit(ctx_id
, nr
, iocb64
);
526 struct compat_ncp_mount_data
{
527 compat_int_t version
;
528 compat_uint_t ncp_fd
;
529 __compat_uid_t mounted_uid
;
530 compat_pid_t wdog_pid
;
531 unsigned char mounted_vol
[NCP_VOLNAME_LEN
+ 1];
532 compat_uint_t time_out
;
533 compat_uint_t retry_count
;
537 compat_mode_t file_mode
;
538 compat_mode_t dir_mode
;
541 struct compat_ncp_mount_data_v4
{
542 compat_int_t version
;
543 compat_ulong_t flags
;
544 compat_ulong_t mounted_uid
;
545 compat_long_t wdog_pid
;
546 compat_uint_t ncp_fd
;
547 compat_uint_t time_out
;
548 compat_uint_t retry_count
;
551 compat_ulong_t file_mode
;
552 compat_ulong_t dir_mode
;
555 static void *do_ncp_super_data_conv(void *raw_data
)
557 int version
= *(unsigned int *)raw_data
;
560 struct compat_ncp_mount_data
*c_n
= raw_data
;
561 struct ncp_mount_data
*n
= raw_data
;
563 n
->dir_mode
= c_n
->dir_mode
;
564 n
->file_mode
= c_n
->file_mode
;
567 memmove (n
->mounted_vol
, c_n
->mounted_vol
, (sizeof (c_n
->mounted_vol
) + 3 * sizeof (unsigned int)));
568 n
->wdog_pid
= c_n
->wdog_pid
;
569 n
->mounted_uid
= c_n
->mounted_uid
;
570 } else if (version
== 4) {
571 struct compat_ncp_mount_data_v4
*c_n
= raw_data
;
572 struct ncp_mount_data_v4
*n
= raw_data
;
574 n
->dir_mode
= c_n
->dir_mode
;
575 n
->file_mode
= c_n
->file_mode
;
578 n
->retry_count
= c_n
->retry_count
;
579 n
->time_out
= c_n
->time_out
;
580 n
->ncp_fd
= c_n
->ncp_fd
;
581 n
->wdog_pid
= c_n
->wdog_pid
;
582 n
->mounted_uid
= c_n
->mounted_uid
;
583 n
->flags
= c_n
->flags
;
584 } else if (version
!= 5) {
591 struct compat_smb_mount_data
{
592 compat_int_t version
;
593 __compat_uid_t mounted_uid
;
596 compat_mode_t file_mode
;
597 compat_mode_t dir_mode
;
600 static void *do_smb_super_data_conv(void *raw_data
)
602 struct smb_mount_data
*s
= raw_data
;
603 struct compat_smb_mount_data
*c_s
= raw_data
;
605 if (c_s
->version
!= SMB_MOUNT_OLDVERSION
)
607 s
->dir_mode
= c_s
->dir_mode
;
608 s
->file_mode
= c_s
->file_mode
;
611 s
->mounted_uid
= c_s
->mounted_uid
;
616 struct compat_nfs_string
{
621 static inline void compat_nfs_string(struct nfs_string
*dst
,
622 struct compat_nfs_string
*src
)
624 dst
->data
= compat_ptr(src
->data
);
628 struct compat_nfs4_mount_data_v1
{
629 compat_int_t version
;
634 compat_int_t retrans
;
635 compat_int_t acregmin
;
636 compat_int_t acregmax
;
637 compat_int_t acdirmin
;
638 compat_int_t acdirmax
;
639 struct compat_nfs_string client_addr
;
640 struct compat_nfs_string mnt_path
;
641 struct compat_nfs_string hostname
;
642 compat_uint_t host_addrlen
;
643 compat_uptr_t host_addr
;
645 compat_int_t auth_flavourlen
;
646 compat_uptr_t auth_flavours
;
649 static int do_nfs4_super_data_conv(void *raw_data
)
651 int version
= *(compat_uint_t
*) raw_data
;
654 struct compat_nfs4_mount_data_v1
*raw
= raw_data
;
655 struct nfs4_mount_data
*real
= raw_data
;
657 /* copy the fields backwards */
658 real
->auth_flavours
= compat_ptr(raw
->auth_flavours
);
659 real
->auth_flavourlen
= raw
->auth_flavourlen
;
660 real
->proto
= raw
->proto
;
661 real
->host_addr
= compat_ptr(raw
->host_addr
);
662 real
->host_addrlen
= raw
->host_addrlen
;
663 compat_nfs_string(&real
->hostname
, &raw
->hostname
);
664 compat_nfs_string(&real
->mnt_path
, &raw
->mnt_path
);
665 compat_nfs_string(&real
->client_addr
, &raw
->client_addr
);
666 real
->acdirmax
= raw
->acdirmax
;
667 real
->acdirmin
= raw
->acdirmin
;
668 real
->acregmax
= raw
->acregmax
;
669 real
->acregmin
= raw
->acregmin
;
670 real
->retrans
= raw
->retrans
;
671 real
->timeo
= raw
->timeo
;
672 real
->wsize
= raw
->wsize
;
673 real
->rsize
= raw
->rsize
;
674 real
->flags
= raw
->flags
;
675 real
->version
= raw
->version
;
684 #define SMBFS_NAME "smbfs"
685 #define NCPFS_NAME "ncpfs"
686 #define NFS4_NAME "nfs4"
688 asmlinkage
long compat_sys_mount(char __user
* dev_name
, char __user
* dir_name
,
689 char __user
* type
, unsigned long flags
,
692 unsigned long type_page
;
693 unsigned long data_page
;
694 unsigned long dev_page
;
698 retval
= copy_mount_options (type
, &type_page
);
702 dir_page
= getname(dir_name
);
703 retval
= PTR_ERR(dir_page
);
704 if (IS_ERR(dir_page
))
707 retval
= copy_mount_options (dev_name
, &dev_page
);
711 retval
= copy_mount_options (data
, &data_page
);
717 if (type_page
&& data_page
) {
718 if (!strcmp((char *)type_page
, SMBFS_NAME
)) {
719 do_smb_super_data_conv((void *)data_page
);
720 } else if (!strcmp((char *)type_page
, NCPFS_NAME
)) {
721 do_ncp_super_data_conv((void *)data_page
);
722 } else if (!strcmp((char *)type_page
, NFS4_NAME
)) {
723 if (do_nfs4_super_data_conv((void *) data_page
))
729 retval
= do_mount((char*)dev_page
, dir_page
, (char*)type_page
,
730 flags
, (void*)data_page
);
734 free_page(data_page
);
740 free_page(type_page
);
745 #define NAME_OFFSET(de) ((int) ((de)->d_name - (char __user *) (de)))
747 struct compat_old_linux_dirent
{
748 compat_ulong_t d_ino
;
749 compat_ulong_t d_offset
;
750 unsigned short d_namlen
;
754 struct compat_readdir_callback
{
755 struct compat_old_linux_dirent __user
*dirent
;
759 static int compat_fillonedir(void *__buf
, const char *name
, int namlen
,
760 loff_t offset
, u64 ino
, unsigned int d_type
)
762 struct compat_readdir_callback
*buf
= __buf
;
763 struct compat_old_linux_dirent __user
*dirent
;
764 compat_ulong_t d_ino
;
769 if (sizeof(d_ino
) < sizeof(ino
) && d_ino
!= ino
)
772 dirent
= buf
->dirent
;
773 if (!access_ok(VERIFY_WRITE
, dirent
,
774 (unsigned long)(dirent
->d_name
+ namlen
+ 1) -
775 (unsigned long)dirent
))
777 if ( __put_user(d_ino
, &dirent
->d_ino
) ||
778 __put_user(offset
, &dirent
->d_offset
) ||
779 __put_user(namlen
, &dirent
->d_namlen
) ||
780 __copy_to_user(dirent
->d_name
, name
, namlen
) ||
781 __put_user(0, dirent
->d_name
+ namlen
))
785 buf
->result
= -EFAULT
;
789 asmlinkage
long compat_sys_old_readdir(unsigned int fd
,
790 struct compat_old_linux_dirent __user
*dirent
, unsigned int count
)
794 struct compat_readdir_callback buf
;
804 error
= vfs_readdir(file
, compat_fillonedir
, &buf
);
813 struct compat_linux_dirent
{
814 compat_ulong_t d_ino
;
815 compat_ulong_t d_off
;
816 unsigned short d_reclen
;
820 struct compat_getdents_callback
{
821 struct compat_linux_dirent __user
*current_dir
;
822 struct compat_linux_dirent __user
*previous
;
827 static int compat_filldir(void *__buf
, const char *name
, int namlen
,
828 loff_t offset
, u64 ino
, unsigned int d_type
)
830 struct compat_linux_dirent __user
* dirent
;
831 struct compat_getdents_callback
*buf
= __buf
;
832 compat_ulong_t d_ino
;
833 int reclen
= ALIGN(NAME_OFFSET(dirent
) + namlen
+ 2, sizeof(compat_long_t
));
835 buf
->error
= -EINVAL
; /* only used if we fail.. */
836 if (reclen
> buf
->count
)
839 if (sizeof(d_ino
) < sizeof(ino
) && d_ino
!= ino
)
841 dirent
= buf
->previous
;
843 if (__put_user(offset
, &dirent
->d_off
))
846 dirent
= buf
->current_dir
;
847 if (__put_user(d_ino
, &dirent
->d_ino
))
849 if (__put_user(reclen
, &dirent
->d_reclen
))
851 if (copy_to_user(dirent
->d_name
, name
, namlen
))
853 if (__put_user(0, dirent
->d_name
+ namlen
))
855 if (__put_user(d_type
, (char __user
*) dirent
+ reclen
- 1))
857 buf
->previous
= dirent
;
858 dirent
= (void __user
*)dirent
+ reclen
;
859 buf
->current_dir
= dirent
;
860 buf
->count
-= reclen
;
863 buf
->error
= -EFAULT
;
867 asmlinkage
long compat_sys_getdents(unsigned int fd
,
868 struct compat_linux_dirent __user
*dirent
, unsigned int count
)
871 struct compat_linux_dirent __user
* lastdirent
;
872 struct compat_getdents_callback buf
;
876 if (!access_ok(VERIFY_WRITE
, dirent
, count
))
884 buf
.current_dir
= dirent
;
889 error
= vfs_readdir(file
, compat_filldir
, &buf
);
893 lastdirent
= buf
.previous
;
895 if (put_user(file
->f_pos
, &lastdirent
->d_off
))
898 error
= count
- buf
.count
;
907 #ifndef __ARCH_OMIT_COMPAT_SYS_GETDENTS64
909 struct compat_getdents_callback64
{
910 struct linux_dirent64 __user
*current_dir
;
911 struct linux_dirent64 __user
*previous
;
916 static int compat_filldir64(void * __buf
, const char * name
, int namlen
, loff_t offset
,
917 u64 ino
, unsigned int d_type
)
919 struct linux_dirent64 __user
*dirent
;
920 struct compat_getdents_callback64
*buf
= __buf
;
921 int jj
= NAME_OFFSET(dirent
);
922 int reclen
= ALIGN(jj
+ namlen
+ 1, sizeof(u64
));
925 buf
->error
= -EINVAL
; /* only used if we fail.. */
926 if (reclen
> buf
->count
)
928 dirent
= buf
->previous
;
931 if (__put_user_unaligned(offset
, &dirent
->d_off
))
934 dirent
= buf
->current_dir
;
935 if (__put_user_unaligned(ino
, &dirent
->d_ino
))
938 if (__put_user_unaligned(off
, &dirent
->d_off
))
940 if (__put_user(reclen
, &dirent
->d_reclen
))
942 if (__put_user(d_type
, &dirent
->d_type
))
944 if (copy_to_user(dirent
->d_name
, name
, namlen
))
946 if (__put_user(0, dirent
->d_name
+ namlen
))
948 buf
->previous
= dirent
;
949 dirent
= (void __user
*)dirent
+ reclen
;
950 buf
->current_dir
= dirent
;
951 buf
->count
-= reclen
;
954 buf
->error
= -EFAULT
;
958 asmlinkage
long compat_sys_getdents64(unsigned int fd
,
959 struct linux_dirent64 __user
* dirent
, unsigned int count
)
962 struct linux_dirent64 __user
* lastdirent
;
963 struct compat_getdents_callback64 buf
;
967 if (!access_ok(VERIFY_WRITE
, dirent
, count
))
975 buf
.current_dir
= dirent
;
980 error
= vfs_readdir(file
, compat_filldir64
, &buf
);
984 lastdirent
= buf
.previous
;
986 typeof(lastdirent
->d_off
) d_off
= file
->f_pos
;
988 if (__put_user_unaligned(d_off
, &lastdirent
->d_off
))
990 error
= count
- buf
.count
;
998 #endif /* ! __ARCH_OMIT_COMPAT_SYS_GETDENTS64 */
1000 static ssize_t
compat_do_readv_writev(int type
, struct file
*file
,
1001 const struct compat_iovec __user
*uvector
,
1002 unsigned long nr_segs
, loff_t
*pos
)
1004 compat_ssize_t tot_len
;
1005 struct iovec iovstack
[UIO_FASTIOV
];
1006 struct iovec
*iov
=iovstack
, *vector
;
1013 * SuS says "The readv() function *may* fail if the iovcnt argument
1014 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
1015 * traditionally returned zero for zero segments, so...
1022 * First get the "struct iovec" from user memory and
1023 * verify all the pointers
1026 if ((nr_segs
> UIO_MAXIOV
) || (nr_segs
<= 0))
1030 if (nr_segs
> UIO_FASTIOV
) {
1032 iov
= kmalloc(nr_segs
*sizeof(struct iovec
), GFP_KERNEL
);
1037 if (!access_ok(VERIFY_READ
, uvector
, nr_segs
*sizeof(*uvector
)))
1041 * Single unix specification:
1042 * We should -EINVAL if an element length is not >= 0 and fitting an
1043 * ssize_t. The total length is fitting an ssize_t
1045 * Be careful here because iov_len is a size_t not an ssize_t
1050 for (seg
= 0 ; seg
< nr_segs
; seg
++) {
1051 compat_ssize_t tmp
= tot_len
;
1055 if (__get_user(len
, &uvector
->iov_len
) ||
1056 __get_user(buf
, &uvector
->iov_base
)) {
1060 if (len
< 0) /* size_t not fitting an compat_ssize_t .. */
1063 if (tot_len
< tmp
) /* maths overflow on the compat_ssize_t */
1065 vector
->iov_base
= compat_ptr(buf
);
1066 vector
->iov_len
= (compat_size_t
) len
;
1075 ret
= rw_verify_area(type
, file
, pos
, tot_len
);
1079 ret
= security_file_permission(file
, type
== READ
? MAY_READ
:MAY_WRITE
);
1085 fn
= file
->f_op
->read
;
1086 fnv
= file
->f_op
->aio_read
;
1088 fn
= (io_fn_t
)file
->f_op
->write
;
1089 fnv
= file
->f_op
->aio_write
;
1093 ret
= do_sync_readv_writev(file
, iov
, nr_segs
, tot_len
,
1096 ret
= do_loop_readv_writev(file
, iov
, nr_segs
, pos
, fn
);
1099 if (iov
!= iovstack
)
1101 if ((ret
+ (type
== READ
)) > 0) {
1102 struct dentry
*dentry
= file
->f_path
.dentry
;
1104 fsnotify_access(dentry
);
1106 fsnotify_modify(dentry
);
1112 compat_sys_readv(unsigned long fd
, const struct compat_iovec __user
*vec
, unsigned long vlen
)
1115 ssize_t ret
= -EBADF
;
1121 if (!(file
->f_mode
& FMODE_READ
))
1125 if (!file
->f_op
|| (!file
->f_op
->aio_read
&& !file
->f_op
->read
))
1128 ret
= compat_do_readv_writev(READ
, file
, vec
, vlen
, &file
->f_pos
);
1136 compat_sys_writev(unsigned long fd
, const struct compat_iovec __user
*vec
, unsigned long vlen
)
1139 ssize_t ret
= -EBADF
;
1144 if (!(file
->f_mode
& FMODE_WRITE
))
1148 if (!file
->f_op
|| (!file
->f_op
->aio_write
&& !file
->f_op
->write
))
1151 ret
= compat_do_readv_writev(WRITE
, file
, vec
, vlen
, &file
->f_pos
);
1159 compat_sys_vmsplice(int fd
, const struct compat_iovec __user
*iov32
,
1160 unsigned int nr_segs
, unsigned int flags
)
1163 struct iovec __user
*iov
;
1164 if (nr_segs
> UIO_MAXIOV
)
1166 iov
= compat_alloc_user_space(nr_segs
* sizeof(struct iovec
));
1167 for (i
= 0; i
< nr_segs
; i
++) {
1168 struct compat_iovec v
;
1169 if (get_user(v
.iov_base
, &iov32
[i
].iov_base
) ||
1170 get_user(v
.iov_len
, &iov32
[i
].iov_len
) ||
1171 put_user(compat_ptr(v
.iov_base
), &iov
[i
].iov_base
) ||
1172 put_user(v
.iov_len
, &iov
[i
].iov_len
))
1175 return sys_vmsplice(fd
, iov
, nr_segs
, flags
);
1179 * Exactly like fs/open.c:sys_open(), except that it doesn't set the
1183 compat_sys_open(const char __user
*filename
, int flags
, int mode
)
1185 return do_sys_open(AT_FDCWD
, filename
, flags
, mode
);
1189 * Exactly like fs/open.c:sys_openat(), except that it doesn't set the
1193 compat_sys_openat(unsigned int dfd
, const char __user
*filename
, int flags
, int mode
)
1195 return do_sys_open(dfd
, filename
, flags
, mode
);
1199 * compat_count() counts the number of arguments/envelopes. It is basically
1200 * a copy of count() from fs/exec.c, except that it works with 32 bit argv
1201 * and envp pointers.
1203 static int compat_count(compat_uptr_t __user
*argv
, int max
)
1211 if (get_user(p
, argv
))
1224 * compat_copy_strings() is basically a copy of copy_strings() from fs/exec.c
1225 * except that it works with 32 bit argv and envp pointers.
1227 static int compat_copy_strings(int argc
, compat_uptr_t __user
*argv
,
1228 struct linux_binprm
*bprm
)
1230 struct page
*kmapped_page
= NULL
;
1234 while (argc
-- > 0) {
1239 if (get_user(str
, argv
+argc
) ||
1240 !(len
= strnlen_user(compat_ptr(str
), bprm
->p
))) {
1245 if (bprm
->p
< len
) {
1251 /* XXX: add architecture specific overflow check here. */
1256 int offset
, bytes_to_copy
;
1259 offset
= pos
% PAGE_SIZE
;
1261 page
= bprm
->page
[i
];
1264 page
= alloc_page(GFP_HIGHUSER
);
1265 bprm
->page
[i
] = page
;
1273 if (page
!= kmapped_page
) {
1275 kunmap(kmapped_page
);
1276 kmapped_page
= page
;
1277 kaddr
= kmap(kmapped_page
);
1280 memset(kaddr
, 0, offset
);
1281 bytes_to_copy
= PAGE_SIZE
- offset
;
1282 if (bytes_to_copy
> len
) {
1283 bytes_to_copy
= len
;
1285 memset(kaddr
+offset
+len
, 0,
1286 PAGE_SIZE
-offset
-len
);
1288 err
= copy_from_user(kaddr
+offset
, compat_ptr(str
),
1295 pos
+= bytes_to_copy
;
1296 str
+= bytes_to_copy
;
1297 len
-= bytes_to_copy
;
1303 kunmap(kmapped_page
);
1309 #define free_arg_pages(bprm) do { } while (0)
1313 static inline void free_arg_pages(struct linux_binprm
*bprm
)
1317 for (i
= 0; i
< MAX_ARG_PAGES
; i
++) {
1319 __free_page(bprm
->page
[i
]);
1320 bprm
->page
[i
] = NULL
;
1324 #endif /* CONFIG_MMU */
1327 * compat_do_execve() is mostly a copy of do_execve(), with the exception
1328 * that it processes 32 bit argv and envp pointers.
1330 int compat_do_execve(char * filename
,
1331 compat_uptr_t __user
*argv
,
1332 compat_uptr_t __user
*envp
,
1333 struct pt_regs
* regs
)
1335 struct linux_binprm
*bprm
;
1341 bprm
= kzalloc(sizeof(*bprm
), GFP_KERNEL
);
1345 file
= open_exec(filename
);
1346 retval
= PTR_ERR(file
);
1352 bprm
->p
= PAGE_SIZE
*MAX_ARG_PAGES
-sizeof(void *);
1354 bprm
->filename
= filename
;
1355 bprm
->interp
= filename
;
1356 bprm
->mm
= mm_alloc();
1361 retval
= init_new_context(current
, bprm
->mm
);
1365 bprm
->argc
= compat_count(argv
, bprm
->p
/ sizeof(compat_uptr_t
));
1366 if ((retval
= bprm
->argc
) < 0)
1369 bprm
->envc
= compat_count(envp
, bprm
->p
/ sizeof(compat_uptr_t
));
1370 if ((retval
= bprm
->envc
) < 0)
1373 retval
= security_bprm_alloc(bprm
);
1377 retval
= prepare_binprm(bprm
);
1381 retval
= copy_strings_kernel(1, &bprm
->filename
, bprm
);
1385 bprm
->exec
= bprm
->p
;
1386 retval
= compat_copy_strings(bprm
->envc
, envp
, bprm
);
1390 retval
= compat_copy_strings(bprm
->argc
, argv
, bprm
);
1394 retval
= search_binary_handler(bprm
, regs
);
1396 free_arg_pages(bprm
);
1398 /* execve success */
1399 security_bprm_free(bprm
);
1400 acct_update_integrals(current
);
1406 /* Something went wrong, return the inode and free the argument pages*/
1407 for (i
= 0 ; i
< MAX_ARG_PAGES
; i
++) {
1408 struct page
* page
= bprm
->page
[i
];
1414 security_bprm_free(bprm
);
1422 allow_write_access(bprm
->file
);
1433 #define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t))
1436 * Ooo, nasty. We need here to frob 32-bit unsigned longs to
1437 * 64-bit unsigned longs.
1440 int compat_get_fd_set(unsigned long nr
, compat_ulong_t __user
*ufdset
,
1441 unsigned long *fdset
)
1443 nr
= DIV_ROUND_UP(nr
, __COMPAT_NFDBITS
);
1447 if (!access_ok(VERIFY_WRITE
, ufdset
, nr
*sizeof(compat_ulong_t
)))
1454 if (__get_user(l
, ufdset
) || __get_user(h
, ufdset
+1))
1457 *fdset
++ = h
<< 32 | l
;
1460 if (odd
&& __get_user(*fdset
, ufdset
))
1463 /* Tricky, must clear full unsigned long in the
1464 * kernel fdset at the end, this makes sure that
1467 memset(fdset
, 0, ((nr
+ 1) & ~1)*sizeof(compat_ulong_t
));
1473 int compat_set_fd_set(unsigned long nr
, compat_ulong_t __user
*ufdset
,
1474 unsigned long *fdset
)
1477 nr
= DIV_ROUND_UP(nr
, __COMPAT_NFDBITS
);
1488 if (__put_user(l
, ufdset
) || __put_user(h
, ufdset
+1))
1493 if (odd
&& __put_user(*fdset
, ufdset
))
1500 * This is a virtual copy of sys_select from fs/select.c and probably
1501 * should be compared to it from time to time
1505 * We can actually return ERESTARTSYS instead of EINTR, but I'd
1506 * like to be certain this leads to no problems. So I return
1507 * EINTR just for safety.
1509 * Update: ERESTARTSYS breaks at least the xview clock binary, so
1510 * I'm trying ERESTARTNOHAND which restart only when you want to.
1512 #define MAX_SELECT_SECONDS \
1513 ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1)
1515 int compat_core_sys_select(int n
, compat_ulong_t __user
*inp
,
1516 compat_ulong_t __user
*outp
, compat_ulong_t __user
*exp
, s64
*timeout
)
1520 int size
, max_fds
, ret
= -EINVAL
;
1521 struct fdtable
*fdt
;
1526 /* max_fds can increase, so grab it once to avoid race */
1528 fdt
= files_fdtable(current
->files
);
1529 max_fds
= fdt
->max_fds
;
1535 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
1536 * since we used fdset we need to allocate memory in units of
1540 size
= FDS_BYTES(n
);
1541 bits
= kmalloc(6 * size
, GFP_KERNEL
);
1544 fds
.in
= (unsigned long *) bits
;
1545 fds
.out
= (unsigned long *) (bits
+ size
);
1546 fds
.ex
= (unsigned long *) (bits
+ 2*size
);
1547 fds
.res_in
= (unsigned long *) (bits
+ 3*size
);
1548 fds
.res_out
= (unsigned long *) (bits
+ 4*size
);
1549 fds
.res_ex
= (unsigned long *) (bits
+ 5*size
);
1551 if ((ret
= compat_get_fd_set(n
, inp
, fds
.in
)) ||
1552 (ret
= compat_get_fd_set(n
, outp
, fds
.out
)) ||
1553 (ret
= compat_get_fd_set(n
, exp
, fds
.ex
)))
1555 zero_fd_set(n
, fds
.res_in
);
1556 zero_fd_set(n
, fds
.res_out
);
1557 zero_fd_set(n
, fds
.res_ex
);
1559 ret
= do_select(n
, &fds
, timeout
);
1564 ret
= -ERESTARTNOHAND
;
1565 if (signal_pending(current
))
1570 if (compat_set_fd_set(n
, inp
, fds
.res_in
) ||
1571 compat_set_fd_set(n
, outp
, fds
.res_out
) ||
1572 compat_set_fd_set(n
, exp
, fds
.res_ex
))
1580 asmlinkage
long compat_sys_select(int n
, compat_ulong_t __user
*inp
,
1581 compat_ulong_t __user
*outp
, compat_ulong_t __user
*exp
,
1582 struct compat_timeval __user
*tvp
)
1585 struct compat_timeval tv
;
1589 if (copy_from_user(&tv
, tvp
, sizeof(tv
)))
1592 if (tv
.tv_sec
< 0 || tv
.tv_usec
< 0)
1595 /* Cast to u64 to make GCC stop complaining */
1596 if ((u64
)tv
.tv_sec
>= (u64
)MAX_INT64_SECONDS
)
1597 timeout
= -1; /* infinite */
1599 timeout
= DIV_ROUND_UP(tv
.tv_usec
, 1000000/HZ
);
1600 timeout
+= tv
.tv_sec
* HZ
;
1604 ret
= compat_core_sys_select(n
, inp
, outp
, exp
, &timeout
);
1607 struct compat_timeval rtv
;
1609 if (current
->personality
& STICKY_TIMEOUTS
)
1611 rtv
.tv_usec
= jiffies_to_usecs(do_div((*(u64
*)&timeout
), HZ
));
1612 rtv
.tv_sec
= timeout
;
1613 if (compat_timeval_compare(&rtv
, &tv
) >= 0)
1615 if (copy_to_user(tvp
, &rtv
, sizeof(rtv
))) {
1618 * If an application puts its timeval in read-only
1619 * memory, we don't want the Linux-specific update to
1620 * the timeval to cause a fault after the select has
1621 * completed successfully. However, because we're not
1622 * updating the timeval, we can't restart the system
1625 if (ret
== -ERESTARTNOHAND
)
1633 #ifdef TIF_RESTORE_SIGMASK
1634 asmlinkage
long compat_sys_pselect7(int n
, compat_ulong_t __user
*inp
,
1635 compat_ulong_t __user
*outp
, compat_ulong_t __user
*exp
,
1636 struct compat_timespec __user
*tsp
, compat_sigset_t __user
*sigmask
,
1637 compat_size_t sigsetsize
)
1639 compat_sigset_t ss32
;
1640 sigset_t ksigmask
, sigsaved
;
1641 s64 timeout
= MAX_SCHEDULE_TIMEOUT
;
1642 struct compat_timespec ts
;
1646 if (copy_from_user(&ts
, tsp
, sizeof(ts
)))
1649 if (ts
.tv_sec
< 0 || ts
.tv_nsec
< 0)
1654 if (sigsetsize
!= sizeof(compat_sigset_t
))
1656 if (copy_from_user(&ss32
, sigmask
, sizeof(ss32
)))
1658 sigset_from_compat(&ksigmask
, &ss32
);
1660 sigdelsetmask(&ksigmask
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
1661 sigprocmask(SIG_SETMASK
, &ksigmask
, &sigsaved
);
1666 if ((unsigned long)ts
.tv_sec
< MAX_SELECT_SECONDS
) {
1667 timeout
= DIV_ROUND_UP(ts
.tv_nsec
, 1000000000/HZ
);
1668 timeout
+= ts
.tv_sec
* (unsigned long)HZ
;
1672 ts
.tv_sec
-= MAX_SELECT_SECONDS
;
1673 timeout
= MAX_SELECT_SECONDS
* HZ
;
1677 ret
= compat_core_sys_select(n
, inp
, outp
, exp
, &timeout
);
1679 } while (!ret
&& !timeout
&& tsp
&& (ts
.tv_sec
|| ts
.tv_nsec
));
1682 struct compat_timespec rts
;
1684 if (current
->personality
& STICKY_TIMEOUTS
)
1687 rts
.tv_sec
= timeout
/ HZ
;
1688 rts
.tv_nsec
= (timeout
% HZ
) * (NSEC_PER_SEC
/HZ
);
1689 if (rts
.tv_nsec
>= NSEC_PER_SEC
) {
1691 rts
.tv_nsec
-= NSEC_PER_SEC
;
1693 if (compat_timespec_compare(&rts
, &ts
) >= 0)
1695 if (copy_to_user(tsp
, &rts
, sizeof(rts
))) {
1698 * If an application puts its timeval in read-only
1699 * memory, we don't want the Linux-specific update to
1700 * the timeval to cause a fault after the select has
1701 * completed successfully. However, because we're not
1702 * updating the timeval, we can't restart the system
1705 if (ret
== -ERESTARTNOHAND
)
1710 if (ret
== -ERESTARTNOHAND
) {
1712 * Don't restore the signal mask yet. Let do_signal() deliver
1713 * the signal on the way back to userspace, before the signal
1717 memcpy(¤t
->saved_sigmask
, &sigsaved
,
1719 set_thread_flag(TIF_RESTORE_SIGMASK
);
1722 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
1727 asmlinkage
long compat_sys_pselect6(int n
, compat_ulong_t __user
*inp
,
1728 compat_ulong_t __user
*outp
, compat_ulong_t __user
*exp
,
1729 struct compat_timespec __user
*tsp
, void __user
*sig
)
1731 compat_size_t sigsetsize
= 0;
1732 compat_uptr_t up
= 0;
1735 if (!access_ok(VERIFY_READ
, sig
,
1736 sizeof(compat_uptr_t
)+sizeof(compat_size_t
)) ||
1737 __get_user(up
, (compat_uptr_t __user
*)sig
) ||
1738 __get_user(sigsetsize
,
1739 (compat_size_t __user
*)(sig
+sizeof(up
))))
1742 return compat_sys_pselect7(n
, inp
, outp
, exp
, tsp
, compat_ptr(up
),
1746 asmlinkage
long compat_sys_ppoll(struct pollfd __user
*ufds
,
1747 unsigned int nfds
, struct compat_timespec __user
*tsp
,
1748 const compat_sigset_t __user
*sigmask
, compat_size_t sigsetsize
)
1750 compat_sigset_t ss32
;
1751 sigset_t ksigmask
, sigsaved
;
1752 struct compat_timespec ts
;
1757 if (copy_from_user(&ts
, tsp
, sizeof(ts
)))
1760 /* We assume that ts.tv_sec is always lower than
1761 the number of seconds that can be expressed in
1762 an s64. Otherwise the compiler bitches at us */
1763 timeout
= DIV_ROUND_UP(ts
.tv_nsec
, 1000000000/HZ
);
1764 timeout
+= ts
.tv_sec
* HZ
;
1768 if (sigsetsize
!= sizeof(compat_sigset_t
))
1770 if (copy_from_user(&ss32
, sigmask
, sizeof(ss32
)))
1772 sigset_from_compat(&ksigmask
, &ss32
);
1774 sigdelsetmask(&ksigmask
, sigmask(SIGKILL
)|sigmask(SIGSTOP
));
1775 sigprocmask(SIG_SETMASK
, &ksigmask
, &sigsaved
);
1778 ret
= do_sys_poll(ufds
, nfds
, &timeout
);
1780 /* We can restart this syscall, usually */
1781 if (ret
== -EINTR
) {
1783 * Don't restore the signal mask yet. Let do_signal() deliver
1784 * the signal on the way back to userspace, before the signal
1788 memcpy(¤t
->saved_sigmask
, &sigsaved
,
1790 set_thread_flag(TIF_RESTORE_SIGMASK
);
1792 ret
= -ERESTARTNOHAND
;
1794 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
1796 if (tsp
&& timeout
>= 0) {
1797 struct compat_timespec rts
;
1799 if (current
->personality
& STICKY_TIMEOUTS
)
1801 /* Yes, we know it's actually an s64, but it's also positive. */
1802 rts
.tv_nsec
= jiffies_to_usecs(do_div((*(u64
*)&timeout
), HZ
)) *
1804 rts
.tv_sec
= timeout
;
1805 if (compat_timespec_compare(&rts
, &ts
) >= 0)
1807 if (copy_to_user(tsp
, &rts
, sizeof(rts
))) {
1810 * If an application puts its timeval in read-only
1811 * memory, we don't want the Linux-specific update to
1812 * the timeval to cause a fault after the select has
1813 * completed successfully. However, because we're not
1814 * updating the timeval, we can't restart the system
1817 if (ret
== -ERESTARTNOHAND
&& timeout
>= 0)
1824 #endif /* TIF_RESTORE_SIGMASK */
1826 #if defined(CONFIG_NFSD) || defined(CONFIG_NFSD_MODULE)
1827 /* Stuff for NFS server syscalls... */
1828 struct compat_nfsctl_svc
{
1833 struct compat_nfsctl_client
{
1834 s8 cl32_ident
[NFSCLNT_IDMAX
+1];
1836 struct in_addr cl32_addrlist
[NFSCLNT_ADDRMAX
];
1839 u8 cl32_fhkey
[NFSCLNT_KEYMAX
];
1842 struct compat_nfsctl_export
{
1843 char ex32_client
[NFSCLNT_IDMAX
+1];
1844 char ex32_path
[NFS_MAXPATHLEN
+1];
1845 compat_dev_t ex32_dev
;
1846 compat_ino_t ex32_ino
;
1847 compat_int_t ex32_flags
;
1848 __compat_uid_t ex32_anon_uid
;
1849 __compat_gid_t ex32_anon_gid
;
1852 struct compat_nfsctl_fdparm
{
1853 struct sockaddr gd32_addr
;
1854 s8 gd32_path
[NFS_MAXPATHLEN
+1];
1855 compat_int_t gd32_version
;
1858 struct compat_nfsctl_fsparm
{
1859 struct sockaddr gd32_addr
;
1860 s8 gd32_path
[NFS_MAXPATHLEN
+1];
1861 compat_int_t gd32_maxlen
;
1864 struct compat_nfsctl_arg
{
1865 compat_int_t ca32_version
; /* safeguard */
1867 struct compat_nfsctl_svc u32_svc
;
1868 struct compat_nfsctl_client u32_client
;
1869 struct compat_nfsctl_export u32_export
;
1870 struct compat_nfsctl_fdparm u32_getfd
;
1871 struct compat_nfsctl_fsparm u32_getfs
;
1873 #define ca32_svc u.u32_svc
1874 #define ca32_client u.u32_client
1875 #define ca32_export u.u32_export
1876 #define ca32_getfd u.u32_getfd
1877 #define ca32_getfs u.u32_getfs
1880 union compat_nfsctl_res
{
1881 __u8 cr32_getfh
[NFS_FHSIZE
];
1882 struct knfsd_fh cr32_getfs
;
1885 static int compat_nfs_svc_trans(struct nfsctl_arg
*karg
,
1886 struct compat_nfsctl_arg __user
*arg
)
1888 if (!access_ok(VERIFY_READ
, &arg
->ca32_svc
, sizeof(arg
->ca32_svc
)) ||
1889 get_user(karg
->ca_version
, &arg
->ca32_version
) ||
1890 __get_user(karg
->ca_svc
.svc_port
, &arg
->ca32_svc
.svc32_port
) ||
1891 __get_user(karg
->ca_svc
.svc_nthreads
,
1892 &arg
->ca32_svc
.svc32_nthreads
))
1897 static int compat_nfs_clnt_trans(struct nfsctl_arg
*karg
,
1898 struct compat_nfsctl_arg __user
*arg
)
1900 if (!access_ok(VERIFY_READ
, &arg
->ca32_client
,
1901 sizeof(arg
->ca32_client
)) ||
1902 get_user(karg
->ca_version
, &arg
->ca32_version
) ||
1903 __copy_from_user(&karg
->ca_client
.cl_ident
[0],
1904 &arg
->ca32_client
.cl32_ident
[0],
1906 __get_user(karg
->ca_client
.cl_naddr
,
1907 &arg
->ca32_client
.cl32_naddr
) ||
1908 __copy_from_user(&karg
->ca_client
.cl_addrlist
[0],
1909 &arg
->ca32_client
.cl32_addrlist
[0],
1910 (sizeof(struct in_addr
) * NFSCLNT_ADDRMAX
)) ||
1911 __get_user(karg
->ca_client
.cl_fhkeytype
,
1912 &arg
->ca32_client
.cl32_fhkeytype
) ||
1913 __get_user(karg
->ca_client
.cl_fhkeylen
,
1914 &arg
->ca32_client
.cl32_fhkeylen
) ||
1915 __copy_from_user(&karg
->ca_client
.cl_fhkey
[0],
1916 &arg
->ca32_client
.cl32_fhkey
[0],
1923 static int compat_nfs_exp_trans(struct nfsctl_arg
*karg
,
1924 struct compat_nfsctl_arg __user
*arg
)
1926 if (!access_ok(VERIFY_READ
, &arg
->ca32_export
,
1927 sizeof(arg
->ca32_export
)) ||
1928 get_user(karg
->ca_version
, &arg
->ca32_version
) ||
1929 __copy_from_user(&karg
->ca_export
.ex_client
[0],
1930 &arg
->ca32_export
.ex32_client
[0],
1932 __copy_from_user(&karg
->ca_export
.ex_path
[0],
1933 &arg
->ca32_export
.ex32_path
[0],
1935 __get_user(karg
->ca_export
.ex_dev
,
1936 &arg
->ca32_export
.ex32_dev
) ||
1937 __get_user(karg
->ca_export
.ex_ino
,
1938 &arg
->ca32_export
.ex32_ino
) ||
1939 __get_user(karg
->ca_export
.ex_flags
,
1940 &arg
->ca32_export
.ex32_flags
) ||
1941 __get_user(karg
->ca_export
.ex_anon_uid
,
1942 &arg
->ca32_export
.ex32_anon_uid
) ||
1943 __get_user(karg
->ca_export
.ex_anon_gid
,
1944 &arg
->ca32_export
.ex32_anon_gid
))
1946 SET_UID(karg
->ca_export
.ex_anon_uid
, karg
->ca_export
.ex_anon_uid
);
1947 SET_GID(karg
->ca_export
.ex_anon_gid
, karg
->ca_export
.ex_anon_gid
);
1952 static int compat_nfs_getfd_trans(struct nfsctl_arg
*karg
,
1953 struct compat_nfsctl_arg __user
*arg
)
1955 if (!access_ok(VERIFY_READ
, &arg
->ca32_getfd
,
1956 sizeof(arg
->ca32_getfd
)) ||
1957 get_user(karg
->ca_version
, &arg
->ca32_version
) ||
1958 __copy_from_user(&karg
->ca_getfd
.gd_addr
,
1959 &arg
->ca32_getfd
.gd32_addr
,
1960 (sizeof(struct sockaddr
))) ||
1961 __copy_from_user(&karg
->ca_getfd
.gd_path
,
1962 &arg
->ca32_getfd
.gd32_path
,
1963 (NFS_MAXPATHLEN
+1)) ||
1964 __get_user(karg
->ca_getfd
.gd_version
,
1965 &arg
->ca32_getfd
.gd32_version
))
1971 static int compat_nfs_getfs_trans(struct nfsctl_arg
*karg
,
1972 struct compat_nfsctl_arg __user
*arg
)
1974 if (!access_ok(VERIFY_READ
,&arg
->ca32_getfs
,sizeof(arg
->ca32_getfs
)) ||
1975 get_user(karg
->ca_version
, &arg
->ca32_version
) ||
1976 __copy_from_user(&karg
->ca_getfs
.gd_addr
,
1977 &arg
->ca32_getfs
.gd32_addr
,
1978 (sizeof(struct sockaddr
))) ||
1979 __copy_from_user(&karg
->ca_getfs
.gd_path
,
1980 &arg
->ca32_getfs
.gd32_path
,
1981 (NFS_MAXPATHLEN
+1)) ||
1982 __get_user(karg
->ca_getfs
.gd_maxlen
,
1983 &arg
->ca32_getfs
.gd32_maxlen
))
1989 /* This really doesn't need translations, we are only passing
1990 * back a union which contains opaque nfs file handle data.
1992 static int compat_nfs_getfh_res_trans(union nfsctl_res
*kres
,
1993 union compat_nfsctl_res __user
*res
)
1997 err
= copy_to_user(res
, kres
, sizeof(*res
));
1999 return (err
) ? -EFAULT
: 0;
2002 asmlinkage
long compat_sys_nfsservctl(int cmd
,
2003 struct compat_nfsctl_arg __user
*arg
,
2004 union compat_nfsctl_res __user
*res
)
2006 struct nfsctl_arg
*karg
;
2007 union nfsctl_res
*kres
;
2011 karg
= kmalloc(sizeof(*karg
), GFP_USER
);
2012 kres
= kmalloc(sizeof(*kres
), GFP_USER
);
2013 if(!karg
|| !kres
) {
2020 err
= compat_nfs_svc_trans(karg
, arg
);
2023 case NFSCTL_ADDCLIENT
:
2024 err
= compat_nfs_clnt_trans(karg
, arg
);
2027 case NFSCTL_DELCLIENT
:
2028 err
= compat_nfs_clnt_trans(karg
, arg
);
2032 case NFSCTL_UNEXPORT
:
2033 err
= compat_nfs_exp_trans(karg
, arg
);
2037 err
= compat_nfs_getfd_trans(karg
, arg
);
2041 err
= compat_nfs_getfs_trans(karg
, arg
);
2054 /* The __user pointer casts are valid because of the set_fs() */
2055 err
= sys_nfsservctl(cmd
, (void __user
*) karg
, (void __user
*) kres
);
2061 if((cmd
== NFSCTL_GETFD
) ||
2062 (cmd
== NFSCTL_GETFS
))
2063 err
= compat_nfs_getfh_res_trans(kres
, res
);
2071 long asmlinkage
compat_sys_nfsservctl(int cmd
, void *notused
, void *notused2
)
2073 return sys_ni_syscall();
2079 #ifdef CONFIG_HAS_COMPAT_EPOLL_EVENT
2080 asmlinkage
long compat_sys_epoll_ctl(int epfd
, int op
, int fd
,
2081 struct compat_epoll_event __user
*event
)
2084 struct compat_epoll_event user
;
2085 struct epoll_event __user
*kernel
= NULL
;
2088 if (copy_from_user(&user
, event
, sizeof(user
)))
2090 kernel
= compat_alloc_user_space(sizeof(struct epoll_event
));
2091 err
|= __put_user(user
.events
, &kernel
->events
);
2092 err
|= __put_user(user
.data
, &kernel
->data
);
2095 return err
? err
: sys_epoll_ctl(epfd
, op
, fd
, kernel
);
2099 asmlinkage
long compat_sys_epoll_wait(int epfd
,
2100 struct compat_epoll_event __user
*events
,
2101 int maxevents
, int timeout
)
2103 long i
, ret
, err
= 0;
2104 struct epoll_event __user
*kbuf
;
2105 struct epoll_event ev
;
2107 if ((maxevents
<= 0) ||
2108 (maxevents
> (INT_MAX
/ sizeof(struct epoll_event
))))
2110 kbuf
= compat_alloc_user_space(sizeof(struct epoll_event
) * maxevents
);
2111 ret
= sys_epoll_wait(epfd
, kbuf
, maxevents
, timeout
);
2112 for (i
= 0; i
< ret
; i
++) {
2113 err
|= __get_user(ev
.events
, &kbuf
[i
].events
);
2114 err
|= __get_user(ev
.data
, &kbuf
[i
].data
);
2115 err
|= __put_user(ev
.events
, &events
->events
);
2116 err
|= __put_user_unaligned(ev
.data
, &events
->data
);
2120 return err
? -EFAULT
: ret
;
2122 #endif /* CONFIG_HAS_COMPAT_EPOLL_EVENT */
2124 #ifdef TIF_RESTORE_SIGMASK
2125 asmlinkage
long compat_sys_epoll_pwait(int epfd
,
2126 struct compat_epoll_event __user
*events
,
2127 int maxevents
, int timeout
,
2128 const compat_sigset_t __user
*sigmask
,
2129 compat_size_t sigsetsize
)
2132 compat_sigset_t csigmask
;
2133 sigset_t ksigmask
, sigsaved
;
2136 * If the caller wants a certain signal mask to be set during the wait,
2140 if (sigsetsize
!= sizeof(compat_sigset_t
))
2142 if (copy_from_user(&csigmask
, sigmask
, sizeof(csigmask
)))
2144 sigset_from_compat(&ksigmask
, &csigmask
);
2145 sigdelsetmask(&ksigmask
, sigmask(SIGKILL
) | sigmask(SIGSTOP
));
2146 sigprocmask(SIG_SETMASK
, &ksigmask
, &sigsaved
);
2149 #ifdef CONFIG_HAS_COMPAT_EPOLL_EVENT
2150 err
= compat_sys_epoll_wait(epfd
, events
, maxevents
, timeout
);
2152 err
= sys_epoll_wait(epfd
, events
, maxevents
, timeout
);
2156 * If we changed the signal mask, we need to restore the original one.
2157 * In case we've got a signal while waiting, we do not restore the
2158 * signal mask yet, and we allow do_signal() to deliver the signal on
2159 * the way back to userspace, before the signal mask is restored.
2162 if (err
== -EINTR
) {
2163 memcpy(¤t
->saved_sigmask
, &sigsaved
,
2165 set_thread_flag(TIF_RESTORE_SIGMASK
);
2167 sigprocmask(SIG_SETMASK
, &sigsaved
, NULL
);
2172 #endif /* TIF_RESTORE_SIGMASK */
2174 #endif /* CONFIG_EPOLL */