2 * Copyright 2008 Red Hat, Inc. All rights reserved.
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.
10 #include <linux/miscdevice.h>
11 #include <linux/compat.h>
12 #include <linux/syscalls.h>
13 #include <linux/magic.h>
18 * This module implements an interface for routing autofs ioctl control
19 * commands via a miscellaneous device file.
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.
29 typedef int (*ioctl_fn)(struct file *, struct autofs_sb_info *,
30 struct autofs_dev_ioctl *);
32 static int check_name(const char *name)
34 if (!strchr(name, '/'))
40 * Check a string doesn't overrun the chunk of
41 * memory we copied from user land.
43 static int invalid_str(char *str, size_t size)
45 if (memchr(str, 0, size))
51 * Check that the user compiled against correct version of autofs
54 * As well as checking the version compatibility this always copies
55 * the kernel interface version out.
57 static int check_dev_ioctl_version(int cmd, struct autofs_dev_ioctl *param)
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);
71 /* Fill in the kernel version. */
72 param->ver_major = AUTOFS_DEV_IOCTL_VERSION_MAJOR;
73 param->ver_minor = AUTOFS_DEV_IOCTL_VERSION_MINOR;
79 * Copy parameter control struct, including a possible path allocated
80 * at the end of the struct.
82 static struct autofs_dev_ioctl *
83 copy_dev_ioctl(struct autofs_dev_ioctl __user *in)
85 struct autofs_dev_ioctl tmp, *res;
87 if (copy_from_user(&tmp, in, AUTOFS_DEV_IOCTL_SIZE))
88 return ERR_PTR(-EFAULT);
90 if (tmp.size < AUTOFS_DEV_IOCTL_SIZE)
91 return ERR_PTR(-EINVAL);
93 if (tmp.size > AUTOFS_DEV_IOCTL_SIZE + PATH_MAX)
94 return ERR_PTR(-ENAMETOOLONG);
96 res = memdup_user(in, tmp.size);
103 static inline void free_dev_ioctl(struct autofs_dev_ioctl *param)
109 * Check sanity of parameter control fields and if a path is present
110 * check that it is terminated and contains at least one "/".
112 static int validate_dev_ioctl(int cmd, struct autofs_dev_ioctl *param)
116 err = check_dev_ioctl_version(cmd, param);
118 pr_warn("invalid device control module version "
119 "supplied for cmd(0x%08x)\n", cmd);
123 if (param->size > AUTOFS_DEV_IOCTL_SIZE) {
124 err = invalid_str(param->path, param->size - AUTOFS_DEV_IOCTL_SIZE);
127 "path string terminator missing for cmd(0x%08x)\n",
132 err = check_name(param->path);
134 pr_warn("invalid path supplied for cmd(0x%08x)\n",
146 * Get the autofs super block info struct from the file opened on
147 * the autofs mount point.
149 static struct autofs_sb_info *autofs_dev_ioctl_sbi(struct file *f)
151 struct autofs_sb_info *sbi = NULL;
155 inode = file_inode(f);
156 sbi = autofs_sbi(inode->i_sb);
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)
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;
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)
177 param->protover.version = sbi->version;
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)
186 param->protosubver.sub_version = sbi->sub_version;
190 /* Find the topmost mount satisfying test() */
191 static int find_autofs_mount(const char *pathname,
193 int test(const struct path *path, void *data),
199 err = kern_path_mountpoint(AT_FDCWD, pathname, &path, 0);
203 while (path.dentry == path.mnt->mnt_root) {
204 if (path.dentry->d_sb->s_magic == AUTOFS_SUPER_MAGIC) {
205 if (test(&path, data)) {
212 if (!follow_up(&path))
219 static int test_by_dev(const struct path *path, void *p)
221 return path->dentry->d_sb->s_dev == *(dev_t *)p;
224 static int test_by_type(const struct path *path, void *p)
226 struct autofs_info *ino = autofs_dentry_ino(path->dentry);
228 return ino && ino->sbi->type & *(unsigned *)p;
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)).
235 static int autofs_dev_ioctl_open_mountpoint(const char *name, dev_t devid)
239 fd = get_unused_fd_flags(O_CLOEXEC);
240 if (likely(fd >= 0)) {
244 err = find_autofs_mount(name, &path, test_by_dev, &devid);
248 filp = dentry_open(&path, O_RDONLY, current_cred());
255 fd_install(fd, filp);
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)
274 /* param->path has already been checked */
275 if (!param->openmount.devid)
281 devid = new_decode_dev(param->openmount.devid);
284 fd = autofs_dev_ioctl_open_mountpoint(path, devid);
285 if (unlikely(fd < 0)) {
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)
300 return ksys_close(param->ioctlfd);
304 * Send "ready" status for an existing wait (either a mount or an expire
307 static int autofs_dev_ioctl_ready(struct file *fp,
308 struct autofs_sb_info *sbi,
309 struct autofs_dev_ioctl *param)
313 token = (autofs_wqt_t) param->ready.token;
314 return autofs_wait_release(sbi, token, 0);
318 * Send "fail" status for an existing wait (either a mount or an expire
321 static int autofs_dev_ioctl_fail(struct file *fp,
322 struct autofs_sb_info *sbi,
323 struct autofs_dev_ioctl *param)
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);
334 * Set the pipe fd for kernel communication to the daemon.
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.
342 * This also sets the process group id used to identify the
343 * controlling process (eg. the owning automount(8) daemon).
345 static int autofs_dev_ioctl_setpipefd(struct file *fp,
346 struct autofs_sb_info *sbi,
347 struct autofs_dev_ioctl *param)
351 struct pid *new_pid = NULL;
353 if (param->setpipefd.pipefd == -1)
356 pipefd = param->setpipefd.pipefd;
358 mutex_lock(&sbi->wq_mutex);
359 if (!sbi->catatonic) {
360 mutex_unlock(&sbi->wq_mutex);
365 new_pid = get_task_pid(current, PIDTYPE_PGID);
367 if (ns_of_pid(new_pid) != ns_of_pid(sbi->oz_pgrp)) {
368 pr_warn("not allowed to change PID namespace\n");
378 if (autofs_prepare_pipe(pipe) < 0) {
383 swap(sbi->oz_pgrp, new_pid);
384 sbi->pipefd = pipefd;
390 mutex_unlock(&sbi->wq_mutex);
395 * Make the autofs mount point catatonic, no longer responsive to
396 * mount requests. Also closes the kernel pipe file descriptor.
398 static int autofs_dev_ioctl_catatonic(struct file *fp,
399 struct autofs_sb_info *sbi,
400 struct autofs_dev_ioctl *param)
402 autofs_catatonic_mode(sbi);
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)
411 unsigned long timeout;
413 timeout = param->timeout.timeout;
414 param->timeout.timeout = sbi->exp_timeout / HZ;
415 sbi->exp_timeout = timeout * HZ;
420 * Return the uid and gid of the last request for the mount
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.
427 static int autofs_dev_ioctl_requester(struct file *fp,
428 struct autofs_sb_info *sbi,
429 struct autofs_dev_ioctl *param)
431 struct autofs_info *ino;
436 if (param->size <= AUTOFS_DEV_IOCTL_SIZE) {
441 devid = sbi->sb->s_dev;
443 param->requester.uid = param->requester.gid = -1;
445 err = find_autofs_mount(param->path, &path, test_by_dev, &devid);
449 ino = autofs_dentry_ino(path.dentry);
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);
466 * Call repeatedly until it returns -EAGAIN, meaning there's nothing
467 * more that can be done.
469 static int autofs_dev_ioctl_expire(struct file *fp,
470 struct autofs_sb_info *sbi,
471 struct autofs_dev_ioctl *param)
473 struct vfsmount *mnt;
476 how = param->expire.how;
477 mnt = fp->f_path.mnt;
479 return autofs_do_expire_multi(sbi->sb, mnt, sbi, how);
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)
487 param->askumount.may_umount = 0;
488 if (may_umount(fp->f_path.mnt))
489 param->askumount.may_umount = 1;
494 * Check if the given path is a mountpoint.
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.
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.
511 * In both cases the the device number (as returned by
512 * new_encode_dev()) is also returned.
514 static int autofs_dev_ioctl_ismountpoint(struct file *fp,
515 struct autofs_sb_info *sbi,
516 struct autofs_dev_ioctl *param)
521 unsigned int devid, magic;
524 if (param->size <= AUTOFS_DEV_IOCTL_SIZE) {
530 type = param->ismountpoint.in.type;
532 param->ismountpoint.out.devid = devid = 0;
533 param->ismountpoint.out.magic = magic = 0;
535 if (!fp || param->ioctlfd == -1) {
536 if (autofs_type_any(type))
537 err = kern_path_mountpoint(AT_FDCWD,
538 name, &path, LOOKUP_FOLLOW);
540 err = find_autofs_mount(name, &path,
541 test_by_type, &type);
544 devid = new_encode_dev(path.dentry->d_sb->s_dev);
546 if (path.mnt->mnt_root == path.dentry) {
548 magic = path.dentry->d_sb->s_magic;
551 dev_t dev = sbi->sb->s_dev;
553 err = find_autofs_mount(name, &path, test_by_dev, &dev);
557 devid = new_encode_dev(dev);
559 err = path_has_submounts(&path);
561 if (follow_down_one(&path))
562 magic = path.dentry->d_sb->s_magic;
565 param->ismountpoint.out.devid = devid;
566 param->ismountpoint.out.magic = magic;
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
577 #define cmd_idx(cmd) (cmd - _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST))
579 static ioctl_fn lookup_dev_ioctl(unsigned int cmd)
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,
597 unsigned int idx = cmd_idx(cmd);
599 return (idx >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[idx];
602 /* ioctl dispatcher */
603 static int _autofs_dev_ioctl(unsigned int command,
604 struct autofs_dev_ioctl __user *user)
606 struct autofs_dev_ioctl *param;
608 struct autofs_sb_info *sbi;
609 unsigned int cmd_first, cmd;
613 cmd_first = _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST);
614 cmd = _IOC_NR(command);
616 if (_IOC_TYPE(command) != _IOC_TYPE(AUTOFS_DEV_IOCTL_IOC_FIRST) ||
617 cmd - cmd_first > AUTOFS_DEV_IOCTL_IOC_COUNT) {
621 /* Only root can use ioctls other than AUTOFS_DEV_IOCTL_VERSION_CMD
622 * and AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD
624 if (cmd != AUTOFS_DEV_IOCTL_VERSION_CMD &&
625 cmd != AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD &&
626 !capable(CAP_SYS_ADMIN))
629 /* Copy the parameters into kernel space. */
630 param = copy_dev_ioctl(user);
632 return PTR_ERR(param);
634 err = validate_dev_ioctl(command, param);
638 fn = lookup_dev_ioctl(cmd);
640 pr_warn("unknown command 0x%08x\n", command);
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.
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);
659 if (cmd == AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD)
665 sbi = autofs_dev_ioctl_sbi(fp);
666 if (!sbi || sbi->magic != AUTOFS_SBI_MAGIC) {
673 * Admin needs to be able to set the mount catatonic in
674 * order to be able to perform the re-open.
676 if (!autofs_oz_mode(sbi) &&
677 cmd != AUTOFS_DEV_IOCTL_CATATONIC_CMD) {
684 err = fn(fp, sbi, param);
688 if (err >= 0 && copy_to_user(user, param, AUTOFS_DEV_IOCTL_SIZE))
691 free_dev_ioctl(param);
695 static long autofs_dev_ioctl(struct file *file, unsigned int command,
700 err = _autofs_dev_ioctl(command, (struct autofs_dev_ioctl __user *) u);
705 static long autofs_dev_ioctl_compat(struct file *file, unsigned int command,
708 return autofs_dev_ioctl(file, command, (unsigned long) compat_ptr(u));
711 #define autofs_dev_ioctl_compat NULL
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,
721 static struct miscdevice _autofs_dev_ioctl_misc = {
722 .minor = AUTOFS_MINOR,
723 .name = AUTOFS_DEVICE_NAME,
724 .fops = &_dev_ioctl_fops,
728 MODULE_ALIAS_MISCDEV(AUTOFS_MINOR);
729 MODULE_ALIAS("devname:autofs");
731 /* Register/deregister misc character device */
732 int __init autofs_dev_ioctl_init(void)
736 r = misc_register(&_autofs_dev_ioctl_misc);
738 pr_err("misc_register failed for control device\n");
745 void autofs_dev_ioctl_exit(void)
747 misc_deregister(&_autofs_dev_ioctl_misc);