2 * Copyright 2008 Red Hat, Inc. All rights reserved.
3 * Copyright 2008 Ian Kent <raven@themaw.net>
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/module.h>
11 #include <linux/vmalloc.h>
12 #include <linux/miscdevice.h>
13 #include <linux/init.h>
14 #include <linux/wait.h>
15 #include <linux/namei.h>
16 #include <linux/fcntl.h>
17 #include <linux/file.h>
18 #include <linux/fdtable.h>
19 #include <linux/sched.h>
20 #include <linux/compat.h>
21 #include <linux/syscalls.h>
22 #include <linux/magic.h>
23 #include <linux/dcache.h>
24 #include <linux/uaccess.h>
25 #include <linux/slab.h>
30 * This module implements an interface for routing autofs ioctl control
31 * commands via a miscellaneous device file.
33 * The alternate interface is needed because we need to be able open
34 * an ioctl file descriptor on an autofs mount that may be covered by
35 * another mount. This situation arises when starting automount(8)
36 * or other user space daemon which uses direct mounts or offset
37 * mounts (used for autofs lazy mount/umount of nested mount trees),
38 * which have been left busy at at service shutdown.
41 typedef int (*ioctl_fn)(struct file *, struct autofs_sb_info *,
42 struct autofs_dev_ioctl *);
44 static int check_name(const char *name)
46 if (!strchr(name, '/'))
52 * Check a string doesn't overrun the chunk of
53 * memory we copied from user land.
55 static int invalid_str(char *str, size_t size)
57 if (memchr(str, 0, size))
63 * Check that the user compiled against correct version of autofs
66 * As well as checking the version compatibility this always copies
67 * the kernel interface version out.
69 static int check_dev_ioctl_version(int cmd, struct autofs_dev_ioctl *param)
73 if ((param->ver_major != AUTOFS_DEV_IOCTL_VERSION_MAJOR) ||
74 (param->ver_minor > AUTOFS_DEV_IOCTL_VERSION_MINOR)) {
75 pr_warn("ioctl control interface version mismatch: "
76 "kernel(%u.%u), user(%u.%u), cmd(0x%08x)\n",
77 AUTOFS_DEV_IOCTL_VERSION_MAJOR,
78 AUTOFS_DEV_IOCTL_VERSION_MINOR,
79 param->ver_major, param->ver_minor, cmd);
83 /* Fill in the kernel version. */
84 param->ver_major = AUTOFS_DEV_IOCTL_VERSION_MAJOR;
85 param->ver_minor = AUTOFS_DEV_IOCTL_VERSION_MINOR;
91 * Copy parameter control struct, including a possible path allocated
92 * at the end of the struct.
94 static struct autofs_dev_ioctl *
95 copy_dev_ioctl(struct autofs_dev_ioctl __user *in)
97 struct autofs_dev_ioctl tmp, *res;
99 if (copy_from_user(&tmp, in, sizeof(tmp)))
100 return ERR_PTR(-EFAULT);
102 if (tmp.size < sizeof(tmp))
103 return ERR_PTR(-EINVAL);
105 if (tmp.size > (PATH_MAX + sizeof(tmp)))
106 return ERR_PTR(-ENAMETOOLONG);
108 res = memdup_user(in, tmp.size);
110 res->size = tmp.size;
115 static inline void free_dev_ioctl(struct autofs_dev_ioctl *param)
121 * Check sanity of parameter control fields and if a path is present
122 * check that it is terminated and contains at least one "/".
124 static int validate_dev_ioctl(int cmd, struct autofs_dev_ioctl *param)
128 err = check_dev_ioctl_version(cmd, param);
130 pr_warn("invalid device control module version "
131 "supplied for cmd(0x%08x)\n", cmd);
135 if (param->size > sizeof(*param)) {
136 err = invalid_str(param->path, param->size - sizeof(*param));
139 "path string terminator missing for cmd(0x%08x)\n",
144 err = check_name(param->path);
146 pr_warn("invalid path supplied for cmd(0x%08x)\n",
158 * Get the autofs super block info struct from the file opened on
159 * the autofs mount point.
161 static struct autofs_sb_info *autofs_dev_ioctl_sbi(struct file *f)
163 struct autofs_sb_info *sbi = NULL;
167 inode = file_inode(f);
168 sbi = autofs4_sbi(inode->i_sb);
173 /* Return autofs dev ioctl version */
174 static int autofs_dev_ioctl_version(struct file *fp,
175 struct autofs_sb_info *sbi,
176 struct autofs_dev_ioctl *param)
178 /* This should have already been set. */
179 param->ver_major = AUTOFS_DEV_IOCTL_VERSION_MAJOR;
180 param->ver_minor = AUTOFS_DEV_IOCTL_VERSION_MINOR;
184 /* Return autofs module protocol version */
185 static int autofs_dev_ioctl_protover(struct file *fp,
186 struct autofs_sb_info *sbi,
187 struct autofs_dev_ioctl *param)
189 param->protover.version = sbi->version;
193 /* Return autofs module protocol sub version */
194 static int autofs_dev_ioctl_protosubver(struct file *fp,
195 struct autofs_sb_info *sbi,
196 struct autofs_dev_ioctl *param)
198 param->protosubver.sub_version = sbi->sub_version;
202 /* Find the topmost mount satisfying test() */
203 static int find_autofs_mount(const char *pathname,
205 int test(const struct path *path, void *data),
211 err = kern_path_mountpoint(AT_FDCWD, pathname, &path, 0);
215 while (path.dentry == path.mnt->mnt_root) {
216 if (path.dentry->d_sb->s_magic == AUTOFS_SUPER_MAGIC) {
217 if (test(&path, data)) {
224 if (!follow_up(&path))
231 static int test_by_dev(const struct path *path, void *p)
233 return path->dentry->d_sb->s_dev == *(dev_t *)p;
236 static int test_by_type(const struct path *path, void *p)
238 struct autofs_info *ino = autofs4_dentry_ino(path->dentry);
240 return ino && ino->sbi->type & *(unsigned *)p;
244 * Open a file descriptor on the autofs mount point corresponding
245 * to the given path and device number (aka. new_encode_dev(sb->s_dev)).
247 static int autofs_dev_ioctl_open_mountpoint(const char *name, dev_t devid)
251 fd = get_unused_fd_flags(O_CLOEXEC);
252 if (likely(fd >= 0)) {
256 err = find_autofs_mount(name, &path, test_by_dev, &devid);
261 * Find autofs super block that has the device number
262 * corresponding to the autofs fs we want to open.
265 filp = dentry_open(&path, O_RDONLY, current_cred());
272 fd_install(fd, filp);
282 /* Open a file descriptor on an autofs mount point */
283 static int autofs_dev_ioctl_openmount(struct file *fp,
284 struct autofs_sb_info *sbi,
285 struct autofs_dev_ioctl *param)
291 /* param->path has already been checked */
292 if (!param->openmount.devid)
298 devid = new_decode_dev(param->openmount.devid);
301 fd = autofs_dev_ioctl_open_mountpoint(path, devid);
302 if (unlikely(fd < 0)) {
312 /* Close file descriptor allocated above (user can also use close(2)). */
313 static int autofs_dev_ioctl_closemount(struct file *fp,
314 struct autofs_sb_info *sbi,
315 struct autofs_dev_ioctl *param)
317 return sys_close(param->ioctlfd);
321 * Send "ready" status for an existing wait (either a mount or an expire
324 static int autofs_dev_ioctl_ready(struct file *fp,
325 struct autofs_sb_info *sbi,
326 struct autofs_dev_ioctl *param)
330 token = (autofs_wqt_t) param->ready.token;
331 return autofs4_wait_release(sbi, token, 0);
335 * Send "fail" status for an existing wait (either a mount or an expire
338 static int autofs_dev_ioctl_fail(struct file *fp,
339 struct autofs_sb_info *sbi,
340 struct autofs_dev_ioctl *param)
345 token = (autofs_wqt_t) param->fail.token;
346 status = param->fail.status ? param->fail.status : -ENOENT;
347 return autofs4_wait_release(sbi, token, status);
351 * Set the pipe fd for kernel communication to the daemon.
353 * Normally this is set at mount using an option but if we
354 * are reconnecting to a busy mount then we need to use this
355 * to tell the autofs mount about the new kernel pipe fd. In
356 * order to protect mounts against incorrectly setting the
357 * pipefd we also require that the autofs mount be catatonic.
359 * This also sets the process group id used to identify the
360 * controlling process (eg. the owning automount(8) daemon).
362 static int autofs_dev_ioctl_setpipefd(struct file *fp,
363 struct autofs_sb_info *sbi,
364 struct autofs_dev_ioctl *param)
368 struct pid *new_pid = NULL;
370 if (param->setpipefd.pipefd == -1)
373 pipefd = param->setpipefd.pipefd;
375 mutex_lock(&sbi->wq_mutex);
376 if (!sbi->catatonic) {
377 mutex_unlock(&sbi->wq_mutex);
382 new_pid = get_task_pid(current, PIDTYPE_PGID);
384 if (ns_of_pid(new_pid) != ns_of_pid(sbi->oz_pgrp)) {
385 pr_warn("not allowed to change PID namespace\n");
395 if (autofs_prepare_pipe(pipe) < 0) {
400 swap(sbi->oz_pgrp, new_pid);
401 sbi->pipefd = pipefd;
407 mutex_unlock(&sbi->wq_mutex);
412 * Make the autofs mount point catatonic, no longer responsive to
413 * mount requests. Also closes the kernel pipe file descriptor.
415 static int autofs_dev_ioctl_catatonic(struct file *fp,
416 struct autofs_sb_info *sbi,
417 struct autofs_dev_ioctl *param)
419 autofs4_catatonic_mode(sbi);
423 /* Set the autofs mount timeout */
424 static int autofs_dev_ioctl_timeout(struct file *fp,
425 struct autofs_sb_info *sbi,
426 struct autofs_dev_ioctl *param)
428 unsigned long timeout;
430 timeout = param->timeout.timeout;
431 param->timeout.timeout = sbi->exp_timeout / HZ;
432 sbi->exp_timeout = timeout * HZ;
437 * Return the uid and gid of the last request for the mount
439 * When reconstructing an autofs mount tree with active mounts
440 * we need to re-connect to mounts that may have used the original
441 * process uid and gid (or string variations of them) for mount
442 * lookups within the map entry.
444 static int autofs_dev_ioctl_requester(struct file *fp,
445 struct autofs_sb_info *sbi,
446 struct autofs_dev_ioctl *param)
448 struct autofs_info *ino;
453 if (param->size <= sizeof(*param)) {
458 devid = sbi->sb->s_dev;
460 param->requester.uid = param->requester.gid = -1;
462 err = find_autofs_mount(param->path, &path, test_by_dev, &devid);
466 ino = autofs4_dentry_ino(path.dentry);
469 autofs4_expire_wait(&path, 0);
470 spin_lock(&sbi->fs_lock);
471 param->requester.uid =
472 from_kuid_munged(current_user_ns(), ino->uid);
473 param->requester.gid =
474 from_kgid_munged(current_user_ns(), ino->gid);
475 spin_unlock(&sbi->fs_lock);
483 * Call repeatedly until it returns -EAGAIN, meaning there's nothing
484 * more that can be done.
486 static int autofs_dev_ioctl_expire(struct file *fp,
487 struct autofs_sb_info *sbi,
488 struct autofs_dev_ioctl *param)
490 struct vfsmount *mnt;
493 how = param->expire.how;
494 mnt = fp->f_path.mnt;
496 return autofs4_do_expire_multi(sbi->sb, mnt, sbi, how);
499 /* Check if autofs mount point is in use */
500 static int autofs_dev_ioctl_askumount(struct file *fp,
501 struct autofs_sb_info *sbi,
502 struct autofs_dev_ioctl *param)
504 param->askumount.may_umount = 0;
505 if (may_umount(fp->f_path.mnt))
506 param->askumount.may_umount = 1;
511 * Check if the given path is a mountpoint.
513 * If we are supplied with the file descriptor of an autofs
514 * mount we're looking for a specific mount. In this case
515 * the path is considered a mountpoint if it is itself a
516 * mountpoint or contains a mount, such as a multi-mount
517 * without a root mount. In this case we return 1 if the
518 * path is a mount point and the super magic of the covering
519 * mount if there is one or 0 if it isn't a mountpoint.
521 * If we aren't supplied with a file descriptor then we
522 * lookup the path and check if it is the root of a mount.
523 * If a type is given we are looking for a particular autofs
524 * mount and if we don't find a match we return fail. If the
525 * located path is the root of a mount we return 1 along with
526 * the super magic of the mount or 0 otherwise.
528 * In both cases the the device number (as returned by
529 * new_encode_dev()) is also returned.
531 static int autofs_dev_ioctl_ismountpoint(struct file *fp,
532 struct autofs_sb_info *sbi,
533 struct autofs_dev_ioctl *param)
538 unsigned int devid, magic;
541 if (param->size <= sizeof(*param)) {
547 type = param->ismountpoint.in.type;
549 param->ismountpoint.out.devid = devid = 0;
550 param->ismountpoint.out.magic = magic = 0;
552 if (!fp || param->ioctlfd == -1) {
553 if (autofs_type_any(type))
554 err = kern_path_mountpoint(AT_FDCWD,
555 name, &path, LOOKUP_FOLLOW);
557 err = find_autofs_mount(name, &path,
558 test_by_type, &type);
561 devid = new_encode_dev(path.dentry->d_sb->s_dev);
563 if (path.mnt->mnt_root == path.dentry) {
565 magic = path.dentry->d_sb->s_magic;
568 dev_t dev = sbi->sb->s_dev;
570 err = find_autofs_mount(name, &path, test_by_dev, &dev);
574 devid = new_encode_dev(dev);
576 err = path_has_submounts(&path);
578 if (follow_down_one(&path))
579 magic = path.dentry->d_sb->s_magic;
582 param->ismountpoint.out.devid = devid;
583 param->ismountpoint.out.magic = magic;
590 * Our range of ioctl numbers isn't 0 based so we need to shift
591 * the array index by _IOC_NR(AUTOFS_CTL_IOC_FIRST) for the table
594 #define cmd_idx(cmd) (cmd - _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST))
596 static ioctl_fn lookup_dev_ioctl(unsigned int cmd)
598 static ioctl_fn _ioctls[] = {
599 autofs_dev_ioctl_version,
600 autofs_dev_ioctl_protover,
601 autofs_dev_ioctl_protosubver,
602 autofs_dev_ioctl_openmount,
603 autofs_dev_ioctl_closemount,
604 autofs_dev_ioctl_ready,
605 autofs_dev_ioctl_fail,
606 autofs_dev_ioctl_setpipefd,
607 autofs_dev_ioctl_catatonic,
608 autofs_dev_ioctl_timeout,
609 autofs_dev_ioctl_requester,
610 autofs_dev_ioctl_expire,
611 autofs_dev_ioctl_askumount,
612 autofs_dev_ioctl_ismountpoint,
614 unsigned int idx = cmd_idx(cmd);
616 return (idx >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[idx];
619 /* ioctl dispatcher */
620 static int _autofs_dev_ioctl(unsigned int command,
621 struct autofs_dev_ioctl __user *user)
623 struct autofs_dev_ioctl *param;
625 struct autofs_sb_info *sbi;
626 unsigned int cmd_first, cmd;
630 /* only root can play with this */
631 if (!capable(CAP_SYS_ADMIN))
634 cmd_first = _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST);
635 cmd = _IOC_NR(command);
637 if (_IOC_TYPE(command) != _IOC_TYPE(AUTOFS_DEV_IOCTL_IOC_FIRST) ||
638 cmd - cmd_first > AUTOFS_DEV_IOCTL_IOC_COUNT) {
642 /* Copy the parameters into kernel space. */
643 param = copy_dev_ioctl(user);
645 return PTR_ERR(param);
647 err = validate_dev_ioctl(command, param);
651 fn = lookup_dev_ioctl(cmd);
653 pr_warn("unknown command 0x%08x\n", command);
662 * For obvious reasons the openmount can't have a file
663 * descriptor yet. We don't take a reference to the
664 * file during close to allow for immediate release,
665 * and the same for retrieving ioctl version.
667 if (cmd != AUTOFS_DEV_IOCTL_VERSION_CMD &&
668 cmd != AUTOFS_DEV_IOCTL_OPENMOUNT_CMD &&
669 cmd != AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD) {
670 fp = fget(param->ioctlfd);
672 if (cmd == AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD)
678 sbi = autofs_dev_ioctl_sbi(fp);
679 if (!sbi || sbi->magic != AUTOFS_SBI_MAGIC) {
686 * Admin needs to be able to set the mount catatonic in
687 * order to be able to perform the re-open.
689 if (!autofs4_oz_mode(sbi) &&
690 cmd != AUTOFS_DEV_IOCTL_CATATONIC_CMD) {
697 err = fn(fp, sbi, param);
701 if (err >= 0 && copy_to_user(user, param, AUTOFS_DEV_IOCTL_SIZE))
704 free_dev_ioctl(param);
708 static long autofs_dev_ioctl(struct file *file, uint command, ulong u)
712 err = _autofs_dev_ioctl(command, (struct autofs_dev_ioctl __user *) u);
717 static long autofs_dev_ioctl_compat(struct file *file, uint command, ulong u)
719 return (long) autofs_dev_ioctl(file, command, (ulong) compat_ptr(u));
722 #define autofs_dev_ioctl_compat NULL
725 static const struct file_operations _dev_ioctl_fops = {
726 .unlocked_ioctl = autofs_dev_ioctl,
727 .compat_ioctl = autofs_dev_ioctl_compat,
728 .owner = THIS_MODULE,
729 .llseek = noop_llseek,
732 static struct miscdevice _autofs_dev_ioctl_misc = {
733 .minor = AUTOFS_MINOR,
734 .name = AUTOFS_DEVICE_NAME,
735 .fops = &_dev_ioctl_fops
738 MODULE_ALIAS_MISCDEV(AUTOFS_MINOR);
739 MODULE_ALIAS("devname:autofs");
741 /* Register/deregister misc character device */
742 int __init autofs_dev_ioctl_init(void)
746 r = misc_register(&_autofs_dev_ioctl_misc);
748 pr_err("misc_register failed for control device\n");
755 void autofs_dev_ioctl_exit(void)
757 misc_deregister(&_autofs_dev_ioctl_misc);