blob: 806df746f1a93b5a06ca970735faa3e775c28566 [file] [log] [blame]
Ian Kent8d7b48e2008-10-15 22:02:54 -07001/*
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>
20#include <linux/compat.h>
21#include <linux/syscalls.h>
Ian Kent8d7b48e2008-10-15 22:02:54 -070022#include <linux/magic.h>
23#include <linux/dcache.h>
24#include <linux/uaccess.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Ian Kent8d7b48e2008-10-15 22:02:54 -070026
27#include "autofs_i.h"
28
29/*
30 * This module implements an interface for routing autofs ioctl control
31 * commands via a miscellaneous device file.
32 *
33 * The alternate interface is needed because we need to be able open
34 * an ioctl file descriptor on an autofs mount that may be covered by
35 * another mount. This situation arises when starting automount(8)
36 * or other user space daemon which uses direct mounts or offset
37 * mounts (used for autofs lazy mount/umount of nested mount trees),
38 * which have been left busy at at service shutdown.
39 */
40
Ian Kent8d7b48e2008-10-15 22:02:54 -070041typedef int (*ioctl_fn)(struct file *, struct autofs_sb_info *,
42 struct autofs_dev_ioctl *);
43
44static int check_name(const char *name)
45{
46 if (!strchr(name, '/'))
47 return -EINVAL;
48 return 0;
49}
50
51/*
52 * Check a string doesn't overrun the chunk of
53 * memory we copied from user land.
54 */
Al Viro3eac8772009-04-07 11:12:46 -040055static int invalid_str(char *str, size_t size)
Ian Kent8d7b48e2008-10-15 22:02:54 -070056{
Al Viro3eac8772009-04-07 11:12:46 -040057 if (memchr(str, 0, size))
58 return 0;
Ian Kent8d7b48e2008-10-15 22:02:54 -070059 return -EINVAL;
60}
61
62/*
63 * Check that the user compiled against correct version of autofs
64 * misc device code.
65 *
66 * As well as checking the version compatibility this always copies
67 * the kernel interface version out.
68 */
69static int check_dev_ioctl_version(int cmd, struct autofs_dev_ioctl *param)
70{
71 int err = 0;
72
Ian Kente9a7c2f2016-03-15 14:58:25 -070073 if ((param->ver_major != AUTOFS_DEV_IOCTL_VERSION_MAJOR) ||
74 (param->ver_minor > AUTOFS_DEV_IOCTL_VERSION_MINOR)) {
Ian Kent8a78d592016-03-15 14:58:45 -070075 pr_warn("ioctl control interface version mismatch: "
Tomohiro Kusumi39085552016-10-11 13:53:08 -070076 "kernel(%u.%u), user(%u.%u), cmd(0x%08x)\n",
Ian Kent8a78d592016-03-15 14:58:45 -070077 AUTOFS_DEV_IOCTL_VERSION_MAJOR,
78 AUTOFS_DEV_IOCTL_VERSION_MINOR,
79 param->ver_major, param->ver_minor, cmd);
Ian Kent8d7b48e2008-10-15 22:02:54 -070080 err = -EINVAL;
81 }
82
83 /* Fill in the kernel version. */
84 param->ver_major = AUTOFS_DEV_IOCTL_VERSION_MAJOR;
85 param->ver_minor = AUTOFS_DEV_IOCTL_VERSION_MINOR;
86
87 return err;
88}
89
90/*
91 * Copy parameter control struct, including a possible path allocated
92 * at the end of the struct.
93 */
Ian Kente9a7c2f2016-03-15 14:58:25 -070094static struct autofs_dev_ioctl *
95 copy_dev_ioctl(struct autofs_dev_ioctl __user *in)
Ian Kent8d7b48e2008-10-15 22:02:54 -070096{
Al Viro0a280962015-02-21 22:19:57 -050097 struct autofs_dev_ioctl tmp, *res;
Ian Kent8d7b48e2008-10-15 22:02:54 -070098
99 if (copy_from_user(&tmp, in, sizeof(tmp)))
100 return ERR_PTR(-EFAULT);
101
102 if (tmp.size < sizeof(tmp))
103 return ERR_PTR(-EINVAL);
104
Sasha Levine53d77e2014-04-08 16:04:11 -0700105 if (tmp.size > (PATH_MAX + sizeof(tmp)))
106 return ERR_PTR(-ENAMETOOLONG);
107
Al Viro0a280962015-02-21 22:19:57 -0500108 res = memdup_user(in, tmp.size);
109 if (!IS_ERR(res))
110 res->size = tmp.size;
111
112 return res;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700113}
114
115static inline void free_dev_ioctl(struct autofs_dev_ioctl *param)
116{
117 kfree(param);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700118}
119
120/*
121 * Check sanity of parameter control fields and if a path is present
Ian Kentbae8ec62009-01-06 14:42:09 -0800122 * check that it is terminated and contains at least one "/".
Ian Kent8d7b48e2008-10-15 22:02:54 -0700123 */
124static int validate_dev_ioctl(int cmd, struct autofs_dev_ioctl *param)
125{
Ian Kent96b03172008-11-06 12:53:23 -0800126 int err;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700127
Ian Kent96b03172008-11-06 12:53:23 -0800128 err = check_dev_ioctl_version(cmd, param);
129 if (err) {
Ian Kent8a78d592016-03-15 14:58:45 -0700130 pr_warn("invalid device control module version "
131 "supplied for cmd(0x%08x)\n", cmd);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700132 goto out;
133 }
134
135 if (param->size > sizeof(*param)) {
Al Viro3eac8772009-04-07 11:12:46 -0400136 err = invalid_str(param->path, param->size - sizeof(*param));
Ian Kent8d7b48e2008-10-15 22:02:54 -0700137 if (err) {
Ian Kent8a78d592016-03-15 14:58:45 -0700138 pr_warn(
Ian Kent90967c82016-03-15 14:58:42 -0700139 "path string terminator missing for cmd(0x%08x)\n",
Ian Kentbae8ec62009-01-06 14:42:09 -0800140 cmd);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700141 goto out;
142 }
143
Ian Kentbae8ec62009-01-06 14:42:09 -0800144 err = check_name(param->path);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700145 if (err) {
Ian Kent8a78d592016-03-15 14:58:45 -0700146 pr_warn("invalid path supplied for cmd(0x%08x)\n",
147 cmd);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700148 goto out;
149 }
150 }
151
152 err = 0;
153out:
154 return err;
155}
156
157/*
158 * Get the autofs super block info struct from the file opened on
159 * the autofs mount point.
160 */
161static struct autofs_sb_info *autofs_dev_ioctl_sbi(struct file *f)
162{
163 struct autofs_sb_info *sbi = NULL;
164 struct inode *inode;
165
166 if (f) {
Al Viro496ad9a2013-01-23 17:07:38 -0500167 inode = file_inode(f);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700168 sbi = autofs4_sbi(inode->i_sb);
169 }
170 return sbi;
171}
172
Ian Kentd9e19232016-10-11 13:53:05 -0700173/* Return autofs dev ioctl version */
174static int autofs_dev_ioctl_version(struct file *fp,
175 struct autofs_sb_info *sbi,
176 struct autofs_dev_ioctl *param)
177{
178 /* This should have already been set. */
179 param->ver_major = AUTOFS_DEV_IOCTL_VERSION_MAJOR;
180 param->ver_minor = AUTOFS_DEV_IOCTL_VERSION_MINOR;
181 return 0;
182}
183
Ian Kent8d7b48e2008-10-15 22:02:54 -0700184/* Return autofs module protocol version */
185static int autofs_dev_ioctl_protover(struct file *fp,
186 struct autofs_sb_info *sbi,
187 struct autofs_dev_ioctl *param)
188{
Ian Kent730c9ee2009-01-06 14:42:06 -0800189 param->protover.version = sbi->version;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700190 return 0;
191}
192
193/* Return autofs module protocol sub version */
194static int autofs_dev_ioctl_protosubver(struct file *fp,
195 struct autofs_sb_info *sbi,
196 struct autofs_dev_ioctl *param)
197{
Ian Kent730c9ee2009-01-06 14:42:06 -0800198 param->protosubver.sub_version = sbi->sub_version;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700199 return 0;
200}
201
Ian Kentac838712013-09-08 16:47:23 +0800202/* Find the topmost mount satisfying test() */
Al Viro4e44b682009-04-07 11:08:56 -0400203static int find_autofs_mount(const char *pathname,
204 struct path *res,
Al Viro5b5577e2016-11-20 19:33:32 -0500205 int test(const struct path *path, void *data),
Al Viro4e44b682009-04-07 11:08:56 -0400206 void *data)
Ian Kent8d7b48e2008-10-15 22:02:54 -0700207{
Al Viro4e44b682009-04-07 11:08:56 -0400208 struct path path;
Ian Kente9a7c2f2016-03-15 14:58:25 -0700209 int err;
210
211 err = kern_path_mountpoint(AT_FDCWD, pathname, &path, 0);
Al Viro4e44b682009-04-07 11:08:56 -0400212 if (err)
213 return err;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700214 err = -ENOENT;
Al Viro4e44b682009-04-07 11:08:56 -0400215 while (path.dentry == path.mnt->mnt_root) {
Al Virod8c95842011-12-07 18:16:57 -0500216 if (path.dentry->d_sb->s_magic == AUTOFS_SUPER_MAGIC) {
Al Viro4e44b682009-04-07 11:08:56 -0400217 if (test(&path, data)) {
218 path_get(&path);
Al Viro4e44b682009-04-07 11:08:56 -0400219 *res = path;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700220 err = 0;
Ian Kentac838712013-09-08 16:47:23 +0800221 break;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700222 }
223 }
Al Virobab77eb2009-04-18 03:26:48 -0400224 if (!follow_up(&path))
Al Viro4e44b682009-04-07 11:08:56 -0400225 break;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700226 }
Al Viro4e44b682009-04-07 11:08:56 -0400227 path_put(&path);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700228 return err;
229}
230
Al Viro5b5577e2016-11-20 19:33:32 -0500231static int test_by_dev(const struct path *path, void *p)
Ian Kent8d7b48e2008-10-15 22:02:54 -0700232{
Al Virod8c95842011-12-07 18:16:57 -0500233 return path->dentry->d_sb->s_dev == *(dev_t *)p;
Al Viro4e44b682009-04-07 11:08:56 -0400234}
Ian Kent8d7b48e2008-10-15 22:02:54 -0700235
Al Viro5b5577e2016-11-20 19:33:32 -0500236static int test_by_type(const struct path *path, void *p)
Al Viro4e44b682009-04-07 11:08:56 -0400237{
238 struct autofs_info *ino = autofs4_dentry_ino(path->dentry);
Ian Kente9a7c2f2016-03-15 14:58:25 -0700239
Al Viro4e44b682009-04-07 11:08:56 -0400240 return ino && ino->sbi->type & *(unsigned *)p;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700241}
242
Ian Kent8d7b48e2008-10-15 22:02:54 -0700243/*
244 * Open a file descriptor on the autofs mount point corresponding
245 * to the given path and device number (aka. new_encode_dev(sb->s_dev)).
246 */
Al Viro4e44b682009-04-07 11:08:56 -0400247static int autofs_dev_ioctl_open_mountpoint(const char *name, dev_t devid)
Ian Kent8d7b48e2008-10-15 22:02:54 -0700248{
Ian Kent8d7b48e2008-10-15 22:02:54 -0700249 int err, fd;
250
Al Viroc921b402012-08-12 18:04:37 -0400251 fd = get_unused_fd_flags(O_CLOEXEC);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700252 if (likely(fd >= 0)) {
Al Viro4e44b682009-04-07 11:08:56 -0400253 struct file *filp;
254 struct path path;
255
256 err = find_autofs_mount(name, &path, test_by_dev, &devid);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700257 if (err)
258 goto out;
259
260 /*
Al Viro4e44b682009-04-07 11:08:56 -0400261 * Find autofs super block that has the device number
Ian Kent8d7b48e2008-10-15 22:02:54 -0700262 * corresponding to the autofs fs we want to open.
263 */
Ian Kent8d7b48e2008-10-15 22:02:54 -0700264
Al Viro765927b2012-06-26 21:58:53 +0400265 filp = dentry_open(&path, O_RDONLY, current_cred());
266 path_put(&path);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700267 if (IS_ERR(filp)) {
268 err = PTR_ERR(filp);
269 goto out;
270 }
271
Al Viroc921b402012-08-12 18:04:37 -0400272 fd_install(fd, filp);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700273 }
274
275 return fd;
276
277out:
278 put_unused_fd(fd);
279 return err;
280}
281
282/* Open a file descriptor on an autofs mount point */
283static int autofs_dev_ioctl_openmount(struct file *fp,
284 struct autofs_sb_info *sbi,
285 struct autofs_dev_ioctl *param)
286{
287 const char *path;
288 dev_t devid;
289 int err, fd;
290
291 /* param->path has already been checked */
Ian Kent730c9ee2009-01-06 14:42:06 -0800292 if (!param->openmount.devid)
Ian Kent8d7b48e2008-10-15 22:02:54 -0700293 return -EINVAL;
294
295 param->ioctlfd = -1;
296
297 path = param->path;
Al Viro4e44b682009-04-07 11:08:56 -0400298 devid = new_decode_dev(param->openmount.devid);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700299
300 err = 0;
301 fd = autofs_dev_ioctl_open_mountpoint(path, devid);
302 if (unlikely(fd < 0)) {
303 err = fd;
304 goto out;
305 }
306
307 param->ioctlfd = fd;
308out:
309 return err;
310}
311
312/* Close file descriptor allocated above (user can also use close(2)). */
313static int autofs_dev_ioctl_closemount(struct file *fp,
314 struct autofs_sb_info *sbi,
315 struct autofs_dev_ioctl *param)
316{
317 return sys_close(param->ioctlfd);
318}
319
320/*
321 * Send "ready" status for an existing wait (either a mount or an expire
322 * request).
323 */
324static int autofs_dev_ioctl_ready(struct file *fp,
325 struct autofs_sb_info *sbi,
326 struct autofs_dev_ioctl *param)
327{
328 autofs_wqt_t token;
329
Ian Kent730c9ee2009-01-06 14:42:06 -0800330 token = (autofs_wqt_t) param->ready.token;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700331 return autofs4_wait_release(sbi, token, 0);
332}
333
334/*
335 * Send "fail" status for an existing wait (either a mount or an expire
336 * request).
337 */
338static int autofs_dev_ioctl_fail(struct file *fp,
339 struct autofs_sb_info *sbi,
340 struct autofs_dev_ioctl *param)
341{
342 autofs_wqt_t token;
343 int status;
344
Ian Kent730c9ee2009-01-06 14:42:06 -0800345 token = (autofs_wqt_t) param->fail.token;
346 status = param->fail.status ? param->fail.status : -ENOENT;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700347 return autofs4_wait_release(sbi, token, status);
348}
349
350/*
351 * Set the pipe fd for kernel communication to the daemon.
352 *
353 * Normally this is set at mount using an option but if we
354 * are reconnecting to a busy mount then we need to use this
355 * to tell the autofs mount about the new kernel pipe fd. In
356 * order to protect mounts against incorrectly setting the
357 * pipefd we also require that the autofs mount be catatonic.
358 *
359 * This also sets the process group id used to identify the
360 * controlling process (eg. the owning automount(8) daemon).
361 */
362static int autofs_dev_ioctl_setpipefd(struct file *fp,
363 struct autofs_sb_info *sbi,
364 struct autofs_dev_ioctl *param)
365{
366 int pipefd;
367 int err = 0;
Sukadev Bhattiprolu6eaba352014-01-23 15:54:57 -0800368 struct pid *new_pid = NULL;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700369
Ian Kent730c9ee2009-01-06 14:42:06 -0800370 if (param->setpipefd.pipefd == -1)
Ian Kent8d7b48e2008-10-15 22:02:54 -0700371 return -EINVAL;
372
Ian Kent730c9ee2009-01-06 14:42:06 -0800373 pipefd = param->setpipefd.pipefd;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700374
375 mutex_lock(&sbi->wq_mutex);
376 if (!sbi->catatonic) {
377 mutex_unlock(&sbi->wq_mutex);
378 return -EBUSY;
379 } else {
Sukadev Bhattiprolu6eaba352014-01-23 15:54:57 -0800380 struct file *pipe;
381
382 new_pid = get_task_pid(current, PIDTYPE_PGID);
383
384 if (ns_of_pid(new_pid) != ns_of_pid(sbi->oz_pgrp)) {
Ian Kent8a78d592016-03-15 14:58:45 -0700385 pr_warn("not allowed to change PID namespace\n");
Sukadev Bhattiprolu6eaba352014-01-23 15:54:57 -0800386 err = -EINVAL;
387 goto out;
388 }
389
390 pipe = fget(pipefd);
Jesper Juhl3dc8fe42011-03-25 01:51:37 +0800391 if (!pipe) {
392 err = -EBADF;
393 goto out;
394 }
Linus Torvalds64f371b2012-04-29 13:30:08 -0700395 if (autofs_prepare_pipe(pipe) < 0) {
Ian Kent8d7b48e2008-10-15 22:02:54 -0700396 err = -EPIPE;
397 fput(pipe);
398 goto out;
399 }
Sukadev Bhattiprolu6eaba352014-01-23 15:54:57 -0800400 swap(sbi->oz_pgrp, new_pid);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700401 sbi->pipefd = pipefd;
402 sbi->pipe = pipe;
403 sbi->catatonic = 0;
404 }
405out:
Sukadev Bhattiprolu6eaba352014-01-23 15:54:57 -0800406 put_pid(new_pid);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700407 mutex_unlock(&sbi->wq_mutex);
408 return err;
409}
410
411/*
412 * Make the autofs mount point catatonic, no longer responsive to
413 * mount requests. Also closes the kernel pipe file descriptor.
414 */
415static int autofs_dev_ioctl_catatonic(struct file *fp,
416 struct autofs_sb_info *sbi,
417 struct autofs_dev_ioctl *param)
418{
419 autofs4_catatonic_mode(sbi);
420 return 0;
421}
422
423/* Set the autofs mount timeout */
424static int autofs_dev_ioctl_timeout(struct file *fp,
425 struct autofs_sb_info *sbi,
426 struct autofs_dev_ioctl *param)
427{
428 unsigned long timeout;
429
Ian Kent730c9ee2009-01-06 14:42:06 -0800430 timeout = param->timeout.timeout;
431 param->timeout.timeout = sbi->exp_timeout / HZ;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700432 sbi->exp_timeout = timeout * HZ;
433 return 0;
434}
435
436/*
437 * Return the uid and gid of the last request for the mount
438 *
439 * When reconstructing an autofs mount tree with active mounts
440 * we need to re-connect to mounts that may have used the original
441 * process uid and gid (or string variations of them) for mount
442 * lookups within the map entry.
443 */
444static int autofs_dev_ioctl_requester(struct file *fp,
445 struct autofs_sb_info *sbi,
446 struct autofs_dev_ioctl *param)
447{
448 struct autofs_info *ino;
Al Viro4e44b682009-04-07 11:08:56 -0400449 struct path path;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700450 dev_t devid;
451 int err = -ENOENT;
452
453 if (param->size <= sizeof(*param)) {
454 err = -EINVAL;
455 goto out;
456 }
457
Al Viro4e44b682009-04-07 11:08:56 -0400458 devid = sbi->sb->s_dev;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700459
Ian Kent730c9ee2009-01-06 14:42:06 -0800460 param->requester.uid = param->requester.gid = -1;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700461
Al Viro4e44b682009-04-07 11:08:56 -0400462 err = find_autofs_mount(param->path, &path, test_by_dev, &devid);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700463 if (err)
464 goto out;
465
Al Viro4e44b682009-04-07 11:08:56 -0400466 ino = autofs4_dentry_ino(path.dentry);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700467 if (ino) {
468 err = 0;
Ian Kent74f504c2016-11-24 08:03:41 +1100469 autofs4_expire_wait(&path, 0);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700470 spin_lock(&sbi->fs_lock);
Ian Kente9a7c2f2016-03-15 14:58:25 -0700471 param->requester.uid =
472 from_kuid_munged(current_user_ns(), ino->uid);
473 param->requester.gid =
474 from_kgid_munged(current_user_ns(), ino->gid);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700475 spin_unlock(&sbi->fs_lock);
476 }
Al Viro4e44b682009-04-07 11:08:56 -0400477 path_put(&path);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700478out:
479 return err;
480}
481
482/*
483 * Call repeatedly until it returns -EAGAIN, meaning there's nothing
484 * more that can be done.
485 */
486static int autofs_dev_ioctl_expire(struct file *fp,
487 struct autofs_sb_info *sbi,
488 struct autofs_dev_ioctl *param)
489{
Ian Kent8d7b48e2008-10-15 22:02:54 -0700490 struct vfsmount *mnt;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700491 int how;
492
Ian Kent730c9ee2009-01-06 14:42:06 -0800493 how = param->expire.how;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700494 mnt = fp->f_path.mnt;
495
Ian Kent56fcef72009-03-31 15:24:43 -0700496 return autofs4_do_expire_multi(sbi->sb, mnt, sbi, how);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700497}
498
499/* Check if autofs mount point is in use */
500static int autofs_dev_ioctl_askumount(struct file *fp,
501 struct autofs_sb_info *sbi,
502 struct autofs_dev_ioctl *param)
503{
Ian Kent730c9ee2009-01-06 14:42:06 -0800504 param->askumount.may_umount = 0;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700505 if (may_umount(fp->f_path.mnt))
Ian Kent730c9ee2009-01-06 14:42:06 -0800506 param->askumount.may_umount = 1;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700507 return 0;
508}
509
510/*
511 * Check if the given path is a mountpoint.
512 *
513 * If we are supplied with the file descriptor of an autofs
514 * mount we're looking for a specific mount. In this case
515 * the path is considered a mountpoint if it is itself a
516 * mountpoint or contains a mount, such as a multi-mount
517 * without a root mount. In this case we return 1 if the
518 * path is a mount point and the super magic of the covering
519 * mount if there is one or 0 if it isn't a mountpoint.
520 *
521 * If we aren't supplied with a file descriptor then we
Ian Kentac838712013-09-08 16:47:23 +0800522 * lookup the path and check if it is the root of a mount.
523 * If a type is given we are looking for a particular autofs
524 * mount and if we don't find a match we return fail. If the
525 * located path is the root of a mount we return 1 along with
526 * the super magic of the mount or 0 otherwise.
Ian Kent8d7b48e2008-10-15 22:02:54 -0700527 *
528 * In both cases the the device number (as returned by
529 * new_encode_dev()) is also returned.
530 */
531static int autofs_dev_ioctl_ismountpoint(struct file *fp,
532 struct autofs_sb_info *sbi,
533 struct autofs_dev_ioctl *param)
534{
Al Viro4e44b682009-04-07 11:08:56 -0400535 struct path path;
536 const char *name;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700537 unsigned int type;
Ian Kent730c9ee2009-01-06 14:42:06 -0800538 unsigned int devid, magic;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700539 int err = -ENOENT;
540
541 if (param->size <= sizeof(*param)) {
542 err = -EINVAL;
543 goto out;
544 }
545
Al Viro4e44b682009-04-07 11:08:56 -0400546 name = param->path;
Ian Kent730c9ee2009-01-06 14:42:06 -0800547 type = param->ismountpoint.in.type;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700548
Ian Kent730c9ee2009-01-06 14:42:06 -0800549 param->ismountpoint.out.devid = devid = 0;
550 param->ismountpoint.out.magic = magic = 0;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700551
552 if (!fp || param->ioctlfd == -1) {
Al Viro4e44b682009-04-07 11:08:56 -0400553 if (autofs_type_any(type))
Ian Kentac838712013-09-08 16:47:23 +0800554 err = kern_path_mountpoint(AT_FDCWD,
555 name, &path, LOOKUP_FOLLOW);
Al Viro4e44b682009-04-07 11:08:56 -0400556 else
Ian Kentac838712013-09-08 16:47:23 +0800557 err = find_autofs_mount(name, &path,
558 test_by_type, &type);
Al Viro4e44b682009-04-07 11:08:56 -0400559 if (err)
560 goto out;
Al Virod8c95842011-12-07 18:16:57 -0500561 devid = new_encode_dev(path.dentry->d_sb->s_dev);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700562 err = 0;
Al Virof598f9f2010-01-23 20:08:53 -0500563 if (path.mnt->mnt_root == path.dentry) {
Ian Kent8d7b48e2008-10-15 22:02:54 -0700564 err = 1;
Al Virod8c95842011-12-07 18:16:57 -0500565 magic = path.dentry->d_sb->s_magic;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700566 }
567 } else {
Al Viro4e44b682009-04-07 11:08:56 -0400568 dev_t dev = sbi->sb->s_dev;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700569
Al Viro4e44b682009-04-07 11:08:56 -0400570 err = find_autofs_mount(name, &path, test_by_dev, &dev);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700571 if (err)
572 goto out;
573
Al Viro4e44b682009-04-07 11:08:56 -0400574 devid = new_encode_dev(dev);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700575
Ian Kent60359742016-11-24 08:03:42 +1100576 err = path_has_submounts(&path);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700577
David Howellscc53ce52011-01-14 18:45:26 +0000578 if (follow_down_one(&path))
Al Virod8c95842011-12-07 18:16:57 -0500579 magic = path.dentry->d_sb->s_magic;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700580 }
581
Ian Kent730c9ee2009-01-06 14:42:06 -0800582 param->ismountpoint.out.devid = devid;
583 param->ismountpoint.out.magic = magic;
Al Viro4e44b682009-04-07 11:08:56 -0400584 path_put(&path);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700585out:
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
596static ioctl_fn lookup_dev_ioctl(unsigned int cmd)
597{
Tomohiro Kusumifcc24532016-10-11 13:53:19 -0700598 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 Kent8d7b48e2008-10-15 22:02:54 -0700613 };
614 unsigned int idx = cmd_idx(cmd);
615
Tomohiro Kusumifcc24532016-10-11 13:53:19 -0700616 return (idx >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[idx];
Ian Kent8d7b48e2008-10-15 22:02:54 -0700617}
618
619/* ioctl dispatcher */
Ian Kente9a7c2f2016-03-15 14:58:25 -0700620static int _autofs_dev_ioctl(unsigned int command,
621 struct autofs_dev_ioctl __user *user)
Ian Kent8d7b48e2008-10-15 22:02:54 -0700622{
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
630 /* only root can play with this */
631 if (!capable(CAP_SYS_ADMIN))
632 return -EPERM;
633
634 cmd_first = _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST);
635 cmd = _IOC_NR(command);
636
637 if (_IOC_TYPE(command) != _IOC_TYPE(AUTOFS_DEV_IOCTL_IOC_FIRST) ||
Ian Kentaa841932016-10-11 13:53:02 -0700638 cmd - cmd_first > AUTOFS_DEV_IOCTL_IOC_COUNT) {
Ian Kent8d7b48e2008-10-15 22:02:54 -0700639 return -ENOTTY;
640 }
641
642 /* Copy the parameters into kernel space. */
643 param = copy_dev_ioctl(user);
644 if (IS_ERR(param))
645 return PTR_ERR(param);
646
647 err = validate_dev_ioctl(command, param);
648 if (err)
649 goto out;
650
Ian Kent8d7b48e2008-10-15 22:02:54 -0700651 fn = lookup_dev_ioctl(cmd);
652 if (!fn) {
Ian Kent8a78d592016-03-15 14:58:45 -0700653 pr_warn("unknown command 0x%08x\n", command);
Tomohiro Kusumi41a44972016-10-11 13:52:48 -0700654 err = -ENOTTY;
655 goto out;
Ian Kent8d7b48e2008-10-15 22:02:54 -0700656 }
657
658 fp = NULL;
659 sbi = NULL;
660
661 /*
662 * For obvious reasons the openmount can't have a file
663 * descriptor yet. We don't take a reference to the
Ian Kentd9e19232016-10-11 13:53:05 -0700664 * file during close to allow for immediate release,
665 * and the same for retrieving ioctl version.
Ian Kent8d7b48e2008-10-15 22:02:54 -0700666 */
Ian Kentd9e19232016-10-11 13:53:05 -0700667 if (cmd != AUTOFS_DEV_IOCTL_VERSION_CMD &&
668 cmd != AUTOFS_DEV_IOCTL_OPENMOUNT_CMD &&
Ian Kent8d7b48e2008-10-15 22:02:54 -0700669 cmd != AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD) {
670 fp = fget(param->ioctlfd);
671 if (!fp) {
672 if (cmd == AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD)
673 goto cont;
674 err = -EBADF;
675 goto out;
676 }
677
Ian Kent8d7b48e2008-10-15 22:02:54 -0700678 sbi = autofs_dev_ioctl_sbi(fp);
679 if (!sbi || sbi->magic != AUTOFS_SBI_MAGIC) {
680 err = -EINVAL;
681 fput(fp);
682 goto out;
683 }
684
685 /*
686 * Admin needs to be able to set the mount catatonic in
687 * order to be able to perform the re-open.
688 */
689 if (!autofs4_oz_mode(sbi) &&
690 cmd != AUTOFS_DEV_IOCTL_CATATONIC_CMD) {
691 err = -EACCES;
692 fput(fp);
693 goto out;
694 }
695 }
696cont:
697 err = fn(fp, sbi, param);
698
699 if (fp)
700 fput(fp);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700701 if (err >= 0 && copy_to_user(user, param, AUTOFS_DEV_IOCTL_SIZE))
702 err = -EFAULT;
703out:
704 free_dev_ioctl(param);
705 return err;
706}
707
708static long autofs_dev_ioctl(struct file *file, uint command, ulong u)
709{
710 int err;
Ian Kente9a7c2f2016-03-15 14:58:25 -0700711
Ian Kent8d7b48e2008-10-15 22:02:54 -0700712 err = _autofs_dev_ioctl(command, (struct autofs_dev_ioctl __user *) u);
713 return (long) err;
714}
715
716#ifdef CONFIG_COMPAT
717static long autofs_dev_ioctl_compat(struct file *file, uint command, ulong u)
718{
719 return (long) autofs_dev_ioctl(file, command, (ulong) compat_ptr(u));
720}
721#else
722#define autofs_dev_ioctl_compat NULL
723#endif
724
725static const struct file_operations _dev_ioctl_fops = {
726 .unlocked_ioctl = autofs_dev_ioctl,
727 .compat_ioctl = autofs_dev_ioctl_compat,
728 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200729 .llseek = noop_llseek,
Ian Kent8d7b48e2008-10-15 22:02:54 -0700730};
731
732static struct miscdevice _autofs_dev_ioctl_misc = {
Kay Sievers578454f2010-05-20 18:07:20 +0200733 .minor = AUTOFS_MINOR,
Ian Kente9a7c2f2016-03-15 14:58:25 -0700734 .name = AUTOFS_DEVICE_NAME,
735 .fops = &_dev_ioctl_fops
Ian Kent8d7b48e2008-10-15 22:02:54 -0700736};
737
Kay Sievers578454f2010-05-20 18:07:20 +0200738MODULE_ALIAS_MISCDEV(AUTOFS_MINOR);
739MODULE_ALIAS("devname:autofs");
740
Ian Kent8d7b48e2008-10-15 22:02:54 -0700741/* Register/deregister misc character device */
Fabian Frederick3ff6db32014-06-04 16:12:21 -0700742int __init autofs_dev_ioctl_init(void)
Ian Kent8d7b48e2008-10-15 22:02:54 -0700743{
744 int r;
745
746 r = misc_register(&_autofs_dev_ioctl_misc);
747 if (r) {
Ian Kent8a78d592016-03-15 14:58:45 -0700748 pr_err("misc_register failed for control device\n");
Ian Kent8d7b48e2008-10-15 22:02:54 -0700749 return r;
750 }
751
752 return 0;
753}
754
755void autofs_dev_ioctl_exit(void)
756{
757 misc_deregister(&_autofs_dev_ioctl_misc);
Ian Kent8d7b48e2008-10-15 22:02:54 -0700758}