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