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>
15 * This module implements an interface for routing autofs ioctl control
16 * commands via a miscellaneous device file.
18 * The alternate interface is needed because we need to be able open
19 * an ioctl file descriptor on an autofs mount that may be covered by
20 * another mount. This situation arises when starting automount(8)
21 * or other user space daemon which uses direct mounts or offset
22 * mounts (used for autofs lazy mount/umount of nested mount trees),
23 * which have been left busy at at service shutdown.
26 typedef int (*ioctl_fn
)(struct file
*, struct autofs_sb_info
*,
27 struct autofs_dev_ioctl
*);
29 static int check_name(const char *name
)
31 if (!strchr(name
, '/'))
37 * Check a string doesn't overrun the chunk of
38 * memory we copied from user land.
40 static int invalid_str(char *str
, size_t size
)
42 if (memchr(str
, 0, size
))
48 * Check that the user compiled against correct version of autofs
51 * As well as checking the version compatibility this always copies
52 * the kernel interface version out.
54 static int check_dev_ioctl_version(int cmd
, struct autofs_dev_ioctl
*param
)
58 if ((param
->ver_major
!= AUTOFS_DEV_IOCTL_VERSION_MAJOR
) ||
59 (param
->ver_minor
> AUTOFS_DEV_IOCTL_VERSION_MINOR
)) {
60 pr_warn("ioctl control interface version mismatch: "
61 "kernel(%u.%u), user(%u.%u), cmd(0x%08x)\n",
62 AUTOFS_DEV_IOCTL_VERSION_MAJOR
,
63 AUTOFS_DEV_IOCTL_VERSION_MINOR
,
64 param
->ver_major
, param
->ver_minor
, cmd
);
68 /* Fill in the kernel version. */
69 param
->ver_major
= AUTOFS_DEV_IOCTL_VERSION_MAJOR
;
70 param
->ver_minor
= AUTOFS_DEV_IOCTL_VERSION_MINOR
;
76 * Copy parameter control struct, including a possible path allocated
77 * at the end of the struct.
79 static struct autofs_dev_ioctl
*
80 copy_dev_ioctl(struct autofs_dev_ioctl __user
*in
)
82 struct autofs_dev_ioctl tmp
, *res
;
84 if (copy_from_user(&tmp
, in
, AUTOFS_DEV_IOCTL_SIZE
))
85 return ERR_PTR(-EFAULT
);
87 if (tmp
.size
< AUTOFS_DEV_IOCTL_SIZE
)
88 return ERR_PTR(-EINVAL
);
90 if (tmp
.size
> AUTOFS_DEV_IOCTL_SIZE
+ PATH_MAX
)
91 return ERR_PTR(-ENAMETOOLONG
);
93 res
= memdup_user(in
, tmp
.size
);
100 static inline void free_dev_ioctl(struct autofs_dev_ioctl
*param
)
106 * Check sanity of parameter control fields and if a path is present
107 * check that it is terminated and contains at least one "/".
109 static int validate_dev_ioctl(int cmd
, struct autofs_dev_ioctl
*param
)
113 err
= check_dev_ioctl_version(cmd
, param
);
115 pr_warn("invalid device control module version "
116 "supplied for cmd(0x%08x)\n", cmd
);
120 if (param
->size
> AUTOFS_DEV_IOCTL_SIZE
) {
121 err
= invalid_str(param
->path
, param
->size
- AUTOFS_DEV_IOCTL_SIZE
);
124 "path string terminator missing for cmd(0x%08x)\n",
129 err
= check_name(param
->path
);
131 pr_warn("invalid path supplied for cmd(0x%08x)\n",
136 unsigned int inr
= _IOC_NR(cmd
);
138 if (inr
== AUTOFS_DEV_IOCTL_OPENMOUNT_CMD
||
139 inr
== AUTOFS_DEV_IOCTL_REQUESTER_CMD
||
140 inr
== AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD
) {
151 /* Return autofs dev ioctl version */
152 static int autofs_dev_ioctl_version(struct file
*fp
,
153 struct autofs_sb_info
*sbi
,
154 struct autofs_dev_ioctl
*param
)
156 /* This should have already been set. */
157 param
->ver_major
= AUTOFS_DEV_IOCTL_VERSION_MAJOR
;
158 param
->ver_minor
= AUTOFS_DEV_IOCTL_VERSION_MINOR
;
162 /* Return autofs module protocol version */
163 static int autofs_dev_ioctl_protover(struct file
*fp
,
164 struct autofs_sb_info
*sbi
,
165 struct autofs_dev_ioctl
*param
)
167 param
->protover
.version
= sbi
->version
;
171 /* Return autofs module protocol sub version */
172 static int autofs_dev_ioctl_protosubver(struct file
*fp
,
173 struct autofs_sb_info
*sbi
,
174 struct autofs_dev_ioctl
*param
)
176 param
->protosubver
.sub_version
= sbi
->sub_version
;
180 /* Find the topmost mount satisfying test() */
181 static int find_autofs_mount(const char *pathname
,
183 int test(const struct path
*path
, void *data
),
189 err
= kern_path(pathname
, LOOKUP_MOUNTPOINT
, &path
);
193 while (path
.dentry
== path
.mnt
->mnt_root
) {
194 if (path
.dentry
->d_sb
->s_magic
== AUTOFS_SUPER_MAGIC
) {
195 if (test(&path
, data
)) {
202 if (!follow_up(&path
))
209 static int test_by_dev(const struct path
*path
, void *p
)
211 return path
->dentry
->d_sb
->s_dev
== *(dev_t
*)p
;
214 static int test_by_type(const struct path
*path
, void *p
)
216 struct autofs_info
*ino
= autofs_dentry_ino(path
->dentry
);
218 return ino
&& ino
->sbi
->type
& *(unsigned *)p
;
222 * Open a file descriptor on the autofs mount point corresponding
223 * to the given path and device number (aka. new_encode_dev(sb->s_dev)).
225 static int autofs_dev_ioctl_open_mountpoint(const char *name
, dev_t devid
)
229 fd
= get_unused_fd_flags(O_CLOEXEC
);
230 if (likely(fd
>= 0)) {
234 err
= find_autofs_mount(name
, &path
, test_by_dev
, &devid
);
238 filp
= dentry_open(&path
, O_RDONLY
, current_cred());
245 fd_install(fd
, filp
);
255 /* Open a file descriptor on an autofs mount point */
256 static int autofs_dev_ioctl_openmount(struct file
*fp
,
257 struct autofs_sb_info
*sbi
,
258 struct autofs_dev_ioctl
*param
)
264 /* param->path has been checked in validate_dev_ioctl() */
266 if (!param
->openmount
.devid
)
272 devid
= new_decode_dev(param
->openmount
.devid
);
275 fd
= autofs_dev_ioctl_open_mountpoint(path
, devid
);
276 if (unlikely(fd
< 0)) {
286 /* Close file descriptor allocated above (user can also use close(2)). */
287 static int autofs_dev_ioctl_closemount(struct file
*fp
,
288 struct autofs_sb_info
*sbi
,
289 struct autofs_dev_ioctl
*param
)
291 return ksys_close(param
->ioctlfd
);
295 * Send "ready" status for an existing wait (either a mount or an expire
298 static int autofs_dev_ioctl_ready(struct file
*fp
,
299 struct autofs_sb_info
*sbi
,
300 struct autofs_dev_ioctl
*param
)
304 token
= (autofs_wqt_t
) param
->ready
.token
;
305 return autofs_wait_release(sbi
, token
, 0);
309 * Send "fail" status for an existing wait (either a mount or an expire
312 static int autofs_dev_ioctl_fail(struct file
*fp
,
313 struct autofs_sb_info
*sbi
,
314 struct autofs_dev_ioctl
*param
)
319 token
= (autofs_wqt_t
) param
->fail
.token
;
320 status
= param
->fail
.status
< 0 ? param
->fail
.status
: -ENOENT
;
321 return autofs_wait_release(sbi
, token
, status
);
325 * Set the pipe fd for kernel communication to the daemon.
327 * Normally this is set at mount using an option but if we
328 * are reconnecting to a busy mount then we need to use this
329 * to tell the autofs mount about the new kernel pipe fd. In
330 * order to protect mounts against incorrectly setting the
331 * pipefd we also require that the autofs mount be catatonic.
333 * This also sets the process group id used to identify the
334 * controlling process (eg. the owning automount(8) daemon).
336 static int autofs_dev_ioctl_setpipefd(struct file
*fp
,
337 struct autofs_sb_info
*sbi
,
338 struct autofs_dev_ioctl
*param
)
342 struct pid
*new_pid
= NULL
;
344 if (param
->setpipefd
.pipefd
== -1)
347 pipefd
= param
->setpipefd
.pipefd
;
349 mutex_lock(&sbi
->wq_mutex
);
350 if (!(sbi
->flags
& AUTOFS_SBI_CATATONIC
)) {
351 mutex_unlock(&sbi
->wq_mutex
);
356 new_pid
= get_task_pid(current
, PIDTYPE_PGID
);
358 if (ns_of_pid(new_pid
) != ns_of_pid(sbi
->oz_pgrp
)) {
359 pr_warn("not allowed to change PID namespace\n");
369 if (autofs_prepare_pipe(pipe
) < 0) {
374 swap(sbi
->oz_pgrp
, new_pid
);
375 sbi
->pipefd
= pipefd
;
377 sbi
->flags
&= ~AUTOFS_SBI_CATATONIC
;
381 mutex_unlock(&sbi
->wq_mutex
);
386 * Make the autofs mount point catatonic, no longer responsive to
387 * mount requests. Also closes the kernel pipe file descriptor.
389 static int autofs_dev_ioctl_catatonic(struct file
*fp
,
390 struct autofs_sb_info
*sbi
,
391 struct autofs_dev_ioctl
*param
)
393 autofs_catatonic_mode(sbi
);
397 /* Set the autofs mount timeout */
398 static int autofs_dev_ioctl_timeout(struct file
*fp
,
399 struct autofs_sb_info
*sbi
,
400 struct autofs_dev_ioctl
*param
)
402 unsigned long timeout
;
404 timeout
= param
->timeout
.timeout
;
405 param
->timeout
.timeout
= sbi
->exp_timeout
/ HZ
;
406 sbi
->exp_timeout
= timeout
* HZ
;
411 * Return the uid and gid of the last request for the mount
413 * When reconstructing an autofs mount tree with active mounts
414 * we need to re-connect to mounts that may have used the original
415 * process uid and gid (or string variations of them) for mount
416 * lookups within the map entry.
418 static int autofs_dev_ioctl_requester(struct file
*fp
,
419 struct autofs_sb_info
*sbi
,
420 struct autofs_dev_ioctl
*param
)
422 struct autofs_info
*ino
;
427 /* param->path has been checked in validate_dev_ioctl() */
429 devid
= sbi
->sb
->s_dev
;
431 param
->requester
.uid
= param
->requester
.gid
= -1;
433 err
= find_autofs_mount(param
->path
, &path
, test_by_dev
, &devid
);
437 ino
= autofs_dentry_ino(path
.dentry
);
440 autofs_expire_wait(&path
, 0);
441 spin_lock(&sbi
->fs_lock
);
442 param
->requester
.uid
=
443 from_kuid_munged(current_user_ns(), ino
->uid
);
444 param
->requester
.gid
=
445 from_kgid_munged(current_user_ns(), ino
->gid
);
446 spin_unlock(&sbi
->fs_lock
);
454 * Call repeatedly until it returns -EAGAIN, meaning there's nothing
455 * more that can be done.
457 static int autofs_dev_ioctl_expire(struct file
*fp
,
458 struct autofs_sb_info
*sbi
,
459 struct autofs_dev_ioctl
*param
)
461 struct vfsmount
*mnt
;
464 how
= param
->expire
.how
;
465 mnt
= fp
->f_path
.mnt
;
467 return autofs_do_expire_multi(sbi
->sb
, mnt
, sbi
, how
);
470 /* Check if autofs mount point is in use */
471 static int autofs_dev_ioctl_askumount(struct file
*fp
,
472 struct autofs_sb_info
*sbi
,
473 struct autofs_dev_ioctl
*param
)
475 param
->askumount
.may_umount
= 0;
476 if (may_umount(fp
->f_path
.mnt
))
477 param
->askumount
.may_umount
= 1;
482 * Check if the given path is a mountpoint.
484 * If we are supplied with the file descriptor of an autofs
485 * mount we're looking for a specific mount. In this case
486 * the path is considered a mountpoint if it is itself a
487 * mountpoint or contains a mount, such as a multi-mount
488 * without a root mount. In this case we return 1 if the
489 * path is a mount point and the super magic of the covering
490 * mount if there is one or 0 if it isn't a mountpoint.
492 * If we aren't supplied with a file descriptor then we
493 * lookup the path and check if it is the root of a mount.
494 * If a type is given we are looking for a particular autofs
495 * mount and if we don't find a match we return fail. If the
496 * located path is the root of a mount we return 1 along with
497 * the super magic of the mount or 0 otherwise.
499 * In both cases the the device number (as returned by
500 * new_encode_dev()) is also returned.
502 static int autofs_dev_ioctl_ismountpoint(struct file
*fp
,
503 struct autofs_sb_info
*sbi
,
504 struct autofs_dev_ioctl
*param
)
509 unsigned int devid
, magic
;
512 /* param->path has been checked in validate_dev_ioctl() */
515 type
= param
->ismountpoint
.in
.type
;
517 param
->ismountpoint
.out
.devid
= devid
= 0;
518 param
->ismountpoint
.out
.magic
= magic
= 0;
520 if (!fp
|| param
->ioctlfd
== -1) {
521 if (autofs_type_any(type
))
522 err
= kern_path(name
, LOOKUP_FOLLOW
| LOOKUP_MOUNTPOINT
,
525 err
= find_autofs_mount(name
, &path
,
526 test_by_type
, &type
);
529 devid
= new_encode_dev(path
.dentry
->d_sb
->s_dev
);
531 if (path
.mnt
->mnt_root
== path
.dentry
) {
533 magic
= path
.dentry
->d_sb
->s_magic
;
536 dev_t dev
= sbi
->sb
->s_dev
;
538 err
= find_autofs_mount(name
, &path
, test_by_dev
, &dev
);
542 devid
= new_encode_dev(dev
);
544 err
= path_has_submounts(&path
);
546 if (follow_down_one(&path
))
547 magic
= path
.dentry
->d_sb
->s_magic
;
550 param
->ismountpoint
.out
.devid
= devid
;
551 param
->ismountpoint
.out
.magic
= magic
;
558 * Our range of ioctl numbers isn't 0 based so we need to shift
559 * the array index by _IOC_NR(AUTOFS_CTL_IOC_FIRST) for the table
562 #define cmd_idx(cmd) (cmd - _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST))
564 static ioctl_fn
lookup_dev_ioctl(unsigned int cmd
)
566 static ioctl_fn _ioctls
[] = {
567 autofs_dev_ioctl_version
,
568 autofs_dev_ioctl_protover
,
569 autofs_dev_ioctl_protosubver
,
570 autofs_dev_ioctl_openmount
,
571 autofs_dev_ioctl_closemount
,
572 autofs_dev_ioctl_ready
,
573 autofs_dev_ioctl_fail
,
574 autofs_dev_ioctl_setpipefd
,
575 autofs_dev_ioctl_catatonic
,
576 autofs_dev_ioctl_timeout
,
577 autofs_dev_ioctl_requester
,
578 autofs_dev_ioctl_expire
,
579 autofs_dev_ioctl_askumount
,
580 autofs_dev_ioctl_ismountpoint
,
582 unsigned int idx
= cmd_idx(cmd
);
584 return (idx
>= ARRAY_SIZE(_ioctls
)) ? NULL
: _ioctls
[idx
];
587 /* ioctl dispatcher */
588 static int _autofs_dev_ioctl(unsigned int command
,
589 struct autofs_dev_ioctl __user
*user
)
591 struct autofs_dev_ioctl
*param
;
593 struct autofs_sb_info
*sbi
;
594 unsigned int cmd_first
, cmd
;
598 cmd_first
= _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST
);
599 cmd
= _IOC_NR(command
);
601 if (_IOC_TYPE(command
) != _IOC_TYPE(AUTOFS_DEV_IOCTL_IOC_FIRST
) ||
602 cmd
- cmd_first
> AUTOFS_DEV_IOCTL_IOC_COUNT
) {
606 /* Only root can use ioctls other than AUTOFS_DEV_IOCTL_VERSION_CMD
607 * and AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD
609 if (cmd
!= AUTOFS_DEV_IOCTL_VERSION_CMD
&&
610 cmd
!= AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD
&&
611 !capable(CAP_SYS_ADMIN
))
614 /* Copy the parameters into kernel space. */
615 param
= copy_dev_ioctl(user
);
617 return PTR_ERR(param
);
619 err
= validate_dev_ioctl(command
, param
);
623 fn
= lookup_dev_ioctl(cmd
);
625 pr_warn("unknown command 0x%08x\n", command
);
634 * For obvious reasons the openmount can't have a file
635 * descriptor yet. We don't take a reference to the
636 * file during close to allow for immediate release,
637 * and the same for retrieving ioctl version.
639 if (cmd
!= AUTOFS_DEV_IOCTL_VERSION_CMD
&&
640 cmd
!= AUTOFS_DEV_IOCTL_OPENMOUNT_CMD
&&
641 cmd
!= AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD
) {
642 struct super_block
*sb
;
644 fp
= fget(param
->ioctlfd
);
646 if (cmd
== AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD
)
652 sb
= file_inode(fp
)->i_sb
;
653 if (sb
->s_type
!= &autofs_fs_type
) {
658 sbi
= autofs_sbi(sb
);
661 * Admin needs to be able to set the mount catatonic in
662 * order to be able to perform the re-open.
664 if (!autofs_oz_mode(sbi
) &&
665 cmd
!= AUTOFS_DEV_IOCTL_CATATONIC_CMD
) {
672 err
= fn(fp
, sbi
, param
);
676 if (err
>= 0 && copy_to_user(user
, param
, AUTOFS_DEV_IOCTL_SIZE
))
679 free_dev_ioctl(param
);
683 static long autofs_dev_ioctl(struct file
*file
, unsigned int command
,
688 err
= _autofs_dev_ioctl(command
, (struct autofs_dev_ioctl __user
*) u
);
693 static long autofs_dev_ioctl_compat(struct file
*file
, unsigned int command
,
696 return autofs_dev_ioctl(file
, command
, (unsigned long) compat_ptr(u
));
699 #define autofs_dev_ioctl_compat NULL
702 static const struct file_operations _dev_ioctl_fops
= {
703 .unlocked_ioctl
= autofs_dev_ioctl
,
704 .compat_ioctl
= autofs_dev_ioctl_compat
,
705 .owner
= THIS_MODULE
,
706 .llseek
= noop_llseek
,
709 static struct miscdevice _autofs_dev_ioctl_misc
= {
710 .minor
= AUTOFS_MINOR
,
711 .name
= AUTOFS_DEVICE_NAME
,
712 .fops
= &_dev_ioctl_fops
,
716 MODULE_ALIAS_MISCDEV(AUTOFS_MINOR
);
717 MODULE_ALIAS("devname:autofs");
719 /* Register/deregister misc character device */
720 int __init
autofs_dev_ioctl_init(void)
724 r
= misc_register(&_autofs_dev_ioctl_misc
);
726 pr_err("misc_register failed for control device\n");
733 void autofs_dev_ioctl_exit(void)
735 misc_deregister(&_autofs_dev_ioctl_misc
);