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