1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2008 Red Hat, Inc. All rights reserved.
4 * Copyright 2008 Ian Kent <raven@themaw.net>
7 #include <linux/miscdevice.h>
8 #include <linux/compat.h>
9 #include <linux/syscalls.h>
10 #include <linux/magic.h>
11 #include <linux/nospec.h>
16 * This module implements an interface for routing autofs ioctl control
17 * commands via a miscellaneous device file.
19 * The alternate interface is needed because we need to be able open
20 * an ioctl file descriptor on an autofs mount that may be covered by
21 * another mount. This situation arises when starting automount(8)
22 * or other user space daemon which uses direct mounts or offset
23 * mounts (used for autofs lazy mount/umount of nested mount trees),
24 * which have been left busy at service shutdown.
27 typedef int (*ioctl_fn
)(struct file
*, struct autofs_sb_info
*,
28 struct autofs_dev_ioctl
*);
30 static int check_name(const char *name
)
32 if (!strchr(name
, '/'))
38 * Check a string doesn't overrun the chunk of
39 * memory we copied from user land.
41 static int invalid_str(char *str
, size_t size
)
43 if (memchr(str
, 0, size
))
49 * Check that the user compiled against correct version of autofs
52 * As well as checking the version compatibility this always copies
53 * the kernel interface version out.
55 static int check_dev_ioctl_version(int cmd
, struct autofs_dev_ioctl
*param
)
59 if ((param
->ver_major
!= AUTOFS_DEV_IOCTL_VERSION_MAJOR
) ||
60 (param
->ver_minor
> AUTOFS_DEV_IOCTL_VERSION_MINOR
)) {
61 pr_warn("ioctl control interface version mismatch: "
62 "kernel(%u.%u), user(%u.%u), cmd(0x%08x)\n",
63 AUTOFS_DEV_IOCTL_VERSION_MAJOR
,
64 AUTOFS_DEV_IOCTL_VERSION_MINOR
,
65 param
->ver_major
, param
->ver_minor
, cmd
);
69 /* Fill in the kernel version. */
70 param
->ver_major
= AUTOFS_DEV_IOCTL_VERSION_MAJOR
;
71 param
->ver_minor
= AUTOFS_DEV_IOCTL_VERSION_MINOR
;
77 * Copy parameter control struct, including a possible path allocated
78 * at the end of the struct.
80 static struct autofs_dev_ioctl
*
81 copy_dev_ioctl(struct autofs_dev_ioctl __user
*in
)
83 struct autofs_dev_ioctl tmp
, *res
;
85 if (copy_from_user(&tmp
, in
, AUTOFS_DEV_IOCTL_SIZE
))
86 return ERR_PTR(-EFAULT
);
88 if (tmp
.size
< AUTOFS_DEV_IOCTL_SIZE
)
89 return ERR_PTR(-EINVAL
);
91 if (tmp
.size
> AUTOFS_DEV_IOCTL_SIZE
+ PATH_MAX
)
92 return ERR_PTR(-ENAMETOOLONG
);
94 res
= memdup_user(in
, tmp
.size
);
101 static inline void free_dev_ioctl(struct autofs_dev_ioctl
*param
)
107 * Check sanity of parameter control fields and if a path is present
108 * check that it is terminated and contains at least one "/".
110 static int validate_dev_ioctl(int cmd
, struct autofs_dev_ioctl
*param
)
114 err
= check_dev_ioctl_version(cmd
, param
);
116 pr_warn("invalid device control module version "
117 "supplied for cmd(0x%08x)\n", cmd
);
121 if (param
->size
> AUTOFS_DEV_IOCTL_SIZE
) {
122 err
= invalid_str(param
->path
, param
->size
- AUTOFS_DEV_IOCTL_SIZE
);
125 "path string terminator missing for cmd(0x%08x)\n",
130 err
= check_name(param
->path
);
132 pr_warn("invalid path supplied for cmd(0x%08x)\n",
137 unsigned int inr
= _IOC_NR(cmd
);
139 if (inr
== AUTOFS_DEV_IOCTL_OPENMOUNT_CMD
||
140 inr
== AUTOFS_DEV_IOCTL_REQUESTER_CMD
||
141 inr
== AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD
) {
152 /* Return autofs dev ioctl version */
153 static int autofs_dev_ioctl_version(struct file
*fp
,
154 struct autofs_sb_info
*sbi
,
155 struct autofs_dev_ioctl
*param
)
157 /* This should have already been set. */
158 param
->ver_major
= AUTOFS_DEV_IOCTL_VERSION_MAJOR
;
159 param
->ver_minor
= AUTOFS_DEV_IOCTL_VERSION_MINOR
;
163 /* Return autofs module protocol version */
164 static int autofs_dev_ioctl_protover(struct file
*fp
,
165 struct autofs_sb_info
*sbi
,
166 struct autofs_dev_ioctl
*param
)
168 param
->protover
.version
= sbi
->version
;
172 /* Return autofs module protocol sub version */
173 static int autofs_dev_ioctl_protosubver(struct file
*fp
,
174 struct autofs_sb_info
*sbi
,
175 struct autofs_dev_ioctl
*param
)
177 param
->protosubver
.sub_version
= sbi
->sub_version
;
181 /* Find the topmost mount satisfying test() */
182 static int find_autofs_mount(const char *pathname
,
184 int test(const struct path
*path
, void *data
),
190 err
= kern_path(pathname
, LOOKUP_MOUNTPOINT
, &path
);
194 while (path
.dentry
== path
.mnt
->mnt_root
) {
195 if (path
.dentry
->d_sb
->s_magic
== AUTOFS_SUPER_MAGIC
) {
196 if (test(&path
, data
)) {
203 if (!follow_up(&path
))
210 static int test_by_dev(const struct path
*path
, void *p
)
212 return path
->dentry
->d_sb
->s_dev
== *(dev_t
*)p
;
215 static int test_by_type(const struct path
*path
, void *p
)
217 struct autofs_info
*ino
= autofs_dentry_ino(path
->dentry
);
219 return ino
&& ino
->sbi
->type
& *(unsigned *)p
;
223 * Open a file descriptor on the autofs mount point corresponding
224 * to the given path and device number (aka. new_encode_dev(sb->s_dev)).
226 static int autofs_dev_ioctl_open_mountpoint(const char *name
, dev_t devid
)
230 fd
= get_unused_fd_flags(O_CLOEXEC
);
231 if (likely(fd
>= 0)) {
235 err
= find_autofs_mount(name
, &path
, test_by_dev
, &devid
);
239 filp
= dentry_open(&path
, O_RDONLY
, current_cred());
246 fd_install(fd
, filp
);
256 /* Open a file descriptor on an autofs mount point */
257 static int autofs_dev_ioctl_openmount(struct file
*fp
,
258 struct autofs_sb_info
*sbi
,
259 struct autofs_dev_ioctl
*param
)
265 /* param->path has been checked in validate_dev_ioctl() */
267 if (!param
->openmount
.devid
)
273 devid
= new_decode_dev(param
->openmount
.devid
);
276 fd
= autofs_dev_ioctl_open_mountpoint(path
, devid
);
277 if (unlikely(fd
< 0)) {
287 /* Close file descriptor allocated above (user can also use close(2)). */
288 static int autofs_dev_ioctl_closemount(struct file
*fp
,
289 struct autofs_sb_info
*sbi
,
290 struct autofs_dev_ioctl
*param
)
292 return ksys_close(param
->ioctlfd
);
296 * Send "ready" status for an existing wait (either a mount or an expire
299 static int autofs_dev_ioctl_ready(struct file
*fp
,
300 struct autofs_sb_info
*sbi
,
301 struct autofs_dev_ioctl
*param
)
305 token
= (autofs_wqt_t
) param
->ready
.token
;
306 return autofs_wait_release(sbi
, token
, 0);
310 * Send "fail" status for an existing wait (either a mount or an expire
313 static int autofs_dev_ioctl_fail(struct file
*fp
,
314 struct autofs_sb_info
*sbi
,
315 struct autofs_dev_ioctl
*param
)
320 token
= (autofs_wqt_t
) param
->fail
.token
;
321 status
= param
->fail
.status
< 0 ? param
->fail
.status
: -ENOENT
;
322 return autofs_wait_release(sbi
, token
, status
);
326 * Set the pipe fd for kernel communication to the daemon.
328 * Normally this is set at mount using an option but if we
329 * are reconnecting to a busy mount then we need to use this
330 * to tell the autofs mount about the new kernel pipe fd. In
331 * order to protect mounts against incorrectly setting the
332 * pipefd we also require that the autofs mount be catatonic.
334 * This also sets the process group id used to identify the
335 * controlling process (eg. the owning automount(8) daemon).
337 static int autofs_dev_ioctl_setpipefd(struct file
*fp
,
338 struct autofs_sb_info
*sbi
,
339 struct autofs_dev_ioctl
*param
)
343 struct pid
*new_pid
= NULL
;
345 if (param
->setpipefd
.pipefd
== -1)
348 pipefd
= param
->setpipefd
.pipefd
;
350 mutex_lock(&sbi
->wq_mutex
);
351 if (!(sbi
->flags
& AUTOFS_SBI_CATATONIC
)) {
352 mutex_unlock(&sbi
->wq_mutex
);
357 new_pid
= get_task_pid(current
, PIDTYPE_PGID
);
359 if (ns_of_pid(new_pid
) != ns_of_pid(sbi
->oz_pgrp
)) {
360 pr_warn("not allowed to change PID namespace\n");
370 if (autofs_prepare_pipe(pipe
) < 0) {
375 swap(sbi
->oz_pgrp
, new_pid
);
376 sbi
->pipefd
= pipefd
;
378 sbi
->flags
&= ~AUTOFS_SBI_CATATONIC
;
382 mutex_unlock(&sbi
->wq_mutex
);
387 * Make the autofs mount point catatonic, no longer responsive to
388 * mount requests. Also closes the kernel pipe file descriptor.
390 static int autofs_dev_ioctl_catatonic(struct file
*fp
,
391 struct autofs_sb_info
*sbi
,
392 struct autofs_dev_ioctl
*param
)
394 autofs_catatonic_mode(sbi
);
398 /* Set the autofs mount timeout */
399 static int autofs_dev_ioctl_timeout(struct file
*fp
,
400 struct autofs_sb_info
*sbi
,
401 struct autofs_dev_ioctl
*param
)
403 unsigned long timeout
;
405 timeout
= param
->timeout
.timeout
;
406 param
->timeout
.timeout
= sbi
->exp_timeout
/ HZ
;
407 sbi
->exp_timeout
= timeout
* HZ
;
412 * Return the uid and gid of the last request for the mount
414 * When reconstructing an autofs mount tree with active mounts
415 * we need to re-connect to mounts that may have used the original
416 * process uid and gid (or string variations of them) for mount
417 * lookups within the map entry.
419 static int autofs_dev_ioctl_requester(struct file
*fp
,
420 struct autofs_sb_info
*sbi
,
421 struct autofs_dev_ioctl
*param
)
423 struct autofs_info
*ino
;
428 /* param->path has been checked in validate_dev_ioctl() */
430 devid
= sbi
->sb
->s_dev
;
432 param
->requester
.uid
= param
->requester
.gid
= -1;
434 err
= find_autofs_mount(param
->path
, &path
, test_by_dev
, &devid
);
438 ino
= autofs_dentry_ino(path
.dentry
);
441 autofs_expire_wait(&path
, 0);
442 spin_lock(&sbi
->fs_lock
);
443 param
->requester
.uid
=
444 from_kuid_munged(current_user_ns(), ino
->uid
);
445 param
->requester
.gid
=
446 from_kgid_munged(current_user_ns(), ino
->gid
);
447 spin_unlock(&sbi
->fs_lock
);
455 * Call repeatedly until it returns -EAGAIN, meaning there's nothing
456 * more that can be done.
458 static int autofs_dev_ioctl_expire(struct file
*fp
,
459 struct autofs_sb_info
*sbi
,
460 struct autofs_dev_ioctl
*param
)
462 struct vfsmount
*mnt
;
465 how
= param
->expire
.how
;
466 mnt
= fp
->f_path
.mnt
;
468 return autofs_do_expire_multi(sbi
->sb
, mnt
, sbi
, how
);
471 /* Check if autofs mount point is in use */
472 static int autofs_dev_ioctl_askumount(struct file
*fp
,
473 struct autofs_sb_info
*sbi
,
474 struct autofs_dev_ioctl
*param
)
476 param
->askumount
.may_umount
= 0;
477 if (may_umount(fp
->f_path
.mnt
))
478 param
->askumount
.may_umount
= 1;
483 * Check if the given path is a mountpoint.
485 * If we are supplied with the file descriptor of an autofs
486 * mount we're looking for a specific mount. In this case
487 * the path is considered a mountpoint if it is itself a
488 * mountpoint or contains a mount, such as a multi-mount
489 * without a root mount. In this case we return 1 if the
490 * path is a mount point and the super magic of the covering
491 * mount if there is one or 0 if it isn't a mountpoint.
493 * If we aren't supplied with a file descriptor then we
494 * lookup the path and check if it is the root of a mount.
495 * If a type is given we are looking for a particular autofs
496 * mount and if we don't find a match we return fail. If the
497 * located path is the root of a mount we return 1 along with
498 * the super magic of the mount or 0 otherwise.
500 * In both cases the device number (as returned by
501 * new_encode_dev()) is also returned.
503 static int autofs_dev_ioctl_ismountpoint(struct file
*fp
,
504 struct autofs_sb_info
*sbi
,
505 struct autofs_dev_ioctl
*param
)
510 unsigned int devid
, magic
;
513 /* param->path has been checked in validate_dev_ioctl() */
516 type
= param
->ismountpoint
.in
.type
;
518 param
->ismountpoint
.out
.devid
= devid
= 0;
519 param
->ismountpoint
.out
.magic
= magic
= 0;
521 if (!fp
|| param
->ioctlfd
== -1) {
522 if (autofs_type_any(type
))
523 err
= kern_path(name
, LOOKUP_FOLLOW
| LOOKUP_MOUNTPOINT
,
526 err
= find_autofs_mount(name
, &path
,
527 test_by_type
, &type
);
530 devid
= new_encode_dev(path
.dentry
->d_sb
->s_dev
);
532 if (path
.mnt
->mnt_root
== path
.dentry
) {
534 magic
= path
.dentry
->d_sb
->s_magic
;
537 dev_t dev
= sbi
->sb
->s_dev
;
539 err
= find_autofs_mount(name
, &path
, test_by_dev
, &dev
);
543 devid
= new_encode_dev(dev
);
545 err
= path_has_submounts(&path
);
547 if (follow_down_one(&path
))
548 magic
= path
.dentry
->d_sb
->s_magic
;
551 param
->ismountpoint
.out
.devid
= devid
;
552 param
->ismountpoint
.out
.magic
= magic
;
559 * Our range of ioctl numbers isn't 0 based so we need to shift
560 * the array index by _IOC_NR(AUTOFS_CTL_IOC_FIRST) for the table
563 #define cmd_idx(cmd) (cmd - _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST))
565 static ioctl_fn
lookup_dev_ioctl(unsigned int cmd
)
567 static const ioctl_fn _ioctls
[] = {
568 autofs_dev_ioctl_version
,
569 autofs_dev_ioctl_protover
,
570 autofs_dev_ioctl_protosubver
,
571 autofs_dev_ioctl_openmount
,
572 autofs_dev_ioctl_closemount
,
573 autofs_dev_ioctl_ready
,
574 autofs_dev_ioctl_fail
,
575 autofs_dev_ioctl_setpipefd
,
576 autofs_dev_ioctl_catatonic
,
577 autofs_dev_ioctl_timeout
,
578 autofs_dev_ioctl_requester
,
579 autofs_dev_ioctl_expire
,
580 autofs_dev_ioctl_askumount
,
581 autofs_dev_ioctl_ismountpoint
,
583 unsigned int idx
= cmd_idx(cmd
);
585 if (idx
>= ARRAY_SIZE(_ioctls
))
587 idx
= array_index_nospec(idx
, ARRAY_SIZE(_ioctls
));
591 /* ioctl dispatcher */
592 static int _autofs_dev_ioctl(unsigned int command
,
593 struct autofs_dev_ioctl __user
*user
)
595 struct autofs_dev_ioctl
*param
;
597 struct autofs_sb_info
*sbi
;
598 unsigned int cmd_first
, cmd
;
602 cmd_first
= _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST
);
603 cmd
= _IOC_NR(command
);
605 if (_IOC_TYPE(command
) != _IOC_TYPE(AUTOFS_DEV_IOCTL_IOC_FIRST
) ||
606 cmd
- cmd_first
> AUTOFS_DEV_IOCTL_IOC_COUNT
) {
610 /* Only root can use ioctls other than AUTOFS_DEV_IOCTL_VERSION_CMD
611 * and AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD
613 if (cmd
!= AUTOFS_DEV_IOCTL_VERSION_CMD
&&
614 cmd
!= AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD
&&
615 !capable(CAP_SYS_ADMIN
))
618 /* Copy the parameters into kernel space. */
619 param
= copy_dev_ioctl(user
);
621 return PTR_ERR(param
);
623 err
= validate_dev_ioctl(command
, param
);
627 fn
= lookup_dev_ioctl(cmd
);
629 pr_warn("unknown command 0x%08x\n", command
);
638 * For obvious reasons the openmount can't have a file
639 * descriptor yet. We don't take a reference to the
640 * file during close to allow for immediate release,
641 * and the same for retrieving ioctl version.
643 if (cmd
!= AUTOFS_DEV_IOCTL_VERSION_CMD
&&
644 cmd
!= AUTOFS_DEV_IOCTL_OPENMOUNT_CMD
&&
645 cmd
!= AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD
) {
646 struct super_block
*sb
;
648 fp
= fget(param
->ioctlfd
);
650 if (cmd
== AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD
)
656 sb
= file_inode(fp
)->i_sb
;
657 if (sb
->s_type
!= &autofs_fs_type
) {
662 sbi
= autofs_sbi(sb
);
665 * Admin needs to be able to set the mount catatonic in
666 * order to be able to perform the re-open.
668 if (!autofs_oz_mode(sbi
) &&
669 cmd
!= AUTOFS_DEV_IOCTL_CATATONIC_CMD
) {
676 err
= fn(fp
, sbi
, param
);
680 if (err
>= 0 && copy_to_user(user
, param
, AUTOFS_DEV_IOCTL_SIZE
))
683 free_dev_ioctl(param
);
687 static long autofs_dev_ioctl(struct file
*file
, unsigned int command
,
692 err
= _autofs_dev_ioctl(command
, (struct autofs_dev_ioctl __user
*) u
);
697 static long autofs_dev_ioctl_compat(struct file
*file
, unsigned int command
,
700 return autofs_dev_ioctl(file
, command
, (unsigned long) compat_ptr(u
));
703 #define autofs_dev_ioctl_compat NULL
706 static const struct file_operations _dev_ioctl_fops
= {
707 .unlocked_ioctl
= autofs_dev_ioctl
,
708 .compat_ioctl
= autofs_dev_ioctl_compat
,
709 .owner
= THIS_MODULE
,
710 .llseek
= noop_llseek
,
713 static struct miscdevice _autofs_dev_ioctl_misc
= {
714 .minor
= AUTOFS_MINOR
,
715 .name
= AUTOFS_DEVICE_NAME
,
716 .fops
= &_dev_ioctl_fops
,
720 MODULE_ALIAS_MISCDEV(AUTOFS_MINOR
);
721 MODULE_ALIAS("devname:autofs");
723 /* Register/deregister misc character device */
724 int __init
autofs_dev_ioctl_init(void)
728 r
= misc_register(&_autofs_dev_ioctl_misc
);
730 pr_err("misc_register failed for control device\n");
737 void autofs_dev_ioctl_exit(void)
739 misc_deregister(&_autofs_dev_ioctl_misc
);