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