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@ucw.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/stddef.h>
19 #include <linux/kernel.h>
20 #include <linux/linkage.h>
21 #include <linux/compat.h>
22 #include <linux/errno.h>
23 #include <linux/time.h>
24 #include <linux/cred.h>
26 #include <linux/fcntl.h>
27 #include <linux/namei.h>
28 #include <linux/file.h>
29 #include <linux/fdtable.h>
30 #include <linux/vfs.h>
31 #include <linux/ioctl.h>
32 #include <linux/init.h>
33 #include <linux/ncp_mount.h>
34 #include <linux/nfs4_mount.h>
35 #include <linux/syscalls.h>
36 #include <linux/ctype.h>
37 #include <linux/dirent.h>
38 #include <linux/fsnotify.h>
39 #include <linux/highuid.h>
40 #include <linux/personality.h>
41 #include <linux/rwsem.h>
42 #include <linux/tsacct_kern.h>
43 #include <linux/security.h>
44 #include <linux/highmem.h>
45 #include <linux/signal.h>
46 #include <linux/poll.h>
48 #include <linux/fs_struct.h>
49 #include <linux/slab.h>
50 #include <linux/pagemap.h>
51 #include <linux/aio.h>
53 #include <linux/uaccess.h>
54 #include <asm/mmu_context.h>
55 #include <asm/ioctls.h>
59 * Not all architectures have sys_utime, so implement this in terms
62 COMPAT_SYSCALL_DEFINE2(utime, const char __user *, filename,
63 struct compat_utimbuf __user *, t)
65 struct timespec tv[2];
68 if (get_user(tv[0].tv_sec, &t->actime) ||
69 get_user(tv[1].tv_sec, &t->modtime))
74 return do_utimes(AT_FDCWD, filename, t ? tv : NULL, 0);
77 COMPAT_SYSCALL_DEFINE4(utimensat, unsigned int, dfd, const char __user *, filename, struct compat_timespec __user *, t, int, flags)
79 struct timespec tv[2];
82 if (compat_get_timespec(&tv[0], &t[0]) ||
83 compat_get_timespec(&tv[1], &t[1]))
86 if (tv[0].tv_nsec == UTIME_OMIT && tv[1].tv_nsec == UTIME_OMIT)
89 return do_utimes(dfd, filename, t ? tv : NULL, flags);
92 COMPAT_SYSCALL_DEFINE3(futimesat, unsigned int, dfd, const char __user *, filename, struct compat_timeval __user *, t)
94 struct timespec tv[2];
97 if (get_user(tv[0].tv_sec, &t[0].tv_sec) ||
98 get_user(tv[0].tv_nsec, &t[0].tv_usec) ||
99 get_user(tv[1].tv_sec, &t[1].tv_sec) ||
100 get_user(tv[1].tv_nsec, &t[1].tv_usec))
102 if (tv[0].tv_nsec >= 1000000 || tv[0].tv_nsec < 0 ||
103 tv[1].tv_nsec >= 1000000 || tv[1].tv_nsec < 0)
105 tv[0].tv_nsec *= 1000;
106 tv[1].tv_nsec *= 1000;
108 return do_utimes(dfd, filename, t ? tv : NULL, 0);
111 COMPAT_SYSCALL_DEFINE2(utimes, const char __user *, filename, struct compat_timeval __user *, t)
113 return compat_sys_futimesat(AT_FDCWD, filename, t);
116 static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf)
118 struct compat_stat tmp;
120 if (!old_valid_dev(stat->dev) || !old_valid_dev(stat->rdev))
123 memset(&tmp, 0, sizeof(tmp));
124 tmp.st_dev = old_encode_dev(stat->dev);
125 tmp.st_ino = stat->ino;
126 if (sizeof(tmp.st_ino) < sizeof(stat->ino) && tmp.st_ino != stat->ino)
128 tmp.st_mode = stat->mode;
129 tmp.st_nlink = stat->nlink;
130 if (tmp.st_nlink != stat->nlink)
132 SET_UID(tmp.st_uid, from_kuid_munged(current_user_ns(), stat->uid));
133 SET_GID(tmp.st_gid, from_kgid_munged(current_user_ns(), stat->gid));
134 tmp.st_rdev = old_encode_dev(stat->rdev);
135 if ((u64) stat->size > MAX_NON_LFS)
137 tmp.st_size = stat->size;
138 tmp.st_atime = stat->atime.tv_sec;
139 tmp.st_atime_nsec = stat->atime.tv_nsec;
140 tmp.st_mtime = stat->mtime.tv_sec;
141 tmp.st_mtime_nsec = stat->mtime.tv_nsec;
142 tmp.st_ctime = stat->ctime.tv_sec;
143 tmp.st_ctime_nsec = stat->ctime.tv_nsec;
144 tmp.st_blocks = stat->blocks;
145 tmp.st_blksize = stat->blksize;
146 return copy_to_user(ubuf, &tmp, sizeof(tmp)) ? -EFAULT : 0;
149 COMPAT_SYSCALL_DEFINE2(newstat, const char __user *, filename,
150 struct compat_stat __user *, statbuf)
155 error = vfs_stat(filename, &stat);
158 return cp_compat_stat(&stat, statbuf);
161 COMPAT_SYSCALL_DEFINE2(newlstat, const char __user *, filename,
162 struct compat_stat __user *, statbuf)
167 error = vfs_lstat(filename, &stat);
170 return cp_compat_stat(&stat, statbuf);
173 #ifndef __ARCH_WANT_STAT64
174 COMPAT_SYSCALL_DEFINE4(newfstatat, unsigned int, dfd,
175 const char __user *, filename,
176 struct compat_stat __user *, statbuf, int, flag)
181 error = vfs_fstatat(dfd, filename, &stat, flag);
184 return cp_compat_stat(&stat, statbuf);
188 COMPAT_SYSCALL_DEFINE2(newfstat, unsigned int, fd,
189 struct compat_stat __user *, statbuf)
192 int error = vfs_fstat(fd, &stat);
195 error = cp_compat_stat(&stat, statbuf);
199 static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf)
202 if (sizeof ubuf->f_blocks == 4) {
203 if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail |
204 kbuf->f_bsize | kbuf->f_frsize) & 0xffffffff00000000ULL)
206 /* f_files and f_ffree may be -1; it's okay
207 * to stuff that into 32 bits */
208 if (kbuf->f_files != 0xffffffffffffffffULL
209 && (kbuf->f_files & 0xffffffff00000000ULL))
211 if (kbuf->f_ffree != 0xffffffffffffffffULL
212 && (kbuf->f_ffree & 0xffffffff00000000ULL))
215 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
216 __put_user(kbuf->f_type, &ubuf->f_type) ||
217 __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
218 __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
219 __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
220 __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
221 __put_user(kbuf->f_files, &ubuf->f_files) ||
222 __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
223 __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
224 __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
225 __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
226 __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
227 __put_user(kbuf->f_flags, &ubuf->f_flags) ||
228 __clear_user(ubuf->f_spare, sizeof(ubuf->f_spare)))
234 * The following statfs calls are copies of code from fs/statfs.c and
235 * should be checked against those from time to time
237 COMPAT_SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct compat_statfs __user *, buf)
240 int error = user_statfs(pathname, &tmp);
242 error = put_compat_statfs(buf, &tmp);
246 COMPAT_SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct compat_statfs __user *, buf)
249 int error = fd_statfs(fd, &tmp);
251 error = put_compat_statfs(buf, &tmp);
255 static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf)
257 if (sizeof(ubuf->f_bsize) == 4) {
258 if ((kbuf->f_type | kbuf->f_bsize | kbuf->f_namelen |
259 kbuf->f_frsize | kbuf->f_flags) & 0xffffffff00000000ULL)
261 /* f_files and f_ffree may be -1; it's okay
262 * to stuff that into 32 bits */
263 if (kbuf->f_files != 0xffffffffffffffffULL
264 && (kbuf->f_files & 0xffffffff00000000ULL))
266 if (kbuf->f_ffree != 0xffffffffffffffffULL
267 && (kbuf->f_ffree & 0xffffffff00000000ULL))
270 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) ||
271 __put_user(kbuf->f_type, &ubuf->f_type) ||
272 __put_user(kbuf->f_bsize, &ubuf->f_bsize) ||
273 __put_user(kbuf->f_blocks, &ubuf->f_blocks) ||
274 __put_user(kbuf->f_bfree, &ubuf->f_bfree) ||
275 __put_user(kbuf->f_bavail, &ubuf->f_bavail) ||
276 __put_user(kbuf->f_files, &ubuf->f_files) ||
277 __put_user(kbuf->f_ffree, &ubuf->f_ffree) ||
278 __put_user(kbuf->f_namelen, &ubuf->f_namelen) ||
279 __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) ||
280 __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) ||
281 __put_user(kbuf->f_frsize, &ubuf->f_frsize) ||
282 __put_user(kbuf->f_flags, &ubuf->f_flags) ||
283 __clear_user(ubuf->f_spare, sizeof(ubuf->f_spare)))
288 COMPAT_SYSCALL_DEFINE3(statfs64, const char __user *, pathname, compat_size_t, sz, struct compat_statfs64 __user *, buf)
293 if (sz != sizeof(*buf))
296 error = user_statfs(pathname, &tmp);
298 error = put_compat_statfs64(buf, &tmp);
302 COMPAT_SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, compat_size_t, sz, struct compat_statfs64 __user *, buf)
307 if (sz != sizeof(*buf))
310 error = fd_statfs(fd, &tmp);
312 error = put_compat_statfs64(buf, &tmp);
317 * This is a copy of sys_ustat, just dealing with a structure layout.
318 * Given how simple this syscall is that apporach is more maintainable
319 * than the various conversion hacks.
321 COMPAT_SYSCALL_DEFINE2(ustat, unsigned, dev, struct compat_ustat __user *, u)
323 struct compat_ustat tmp;
325 int err = vfs_ustat(new_decode_dev(dev), &sbuf);
329 memset(&tmp, 0, sizeof(struct compat_ustat));
330 tmp.f_tfree = sbuf.f_bfree;
331 tmp.f_tinode = sbuf.f_ffree;
332 if (copy_to_user(u, &tmp, sizeof(struct compat_ustat)))
337 static int get_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
339 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
340 __get_user(kfl->l_type, &ufl->l_type) ||
341 __get_user(kfl->l_whence, &ufl->l_whence) ||
342 __get_user(kfl->l_start, &ufl->l_start) ||
343 __get_user(kfl->l_len, &ufl->l_len) ||
344 __get_user(kfl->l_pid, &ufl->l_pid))
349 static int put_compat_flock(struct flock *kfl, struct compat_flock __user *ufl)
351 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
352 __put_user(kfl->l_type, &ufl->l_type) ||
353 __put_user(kfl->l_whence, &ufl->l_whence) ||
354 __put_user(kfl->l_start, &ufl->l_start) ||
355 __put_user(kfl->l_len, &ufl->l_len) ||
356 __put_user(kfl->l_pid, &ufl->l_pid))
361 #ifndef HAVE_ARCH_GET_COMPAT_FLOCK64
362 static int get_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
364 if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) ||
365 __get_user(kfl->l_type, &ufl->l_type) ||
366 __get_user(kfl->l_whence, &ufl->l_whence) ||
367 __get_user(kfl->l_start, &ufl->l_start) ||
368 __get_user(kfl->l_len, &ufl->l_len) ||
369 __get_user(kfl->l_pid, &ufl->l_pid))
375 #ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64
376 static int put_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl)
378 if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) ||
379 __put_user(kfl->l_type, &ufl->l_type) ||
380 __put_user(kfl->l_whence, &ufl->l_whence) ||
381 __put_user(kfl->l_start, &ufl->l_start) ||
382 __put_user(kfl->l_len, &ufl->l_len) ||
383 __put_user(kfl->l_pid, &ufl->l_pid))
390 convert_fcntl_cmd(unsigned int cmd)
404 COMPAT_SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd,
410 unsigned int conv_cmd;
416 ret = get_compat_flock(&f, compat_ptr(arg));
421 ret = sys_fcntl(fd, cmd, (unsigned long)&f);
423 if (cmd == F_GETLK && ret == 0) {
424 /* GETLK was successful and we need to return the data...
425 * but it needs to fit in the compat structure.
426 * l_start shouldn't be too big, unless the original
427 * start + end is greater than COMPAT_OFF_T_MAX, in which
428 * case the app was asking for trouble, so we return
429 * -EOVERFLOW in that case.
430 * l_len could be too big, in which case we just truncate it,
431 * and only allow the app to see that part of the conflicting
432 * lock that might make sense to it anyway
435 if (f.l_start > COMPAT_OFF_T_MAX)
437 if (f.l_len > COMPAT_OFF_T_MAX)
438 f.l_len = COMPAT_OFF_T_MAX;
440 ret = put_compat_flock(&f, compat_ptr(arg));
450 ret = get_compat_flock64(&f, compat_ptr(arg));
455 conv_cmd = convert_fcntl_cmd(cmd);
456 ret = sys_fcntl(fd, conv_cmd, (unsigned long)&f);
458 if ((conv_cmd == F_GETLK || conv_cmd == F_OFD_GETLK) && ret == 0) {
459 /* need to return lock information - see above for commentary */
460 if (f.l_start > COMPAT_LOFF_T_MAX)
462 if (f.l_len > COMPAT_LOFF_T_MAX)
463 f.l_len = COMPAT_LOFF_T_MAX;
465 ret = put_compat_flock64(&f, compat_ptr(arg));
470 ret = sys_fcntl(fd, cmd, arg);
476 COMPAT_SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd,
488 return compat_sys_fcntl64(fd, cmd, arg);
491 /* A write operation does a read from user space and vice versa */
492 #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ)
494 ssize_t compat_rw_copy_check_uvector(int type,
495 const struct compat_iovec __user *uvector, unsigned long nr_segs,
496 unsigned long fast_segs, struct iovec *fast_pointer,
497 struct iovec **ret_pointer)
499 compat_ssize_t tot_len;
500 struct iovec *iov = *ret_pointer = fast_pointer;
505 * SuS says "The readv() function *may* fail if the iovcnt argument
506 * was less than or equal to 0, or greater than {IOV_MAX}. Linux has
507 * traditionally returned zero for zero segments, so...
513 if (nr_segs > UIO_MAXIOV)
515 if (nr_segs > fast_segs) {
517 iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL);
524 if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector)))
528 * Single unix specification:
529 * We should -EINVAL if an element length is not >= 0 and fitting an
532 * In Linux, the total length is limited to MAX_RW_COUNT, there is
533 * no overflow possibility.
537 for (seg = 0; seg < nr_segs; seg++) {
541 if (__get_user(len, &uvector->iov_len) ||
542 __get_user(buf, &uvector->iov_base)) {
546 if (len < 0) /* size_t not fitting in compat_ssize_t .. */
549 !access_ok(vrfy_dir(type), compat_ptr(buf), len)) {
553 if (len > MAX_RW_COUNT - tot_len)
554 len = MAX_RW_COUNT - tot_len;
556 iov->iov_base = compat_ptr(buf);
557 iov->iov_len = (compat_size_t) len;
567 struct compat_ncp_mount_data {
568 compat_int_t version;
569 compat_uint_t ncp_fd;
570 __compat_uid_t mounted_uid;
571 compat_pid_t wdog_pid;
572 unsigned char mounted_vol[NCP_VOLNAME_LEN + 1];
573 compat_uint_t time_out;
574 compat_uint_t retry_count;
578 compat_mode_t file_mode;
579 compat_mode_t dir_mode;
582 struct compat_ncp_mount_data_v4 {
583 compat_int_t version;
584 compat_ulong_t flags;
585 compat_ulong_t mounted_uid;
586 compat_long_t wdog_pid;
587 compat_uint_t ncp_fd;
588 compat_uint_t time_out;
589 compat_uint_t retry_count;
592 compat_ulong_t file_mode;
593 compat_ulong_t dir_mode;
596 static void *do_ncp_super_data_conv(void *raw_data)
598 int version = *(unsigned int *)raw_data;
601 struct compat_ncp_mount_data *c_n = raw_data;
602 struct ncp_mount_data *n = raw_data;
604 n->dir_mode = c_n->dir_mode;
605 n->file_mode = c_n->file_mode;
608 memmove (n->mounted_vol, c_n->mounted_vol, (sizeof (c_n->mounted_vol) + 3 * sizeof (unsigned int)));
609 n->wdog_pid = c_n->wdog_pid;
610 n->mounted_uid = c_n->mounted_uid;
611 } else if (version == 4) {
612 struct compat_ncp_mount_data_v4 *c_n = raw_data;
613 struct ncp_mount_data_v4 *n = raw_data;
615 n->dir_mode = c_n->dir_mode;
616 n->file_mode = c_n->file_mode;
619 n->retry_count = c_n->retry_count;
620 n->time_out = c_n->time_out;
621 n->ncp_fd = c_n->ncp_fd;
622 n->wdog_pid = c_n->wdog_pid;
623 n->mounted_uid = c_n->mounted_uid;
624 n->flags = c_n->flags;
625 } else if (version != 5) {
633 struct compat_nfs_string {
638 static inline void compat_nfs_string(struct nfs_string *dst,
639 struct compat_nfs_string *src)
641 dst->data = compat_ptr(src->data);
645 struct compat_nfs4_mount_data_v1 {
646 compat_int_t version;
651 compat_int_t retrans;
652 compat_int_t acregmin;
653 compat_int_t acregmax;
654 compat_int_t acdirmin;
655 compat_int_t acdirmax;
656 struct compat_nfs_string client_addr;
657 struct compat_nfs_string mnt_path;
658 struct compat_nfs_string hostname;
659 compat_uint_t host_addrlen;
660 compat_uptr_t host_addr;
662 compat_int_t auth_flavourlen;
663 compat_uptr_t auth_flavours;
666 static int do_nfs4_super_data_conv(void *raw_data)
668 int version = *(compat_uint_t *) raw_data;
671 struct compat_nfs4_mount_data_v1 *raw = raw_data;
672 struct nfs4_mount_data *real = raw_data;
674 /* copy the fields backwards */
675 real->auth_flavours = compat_ptr(raw->auth_flavours);
676 real->auth_flavourlen = raw->auth_flavourlen;
677 real->proto = raw->proto;
678 real->host_addr = compat_ptr(raw->host_addr);
679 real->host_addrlen = raw->host_addrlen;
680 compat_nfs_string(&real->hostname, &raw->hostname);
681 compat_nfs_string(&real->mnt_path, &raw->mnt_path);
682 compat_nfs_string(&real->client_addr, &raw->client_addr);
683 real->acdirmax = raw->acdirmax;
684 real->acdirmin = raw->acdirmin;
685 real->acregmax = raw->acregmax;
686 real->acregmin = raw->acregmin;
687 real->retrans = raw->retrans;
688 real->timeo = raw->timeo;
689 real->wsize = raw->wsize;
690 real->rsize = raw->rsize;
691 real->flags = raw->flags;
692 real->version = raw->version;
698 #define NCPFS_NAME "ncpfs"
699 #define NFS4_NAME "nfs4"
701 COMPAT_SYSCALL_DEFINE5(mount, const char __user *, dev_name,
702 const char __user *, dir_name,
703 const char __user *, type, compat_ulong_t, flags,
704 const void __user *, data)
711 kernel_type = copy_mount_string(type);
712 retval = PTR_ERR(kernel_type);
713 if (IS_ERR(kernel_type))
716 kernel_dev = copy_mount_string(dev_name);
717 retval = PTR_ERR(kernel_dev);
718 if (IS_ERR(kernel_dev))
721 options = copy_mount_options(data);
722 retval = PTR_ERR(options);
726 if (kernel_type && options) {
727 if (!strcmp(kernel_type, NCPFS_NAME)) {
728 do_ncp_super_data_conv(options);
729 } else if (!strcmp(kernel_type, NFS4_NAME)) {
731 if (do_nfs4_super_data_conv(options))
736 retval = do_mount(kernel_dev, dir_name, kernel_type, flags, options);
748 struct compat_old_linux_dirent {
749 compat_ulong_t d_ino;
750 compat_ulong_t d_offset;
751 unsigned short d_namlen;
755 struct compat_readdir_callback {
756 struct dir_context ctx;
757 struct compat_old_linux_dirent __user *dirent;
761 static int compat_fillonedir(struct dir_context *ctx, const char *name,
762 int namlen, loff_t offset, u64 ino,
765 struct compat_readdir_callback *buf =
766 container_of(ctx, struct compat_readdir_callback, ctx);
767 struct compat_old_linux_dirent __user *dirent;
768 compat_ulong_t d_ino;
773 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
774 buf->result = -EOVERFLOW;
778 dirent = buf->dirent;
779 if (!access_ok(VERIFY_WRITE, dirent,
780 (unsigned long)(dirent->d_name + namlen + 1) -
781 (unsigned long)dirent))
783 if ( __put_user(d_ino, &dirent->d_ino) ||
784 __put_user(offset, &dirent->d_offset) ||
785 __put_user(namlen, &dirent->d_namlen) ||
786 __copy_to_user(dirent->d_name, name, namlen) ||
787 __put_user(0, dirent->d_name + namlen))
791 buf->result = -EFAULT;
795 COMPAT_SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
796 struct compat_old_linux_dirent __user *, dirent, unsigned int, count)
799 struct fd f = fdget_pos(fd);
800 struct compat_readdir_callback buf = {
801 .ctx.actor = compat_fillonedir,
808 error = iterate_dir(f.file, &buf.ctx);
816 struct compat_linux_dirent {
817 compat_ulong_t d_ino;
818 compat_ulong_t d_off;
819 unsigned short d_reclen;
823 struct compat_getdents_callback {
824 struct dir_context ctx;
825 struct compat_linux_dirent __user *current_dir;
826 struct compat_linux_dirent __user *previous;
831 static int compat_filldir(struct dir_context *ctx, const char *name, int namlen,
832 loff_t offset, u64 ino, unsigned int d_type)
834 struct compat_linux_dirent __user * dirent;
835 struct compat_getdents_callback *buf =
836 container_of(ctx, struct compat_getdents_callback, ctx);
837 compat_ulong_t d_ino;
838 int reclen = ALIGN(offsetof(struct compat_linux_dirent, d_name) +
839 namlen + 2, sizeof(compat_long_t));
841 buf->error = -EINVAL; /* only used if we fail.. */
842 if (reclen > buf->count)
845 if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) {
846 buf->error = -EOVERFLOW;
849 dirent = buf->previous;
851 if (signal_pending(current))
853 if (__put_user(offset, &dirent->d_off))
856 dirent = buf->current_dir;
857 if (__put_user(d_ino, &dirent->d_ino))
859 if (__put_user(reclen, &dirent->d_reclen))
861 if (copy_to_user(dirent->d_name, name, namlen))
863 if (__put_user(0, dirent->d_name + namlen))
865 if (__put_user(d_type, (char __user *) dirent + reclen - 1))
867 buf->previous = dirent;
868 dirent = (void __user *)dirent + reclen;
869 buf->current_dir = dirent;
870 buf->count -= reclen;
873 buf->error = -EFAULT;
877 COMPAT_SYSCALL_DEFINE3(getdents, unsigned int, fd,
878 struct compat_linux_dirent __user *, dirent, unsigned int, count)
881 struct compat_linux_dirent __user * lastdirent;
882 struct compat_getdents_callback buf = {
883 .ctx.actor = compat_filldir,
884 .current_dir = dirent,
889 if (!access_ok(VERIFY_WRITE, dirent, count))
896 error = iterate_dir(f.file, &buf.ctx);
899 lastdirent = buf.previous;
901 if (put_user(buf.ctx.pos, &lastdirent->d_off))
904 error = count - buf.count;
910 #ifdef __ARCH_WANT_COMPAT_SYS_GETDENTS64
912 struct compat_getdents_callback64 {
913 struct dir_context ctx;
914 struct linux_dirent64 __user *current_dir;
915 struct linux_dirent64 __user *previous;
920 static int compat_filldir64(struct dir_context *ctx, const char *name,
921 int namlen, loff_t offset, u64 ino,
924 struct linux_dirent64 __user *dirent;
925 struct compat_getdents_callback64 *buf =
926 container_of(ctx, struct compat_getdents_callback64, ctx);
927 int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1,
931 buf->error = -EINVAL; /* only used if we fail.. */
932 if (reclen > buf->count)
934 dirent = buf->previous;
937 if (signal_pending(current))
939 if (__put_user_unaligned(offset, &dirent->d_off))
942 dirent = buf->current_dir;
943 if (__put_user_unaligned(ino, &dirent->d_ino))
946 if (__put_user_unaligned(off, &dirent->d_off))
948 if (__put_user(reclen, &dirent->d_reclen))
950 if (__put_user(d_type, &dirent->d_type))
952 if (copy_to_user(dirent->d_name, name, namlen))
954 if (__put_user(0, dirent->d_name + namlen))
956 buf->previous = dirent;
957 dirent = (void __user *)dirent + reclen;
958 buf->current_dir = dirent;
959 buf->count -= reclen;
962 buf->error = -EFAULT;
966 COMPAT_SYSCALL_DEFINE3(getdents64, unsigned int, fd,
967 struct linux_dirent64 __user *, dirent, unsigned int, count)
970 struct linux_dirent64 __user * lastdirent;
971 struct compat_getdents_callback64 buf = {
972 .ctx.actor = compat_filldir64,
973 .current_dir = dirent,
978 if (!access_ok(VERIFY_WRITE, dirent, count))
985 error = iterate_dir(f.file, &buf.ctx);
988 lastdirent = buf.previous;
990 typeof(lastdirent->d_off) d_off = buf.ctx.pos;
991 if (__put_user_unaligned(d_off, &lastdirent->d_off))
994 error = count - buf.count;
999 #endif /* __ARCH_WANT_COMPAT_SYS_GETDENTS64 */
1002 * Exactly like fs/open.c:sys_open(), except that it doesn't set the
1005 COMPAT_SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
1007 return do_sys_open(AT_FDCWD, filename, flags, mode);
1011 * Exactly like fs/open.c:sys_openat(), except that it doesn't set the
1014 COMPAT_SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags, umode_t, mode)
1016 return do_sys_open(dfd, filename, flags, mode);
1019 #define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t))
1021 static int poll_select_copy_remaining(struct timespec *end_time, void __user *p,
1022 int timeval, int ret)
1029 if (current->personality & STICKY_TIMEOUTS)
1032 /* No update for zero timeout */
1033 if (!end_time->tv_sec && !end_time->tv_nsec)
1037 ts = timespec_sub(*end_time, ts);
1039 ts.tv_sec = ts.tv_nsec = 0;
1042 struct compat_timeval rtv;
1044 rtv.tv_sec = ts.tv_sec;
1045 rtv.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
1047 if (!copy_to_user(p, &rtv, sizeof(rtv)))
1050 struct compat_timespec rts;
1052 rts.tv_sec = ts.tv_sec;
1053 rts.tv_nsec = ts.tv_nsec;
1055 if (!copy_to_user(p, &rts, sizeof(rts)))
1059 * If an application puts its timeval in read-only memory, we
1060 * don't want the Linux-specific update to the timeval to
1061 * cause a fault after the select has completed
1062 * successfully. However, because we're not updating the
1063 * timeval, we can't restart the system call.
1067 if (ret == -ERESTARTNOHAND)
1073 * Ooo, nasty. We need here to frob 32-bit unsigned longs to
1074 * 64-bit unsigned longs.
1077 int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1078 unsigned long *fdset)
1080 nr = DIV_ROUND_UP(nr, __COMPAT_NFDBITS);
1084 if (!access_ok(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t)))
1091 if (__get_user(l, ufdset) || __get_user(h, ufdset+1))
1094 *fdset++ = h << 32 | l;
1097 if (odd && __get_user(*fdset, ufdset))
1100 /* Tricky, must clear full unsigned long in the
1101 * kernel fdset at the end, this makes sure that
1104 memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t));
1110 int compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset,
1111 unsigned long *fdset)
1114 nr = DIV_ROUND_UP(nr, __COMPAT_NFDBITS);
1125 if (__put_user(l, ufdset) || __put_user(h, ufdset+1))
1130 if (odd && __put_user(*fdset, ufdset))
1137 * This is a virtual copy of sys_select from fs/select.c and probably
1138 * should be compared to it from time to time
1142 * We can actually return ERESTARTSYS instead of EINTR, but I'd
1143 * like to be certain this leads to no problems. So I return
1144 * EINTR just for safety.
1146 * Update: ERESTARTSYS breaks at least the xview clock binary, so
1147 * I'm trying ERESTARTNOHAND which restart only when you want to.
1149 int compat_core_sys_select(int n, compat_ulong_t __user *inp,
1150 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1151 struct timespec *end_time)
1155 int size, max_fds, ret = -EINVAL;
1156 struct fdtable *fdt;
1157 long stack_fds[SELECT_STACK_ALLOC/sizeof(long)];
1162 /* max_fds can increase, so grab it once to avoid race */
1164 fdt = files_fdtable(current->files);
1165 max_fds = fdt->max_fds;
1171 * We need 6 bitmaps (in/out/ex for both incoming and outgoing),
1172 * since we used fdset we need to allocate memory in units of
1175 size = FDS_BYTES(n);
1177 if (size > sizeof(stack_fds) / 6) {
1178 bits = kmalloc(6 * size, GFP_KERNEL);
1183 fds.in = (unsigned long *) bits;
1184 fds.out = (unsigned long *) (bits + size);
1185 fds.ex = (unsigned long *) (bits + 2*size);
1186 fds.res_in = (unsigned long *) (bits + 3*size);
1187 fds.res_out = (unsigned long *) (bits + 4*size);
1188 fds.res_ex = (unsigned long *) (bits + 5*size);
1190 if ((ret = compat_get_fd_set(n, inp, fds.in)) ||
1191 (ret = compat_get_fd_set(n, outp, fds.out)) ||
1192 (ret = compat_get_fd_set(n, exp, fds.ex)))
1194 zero_fd_set(n, fds.res_in);
1195 zero_fd_set(n, fds.res_out);
1196 zero_fd_set(n, fds.res_ex);
1198 ret = do_select(n, &fds, end_time);
1203 ret = -ERESTARTNOHAND;
1204 if (signal_pending(current))
1209 if (compat_set_fd_set(n, inp, fds.res_in) ||
1210 compat_set_fd_set(n, outp, fds.res_out) ||
1211 compat_set_fd_set(n, exp, fds.res_ex))
1214 if (bits != stack_fds)
1220 COMPAT_SYSCALL_DEFINE5(select, int, n, compat_ulong_t __user *, inp,
1221 compat_ulong_t __user *, outp, compat_ulong_t __user *, exp,
1222 struct compat_timeval __user *, tvp)
1224 struct timespec end_time, *to = NULL;
1225 struct compat_timeval tv;
1229 if (copy_from_user(&tv, tvp, sizeof(tv)))
1233 if (poll_select_set_timeout(to,
1234 tv.tv_sec + (tv.tv_usec / USEC_PER_SEC),
1235 (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC))
1239 ret = compat_core_sys_select(n, inp, outp, exp, to);
1240 ret = poll_select_copy_remaining(&end_time, tvp, 1, ret);
1245 struct compat_sel_arg_struct {
1253 COMPAT_SYSCALL_DEFINE1(old_select, struct compat_sel_arg_struct __user *, arg)
1255 struct compat_sel_arg_struct a;
1257 if (copy_from_user(&a, arg, sizeof(a)))
1259 return compat_sys_select(a.n, compat_ptr(a.inp), compat_ptr(a.outp),
1260 compat_ptr(a.exp), compat_ptr(a.tvp));
1263 static long do_compat_pselect(int n, compat_ulong_t __user *inp,
1264 compat_ulong_t __user *outp, compat_ulong_t __user *exp,
1265 struct compat_timespec __user *tsp, compat_sigset_t __user *sigmask,
1266 compat_size_t sigsetsize)
1268 compat_sigset_t ss32;
1269 sigset_t ksigmask, sigsaved;
1270 struct compat_timespec ts;
1271 struct timespec end_time, *to = NULL;
1275 if (copy_from_user(&ts, tsp, sizeof(ts)))
1279 if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
1284 if (sigsetsize != sizeof(compat_sigset_t))
1286 if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
1288 sigset_from_compat(&ksigmask, &ss32);
1290 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
1291 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1294 ret = compat_core_sys_select(n, inp, outp, exp, to);
1295 ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
1297 if (ret == -ERESTARTNOHAND) {
1299 * Don't restore the signal mask yet. Let do_signal() deliver
1300 * the signal on the way back to userspace, before the signal
1304 memcpy(¤t->saved_sigmask, &sigsaved,
1306 set_restore_sigmask();
1309 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1314 COMPAT_SYSCALL_DEFINE6(pselect6, int, n, compat_ulong_t __user *, inp,
1315 compat_ulong_t __user *, outp, compat_ulong_t __user *, exp,
1316 struct compat_timespec __user *, tsp, void __user *, sig)
1318 compat_size_t sigsetsize = 0;
1319 compat_uptr_t up = 0;
1322 if (!access_ok(VERIFY_READ, sig,
1323 sizeof(compat_uptr_t)+sizeof(compat_size_t)) ||
1324 __get_user(up, (compat_uptr_t __user *)sig) ||
1325 __get_user(sigsetsize,
1326 (compat_size_t __user *)(sig+sizeof(up))))
1329 return do_compat_pselect(n, inp, outp, exp, tsp, compat_ptr(up),
1333 COMPAT_SYSCALL_DEFINE5(ppoll, struct pollfd __user *, ufds,
1334 unsigned int, nfds, struct compat_timespec __user *, tsp,
1335 const compat_sigset_t __user *, sigmask, compat_size_t, sigsetsize)
1337 compat_sigset_t ss32;
1338 sigset_t ksigmask, sigsaved;
1339 struct compat_timespec ts;
1340 struct timespec end_time, *to = NULL;
1344 if (copy_from_user(&ts, tsp, sizeof(ts)))
1348 if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec))
1353 if (sigsetsize != sizeof(compat_sigset_t))
1355 if (copy_from_user(&ss32, sigmask, sizeof(ss32)))
1357 sigset_from_compat(&ksigmask, &ss32);
1359 sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
1360 sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
1363 ret = do_sys_poll(ufds, nfds, to);
1365 /* We can restart this syscall, usually */
1366 if (ret == -EINTR) {
1368 * Don't restore the signal mask yet. Let do_signal() deliver
1369 * the signal on the way back to userspace, before the signal
1373 memcpy(¤t->saved_sigmask, &sigsaved,
1375 set_restore_sigmask();
1377 ret = -ERESTARTNOHAND;
1379 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
1381 ret = poll_select_copy_remaining(&end_time, tsp, 0, ret);
1386 #ifdef CONFIG_FHANDLE
1388 * Exactly like fs/open.c:sys_open_by_handle_at(), except that it
1389 * doesn't set the O_LARGEFILE flag.
1391 COMPAT_SYSCALL_DEFINE3(open_by_handle_at, int, mountdirfd,
1392 struct file_handle __user *, handle, int, flags)
1394 return do_handle_open(mountdirfd, handle, flags);