• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright 2008 Red Hat, Inc. All rights reserved.
4  * Copyright 2008 Ian Kent <raven@themaw.net>
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  * This module implements an interface for routing autofs ioctl control
17  * commands via a miscellaneous device file.
18  *
19  * The alternate interface is needed because we need to be able open
20  * an ioctl file descriptor on an autofs mount that may be covered by
21  * another mount. This situation arises when starting automount(8)
22  * or other user space daemon which uses direct mounts or offset
23  * mounts (used for autofs lazy mount/umount of nested mount trees),
24  * which have been left busy at service shutdown.
25  */
26 
27 typedef int (*ioctl_fn)(struct file *, struct autofs_sb_info *,
28 			struct autofs_dev_ioctl *);
29 
check_name(const char * name)30 static int check_name(const char *name)
31 {
32 	if (!strchr(name, '/'))
33 		return -EINVAL;
34 	return 0;
35 }
36 
37 /*
38  * Check a string doesn't overrun the chunk of
39  * memory we copied from user land.
40  */
invalid_str(char * str,size_t size)41 static 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  * Check that the user compiled against correct version of autofs
50  * misc device code.
51  *
52  * As well as checking the version compatibility this always copies
53  * the kernel interface version out.
54  */
check_dev_ioctl_version(int cmd,struct autofs_dev_ioctl * param)55 static 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 	/* Fill in the kernel version. */
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  * Copy parameter control struct, including a possible path allocated
78  * at the end of the struct.
79  */
80 static struct autofs_dev_ioctl *
copy_dev_ioctl(struct autofs_dev_ioctl __user * in)81 copy_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 
free_dev_ioctl(struct autofs_dev_ioctl * param)101 static inline void free_dev_ioctl(struct autofs_dev_ioctl *param)
102 {
103 	kfree(param);
104 }
105 
106 /*
107  * Check sanity of parameter control fields and if a path is present
108  * check that it is terminated and contains at least one "/".
109  */
validate_dev_ioctl(int cmd,struct autofs_dev_ioctl * param)110 static 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;
148 out:
149 	return err;
150 }
151 
152 /* Return autofs dev ioctl version */
autofs_dev_ioctl_version(struct file * fp,struct autofs_sb_info * sbi,struct autofs_dev_ioctl * param)153 static int autofs_dev_ioctl_version(struct file *fp,
154 				    struct autofs_sb_info *sbi,
155 				    struct autofs_dev_ioctl *param)
156 {
157 	/* This should have already been set. */
158 	param->ver_major = AUTOFS_DEV_IOCTL_VERSION_MAJOR;
159 	param->ver_minor = AUTOFS_DEV_IOCTL_VERSION_MINOR;
160 	return 0;
161 }
162 
163 /* Return autofs module protocol version */
autofs_dev_ioctl_protover(struct file * fp,struct autofs_sb_info * sbi,struct autofs_dev_ioctl * param)164 static 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 /* Return autofs module protocol sub version */
autofs_dev_ioctl_protosubver(struct file * fp,struct autofs_sb_info * sbi,struct autofs_dev_ioctl * param)173 static 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 /* Find the topmost mount satisfying test() */
find_autofs_mount(const char * pathname,struct path * res,int test (const struct path * path,void * data),void * data)182 static 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 
test_by_dev(const struct path * path,void * p)210 static int test_by_dev(const struct path *path, void *p)
211 {
212 	return path->dentry->d_sb->s_dev == *(dev_t *)p;
213 }
214 
test_by_type(const struct path * path,void * p)215 static 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  * Open a file descriptor on the autofs mount point corresponding
224  * to the given path and device number (aka. new_encode_dev(sb->s_dev)).
225  */
autofs_dev_ioctl_open_mountpoint(const char * name,dev_t devid)226 static 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 
251 out:
252 	put_unused_fd(fd);
253 	return err;
254 }
255 
256 /* Open a file descriptor on an autofs mount point */
autofs_dev_ioctl_openmount(struct file * fp,struct autofs_sb_info * sbi,struct autofs_dev_ioctl * param)257 static 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 	/* param->path has been checked in validate_dev_ioctl() */
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;
283 out:
284 	return err;
285 }
286 
287 /* Close file descriptor allocated above (user can also use close(2)). */
autofs_dev_ioctl_closemount(struct file * fp,struct autofs_sb_info * sbi,struct autofs_dev_ioctl * param)288 static 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  * Send "ready" status for an existing wait (either a mount or an expire
297  * request).
298  */
autofs_dev_ioctl_ready(struct file * fp,struct autofs_sb_info * sbi,struct autofs_dev_ioctl * param)299 static 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  * Send "fail" status for an existing wait (either a mount or an expire
311  * request).
312  */
autofs_dev_ioctl_fail(struct file * fp,struct autofs_sb_info * sbi,struct autofs_dev_ioctl * param)313 static 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  * Set the pipe fd for kernel communication to the daemon.
327  *
328  * Normally this is set at mount using an option but if we
329  * are reconnecting to a busy mount then we need to use this
330  * to tell the autofs mount about the new kernel pipe fd. In
331  * order to protect mounts against incorrectly setting the
332  * pipefd we also require that the autofs mount be catatonic.
333  *
334  * This also sets the process group id used to identify the
335  * controlling process (eg. the owning automount(8) daemon).
336  */
autofs_dev_ioctl_setpipefd(struct file * fp,struct autofs_sb_info * sbi,struct autofs_dev_ioctl * param)337 static 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 	}
380 out:
381 	put_pid(new_pid);
382 	mutex_unlock(&sbi->wq_mutex);
383 	return err;
384 }
385 
386 /*
387  * Make the autofs mount point catatonic, no longer responsive to
388  * mount requests. Also closes the kernel pipe file descriptor.
389  */
autofs_dev_ioctl_catatonic(struct file * fp,struct autofs_sb_info * sbi,struct autofs_dev_ioctl * param)390 static 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 /* Set the autofs mount timeout */
autofs_dev_ioctl_timeout(struct file * fp,struct autofs_sb_info * sbi,struct autofs_dev_ioctl * param)399 static 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  * Return the uid and gid of the last request for the mount
413  *
414  * When reconstructing an autofs mount tree with active mounts
415  * we need to re-connect to mounts that may have used the original
416  * process uid and gid (or string variations of them) for mount
417  * lookups within the map entry.
418  */
autofs_dev_ioctl_requester(struct file * fp,struct autofs_sb_info * sbi,struct autofs_dev_ioctl * param)419 static 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 	/* param->path has been checked in validate_dev_ioctl() */
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);
450 out:
451 	return err;
452 }
453 
454 /*
455  * Call repeatedly until it returns -EAGAIN, meaning there's nothing
456  * more that can be done.
457  */
autofs_dev_ioctl_expire(struct file * fp,struct autofs_sb_info * sbi,struct autofs_dev_ioctl * param)458 static 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 /* Check if autofs mount point is in use */
autofs_dev_ioctl_askumount(struct file * fp,struct autofs_sb_info * sbi,struct autofs_dev_ioctl * param)472 static 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  * Check if the given path is a mountpoint.
484  *
485  * If we are supplied with the file descriptor of an autofs
486  * mount we're looking for a specific mount. In this case
487  * the path is considered a mountpoint if it is itself a
488  * mountpoint or contains a mount, such as a multi-mount
489  * without a root mount. In this case we return 1 if the
490  * path is a mount point and the super magic of the covering
491  * mount if there is one or 0 if it isn't a mountpoint.
492  *
493  * If we aren't supplied with a file descriptor then we
494  * lookup the path and check if it is the root of a mount.
495  * If a type is given we are looking for a particular autofs
496  * mount and if we don't find a match we return fail. If the
497  * located path is the root of a mount we return 1 along with
498  * the super magic of the mount or 0 otherwise.
499  *
500  * In both cases the device number (as returned by
501  * new_encode_dev()) is also returned.
502  */
autofs_dev_ioctl_ismountpoint(struct file * fp,struct autofs_sb_info * sbi,struct autofs_dev_ioctl * param)503 static 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 	/* param->path has been checked in validate_dev_ioctl() */
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);
554 out:
555 	return err;
556 }
557 
558 /*
559  * Our range of ioctl numbers isn't 0 based so we need to shift
560  * the array index by _IOC_NR(AUTOFS_CTL_IOC_FIRST) for the table
561  * lookup.
562  */
563 #define cmd_idx(cmd)	(cmd - _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST))
564 
lookup_dev_ioctl(unsigned int cmd)565 static 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 /* ioctl dispatcher */
_autofs_dev_ioctl(unsigned int command,struct autofs_dev_ioctl __user * user)592 static 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 	/* Only root can use ioctls other than AUTOFS_DEV_IOCTL_VERSION_CMD
611 	 * and AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD
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 	/* Copy the parameters into kernel space. */
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 	 * For obvious reasons the openmount can't have a file
639 	 * descriptor yet. We don't take a reference to the
640 	 * file during close to allow for immediate release,
641 	 * and the same for retrieving ioctl version.
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 		 * Admin needs to be able to set the mount catatonic in
666 		 * order to be able to perform the re-open.
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 	}
675 cont:
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;
682 out:
683 	free_dev_ioctl(param);
684 	return err;
685 }
686 
autofs_dev_ioctl(struct file * file,unsigned int command,unsigned long u)687 static 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
autofs_dev_ioctl_compat(struct file * file,unsigned int command,unsigned long u)697 static 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 
706 static 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 
713 static 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 
720 MODULE_ALIAS_MISCDEV(AUTOFS_MINOR);
721 MODULE_ALIAS("devname:autofs");
722 
723 /* Register/deregister misc character device */
autofs_dev_ioctl_init(void)724 int __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 
autofs_dev_ioctl_exit(void)737 void autofs_dev_ioctl_exit(void)
738 {
739 	misc_deregister(&_autofs_dev_ioctl_misc);
740 }
741