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