Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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