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 |
| 11 | * Copyright (C) 2003 Pavel Machek (pavel@suse.cz) |
| 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 | |
| 18 | #include <linux/linkage.h> |
| 19 | #include <linux/compat.h> |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/time.h> |
| 22 | #include <linux/fs.h> |
| 23 | #include <linux/fcntl.h> |
| 24 | #include <linux/namei.h> |
| 25 | #include <linux/file.h> |
| 26 | #include <linux/vfs.h> |
| 27 | #include <linux/ioctl32.h> |
| 28 | #include <linux/ioctl.h> |
| 29 | #include <linux/init.h> |
| 30 | #include <linux/sockios.h> /* for SIOCDEVPRIVATE */ |
| 31 | #include <linux/smb.h> |
| 32 | #include <linux/smb_mount.h> |
| 33 | #include <linux/ncp_mount.h> |
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/smp_lock.h> |
| 36 | #include <linux/syscalls.h> |
| 37 | #include <linux/ctype.h> |
| 38 | #include <linux/module.h> |
| 39 | #include <linux/dirent.h> |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 40 | #include <linux/fsnotify.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/highuid.h> |
| 42 | #include <linux/sunrpc/svc.h> |
| 43 | #include <linux/nfsd/nfsd.h> |
| 44 | #include <linux/nfsd/syscall.h> |
| 45 | #include <linux/personality.h> |
| 46 | #include <linux/rwsem.h> |
Jay Lan | 8f0ab51 | 2006-09-30 23:28:59 -0700 | [diff] [blame] | 47 | #include <linux/tsacct_kern.h> |
David S. Miller | 4a805e8 | 2005-09-14 21:40:00 -0700 | [diff] [blame] | 48 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 50 | #include <net/sock.h> /* siocdevprivate_ioctl */ |
| 51 | |
| 52 | #include <asm/uaccess.h> |
| 53 | #include <asm/mmu_context.h> |
| 54 | #include <asm/ioctls.h> |
David Howells | 07f3f05 | 2006-09-30 20:52:18 +0200 | [diff] [blame] | 55 | #include "internal.h" |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 56 | |
Andi Kleen | bebfa10 | 2006-06-26 13:56:52 +0200 | [diff] [blame] | 57 | int compat_log = 1; |
| 58 | |
| 59 | int compat_printk(const char *fmt, ...) |
| 60 | { |
| 61 | va_list ap; |
| 62 | int ret; |
| 63 | if (!compat_log) |
| 64 | return 0; |
| 65 | va_start(ap, fmt); |
| 66 | ret = vprintk(fmt, ap); |
| 67 | va_end(ap); |
| 68 | return ret; |
| 69 | } |
| 70 | |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 71 | #include "read_write.h" |
| 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | /* |
| 74 | * Not all architectures have sys_utime, so implement this in terms |
| 75 | * of sys_utimes. |
| 76 | */ |
| 77 | asmlinkage long compat_sys_utime(char __user *filename, struct compat_utimbuf __user *t) |
| 78 | { |
| 79 | struct timeval tv[2]; |
| 80 | |
| 81 | if (t) { |
| 82 | if (get_user(tv[0].tv_sec, &t->actime) || |
| 83 | get_user(tv[1].tv_sec, &t->modtime)) |
| 84 | return -EFAULT; |
| 85 | tv[0].tv_usec = 0; |
| 86 | tv[1].tv_usec = 0; |
| 87 | } |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 88 | return do_utimes(AT_FDCWD, filename, t ? tv : NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Stephen Rothwell | 9ad11ab | 2006-02-02 16:11:51 +1100 | [diff] [blame] | 91 | asmlinkage long compat_sys_futimesat(unsigned int dfd, char __user *filename, struct compat_timeval __user *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | { |
| 93 | struct timeval tv[2]; |
| 94 | |
Stephen Rothwell | 9ad11ab | 2006-02-02 16:11:51 +1100 | [diff] [blame] | 95 | if (t) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | if (get_user(tv[0].tv_sec, &t[0].tv_sec) || |
| 97 | get_user(tv[0].tv_usec, &t[0].tv_usec) || |
| 98 | get_user(tv[1].tv_sec, &t[1].tv_sec) || |
| 99 | get_user(tv[1].tv_usec, &t[1].tv_usec)) |
Stephen Rothwell | 9ad11ab | 2006-02-02 16:11:51 +1100 | [diff] [blame] | 100 | return -EFAULT; |
| 101 | } |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 102 | return do_utimes(dfd, filename, t ? tv : NULL); |
| 103 | } |
| 104 | |
| 105 | asmlinkage long compat_sys_utimes(char __user *filename, struct compat_timeval __user *t) |
| 106 | { |
| 107 | return compat_sys_futimesat(AT_FDCWD, filename, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | asmlinkage long compat_sys_newstat(char __user * filename, |
| 111 | struct compat_stat __user *statbuf) |
| 112 | { |
| 113 | struct kstat stat; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 114 | int error = vfs_stat_fd(AT_FDCWD, filename, &stat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | if (!error) |
| 117 | error = cp_compat_stat(&stat, statbuf); |
| 118 | return error; |
| 119 | } |
| 120 | |
| 121 | asmlinkage long compat_sys_newlstat(char __user * filename, |
| 122 | struct compat_stat __user *statbuf) |
| 123 | { |
| 124 | struct kstat stat; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 125 | int error = vfs_lstat_fd(AT_FDCWD, filename, &stat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
| 127 | if (!error) |
| 128 | error = cp_compat_stat(&stat, statbuf); |
| 129 | return error; |
| 130 | } |
| 131 | |
Kyle McMartin | 82d821d | 2006-03-24 03:18:20 -0800 | [diff] [blame] | 132 | #ifndef __ARCH_WANT_STAT64 |
Stephen Rothwell | 9ad11ab | 2006-02-02 16:11:51 +1100 | [diff] [blame] | 133 | asmlinkage long compat_sys_newfstatat(unsigned int dfd, char __user *filename, |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 134 | struct compat_stat __user *statbuf, int flag) |
| 135 | { |
| 136 | struct kstat stat; |
| 137 | int error = -EINVAL; |
| 138 | |
| 139 | if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0) |
| 140 | goto out; |
| 141 | |
| 142 | if (flag & AT_SYMLINK_NOFOLLOW) |
| 143 | error = vfs_lstat_fd(dfd, filename, &stat); |
| 144 | else |
| 145 | error = vfs_stat_fd(dfd, filename, &stat); |
| 146 | |
| 147 | if (!error) |
| 148 | error = cp_compat_stat(&stat, statbuf); |
| 149 | |
| 150 | out: |
| 151 | return error; |
| 152 | } |
Kyle McMartin | 82d821d | 2006-03-24 03:18:20 -0800 | [diff] [blame] | 153 | #endif |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 154 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | asmlinkage long compat_sys_newfstat(unsigned int fd, |
| 156 | struct compat_stat __user * statbuf) |
| 157 | { |
| 158 | struct kstat stat; |
| 159 | int error = vfs_fstat(fd, &stat); |
| 160 | |
| 161 | if (!error) |
| 162 | error = cp_compat_stat(&stat, statbuf); |
| 163 | return error; |
| 164 | } |
| 165 | |
| 166 | static int put_compat_statfs(struct compat_statfs __user *ubuf, struct kstatfs *kbuf) |
| 167 | { |
| 168 | |
| 169 | if (sizeof ubuf->f_blocks == 4) { |
| 170 | if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail) & |
| 171 | 0xffffffff00000000ULL) |
| 172 | return -EOVERFLOW; |
| 173 | /* f_files and f_ffree may be -1; it's okay |
| 174 | * to stuff that into 32 bits */ |
| 175 | if (kbuf->f_files != 0xffffffffffffffffULL |
| 176 | && (kbuf->f_files & 0xffffffff00000000ULL)) |
| 177 | return -EOVERFLOW; |
| 178 | if (kbuf->f_ffree != 0xffffffffffffffffULL |
| 179 | && (kbuf->f_ffree & 0xffffffff00000000ULL)) |
| 180 | return -EOVERFLOW; |
| 181 | } |
| 182 | if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) || |
| 183 | __put_user(kbuf->f_type, &ubuf->f_type) || |
| 184 | __put_user(kbuf->f_bsize, &ubuf->f_bsize) || |
| 185 | __put_user(kbuf->f_blocks, &ubuf->f_blocks) || |
| 186 | __put_user(kbuf->f_bfree, &ubuf->f_bfree) || |
| 187 | __put_user(kbuf->f_bavail, &ubuf->f_bavail) || |
| 188 | __put_user(kbuf->f_files, &ubuf->f_files) || |
| 189 | __put_user(kbuf->f_ffree, &ubuf->f_ffree) || |
| 190 | __put_user(kbuf->f_namelen, &ubuf->f_namelen) || |
| 191 | __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) || |
| 192 | __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) || |
| 193 | __put_user(kbuf->f_frsize, &ubuf->f_frsize) || |
| 194 | __put_user(0, &ubuf->f_spare[0]) || |
| 195 | __put_user(0, &ubuf->f_spare[1]) || |
| 196 | __put_user(0, &ubuf->f_spare[2]) || |
| 197 | __put_user(0, &ubuf->f_spare[3]) || |
| 198 | __put_user(0, &ubuf->f_spare[4])) |
| 199 | return -EFAULT; |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * The following statfs calls are copies of code from fs/open.c and |
| 205 | * should be checked against those from time to time |
| 206 | */ |
| 207 | asmlinkage long compat_sys_statfs(const char __user *path, struct compat_statfs __user *buf) |
| 208 | { |
| 209 | struct nameidata nd; |
| 210 | int error; |
| 211 | |
| 212 | error = user_path_walk(path, &nd); |
| 213 | if (!error) { |
| 214 | struct kstatfs tmp; |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 215 | error = vfs_statfs(nd.dentry, &tmp); |
David Gibson | 86e07ce | 2005-11-21 21:32:23 -0800 | [diff] [blame] | 216 | if (!error) |
| 217 | error = put_compat_statfs(buf, &tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | path_release(&nd); |
| 219 | } |
| 220 | return error; |
| 221 | } |
| 222 | |
| 223 | asmlinkage long compat_sys_fstatfs(unsigned int fd, struct compat_statfs __user *buf) |
| 224 | { |
| 225 | struct file * file; |
| 226 | struct kstatfs tmp; |
| 227 | int error; |
| 228 | |
| 229 | error = -EBADF; |
| 230 | file = fget(fd); |
| 231 | if (!file) |
| 232 | goto out; |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 233 | error = vfs_statfs(file->f_dentry, &tmp); |
David Gibson | 86e07ce | 2005-11-21 21:32:23 -0800 | [diff] [blame] | 234 | if (!error) |
| 235 | error = put_compat_statfs(buf, &tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | fput(file); |
| 237 | out: |
| 238 | return error; |
| 239 | } |
| 240 | |
| 241 | static int put_compat_statfs64(struct compat_statfs64 __user *ubuf, struct kstatfs *kbuf) |
| 242 | { |
| 243 | if (sizeof ubuf->f_blocks == 4) { |
| 244 | if ((kbuf->f_blocks | kbuf->f_bfree | kbuf->f_bavail) & |
| 245 | 0xffffffff00000000ULL) |
| 246 | return -EOVERFLOW; |
| 247 | /* f_files and f_ffree may be -1; it's okay |
| 248 | * to stuff that into 32 bits */ |
| 249 | if (kbuf->f_files != 0xffffffffffffffffULL |
| 250 | && (kbuf->f_files & 0xffffffff00000000ULL)) |
| 251 | return -EOVERFLOW; |
| 252 | if (kbuf->f_ffree != 0xffffffffffffffffULL |
| 253 | && (kbuf->f_ffree & 0xffffffff00000000ULL)) |
| 254 | return -EOVERFLOW; |
| 255 | } |
| 256 | if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)) || |
| 257 | __put_user(kbuf->f_type, &ubuf->f_type) || |
| 258 | __put_user(kbuf->f_bsize, &ubuf->f_bsize) || |
| 259 | __put_user(kbuf->f_blocks, &ubuf->f_blocks) || |
| 260 | __put_user(kbuf->f_bfree, &ubuf->f_bfree) || |
| 261 | __put_user(kbuf->f_bavail, &ubuf->f_bavail) || |
| 262 | __put_user(kbuf->f_files, &ubuf->f_files) || |
| 263 | __put_user(kbuf->f_ffree, &ubuf->f_ffree) || |
| 264 | __put_user(kbuf->f_namelen, &ubuf->f_namelen) || |
| 265 | __put_user(kbuf->f_fsid.val[0], &ubuf->f_fsid.val[0]) || |
| 266 | __put_user(kbuf->f_fsid.val[1], &ubuf->f_fsid.val[1]) || |
| 267 | __put_user(kbuf->f_frsize, &ubuf->f_frsize)) |
| 268 | return -EFAULT; |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | asmlinkage long compat_sys_statfs64(const char __user *path, compat_size_t sz, struct compat_statfs64 __user *buf) |
| 273 | { |
| 274 | struct nameidata nd; |
| 275 | int error; |
| 276 | |
| 277 | if (sz != sizeof(*buf)) |
| 278 | return -EINVAL; |
| 279 | |
| 280 | error = user_path_walk(path, &nd); |
| 281 | if (!error) { |
| 282 | struct kstatfs tmp; |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 283 | error = vfs_statfs(nd.dentry, &tmp); |
David Gibson | 86e07ce | 2005-11-21 21:32:23 -0800 | [diff] [blame] | 284 | if (!error) |
| 285 | error = put_compat_statfs64(buf, &tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | path_release(&nd); |
| 287 | } |
| 288 | return error; |
| 289 | } |
| 290 | |
| 291 | asmlinkage long compat_sys_fstatfs64(unsigned int fd, compat_size_t sz, struct compat_statfs64 __user *buf) |
| 292 | { |
| 293 | struct file * file; |
| 294 | struct kstatfs tmp; |
| 295 | int error; |
| 296 | |
| 297 | if (sz != sizeof(*buf)) |
| 298 | return -EINVAL; |
| 299 | |
| 300 | error = -EBADF; |
| 301 | file = fget(fd); |
| 302 | if (!file) |
| 303 | goto out; |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 304 | error = vfs_statfs(file->f_dentry, &tmp); |
David Gibson | 86e07ce | 2005-11-21 21:32:23 -0800 | [diff] [blame] | 305 | if (!error) |
| 306 | error = put_compat_statfs64(buf, &tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | fput(file); |
| 308 | out: |
| 309 | return error; |
| 310 | } |
| 311 | |
| 312 | /* ioctl32 stuff, used by sparc64, parisc, s390x, ppc64, x86_64, MIPS */ |
| 313 | |
| 314 | #define IOCTL_HASHSIZE 256 |
| 315 | static struct ioctl_trans *ioctl32_hash_table[IOCTL_HASHSIZE]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | static inline unsigned long ioctl32_hash(unsigned long cmd) |
| 318 | { |
| 319 | return (((cmd >> 6) ^ (cmd >> 4) ^ cmd)) % IOCTL_HASHSIZE; |
| 320 | } |
| 321 | |
| 322 | static void ioctl32_insert_translation(struct ioctl_trans *trans) |
| 323 | { |
| 324 | unsigned long hash; |
| 325 | struct ioctl_trans *t; |
| 326 | |
| 327 | hash = ioctl32_hash (trans->cmd); |
| 328 | if (!ioctl32_hash_table[hash]) |
| 329 | ioctl32_hash_table[hash] = trans; |
| 330 | else { |
| 331 | t = ioctl32_hash_table[hash]; |
| 332 | while (t->next) |
| 333 | t = t->next; |
| 334 | trans->next = NULL; |
| 335 | t->next = trans; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | static int __init init_sys32_ioctl(void) |
| 340 | { |
| 341 | int i; |
| 342 | |
| 343 | for (i = 0; i < ioctl_table_size; i++) { |
| 344 | if (ioctl_start[i].next != 0) { |
| 345 | printk("ioctl translation %d bad\n",i); |
| 346 | return -1; |
| 347 | } |
| 348 | |
| 349 | ioctl32_insert_translation(&ioctl_start[i]); |
| 350 | } |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | __initcall(init_sys32_ioctl); |
| 355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | static void compat_ioctl_error(struct file *filp, unsigned int fd, |
| 357 | unsigned int cmd, unsigned long arg) |
| 358 | { |
| 359 | char buf[10]; |
| 360 | char *fn = "?"; |
| 361 | char *path; |
| 362 | |
| 363 | /* find the name of the device. */ |
| 364 | path = (char *)__get_free_page(GFP_KERNEL); |
| 365 | if (path) { |
| 366 | fn = d_path(filp->f_dentry, filp->f_vfsmnt, path, PAGE_SIZE); |
| 367 | if (IS_ERR(fn)) |
| 368 | fn = "?"; |
| 369 | } |
| 370 | |
| 371 | sprintf(buf,"'%c'", (cmd>>24) & 0x3f); |
| 372 | if (!isprint(buf[1])) |
| 373 | sprintf(buf, "%02x", buf[1]); |
Andi Kleen | bebfa10 | 2006-06-26 13:56:52 +0200 | [diff] [blame] | 374 | compat_printk("ioctl32(%s:%d): Unknown cmd fd(%d) " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | "cmd(%08x){%s} arg(%08x) on %s\n", |
| 376 | current->comm, current->pid, |
| 377 | (int)fd, (unsigned int)cmd, buf, |
| 378 | (unsigned int)arg, fn); |
| 379 | |
| 380 | if (path) |
| 381 | free_page((unsigned long)path); |
| 382 | } |
| 383 | |
| 384 | asmlinkage long compat_sys_ioctl(unsigned int fd, unsigned int cmd, |
| 385 | unsigned long arg) |
| 386 | { |
| 387 | struct file *filp; |
| 388 | int error = -EBADF; |
| 389 | struct ioctl_trans *t; |
| 390 | int fput_needed; |
| 391 | |
| 392 | filp = fget_light(fd, &fput_needed); |
| 393 | if (!filp) |
| 394 | goto out; |
| 395 | |
| 396 | /* RED-PEN how should LSM module know it's handling 32bit? */ |
| 397 | error = security_file_ioctl(filp, cmd, arg); |
| 398 | if (error) |
| 399 | goto out_fput; |
| 400 | |
| 401 | /* |
| 402 | * To allow the compat_ioctl handlers to be self contained |
| 403 | * we need to check the common ioctls here first. |
| 404 | * Just handle them with the standard handlers below. |
| 405 | */ |
| 406 | switch (cmd) { |
| 407 | case FIOCLEX: |
| 408 | case FIONCLEX: |
| 409 | case FIONBIO: |
| 410 | case FIOASYNC: |
| 411 | case FIOQSIZE: |
| 412 | break; |
| 413 | |
| 414 | case FIBMAP: |
| 415 | case FIGETBSZ: |
| 416 | case FIONREAD: |
| 417 | if (S_ISREG(filp->f_dentry->d_inode->i_mode)) |
| 418 | break; |
| 419 | /*FALL THROUGH*/ |
| 420 | |
| 421 | default: |
| 422 | if (filp->f_op && filp->f_op->compat_ioctl) { |
| 423 | error = filp->f_op->compat_ioctl(filp, cmd, arg); |
| 424 | if (error != -ENOIOCTLCMD) |
| 425 | goto out_fput; |
| 426 | } |
| 427 | |
| 428 | if (!filp->f_op || |
| 429 | (!filp->f_op->ioctl && !filp->f_op->unlocked_ioctl)) |
| 430 | goto do_ioctl; |
| 431 | break; |
| 432 | } |
| 433 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | for (t = ioctl32_hash_table[ioctl32_hash(cmd)]; t; t = t->next) { |
| 435 | if (t->cmd == cmd) |
| 436 | goto found_handler; |
| 437 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | |
| 439 | if (S_ISSOCK(filp->f_dentry->d_inode->i_mode) && |
| 440 | cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) { |
| 441 | error = siocdevprivate_ioctl(fd, cmd, arg); |
| 442 | } else { |
| 443 | static int count; |
| 444 | |
| 445 | if (++count <= 50) |
| 446 | compat_ioctl_error(filp, fd, cmd, arg); |
| 447 | error = -EINVAL; |
| 448 | } |
| 449 | |
| 450 | goto out_fput; |
| 451 | |
| 452 | found_handler: |
| 453 | if (t->handler) { |
| 454 | lock_kernel(); |
| 455 | error = t->handler(fd, cmd, arg, filp); |
| 456 | unlock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | goto out_fput; |
| 458 | } |
| 459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | do_ioctl: |
| 461 | error = vfs_ioctl(filp, fd, cmd, arg); |
| 462 | out_fput: |
| 463 | fput_light(filp, fput_needed); |
| 464 | out: |
| 465 | return error; |
| 466 | } |
| 467 | |
| 468 | static int get_compat_flock(struct flock *kfl, struct compat_flock __user *ufl) |
| 469 | { |
| 470 | if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) || |
| 471 | __get_user(kfl->l_type, &ufl->l_type) || |
| 472 | __get_user(kfl->l_whence, &ufl->l_whence) || |
| 473 | __get_user(kfl->l_start, &ufl->l_start) || |
| 474 | __get_user(kfl->l_len, &ufl->l_len) || |
| 475 | __get_user(kfl->l_pid, &ufl->l_pid)) |
| 476 | return -EFAULT; |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | static int put_compat_flock(struct flock *kfl, struct compat_flock __user *ufl) |
| 481 | { |
| 482 | if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) || |
| 483 | __put_user(kfl->l_type, &ufl->l_type) || |
| 484 | __put_user(kfl->l_whence, &ufl->l_whence) || |
| 485 | __put_user(kfl->l_start, &ufl->l_start) || |
| 486 | __put_user(kfl->l_len, &ufl->l_len) || |
| 487 | __put_user(kfl->l_pid, &ufl->l_pid)) |
| 488 | return -EFAULT; |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | #ifndef HAVE_ARCH_GET_COMPAT_FLOCK64 |
| 493 | static int get_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl) |
| 494 | { |
| 495 | if (!access_ok(VERIFY_READ, ufl, sizeof(*ufl)) || |
| 496 | __get_user(kfl->l_type, &ufl->l_type) || |
| 497 | __get_user(kfl->l_whence, &ufl->l_whence) || |
| 498 | __get_user(kfl->l_start, &ufl->l_start) || |
| 499 | __get_user(kfl->l_len, &ufl->l_len) || |
| 500 | __get_user(kfl->l_pid, &ufl->l_pid)) |
| 501 | return -EFAULT; |
| 502 | return 0; |
| 503 | } |
| 504 | #endif |
| 505 | |
| 506 | #ifndef HAVE_ARCH_PUT_COMPAT_FLOCK64 |
| 507 | static int put_compat_flock64(struct flock *kfl, struct compat_flock64 __user *ufl) |
| 508 | { |
| 509 | if (!access_ok(VERIFY_WRITE, ufl, sizeof(*ufl)) || |
| 510 | __put_user(kfl->l_type, &ufl->l_type) || |
| 511 | __put_user(kfl->l_whence, &ufl->l_whence) || |
| 512 | __put_user(kfl->l_start, &ufl->l_start) || |
| 513 | __put_user(kfl->l_len, &ufl->l_len) || |
| 514 | __put_user(kfl->l_pid, &ufl->l_pid)) |
| 515 | return -EFAULT; |
| 516 | return 0; |
| 517 | } |
| 518 | #endif |
| 519 | |
| 520 | asmlinkage long compat_sys_fcntl64(unsigned int fd, unsigned int cmd, |
| 521 | unsigned long arg) |
| 522 | { |
| 523 | mm_segment_t old_fs; |
| 524 | struct flock f; |
| 525 | long ret; |
| 526 | |
| 527 | switch (cmd) { |
| 528 | case F_GETLK: |
| 529 | case F_SETLK: |
| 530 | case F_SETLKW: |
| 531 | ret = get_compat_flock(&f, compat_ptr(arg)); |
| 532 | if (ret != 0) |
| 533 | break; |
| 534 | old_fs = get_fs(); |
| 535 | set_fs(KERNEL_DS); |
| 536 | ret = sys_fcntl(fd, cmd, (unsigned long)&f); |
| 537 | set_fs(old_fs); |
| 538 | if (cmd == F_GETLK && ret == 0) { |
NeilBrown | 2520f14 | 2006-01-08 01:02:40 -0800 | [diff] [blame] | 539 | /* GETLK was successfule and we need to return the data... |
| 540 | * but it needs to fit in the compat structure. |
| 541 | * l_start shouldn't be too big, unless the original |
| 542 | * start + end is greater than COMPAT_OFF_T_MAX, in which |
| 543 | * case the app was asking for trouble, so we return |
| 544 | * -EOVERFLOW in that case. |
| 545 | * l_len could be too big, in which case we just truncate it, |
| 546 | * and only allow the app to see that part of the conflicting |
| 547 | * lock that might make sense to it anyway |
| 548 | */ |
| 549 | |
| 550 | if (f.l_start > COMPAT_OFF_T_MAX) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | ret = -EOVERFLOW; |
NeilBrown | 2520f14 | 2006-01-08 01:02:40 -0800 | [diff] [blame] | 552 | if (f.l_len > COMPAT_OFF_T_MAX) |
| 553 | f.l_len = COMPAT_OFF_T_MAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | if (ret == 0) |
| 555 | ret = put_compat_flock(&f, compat_ptr(arg)); |
| 556 | } |
| 557 | break; |
| 558 | |
| 559 | case F_GETLK64: |
| 560 | case F_SETLK64: |
| 561 | case F_SETLKW64: |
| 562 | ret = get_compat_flock64(&f, compat_ptr(arg)); |
| 563 | if (ret != 0) |
| 564 | break; |
| 565 | old_fs = get_fs(); |
| 566 | set_fs(KERNEL_DS); |
| 567 | ret = sys_fcntl(fd, (cmd == F_GETLK64) ? F_GETLK : |
| 568 | ((cmd == F_SETLK64) ? F_SETLK : F_SETLKW), |
| 569 | (unsigned long)&f); |
| 570 | set_fs(old_fs); |
| 571 | if (cmd == F_GETLK64 && ret == 0) { |
NeilBrown | 2520f14 | 2006-01-08 01:02:40 -0800 | [diff] [blame] | 572 | /* need to return lock information - see above for commentary */ |
| 573 | if (f.l_start > COMPAT_LOFF_T_MAX) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | ret = -EOVERFLOW; |
NeilBrown | 2520f14 | 2006-01-08 01:02:40 -0800 | [diff] [blame] | 575 | if (f.l_len > COMPAT_LOFF_T_MAX) |
| 576 | f.l_len = COMPAT_LOFF_T_MAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | if (ret == 0) |
| 578 | ret = put_compat_flock64(&f, compat_ptr(arg)); |
| 579 | } |
| 580 | break; |
| 581 | |
| 582 | default: |
| 583 | ret = sys_fcntl(fd, cmd, arg); |
| 584 | break; |
| 585 | } |
| 586 | return ret; |
| 587 | } |
| 588 | |
| 589 | asmlinkage long compat_sys_fcntl(unsigned int fd, unsigned int cmd, |
| 590 | unsigned long arg) |
| 591 | { |
| 592 | if ((cmd == F_GETLK64) || (cmd == F_SETLK64) || (cmd == F_SETLKW64)) |
| 593 | return -EINVAL; |
| 594 | return compat_sys_fcntl64(fd, cmd, arg); |
| 595 | } |
| 596 | |
| 597 | asmlinkage long |
| 598 | compat_sys_io_setup(unsigned nr_reqs, u32 __user *ctx32p) |
| 599 | { |
| 600 | long ret; |
| 601 | aio_context_t ctx64; |
| 602 | |
| 603 | mm_segment_t oldfs = get_fs(); |
| 604 | if (unlikely(get_user(ctx64, ctx32p))) |
| 605 | return -EFAULT; |
| 606 | |
| 607 | set_fs(KERNEL_DS); |
| 608 | /* The __user pointer cast is valid because of the set_fs() */ |
| 609 | ret = sys_io_setup(nr_reqs, (aio_context_t __user *) &ctx64); |
| 610 | set_fs(oldfs); |
| 611 | /* truncating is ok because it's a user address */ |
| 612 | if (!ret) |
| 613 | ret = put_user((u32) ctx64, ctx32p); |
| 614 | return ret; |
| 615 | } |
| 616 | |
| 617 | asmlinkage long |
| 618 | compat_sys_io_getevents(aio_context_t ctx_id, |
| 619 | unsigned long min_nr, |
| 620 | unsigned long nr, |
| 621 | struct io_event __user *events, |
| 622 | struct compat_timespec __user *timeout) |
| 623 | { |
| 624 | long ret; |
| 625 | struct timespec t; |
| 626 | struct timespec __user *ut = NULL; |
| 627 | |
| 628 | ret = -EFAULT; |
| 629 | if (unlikely(!access_ok(VERIFY_WRITE, events, |
| 630 | nr * sizeof(struct io_event)))) |
| 631 | goto out; |
| 632 | if (timeout) { |
| 633 | if (get_compat_timespec(&t, timeout)) |
| 634 | goto out; |
| 635 | |
| 636 | ut = compat_alloc_user_space(sizeof(*ut)); |
| 637 | if (copy_to_user(ut, &t, sizeof(t)) ) |
| 638 | goto out; |
| 639 | } |
| 640 | ret = sys_io_getevents(ctx_id, min_nr, nr, events, ut); |
| 641 | out: |
| 642 | return ret; |
| 643 | } |
| 644 | |
| 645 | static inline long |
| 646 | copy_iocb(long nr, u32 __user *ptr32, struct iocb __user * __user *ptr64) |
| 647 | { |
| 648 | compat_uptr_t uptr; |
| 649 | int i; |
| 650 | |
| 651 | for (i = 0; i < nr; ++i) { |
| 652 | if (get_user(uptr, ptr32 + i)) |
| 653 | return -EFAULT; |
| 654 | if (put_user(compat_ptr(uptr), ptr64 + i)) |
| 655 | return -EFAULT; |
| 656 | } |
| 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | #define MAX_AIO_SUBMITS (PAGE_SIZE/sizeof(struct iocb *)) |
| 661 | |
| 662 | asmlinkage long |
| 663 | compat_sys_io_submit(aio_context_t ctx_id, int nr, u32 __user *iocb) |
| 664 | { |
| 665 | struct iocb __user * __user *iocb64; |
| 666 | long ret; |
| 667 | |
| 668 | if (unlikely(nr < 0)) |
| 669 | return -EINVAL; |
| 670 | |
| 671 | if (nr > MAX_AIO_SUBMITS) |
| 672 | nr = MAX_AIO_SUBMITS; |
| 673 | |
| 674 | iocb64 = compat_alloc_user_space(nr * sizeof(*iocb64)); |
| 675 | ret = copy_iocb(nr, iocb, iocb64); |
| 676 | if (!ret) |
| 677 | ret = sys_io_submit(ctx_id, nr, iocb64); |
| 678 | return ret; |
| 679 | } |
| 680 | |
| 681 | struct compat_ncp_mount_data { |
| 682 | compat_int_t version; |
| 683 | compat_uint_t ncp_fd; |
Stephen Rothwell | 202e597 | 2005-09-06 15:16:40 -0700 | [diff] [blame] | 684 | __compat_uid_t mounted_uid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | compat_pid_t wdog_pid; |
| 686 | unsigned char mounted_vol[NCP_VOLNAME_LEN + 1]; |
| 687 | compat_uint_t time_out; |
| 688 | compat_uint_t retry_count; |
| 689 | compat_uint_t flags; |
Stephen Rothwell | 202e597 | 2005-09-06 15:16:40 -0700 | [diff] [blame] | 690 | __compat_uid_t uid; |
| 691 | __compat_gid_t gid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | compat_mode_t file_mode; |
| 693 | compat_mode_t dir_mode; |
| 694 | }; |
| 695 | |
| 696 | struct compat_ncp_mount_data_v4 { |
| 697 | compat_int_t version; |
| 698 | compat_ulong_t flags; |
| 699 | compat_ulong_t mounted_uid; |
| 700 | compat_long_t wdog_pid; |
| 701 | compat_uint_t ncp_fd; |
| 702 | compat_uint_t time_out; |
| 703 | compat_uint_t retry_count; |
| 704 | compat_ulong_t uid; |
| 705 | compat_ulong_t gid; |
| 706 | compat_ulong_t file_mode; |
| 707 | compat_ulong_t dir_mode; |
| 708 | }; |
| 709 | |
| 710 | static void *do_ncp_super_data_conv(void *raw_data) |
| 711 | { |
| 712 | int version = *(unsigned int *)raw_data; |
| 713 | |
| 714 | if (version == 3) { |
| 715 | struct compat_ncp_mount_data *c_n = raw_data; |
| 716 | struct ncp_mount_data *n = raw_data; |
| 717 | |
| 718 | n->dir_mode = c_n->dir_mode; |
| 719 | n->file_mode = c_n->file_mode; |
| 720 | n->gid = c_n->gid; |
| 721 | n->uid = c_n->uid; |
| 722 | memmove (n->mounted_vol, c_n->mounted_vol, (sizeof (c_n->mounted_vol) + 3 * sizeof (unsigned int))); |
| 723 | n->wdog_pid = c_n->wdog_pid; |
| 724 | n->mounted_uid = c_n->mounted_uid; |
| 725 | } else if (version == 4) { |
| 726 | struct compat_ncp_mount_data_v4 *c_n = raw_data; |
| 727 | struct ncp_mount_data_v4 *n = raw_data; |
| 728 | |
| 729 | n->dir_mode = c_n->dir_mode; |
| 730 | n->file_mode = c_n->file_mode; |
| 731 | n->gid = c_n->gid; |
| 732 | n->uid = c_n->uid; |
| 733 | n->retry_count = c_n->retry_count; |
| 734 | n->time_out = c_n->time_out; |
| 735 | n->ncp_fd = c_n->ncp_fd; |
| 736 | n->wdog_pid = c_n->wdog_pid; |
| 737 | n->mounted_uid = c_n->mounted_uid; |
| 738 | n->flags = c_n->flags; |
| 739 | } else if (version != 5) { |
| 740 | return NULL; |
| 741 | } |
| 742 | |
| 743 | return raw_data; |
| 744 | } |
| 745 | |
| 746 | struct compat_smb_mount_data { |
| 747 | compat_int_t version; |
Stephen Rothwell | 202e597 | 2005-09-06 15:16:40 -0700 | [diff] [blame] | 748 | __compat_uid_t mounted_uid; |
| 749 | __compat_uid_t uid; |
| 750 | __compat_gid_t gid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | compat_mode_t file_mode; |
| 752 | compat_mode_t dir_mode; |
| 753 | }; |
| 754 | |
| 755 | static void *do_smb_super_data_conv(void *raw_data) |
| 756 | { |
| 757 | struct smb_mount_data *s = raw_data; |
| 758 | struct compat_smb_mount_data *c_s = raw_data; |
| 759 | |
| 760 | if (c_s->version != SMB_MOUNT_OLDVERSION) |
| 761 | goto out; |
| 762 | s->dir_mode = c_s->dir_mode; |
| 763 | s->file_mode = c_s->file_mode; |
| 764 | s->gid = c_s->gid; |
| 765 | s->uid = c_s->uid; |
| 766 | s->mounted_uid = c_s->mounted_uid; |
| 767 | out: |
| 768 | return raw_data; |
| 769 | } |
| 770 | |
David Howells | 9a9947b | 2005-04-18 10:54:51 -0700 | [diff] [blame] | 771 | struct compat_nfs_string { |
| 772 | compat_uint_t len; |
David Howells | 5fc3e62 | 2005-04-27 15:39:03 -0700 | [diff] [blame] | 773 | compat_uptr_t data; |
David Howells | 9a9947b | 2005-04-18 10:54:51 -0700 | [diff] [blame] | 774 | }; |
| 775 | |
| 776 | static inline void compat_nfs_string(struct nfs_string *dst, |
| 777 | struct compat_nfs_string *src) |
| 778 | { |
| 779 | dst->data = compat_ptr(src->data); |
| 780 | dst->len = src->len; |
| 781 | } |
| 782 | |
| 783 | struct compat_nfs4_mount_data_v1 { |
| 784 | compat_int_t version; |
| 785 | compat_int_t flags; |
| 786 | compat_int_t rsize; |
| 787 | compat_int_t wsize; |
| 788 | compat_int_t timeo; |
| 789 | compat_int_t retrans; |
| 790 | compat_int_t acregmin; |
| 791 | compat_int_t acregmax; |
| 792 | compat_int_t acdirmin; |
| 793 | compat_int_t acdirmax; |
| 794 | struct compat_nfs_string client_addr; |
| 795 | struct compat_nfs_string mnt_path; |
| 796 | struct compat_nfs_string hostname; |
| 797 | compat_uint_t host_addrlen; |
David Howells | 5fc3e62 | 2005-04-27 15:39:03 -0700 | [diff] [blame] | 798 | compat_uptr_t host_addr; |
David Howells | 9a9947b | 2005-04-18 10:54:51 -0700 | [diff] [blame] | 799 | compat_int_t proto; |
| 800 | compat_int_t auth_flavourlen; |
David Howells | 5fc3e62 | 2005-04-27 15:39:03 -0700 | [diff] [blame] | 801 | compat_uptr_t auth_flavours; |
David Howells | 9a9947b | 2005-04-18 10:54:51 -0700 | [diff] [blame] | 802 | }; |
| 803 | |
| 804 | static int do_nfs4_super_data_conv(void *raw_data) |
| 805 | { |
| 806 | int version = *(compat_uint_t *) raw_data; |
| 807 | |
| 808 | if (version == 1) { |
| 809 | struct compat_nfs4_mount_data_v1 *raw = raw_data; |
| 810 | struct nfs4_mount_data *real = raw_data; |
| 811 | |
| 812 | /* copy the fields backwards */ |
| 813 | real->auth_flavours = compat_ptr(raw->auth_flavours); |
| 814 | real->auth_flavourlen = raw->auth_flavourlen; |
| 815 | real->proto = raw->proto; |
| 816 | real->host_addr = compat_ptr(raw->host_addr); |
| 817 | real->host_addrlen = raw->host_addrlen; |
| 818 | compat_nfs_string(&real->hostname, &raw->hostname); |
| 819 | compat_nfs_string(&real->mnt_path, &raw->mnt_path); |
| 820 | compat_nfs_string(&real->client_addr, &raw->client_addr); |
| 821 | real->acdirmax = raw->acdirmax; |
| 822 | real->acdirmin = raw->acdirmin; |
| 823 | real->acregmax = raw->acregmax; |
| 824 | real->acregmin = raw->acregmin; |
| 825 | real->retrans = raw->retrans; |
| 826 | real->timeo = raw->timeo; |
| 827 | real->wsize = raw->wsize; |
| 828 | real->rsize = raw->rsize; |
| 829 | real->flags = raw->flags; |
| 830 | real->version = raw->version; |
| 831 | } |
| 832 | else { |
| 833 | return -EINVAL; |
| 834 | } |
| 835 | |
| 836 | return 0; |
| 837 | } |
| 838 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | #define SMBFS_NAME "smbfs" |
| 840 | #define NCPFS_NAME "ncpfs" |
David Howells | 9a9947b | 2005-04-18 10:54:51 -0700 | [diff] [blame] | 841 | #define NFS4_NAME "nfs4" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | |
| 843 | asmlinkage long compat_sys_mount(char __user * dev_name, char __user * dir_name, |
| 844 | char __user * type, unsigned long flags, |
| 845 | void __user * data) |
| 846 | { |
| 847 | unsigned long type_page; |
| 848 | unsigned long data_page; |
| 849 | unsigned long dev_page; |
| 850 | char *dir_page; |
| 851 | int retval; |
| 852 | |
| 853 | retval = copy_mount_options (type, &type_page); |
| 854 | if (retval < 0) |
| 855 | goto out; |
| 856 | |
| 857 | dir_page = getname(dir_name); |
| 858 | retval = PTR_ERR(dir_page); |
| 859 | if (IS_ERR(dir_page)) |
| 860 | goto out1; |
| 861 | |
| 862 | retval = copy_mount_options (dev_name, &dev_page); |
| 863 | if (retval < 0) |
| 864 | goto out2; |
| 865 | |
| 866 | retval = copy_mount_options (data, &data_page); |
| 867 | if (retval < 0) |
| 868 | goto out3; |
| 869 | |
| 870 | retval = -EINVAL; |
| 871 | |
| 872 | if (type_page) { |
| 873 | if (!strcmp((char *)type_page, SMBFS_NAME)) { |
| 874 | do_smb_super_data_conv((void *)data_page); |
| 875 | } else if (!strcmp((char *)type_page, NCPFS_NAME)) { |
| 876 | do_ncp_super_data_conv((void *)data_page); |
David Howells | 9a9947b | 2005-04-18 10:54:51 -0700 | [diff] [blame] | 877 | } else if (!strcmp((char *)type_page, NFS4_NAME)) { |
| 878 | if (do_nfs4_super_data_conv((void *) data_page)) |
| 879 | goto out4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | } |
| 881 | } |
| 882 | |
| 883 | lock_kernel(); |
| 884 | retval = do_mount((char*)dev_page, dir_page, (char*)type_page, |
| 885 | flags, (void*)data_page); |
| 886 | unlock_kernel(); |
| 887 | |
David Howells | 9a9947b | 2005-04-18 10:54:51 -0700 | [diff] [blame] | 888 | out4: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | free_page(data_page); |
| 890 | out3: |
| 891 | free_page(dev_page); |
| 892 | out2: |
| 893 | putname(dir_page); |
| 894 | out1: |
| 895 | free_page(type_page); |
| 896 | out: |
| 897 | return retval; |
| 898 | } |
| 899 | |
| 900 | #define NAME_OFFSET(de) ((int) ((de)->d_name - (char __user *) (de))) |
| 901 | #define COMPAT_ROUND_UP(x) (((x)+sizeof(compat_long_t)-1) & \ |
| 902 | ~(sizeof(compat_long_t)-1)) |
| 903 | |
| 904 | struct compat_old_linux_dirent { |
| 905 | compat_ulong_t d_ino; |
| 906 | compat_ulong_t d_offset; |
| 907 | unsigned short d_namlen; |
| 908 | char d_name[1]; |
| 909 | }; |
| 910 | |
| 911 | struct compat_readdir_callback { |
| 912 | struct compat_old_linux_dirent __user *dirent; |
| 913 | int result; |
| 914 | }; |
| 915 | |
| 916 | static int compat_fillonedir(void *__buf, const char *name, int namlen, |
| 917 | loff_t offset, ino_t ino, unsigned int d_type) |
| 918 | { |
| 919 | struct compat_readdir_callback *buf = __buf; |
| 920 | struct compat_old_linux_dirent __user *dirent; |
| 921 | |
| 922 | if (buf->result) |
| 923 | return -EINVAL; |
| 924 | buf->result++; |
| 925 | dirent = buf->dirent; |
| 926 | if (!access_ok(VERIFY_WRITE, dirent, |
| 927 | (unsigned long)(dirent->d_name + namlen + 1) - |
| 928 | (unsigned long)dirent)) |
| 929 | goto efault; |
| 930 | if ( __put_user(ino, &dirent->d_ino) || |
| 931 | __put_user(offset, &dirent->d_offset) || |
| 932 | __put_user(namlen, &dirent->d_namlen) || |
| 933 | __copy_to_user(dirent->d_name, name, namlen) || |
| 934 | __put_user(0, dirent->d_name + namlen)) |
| 935 | goto efault; |
| 936 | return 0; |
| 937 | efault: |
| 938 | buf->result = -EFAULT; |
| 939 | return -EFAULT; |
| 940 | } |
| 941 | |
| 942 | asmlinkage long compat_sys_old_readdir(unsigned int fd, |
| 943 | struct compat_old_linux_dirent __user *dirent, unsigned int count) |
| 944 | { |
| 945 | int error; |
| 946 | struct file *file; |
| 947 | struct compat_readdir_callback buf; |
| 948 | |
| 949 | error = -EBADF; |
| 950 | file = fget(fd); |
| 951 | if (!file) |
| 952 | goto out; |
| 953 | |
| 954 | buf.result = 0; |
| 955 | buf.dirent = dirent; |
| 956 | |
| 957 | error = vfs_readdir(file, compat_fillonedir, &buf); |
| 958 | if (error >= 0) |
| 959 | error = buf.result; |
| 960 | |
| 961 | fput(file); |
| 962 | out: |
| 963 | return error; |
| 964 | } |
| 965 | |
| 966 | struct compat_linux_dirent { |
| 967 | compat_ulong_t d_ino; |
| 968 | compat_ulong_t d_off; |
| 969 | unsigned short d_reclen; |
| 970 | char d_name[1]; |
| 971 | }; |
| 972 | |
| 973 | struct compat_getdents_callback { |
| 974 | struct compat_linux_dirent __user *current_dir; |
| 975 | struct compat_linux_dirent __user *previous; |
| 976 | int count; |
| 977 | int error; |
| 978 | }; |
| 979 | |
| 980 | static int compat_filldir(void *__buf, const char *name, int namlen, |
| 981 | loff_t offset, ino_t ino, unsigned int d_type) |
| 982 | { |
| 983 | struct compat_linux_dirent __user * dirent; |
| 984 | struct compat_getdents_callback *buf = __buf; |
| 985 | int reclen = COMPAT_ROUND_UP(NAME_OFFSET(dirent) + namlen + 2); |
| 986 | |
| 987 | buf->error = -EINVAL; /* only used if we fail.. */ |
| 988 | if (reclen > buf->count) |
| 989 | return -EINVAL; |
| 990 | dirent = buf->previous; |
| 991 | if (dirent) { |
| 992 | if (__put_user(offset, &dirent->d_off)) |
| 993 | goto efault; |
| 994 | } |
| 995 | dirent = buf->current_dir; |
| 996 | if (__put_user(ino, &dirent->d_ino)) |
| 997 | goto efault; |
| 998 | if (__put_user(reclen, &dirent->d_reclen)) |
| 999 | goto efault; |
| 1000 | if (copy_to_user(dirent->d_name, name, namlen)) |
| 1001 | goto efault; |
| 1002 | if (__put_user(0, dirent->d_name + namlen)) |
| 1003 | goto efault; |
| 1004 | if (__put_user(d_type, (char __user *) dirent + reclen - 1)) |
| 1005 | goto efault; |
| 1006 | buf->previous = dirent; |
| 1007 | dirent = (void __user *)dirent + reclen; |
| 1008 | buf->current_dir = dirent; |
| 1009 | buf->count -= reclen; |
| 1010 | return 0; |
| 1011 | efault: |
| 1012 | buf->error = -EFAULT; |
| 1013 | return -EFAULT; |
| 1014 | } |
| 1015 | |
| 1016 | asmlinkage long compat_sys_getdents(unsigned int fd, |
| 1017 | struct compat_linux_dirent __user *dirent, unsigned int count) |
| 1018 | { |
| 1019 | struct file * file; |
| 1020 | struct compat_linux_dirent __user * lastdirent; |
| 1021 | struct compat_getdents_callback buf; |
| 1022 | int error; |
| 1023 | |
| 1024 | error = -EFAULT; |
| 1025 | if (!access_ok(VERIFY_WRITE, dirent, count)) |
| 1026 | goto out; |
| 1027 | |
| 1028 | error = -EBADF; |
| 1029 | file = fget(fd); |
| 1030 | if (!file) |
| 1031 | goto out; |
| 1032 | |
| 1033 | buf.current_dir = dirent; |
| 1034 | buf.previous = NULL; |
| 1035 | buf.count = count; |
| 1036 | buf.error = 0; |
| 1037 | |
| 1038 | error = vfs_readdir(file, compat_filldir, &buf); |
| 1039 | if (error < 0) |
| 1040 | goto out_putf; |
| 1041 | error = buf.error; |
| 1042 | lastdirent = buf.previous; |
| 1043 | if (lastdirent) { |
| 1044 | if (put_user(file->f_pos, &lastdirent->d_off)) |
| 1045 | error = -EFAULT; |
| 1046 | else |
| 1047 | error = count - buf.count; |
| 1048 | } |
| 1049 | |
| 1050 | out_putf: |
| 1051 | fput(file); |
| 1052 | out: |
| 1053 | return error; |
| 1054 | } |
| 1055 | |
| 1056 | #ifndef __ARCH_OMIT_COMPAT_SYS_GETDENTS64 |
| 1057 | #define COMPAT_ROUND_UP64(x) (((x)+sizeof(u64)-1) & ~(sizeof(u64)-1)) |
| 1058 | |
| 1059 | struct compat_getdents_callback64 { |
| 1060 | struct linux_dirent64 __user *current_dir; |
| 1061 | struct linux_dirent64 __user *previous; |
| 1062 | int count; |
| 1063 | int error; |
| 1064 | }; |
| 1065 | |
| 1066 | static int compat_filldir64(void * __buf, const char * name, int namlen, loff_t offset, |
| 1067 | ino_t ino, unsigned int d_type) |
| 1068 | { |
| 1069 | struct linux_dirent64 __user *dirent; |
| 1070 | struct compat_getdents_callback64 *buf = __buf; |
| 1071 | int jj = NAME_OFFSET(dirent); |
| 1072 | int reclen = COMPAT_ROUND_UP64(jj + namlen + 1); |
| 1073 | u64 off; |
| 1074 | |
| 1075 | buf->error = -EINVAL; /* only used if we fail.. */ |
| 1076 | if (reclen > buf->count) |
| 1077 | return -EINVAL; |
| 1078 | dirent = buf->previous; |
| 1079 | |
| 1080 | if (dirent) { |
| 1081 | if (__put_user_unaligned(offset, &dirent->d_off)) |
| 1082 | goto efault; |
| 1083 | } |
| 1084 | dirent = buf->current_dir; |
| 1085 | if (__put_user_unaligned(ino, &dirent->d_ino)) |
| 1086 | goto efault; |
| 1087 | off = 0; |
| 1088 | if (__put_user_unaligned(off, &dirent->d_off)) |
| 1089 | goto efault; |
| 1090 | if (__put_user(reclen, &dirent->d_reclen)) |
| 1091 | goto efault; |
| 1092 | if (__put_user(d_type, &dirent->d_type)) |
| 1093 | goto efault; |
| 1094 | if (copy_to_user(dirent->d_name, name, namlen)) |
| 1095 | goto efault; |
| 1096 | if (__put_user(0, dirent->d_name + namlen)) |
| 1097 | goto efault; |
| 1098 | buf->previous = dirent; |
| 1099 | dirent = (void __user *)dirent + reclen; |
| 1100 | buf->current_dir = dirent; |
| 1101 | buf->count -= reclen; |
| 1102 | return 0; |
| 1103 | efault: |
| 1104 | buf->error = -EFAULT; |
| 1105 | return -EFAULT; |
| 1106 | } |
| 1107 | |
| 1108 | asmlinkage long compat_sys_getdents64(unsigned int fd, |
| 1109 | struct linux_dirent64 __user * dirent, unsigned int count) |
| 1110 | { |
| 1111 | struct file * file; |
| 1112 | struct linux_dirent64 __user * lastdirent; |
| 1113 | struct compat_getdents_callback64 buf; |
| 1114 | int error; |
| 1115 | |
| 1116 | error = -EFAULT; |
| 1117 | if (!access_ok(VERIFY_WRITE, dirent, count)) |
| 1118 | goto out; |
| 1119 | |
| 1120 | error = -EBADF; |
| 1121 | file = fget(fd); |
| 1122 | if (!file) |
| 1123 | goto out; |
| 1124 | |
| 1125 | buf.current_dir = dirent; |
| 1126 | buf.previous = NULL; |
| 1127 | buf.count = count; |
| 1128 | buf.error = 0; |
| 1129 | |
| 1130 | error = vfs_readdir(file, compat_filldir64, &buf); |
| 1131 | if (error < 0) |
| 1132 | goto out_putf; |
| 1133 | error = buf.error; |
| 1134 | lastdirent = buf.previous; |
| 1135 | if (lastdirent) { |
| 1136 | typeof(lastdirent->d_off) d_off = file->f_pos; |
| 1137 | __put_user_unaligned(d_off, &lastdirent->d_off); |
| 1138 | error = count - buf.count; |
| 1139 | } |
| 1140 | |
| 1141 | out_putf: |
| 1142 | fput(file); |
| 1143 | out: |
| 1144 | return error; |
| 1145 | } |
| 1146 | #endif /* ! __ARCH_OMIT_COMPAT_SYS_GETDENTS64 */ |
| 1147 | |
| 1148 | static ssize_t compat_do_readv_writev(int type, struct file *file, |
| 1149 | const struct compat_iovec __user *uvector, |
| 1150 | unsigned long nr_segs, loff_t *pos) |
| 1151 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | compat_ssize_t tot_len; |
| 1153 | struct iovec iovstack[UIO_FASTIOV]; |
| 1154 | struct iovec *iov=iovstack, *vector; |
| 1155 | ssize_t ret; |
| 1156 | int seg; |
| 1157 | io_fn_t fn; |
| 1158 | iov_fn_t fnv; |
| 1159 | |
| 1160 | /* |
| 1161 | * SuS says "The readv() function *may* fail if the iovcnt argument |
| 1162 | * was less than or equal to 0, or greater than {IOV_MAX}. Linux has |
| 1163 | * traditionally returned zero for zero segments, so... |
| 1164 | */ |
| 1165 | ret = 0; |
| 1166 | if (nr_segs == 0) |
| 1167 | goto out; |
| 1168 | |
| 1169 | /* |
| 1170 | * First get the "struct iovec" from user memory and |
| 1171 | * verify all the pointers |
| 1172 | */ |
| 1173 | ret = -EINVAL; |
| 1174 | if ((nr_segs > UIO_MAXIOV) || (nr_segs <= 0)) |
| 1175 | goto out; |
| 1176 | if (!file->f_op) |
| 1177 | goto out; |
| 1178 | if (nr_segs > UIO_FASTIOV) { |
| 1179 | ret = -ENOMEM; |
| 1180 | iov = kmalloc(nr_segs*sizeof(struct iovec), GFP_KERNEL); |
| 1181 | if (!iov) |
| 1182 | goto out; |
| 1183 | } |
| 1184 | ret = -EFAULT; |
| 1185 | if (!access_ok(VERIFY_READ, uvector, nr_segs*sizeof(*uvector))) |
| 1186 | goto out; |
| 1187 | |
| 1188 | /* |
| 1189 | * Single unix specification: |
| 1190 | * We should -EINVAL if an element length is not >= 0 and fitting an |
| 1191 | * ssize_t. The total length is fitting an ssize_t |
| 1192 | * |
| 1193 | * Be careful here because iov_len is a size_t not an ssize_t |
| 1194 | */ |
| 1195 | tot_len = 0; |
| 1196 | vector = iov; |
| 1197 | ret = -EINVAL; |
| 1198 | for (seg = 0 ; seg < nr_segs; seg++) { |
| 1199 | compat_ssize_t tmp = tot_len; |
| 1200 | compat_ssize_t len; |
| 1201 | compat_uptr_t buf; |
| 1202 | |
| 1203 | if (__get_user(len, &uvector->iov_len) || |
| 1204 | __get_user(buf, &uvector->iov_base)) { |
| 1205 | ret = -EFAULT; |
| 1206 | goto out; |
| 1207 | } |
| 1208 | if (len < 0) /* size_t not fitting an compat_ssize_t .. */ |
| 1209 | goto out; |
| 1210 | tot_len += len; |
| 1211 | if (tot_len < tmp) /* maths overflow on the compat_ssize_t */ |
| 1212 | goto out; |
| 1213 | vector->iov_base = compat_ptr(buf); |
| 1214 | vector->iov_len = (compat_size_t) len; |
| 1215 | uvector++; |
| 1216 | vector++; |
| 1217 | } |
| 1218 | if (tot_len == 0) { |
| 1219 | ret = 0; |
| 1220 | goto out; |
| 1221 | } |
| 1222 | |
| 1223 | ret = rw_verify_area(type, file, pos, tot_len); |
Linus Torvalds | e28cc71 | 2006-01-04 16:20:40 -0800 | [diff] [blame] | 1224 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | goto out; |
| 1226 | |
James Morris | e7edf9c | 2006-04-26 02:45:03 -0400 | [diff] [blame] | 1227 | ret = security_file_permission(file, type == READ ? MAY_READ:MAY_WRITE); |
| 1228 | if (ret) |
| 1229 | goto out; |
| 1230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | fnv = NULL; |
| 1232 | if (type == READ) { |
| 1233 | fn = file->f_op->read; |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 1234 | fnv = file->f_op->aio_read; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | } else { |
| 1236 | fn = (io_fn_t)file->f_op->write; |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 1237 | fnv = file->f_op->aio_write; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | } |
| 1239 | |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 1240 | if (fnv) |
| 1241 | ret = do_sync_readv_writev(file, iov, nr_segs, tot_len, |
| 1242 | pos, fnv); |
| 1243 | else |
| 1244 | ret = do_loop_readv_writev(file, iov, nr_segs, pos, fn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | out: |
| 1247 | if (iov != iovstack) |
| 1248 | kfree(iov); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 1249 | if ((ret + (type == READ)) > 0) { |
| 1250 | struct dentry *dentry = file->f_dentry; |
| 1251 | if (type == READ) |
| 1252 | fsnotify_access(dentry); |
| 1253 | else |
| 1254 | fsnotify_modify(dentry); |
| 1255 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | return ret; |
| 1257 | } |
| 1258 | |
| 1259 | asmlinkage ssize_t |
| 1260 | compat_sys_readv(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen) |
| 1261 | { |
| 1262 | struct file *file; |
| 1263 | ssize_t ret = -EBADF; |
| 1264 | |
| 1265 | file = fget(fd); |
| 1266 | if (!file) |
| 1267 | return -EBADF; |
| 1268 | |
| 1269 | if (!(file->f_mode & FMODE_READ)) |
| 1270 | goto out; |
| 1271 | |
| 1272 | ret = -EINVAL; |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 1273 | if (!file->f_op || (!file->f_op->aio_read && !file->f_op->read)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | goto out; |
| 1275 | |
| 1276 | ret = compat_do_readv_writev(READ, file, vec, vlen, &file->f_pos); |
| 1277 | |
| 1278 | out: |
| 1279 | fput(file); |
| 1280 | return ret; |
| 1281 | } |
| 1282 | |
| 1283 | asmlinkage ssize_t |
| 1284 | compat_sys_writev(unsigned long fd, const struct compat_iovec __user *vec, unsigned long vlen) |
| 1285 | { |
| 1286 | struct file *file; |
| 1287 | ssize_t ret = -EBADF; |
| 1288 | |
| 1289 | file = fget(fd); |
| 1290 | if (!file) |
| 1291 | return -EBADF; |
| 1292 | if (!(file->f_mode & FMODE_WRITE)) |
| 1293 | goto out; |
| 1294 | |
| 1295 | ret = -EINVAL; |
Badari Pulavarty | ee0b3e6 | 2006-09-30 23:28:47 -0700 | [diff] [blame] | 1296 | if (!file->f_op || (!file->f_op->aio_write && !file->f_op->write)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | goto out; |
| 1298 | |
| 1299 | ret = compat_do_readv_writev(WRITE, file, vec, vlen, &file->f_pos); |
| 1300 | |
| 1301 | out: |
| 1302 | fput(file); |
| 1303 | return ret; |
| 1304 | } |
| 1305 | |
Andi Kleen | d261020 | 2006-05-01 12:15:48 -0700 | [diff] [blame] | 1306 | asmlinkage long |
| 1307 | compat_sys_vmsplice(int fd, const struct compat_iovec __user *iov32, |
| 1308 | unsigned int nr_segs, unsigned int flags) |
| 1309 | { |
| 1310 | unsigned i; |
| 1311 | struct iovec *iov; |
Jens Axboe | 98232d5 | 2006-05-04 09:13:49 +0200 | [diff] [blame] | 1312 | if (nr_segs > UIO_MAXIOV) |
Andi Kleen | d261020 | 2006-05-01 12:15:48 -0700 | [diff] [blame] | 1313 | return -EINVAL; |
| 1314 | iov = compat_alloc_user_space(nr_segs * sizeof(struct iovec)); |
| 1315 | for (i = 0; i < nr_segs; i++) { |
| 1316 | struct compat_iovec v; |
| 1317 | if (get_user(v.iov_base, &iov32[i].iov_base) || |
| 1318 | get_user(v.iov_len, &iov32[i].iov_len) || |
| 1319 | put_user(compat_ptr(v.iov_base), &iov[i].iov_base) || |
| 1320 | put_user(v.iov_len, &iov[i].iov_len)) |
| 1321 | return -EFAULT; |
| 1322 | } |
| 1323 | return sys_vmsplice(fd, iov, nr_segs, flags); |
| 1324 | } |
| 1325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | /* |
Miklos Szeredi | e922efc | 2005-09-06 15:18:25 -0700 | [diff] [blame] | 1327 | * Exactly like fs/open.c:sys_open(), except that it doesn't set the |
| 1328 | * O_LARGEFILE flag. |
| 1329 | */ |
| 1330 | asmlinkage long |
| 1331 | compat_sys_open(const char __user *filename, int flags, int mode) |
| 1332 | { |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1333 | return do_sys_open(AT_FDCWD, filename, flags, mode); |
| 1334 | } |
| 1335 | |
| 1336 | /* |
| 1337 | * Exactly like fs/open.c:sys_openat(), except that it doesn't set the |
| 1338 | * O_LARGEFILE flag. |
| 1339 | */ |
| 1340 | asmlinkage long |
Stephen Rothwell | 9ad11ab | 2006-02-02 16:11:51 +1100 | [diff] [blame] | 1341 | compat_sys_openat(unsigned int dfd, const char __user *filename, int flags, int mode) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1342 | { |
| 1343 | return do_sys_open(dfd, filename, flags, mode); |
Miklos Szeredi | e922efc | 2005-09-06 15:18:25 -0700 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | * compat_count() counts the number of arguments/envelopes. It is basically |
| 1348 | * a copy of count() from fs/exec.c, except that it works with 32 bit argv |
| 1349 | * and envp pointers. |
| 1350 | */ |
| 1351 | static int compat_count(compat_uptr_t __user *argv, int max) |
| 1352 | { |
| 1353 | int i = 0; |
| 1354 | |
| 1355 | if (argv != NULL) { |
| 1356 | for (;;) { |
| 1357 | compat_uptr_t p; |
| 1358 | |
| 1359 | if (get_user(p, argv)) |
| 1360 | return -EFAULT; |
| 1361 | if (!p) |
| 1362 | break; |
| 1363 | argv++; |
| 1364 | if(++i > max) |
| 1365 | return -E2BIG; |
| 1366 | } |
| 1367 | } |
| 1368 | return i; |
| 1369 | } |
| 1370 | |
| 1371 | /* |
| 1372 | * compat_copy_strings() is basically a copy of copy_strings() from fs/exec.c |
| 1373 | * except that it works with 32 bit argv and envp pointers. |
| 1374 | */ |
| 1375 | static int compat_copy_strings(int argc, compat_uptr_t __user *argv, |
| 1376 | struct linux_binprm *bprm) |
| 1377 | { |
| 1378 | struct page *kmapped_page = NULL; |
| 1379 | char *kaddr = NULL; |
| 1380 | int ret; |
| 1381 | |
| 1382 | while (argc-- > 0) { |
| 1383 | compat_uptr_t str; |
| 1384 | int len; |
| 1385 | unsigned long pos; |
| 1386 | |
| 1387 | if (get_user(str, argv+argc) || |
| 1388 | !(len = strnlen_user(compat_ptr(str), bprm->p))) { |
| 1389 | ret = -EFAULT; |
| 1390 | goto out; |
| 1391 | } |
| 1392 | |
| 1393 | if (bprm->p < len) { |
| 1394 | ret = -E2BIG; |
| 1395 | goto out; |
| 1396 | } |
| 1397 | |
| 1398 | bprm->p -= len; |
| 1399 | /* XXX: add architecture specific overflow check here. */ |
| 1400 | pos = bprm->p; |
| 1401 | |
| 1402 | while (len > 0) { |
| 1403 | int i, new, err; |
| 1404 | int offset, bytes_to_copy; |
| 1405 | struct page *page; |
| 1406 | |
| 1407 | offset = pos % PAGE_SIZE; |
| 1408 | i = pos/PAGE_SIZE; |
| 1409 | page = bprm->page[i]; |
| 1410 | new = 0; |
| 1411 | if (!page) { |
| 1412 | page = alloc_page(GFP_HIGHUSER); |
| 1413 | bprm->page[i] = page; |
| 1414 | if (!page) { |
| 1415 | ret = -ENOMEM; |
| 1416 | goto out; |
| 1417 | } |
| 1418 | new = 1; |
| 1419 | } |
| 1420 | |
| 1421 | if (page != kmapped_page) { |
| 1422 | if (kmapped_page) |
| 1423 | kunmap(kmapped_page); |
| 1424 | kmapped_page = page; |
| 1425 | kaddr = kmap(kmapped_page); |
| 1426 | } |
| 1427 | if (new && offset) |
| 1428 | memset(kaddr, 0, offset); |
| 1429 | bytes_to_copy = PAGE_SIZE - offset; |
| 1430 | if (bytes_to_copy > len) { |
| 1431 | bytes_to_copy = len; |
| 1432 | if (new) |
| 1433 | memset(kaddr+offset+len, 0, |
| 1434 | PAGE_SIZE-offset-len); |
| 1435 | } |
| 1436 | err = copy_from_user(kaddr+offset, compat_ptr(str), |
| 1437 | bytes_to_copy); |
| 1438 | if (err) { |
| 1439 | ret = -EFAULT; |
| 1440 | goto out; |
| 1441 | } |
| 1442 | |
| 1443 | pos += bytes_to_copy; |
| 1444 | str += bytes_to_copy; |
| 1445 | len -= bytes_to_copy; |
| 1446 | } |
| 1447 | } |
| 1448 | ret = 0; |
| 1449 | out: |
| 1450 | if (kmapped_page) |
| 1451 | kunmap(kmapped_page); |
| 1452 | return ret; |
| 1453 | } |
| 1454 | |
| 1455 | #ifdef CONFIG_MMU |
| 1456 | |
| 1457 | #define free_arg_pages(bprm) do { } while (0) |
| 1458 | |
| 1459 | #else |
| 1460 | |
| 1461 | static inline void free_arg_pages(struct linux_binprm *bprm) |
| 1462 | { |
| 1463 | int i; |
| 1464 | |
| 1465 | for (i = 0; i < MAX_ARG_PAGES; i++) { |
| 1466 | if (bprm->page[i]) |
| 1467 | __free_page(bprm->page[i]); |
| 1468 | bprm->page[i] = NULL; |
| 1469 | } |
| 1470 | } |
| 1471 | |
| 1472 | #endif /* CONFIG_MMU */ |
| 1473 | |
| 1474 | /* |
| 1475 | * compat_do_execve() is mostly a copy of do_execve(), with the exception |
| 1476 | * that it processes 32 bit argv and envp pointers. |
| 1477 | */ |
| 1478 | int compat_do_execve(char * filename, |
| 1479 | compat_uptr_t __user *argv, |
| 1480 | compat_uptr_t __user *envp, |
| 1481 | struct pt_regs * regs) |
| 1482 | { |
| 1483 | struct linux_binprm *bprm; |
| 1484 | struct file *file; |
| 1485 | int retval; |
| 1486 | int i; |
| 1487 | |
| 1488 | retval = -ENOMEM; |
Oliver Neukum | 11b0b5a | 2006-03-25 03:08:13 -0800 | [diff] [blame] | 1489 | bprm = kzalloc(sizeof(*bprm), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | if (!bprm) |
| 1491 | goto out_ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | |
| 1493 | file = open_exec(filename); |
| 1494 | retval = PTR_ERR(file); |
| 1495 | if (IS_ERR(file)) |
| 1496 | goto out_kfree; |
| 1497 | |
| 1498 | sched_exec(); |
| 1499 | |
| 1500 | bprm->p = PAGE_SIZE*MAX_ARG_PAGES-sizeof(void *); |
| 1501 | bprm->file = file; |
| 1502 | bprm->filename = filename; |
| 1503 | bprm->interp = filename; |
| 1504 | bprm->mm = mm_alloc(); |
| 1505 | retval = -ENOMEM; |
| 1506 | if (!bprm->mm) |
| 1507 | goto out_file; |
| 1508 | |
| 1509 | retval = init_new_context(current, bprm->mm); |
| 1510 | if (retval < 0) |
| 1511 | goto out_mm; |
| 1512 | |
| 1513 | bprm->argc = compat_count(argv, bprm->p / sizeof(compat_uptr_t)); |
| 1514 | if ((retval = bprm->argc) < 0) |
| 1515 | goto out_mm; |
| 1516 | |
| 1517 | bprm->envc = compat_count(envp, bprm->p / sizeof(compat_uptr_t)); |
| 1518 | if ((retval = bprm->envc) < 0) |
| 1519 | goto out_mm; |
| 1520 | |
| 1521 | retval = security_bprm_alloc(bprm); |
| 1522 | if (retval) |
| 1523 | goto out; |
| 1524 | |
| 1525 | retval = prepare_binprm(bprm); |
| 1526 | if (retval < 0) |
| 1527 | goto out; |
| 1528 | |
| 1529 | retval = copy_strings_kernel(1, &bprm->filename, bprm); |
| 1530 | if (retval < 0) |
| 1531 | goto out; |
| 1532 | |
| 1533 | bprm->exec = bprm->p; |
| 1534 | retval = compat_copy_strings(bprm->envc, envp, bprm); |
| 1535 | if (retval < 0) |
| 1536 | goto out; |
| 1537 | |
| 1538 | retval = compat_copy_strings(bprm->argc, argv, bprm); |
| 1539 | if (retval < 0) |
| 1540 | goto out; |
| 1541 | |
| 1542 | retval = search_binary_handler(bprm, regs); |
| 1543 | if (retval >= 0) { |
| 1544 | free_arg_pages(bprm); |
| 1545 | |
| 1546 | /* execve success */ |
| 1547 | security_bprm_free(bprm); |
David S. Miller | 4a805e8 | 2005-09-14 21:40:00 -0700 | [diff] [blame] | 1548 | acct_update_integrals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | kfree(bprm); |
| 1550 | return retval; |
| 1551 | } |
| 1552 | |
| 1553 | out: |
| 1554 | /* Something went wrong, return the inode and free the argument pages*/ |
| 1555 | for (i = 0 ; i < MAX_ARG_PAGES ; i++) { |
| 1556 | struct page * page = bprm->page[i]; |
| 1557 | if (page) |
| 1558 | __free_page(page); |
| 1559 | } |
| 1560 | |
| 1561 | if (bprm->security) |
| 1562 | security_bprm_free(bprm); |
| 1563 | |
| 1564 | out_mm: |
| 1565 | if (bprm->mm) |
| 1566 | mmdrop(bprm->mm); |
| 1567 | |
| 1568 | out_file: |
| 1569 | if (bprm->file) { |
| 1570 | allow_write_access(bprm->file); |
| 1571 | fput(bprm->file); |
| 1572 | } |
| 1573 | |
| 1574 | out_kfree: |
| 1575 | kfree(bprm); |
| 1576 | |
| 1577 | out_ret: |
| 1578 | return retval; |
| 1579 | } |
| 1580 | |
| 1581 | #define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t)) |
| 1582 | |
| 1583 | #define ROUND_UP(x,y) (((x)+(y)-1)/(y)) |
| 1584 | |
| 1585 | /* |
| 1586 | * Ooo, nasty. We need here to frob 32-bit unsigned longs to |
| 1587 | * 64-bit unsigned longs. |
| 1588 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 1589 | static |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | int compat_get_fd_set(unsigned long nr, compat_ulong_t __user *ufdset, |
| 1591 | unsigned long *fdset) |
| 1592 | { |
| 1593 | nr = ROUND_UP(nr, __COMPAT_NFDBITS); |
| 1594 | if (ufdset) { |
| 1595 | unsigned long odd; |
| 1596 | |
| 1597 | if (!access_ok(VERIFY_WRITE, ufdset, nr*sizeof(compat_ulong_t))) |
| 1598 | return -EFAULT; |
| 1599 | |
| 1600 | odd = nr & 1UL; |
| 1601 | nr &= ~1UL; |
| 1602 | while (nr) { |
| 1603 | unsigned long h, l; |
| 1604 | __get_user(l, ufdset); |
| 1605 | __get_user(h, ufdset+1); |
| 1606 | ufdset += 2; |
| 1607 | *fdset++ = h << 32 | l; |
| 1608 | nr -= 2; |
| 1609 | } |
| 1610 | if (odd) |
| 1611 | __get_user(*fdset, ufdset); |
| 1612 | } else { |
| 1613 | /* Tricky, must clear full unsigned long in the |
| 1614 | * kernel fdset at the end, this makes sure that |
| 1615 | * actually happens. |
| 1616 | */ |
| 1617 | memset(fdset, 0, ((nr + 1) & ~1)*sizeof(compat_ulong_t)); |
| 1618 | } |
| 1619 | return 0; |
| 1620 | } |
| 1621 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 1622 | static |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | void compat_set_fd_set(unsigned long nr, compat_ulong_t __user *ufdset, |
| 1624 | unsigned long *fdset) |
| 1625 | { |
| 1626 | unsigned long odd; |
| 1627 | nr = ROUND_UP(nr, __COMPAT_NFDBITS); |
| 1628 | |
| 1629 | if (!ufdset) |
| 1630 | return; |
| 1631 | |
| 1632 | odd = nr & 1UL; |
| 1633 | nr &= ~1UL; |
| 1634 | while (nr) { |
| 1635 | unsigned long h, l; |
| 1636 | l = *fdset++; |
| 1637 | h = l >> 32; |
| 1638 | __put_user(l, ufdset); |
| 1639 | __put_user(h, ufdset+1); |
| 1640 | ufdset += 2; |
| 1641 | nr -= 2; |
| 1642 | } |
| 1643 | if (odd) |
| 1644 | __put_user(*fdset, ufdset); |
| 1645 | } |
| 1646 | |
| 1647 | |
| 1648 | /* |
| 1649 | * This is a virtual copy of sys_select from fs/select.c and probably |
| 1650 | * should be compared to it from time to time |
| 1651 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | |
| 1653 | /* |
| 1654 | * We can actually return ERESTARTSYS instead of EINTR, but I'd |
| 1655 | * like to be certain this leads to no problems. So I return |
| 1656 | * EINTR just for safety. |
| 1657 | * |
| 1658 | * Update: ERESTARTSYS breaks at least the xview clock binary, so |
| 1659 | * I'm trying ERESTARTNOHAND which restart only when you want to. |
| 1660 | */ |
| 1661 | #define MAX_SELECT_SECONDS \ |
| 1662 | ((unsigned long) (MAX_SCHEDULE_TIMEOUT / HZ)-1) |
| 1663 | |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1664 | int compat_core_sys_select(int n, compat_ulong_t __user *inp, |
| 1665 | compat_ulong_t __user *outp, compat_ulong_t __user *exp, s64 *timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | { |
| 1667 | fd_set_bits fds; |
| 1668 | char *bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | int size, max_fdset, ret = -EINVAL; |
Linus Torvalds | a4531ed | 2005-09-09 15:10:52 -0700 | [diff] [blame] | 1670 | struct fdtable *fdt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | if (n < 0) |
| 1673 | goto out_nofds; |
| 1674 | |
| 1675 | /* max_fdset can increase, so grab it once to avoid race */ |
Linus Torvalds | ac5b8b6 | 2005-09-09 15:42:34 -0700 | [diff] [blame] | 1676 | rcu_read_lock(); |
Linus Torvalds | a4531ed | 2005-09-09 15:10:52 -0700 | [diff] [blame] | 1677 | fdt = files_fdtable(current->files); |
| 1678 | max_fdset = fdt->max_fdset; |
Linus Torvalds | ac5b8b6 | 2005-09-09 15:42:34 -0700 | [diff] [blame] | 1679 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | if (n > max_fdset) |
| 1681 | n = max_fdset; |
| 1682 | |
| 1683 | /* |
| 1684 | * We need 6 bitmaps (in/out/ex for both incoming and outgoing), |
| 1685 | * since we used fdset we need to allocate memory in units of |
| 1686 | * long-words. |
| 1687 | */ |
| 1688 | ret = -ENOMEM; |
| 1689 | size = FDS_BYTES(n); |
Vadim Lobanov | 68c3431 | 2006-03-28 01:56:35 -0800 | [diff] [blame] | 1690 | bits = kmalloc(6 * size, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | if (!bits) |
| 1692 | goto out_nofds; |
| 1693 | fds.in = (unsigned long *) bits; |
| 1694 | fds.out = (unsigned long *) (bits + size); |
| 1695 | fds.ex = (unsigned long *) (bits + 2*size); |
| 1696 | fds.res_in = (unsigned long *) (bits + 3*size); |
| 1697 | fds.res_out = (unsigned long *) (bits + 4*size); |
| 1698 | fds.res_ex = (unsigned long *) (bits + 5*size); |
| 1699 | |
| 1700 | if ((ret = compat_get_fd_set(n, inp, fds.in)) || |
| 1701 | (ret = compat_get_fd_set(n, outp, fds.out)) || |
| 1702 | (ret = compat_get_fd_set(n, exp, fds.ex))) |
| 1703 | goto out; |
| 1704 | zero_fd_set(n, fds.res_in); |
| 1705 | zero_fd_set(n, fds.res_out); |
| 1706 | zero_fd_set(n, fds.res_ex); |
| 1707 | |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1708 | ret = do_select(n, &fds, timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | |
| 1710 | if (ret < 0) |
| 1711 | goto out; |
| 1712 | if (!ret) { |
| 1713 | ret = -ERESTARTNOHAND; |
| 1714 | if (signal_pending(current)) |
| 1715 | goto out; |
| 1716 | ret = 0; |
| 1717 | } |
| 1718 | |
| 1719 | compat_set_fd_set(n, inp, fds.res_in); |
| 1720 | compat_set_fd_set(n, outp, fds.res_out); |
| 1721 | compat_set_fd_set(n, exp, fds.res_ex); |
| 1722 | |
| 1723 | out: |
Vadim Lobanov | 68c3431 | 2006-03-28 01:56:35 -0800 | [diff] [blame] | 1724 | kfree(bits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | out_nofds: |
| 1726 | return ret; |
| 1727 | } |
| 1728 | |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1729 | asmlinkage long compat_sys_select(int n, compat_ulong_t __user *inp, |
| 1730 | compat_ulong_t __user *outp, compat_ulong_t __user *exp, |
| 1731 | struct compat_timeval __user *tvp) |
| 1732 | { |
| 1733 | s64 timeout = -1; |
| 1734 | struct compat_timeval tv; |
| 1735 | int ret; |
| 1736 | |
| 1737 | if (tvp) { |
| 1738 | if (copy_from_user(&tv, tvp, sizeof(tv))) |
| 1739 | return -EFAULT; |
| 1740 | |
| 1741 | if (tv.tv_sec < 0 || tv.tv_usec < 0) |
| 1742 | return -EINVAL; |
| 1743 | |
| 1744 | /* Cast to u64 to make GCC stop complaining */ |
| 1745 | if ((u64)tv.tv_sec >= (u64)MAX_INT64_SECONDS) |
| 1746 | timeout = -1; /* infinite */ |
| 1747 | else { |
David S. Miller | 7e732bf | 2006-01-19 16:40:42 -0800 | [diff] [blame] | 1748 | timeout = ROUND_UP(tv.tv_usec, 1000000/HZ); |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1749 | timeout += tv.tv_sec * HZ; |
| 1750 | } |
| 1751 | } |
| 1752 | |
| 1753 | ret = compat_core_sys_select(n, inp, outp, exp, &timeout); |
| 1754 | |
| 1755 | if (tvp) { |
Andrew Morton | 643a654 | 2006-02-11 17:55:52 -0800 | [diff] [blame] | 1756 | struct compat_timeval rtv; |
| 1757 | |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1758 | if (current->personality & STICKY_TIMEOUTS) |
| 1759 | goto sticky; |
Andrew Morton | 643a654 | 2006-02-11 17:55:52 -0800 | [diff] [blame] | 1760 | rtv.tv_usec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)); |
| 1761 | rtv.tv_sec = timeout; |
Andrew Morton | 74910e6 | 2006-02-17 13:52:58 -0800 | [diff] [blame] | 1762 | if (compat_timeval_compare(&rtv, &tv) >= 0) |
Andrew Morton | 643a654 | 2006-02-11 17:55:52 -0800 | [diff] [blame] | 1763 | rtv = tv; |
| 1764 | if (copy_to_user(tvp, &rtv, sizeof(rtv))) { |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1765 | sticky: |
| 1766 | /* |
| 1767 | * If an application puts its timeval in read-only |
| 1768 | * memory, we don't want the Linux-specific update to |
| 1769 | * the timeval to cause a fault after the select has |
| 1770 | * completed successfully. However, because we're not |
| 1771 | * updating the timeval, we can't restart the system |
| 1772 | * call. |
| 1773 | */ |
| 1774 | if (ret == -ERESTARTNOHAND) |
| 1775 | ret = -EINTR; |
| 1776 | } |
| 1777 | } |
| 1778 | |
| 1779 | return ret; |
| 1780 | } |
| 1781 | |
| 1782 | #ifdef TIF_RESTORE_SIGMASK |
| 1783 | asmlinkage long compat_sys_pselect7(int n, compat_ulong_t __user *inp, |
| 1784 | compat_ulong_t __user *outp, compat_ulong_t __user *exp, |
| 1785 | struct compat_timespec __user *tsp, compat_sigset_t __user *sigmask, |
| 1786 | compat_size_t sigsetsize) |
| 1787 | { |
| 1788 | compat_sigset_t ss32; |
| 1789 | sigset_t ksigmask, sigsaved; |
Andrew Morton | cb82a6c | 2006-02-01 03:04:49 -0800 | [diff] [blame] | 1790 | s64 timeout = MAX_SCHEDULE_TIMEOUT; |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1791 | struct compat_timespec ts; |
| 1792 | int ret; |
| 1793 | |
| 1794 | if (tsp) { |
| 1795 | if (copy_from_user(&ts, tsp, sizeof(ts))) |
| 1796 | return -EFAULT; |
| 1797 | |
| 1798 | if (ts.tv_sec < 0 || ts.tv_nsec < 0) |
| 1799 | return -EINVAL; |
| 1800 | } |
| 1801 | |
| 1802 | if (sigmask) { |
| 1803 | if (sigsetsize != sizeof(compat_sigset_t)) |
| 1804 | return -EINVAL; |
| 1805 | if (copy_from_user(&ss32, sigmask, sizeof(ss32))) |
| 1806 | return -EFAULT; |
| 1807 | sigset_from_compat(&ksigmask, &ss32); |
| 1808 | |
| 1809 | sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP)); |
| 1810 | sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved); |
| 1811 | } |
| 1812 | |
| 1813 | do { |
| 1814 | if (tsp) { |
| 1815 | if ((unsigned long)ts.tv_sec < MAX_SELECT_SECONDS) { |
| 1816 | timeout = ROUND_UP(ts.tv_nsec, 1000000000/HZ); |
| 1817 | timeout += ts.tv_sec * (unsigned long)HZ; |
| 1818 | ts.tv_sec = 0; |
| 1819 | ts.tv_nsec = 0; |
| 1820 | } else { |
| 1821 | ts.tv_sec -= MAX_SELECT_SECONDS; |
| 1822 | timeout = MAX_SELECT_SECONDS * HZ; |
| 1823 | } |
| 1824 | } |
| 1825 | |
| 1826 | ret = compat_core_sys_select(n, inp, outp, exp, &timeout); |
| 1827 | |
| 1828 | } while (!ret && !timeout && tsp && (ts.tv_sec || ts.tv_nsec)); |
| 1829 | |
Andi Kleen | 7583334 | 2006-09-26 10:52:39 +0200 | [diff] [blame] | 1830 | if (ret == 0 && tsp && !(current->personality & STICKY_TIMEOUTS)) { |
Andrew Morton | 643a654 | 2006-02-11 17:55:52 -0800 | [diff] [blame] | 1831 | struct compat_timespec rts; |
| 1832 | |
| 1833 | rts.tv_sec = timeout / HZ; |
| 1834 | rts.tv_nsec = (timeout % HZ) * (NSEC_PER_SEC/HZ); |
| 1835 | if (rts.tv_nsec >= NSEC_PER_SEC) { |
| 1836 | rts.tv_sec++; |
| 1837 | rts.tv_nsec -= NSEC_PER_SEC; |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1838 | } |
Andrew Morton | 74910e6 | 2006-02-17 13:52:58 -0800 | [diff] [blame] | 1839 | if (compat_timespec_compare(&rts, &ts) >= 0) |
Andrew Morton | 643a654 | 2006-02-11 17:55:52 -0800 | [diff] [blame] | 1840 | rts = ts; |
Andi Kleen | 7583334 | 2006-09-26 10:52:39 +0200 | [diff] [blame] | 1841 | if (copy_to_user(tsp, &rts, sizeof(rts))) |
| 1842 | ret = -EFAULT; |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1843 | } |
| 1844 | |
| 1845 | if (ret == -ERESTARTNOHAND) { |
| 1846 | /* |
| 1847 | * Don't restore the signal mask yet. Let do_signal() deliver |
| 1848 | * the signal on the way back to userspace, before the signal |
| 1849 | * mask is restored. |
| 1850 | */ |
| 1851 | if (sigmask) { |
| 1852 | memcpy(¤t->saved_sigmask, &sigsaved, |
| 1853 | sizeof(sigsaved)); |
| 1854 | set_thread_flag(TIF_RESTORE_SIGMASK); |
| 1855 | } |
| 1856 | } else if (sigmask) |
| 1857 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); |
| 1858 | |
| 1859 | return ret; |
| 1860 | } |
| 1861 | |
| 1862 | asmlinkage long compat_sys_pselect6(int n, compat_ulong_t __user *inp, |
| 1863 | compat_ulong_t __user *outp, compat_ulong_t __user *exp, |
| 1864 | struct compat_timespec __user *tsp, void __user *sig) |
| 1865 | { |
| 1866 | compat_size_t sigsetsize = 0; |
| 1867 | compat_uptr_t up = 0; |
| 1868 | |
| 1869 | if (sig) { |
| 1870 | if (!access_ok(VERIFY_READ, sig, |
| 1871 | sizeof(compat_uptr_t)+sizeof(compat_size_t)) || |
| 1872 | __get_user(up, (compat_uptr_t __user *)sig) || |
| 1873 | __get_user(sigsetsize, |
| 1874 | (compat_size_t __user *)(sig+sizeof(up)))) |
| 1875 | return -EFAULT; |
| 1876 | } |
| 1877 | return compat_sys_pselect7(n, inp, outp, exp, tsp, compat_ptr(up), |
| 1878 | sigsetsize); |
| 1879 | } |
| 1880 | |
| 1881 | asmlinkage long compat_sys_ppoll(struct pollfd __user *ufds, |
| 1882 | unsigned int nfds, struct compat_timespec __user *tsp, |
| 1883 | const compat_sigset_t __user *sigmask, compat_size_t sigsetsize) |
| 1884 | { |
| 1885 | compat_sigset_t ss32; |
| 1886 | sigset_t ksigmask, sigsaved; |
| 1887 | struct compat_timespec ts; |
| 1888 | s64 timeout = -1; |
| 1889 | int ret; |
| 1890 | |
| 1891 | if (tsp) { |
| 1892 | if (copy_from_user(&ts, tsp, sizeof(ts))) |
| 1893 | return -EFAULT; |
| 1894 | |
| 1895 | /* We assume that ts.tv_sec is always lower than |
| 1896 | the number of seconds that can be expressed in |
| 1897 | an s64. Otherwise the compiler bitches at us */ |
David S. Miller | 7e732bf | 2006-01-19 16:40:42 -0800 | [diff] [blame] | 1898 | timeout = ROUND_UP(ts.tv_nsec, 1000000000/HZ); |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1899 | timeout += ts.tv_sec * HZ; |
| 1900 | } |
| 1901 | |
| 1902 | if (sigmask) { |
Alexey Dobriyan | 3835a9b | 2006-05-15 09:44:27 -0700 | [diff] [blame] | 1903 | if (sigsetsize != sizeof(compat_sigset_t)) |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1904 | return -EINVAL; |
| 1905 | if (copy_from_user(&ss32, sigmask, sizeof(ss32))) |
| 1906 | return -EFAULT; |
| 1907 | sigset_from_compat(&ksigmask, &ss32); |
| 1908 | |
| 1909 | sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP)); |
| 1910 | sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved); |
| 1911 | } |
| 1912 | |
| 1913 | ret = do_sys_poll(ufds, nfds, &timeout); |
| 1914 | |
| 1915 | /* We can restart this syscall, usually */ |
| 1916 | if (ret == -EINTR) { |
| 1917 | /* |
| 1918 | * Don't restore the signal mask yet. Let do_signal() deliver |
| 1919 | * the signal on the way back to userspace, before the signal |
| 1920 | * mask is restored. |
| 1921 | */ |
| 1922 | if (sigmask) { |
| 1923 | memcpy(¤t->saved_sigmask, &sigsaved, |
| 1924 | sizeof(sigsaved)); |
| 1925 | set_thread_flag(TIF_RESTORE_SIGMASK); |
| 1926 | } |
| 1927 | ret = -ERESTARTNOHAND; |
| 1928 | } else if (sigmask) |
| 1929 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); |
| 1930 | |
| 1931 | if (tsp && timeout >= 0) { |
Andrew Morton | 643a654 | 2006-02-11 17:55:52 -0800 | [diff] [blame] | 1932 | struct compat_timespec rts; |
| 1933 | |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1934 | if (current->personality & STICKY_TIMEOUTS) |
| 1935 | goto sticky; |
| 1936 | /* Yes, we know it's actually an s64, but it's also positive. */ |
Andrew Morton | 643a654 | 2006-02-11 17:55:52 -0800 | [diff] [blame] | 1937 | rts.tv_nsec = jiffies_to_usecs(do_div((*(u64*)&timeout), HZ)) * |
| 1938 | 1000; |
| 1939 | rts.tv_sec = timeout; |
Andrew Morton | 74910e6 | 2006-02-17 13:52:58 -0800 | [diff] [blame] | 1940 | if (compat_timespec_compare(&rts, &ts) >= 0) |
Andrew Morton | 643a654 | 2006-02-11 17:55:52 -0800 | [diff] [blame] | 1941 | rts = ts; |
| 1942 | if (copy_to_user(tsp, &rts, sizeof(rts))) { |
David Woodhouse | 9f72949 | 2006-01-18 17:44:05 -0800 | [diff] [blame] | 1943 | sticky: |
| 1944 | /* |
| 1945 | * If an application puts its timeval in read-only |
| 1946 | * memory, we don't want the Linux-specific update to |
| 1947 | * the timeval to cause a fault after the select has |
| 1948 | * completed successfully. However, because we're not |
| 1949 | * updating the timeval, we can't restart the system |
| 1950 | * call. |
| 1951 | */ |
| 1952 | if (ret == -ERESTARTNOHAND && timeout >= 0) |
| 1953 | ret = -EINTR; |
| 1954 | } |
| 1955 | } |
| 1956 | |
| 1957 | return ret; |
| 1958 | } |
| 1959 | #endif /* TIF_RESTORE_SIGMASK */ |
| 1960 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | #if defined(CONFIG_NFSD) || defined(CONFIG_NFSD_MODULE) |
| 1962 | /* Stuff for NFS server syscalls... */ |
| 1963 | struct compat_nfsctl_svc { |
| 1964 | u16 svc32_port; |
| 1965 | s32 svc32_nthreads; |
| 1966 | }; |
| 1967 | |
| 1968 | struct compat_nfsctl_client { |
| 1969 | s8 cl32_ident[NFSCLNT_IDMAX+1]; |
| 1970 | s32 cl32_naddr; |
| 1971 | struct in_addr cl32_addrlist[NFSCLNT_ADDRMAX]; |
| 1972 | s32 cl32_fhkeytype; |
| 1973 | s32 cl32_fhkeylen; |
| 1974 | u8 cl32_fhkey[NFSCLNT_KEYMAX]; |
| 1975 | }; |
| 1976 | |
| 1977 | struct compat_nfsctl_export { |
| 1978 | char ex32_client[NFSCLNT_IDMAX+1]; |
| 1979 | char ex32_path[NFS_MAXPATHLEN+1]; |
| 1980 | compat_dev_t ex32_dev; |
| 1981 | compat_ino_t ex32_ino; |
| 1982 | compat_int_t ex32_flags; |
Stephen Rothwell | 202e597 | 2005-09-06 15:16:40 -0700 | [diff] [blame] | 1983 | __compat_uid_t ex32_anon_uid; |
| 1984 | __compat_gid_t ex32_anon_gid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1985 | }; |
| 1986 | |
| 1987 | struct compat_nfsctl_fdparm { |
| 1988 | struct sockaddr gd32_addr; |
| 1989 | s8 gd32_path[NFS_MAXPATHLEN+1]; |
| 1990 | compat_int_t gd32_version; |
| 1991 | }; |
| 1992 | |
| 1993 | struct compat_nfsctl_fsparm { |
| 1994 | struct sockaddr gd32_addr; |
| 1995 | s8 gd32_path[NFS_MAXPATHLEN+1]; |
| 1996 | compat_int_t gd32_maxlen; |
| 1997 | }; |
| 1998 | |
| 1999 | struct compat_nfsctl_arg { |
| 2000 | compat_int_t ca32_version; /* safeguard */ |
| 2001 | union { |
| 2002 | struct compat_nfsctl_svc u32_svc; |
| 2003 | struct compat_nfsctl_client u32_client; |
| 2004 | struct compat_nfsctl_export u32_export; |
| 2005 | struct compat_nfsctl_fdparm u32_getfd; |
| 2006 | struct compat_nfsctl_fsparm u32_getfs; |
| 2007 | } u; |
| 2008 | #define ca32_svc u.u32_svc |
| 2009 | #define ca32_client u.u32_client |
| 2010 | #define ca32_export u.u32_export |
| 2011 | #define ca32_getfd u.u32_getfd |
| 2012 | #define ca32_getfs u.u32_getfs |
| 2013 | }; |
| 2014 | |
| 2015 | union compat_nfsctl_res { |
| 2016 | __u8 cr32_getfh[NFS_FHSIZE]; |
| 2017 | struct knfsd_fh cr32_getfs; |
| 2018 | }; |
| 2019 | |
Lin Feng Shen | d64b1c8 | 2006-05-20 14:59:49 -0700 | [diff] [blame] | 2020 | static int compat_nfs_svc_trans(struct nfsctl_arg *karg, |
| 2021 | struct compat_nfsctl_arg __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | { |
Lin Feng Shen | d64b1c8 | 2006-05-20 14:59:49 -0700 | [diff] [blame] | 2023 | if (!access_ok(VERIFY_READ, &arg->ca32_svc, sizeof(arg->ca32_svc)) || |
| 2024 | get_user(karg->ca_version, &arg->ca32_version) || |
| 2025 | __get_user(karg->ca_svc.svc_port, &arg->ca32_svc.svc32_port) || |
| 2026 | __get_user(karg->ca_svc.svc_nthreads, |
| 2027 | &arg->ca32_svc.svc32_nthreads)) |
| 2028 | return -EFAULT; |
| 2029 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2030 | } |
| 2031 | |
Lin Feng Shen | d64b1c8 | 2006-05-20 14:59:49 -0700 | [diff] [blame] | 2032 | static int compat_nfs_clnt_trans(struct nfsctl_arg *karg, |
| 2033 | struct compat_nfsctl_arg __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2034 | { |
Lin Feng Shen | d64b1c8 | 2006-05-20 14:59:49 -0700 | [diff] [blame] | 2035 | if (!access_ok(VERIFY_READ, &arg->ca32_client, |
| 2036 | sizeof(arg->ca32_client)) || |
| 2037 | get_user(karg->ca_version, &arg->ca32_version) || |
| 2038 | __copy_from_user(&karg->ca_client.cl_ident[0], |
| 2039 | &arg->ca32_client.cl32_ident[0], |
| 2040 | NFSCLNT_IDMAX) || |
| 2041 | __get_user(karg->ca_client.cl_naddr, |
| 2042 | &arg->ca32_client.cl32_naddr) || |
| 2043 | __copy_from_user(&karg->ca_client.cl_addrlist[0], |
| 2044 | &arg->ca32_client.cl32_addrlist[0], |
| 2045 | (sizeof(struct in_addr) * NFSCLNT_ADDRMAX)) || |
| 2046 | __get_user(karg->ca_client.cl_fhkeytype, |
| 2047 | &arg->ca32_client.cl32_fhkeytype) || |
| 2048 | __get_user(karg->ca_client.cl_fhkeylen, |
| 2049 | &arg->ca32_client.cl32_fhkeylen) || |
| 2050 | __copy_from_user(&karg->ca_client.cl_fhkey[0], |
| 2051 | &arg->ca32_client.cl32_fhkey[0], |
| 2052 | NFSCLNT_KEYMAX)) |
| 2053 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | |
Lin Feng Shen | d64b1c8 | 2006-05-20 14:59:49 -0700 | [diff] [blame] | 2055 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | } |
| 2057 | |
Lin Feng Shen | d64b1c8 | 2006-05-20 14:59:49 -0700 | [diff] [blame] | 2058 | static int compat_nfs_exp_trans(struct nfsctl_arg *karg, |
| 2059 | struct compat_nfsctl_arg __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | { |
Lin Feng Shen | d64b1c8 | 2006-05-20 14:59:49 -0700 | [diff] [blame] | 2061 | if (!access_ok(VERIFY_READ, &arg->ca32_export, |
| 2062 | sizeof(arg->ca32_export)) || |
| 2063 | get_user(karg->ca_version, &arg->ca32_version) || |
| 2064 | __copy_from_user(&karg->ca_export.ex_client[0], |
| 2065 | &arg->ca32_export.ex32_client[0], |
| 2066 | NFSCLNT_IDMAX) || |
| 2067 | __copy_from_user(&karg->ca_export.ex_path[0], |
| 2068 | &arg->ca32_export.ex32_path[0], |
| 2069 | NFS_MAXPATHLEN) || |
| 2070 | __get_user(karg->ca_export.ex_dev, |
| 2071 | &arg->ca32_export.ex32_dev) || |
| 2072 | __get_user(karg->ca_export.ex_ino, |
| 2073 | &arg->ca32_export.ex32_ino) || |
| 2074 | __get_user(karg->ca_export.ex_flags, |
| 2075 | &arg->ca32_export.ex32_flags) || |
| 2076 | __get_user(karg->ca_export.ex_anon_uid, |
| 2077 | &arg->ca32_export.ex32_anon_uid) || |
| 2078 | __get_user(karg->ca_export.ex_anon_gid, |
| 2079 | &arg->ca32_export.ex32_anon_gid)) |
| 2080 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2081 | SET_UID(karg->ca_export.ex_anon_uid, karg->ca_export.ex_anon_uid); |
| 2082 | SET_GID(karg->ca_export.ex_anon_gid, karg->ca_export.ex_anon_gid); |
| 2083 | |
Lin Feng Shen | d64b1c8 | 2006-05-20 14:59:49 -0700 | [diff] [blame] | 2084 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | } |
| 2086 | |
Lin Feng Shen | d64b1c8 | 2006-05-20 14:59:49 -0700 | [diff] [blame] | 2087 | static int compat_nfs_getfd_trans(struct nfsctl_arg *karg, |
| 2088 | struct compat_nfsctl_arg __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2089 | { |
Lin Feng Shen | d64b1c8 | 2006-05-20 14:59:49 -0700 | [diff] [blame] | 2090 | if (!access_ok(VERIFY_READ, &arg->ca32_getfd, |
| 2091 | sizeof(arg->ca32_getfd)) || |
| 2092 | get_user(karg->ca_version, &arg->ca32_version) || |
| 2093 | __copy_from_user(&karg->ca_getfd.gd_addr, |
| 2094 | &arg->ca32_getfd.gd32_addr, |
| 2095 | (sizeof(struct sockaddr))) || |
| 2096 | __copy_from_user(&karg->ca_getfd.gd_path, |
| 2097 | &arg->ca32_getfd.gd32_path, |
| 2098 | (NFS_MAXPATHLEN+1)) || |
| 2099 | __get_user(karg->ca_getfd.gd_version, |
| 2100 | &arg->ca32_getfd.gd32_version)) |
| 2101 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2102 | |
Lin Feng Shen | d64b1c8 | 2006-05-20 14:59:49 -0700 | [diff] [blame] | 2103 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | } |
| 2105 | |
Lin Feng Shen | d64b1c8 | 2006-05-20 14:59:49 -0700 | [diff] [blame] | 2106 | static int compat_nfs_getfs_trans(struct nfsctl_arg *karg, |
| 2107 | struct compat_nfsctl_arg __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2108 | { |
Lin Feng Shen | d64b1c8 | 2006-05-20 14:59:49 -0700 | [diff] [blame] | 2109 | if (!access_ok(VERIFY_READ,&arg->ca32_getfs,sizeof(arg->ca32_getfs)) || |
| 2110 | get_user(karg->ca_version, &arg->ca32_version) || |
| 2111 | __copy_from_user(&karg->ca_getfs.gd_addr, |
| 2112 | &arg->ca32_getfs.gd32_addr, |
| 2113 | (sizeof(struct sockaddr))) || |
| 2114 | __copy_from_user(&karg->ca_getfs.gd_path, |
| 2115 | &arg->ca32_getfs.gd32_path, |
| 2116 | (NFS_MAXPATHLEN+1)) || |
| 2117 | __get_user(karg->ca_getfs.gd_maxlen, |
| 2118 | &arg->ca32_getfs.gd32_maxlen)) |
| 2119 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | |
Lin Feng Shen | d64b1c8 | 2006-05-20 14:59:49 -0700 | [diff] [blame] | 2121 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | } |
| 2123 | |
| 2124 | /* This really doesn't need translations, we are only passing |
| 2125 | * back a union which contains opaque nfs file handle data. |
| 2126 | */ |
Lin Feng Shen | d64b1c8 | 2006-05-20 14:59:49 -0700 | [diff] [blame] | 2127 | static int compat_nfs_getfh_res_trans(union nfsctl_res *kres, |
| 2128 | union compat_nfsctl_res __user *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | { |
| 2130 | int err; |
| 2131 | |
| 2132 | err = copy_to_user(res, kres, sizeof(*res)); |
| 2133 | |
| 2134 | return (err) ? -EFAULT : 0; |
| 2135 | } |
| 2136 | |
Lin Feng Shen | d64b1c8 | 2006-05-20 14:59:49 -0700 | [diff] [blame] | 2137 | asmlinkage long compat_sys_nfsservctl(int cmd, |
| 2138 | struct compat_nfsctl_arg __user *arg, |
| 2139 | union compat_nfsctl_res __user *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | { |
| 2141 | struct nfsctl_arg *karg; |
| 2142 | union nfsctl_res *kres; |
| 2143 | mm_segment_t oldfs; |
| 2144 | int err; |
| 2145 | |
| 2146 | karg = kmalloc(sizeof(*karg), GFP_USER); |
| 2147 | kres = kmalloc(sizeof(*kres), GFP_USER); |
| 2148 | if(!karg || !kres) { |
| 2149 | err = -ENOMEM; |
| 2150 | goto done; |
| 2151 | } |
| 2152 | |
| 2153 | switch(cmd) { |
| 2154 | case NFSCTL_SVC: |
| 2155 | err = compat_nfs_svc_trans(karg, arg); |
| 2156 | break; |
| 2157 | |
| 2158 | case NFSCTL_ADDCLIENT: |
| 2159 | err = compat_nfs_clnt_trans(karg, arg); |
| 2160 | break; |
| 2161 | |
| 2162 | case NFSCTL_DELCLIENT: |
| 2163 | err = compat_nfs_clnt_trans(karg, arg); |
| 2164 | break; |
| 2165 | |
| 2166 | case NFSCTL_EXPORT: |
| 2167 | case NFSCTL_UNEXPORT: |
| 2168 | err = compat_nfs_exp_trans(karg, arg); |
| 2169 | break; |
| 2170 | |
| 2171 | case NFSCTL_GETFD: |
| 2172 | err = compat_nfs_getfd_trans(karg, arg); |
| 2173 | break; |
| 2174 | |
| 2175 | case NFSCTL_GETFS: |
| 2176 | err = compat_nfs_getfs_trans(karg, arg); |
| 2177 | break; |
| 2178 | |
| 2179 | default: |
| 2180 | err = -EINVAL; |
Peter Staubach | 57070d0 | 2006-03-25 03:08:04 -0800 | [diff] [blame] | 2181 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | } |
| 2183 | |
Peter Staubach | 57070d0 | 2006-03-25 03:08:04 -0800 | [diff] [blame] | 2184 | if (err) |
| 2185 | goto done; |
| 2186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | oldfs = get_fs(); |
| 2188 | set_fs(KERNEL_DS); |
| 2189 | /* The __user pointer casts are valid because of the set_fs() */ |
| 2190 | err = sys_nfsservctl(cmd, (void __user *) karg, (void __user *) kres); |
| 2191 | set_fs(oldfs); |
| 2192 | |
| 2193 | if (err) |
| 2194 | goto done; |
| 2195 | |
| 2196 | if((cmd == NFSCTL_GETFD) || |
| 2197 | (cmd == NFSCTL_GETFS)) |
| 2198 | err = compat_nfs_getfh_res_trans(kres, res); |
| 2199 | |
| 2200 | done: |
| 2201 | kfree(karg); |
| 2202 | kfree(kres); |
| 2203 | return err; |
| 2204 | } |
| 2205 | #else /* !NFSD */ |
| 2206 | long asmlinkage compat_sys_nfsservctl(int cmd, void *notused, void *notused2) |
| 2207 | { |
| 2208 | return sys_ni_syscall(); |
| 2209 | } |
| 2210 | #endif |