Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/compat.c |
| 3 | * |
| 4 | * Kernel compatibililty routines for e.g. 32 bit syscall support |
| 5 | * on 64 bit kernels. |
| 6 | * |
| 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 |
Pavel Machek | a253129 | 2010-07-18 14:27:13 +0200 | [diff] [blame] | 11 | * Copyright (C) 2003 Pavel Machek (pavel@ucw.cz) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * |
| 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. |
| 16 | */ |
| 17 | |
Kevin Winchester | 85c9fe8 | 2010-08-09 17:20:22 -0700 | [diff] [blame] | 18 | #include <linux/stddef.h> |
Milind Arun Choudhary | 022a169 | 2007-05-08 00:29:02 -0700 | [diff] [blame] | 19 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/linkage.h> |
| 21 | #include <linux/compat.h> |
| 22 | #include <linux/errno.h> |
| 23 | #include <linux/time.h> |
Ingo Molnar | 5b825c3 | 2017-02-02 17:54:15 +0100 | [diff] [blame] | 24 | #include <linux/cred.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/fs.h> |
| 26 | #include <linux/fcntl.h> |
| 27 | #include <linux/namei.h> |
| 28 | #include <linux/file.h> |
Al Viro | 9f3acc3 | 2008-04-24 07:44:08 -0400 | [diff] [blame] | 29 | #include <linux/fdtable.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/vfs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/ioctl.h> |
| 32 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/ncp_mount.h> |
David Howells | 9a9947b | 2005-04-18 10:54:51 -0700 | [diff] [blame] | 34 | #include <linux/nfs4_mount.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/syscalls.h> |
| 36 | #include <linux/ctype.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/dirent.h> |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 38 | #include <linux/fsnotify.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/highuid.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/personality.h> |
| 41 | #include <linux/rwsem.h> |
Jay Lan | 8f0ab51 | 2006-09-30 23:28:59 -0700 | [diff] [blame] | 42 | #include <linux/tsacct_kern.h> |
Christoph Hellwig | 6272e26 | 2007-05-08 00:29:21 -0700 | [diff] [blame] | 43 | #include <linux/security.h> |
Al Viro | a1f8e7f7 | 2006-10-19 16:08:53 -0400 | [diff] [blame] | 44 | #include <linux/highmem.h> |
Davide Libenzi | 6d18c92 | 2007-05-10 22:23:15 -0700 | [diff] [blame] | 45 | #include <linux/signal.h> |
Al Viro | bd01f84 | 2006-10-19 17:23:57 -0400 | [diff] [blame] | 46 | #include <linux/poll.h> |
David S. Miller | 4a805e8 | 2005-09-14 21:40:00 -0700 | [diff] [blame] | 47 | #include <linux/mm.h> |
Al Viro | 498052b | 2009-03-30 07:20:30 -0400 | [diff] [blame] | 48 | #include <linux/fs_struct.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 49 | #include <linux/slab.h> |
wu zhangjin | 504b701 | 2010-10-30 08:19:35 -0700 | [diff] [blame] | 50 | #include <linux/pagemap.h> |
Kent Overstreet | a27bb33 | 2013-05-07 16:19:08 -0700 | [diff] [blame] | 51 | #include <linux/aio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 53 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #include <asm/mmu_context.h> |
| 55 | #include <asm/ioctls.h> |
David Howells | 07f3f05 | 2006-09-30 20:52:18 +0200 | [diff] [blame] | 56 | #include "internal.h" |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | /* |
| 59 | * Not all architectures have sys_utime, so implement this in terms |
| 60 | * of sys_utimes. |
| 61 | */ |
Heiko Carstens | 625b1d7 | 2014-03-04 10:53:50 +0100 | [diff] [blame] | 62 | COMPAT_SYSCALL_DEFINE2(utime, const char __user *, filename, |
| 63 | struct compat_utimbuf __user *, t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | { |
Ulrich Drepper | 1c710c8 | 2007-05-08 00:33:25 -0700 | [diff] [blame] | 65 | struct timespec tv[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
| 67 | if (t) { |
| 68 | if (get_user(tv[0].tv_sec, &t->actime) || |
| 69 | get_user(tv[1].tv_sec, &t->modtime)) |
| 70 | return -EFAULT; |
Ulrich Drepper | 1c710c8 | 2007-05-08 00:33:25 -0700 | [diff] [blame] | 71 | tv[0].tv_nsec = 0; |
| 72 | tv[1].tv_nsec = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } |
Ulrich Drepper | 1c710c8 | 2007-05-08 00:33:25 -0700 | [diff] [blame] | 74 | return do_utimes(AT_FDCWD, filename, t ? tv : NULL, 0); |
| 75 | } |
| 76 | |
Heiko Carstens | 625b1d7 | 2014-03-04 10:53:50 +0100 | [diff] [blame] | 77 | COMPAT_SYSCALL_DEFINE4(utimensat, unsigned int, dfd, const char __user *, filename, struct compat_timespec __user *, t, int, flags) |
Ulrich Drepper | 1c710c8 | 2007-05-08 00:33:25 -0700 | [diff] [blame] | 78 | { |
| 79 | struct timespec tv[2]; |
| 80 | |
| 81 | if (t) { |
H. Peter Anvin | 81993e8 | 2014-02-01 18:54:11 -0800 | [diff] [blame] | 82 | if (compat_get_timespec(&tv[0], &t[0]) || |
| 83 | compat_get_timespec(&tv[1], &t[1])) |
Ulrich Drepper | 1c710c8 | 2007-05-08 00:33:25 -0700 | [diff] [blame] | 84 | return -EFAULT; |
| 85 | |
Ulrich Drepper | 1c710c8 | 2007-05-08 00:33:25 -0700 | [diff] [blame] | 86 | if (tv[0].tv_nsec == UTIME_OMIT && tv[1].tv_nsec == UTIME_OMIT) |
| 87 | return 0; |
| 88 | } |
| 89 | return do_utimes(dfd, filename, t ? tv : NULL, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Heiko Carstens | 625b1d7 | 2014-03-04 10:53:50 +0100 | [diff] [blame] | 92 | COMPAT_SYSCALL_DEFINE3(futimesat, unsigned int, dfd, const char __user *, filename, struct compat_timeval __user *, t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { |
Ulrich Drepper | 1c710c8 | 2007-05-08 00:33:25 -0700 | [diff] [blame] | 94 | struct timespec tv[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Stephen Rothwell | 9ad11ab | 2006-02-02 16:11:51 +1100 | [diff] [blame] | 96 | if (t) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | if (get_user(tv[0].tv_sec, &t[0].tv_sec) || |
Ulrich Drepper | 1c710c8 | 2007-05-08 00:33:25 -0700 | [diff] [blame] | 98 | get_user(tv[0].tv_nsec, &t[0].tv_usec) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | get_user(tv[1].tv_sec, &t[1].tv_sec) || |
Ulrich Drepper | 1c710c8 | 2007-05-08 00:33:25 -0700 | [diff] [blame] | 100 | get_user(tv[1].tv_nsec, &t[1].tv_usec)) |
Stephen Rothwell | 9ad11ab | 2006-02-02 16:11:51 +1100 | [diff] [blame] | 101 | return -EFAULT; |
Ulrich Drepper | 1c710c8 | 2007-05-08 00:33:25 -0700 | [diff] [blame] | 102 | if (tv[0].tv_nsec >= 1000000 || tv[0].tv_nsec < 0 || |
| 103 | tv[1].tv_nsec >= 1000000 || tv[1].tv_nsec < 0) |
| 104 | return -EINVAL; |
| 105 | tv[0].tv_nsec *= 1000; |
| 106 | tv[1].tv_nsec *= 1000; |
Stephen Rothwell | 9ad11ab | 2006-02-02 16:11:51 +1100 | [diff] [blame] | 107 | } |
Ulrich Drepper | 1c710c8 | 2007-05-08 00:33:25 -0700 | [diff] [blame] | 108 | return do_utimes(dfd, filename, t ? tv : NULL, 0); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Heiko Carstens | 625b1d7 | 2014-03-04 10:53:50 +0100 | [diff] [blame] | 111 | COMPAT_SYSCALL_DEFINE2(utimes, const char __user *, filename, struct compat_timeval __user *, t) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 112 | { |
| 113 | return compat_sys_futimesat(AT_FDCWD, filename, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Christoph Hellwig | f7a5000 | 2008-10-15 22:02:05 -0700 | [diff] [blame] | 116 | static int cp_compat_stat(struct kstat *stat, struct compat_stat __user *ubuf) |
| 117 | { |
Al Viro | fcf8306 | 2012-02-12 20:56:29 -0500 | [diff] [blame] | 118 | struct compat_stat tmp; |
Christoph Hellwig | f7a5000 | 2008-10-15 22:02:05 -0700 | [diff] [blame] | 119 | |
Al Viro | fcf8306 | 2012-02-12 20:56:29 -0500 | [diff] [blame] | 120 | if (!old_valid_dev(stat->dev) || !old_valid_dev(stat->rdev)) |
Christoph Hellwig | f7a5000 | 2008-10-15 22:02:05 -0700 | [diff] [blame] | 121 | return -EOVERFLOW; |
| 122 | |
Al Viro | fcf8306 | 2012-02-12 20:56:29 -0500 | [diff] [blame] | 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) |
| 127 | return -EOVERFLOW; |
| 128 | tmp.st_mode = stat->mode; |
| 129 | tmp.st_nlink = stat->nlink; |
| 130 | if (tmp.st_nlink != stat->nlink) |
| 131 | return -EOVERFLOW; |
Eric W. Biederman | a7c1938 | 2012-02-09 09:10:30 -0800 | [diff] [blame] | 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)); |
Al Viro | fcf8306 | 2012-02-12 20:56:29 -0500 | [diff] [blame] | 134 | tmp.st_rdev = old_encode_dev(stat->rdev); |
| 135 | if ((u64) stat->size > MAX_NON_LFS) |
| 136 | return -EOVERFLOW; |
| 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; |
Christoph Hellwig | f7a5000 | 2008-10-15 22:02:05 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Heiko Carstens | 625b1d7 | 2014-03-04 10:53:50 +0100 | [diff] [blame] | 149 | COMPAT_SYSCALL_DEFINE2(newstat, const char __user *, filename, |
| 150 | struct compat_stat __user *, statbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | { |
| 152 | struct kstat stat; |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 153 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 155 | error = vfs_stat(filename, &stat); |
| 156 | if (error) |
| 157 | return error; |
| 158 | return cp_compat_stat(&stat, statbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Heiko Carstens | 625b1d7 | 2014-03-04 10:53:50 +0100 | [diff] [blame] | 161 | COMPAT_SYSCALL_DEFINE2(newlstat, const char __user *, filename, |
| 162 | struct compat_stat __user *, statbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | { |
| 164 | struct kstat stat; |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 165 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
Christoph Hellwig | 2eae7a1 | 2009-04-08 16:34:03 -0400 | [diff] [blame] | 167 | error = vfs_lstat(filename, &stat); |
| 168 | if (error) |
| 169 | return error; |
| 170 | return cp_compat_stat(&stat, statbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Kyle McMartin | 82d821d | 2006-03-24 03:18:20 -0800 | [diff] [blame] | 173 | #ifndef __ARCH_WANT_STAT64 |
Heiko Carstens | 625b1d7 | 2014-03-04 10:53:50 +0100 | [diff] [blame] | 174 | COMPAT_SYSCALL_DEFINE4(newfstatat, unsigned int, dfd, |
| 175 | const char __user *, filename, |
| 176 | struct compat_stat __user *, statbuf, int, flag) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 177 | { |
| 178 | struct kstat stat; |
Oleg Drokin | 0112fc2 | 2009-04-08 20:05:42 +0400 | [diff] [blame] | 179 | int error; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 180 | |
Oleg Drokin | 0112fc2 | 2009-04-08 20:05:42 +0400 | [diff] [blame] | 181 | error = vfs_fstatat(dfd, filename, &stat, flag); |
| 182 | if (error) |
| 183 | return error; |
| 184 | return cp_compat_stat(&stat, statbuf); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 185 | } |
Kyle McMartin | 82d821d | 2006-03-24 03:18:20 -0800 | [diff] [blame] | 186 | #endif |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 187 | |
Heiko Carstens | 625b1d7 | 2014-03-04 10:53:50 +0100 | [diff] [blame] | 188 | COMPAT_SYSCALL_DEFINE2(newfstat, unsigned int, fd, |
| 189 | struct compat_stat __user *, statbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | { |
| 191 | struct kstat stat; |
| 192 | int error = vfs_fstat(fd, &stat); |
| 193 | |
| 194 | if (!error) |
| 195 | error = cp_compat_stat(&stat, statbuf); |
| 196 | return error; |
| 197 | } |
| 198 | |
| 199 | static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf) |
| 200 | { |
| 201 | |
| 202 | if (sizeof ubuf->f_blocks == 4) { |
Jon Tollefson | f4a67cc | 2008-07-23 21:27:55 -0700 | [diff] [blame] | 203 | if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail | |
| 204 | kbuf->f_bsize | kbuf->f_frsize) & 0xffffffff00000000ULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | return -EOVERFLOW; |
| 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)) |
| 210 | return -EOVERFLOW; |
| 211 | if (kbuf->f_ffree != 0xffffffffffffffffULL |
| 212 | && (kbuf->f_ffree & 0xffffffff00000000ULL)) |
| 213 | return -EOVERFLOW; |
| 214 | } |
| 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) || |
Eric W. Biederman | 1448c72 | 2011-10-17 13:40:02 -0700 | [diff] [blame] | 227 | __put_user(kbuf->f_flags, &ubuf->f_flags) || |
| 228 | __clear_user(ubuf->f_spare, sizeof(ubuf->f_spare))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | return -EFAULT; |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | /* |
Namhyung Kim | 974d879 | 2010-12-27 01:41:53 +0900 | [diff] [blame] | 234 | * The following statfs calls are copies of code from fs/statfs.c and |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | * should be checked against those from time to time |
| 236 | */ |
Heiko Carstens | 625b1d7 | 2014-03-04 10:53:50 +0100 | [diff] [blame] | 237 | COMPAT_SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct compat_statfs __user *, buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | { |
Al Viro | c8b91ac | 2011-03-12 10:41:39 -0500 | [diff] [blame] | 239 | struct kstatfs tmp; |
| 240 | int error = user_statfs(pathname, &tmp); |
| 241 | if (!error) |
| 242 | error = put_compat_statfs(buf, &tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | return error; |
| 244 | } |
| 245 | |
Heiko Carstens | 625b1d7 | 2014-03-04 10:53:50 +0100 | [diff] [blame] | 246 | COMPAT_SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct compat_statfs __user *, buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | struct kstatfs tmp; |
Al Viro | c8b91ac | 2011-03-12 10:41:39 -0500 | [diff] [blame] | 249 | int error = fd_statfs(fd, &tmp); |
David Gibson | 86e07ce | 2005-11-21 21:32:23 -0800 | [diff] [blame] | 250 | if (!error) |
| 251 | error = put_compat_statfs(buf, &tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | return error; |
| 253 | } |
| 254 | |
| 255 | static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf) |
| 256 | { |
Li Wang | 64d2ab3 | 2016-11-15 17:19:01 +0800 | [diff] [blame] | 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) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | return -EOVERFLOW; |
| 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)) |
| 265 | return -EOVERFLOW; |
| 266 | if (kbuf->f_ffree != 0xffffffffffffffffULL |
| 267 | && (kbuf->f_ffree & 0xffffffff00000000ULL)) |
| 268 | return -EOVERFLOW; |
| 269 | } |
| 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]) || |
Namhyung Kim | e0bb6bd | 2010-12-27 01:41:54 +0900 | [diff] [blame] | 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))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | return -EFAULT; |
| 285 | return 0; |
| 286 | } |
| 287 | |
Heiko Carstens | 625b1d7 | 2014-03-04 10:53:50 +0100 | [diff] [blame] | 288 | COMPAT_SYSCALL_DEFINE3(statfs64, const char __user *, pathname, compat_size_t, sz, struct compat_statfs64 __user *, buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | struct kstatfs tmp; |
| 291 | int error; |
| 292 | |
| 293 | if (sz != sizeof(*buf)) |
| 294 | return -EINVAL; |
| 295 | |
Al Viro | c8b91ac | 2011-03-12 10:41:39 -0500 | [diff] [blame] | 296 | error = user_statfs(pathname, &tmp); |
David Gibson | 86e07ce | 2005-11-21 21:32:23 -0800 | [diff] [blame] | 297 | if (!error) |
| 298 | error = put_compat_statfs64(buf, &tmp); |
Al Viro | c8b91ac | 2011-03-12 10:41:39 -0500 | [diff] [blame] | 299 | return error; |
| 300 | } |
| 301 | |
Heiko Carstens | 625b1d7 | 2014-03-04 10:53:50 +0100 | [diff] [blame] | 302 | COMPAT_SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, compat_size_t, sz, struct compat_statfs64 __user *, buf) |
Al Viro | c8b91ac | 2011-03-12 10:41:39 -0500 | [diff] [blame] | 303 | { |
| 304 | struct kstatfs tmp; |
| 305 | int error; |
| 306 | |
| 307 | if (sz != sizeof(*buf)) |
| 308 | return -EINVAL; |
| 309 | |
| 310 | error = fd_statfs(fd, &tmp); |
| 311 | if (!error) |
| 312 | error = put_compat_statfs64(buf, &tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | return error; |
| 314 | } |
| 315 | |
Christoph Hellwig | 2b1c6bd | 2008-11-28 10:09:09 +0100 | [diff] [blame] | 316 | /* |
| 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. |
| 320 | */ |
Heiko Carstens | 625b1d7 | 2014-03-04 10:53:50 +0100 | [diff] [blame] | 321 | COMPAT_SYSCALL_DEFINE2(ustat, unsigned, dev, struct compat_ustat __user *, u) |
Christoph Hellwig | 2b1c6bd | 2008-11-28 10:09:09 +0100 | [diff] [blame] | 322 | { |
Christoph Hellwig | 2b1c6bd | 2008-11-28 10:09:09 +0100 | [diff] [blame] | 323 | struct compat_ustat tmp; |
| 324 | struct kstatfs sbuf; |
Al Viro | cf31e70 | 2012-01-02 22:28:36 -0500 | [diff] [blame] | 325 | int err = vfs_ustat(new_decode_dev(dev), &sbuf); |
Christoph Hellwig | 2b1c6bd | 2008-11-28 10:09:09 +0100 | [diff] [blame] | 326 | if (err) |
| 327 | return err; |
| 328 | |
| 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))) |
| 333 | return -EFAULT; |
| 334 | return 0; |
| 335 | } |
| 336 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | static int get_compat_flock(struct flock *kfl, struct compat_flock __user *ufl) |
| 338 | { |
| 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)) |
| 345 | return -EFAULT; |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | static int put_compat_flock(struct flock *kfl, struct compat_flock __user *ufl) |
| 350 | { |
| 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)) |
| 357 | return -EFAULT; |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | #ifndef HAVE_ARCH_GET_COMPAT_FLOCK64 |
| 362 | static int get_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl) |
| 363 | { |
| 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)) |
| 370 | return -EFAULT; |
| 371 | return 0; |
| 372 | } |
| 373 | #endif |
| 374 | |
| 375 | #ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64 |
| 376 | static int put_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl) |
| 377 | { |
| 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)) |
| 384 | return -EFAULT; |
| 385 | return 0; |
| 386 | } |
| 387 | #endif |
| 388 | |
Jeff Layton | 5d50ffd | 2014-02-03 12:13:10 -0500 | [diff] [blame] | 389 | static unsigned int |
| 390 | convert_fcntl_cmd(unsigned int cmd) |
| 391 | { |
| 392 | switch (cmd) { |
| 393 | case F_GETLK64: |
| 394 | return F_GETLK; |
| 395 | case F_SETLK64: |
| 396 | return F_SETLK; |
| 397 | case F_SETLKW64: |
| 398 | return F_SETLKW; |
| 399 | } |
| 400 | |
| 401 | return cmd; |
| 402 | } |
| 403 | |
Heiko Carstens | 932602e | 2014-03-04 16:07:52 +0100 | [diff] [blame] | 404 | COMPAT_SYSCALL_DEFINE3(fcntl64, unsigned int, fd, unsigned int, cmd, |
| 405 | compat_ulong_t, arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | { |
| 407 | mm_segment_t old_fs; |
| 408 | struct flock f; |
| 409 | long ret; |
Jeff Layton | 5d50ffd | 2014-02-03 12:13:10 -0500 | [diff] [blame] | 410 | unsigned int conv_cmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | |
| 412 | switch (cmd) { |
| 413 | case F_GETLK: |
| 414 | case F_SETLK: |
| 415 | case F_SETLKW: |
| 416 | ret = get_compat_flock(&f, compat_ptr(arg)); |
| 417 | if (ret != 0) |
| 418 | break; |
| 419 | old_fs = get_fs(); |
| 420 | set_fs(KERNEL_DS); |
| 421 | ret = sys_fcntl(fd, cmd, (unsigned long)&f); |
| 422 | set_fs(old_fs); |
| 423 | if (cmd == F_GETLK && ret == 0) { |
Nikanth Karthikesan | ff677f8 | 2009-04-01 14:40:51 +0530 | [diff] [blame] | 424 | /* GETLK was successful and we need to return the data... |
NeilBrown | 2520f14 | 2006-01-08 01:02:40 -0800 | [diff] [blame] | 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 |
| 433 | */ |
| 434 | |
| 435 | if (f.l_start > COMPAT_OFF_T_MAX) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | ret = -EOVERFLOW; |
NeilBrown | 2520f14 | 2006-01-08 01:02:40 -0800 | [diff] [blame] | 437 | if (f.l_len > COMPAT_OFF_T_MAX) |
| 438 | f.l_len = COMPAT_OFF_T_MAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | if (ret == 0) |
| 440 | ret = put_compat_flock(&f, compat_ptr(arg)); |
| 441 | } |
| 442 | break; |
| 443 | |
| 444 | case F_GETLK64: |
| 445 | case F_SETLK64: |
| 446 | case F_SETLKW64: |
Jeff Layton | 0d3f7a2 | 2014-04-22 08:23:58 -0400 | [diff] [blame] | 447 | case F_OFD_GETLK: |
| 448 | case F_OFD_SETLK: |
| 449 | case F_OFD_SETLKW: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | ret = get_compat_flock64(&f, compat_ptr(arg)); |
| 451 | if (ret != 0) |
| 452 | break; |
| 453 | old_fs = get_fs(); |
| 454 | set_fs(KERNEL_DS); |
Jeff Layton | 5d50ffd | 2014-02-03 12:13:10 -0500 | [diff] [blame] | 455 | conv_cmd = convert_fcntl_cmd(cmd); |
| 456 | ret = sys_fcntl(fd, conv_cmd, (unsigned long)&f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | set_fs(old_fs); |
Jeff Layton | 0d3f7a2 | 2014-04-22 08:23:58 -0400 | [diff] [blame] | 458 | if ((conv_cmd == F_GETLK || conv_cmd == F_OFD_GETLK) && ret == 0) { |
NeilBrown | 2520f14 | 2006-01-08 01:02:40 -0800 | [diff] [blame] | 459 | /* need to return lock information - see above for commentary */ |
| 460 | if (f.l_start > COMPAT_LOFF_T_MAX) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | ret = -EOVERFLOW; |
NeilBrown | 2520f14 | 2006-01-08 01:02:40 -0800 | [diff] [blame] | 462 | if (f.l_len > COMPAT_LOFF_T_MAX) |
| 463 | f.l_len = COMPAT_LOFF_T_MAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | if (ret == 0) |
| 465 | ret = put_compat_flock64(&f, compat_ptr(arg)); |
| 466 | } |
| 467 | break; |
| 468 | |
| 469 | default: |
| 470 | ret = sys_fcntl(fd, cmd, arg); |
| 471 | break; |
| 472 | } |
| 473 | return ret; |
| 474 | } |
| 475 | |
Heiko Carstens | 932602e | 2014-03-04 16:07:52 +0100 | [diff] [blame] | 476 | COMPAT_SYSCALL_DEFINE3(fcntl, unsigned int, fd, unsigned int, cmd, |
| 477 | compat_ulong_t, arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | { |
Jeff Layton | 5d50ffd | 2014-02-03 12:13:10 -0500 | [diff] [blame] | 479 | switch (cmd) { |
| 480 | case F_GETLK64: |
| 481 | case F_SETLK64: |
| 482 | case F_SETLKW64: |
Jeff Layton | 0d3f7a2 | 2014-04-22 08:23:58 -0400 | [diff] [blame] | 483 | case F_OFD_GETLK: |
| 484 | case F_OFD_SETLK: |
| 485 | case F_OFD_SETLKW: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | return -EINVAL; |
Jeff Layton | 5d50ffd | 2014-02-03 12:13:10 -0500 | [diff] [blame] | 487 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | return compat_sys_fcntl64(fd, cmd, arg); |
| 489 | } |
| 490 | |
Jeff Moyer | b837336 | 2010-05-26 14:44:25 -0700 | [diff] [blame] | 491 | /* A write operation does a read from user space and vice versa */ |
| 492 | #define vrfy_dir(type) ((type) == READ ? VERIFY_WRITE : VERIFY_READ) |
| 493 | |
| 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, |
Christopher Yeoh | ac34ebb | 2012-05-31 16:26:42 -0700 | [diff] [blame] | 497 | struct iovec **ret_pointer) |
Jeff Moyer | b837336 | 2010-05-26 14:44:25 -0700 | [diff] [blame] | 498 | { |
| 499 | compat_ssize_t tot_len; |
| 500 | struct iovec *iov = *ret_pointer = fast_pointer; |
| 501 | ssize_t ret = 0; |
| 502 | int seg; |
| 503 | |
| 504 | /* |
| 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... |
| 508 | */ |
| 509 | if (nr_segs == 0) |
| 510 | goto out; |
| 511 | |
| 512 | ret = -EINVAL; |
Shawn Lin | 85e7340 | 2016-08-22 15:45:59 +0800 | [diff] [blame] | 513 | if (nr_segs > UIO_MAXIOV) |
Jeff Moyer | b837336 | 2010-05-26 14:44:25 -0700 | [diff] [blame] | 514 | goto out; |
| 515 | if (nr_segs > fast_segs) { |
| 516 | ret = -ENOMEM; |
| 517 | iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL); |
Namhyung Kim | 6a5640f | 2010-12-27 01:41:52 +0900 | [diff] [blame] | 518 | if (iov == NULL) |
Jeff Moyer | b837336 | 2010-05-26 14:44:25 -0700 | [diff] [blame] | 519 | goto out; |
Jeff Moyer | b837336 | 2010-05-26 14:44:25 -0700 | [diff] [blame] | 520 | } |
| 521 | *ret_pointer = iov; |
| 522 | |
Mathieu Desnoyers | 8aec0f5 | 2013-02-25 10:20:36 -0500 | [diff] [blame] | 523 | ret = -EFAULT; |
| 524 | if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector))) |
| 525 | goto out; |
| 526 | |
Jeff Moyer | b837336 | 2010-05-26 14:44:25 -0700 | [diff] [blame] | 527 | /* |
| 528 | * Single unix specification: |
| 529 | * We should -EINVAL if an element length is not >= 0 and fitting an |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 530 | * ssize_t. |
Jeff Moyer | b837336 | 2010-05-26 14:44:25 -0700 | [diff] [blame] | 531 | * |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 532 | * In Linux, the total length is limited to MAX_RW_COUNT, there is |
| 533 | * no overflow possibility. |
Jeff Moyer | b837336 | 2010-05-26 14:44:25 -0700 | [diff] [blame] | 534 | */ |
| 535 | tot_len = 0; |
| 536 | ret = -EINVAL; |
| 537 | for (seg = 0; seg < nr_segs; seg++) { |
Jeff Moyer | b837336 | 2010-05-26 14:44:25 -0700 | [diff] [blame] | 538 | compat_uptr_t buf; |
| 539 | compat_ssize_t len; |
| 540 | |
| 541 | if (__get_user(len, &uvector->iov_len) || |
| 542 | __get_user(buf, &uvector->iov_base)) { |
| 543 | ret = -EFAULT; |
| 544 | goto out; |
| 545 | } |
| 546 | if (len < 0) /* size_t not fitting in compat_ssize_t .. */ |
| 547 | goto out; |
Christopher Yeoh | ac34ebb | 2012-05-31 16:26:42 -0700 | [diff] [blame] | 548 | if (type >= 0 && |
Christopher Yeoh | fcf6340 | 2011-10-31 17:06:39 -0700 | [diff] [blame] | 549 | !access_ok(vrfy_dir(type), compat_ptr(buf), len)) { |
Jeff Moyer | b837336 | 2010-05-26 14:44:25 -0700 | [diff] [blame] | 550 | ret = -EFAULT; |
| 551 | goto out; |
| 552 | } |
Linus Torvalds | 435f49a | 2010-10-29 10:36:49 -0700 | [diff] [blame] | 553 | if (len > MAX_RW_COUNT - tot_len) |
| 554 | len = MAX_RW_COUNT - tot_len; |
| 555 | tot_len += len; |
Jeff Moyer | b837336 | 2010-05-26 14:44:25 -0700 | [diff] [blame] | 556 | iov->iov_base = compat_ptr(buf); |
| 557 | iov->iov_len = (compat_size_t) len; |
| 558 | uvector++; |
| 559 | iov++; |
| 560 | } |
| 561 | ret = tot_len; |
| 562 | |
| 563 | out: |
| 564 | return ret; |
| 565 | } |
| 566 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | struct compat_ncp_mount_data { |
| 568 | compat_int_t version; |
| 569 | compat_uint_t ncp_fd; |
Stephen Rothwell | 202e597 | 2005-09-06 15:16:40 -0700 | [diff] [blame] | 570 | __compat_uid_t mounted_uid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
| 575 | compat_uint_t flags; |
Stephen Rothwell | 202e597 | 2005-09-06 15:16:40 -0700 | [diff] [blame] | 576 | __compat_uid_t uid; |
| 577 | __compat_gid_t gid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | compat_mode_t file_mode; |
| 579 | compat_mode_t dir_mode; |
| 580 | }; |
| 581 | |
| 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; |
| 590 | compat_ulong_t uid; |
| 591 | compat_ulong_t gid; |
| 592 | compat_ulong_t file_mode; |
| 593 | compat_ulong_t dir_mode; |
| 594 | }; |
| 595 | |
| 596 | static void *do_ncp_super_data_conv(void *raw_data) |
| 597 | { |
| 598 | int version = *(unsigned int *)raw_data; |
| 599 | |
| 600 | if (version == 3) { |
| 601 | struct compat_ncp_mount_data *c_n = raw_data; |
| 602 | struct ncp_mount_data *n = raw_data; |
| 603 | |
| 604 | n->dir_mode = c_n->dir_mode; |
| 605 | n->file_mode = c_n->file_mode; |
| 606 | n->gid = c_n->gid; |
| 607 | n->uid = c_n->uid; |
| 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; |
| 614 | |
| 615 | n->dir_mode = c_n->dir_mode; |
| 616 | n->file_mode = c_n->file_mode; |
| 617 | n->gid = c_n->gid; |
| 618 | n->uid = c_n->uid; |
| 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) { |
| 626 | return NULL; |
| 627 | } |
| 628 | |
| 629 | return raw_data; |
| 630 | } |
| 631 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | |
David Howells | 9a9947b | 2005-04-18 10:54:51 -0700 | [diff] [blame] | 633 | struct compat_nfs_string { |
| 634 | compat_uint_t len; |
David Howells | 5fc3e62 | 2005-04-27 15:39:03 -0700 | [diff] [blame] | 635 | compat_uptr_t data; |
David Howells | 9a9947b | 2005-04-18 10:54:51 -0700 | [diff] [blame] | 636 | }; |
| 637 | |
| 638 | static inline void compat_nfs_string(struct nfs_string *dst, |
| 639 | struct compat_nfs_string *src) |
| 640 | { |
| 641 | dst->data = compat_ptr(src->data); |
| 642 | dst->len = src->len; |
| 643 | } |
| 644 | |
| 645 | struct compat_nfs4_mount_data_v1 { |
| 646 | compat_int_t version; |
| 647 | compat_int_t flags; |
| 648 | compat_int_t rsize; |
| 649 | compat_int_t wsize; |
| 650 | compat_int_t timeo; |
| 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; |
David Howells | 5fc3e62 | 2005-04-27 15:39:03 -0700 | [diff] [blame] | 660 | compat_uptr_t host_addr; |
David Howells | 9a9947b | 2005-04-18 10:54:51 -0700 | [diff] [blame] | 661 | compat_int_t proto; |
| 662 | compat_int_t auth_flavourlen; |
David Howells | 5fc3e62 | 2005-04-27 15:39:03 -0700 | [diff] [blame] | 663 | compat_uptr_t auth_flavours; |
David Howells | 9a9947b | 2005-04-18 10:54:51 -0700 | [diff] [blame] | 664 | }; |
| 665 | |
| 666 | static int do_nfs4_super_data_conv(void *raw_data) |
| 667 | { |
| 668 | int version = *(compat_uint_t *) raw_data; |
| 669 | |
| 670 | if (version == 1) { |
| 671 | struct compat_nfs4_mount_data_v1 *raw = raw_data; |
| 672 | struct nfs4_mount_data *real = raw_data; |
| 673 | |
| 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; |
| 693 | } |
David Howells | 9a9947b | 2005-04-18 10:54:51 -0700 | [diff] [blame] | 694 | |
| 695 | return 0; |
| 696 | } |
| 697 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | #define NCPFS_NAME "ncpfs" |
David Howells | 9a9947b | 2005-04-18 10:54:51 -0700 | [diff] [blame] | 699 | #define NFS4_NAME "nfs4" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | |
Heiko Carstens | 932602e | 2014-03-04 16:07:52 +0100 | [diff] [blame] | 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) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | { |
Vegard Nossum | eca6f53 | 2009-09-18 13:05:45 -0700 | [diff] [blame] | 706 | char *kernel_type; |
Al Viro | b40ef86 | 2015-12-14 18:44:44 -0500 | [diff] [blame] | 707 | void *options; |
Vegard Nossum | eca6f53 | 2009-09-18 13:05:45 -0700 | [diff] [blame] | 708 | char *kernel_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | int retval; |
| 710 | |
Tim Gardner | b8850d1 | 2014-08-28 11:26:03 -0600 | [diff] [blame] | 711 | kernel_type = copy_mount_string(type); |
| 712 | retval = PTR_ERR(kernel_type); |
| 713 | if (IS_ERR(kernel_type)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | goto out; |
| 715 | |
Tim Gardner | b8850d1 | 2014-08-28 11:26:03 -0600 | [diff] [blame] | 716 | kernel_dev = copy_mount_string(dev_name); |
| 717 | retval = PTR_ERR(kernel_dev); |
| 718 | if (IS_ERR(kernel_dev)) |
Seunghun Lee | 5e6123f | 2014-09-14 22:15:10 +0900 | [diff] [blame] | 719 | goto out1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | |
Al Viro | b40ef86 | 2015-12-14 18:44:44 -0500 | [diff] [blame] | 721 | options = copy_mount_options(data); |
| 722 | retval = PTR_ERR(options); |
| 723 | if (IS_ERR(options)) |
Seunghun Lee | 5e6123f | 2014-09-14 22:15:10 +0900 | [diff] [blame] | 724 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | |
Al Viro | b40ef86 | 2015-12-14 18:44:44 -0500 | [diff] [blame] | 726 | if (kernel_type && options) { |
Arnd Bergmann | 2116b7a | 2010-10-04 22:55:57 +0200 | [diff] [blame] | 727 | if (!strcmp(kernel_type, NCPFS_NAME)) { |
Al Viro | b40ef86 | 2015-12-14 18:44:44 -0500 | [diff] [blame] | 728 | do_ncp_super_data_conv(options); |
Vegard Nossum | eca6f53 | 2009-09-18 13:05:45 -0700 | [diff] [blame] | 729 | } else if (!strcmp(kernel_type, NFS4_NAME)) { |
Al Viro | b40ef86 | 2015-12-14 18:44:44 -0500 | [diff] [blame] | 730 | retval = -EINVAL; |
| 731 | if (do_nfs4_super_data_conv(options)) |
Seunghun Lee | 5e6123f | 2014-09-14 22:15:10 +0900 | [diff] [blame] | 732 | goto out3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | } |
| 734 | } |
| 735 | |
Al Viro | b40ef86 | 2015-12-14 18:44:44 -0500 | [diff] [blame] | 736 | retval = do_mount(kernel_dev, dir_name, kernel_type, flags, options); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | out3: |
Al Viro | b40ef86 | 2015-12-14 18:44:44 -0500 | [diff] [blame] | 739 | kfree(options); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | out2: |
Seunghun Lee | 5e6123f | 2014-09-14 22:15:10 +0900 | [diff] [blame] | 741 | kfree(kernel_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | out1: |
Vegard Nossum | eca6f53 | 2009-09-18 13:05:45 -0700 | [diff] [blame] | 743 | kfree(kernel_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | out: |
| 745 | return retval; |
| 746 | } |
| 747 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | struct compat_old_linux_dirent { |
| 749 | compat_ulong_t d_ino; |
| 750 | compat_ulong_t d_offset; |
| 751 | unsigned short d_namlen; |
| 752 | char d_name[1]; |
| 753 | }; |
| 754 | |
| 755 | struct compat_readdir_callback { |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 756 | struct dir_context ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | struct compat_old_linux_dirent __user *dirent; |
| 758 | int result; |
| 759 | }; |
| 760 | |
Miklos Szeredi | ac7576f | 2014-10-30 17:37:34 +0100 | [diff] [blame] | 761 | static int compat_fillonedir(struct dir_context *ctx, const char *name, |
| 762 | int namlen, loff_t offset, u64 ino, |
| 763 | unsigned int d_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | { |
Miklos Szeredi | ac7576f | 2014-10-30 17:37:34 +0100 | [diff] [blame] | 765 | struct compat_readdir_callback *buf = |
| 766 | container_of(ctx, struct compat_readdir_callback, ctx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | struct compat_old_linux_dirent __user *dirent; |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 768 | compat_ulong_t d_ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | |
| 770 | if (buf->result) |
| 771 | return -EINVAL; |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 772 | d_ino = ino; |
Al Viro | 8f3f655 | 2008-08-12 00:28:24 -0400 | [diff] [blame] | 773 | if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) { |
| 774 | buf->result = -EOVERFLOW; |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 775 | return -EOVERFLOW; |
Al Viro | 8f3f655 | 2008-08-12 00:28:24 -0400 | [diff] [blame] | 776 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | buf->result++; |
| 778 | dirent = buf->dirent; |
| 779 | if (!access_ok(VERIFY_WRITE, dirent, |
| 780 | (unsigned long)(dirent->d_name + namlen + 1) - |
| 781 | (unsigned long)dirent)) |
| 782 | goto efault; |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 783 | if ( __put_user(d_ino, &dirent->d_ino) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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)) |
| 788 | goto efault; |
| 789 | return 0; |
| 790 | efault: |
| 791 | buf->result = -EFAULT; |
| 792 | return -EFAULT; |
| 793 | } |
| 794 | |
Heiko Carstens | 625b1d7 | 2014-03-04 10:53:50 +0100 | [diff] [blame] | 795 | COMPAT_SYSCALL_DEFINE3(old_readdir, unsigned int, fd, |
| 796 | struct compat_old_linux_dirent __user *, dirent, unsigned int, count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | { |
| 798 | int error; |
Al Viro | 63b6df1 | 2016-04-20 17:08:21 -0400 | [diff] [blame] | 799 | struct fd f = fdget_pos(fd); |
Al Viro | ac6614b | 2013-05-22 22:22:04 -0400 | [diff] [blame] | 800 | struct compat_readdir_callback buf = { |
| 801 | .ctx.actor = compat_fillonedir, |
| 802 | .dirent = dirent |
| 803 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 805 | if (!f.file) |
Al Viro | 863ced7 | 2012-04-21 18:40:32 -0400 | [diff] [blame] | 806 | return -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 808 | error = iterate_dir(f.file, &buf.ctx); |
Al Viro | 53c9c5c | 2008-08-24 07:29:52 -0400 | [diff] [blame] | 809 | if (buf.result) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | error = buf.result; |
| 811 | |
Al Viro | 63b6df1 | 2016-04-20 17:08:21 -0400 | [diff] [blame] | 812 | fdput_pos(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | return error; |
| 814 | } |
| 815 | |
| 816 | struct compat_linux_dirent { |
| 817 | compat_ulong_t d_ino; |
| 818 | compat_ulong_t d_off; |
| 819 | unsigned short d_reclen; |
| 820 | char d_name[1]; |
| 821 | }; |
| 822 | |
| 823 | struct compat_getdents_callback { |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 824 | struct dir_context ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | struct compat_linux_dirent __user *current_dir; |
| 826 | struct compat_linux_dirent __user *previous; |
| 827 | int count; |
| 828 | int error; |
| 829 | }; |
| 830 | |
Miklos Szeredi | ac7576f | 2014-10-30 17:37:34 +0100 | [diff] [blame] | 831 | static int compat_filldir(struct dir_context *ctx, const char *name, int namlen, |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 832 | loff_t offset, u64 ino, unsigned int d_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | { |
| 834 | struct compat_linux_dirent __user * dirent; |
Miklos Szeredi | ac7576f | 2014-10-30 17:37:34 +0100 | [diff] [blame] | 835 | struct compat_getdents_callback *buf = |
| 836 | container_of(ctx, struct compat_getdents_callback, ctx); |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 837 | compat_ulong_t d_ino; |
Kevin Winchester | 85c9fe8 | 2010-08-09 17:20:22 -0700 | [diff] [blame] | 838 | int reclen = ALIGN(offsetof(struct compat_linux_dirent, d_name) + |
| 839 | namlen + 2, sizeof(compat_long_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | |
| 841 | buf->error = -EINVAL; /* only used if we fail.. */ |
| 842 | if (reclen > buf->count) |
| 843 | return -EINVAL; |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 844 | d_ino = ino; |
Al Viro | 8f3f655 | 2008-08-12 00:28:24 -0400 | [diff] [blame] | 845 | if (sizeof(d_ino) < sizeof(ino) && d_ino != ino) { |
| 846 | buf->error = -EOVERFLOW; |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 847 | return -EOVERFLOW; |
Al Viro | 8f3f655 | 2008-08-12 00:28:24 -0400 | [diff] [blame] | 848 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | dirent = buf->previous; |
| 850 | if (dirent) { |
Theodore Ts'o | 1f60fbe | 2016-04-23 22:50:07 -0400 | [diff] [blame] | 851 | if (signal_pending(current)) |
| 852 | return -EINTR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | if (__put_user(offset, &dirent->d_off)) |
| 854 | goto efault; |
| 855 | } |
| 856 | dirent = buf->current_dir; |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 857 | if (__put_user(d_ino, &dirent->d_ino)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | goto efault; |
| 859 | if (__put_user(reclen, &dirent->d_reclen)) |
| 860 | goto efault; |
| 861 | if (copy_to_user(dirent->d_name, name, namlen)) |
| 862 | goto efault; |
| 863 | if (__put_user(0, dirent->d_name + namlen)) |
| 864 | goto efault; |
| 865 | if (__put_user(d_type, (char __user *) dirent + reclen - 1)) |
| 866 | goto efault; |
| 867 | buf->previous = dirent; |
| 868 | dirent = (void __user *)dirent + reclen; |
| 869 | buf->current_dir = dirent; |
| 870 | buf->count -= reclen; |
| 871 | return 0; |
| 872 | efault: |
| 873 | buf->error = -EFAULT; |
| 874 | return -EFAULT; |
| 875 | } |
| 876 | |
Heiko Carstens | 625b1d7 | 2014-03-04 10:53:50 +0100 | [diff] [blame] | 877 | COMPAT_SYSCALL_DEFINE3(getdents, unsigned int, fd, |
| 878 | struct compat_linux_dirent __user *, dirent, unsigned int, count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 880 | struct fd f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | struct compat_linux_dirent __user * lastdirent; |
Al Viro | ac6614b | 2013-05-22 22:22:04 -0400 | [diff] [blame] | 882 | struct compat_getdents_callback buf = { |
| 883 | .ctx.actor = compat_filldir, |
| 884 | .current_dir = dirent, |
| 885 | .count = count |
| 886 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | int error; |
| 888 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | if (!access_ok(VERIFY_WRITE, dirent, count)) |
Al Viro | 863ced7 | 2012-04-21 18:40:32 -0400 | [diff] [blame] | 890 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | |
Al Viro | 63b6df1 | 2016-04-20 17:08:21 -0400 | [diff] [blame] | 892 | f = fdget_pos(fd); |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 893 | if (!f.file) |
Al Viro | 863ced7 | 2012-04-21 18:40:32 -0400 | [diff] [blame] | 894 | return -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 896 | error = iterate_dir(f.file, &buf.ctx); |
Al Viro | 53c9c5c | 2008-08-24 07:29:52 -0400 | [diff] [blame] | 897 | if (error >= 0) |
| 898 | error = buf.error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | lastdirent = buf.previous; |
| 900 | if (lastdirent) { |
Al Viro | bb6f619 | 2013-05-15 18:49:12 -0400 | [diff] [blame] | 901 | if (put_user(buf.ctx.pos, &lastdirent->d_off)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | error = -EFAULT; |
| 903 | else |
| 904 | error = count - buf.count; |
| 905 | } |
Al Viro | 63b6df1 | 2016-04-20 17:08:21 -0400 | [diff] [blame] | 906 | fdput_pos(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | return error; |
| 908 | } |
| 909 | |
Heiko Carstens | 0473c9b | 2014-03-03 10:44:03 +0100 | [diff] [blame] | 910 | #ifdef __ARCH_WANT_COMPAT_SYS_GETDENTS64 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | |
| 912 | struct compat_getdents_callback64 { |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 913 | struct dir_context ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | struct linux_dirent64 __user *current_dir; |
| 915 | struct linux_dirent64 __user *previous; |
| 916 | int count; |
| 917 | int error; |
| 918 | }; |
| 919 | |
Miklos Szeredi | ac7576f | 2014-10-30 17:37:34 +0100 | [diff] [blame] | 920 | static int compat_filldir64(struct dir_context *ctx, const char *name, |
| 921 | int namlen, loff_t offset, u64 ino, |
| 922 | unsigned int d_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | { |
| 924 | struct linux_dirent64 __user *dirent; |
Miklos Szeredi | ac7576f | 2014-10-30 17:37:34 +0100 | [diff] [blame] | 925 | struct compat_getdents_callback64 *buf = |
| 926 | container_of(ctx, struct compat_getdents_callback64, ctx); |
Kevin Winchester | 85c9fe8 | 2010-08-09 17:20:22 -0700 | [diff] [blame] | 927 | int reclen = ALIGN(offsetof(struct linux_dirent64, d_name) + namlen + 1, |
| 928 | sizeof(u64)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | u64 off; |
| 930 | |
| 931 | buf->error = -EINVAL; /* only used if we fail.. */ |
| 932 | if (reclen > buf->count) |
| 933 | return -EINVAL; |
| 934 | dirent = buf->previous; |
| 935 | |
| 936 | if (dirent) { |
Theodore Ts'o | 1f60fbe | 2016-04-23 22:50:07 -0400 | [diff] [blame] | 937 | if (signal_pending(current)) |
| 938 | return -EINTR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | if (__put_user_unaligned(offset, &dirent->d_off)) |
| 940 | goto efault; |
| 941 | } |
| 942 | dirent = buf->current_dir; |
| 943 | if (__put_user_unaligned(ino, &dirent->d_ino)) |
| 944 | goto efault; |
| 945 | off = 0; |
| 946 | if (__put_user_unaligned(off, &dirent->d_off)) |
| 947 | goto efault; |
| 948 | if (__put_user(reclen, &dirent->d_reclen)) |
| 949 | goto efault; |
| 950 | if (__put_user(d_type, &dirent->d_type)) |
| 951 | goto efault; |
| 952 | if (copy_to_user(dirent->d_name, name, namlen)) |
| 953 | goto efault; |
| 954 | if (__put_user(0, dirent->d_name + namlen)) |
| 955 | goto efault; |
| 956 | buf->previous = dirent; |
| 957 | dirent = (void __user *)dirent + reclen; |
| 958 | buf->current_dir = dirent; |
| 959 | buf->count -= reclen; |
| 960 | return 0; |
| 961 | efault: |
| 962 | buf->error = -EFAULT; |
| 963 | return -EFAULT; |
| 964 | } |
| 965 | |
Heiko Carstens | 625b1d7 | 2014-03-04 10:53:50 +0100 | [diff] [blame] | 966 | COMPAT_SYSCALL_DEFINE3(getdents64, unsigned int, fd, |
| 967 | struct linux_dirent64 __user *, dirent, unsigned int, count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | { |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 969 | struct fd f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | struct linux_dirent64 __user * lastdirent; |
Al Viro | ac6614b | 2013-05-22 22:22:04 -0400 | [diff] [blame] | 971 | struct compat_getdents_callback64 buf = { |
| 972 | .ctx.actor = compat_filldir64, |
| 973 | .current_dir = dirent, |
| 974 | .count = count |
| 975 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | int error; |
| 977 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | if (!access_ok(VERIFY_WRITE, dirent, count)) |
Al Viro | 863ced7 | 2012-04-21 18:40:32 -0400 | [diff] [blame] | 979 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | |
Al Viro | 63b6df1 | 2016-04-20 17:08:21 -0400 | [diff] [blame] | 981 | f = fdget_pos(fd); |
Al Viro | 2903ff0 | 2012-08-28 12:52:22 -0400 | [diff] [blame] | 982 | if (!f.file) |
Al Viro | 863ced7 | 2012-04-21 18:40:32 -0400 | [diff] [blame] | 983 | return -EBADF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 985 | error = iterate_dir(f.file, &buf.ctx); |
Al Viro | 53c9c5c | 2008-08-24 07:29:52 -0400 | [diff] [blame] | 986 | if (error >= 0) |
| 987 | error = buf.error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | lastdirent = buf.previous; |
| 989 | if (lastdirent) { |
Al Viro | bb6f619 | 2013-05-15 18:49:12 -0400 | [diff] [blame] | 990 | typeof(lastdirent->d_off) d_off = buf.ctx.pos; |
Heiko Carstens | 7116e99 | 2006-12-06 20:36:36 -0800 | [diff] [blame] | 991 | if (__put_user_unaligned(d_off, &lastdirent->d_off)) |
Al Viro | 53c9c5c | 2008-08-24 07:29:52 -0400 | [diff] [blame] | 992 | error = -EFAULT; |
| 993 | else |
| 994 | error = count - buf.count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | } |
Al Viro | 63b6df1 | 2016-04-20 17:08:21 -0400 | [diff] [blame] | 996 | fdput_pos(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | return error; |
| 998 | } |
Heiko Carstens | 0473c9b | 2014-03-03 10:44:03 +0100 | [diff] [blame] | 999 | #endif /* __ARCH_WANT_COMPAT_SYS_GETDENTS64 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | /* |
Miklos Szeredi | e922efc | 2005-09-06 15:18:25 -0700 | [diff] [blame] | 1002 | * Exactly like fs/open.c:sys_open(), except that it doesn't set the |
| 1003 | * O_LARGEFILE flag. |
| 1004 | */ |
Al Viro | f482e1b | 2012-12-25 20:24:38 -0500 | [diff] [blame] | 1005 | COMPAT_SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode) |
Miklos Szeredi | e922efc | 2005-09-06 15:18:25 -0700 | [diff] [blame] | 1006 | { |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1007 | return do_sys_open(AT_FDCWD, filename, flags, mode); |
| 1008 | } |
| 1009 | |
| 1010 | /* |
| 1011 | * Exactly like fs/open.c:sys_openat(), except that it doesn't set the |
| 1012 | * O_LARGEFILE flag. |
| 1013 | */ |
Al Viro | f482e1b | 2012-12-25 20:24:38 -0500 | [diff] [blame] | 1014 | COMPAT_SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags, umode_t, mode) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1015 | { |
| 1016 | return do_sys_open(dfd, filename, flags, mode); |
Miklos Szeredi | e922efc | 2005-09-06 15:18:25 -0700 | [diff] [blame] | 1017 | } |
| 1018 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | #define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t)) |
| 1020 | |
Thomas Gleixner | b773ad4 | 2008-08-31 08:16:57 -0700 | [diff] [blame] | 1021 | static int poll_select_copy_remaining(struct timespec *end_time, void __user *p, |
| 1022 | int timeval, int ret) |
| 1023 | { |
| 1024 | struct timespec ts; |
| 1025 | |
| 1026 | if (!p) |
| 1027 | return ret; |
| 1028 | |
| 1029 | if (current->personality & STICKY_TIMEOUTS) |
| 1030 | goto sticky; |
| 1031 | |
| 1032 | /* No update for zero timeout */ |
| 1033 | if (!end_time->tv_sec && !end_time->tv_nsec) |
| 1034 | return ret; |
| 1035 | |
| 1036 | ktime_get_ts(&ts); |
| 1037 | ts = timespec_sub(*end_time, ts); |
| 1038 | if (ts.tv_sec < 0) |
| 1039 | ts.tv_sec = ts.tv_nsec = 0; |
| 1040 | |
| 1041 | if (timeval) { |
| 1042 | struct compat_timeval rtv; |
| 1043 | |
| 1044 | rtv.tv_sec = ts.tv_sec; |
| 1045 | rtv.tv_usec = ts.tv_nsec / NSEC_PER_USEC; |
| 1046 | |
| 1047 | if (!copy_to_user(p, &rtv, sizeof(rtv))) |
| 1048 | return ret; |
| 1049 | } else { |
| 1050 | struct compat_timespec rts; |
| 1051 | |
| 1052 | rts.tv_sec = ts.tv_sec; |
| 1053 | rts.tv_nsec = ts.tv_nsec; |
| 1054 | |
| 1055 | if (!copy_to_user(p, &rts, sizeof(rts))) |
| 1056 | return ret; |
| 1057 | } |
| 1058 | /* |
| 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. |
| 1064 | */ |
| 1065 | |
| 1066 | sticky: |
| 1067 | if (ret == -ERESTARTNOHAND) |
| 1068 | ret = -EINTR; |
| 1069 | return ret; |
| 1070 | } |
| 1071 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | /* |
| 1073 | * Ooo, nasty. We need here to frob 32-bit unsigned longs to |
| 1074 | * 64-bit unsigned longs. |
| 1075 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 1076 | static |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset, |
| 1078 | unsigned long *fdset) |
| 1079 | { |
Milind Arun Choudhary | 022a169 | 2007-05-08 00:29:02 -0700 | [diff] [blame] | 1080 | nr = DIV_ROUND_UP(nr, __COMPAT_NFDBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | if (ufdset) { |
| 1082 | unsigned long odd; |
| 1083 | |
| 1084 | if (!access_ok(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t))) |
| 1085 | return -EFAULT; |
| 1086 | |
| 1087 | odd = nr & 1UL; |
| 1088 | nr &= ~1UL; |
| 1089 | while (nr) { |
| 1090 | unsigned long h, l; |
Heiko Carstens | 7116e99 | 2006-12-06 20:36:36 -0800 | [diff] [blame] | 1091 | if (__get_user(l, ufdset) || __get_user(h, ufdset+1)) |
| 1092 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | ufdset += 2; |
| 1094 | *fdset++ = h << 32 | l; |
| 1095 | nr -= 2; |
| 1096 | } |
Heiko Carstens | 7116e99 | 2006-12-06 20:36:36 -0800 | [diff] [blame] | 1097 | if (odd && __get_user(*fdset, ufdset)) |
| 1098 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | } else { |
| 1100 | /* Tricky, must clear full unsigned long in the |
| 1101 | * kernel fdset at the end, this makes sure that |
| 1102 | * actually happens. |
| 1103 | */ |
| 1104 | memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t)); |
| 1105 | } |
| 1106 | return 0; |
| 1107 | } |
| 1108 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 1109 | static |
Heiko Carstens | 7116e99 | 2006-12-06 20:36:36 -0800 | [diff] [blame] | 1110 | int compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset, |
| 1111 | unsigned long *fdset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | { |
| 1113 | unsigned long odd; |
Milind Arun Choudhary | 022a169 | 2007-05-08 00:29:02 -0700 | [diff] [blame] | 1114 | nr = DIV_ROUND_UP(nr, __COMPAT_NFDBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | |
| 1116 | if (!ufdset) |
Heiko Carstens | 7116e99 | 2006-12-06 20:36:36 -0800 | [diff] [blame] | 1117 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | |
| 1119 | odd = nr & 1UL; |
| 1120 | nr &= ~1UL; |
| 1121 | while (nr) { |
| 1122 | unsigned long h, l; |
| 1123 | l = *fdset++; |
| 1124 | h = l >> 32; |
Heiko Carstens | 7116e99 | 2006-12-06 20:36:36 -0800 | [diff] [blame] | 1125 | if (__put_user(l, ufdset) || __put_user(h, ufdset+1)) |
| 1126 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | ufdset += 2; |
| 1128 | nr -= 2; |
| 1129 | } |
Heiko Carstens | 7116e99 | 2006-12-06 20:36:36 -0800 | [diff] [blame] | 1130 | if (odd && __put_user(*fdset, ufdset)) |
| 1131 | return -EFAULT; |
| 1132 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | |
| 1136 | /* |
| 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 |
| 1139 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | |
| 1141 | /* |
| 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. |
| 1145 | * |
| 1146 | * Update: ERESTARTSYS breaks at least the xview clock binary, so |
| 1147 | * I'm trying ERESTARTNOHAND which restart only when you want to. |
| 1148 | */ |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1149 | int compat_core_sys_select(int n, compat_ulong_t __user *inp, |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame] | 1150 | compat_ulong_t __user *outp, compat_ulong_t __user *exp, |
| 1151 | struct timespec *end_time) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | { |
| 1153 | fd_set_bits fds; |
Badari Pulavarty | 6087b2d | 2007-05-23 13:57:45 -0700 | [diff] [blame] | 1154 | void *bits; |
Vadim Lobanov | bbea9f6 | 2006-12-10 02:21:12 -0800 | [diff] [blame] | 1155 | int size, max_fds, ret = -EINVAL; |
Linus Torvalds | a4531ed | 2005-09-09 15:10:52 -0700 | [diff] [blame] | 1156 | struct fdtable *fdt; |
Badari Pulavarty | 6087b2d | 2007-05-23 13:57:45 -0700 | [diff] [blame] | 1157 | long stack_fds[SELECT_STACK_ALLOC/sizeof(long)]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | if (n < 0) |
| 1160 | goto out_nofds; |
| 1161 | |
Vadim Lobanov | bbea9f6 | 2006-12-10 02:21:12 -0800 | [diff] [blame] | 1162 | /* max_fds can increase, so grab it once to avoid race */ |
Linus Torvalds | ac5b8b6 | 2005-09-09 15:42:34 -0700 | [diff] [blame] | 1163 | rcu_read_lock(); |
Linus Torvalds | a4531ed | 2005-09-09 15:10:52 -0700 | [diff] [blame] | 1164 | fdt = files_fdtable(current->files); |
Vadim Lobanov | bbea9f6 | 2006-12-10 02:21:12 -0800 | [diff] [blame] | 1165 | max_fds = fdt->max_fds; |
Linus Torvalds | ac5b8b6 | 2005-09-09 15:42:34 -0700 | [diff] [blame] | 1166 | rcu_read_unlock(); |
Vadim Lobanov | bbea9f6 | 2006-12-10 02:21:12 -0800 | [diff] [blame] | 1167 | if (n > max_fds) |
| 1168 | n = max_fds; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | |
| 1170 | /* |
| 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 |
| 1173 | * long-words. |
| 1174 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | size = FDS_BYTES(n); |
Badari Pulavarty | 6087b2d | 2007-05-23 13:57:45 -0700 | [diff] [blame] | 1176 | bits = stack_fds; |
| 1177 | if (size > sizeof(stack_fds) / 6) { |
| 1178 | bits = kmalloc(6 * size, GFP_KERNEL); |
| 1179 | ret = -ENOMEM; |
| 1180 | if (!bits) |
| 1181 | goto out_nofds; |
| 1182 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
| 1189 | |
| 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))) |
| 1193 | goto out; |
| 1194 | zero_fd_set(n, fds.res_in); |
| 1195 | zero_fd_set(n, fds.res_out); |
| 1196 | zero_fd_set(n, fds.res_ex); |
| 1197 | |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame] | 1198 | ret = do_select(n, &fds, end_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | |
| 1200 | if (ret < 0) |
| 1201 | goto out; |
| 1202 | if (!ret) { |
| 1203 | ret = -ERESTARTNOHAND; |
| 1204 | if (signal_pending(current)) |
| 1205 | goto out; |
| 1206 | ret = 0; |
| 1207 | } |
| 1208 | |
Heiko Carstens | 7116e99 | 2006-12-06 20:36:36 -0800 | [diff] [blame] | 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)) |
| 1212 | ret = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | out: |
Badari Pulavarty | 6087b2d | 2007-05-23 13:57:45 -0700 | [diff] [blame] | 1214 | if (bits != stack_fds) |
| 1215 | kfree(bits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | out_nofds: |
| 1217 | return ret; |
| 1218 | } |
| 1219 | |
Heiko Carstens | 625b1d7 | 2014-03-04 10:53:50 +0100 | [diff] [blame] | 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) |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1223 | { |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame] | 1224 | struct timespec end_time, *to = NULL; |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1225 | struct compat_timeval tv; |
| 1226 | int ret; |
| 1227 | |
| 1228 | if (tvp) { |
| 1229 | if (copy_from_user(&tv, tvp, sizeof(tv))) |
| 1230 | return -EFAULT; |
| 1231 | |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame] | 1232 | to = &end_time; |
Arjan van de Ven | 4d36a9e | 2008-10-25 12:41:41 -0700 | [diff] [blame] | 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)) |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1236 | return -EINVAL; |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1237 | } |
| 1238 | |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame] | 1239 | ret = compat_core_sys_select(n, inp, outp, exp, to); |
| 1240 | ret = poll_select_copy_remaining(&end_time, tvp, 1, ret); |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1241 | |
| 1242 | return ret; |
| 1243 | } |
| 1244 | |
Christoph Hellwig | 5d0e528 | 2010-03-10 15:21:13 -0800 | [diff] [blame] | 1245 | struct compat_sel_arg_struct { |
| 1246 | compat_ulong_t n; |
| 1247 | compat_uptr_t inp; |
| 1248 | compat_uptr_t outp; |
| 1249 | compat_uptr_t exp; |
| 1250 | compat_uptr_t tvp; |
| 1251 | }; |
| 1252 | |
Heiko Carstens | 625b1d7 | 2014-03-04 10:53:50 +0100 | [diff] [blame] | 1253 | COMPAT_SYSCALL_DEFINE1(old_select, struct compat_sel_arg_struct __user *, arg) |
Christoph Hellwig | 5d0e528 | 2010-03-10 15:21:13 -0800 | [diff] [blame] | 1254 | { |
| 1255 | struct compat_sel_arg_struct a; |
| 1256 | |
| 1257 | if (copy_from_user(&a, arg, sizeof(a))) |
| 1258 | return -EFAULT; |
| 1259 | return compat_sys_select(a.n, compat_ptr(a.inp), compat_ptr(a.outp), |
| 1260 | compat_ptr(a.exp), compat_ptr(a.tvp)); |
| 1261 | } |
| 1262 | |
Heiko Carstens | c9da9f2 | 2009-01-14 14:13:57 +0100 | [diff] [blame] | 1263 | static long do_compat_pselect(int n, compat_ulong_t __user *inp, |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 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) |
| 1267 | { |
| 1268 | compat_sigset_t ss32; |
| 1269 | sigset_t ksigmask, sigsaved; |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1270 | struct compat_timespec ts; |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame] | 1271 | struct timespec end_time, *to = NULL; |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1272 | int ret; |
| 1273 | |
| 1274 | if (tsp) { |
| 1275 | if (copy_from_user(&ts, tsp, sizeof(ts))) |
| 1276 | return -EFAULT; |
| 1277 | |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame] | 1278 | to = &end_time; |
| 1279 | if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec)) |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1280 | return -EINVAL; |
| 1281 | } |
| 1282 | |
| 1283 | if (sigmask) { |
| 1284 | if (sigsetsize != sizeof(compat_sigset_t)) |
| 1285 | return -EINVAL; |
| 1286 | if (copy_from_user(&ss32, sigmask, sizeof(ss32))) |
| 1287 | return -EFAULT; |
| 1288 | sigset_from_compat(&ksigmask, &ss32); |
| 1289 | |
| 1290 | sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP)); |
| 1291 | sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved); |
| 1292 | } |
| 1293 | |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame] | 1294 | ret = compat_core_sys_select(n, inp, outp, exp, to); |
| 1295 | ret = poll_select_copy_remaining(&end_time, tsp, 0, ret); |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1296 | |
| 1297 | if (ret == -ERESTARTNOHAND) { |
| 1298 | /* |
| 1299 | * Don't restore the signal mask yet. Let do_signal() deliver |
| 1300 | * the signal on the way back to userspace, before the signal |
| 1301 | * mask is restored. |
| 1302 | */ |
| 1303 | if (sigmask) { |
| 1304 | memcpy(¤t->saved_sigmask, &sigsaved, |
| 1305 | sizeof(sigsaved)); |
Roland McGrath | 4e4c22c | 2008-04-30 00:53:06 -0700 | [diff] [blame] | 1306 | set_restore_sigmask(); |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1307 | } |
| 1308 | } else if (sigmask) |
| 1309 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); |
| 1310 | |
| 1311 | return ret; |
| 1312 | } |
| 1313 | |
Heiko Carstens | 625b1d7 | 2014-03-04 10:53:50 +0100 | [diff] [blame] | 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) |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1317 | { |
| 1318 | compat_size_t sigsetsize = 0; |
| 1319 | compat_uptr_t up = 0; |
| 1320 | |
| 1321 | if (sig) { |
| 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)))) |
| 1327 | return -EFAULT; |
| 1328 | } |
Heiko Carstens | c9da9f2 | 2009-01-14 14:13:57 +0100 | [diff] [blame] | 1329 | return do_compat_pselect(n, inp, outp, exp, tsp, compat_ptr(up), |
| 1330 | sigsetsize); |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1331 | } |
| 1332 | |
Heiko Carstens | 625b1d7 | 2014-03-04 10:53:50 +0100 | [diff] [blame] | 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) |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1336 | { |
| 1337 | compat_sigset_t ss32; |
| 1338 | sigset_t ksigmask, sigsaved; |
| 1339 | struct compat_timespec ts; |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame] | 1340 | struct timespec end_time, *to = NULL; |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1341 | int ret; |
| 1342 | |
| 1343 | if (tsp) { |
| 1344 | if (copy_from_user(&ts, tsp, sizeof(ts))) |
| 1345 | return -EFAULT; |
| 1346 | |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame] | 1347 | to = &end_time; |
| 1348 | if (poll_select_set_timeout(to, ts.tv_sec, ts.tv_nsec)) |
| 1349 | return -EINVAL; |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | if (sigmask) { |
Alexey Dobriyan | 3835a9b | 2006-05-15 09:44:27 -0700 | [diff] [blame] | 1353 | if (sigsetsize != sizeof(compat_sigset_t)) |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1354 | return -EINVAL; |
| 1355 | if (copy_from_user(&ss32, sigmask, sizeof(ss32))) |
| 1356 | return -EFAULT; |
| 1357 | sigset_from_compat(&ksigmask, &ss32); |
| 1358 | |
| 1359 | sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP)); |
| 1360 | sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved); |
| 1361 | } |
| 1362 | |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame] | 1363 | ret = do_sys_poll(ufds, nfds, to); |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1364 | |
| 1365 | /* We can restart this syscall, usually */ |
| 1366 | if (ret == -EINTR) { |
| 1367 | /* |
| 1368 | * Don't restore the signal mask yet. Let do_signal() deliver |
| 1369 | * the signal on the way back to userspace, before the signal |
| 1370 | * mask is restored. |
| 1371 | */ |
| 1372 | if (sigmask) { |
| 1373 | memcpy(¤t->saved_sigmask, &sigsaved, |
| 1374 | sizeof(sigsaved)); |
Roland McGrath | 4e4c22c | 2008-04-30 00:53:06 -0700 | [diff] [blame] | 1375 | set_restore_sigmask(); |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1376 | } |
| 1377 | ret = -ERESTARTNOHAND; |
| 1378 | } else if (sigmask) |
| 1379 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); |
| 1380 | |
Arjan van de Ven | 8ff3e8e | 2008-08-31 08:26:40 -0700 | [diff] [blame] | 1381 | ret = poll_select_copy_remaining(&end_time, tsp, 0, ret); |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1382 | |
| 1383 | return ret; |
| 1384 | } |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1385 | |
Aneesh Kumar K.V | becfd1f | 2011-01-29 18:43:26 +0530 | [diff] [blame] | 1386 | #ifdef CONFIG_FHANDLE |
| 1387 | /* |
| 1388 | * Exactly like fs/open.c:sys_open_by_handle_at(), except that it |
| 1389 | * doesn't set the O_LARGEFILE flag. |
| 1390 | */ |
Al Viro | f482e1b | 2012-12-25 20:24:38 -0500 | [diff] [blame] | 1391 | COMPAT_SYSCALL_DEFINE3(open_by_handle_at, int, mountdirfd, |
| 1392 | struct file_handle __user *, handle, int, flags) |
Aneesh Kumar K.V | becfd1f | 2011-01-29 18:43:26 +0530 | [diff] [blame] | 1393 | { |
| 1394 | return do_handle_open(mountdirfd, handle, flags); |
| 1395 | } |
| 1396 | #endif |