1
2
3
4
5
6
7#include <linux/miscdevice.h>
8#include <linux/compat.h>
9#include <linux/syscalls.h>
10#include <linux/magic.h>
11#include <linux/nospec.h>
12
13#include "autofs_i.h"
14
15
16
17
18
19
20
21
22
23
24
25
26
27typedef int (*ioctl_fn)(struct file *, struct autofs_sb_info *,
28 struct autofs_dev_ioctl *);
29
30static int check_name(const char *name)
31{
32 if (!strchr(name, '/'))
33 return -EINVAL;
34 return 0;
35}
36
37
38
39
40
41static int invalid_str(char *str, size_t size)
42{
43 if (memchr(str, 0, size))
44 return 0;
45 return -EINVAL;
46}
47
48
49
50
51
52
53
54
55static int check_dev_ioctl_version(int cmd, struct autofs_dev_ioctl *param)
56{
57 int err = 0;
58
59 if ((param->ver_major != AUTOFS_DEV_IOCTL_VERSION_MAJOR) ||
60 (param->ver_minor > AUTOFS_DEV_IOCTL_VERSION_MINOR)) {
61 pr_warn("ioctl control interface version mismatch: "
62 "kernel(%u.%u), user(%u.%u), cmd(0x%08x)\n",
63 AUTOFS_DEV_IOCTL_VERSION_MAJOR,
64 AUTOFS_DEV_IOCTL_VERSION_MINOR,
65 param->ver_major, param->ver_minor, cmd);
66 err = -EINVAL;
67 }
68
69
70 param->ver_major = AUTOFS_DEV_IOCTL_VERSION_MAJOR;
71 param->ver_minor = AUTOFS_DEV_IOCTL_VERSION_MINOR;
72
73 return err;
74}
75
76
77
78
79
80static struct autofs_dev_ioctl *
81copy_dev_ioctl(struct autofs_dev_ioctl __user *in)
82{
83 struct autofs_dev_ioctl tmp, *res;
84
85 if (copy_from_user(&tmp, in, AUTOFS_DEV_IOCTL_SIZE))
86 return ERR_PTR(-EFAULT);
87
88 if (tmp.size < AUTOFS_DEV_IOCTL_SIZE)
89 return ERR_PTR(-EINVAL);
90
91 if (tmp.size > AUTOFS_DEV_IOCTL_SIZE + PATH_MAX)
92 return ERR_PTR(-ENAMETOOLONG);
93
94 res = memdup_user(in, tmp.size);
95 if (!IS_ERR(res))
96 res->size = tmp.size;
97
98 return res;
99}
100
101static inline void free_dev_ioctl(struct autofs_dev_ioctl *param)
102{
103 kfree(param);
104}
105
106
107
108
109
110static int validate_dev_ioctl(int cmd, struct autofs_dev_ioctl *param)
111{
112 int err;
113
114 err = check_dev_ioctl_version(cmd, param);
115 if (err) {
116 pr_warn("invalid device control module version "
117 "supplied for cmd(0x%08x)\n", cmd);
118 goto out;
119 }
120
121 if (param->size > AUTOFS_DEV_IOCTL_SIZE) {
122 err = invalid_str(param->path, param->size - AUTOFS_DEV_IOCTL_SIZE);
123 if (err) {
124 pr_warn(
125 "path string terminator missing for cmd(0x%08x)\n",
126 cmd);
127 goto out;
128 }
129
130 err = check_name(param->path);
131 if (err) {
132 pr_warn("invalid path supplied for cmd(0x%08x)\n",
133 cmd);
134 goto out;
135 }
136 } else {
137 unsigned int inr = _IOC_NR(cmd);
138
139 if (inr == AUTOFS_DEV_IOCTL_OPENMOUNT_CMD ||
140 inr == AUTOFS_DEV_IOCTL_REQUESTER_CMD ||
141 inr == AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD) {
142 err = -EINVAL;
143 goto out;
144 }
145 }
146
147 err = 0;
148out:
149 return err;
150}
151
152
153static int autofs_dev_ioctl_version(struct file *fp,
154 struct autofs_sb_info *sbi,
155 struct autofs_dev_ioctl *param)
156{
157
158 param->ver_major = AUTOFS_DEV_IOCTL_VERSION_MAJOR;
159 param->ver_minor = AUTOFS_DEV_IOCTL_VERSION_MINOR;
160 return 0;
161}
162
163
164static int autofs_dev_ioctl_protover(struct file *fp,
165 struct autofs_sb_info *sbi,
166 struct autofs_dev_ioctl *param)
167{
168 param->protover.version = sbi->version;
169 return 0;
170}
171
172
173static int autofs_dev_ioctl_protosubver(struct file *fp,
174 struct autofs_sb_info *sbi,
175 struct autofs_dev_ioctl *param)
176{
177 param->protosubver.sub_version = sbi->sub_version;
178 return 0;
179}
180
181
182static int find_autofs_mount(const char *pathname,
183 struct path *res,
184 int test(const struct path *path, void *data),
185 void *data)
186{
187 struct path path;
188 int err;
189
190 err = kern_path(pathname, LOOKUP_MOUNTPOINT, &path);
191 if (err)
192 return err;
193 err = -ENOENT;
194 while (path.dentry == path.mnt->mnt_root) {
195 if (path.dentry->d_sb->s_magic == AUTOFS_SUPER_MAGIC) {
196 if (test(&path, data)) {
197 path_get(&path);
198 *res = path;
199 err = 0;
200 break;
201 }
202 }
203 if (!follow_up(&path))
204 break;
205 }
206 path_put(&path);
207 return err;
208}
209
210static int test_by_dev(const struct path *path, void *p)
211{
212 return path->dentry->d_sb->s_dev == *(dev_t *)p;
213}
214
215static int test_by_type(const struct path *path, void *p)
216{
217 struct autofs_info *ino = autofs_dentry_ino(path->dentry);
218
219 return ino && ino->sbi->type & *(unsigned *)p;
220}
221
222
223
224
225
226static int autofs_dev_ioctl_open_mountpoint(const char *name, dev_t devid)
227{
228 int err, fd;
229
230 fd = get_unused_fd_flags(O_CLOEXEC);
231 if (likely(fd >= 0)) {
232 struct file *filp;
233 struct path path;
234
235 err = find_autofs_mount(name, &path, test_by_dev, &devid);
236 if (err)
237 goto out;
238
239 filp = dentry_open(&path, O_RDONLY, current_cred());
240 path_put(&path);
241 if (IS_ERR(filp)) {
242 err = PTR_ERR(filp);
243 goto out;
244 }
245
246 fd_install(fd, filp);
247 }
248
249 return fd;
250
251out:
252 put_unused_fd(fd);
253 return err;
254}
255
256
257static int autofs_dev_ioctl_openmount(struct file *fp,
258 struct autofs_sb_info *sbi,
259 struct autofs_dev_ioctl *param)
260{
261 const char *path;
262 dev_t devid;
263 int err, fd;
264
265
266
267 if (!param->openmount.devid)
268 return -EINVAL;
269
270 param->ioctlfd = -1;
271
272 path = param->path;
273 devid = new_decode_dev(param->openmount.devid);
274
275 err = 0;
276 fd = autofs_dev_ioctl_open_mountpoint(path, devid);
277 if (unlikely(fd < 0)) {
278 err = fd;
279 goto out;
280 }
281
282 param->ioctlfd = fd;
283out:
284 return err;
285}
286
287
288static int autofs_dev_ioctl_closemount(struct file *fp,
289 struct autofs_sb_info *sbi,
290 struct autofs_dev_ioctl *param)
291{
292 return ksys_close(param->ioctlfd);
293}
294
295
296
297
298
299static int autofs_dev_ioctl_ready(struct file *fp,
300 struct autofs_sb_info *sbi,
301 struct autofs_dev_ioctl *param)
302{
303 autofs_wqt_t token;
304
305 token = (autofs_wqt_t) param->ready.token;
306 return autofs_wait_release(sbi, token, 0);
307}
308
309
310
311
312
313static int autofs_dev_ioctl_fail(struct file *fp,
314 struct autofs_sb_info *sbi,
315 struct autofs_dev_ioctl *param)
316{
317 autofs_wqt_t token;
318 int status;
319
320 token = (autofs_wqt_t) param->fail.token;
321 status = param->fail.status < 0 ? param->fail.status : -ENOENT;
322 return autofs_wait_release(sbi, token, status);
323}
324
325
326
327
328
329
330
331
332
333
334
335
336
337static int autofs_dev_ioctl_setpipefd(struct file *fp,
338 struct autofs_sb_info *sbi,
339 struct autofs_dev_ioctl *param)
340{
341 int pipefd;
342 int err = 0;
343 struct pid *new_pid = NULL;
344
345 if (param->setpipefd.pipefd == -1)
346 return -EINVAL;
347
348 pipefd = param->setpipefd.pipefd;
349
350 mutex_lock(&sbi->wq_mutex);
351 if (!(sbi->flags & AUTOFS_SBI_CATATONIC)) {
352 mutex_unlock(&sbi->wq_mutex);
353 return -EBUSY;
354 } else {
355 struct file *pipe;
356
357 new_pid = get_task_pid(current, PIDTYPE_PGID);
358
359 if (ns_of_pid(new_pid) != ns_of_pid(sbi->oz_pgrp)) {
360 pr_warn("not allowed to change PID namespace\n");
361 err = -EINVAL;
362 goto out;
363 }
364
365 pipe = fget(pipefd);
366 if (!pipe) {
367 err = -EBADF;
368 goto out;
369 }
370 if (autofs_prepare_pipe(pipe) < 0) {
371 err = -EPIPE;
372 fput(pipe);
373 goto out;
374 }
375 swap(sbi->oz_pgrp, new_pid);
376 sbi->pipefd = pipefd;
377 sbi->pipe = pipe;
378 sbi->flags &= ~AUTOFS_SBI_CATATONIC;
379 }
380out:
381 put_pid(new_pid);
382 mutex_unlock(&sbi->wq_mutex);
383 return err;
384}
385
386
387
388
389
390static int autofs_dev_ioctl_catatonic(struct file *fp,
391 struct autofs_sb_info *sbi,
392 struct autofs_dev_ioctl *param)
393{
394 autofs_catatonic_mode(sbi);
395 return 0;
396}
397
398
399static int autofs_dev_ioctl_timeout(struct file *fp,
400 struct autofs_sb_info *sbi,
401 struct autofs_dev_ioctl *param)
402{
403 unsigned long timeout;
404
405 timeout = param->timeout.timeout;
406 param->timeout.timeout = sbi->exp_timeout / HZ;
407 sbi->exp_timeout = timeout * HZ;
408 return 0;
409}
410
411
412
413
414
415
416
417
418
419static int autofs_dev_ioctl_requester(struct file *fp,
420 struct autofs_sb_info *sbi,
421 struct autofs_dev_ioctl *param)
422{
423 struct autofs_info *ino;
424 struct path path;
425 dev_t devid;
426 int err = -ENOENT;
427
428
429
430 devid = sbi->sb->s_dev;
431
432 param->requester.uid = param->requester.gid = -1;
433
434 err = find_autofs_mount(param->path, &path, test_by_dev, &devid);
435 if (err)
436 goto out;
437
438 ino = autofs_dentry_ino(path.dentry);
439 if (ino) {
440 err = 0;
441 autofs_expire_wait(&path, 0);
442 spin_lock(&sbi->fs_lock);
443 param->requester.uid =
444 from_kuid_munged(current_user_ns(), ino->uid);
445 param->requester.gid =
446 from_kgid_munged(current_user_ns(), ino->gid);
447 spin_unlock(&sbi->fs_lock);
448 }
449 path_put(&path);
450out:
451 return err;
452}
453
454
455
456
457
458static int autofs_dev_ioctl_expire(struct file *fp,
459 struct autofs_sb_info *sbi,
460 struct autofs_dev_ioctl *param)
461{
462 struct vfsmount *mnt;
463 int how;
464
465 how = param->expire.how;
466 mnt = fp->f_path.mnt;
467
468 return autofs_do_expire_multi(sbi->sb, mnt, sbi, how);
469}
470
471
472static int autofs_dev_ioctl_askumount(struct file *fp,
473 struct autofs_sb_info *sbi,
474 struct autofs_dev_ioctl *param)
475{
476 param->askumount.may_umount = 0;
477 if (may_umount(fp->f_path.mnt))
478 param->askumount.may_umount = 1;
479 return 0;
480}
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503static int autofs_dev_ioctl_ismountpoint(struct file *fp,
504 struct autofs_sb_info *sbi,
505 struct autofs_dev_ioctl *param)
506{
507 struct path path;
508 const char *name;
509 unsigned int type;
510 unsigned int devid, magic;
511 int err = -ENOENT;
512
513
514
515 name = param->path;
516 type = param->ismountpoint.in.type;
517
518 param->ismountpoint.out.devid = devid = 0;
519 param->ismountpoint.out.magic = magic = 0;
520
521 if (!fp || param->ioctlfd == -1) {
522 if (autofs_type_any(type))
523 err = kern_path(name, LOOKUP_FOLLOW | LOOKUP_MOUNTPOINT,
524 &path);
525 else
526 err = find_autofs_mount(name, &path,
527 test_by_type, &type);
528 if (err)
529 goto out;
530 devid = new_encode_dev(path.dentry->d_sb->s_dev);
531 err = 0;
532 if (path.mnt->mnt_root == path.dentry) {
533 err = 1;
534 magic = path.dentry->d_sb->s_magic;
535 }
536 } else {
537 dev_t dev = sbi->sb->s_dev;
538
539 err = find_autofs_mount(name, &path, test_by_dev, &dev);
540 if (err)
541 goto out;
542
543 devid = new_encode_dev(dev);
544
545 err = path_has_submounts(&path);
546
547 if (follow_down_one(&path))
548 magic = path.dentry->d_sb->s_magic;
549 }
550
551 param->ismountpoint.out.devid = devid;
552 param->ismountpoint.out.magic = magic;
553 path_put(&path);
554out:
555 return err;
556}
557
558
559
560
561
562
563#define cmd_idx(cmd) (cmd - _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST))
564
565static ioctl_fn lookup_dev_ioctl(unsigned int cmd)
566{
567 static const ioctl_fn _ioctls[] = {
568 autofs_dev_ioctl_version,
569 autofs_dev_ioctl_protover,
570 autofs_dev_ioctl_protosubver,
571 autofs_dev_ioctl_openmount,
572 autofs_dev_ioctl_closemount,
573 autofs_dev_ioctl_ready,
574 autofs_dev_ioctl_fail,
575 autofs_dev_ioctl_setpipefd,
576 autofs_dev_ioctl_catatonic,
577 autofs_dev_ioctl_timeout,
578 autofs_dev_ioctl_requester,
579 autofs_dev_ioctl_expire,
580 autofs_dev_ioctl_askumount,
581 autofs_dev_ioctl_ismountpoint,
582 };
583 unsigned int idx = cmd_idx(cmd);
584
585 if (idx >= ARRAY_SIZE(_ioctls))
586 return NULL;
587 idx = array_index_nospec(idx, ARRAY_SIZE(_ioctls));
588 return _ioctls[idx];
589}
590
591
592static int _autofs_dev_ioctl(unsigned int command,
593 struct autofs_dev_ioctl __user *user)
594{
595 struct autofs_dev_ioctl *param;
596 struct file *fp;
597 struct autofs_sb_info *sbi;
598 unsigned int cmd_first, cmd;
599 ioctl_fn fn = NULL;
600 int err = 0;
601
602 cmd_first = _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST);
603 cmd = _IOC_NR(command);
604
605 if (_IOC_TYPE(command) != _IOC_TYPE(AUTOFS_DEV_IOCTL_IOC_FIRST) ||
606 cmd - cmd_first > AUTOFS_DEV_IOCTL_IOC_COUNT) {
607 return -ENOTTY;
608 }
609
610
611
612
613 if (cmd != AUTOFS_DEV_IOCTL_VERSION_CMD &&
614 cmd != AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD &&
615 !capable(CAP_SYS_ADMIN))
616 return -EPERM;
617
618
619 param = copy_dev_ioctl(user);
620 if (IS_ERR(param))
621 return PTR_ERR(param);
622
623 err = validate_dev_ioctl(command, param);
624 if (err)
625 goto out;
626
627 fn = lookup_dev_ioctl(cmd);
628 if (!fn) {
629 pr_warn("unknown command 0x%08x\n", command);
630 err = -ENOTTY;
631 goto out;
632 }
633
634 fp = NULL;
635 sbi = NULL;
636
637
638
639
640
641
642
643 if (cmd != AUTOFS_DEV_IOCTL_VERSION_CMD &&
644 cmd != AUTOFS_DEV_IOCTL_OPENMOUNT_CMD &&
645 cmd != AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD) {
646 struct super_block *sb;
647
648 fp = fget(param->ioctlfd);
649 if (!fp) {
650 if (cmd == AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD)
651 goto cont;
652 err = -EBADF;
653 goto out;
654 }
655
656 sb = file_inode(fp)->i_sb;
657 if (sb->s_type != &autofs_fs_type) {
658 err = -EINVAL;
659 fput(fp);
660 goto out;
661 }
662 sbi = autofs_sbi(sb);
663
664
665
666
667
668 if (!autofs_oz_mode(sbi) &&
669 cmd != AUTOFS_DEV_IOCTL_CATATONIC_CMD) {
670 err = -EACCES;
671 fput(fp);
672 goto out;
673 }
674 }
675cont:
676 err = fn(fp, sbi, param);
677
678 if (fp)
679 fput(fp);
680 if (err >= 0 && copy_to_user(user, param, AUTOFS_DEV_IOCTL_SIZE))
681 err = -EFAULT;
682out:
683 free_dev_ioctl(param);
684 return err;
685}
686
687static long autofs_dev_ioctl(struct file *file, unsigned int command,
688 unsigned long u)
689{
690 int err;
691
692 err = _autofs_dev_ioctl(command, (struct autofs_dev_ioctl __user *) u);
693 return (long) err;
694}
695
696#ifdef CONFIG_COMPAT
697static long autofs_dev_ioctl_compat(struct file *file, unsigned int command,
698 unsigned long u)
699{
700 return autofs_dev_ioctl(file, command, (unsigned long) compat_ptr(u));
701}
702#else
703#define autofs_dev_ioctl_compat NULL
704#endif
705
706static const struct file_operations _dev_ioctl_fops = {
707 .unlocked_ioctl = autofs_dev_ioctl,
708 .compat_ioctl = autofs_dev_ioctl_compat,
709 .owner = THIS_MODULE,
710 .llseek = noop_llseek,
711};
712
713static struct miscdevice _autofs_dev_ioctl_misc = {
714 .minor = AUTOFS_MINOR,
715 .name = AUTOFS_DEVICE_NAME,
716 .fops = &_dev_ioctl_fops,
717 .mode = 0644,
718};
719
720MODULE_ALIAS_MISCDEV(AUTOFS_MINOR);
721MODULE_ALIAS("devname:autofs");
722
723
724int __init autofs_dev_ioctl_init(void)
725{
726 int r;
727
728 r = misc_register(&_autofs_dev_ioctl_misc);
729 if (r) {
730 pr_err("misc_register failed for control device\n");
731 return r;
732 }
733
734 return 0;
735}
736
737void autofs_dev_ioctl_exit(void)
738{
739 misc_deregister(&_autofs_dev_ioctl_misc);
740}
741