1 /* 2 * Copyright 2008 Red Hat, Inc. All rights reserved. 3 * Copyright 2008 Ian Kent <raven@themaw.net> 4 * 5 * This file is part of the Linux kernel and is made available under 6 * the terms of the GNU General Public License, version 2, or at your 7 * option, any later version, incorporated herein by reference. 8 */ 9 10 #include <linux/miscdevice.h> 11 #include <linux/compat.h> 12 #include <linux/syscalls.h> 13 #include <linux/magic.h> 14 15 #include "autofs_i.h" 16 17 /* 18 * This module implements an interface for routing autofs ioctl control 19 * commands via a miscellaneous device file. 20 * 21 * The alternate interface is needed because we need to be able open 22 * an ioctl file descriptor on an autofs mount that may be covered by 23 * another mount. This situation arises when starting automount(8) 24 * or other user space daemon which uses direct mounts or offset 25 * mounts (used for autofs lazy mount/umount of nested mount trees), 26 * which have been left busy at at service shutdown. 27 */ 28 29 typedef int (*ioctl_fn)(struct file *, struct autofs_sb_info *, 30 struct autofs_dev_ioctl *); 31 32 static int check_name(const char *name) 33 { 34 if (!strchr(name, '/')) 35 return -EINVAL; 36 return 0; 37 } 38 39 /* 40 * Check a string doesn't overrun the chunk of 41 * memory we copied from user land. 42 */ 43 static int invalid_str(char *str, size_t size) 44 { 45 if (memchr(str, 0, size)) 46 return 0; 47 return -EINVAL; 48 } 49 50 /* 51 * Check that the user compiled against correct version of autofs 52 * misc device code. 53 * 54 * As well as checking the version compatibility this always copies 55 * the kernel interface version out. 56 */ 57 static int check_dev_ioctl_version(int cmd, struct autofs_dev_ioctl *param) 58 { 59 int err = 0; 60 61 if ((param->ver_major != AUTOFS_DEV_IOCTL_VERSION_MAJOR) || 62 (param->ver_minor > AUTOFS_DEV_IOCTL_VERSION_MINOR)) { 63 pr_warn("ioctl control interface version mismatch: " 64 "kernel(%u.%u), user(%u.%u), cmd(0x%08x)\n", 65 AUTOFS_DEV_IOCTL_VERSION_MAJOR, 66 AUTOFS_DEV_IOCTL_VERSION_MINOR, 67 param->ver_major, param->ver_minor, cmd); 68 err = -EINVAL; 69 } 70 71 /* Fill in the kernel version. */ 72 param->ver_major = AUTOFS_DEV_IOCTL_VERSION_MAJOR; 73 param->ver_minor = AUTOFS_DEV_IOCTL_VERSION_MINOR; 74 75 return err; 76 } 77 78 /* 79 * Copy parameter control struct, including a possible path allocated 80 * at the end of the struct. 81 */ 82 static struct autofs_dev_ioctl * 83 copy_dev_ioctl(struct autofs_dev_ioctl __user *in) 84 { 85 struct autofs_dev_ioctl tmp, *res; 86 87 if (copy_from_user(&tmp, in, AUTOFS_DEV_IOCTL_SIZE)) 88 return ERR_PTR(-EFAULT); 89 90 if (tmp.size < AUTOFS_DEV_IOCTL_SIZE) 91 return ERR_PTR(-EINVAL); 92 93 if (tmp.size > AUTOFS_DEV_IOCTL_SIZE + PATH_MAX) 94 return ERR_PTR(-ENAMETOOLONG); 95 96 res = memdup_user(in, tmp.size); 97 if (!IS_ERR(res)) 98 res->size = tmp.size; 99 100 return res; 101 } 102 103 static inline void free_dev_ioctl(struct autofs_dev_ioctl *param) 104 { 105 kfree(param); 106 } 107 108 /* 109 * Check sanity of parameter control fields and if a path is present 110 * check that it is terminated and contains at least one "/". 111 */ 112 static int validate_dev_ioctl(int cmd, struct autofs_dev_ioctl *param) 113 { 114 int err; 115 116 err = check_dev_ioctl_version(cmd, param); 117 if (err) { 118 pr_warn("invalid device control module version " 119 "supplied for cmd(0x%08x)\n", cmd); 120 goto out; 121 } 122 123 if (param->size > AUTOFS_DEV_IOCTL_SIZE) { 124 err = invalid_str(param->path, param->size - AUTOFS_DEV_IOCTL_SIZE); 125 if (err) { 126 pr_warn( 127 "path string terminator missing for cmd(0x%08x)\n", 128 cmd); 129 goto out; 130 } 131 132 err = check_name(param->path); 133 if (err) { 134 pr_warn("invalid path supplied for cmd(0x%08x)\n", 135 cmd); 136 goto out; 137 } 138 } 139 140 err = 0; 141 out: 142 return err; 143 } 144 145 /* 146 * Get the autofs super block info struct from the file opened on 147 * the autofs mount point. 148 */ 149 static struct autofs_sb_info *autofs_dev_ioctl_sbi(struct file *f) 150 { 151 struct autofs_sb_info *sbi = NULL; 152 struct inode *inode; 153 154 if (f) { 155 inode = file_inode(f); 156 sbi = autofs_sbi(inode->i_sb); 157 } 158 return sbi; 159 } 160 161 /* Return autofs dev ioctl version */ 162 static int autofs_dev_ioctl_version(struct file *fp, 163 struct autofs_sb_info *sbi, 164 struct autofs_dev_ioctl *param) 165 { 166 /* This should have already been set. */ 167 param->ver_major = AUTOFS_DEV_IOCTL_VERSION_MAJOR; 168 param->ver_minor = AUTOFS_DEV_IOCTL_VERSION_MINOR; 169 return 0; 170 } 171 172 /* Return autofs module protocol version */ 173 static int autofs_dev_ioctl_protover(struct file *fp, 174 struct autofs_sb_info *sbi, 175 struct autofs_dev_ioctl *param) 176 { 177 param->protover.version = sbi->version; 178 return 0; 179 } 180 181 /* Return autofs module protocol sub version */ 182 static int autofs_dev_ioctl_protosubver(struct file *fp, 183 struct autofs_sb_info *sbi, 184 struct autofs_dev_ioctl *param) 185 { 186 param->protosubver.sub_version = sbi->sub_version; 187 return 0; 188 } 189 190 /* Find the topmost mount satisfying test() */ 191 static int find_autofs_mount(const char *pathname, 192 struct path *res, 193 int test(const struct path *path, void *data), 194 void *data) 195 { 196 struct path path; 197 int err; 198 199 err = kern_path_mountpoint(AT_FDCWD, pathname, &path, 0); 200 if (err) 201 return err; 202 err = -ENOENT; 203 while (path.dentry == path.mnt->mnt_root) { 204 if (path.dentry->d_sb->s_magic == AUTOFS_SUPER_MAGIC) { 205 if (test(&path, data)) { 206 path_get(&path); 207 *res = path; 208 err = 0; 209 break; 210 } 211 } 212 if (!follow_up(&path)) 213 break; 214 } 215 path_put(&path); 216 return err; 217 } 218 219 static int test_by_dev(const struct path *path, void *p) 220 { 221 return path->dentry->d_sb->s_dev == *(dev_t *)p; 222 } 223 224 static int test_by_type(const struct path *path, void *p) 225 { 226 struct autofs_info *ino = autofs_dentry_ino(path->dentry); 227 228 return ino && ino->sbi->type & *(unsigned *)p; 229 } 230 231 /* 232 * Open a file descriptor on the autofs mount point corresponding 233 * to the given path and device number (aka. new_encode_dev(sb->s_dev)). 234 */ 235 static int autofs_dev_ioctl_open_mountpoint(const char *name, dev_t devid) 236 { 237 int err, fd; 238 239 fd = get_unused_fd_flags(O_CLOEXEC); 240 if (likely(fd >= 0)) { 241 struct file *filp; 242 struct path path; 243 244 err = find_autofs_mount(name, &path, test_by_dev, &devid); 245 if (err) 246 goto out; 247 248 filp = dentry_open(&path, O_RDONLY, current_cred()); 249 path_put(&path); 250 if (IS_ERR(filp)) { 251 err = PTR_ERR(filp); 252 goto out; 253 } 254 255 fd_install(fd, filp); 256 } 257 258 return fd; 259 260 out: 261 put_unused_fd(fd); 262 return err; 263 } 264 265 /* Open a file descriptor on an autofs mount point */ 266 static int autofs_dev_ioctl_openmount(struct file *fp, 267 struct autofs_sb_info *sbi, 268 struct autofs_dev_ioctl *param) 269 { 270 const char *path; 271 dev_t devid; 272 int err, fd; 273 274 /* param->path has already been checked */ 275 if (!param->openmount.devid) 276 return -EINVAL; 277 278 param->ioctlfd = -1; 279 280 path = param->path; 281 devid = new_decode_dev(param->openmount.devid); 282 283 err = 0; 284 fd = autofs_dev_ioctl_open_mountpoint(path, devid); 285 if (unlikely(fd < 0)) { 286 err = fd; 287 goto out; 288 } 289 290 param->ioctlfd = fd; 291 out: 292 return err; 293 } 294 295 /* Close file descriptor allocated above (user can also use close(2)). */ 296 static int autofs_dev_ioctl_closemount(struct file *fp, 297 struct autofs_sb_info *sbi, 298 struct autofs_dev_ioctl *param) 299 { 300 return ksys_close(param->ioctlfd); 301 } 302 303 /* 304 * Send "ready" status for an existing wait (either a mount or an expire 305 * request). 306 */ 307 static int autofs_dev_ioctl_ready(struct file *fp, 308 struct autofs_sb_info *sbi, 309 struct autofs_dev_ioctl *param) 310 { 311 autofs_wqt_t token; 312 313 token = (autofs_wqt_t) param->ready.token; 314 return autofs_wait_release(sbi, token, 0); 315 } 316 317 /* 318 * Send "fail" status for an existing wait (either a mount or an expire 319 * request). 320 */ 321 static int autofs_dev_ioctl_fail(struct file *fp, 322 struct autofs_sb_info *sbi, 323 struct autofs_dev_ioctl *param) 324 { 325 autofs_wqt_t token; 326 int status; 327 328 token = (autofs_wqt_t) param->fail.token; 329 status = param->fail.status < 0 ? param->fail.status : -ENOENT; 330 return autofs_wait_release(sbi, token, status); 331 } 332 333 /* 334 * Set the pipe fd for kernel communication to the daemon. 335 * 336 * Normally this is set at mount using an option but if we 337 * are reconnecting to a busy mount then we need to use this 338 * to tell the autofs mount about the new kernel pipe fd. In 339 * order to protect mounts against incorrectly setting the 340 * pipefd we also require that the autofs mount be catatonic. 341 * 342 * This also sets the process group id used to identify the 343 * controlling process (eg. the owning automount(8) daemon). 344 */ 345 static int autofs_dev_ioctl_setpipefd(struct file *fp, 346 struct autofs_sb_info *sbi, 347 struct autofs_dev_ioctl *param) 348 { 349 int pipefd; 350 int err = 0; 351 struct pid *new_pid = NULL; 352 353 if (param->setpipefd.pipefd == -1) 354 return -EINVAL; 355 356 pipefd = param->setpipefd.pipefd; 357 358 mutex_lock(&sbi->wq_mutex); 359 if (!sbi->catatonic) { 360 mutex_unlock(&sbi->wq_mutex); 361 return -EBUSY; 362 } else { 363 struct file *pipe; 364 365 new_pid = get_task_pid(current, PIDTYPE_PGID); 366 367 if (ns_of_pid(new_pid) != ns_of_pid(sbi->oz_pgrp)) { 368 pr_warn("not allowed to change PID namespace\n"); 369 err = -EINVAL; 370 goto out; 371 } 372 373 pipe = fget(pipefd); 374 if (!pipe) { 375 err = -EBADF; 376 goto out; 377 } 378 if (autofs_prepare_pipe(pipe) < 0) { 379 err = -EPIPE; 380 fput(pipe); 381 goto out; 382 } 383 swap(sbi->oz_pgrp, new_pid); 384 sbi->pipefd = pipefd; 385 sbi->pipe = pipe; 386 sbi->catatonic = 0; 387 } 388 out: 389 put_pid(new_pid); 390 mutex_unlock(&sbi->wq_mutex); 391 return err; 392 } 393 394 /* 395 * Make the autofs mount point catatonic, no longer responsive to 396 * mount requests. Also closes the kernel pipe file descriptor. 397 */ 398 static int autofs_dev_ioctl_catatonic(struct file *fp, 399 struct autofs_sb_info *sbi, 400 struct autofs_dev_ioctl *param) 401 { 402 autofs_catatonic_mode(sbi); 403 return 0; 404 } 405 406 /* Set the autofs mount timeout */ 407 static int autofs_dev_ioctl_timeout(struct file *fp, 408 struct autofs_sb_info *sbi, 409 struct autofs_dev_ioctl *param) 410 { 411 unsigned long timeout; 412 413 timeout = param->timeout.timeout; 414 param->timeout.timeout = sbi->exp_timeout / HZ; 415 sbi->exp_timeout = timeout * HZ; 416 return 0; 417 } 418 419 /* 420 * Return the uid and gid of the last request for the mount 421 * 422 * When reconstructing an autofs mount tree with active mounts 423 * we need to re-connect to mounts that may have used the original 424 * process uid and gid (or string variations of them) for mount 425 * lookups within the map entry. 426 */ 427 static int autofs_dev_ioctl_requester(struct file *fp, 428 struct autofs_sb_info *sbi, 429 struct autofs_dev_ioctl *param) 430 { 431 struct autofs_info *ino; 432 struct path path; 433 dev_t devid; 434 int err = -ENOENT; 435 436 if (param->size <= AUTOFS_DEV_IOCTL_SIZE) { 437 err = -EINVAL; 438 goto out; 439 } 440 441 devid = sbi->sb->s_dev; 442 443 param->requester.uid = param->requester.gid = -1; 444 445 err = find_autofs_mount(param->path, &path, test_by_dev, &devid); 446 if (err) 447 goto out; 448 449 ino = autofs_dentry_ino(path.dentry); 450 if (ino) { 451 err = 0; 452 autofs_expire_wait(&path, 0); 453 spin_lock(&sbi->fs_lock); 454 param->requester.uid = 455 from_kuid_munged(current_user_ns(), ino->uid); 456 param->requester.gid = 457 from_kgid_munged(current_user_ns(), ino->gid); 458 spin_unlock(&sbi->fs_lock); 459 } 460 path_put(&path); 461 out: 462 return err; 463 } 464 465 /* 466 * Call repeatedly until it returns -EAGAIN, meaning there's nothing 467 * more that can be done. 468 */ 469 static int autofs_dev_ioctl_expire(struct file *fp, 470 struct autofs_sb_info *sbi, 471 struct autofs_dev_ioctl *param) 472 { 473 struct vfsmount *mnt; 474 int how; 475 476 how = param->expire.how; 477 mnt = fp->f_path.mnt; 478 479 return autofs_do_expire_multi(sbi->sb, mnt, sbi, how); 480 } 481 482 /* Check if autofs mount point is in use */ 483 static int autofs_dev_ioctl_askumount(struct file *fp, 484 struct autofs_sb_info *sbi, 485 struct autofs_dev_ioctl *param) 486 { 487 param->askumount.may_umount = 0; 488 if (may_umount(fp->f_path.mnt)) 489 param->askumount.may_umount = 1; 490 return 0; 491 } 492 493 /* 494 * Check if the given path is a mountpoint. 495 * 496 * If we are supplied with the file descriptor of an autofs 497 * mount we're looking for a specific mount. In this case 498 * the path is considered a mountpoint if it is itself a 499 * mountpoint or contains a mount, such as a multi-mount 500 * without a root mount. In this case we return 1 if the 501 * path is a mount point and the super magic of the covering 502 * mount if there is one or 0 if it isn't a mountpoint. 503 * 504 * If we aren't supplied with a file descriptor then we 505 * lookup the path and check if it is the root of a mount. 506 * If a type is given we are looking for a particular autofs 507 * mount and if we don't find a match we return fail. If the 508 * located path is the root of a mount we return 1 along with 509 * the super magic of the mount or 0 otherwise. 510 * 511 * In both cases the the device number (as returned by 512 * new_encode_dev()) is also returned. 513 */ 514 static int autofs_dev_ioctl_ismountpoint(struct file *fp, 515 struct autofs_sb_info *sbi, 516 struct autofs_dev_ioctl *param) 517 { 518 struct path path; 519 const char *name; 520 unsigned int type; 521 unsigned int devid, magic; 522 int err = -ENOENT; 523 524 if (param->size <= AUTOFS_DEV_IOCTL_SIZE) { 525 err = -EINVAL; 526 goto out; 527 } 528 529 name = param->path; 530 type = param->ismountpoint.in.type; 531 532 param->ismountpoint.out.devid = devid = 0; 533 param->ismountpoint.out.magic = magic = 0; 534 535 if (!fp || param->ioctlfd == -1) { 536 if (autofs_type_any(type)) 537 err = kern_path_mountpoint(AT_FDCWD, 538 name, &path, LOOKUP_FOLLOW); 539 else 540 err = find_autofs_mount(name, &path, 541 test_by_type, &type); 542 if (err) 543 goto out; 544 devid = new_encode_dev(path.dentry->d_sb->s_dev); 545 err = 0; 546 if (path.mnt->mnt_root == path.dentry) { 547 err = 1; 548 magic = path.dentry->d_sb->s_magic; 549 } 550 } else { 551 dev_t dev = sbi->sb->s_dev; 552 553 err = find_autofs_mount(name, &path, test_by_dev, &dev); 554 if (err) 555 goto out; 556 557 devid = new_encode_dev(dev); 558 559 err = path_has_submounts(&path); 560 561 if (follow_down_one(&path)) 562 magic = path.dentry->d_sb->s_magic; 563 } 564 565 param->ismountpoint.out.devid = devid; 566 param->ismountpoint.out.magic = magic; 567 path_put(&path); 568 out: 569 return err; 570 } 571 572 /* 573 * Our range of ioctl numbers isn't 0 based so we need to shift 574 * the array index by _IOC_NR(AUTOFS_CTL_IOC_FIRST) for the table 575 * lookup. 576 */ 577 #define cmd_idx(cmd) (cmd - _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST)) 578 579 static ioctl_fn lookup_dev_ioctl(unsigned int cmd) 580 { 581 static ioctl_fn _ioctls[] = { 582 autofs_dev_ioctl_version, 583 autofs_dev_ioctl_protover, 584 autofs_dev_ioctl_protosubver, 585 autofs_dev_ioctl_openmount, 586 autofs_dev_ioctl_closemount, 587 autofs_dev_ioctl_ready, 588 autofs_dev_ioctl_fail, 589 autofs_dev_ioctl_setpipefd, 590 autofs_dev_ioctl_catatonic, 591 autofs_dev_ioctl_timeout, 592 autofs_dev_ioctl_requester, 593 autofs_dev_ioctl_expire, 594 autofs_dev_ioctl_askumount, 595 autofs_dev_ioctl_ismountpoint, 596 }; 597 unsigned int idx = cmd_idx(cmd); 598 599 return (idx >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[idx]; 600 } 601 602 /* ioctl dispatcher */ 603 static int _autofs_dev_ioctl(unsigned int command, 604 struct autofs_dev_ioctl __user *user) 605 { 606 struct autofs_dev_ioctl *param; 607 struct file *fp; 608 struct autofs_sb_info *sbi; 609 unsigned int cmd_first, cmd; 610 ioctl_fn fn = NULL; 611 int err = 0; 612 613 cmd_first = _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST); 614 cmd = _IOC_NR(command); 615 616 if (_IOC_TYPE(command) != _IOC_TYPE(AUTOFS_DEV_IOCTL_IOC_FIRST) || 617 cmd - cmd_first > AUTOFS_DEV_IOCTL_IOC_COUNT) { 618 return -ENOTTY; 619 } 620 621 /* Only root can use ioctls other than AUTOFS_DEV_IOCTL_VERSION_CMD 622 * and AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD 623 */ 624 if (cmd != AUTOFS_DEV_IOCTL_VERSION_CMD && 625 cmd != AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD && 626 !capable(CAP_SYS_ADMIN)) 627 return -EPERM; 628 629 /* Copy the parameters into kernel space. */ 630 param = copy_dev_ioctl(user); 631 if (IS_ERR(param)) 632 return PTR_ERR(param); 633 634 err = validate_dev_ioctl(command, param); 635 if (err) 636 goto out; 637 638 fn = lookup_dev_ioctl(cmd); 639 if (!fn) { 640 pr_warn("unknown command 0x%08x\n", command); 641 err = -ENOTTY; 642 goto out; 643 } 644 645 fp = NULL; 646 sbi = NULL; 647 648 /* 649 * For obvious reasons the openmount can't have a file 650 * descriptor yet. We don't take a reference to the 651 * file during close to allow for immediate release, 652 * and the same for retrieving ioctl version. 653 */ 654 if (cmd != AUTOFS_DEV_IOCTL_VERSION_CMD && 655 cmd != AUTOFS_DEV_IOCTL_OPENMOUNT_CMD && 656 cmd != AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD) { 657 fp = fget(param->ioctlfd); 658 if (!fp) { 659 if (cmd == AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD) 660 goto cont; 661 err = -EBADF; 662 goto out; 663 } 664 665 sbi = autofs_dev_ioctl_sbi(fp); 666 if (!sbi || sbi->magic != AUTOFS_SBI_MAGIC) { 667 err = -EINVAL; 668 fput(fp); 669 goto out; 670 } 671 672 /* 673 * Admin needs to be able to set the mount catatonic in 674 * order to be able to perform the re-open. 675 */ 676 if (!autofs_oz_mode(sbi) && 677 cmd != AUTOFS_DEV_IOCTL_CATATONIC_CMD) { 678 err = -EACCES; 679 fput(fp); 680 goto out; 681 } 682 } 683 cont: 684 err = fn(fp, sbi, param); 685 686 if (fp) 687 fput(fp); 688 if (err >= 0 && copy_to_user(user, param, AUTOFS_DEV_IOCTL_SIZE)) 689 err = -EFAULT; 690 out: 691 free_dev_ioctl(param); 692 return err; 693 } 694 695 static long autofs_dev_ioctl(struct file *file, unsigned int command, 696 unsigned long u) 697 { 698 int err; 699 700 err = _autofs_dev_ioctl(command, (struct autofs_dev_ioctl __user *) u); 701 return (long) err; 702 } 703 704 #ifdef CONFIG_COMPAT 705 static long autofs_dev_ioctl_compat(struct file *file, unsigned int command, 706 unsigned long u) 707 { 708 return autofs_dev_ioctl(file, command, (unsigned long) compat_ptr(u)); 709 } 710 #else 711 #define autofs_dev_ioctl_compat NULL 712 #endif 713 714 static const struct file_operations _dev_ioctl_fops = { 715 .unlocked_ioctl = autofs_dev_ioctl, 716 .compat_ioctl = autofs_dev_ioctl_compat, 717 .owner = THIS_MODULE, 718 .llseek = noop_llseek, 719 }; 720 721 static struct miscdevice _autofs_dev_ioctl_misc = { 722 .minor = AUTOFS_MINOR, 723 .name = AUTOFS_DEVICE_NAME, 724 .fops = &_dev_ioctl_fops, 725 .mode = 0644, 726 }; 727 728 MODULE_ALIAS_MISCDEV(AUTOFS_MINOR); 729 MODULE_ALIAS("devname:autofs"); 730 731 /* Register/deregister misc character device */ 732 int __init autofs_dev_ioctl_init(void) 733 { 734 int r; 735 736 r = misc_register(&_autofs_dev_ioctl_misc); 737 if (r) { 738 pr_err("misc_register failed for control device\n"); 739 return r; 740 } 741 742 return 0; 743 } 744 745 void autofs_dev_ioctl_exit(void) 746 { 747 misc_deregister(&_autofs_dev_ioctl_misc); 748 } 749