0001
0002
0003
0004
0005
0006
0007 #include <linux/module.h>
0008 #include <linux/miscdevice.h>
0009 #include <linux/compat.h>
0010 #include <linux/fdtable.h>
0011 #include <linux/magic.h>
0012 #include <linux/nospec.h>
0013
0014 #include "autofs_i.h"
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 typedef int (*ioctl_fn)(struct file *, struct autofs_sb_info *,
0029 struct autofs_dev_ioctl *);
0030
0031 static int check_name(const char *name)
0032 {
0033 if (!strchr(name, '/'))
0034 return -EINVAL;
0035 return 0;
0036 }
0037
0038
0039
0040
0041
0042 static int invalid_str(char *str, size_t size)
0043 {
0044 if (memchr(str, 0, size))
0045 return 0;
0046 return -EINVAL;
0047 }
0048
0049
0050
0051
0052
0053
0054
0055
0056 static int check_dev_ioctl_version(int cmd, struct autofs_dev_ioctl *param)
0057 {
0058 int err = 0;
0059
0060 if ((param->ver_major != AUTOFS_DEV_IOCTL_VERSION_MAJOR) ||
0061 (param->ver_minor > AUTOFS_DEV_IOCTL_VERSION_MINOR)) {
0062 pr_warn("ioctl control interface version mismatch: "
0063 "kernel(%u.%u), user(%u.%u), cmd(0x%08x)\n",
0064 AUTOFS_DEV_IOCTL_VERSION_MAJOR,
0065 AUTOFS_DEV_IOCTL_VERSION_MINOR,
0066 param->ver_major, param->ver_minor, cmd);
0067 err = -EINVAL;
0068 }
0069
0070
0071 param->ver_major = AUTOFS_DEV_IOCTL_VERSION_MAJOR;
0072 param->ver_minor = AUTOFS_DEV_IOCTL_VERSION_MINOR;
0073
0074 return err;
0075 }
0076
0077
0078
0079
0080
0081 static struct autofs_dev_ioctl *
0082 copy_dev_ioctl(struct autofs_dev_ioctl __user *in)
0083 {
0084 struct autofs_dev_ioctl tmp, *res;
0085
0086 if (copy_from_user(&tmp, in, AUTOFS_DEV_IOCTL_SIZE))
0087 return ERR_PTR(-EFAULT);
0088
0089 if (tmp.size < AUTOFS_DEV_IOCTL_SIZE)
0090 return ERR_PTR(-EINVAL);
0091
0092 if (tmp.size > AUTOFS_DEV_IOCTL_SIZE + PATH_MAX)
0093 return ERR_PTR(-ENAMETOOLONG);
0094
0095 res = memdup_user(in, tmp.size);
0096 if (!IS_ERR(res))
0097 res->size = tmp.size;
0098
0099 return res;
0100 }
0101
0102 static inline void free_dev_ioctl(struct autofs_dev_ioctl *param)
0103 {
0104 kfree(param);
0105 }
0106
0107
0108
0109
0110
0111 static int validate_dev_ioctl(int cmd, struct autofs_dev_ioctl *param)
0112 {
0113 int err;
0114
0115 err = check_dev_ioctl_version(cmd, param);
0116 if (err) {
0117 pr_warn("invalid device control module version "
0118 "supplied for cmd(0x%08x)\n", cmd);
0119 goto out;
0120 }
0121
0122 if (param->size > AUTOFS_DEV_IOCTL_SIZE) {
0123 err = invalid_str(param->path, param->size - AUTOFS_DEV_IOCTL_SIZE);
0124 if (err) {
0125 pr_warn(
0126 "path string terminator missing for cmd(0x%08x)\n",
0127 cmd);
0128 goto out;
0129 }
0130
0131 err = check_name(param->path);
0132 if (err) {
0133 pr_warn("invalid path supplied for cmd(0x%08x)\n",
0134 cmd);
0135 goto out;
0136 }
0137 } else {
0138 unsigned int inr = _IOC_NR(cmd);
0139
0140 if (inr == AUTOFS_DEV_IOCTL_OPENMOUNT_CMD ||
0141 inr == AUTOFS_DEV_IOCTL_REQUESTER_CMD ||
0142 inr == AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD) {
0143 err = -EINVAL;
0144 goto out;
0145 }
0146 }
0147
0148 err = 0;
0149 out:
0150 return err;
0151 }
0152
0153
0154 static int autofs_dev_ioctl_version(struct file *fp,
0155 struct autofs_sb_info *sbi,
0156 struct autofs_dev_ioctl *param)
0157 {
0158
0159 param->ver_major = AUTOFS_DEV_IOCTL_VERSION_MAJOR;
0160 param->ver_minor = AUTOFS_DEV_IOCTL_VERSION_MINOR;
0161 return 0;
0162 }
0163
0164
0165 static int autofs_dev_ioctl_protover(struct file *fp,
0166 struct autofs_sb_info *sbi,
0167 struct autofs_dev_ioctl *param)
0168 {
0169 param->protover.version = sbi->version;
0170 return 0;
0171 }
0172
0173
0174 static int autofs_dev_ioctl_protosubver(struct file *fp,
0175 struct autofs_sb_info *sbi,
0176 struct autofs_dev_ioctl *param)
0177 {
0178 param->protosubver.sub_version = sbi->sub_version;
0179 return 0;
0180 }
0181
0182
0183 static int find_autofs_mount(const char *pathname,
0184 struct path *res,
0185 int test(const struct path *path, void *data),
0186 void *data)
0187 {
0188 struct path path;
0189 int err;
0190
0191 err = kern_path(pathname, LOOKUP_MOUNTPOINT, &path);
0192 if (err)
0193 return err;
0194 err = -ENOENT;
0195 while (path.dentry == path.mnt->mnt_root) {
0196 if (path.dentry->d_sb->s_magic == AUTOFS_SUPER_MAGIC) {
0197 if (test(&path, data)) {
0198 path_get(&path);
0199 *res = path;
0200 err = 0;
0201 break;
0202 }
0203 }
0204 if (!follow_up(&path))
0205 break;
0206 }
0207 path_put(&path);
0208 return err;
0209 }
0210
0211 static int test_by_dev(const struct path *path, void *p)
0212 {
0213 return path->dentry->d_sb->s_dev == *(dev_t *)p;
0214 }
0215
0216 static int test_by_type(const struct path *path, void *p)
0217 {
0218 struct autofs_info *ino = autofs_dentry_ino(path->dentry);
0219
0220 return ino && ino->sbi->type & *(unsigned *)p;
0221 }
0222
0223
0224
0225
0226
0227 static int autofs_dev_ioctl_open_mountpoint(const char *name, dev_t devid)
0228 {
0229 int err, fd;
0230
0231 fd = get_unused_fd_flags(O_CLOEXEC);
0232 if (likely(fd >= 0)) {
0233 struct file *filp;
0234 struct path path;
0235
0236 err = find_autofs_mount(name, &path, test_by_dev, &devid);
0237 if (err)
0238 goto out;
0239
0240 filp = dentry_open(&path, O_RDONLY, current_cred());
0241 path_put(&path);
0242 if (IS_ERR(filp)) {
0243 err = PTR_ERR(filp);
0244 goto out;
0245 }
0246
0247 fd_install(fd, filp);
0248 }
0249
0250 return fd;
0251
0252 out:
0253 put_unused_fd(fd);
0254 return err;
0255 }
0256
0257
0258 static int autofs_dev_ioctl_openmount(struct file *fp,
0259 struct autofs_sb_info *sbi,
0260 struct autofs_dev_ioctl *param)
0261 {
0262 const char *path;
0263 dev_t devid;
0264 int err, fd;
0265
0266
0267
0268 if (!param->openmount.devid)
0269 return -EINVAL;
0270
0271 param->ioctlfd = -1;
0272
0273 path = param->path;
0274 devid = new_decode_dev(param->openmount.devid);
0275
0276 err = 0;
0277 fd = autofs_dev_ioctl_open_mountpoint(path, devid);
0278 if (unlikely(fd < 0)) {
0279 err = fd;
0280 goto out;
0281 }
0282
0283 param->ioctlfd = fd;
0284 out:
0285 return err;
0286 }
0287
0288
0289 static int autofs_dev_ioctl_closemount(struct file *fp,
0290 struct autofs_sb_info *sbi,
0291 struct autofs_dev_ioctl *param)
0292 {
0293 return close_fd(param->ioctlfd);
0294 }
0295
0296
0297
0298
0299
0300 static int autofs_dev_ioctl_ready(struct file *fp,
0301 struct autofs_sb_info *sbi,
0302 struct autofs_dev_ioctl *param)
0303 {
0304 autofs_wqt_t token;
0305
0306 token = (autofs_wqt_t) param->ready.token;
0307 return autofs_wait_release(sbi, token, 0);
0308 }
0309
0310
0311
0312
0313
0314 static int autofs_dev_ioctl_fail(struct file *fp,
0315 struct autofs_sb_info *sbi,
0316 struct autofs_dev_ioctl *param)
0317 {
0318 autofs_wqt_t token;
0319 int status;
0320
0321 token = (autofs_wqt_t) param->fail.token;
0322 status = param->fail.status < 0 ? param->fail.status : -ENOENT;
0323 return autofs_wait_release(sbi, token, status);
0324 }
0325
0326
0327
0328
0329
0330
0331
0332
0333
0334
0335
0336
0337
0338 static int autofs_dev_ioctl_setpipefd(struct file *fp,
0339 struct autofs_sb_info *sbi,
0340 struct autofs_dev_ioctl *param)
0341 {
0342 int pipefd;
0343 int err = 0;
0344 struct pid *new_pid = NULL;
0345
0346 if (param->setpipefd.pipefd == -1)
0347 return -EINVAL;
0348
0349 pipefd = param->setpipefd.pipefd;
0350
0351 mutex_lock(&sbi->wq_mutex);
0352 if (!(sbi->flags & AUTOFS_SBI_CATATONIC)) {
0353 mutex_unlock(&sbi->wq_mutex);
0354 return -EBUSY;
0355 } else {
0356 struct file *pipe;
0357
0358 new_pid = get_task_pid(current, PIDTYPE_PGID);
0359
0360 if (ns_of_pid(new_pid) != ns_of_pid(sbi->oz_pgrp)) {
0361 pr_warn("not allowed to change PID namespace\n");
0362 err = -EINVAL;
0363 goto out;
0364 }
0365
0366 pipe = fget(pipefd);
0367 if (!pipe) {
0368 err = -EBADF;
0369 goto out;
0370 }
0371 if (autofs_prepare_pipe(pipe) < 0) {
0372 err = -EPIPE;
0373 fput(pipe);
0374 goto out;
0375 }
0376 swap(sbi->oz_pgrp, new_pid);
0377 sbi->pipefd = pipefd;
0378 sbi->pipe = pipe;
0379 sbi->flags &= ~AUTOFS_SBI_CATATONIC;
0380 }
0381 out:
0382 put_pid(new_pid);
0383 mutex_unlock(&sbi->wq_mutex);
0384 return err;
0385 }
0386
0387
0388
0389
0390
0391 static int autofs_dev_ioctl_catatonic(struct file *fp,
0392 struct autofs_sb_info *sbi,
0393 struct autofs_dev_ioctl *param)
0394 {
0395 autofs_catatonic_mode(sbi);
0396 return 0;
0397 }
0398
0399
0400 static int autofs_dev_ioctl_timeout(struct file *fp,
0401 struct autofs_sb_info *sbi,
0402 struct autofs_dev_ioctl *param)
0403 {
0404 unsigned long timeout;
0405
0406 timeout = param->timeout.timeout;
0407 param->timeout.timeout = sbi->exp_timeout / HZ;
0408 sbi->exp_timeout = timeout * HZ;
0409 return 0;
0410 }
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420 static int autofs_dev_ioctl_requester(struct file *fp,
0421 struct autofs_sb_info *sbi,
0422 struct autofs_dev_ioctl *param)
0423 {
0424 struct autofs_info *ino;
0425 struct path path;
0426 dev_t devid;
0427 int err = -ENOENT;
0428
0429
0430
0431 devid = sbi->sb->s_dev;
0432
0433 param->requester.uid = param->requester.gid = -1;
0434
0435 err = find_autofs_mount(param->path, &path, test_by_dev, &devid);
0436 if (err)
0437 goto out;
0438
0439 ino = autofs_dentry_ino(path.dentry);
0440 if (ino) {
0441 err = 0;
0442 autofs_expire_wait(&path, 0);
0443 spin_lock(&sbi->fs_lock);
0444 param->requester.uid =
0445 from_kuid_munged(current_user_ns(), ino->uid);
0446 param->requester.gid =
0447 from_kgid_munged(current_user_ns(), ino->gid);
0448 spin_unlock(&sbi->fs_lock);
0449 }
0450 path_put(&path);
0451 out:
0452 return err;
0453 }
0454
0455
0456
0457
0458
0459 static int autofs_dev_ioctl_expire(struct file *fp,
0460 struct autofs_sb_info *sbi,
0461 struct autofs_dev_ioctl *param)
0462 {
0463 struct vfsmount *mnt;
0464 int how;
0465
0466 how = param->expire.how;
0467 mnt = fp->f_path.mnt;
0468
0469 return autofs_do_expire_multi(sbi->sb, mnt, sbi, how);
0470 }
0471
0472
0473 static int autofs_dev_ioctl_askumount(struct file *fp,
0474 struct autofs_sb_info *sbi,
0475 struct autofs_dev_ioctl *param)
0476 {
0477 param->askumount.may_umount = 0;
0478 if (may_umount(fp->f_path.mnt))
0479 param->askumount.may_umount = 1;
0480 return 0;
0481 }
0482
0483
0484
0485
0486
0487
0488
0489
0490
0491
0492
0493
0494
0495
0496
0497
0498
0499
0500
0501
0502
0503
0504 static int autofs_dev_ioctl_ismountpoint(struct file *fp,
0505 struct autofs_sb_info *sbi,
0506 struct autofs_dev_ioctl *param)
0507 {
0508 struct path path;
0509 const char *name;
0510 unsigned int type;
0511 unsigned int devid, magic;
0512 int err = -ENOENT;
0513
0514
0515
0516 name = param->path;
0517 type = param->ismountpoint.in.type;
0518
0519 param->ismountpoint.out.devid = devid = 0;
0520 param->ismountpoint.out.magic = magic = 0;
0521
0522 if (!fp || param->ioctlfd == -1) {
0523 if (autofs_type_any(type))
0524 err = kern_path(name, LOOKUP_FOLLOW | LOOKUP_MOUNTPOINT,
0525 &path);
0526 else
0527 err = find_autofs_mount(name, &path,
0528 test_by_type, &type);
0529 if (err)
0530 goto out;
0531 devid = new_encode_dev(path.dentry->d_sb->s_dev);
0532 err = 0;
0533 if (path.mnt->mnt_root == path.dentry) {
0534 err = 1;
0535 magic = path.dentry->d_sb->s_magic;
0536 }
0537 } else {
0538 dev_t dev = sbi->sb->s_dev;
0539
0540 err = find_autofs_mount(name, &path, test_by_dev, &dev);
0541 if (err)
0542 goto out;
0543
0544 devid = new_encode_dev(dev);
0545
0546 err = path_has_submounts(&path);
0547
0548 if (follow_down_one(&path))
0549 magic = path.dentry->d_sb->s_magic;
0550 }
0551
0552 param->ismountpoint.out.devid = devid;
0553 param->ismountpoint.out.magic = magic;
0554 path_put(&path);
0555 out:
0556 return err;
0557 }
0558
0559
0560
0561
0562
0563
0564 #define cmd_idx(cmd) (cmd - _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST))
0565
0566 static ioctl_fn lookup_dev_ioctl(unsigned int cmd)
0567 {
0568 static const ioctl_fn _ioctls[] = {
0569 autofs_dev_ioctl_version,
0570 autofs_dev_ioctl_protover,
0571 autofs_dev_ioctl_protosubver,
0572 autofs_dev_ioctl_openmount,
0573 autofs_dev_ioctl_closemount,
0574 autofs_dev_ioctl_ready,
0575 autofs_dev_ioctl_fail,
0576 autofs_dev_ioctl_setpipefd,
0577 autofs_dev_ioctl_catatonic,
0578 autofs_dev_ioctl_timeout,
0579 autofs_dev_ioctl_requester,
0580 autofs_dev_ioctl_expire,
0581 autofs_dev_ioctl_askumount,
0582 autofs_dev_ioctl_ismountpoint,
0583 };
0584 unsigned int idx = cmd_idx(cmd);
0585
0586 if (idx >= ARRAY_SIZE(_ioctls))
0587 return NULL;
0588 idx = array_index_nospec(idx, ARRAY_SIZE(_ioctls));
0589 return _ioctls[idx];
0590 }
0591
0592
0593 static int _autofs_dev_ioctl(unsigned int command,
0594 struct autofs_dev_ioctl __user *user)
0595 {
0596 struct autofs_dev_ioctl *param;
0597 struct file *fp;
0598 struct autofs_sb_info *sbi;
0599 unsigned int cmd_first, cmd;
0600 ioctl_fn fn = NULL;
0601 int err = 0;
0602
0603 cmd_first = _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST);
0604 cmd = _IOC_NR(command);
0605
0606 if (_IOC_TYPE(command) != _IOC_TYPE(AUTOFS_DEV_IOCTL_IOC_FIRST) ||
0607 cmd - cmd_first > AUTOFS_DEV_IOCTL_IOC_COUNT) {
0608 return -ENOTTY;
0609 }
0610
0611
0612
0613
0614 if (cmd != AUTOFS_DEV_IOCTL_VERSION_CMD &&
0615 cmd != AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD &&
0616 !capable(CAP_SYS_ADMIN))
0617 return -EPERM;
0618
0619
0620 param = copy_dev_ioctl(user);
0621 if (IS_ERR(param))
0622 return PTR_ERR(param);
0623
0624 err = validate_dev_ioctl(command, param);
0625 if (err)
0626 goto out;
0627
0628 fn = lookup_dev_ioctl(cmd);
0629 if (!fn) {
0630 pr_warn("unknown command 0x%08x\n", command);
0631 err = -ENOTTY;
0632 goto out;
0633 }
0634
0635 fp = NULL;
0636 sbi = NULL;
0637
0638
0639
0640
0641
0642
0643
0644 if (cmd != AUTOFS_DEV_IOCTL_VERSION_CMD &&
0645 cmd != AUTOFS_DEV_IOCTL_OPENMOUNT_CMD &&
0646 cmd != AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD) {
0647 struct super_block *sb;
0648
0649 fp = fget(param->ioctlfd);
0650 if (!fp) {
0651 if (cmd == AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD)
0652 goto cont;
0653 err = -EBADF;
0654 goto out;
0655 }
0656
0657 sb = file_inode(fp)->i_sb;
0658 if (sb->s_type != &autofs_fs_type) {
0659 err = -EINVAL;
0660 fput(fp);
0661 goto out;
0662 }
0663 sbi = autofs_sbi(sb);
0664
0665
0666
0667
0668
0669 if (!autofs_oz_mode(sbi) &&
0670 cmd != AUTOFS_DEV_IOCTL_CATATONIC_CMD) {
0671 err = -EACCES;
0672 fput(fp);
0673 goto out;
0674 }
0675 }
0676 cont:
0677 err = fn(fp, sbi, param);
0678
0679 if (fp)
0680 fput(fp);
0681 if (err >= 0 && copy_to_user(user, param, AUTOFS_DEV_IOCTL_SIZE))
0682 err = -EFAULT;
0683 out:
0684 free_dev_ioctl(param);
0685 return err;
0686 }
0687
0688 static long autofs_dev_ioctl(struct file *file, unsigned int command,
0689 unsigned long u)
0690 {
0691 int err;
0692
0693 err = _autofs_dev_ioctl(command, (struct autofs_dev_ioctl __user *) u);
0694 return (long) err;
0695 }
0696
0697 #ifdef CONFIG_COMPAT
0698 static long autofs_dev_ioctl_compat(struct file *file, unsigned int command,
0699 unsigned long u)
0700 {
0701 return autofs_dev_ioctl(file, command, (unsigned long) compat_ptr(u));
0702 }
0703 #else
0704 #define autofs_dev_ioctl_compat NULL
0705 #endif
0706
0707 static const struct file_operations _dev_ioctl_fops = {
0708 .unlocked_ioctl = autofs_dev_ioctl,
0709 .compat_ioctl = autofs_dev_ioctl_compat,
0710 .owner = THIS_MODULE,
0711 .llseek = noop_llseek,
0712 };
0713
0714 static struct miscdevice _autofs_dev_ioctl_misc = {
0715 .minor = AUTOFS_MINOR,
0716 .name = AUTOFS_DEVICE_NAME,
0717 .fops = &_dev_ioctl_fops,
0718 .mode = 0644,
0719 };
0720
0721 MODULE_ALIAS_MISCDEV(AUTOFS_MINOR);
0722 MODULE_ALIAS("devname:autofs");
0723
0724
0725 int __init autofs_dev_ioctl_init(void)
0726 {
0727 int r;
0728
0729 r = misc_register(&_autofs_dev_ioctl_misc);
0730 if (r) {
0731 pr_err("misc_register failed for control device\n");
0732 return r;
0733 }
0734
0735 return 0;
0736 }
0737
0738 void autofs_dev_ioctl_exit(void)
0739 {
0740 misc_deregister(&_autofs_dev_ioctl_misc);
0741 }