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