dev-ioctl.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2008 Red Hat, Inc. All rights reserved.
  4. * Copyright 2008 Ian Kent <[email protected]>
  5. */
  6. #include <linux/module.h>
  7. #include <linux/miscdevice.h>
  8. #include <linux/compat.h>
  9. #include <linux/fdtable.h>
  10. #include <linux/magic.h>
  11. #include <linux/nospec.h>
  12. #include "autofs_i.h"
  13. /*
  14. * This module implements an interface for routing autofs ioctl control
  15. * commands via a miscellaneous device file.
  16. *
  17. * The alternate interface is needed because we need to be able open
  18. * an ioctl file descriptor on an autofs mount that may be covered by
  19. * another mount. This situation arises when starting automount(8)
  20. * or other user space daemon which uses direct mounts or offset
  21. * mounts (used for autofs lazy mount/umount of nested mount trees),
  22. * which have been left busy at service shutdown.
  23. */
  24. typedef int (*ioctl_fn)(struct file *, struct autofs_sb_info *,
  25. struct autofs_dev_ioctl *);
  26. static int check_name(const char *name)
  27. {
  28. if (!strchr(name, '/'))
  29. return -EINVAL;
  30. return 0;
  31. }
  32. /*
  33. * Check a string doesn't overrun the chunk of
  34. * memory we copied from user land.
  35. */
  36. static int invalid_str(char *str, size_t size)
  37. {
  38. if (memchr(str, 0, size))
  39. return 0;
  40. return -EINVAL;
  41. }
  42. /*
  43. * Check that the user compiled against correct version of autofs
  44. * misc device code.
  45. *
  46. * As well as checking the version compatibility this always copies
  47. * the kernel interface version out.
  48. */
  49. static int check_dev_ioctl_version(int cmd, struct autofs_dev_ioctl *param)
  50. {
  51. int err = 0;
  52. if ((param->ver_major != AUTOFS_DEV_IOCTL_VERSION_MAJOR) ||
  53. (param->ver_minor > AUTOFS_DEV_IOCTL_VERSION_MINOR)) {
  54. pr_warn("ioctl control interface version mismatch: "
  55. "kernel(%u.%u), user(%u.%u), cmd(0x%08x)\n",
  56. AUTOFS_DEV_IOCTL_VERSION_MAJOR,
  57. AUTOFS_DEV_IOCTL_VERSION_MINOR,
  58. param->ver_major, param->ver_minor, cmd);
  59. err = -EINVAL;
  60. }
  61. /* Fill in the kernel version. */
  62. param->ver_major = AUTOFS_DEV_IOCTL_VERSION_MAJOR;
  63. param->ver_minor = AUTOFS_DEV_IOCTL_VERSION_MINOR;
  64. return err;
  65. }
  66. /*
  67. * Copy parameter control struct, including a possible path allocated
  68. * at the end of the struct.
  69. */
  70. static struct autofs_dev_ioctl *
  71. copy_dev_ioctl(struct autofs_dev_ioctl __user *in)
  72. {
  73. struct autofs_dev_ioctl tmp, *res;
  74. if (copy_from_user(&tmp, in, AUTOFS_DEV_IOCTL_SIZE))
  75. return ERR_PTR(-EFAULT);
  76. if (tmp.size < AUTOFS_DEV_IOCTL_SIZE)
  77. return ERR_PTR(-EINVAL);
  78. if (tmp.size > AUTOFS_DEV_IOCTL_SIZE + PATH_MAX)
  79. return ERR_PTR(-ENAMETOOLONG);
  80. res = memdup_user(in, tmp.size);
  81. if (!IS_ERR(res))
  82. res->size = tmp.size;
  83. return res;
  84. }
  85. static inline void free_dev_ioctl(struct autofs_dev_ioctl *param)
  86. {
  87. kfree(param);
  88. }
  89. /*
  90. * Check sanity of parameter control fields and if a path is present
  91. * check that it is terminated and contains at least one "/".
  92. */
  93. static int validate_dev_ioctl(int cmd, struct autofs_dev_ioctl *param)
  94. {
  95. int err;
  96. err = check_dev_ioctl_version(cmd, param);
  97. if (err) {
  98. pr_warn("invalid device control module version "
  99. "supplied for cmd(0x%08x)\n", cmd);
  100. goto out;
  101. }
  102. if (param->size > AUTOFS_DEV_IOCTL_SIZE) {
  103. err = invalid_str(param->path, param->size - AUTOFS_DEV_IOCTL_SIZE);
  104. if (err) {
  105. pr_warn(
  106. "path string terminator missing for cmd(0x%08x)\n",
  107. cmd);
  108. goto out;
  109. }
  110. err = check_name(param->path);
  111. if (err) {
  112. pr_warn("invalid path supplied for cmd(0x%08x)\n",
  113. cmd);
  114. goto out;
  115. }
  116. } else {
  117. unsigned int inr = _IOC_NR(cmd);
  118. if (inr == AUTOFS_DEV_IOCTL_OPENMOUNT_CMD ||
  119. inr == AUTOFS_DEV_IOCTL_REQUESTER_CMD ||
  120. inr == AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD) {
  121. err = -EINVAL;
  122. goto out;
  123. }
  124. }
  125. err = 0;
  126. out:
  127. return err;
  128. }
  129. /* Return autofs dev ioctl version */
  130. static int autofs_dev_ioctl_version(struct file *fp,
  131. struct autofs_sb_info *sbi,
  132. struct autofs_dev_ioctl *param)
  133. {
  134. /* This should have already been set. */
  135. param->ver_major = AUTOFS_DEV_IOCTL_VERSION_MAJOR;
  136. param->ver_minor = AUTOFS_DEV_IOCTL_VERSION_MINOR;
  137. return 0;
  138. }
  139. /* Return autofs module protocol version */
  140. static int autofs_dev_ioctl_protover(struct file *fp,
  141. struct autofs_sb_info *sbi,
  142. struct autofs_dev_ioctl *param)
  143. {
  144. param->protover.version = sbi->version;
  145. return 0;
  146. }
  147. /* Return autofs module protocol sub version */
  148. static int autofs_dev_ioctl_protosubver(struct file *fp,
  149. struct autofs_sb_info *sbi,
  150. struct autofs_dev_ioctl *param)
  151. {
  152. param->protosubver.sub_version = sbi->sub_version;
  153. return 0;
  154. }
  155. /* Find the topmost mount satisfying test() */
  156. static int find_autofs_mount(const char *pathname,
  157. struct path *res,
  158. int test(const struct path *path, void *data),
  159. void *data)
  160. {
  161. struct path path;
  162. int err;
  163. err = kern_path(pathname, LOOKUP_MOUNTPOINT, &path);
  164. if (err)
  165. return err;
  166. err = -ENOENT;
  167. while (path.dentry == path.mnt->mnt_root) {
  168. if (path.dentry->d_sb->s_magic == AUTOFS_SUPER_MAGIC) {
  169. if (test(&path, data)) {
  170. path_get(&path);
  171. *res = path;
  172. err = 0;
  173. break;
  174. }
  175. }
  176. if (!follow_up(&path))
  177. break;
  178. }
  179. path_put(&path);
  180. return err;
  181. }
  182. static int test_by_dev(const struct path *path, void *p)
  183. {
  184. return path->dentry->d_sb->s_dev == *(dev_t *)p;
  185. }
  186. static int test_by_type(const struct path *path, void *p)
  187. {
  188. struct autofs_info *ino = autofs_dentry_ino(path->dentry);
  189. return ino && ino->sbi->type & *(unsigned *)p;
  190. }
  191. /*
  192. * Open a file descriptor on the autofs mount point corresponding
  193. * to the given path and device number (aka. new_encode_dev(sb->s_dev)).
  194. */
  195. static int autofs_dev_ioctl_open_mountpoint(const char *name, dev_t devid)
  196. {
  197. int err, fd;
  198. fd = get_unused_fd_flags(O_CLOEXEC);
  199. if (likely(fd >= 0)) {
  200. struct file *filp;
  201. struct path path;
  202. err = find_autofs_mount(name, &path, test_by_dev, &devid);
  203. if (err)
  204. goto out;
  205. filp = dentry_open(&path, O_RDONLY, current_cred());
  206. path_put(&path);
  207. if (IS_ERR(filp)) {
  208. err = PTR_ERR(filp);
  209. goto out;
  210. }
  211. fd_install(fd, filp);
  212. }
  213. return fd;
  214. out:
  215. put_unused_fd(fd);
  216. return err;
  217. }
  218. /* Open a file descriptor on an autofs mount point */
  219. static int autofs_dev_ioctl_openmount(struct file *fp,
  220. struct autofs_sb_info *sbi,
  221. struct autofs_dev_ioctl *param)
  222. {
  223. const char *path;
  224. dev_t devid;
  225. int err, fd;
  226. /* param->path has been checked in validate_dev_ioctl() */
  227. if (!param->openmount.devid)
  228. return -EINVAL;
  229. param->ioctlfd = -1;
  230. path = param->path;
  231. devid = new_decode_dev(param->openmount.devid);
  232. err = 0;
  233. fd = autofs_dev_ioctl_open_mountpoint(path, devid);
  234. if (unlikely(fd < 0)) {
  235. err = fd;
  236. goto out;
  237. }
  238. param->ioctlfd = fd;
  239. out:
  240. return err;
  241. }
  242. /* Close file descriptor allocated above (user can also use close(2)). */
  243. static int autofs_dev_ioctl_closemount(struct file *fp,
  244. struct autofs_sb_info *sbi,
  245. struct autofs_dev_ioctl *param)
  246. {
  247. return close_fd(param->ioctlfd);
  248. }
  249. /*
  250. * Send "ready" status for an existing wait (either a mount or an expire
  251. * request).
  252. */
  253. static int autofs_dev_ioctl_ready(struct file *fp,
  254. struct autofs_sb_info *sbi,
  255. struct autofs_dev_ioctl *param)
  256. {
  257. autofs_wqt_t token;
  258. token = (autofs_wqt_t) param->ready.token;
  259. return autofs_wait_release(sbi, token, 0);
  260. }
  261. /*
  262. * Send "fail" status for an existing wait (either a mount or an expire
  263. * request).
  264. */
  265. static int autofs_dev_ioctl_fail(struct file *fp,
  266. struct autofs_sb_info *sbi,
  267. struct autofs_dev_ioctl *param)
  268. {
  269. autofs_wqt_t token;
  270. int status;
  271. token = (autofs_wqt_t) param->fail.token;
  272. status = param->fail.status < 0 ? param->fail.status : -ENOENT;
  273. return autofs_wait_release(sbi, token, status);
  274. }
  275. /*
  276. * Set the pipe fd for kernel communication to the daemon.
  277. *
  278. * Normally this is set at mount using an option but if we
  279. * are reconnecting to a busy mount then we need to use this
  280. * to tell the autofs mount about the new kernel pipe fd. In
  281. * order to protect mounts against incorrectly setting the
  282. * pipefd we also require that the autofs mount be catatonic.
  283. *
  284. * This also sets the process group id used to identify the
  285. * controlling process (eg. the owning automount(8) daemon).
  286. */
  287. static int autofs_dev_ioctl_setpipefd(struct file *fp,
  288. struct autofs_sb_info *sbi,
  289. struct autofs_dev_ioctl *param)
  290. {
  291. int pipefd;
  292. int err = 0;
  293. struct pid *new_pid = NULL;
  294. if (param->setpipefd.pipefd == -1)
  295. return -EINVAL;
  296. pipefd = param->setpipefd.pipefd;
  297. mutex_lock(&sbi->wq_mutex);
  298. if (!(sbi->flags & AUTOFS_SBI_CATATONIC)) {
  299. mutex_unlock(&sbi->wq_mutex);
  300. return -EBUSY;
  301. } else {
  302. struct file *pipe;
  303. new_pid = get_task_pid(current, PIDTYPE_PGID);
  304. if (ns_of_pid(new_pid) != ns_of_pid(sbi->oz_pgrp)) {
  305. pr_warn("not allowed to change PID namespace\n");
  306. err = -EINVAL;
  307. goto out;
  308. }
  309. pipe = fget(pipefd);
  310. if (!pipe) {
  311. err = -EBADF;
  312. goto out;
  313. }
  314. if (autofs_prepare_pipe(pipe) < 0) {
  315. err = -EPIPE;
  316. fput(pipe);
  317. goto out;
  318. }
  319. swap(sbi->oz_pgrp, new_pid);
  320. sbi->pipefd = pipefd;
  321. sbi->pipe = pipe;
  322. sbi->flags &= ~AUTOFS_SBI_CATATONIC;
  323. }
  324. out:
  325. put_pid(new_pid);
  326. mutex_unlock(&sbi->wq_mutex);
  327. return err;
  328. }
  329. /*
  330. * Make the autofs mount point catatonic, no longer responsive to
  331. * mount requests. Also closes the kernel pipe file descriptor.
  332. */
  333. static int autofs_dev_ioctl_catatonic(struct file *fp,
  334. struct autofs_sb_info *sbi,
  335. struct autofs_dev_ioctl *param)
  336. {
  337. autofs_catatonic_mode(sbi);
  338. return 0;
  339. }
  340. /* Set the autofs mount timeout */
  341. static int autofs_dev_ioctl_timeout(struct file *fp,
  342. struct autofs_sb_info *sbi,
  343. struct autofs_dev_ioctl *param)
  344. {
  345. unsigned long timeout;
  346. timeout = param->timeout.timeout;
  347. param->timeout.timeout = sbi->exp_timeout / HZ;
  348. sbi->exp_timeout = timeout * HZ;
  349. return 0;
  350. }
  351. /*
  352. * Return the uid and gid of the last request for the mount
  353. *
  354. * When reconstructing an autofs mount tree with active mounts
  355. * we need to re-connect to mounts that may have used the original
  356. * process uid and gid (or string variations of them) for mount
  357. * lookups within the map entry.
  358. */
  359. static int autofs_dev_ioctl_requester(struct file *fp,
  360. struct autofs_sb_info *sbi,
  361. struct autofs_dev_ioctl *param)
  362. {
  363. struct autofs_info *ino;
  364. struct path path;
  365. dev_t devid;
  366. int err = -ENOENT;
  367. /* param->path has been checked in validate_dev_ioctl() */
  368. devid = sbi->sb->s_dev;
  369. param->requester.uid = param->requester.gid = -1;
  370. err = find_autofs_mount(param->path, &path, test_by_dev, &devid);
  371. if (err)
  372. goto out;
  373. ino = autofs_dentry_ino(path.dentry);
  374. if (ino) {
  375. err = 0;
  376. autofs_expire_wait(&path, 0);
  377. spin_lock(&sbi->fs_lock);
  378. param->requester.uid =
  379. from_kuid_munged(current_user_ns(), ino->uid);
  380. param->requester.gid =
  381. from_kgid_munged(current_user_ns(), ino->gid);
  382. spin_unlock(&sbi->fs_lock);
  383. }
  384. path_put(&path);
  385. out:
  386. return err;
  387. }
  388. /*
  389. * Call repeatedly until it returns -EAGAIN, meaning there's nothing
  390. * more that can be done.
  391. */
  392. static int autofs_dev_ioctl_expire(struct file *fp,
  393. struct autofs_sb_info *sbi,
  394. struct autofs_dev_ioctl *param)
  395. {
  396. struct vfsmount *mnt;
  397. int how;
  398. how = param->expire.how;
  399. mnt = fp->f_path.mnt;
  400. return autofs_do_expire_multi(sbi->sb, mnt, sbi, how);
  401. }
  402. /* Check if autofs mount point is in use */
  403. static int autofs_dev_ioctl_askumount(struct file *fp,
  404. struct autofs_sb_info *sbi,
  405. struct autofs_dev_ioctl *param)
  406. {
  407. param->askumount.may_umount = 0;
  408. if (may_umount(fp->f_path.mnt))
  409. param->askumount.may_umount = 1;
  410. return 0;
  411. }
  412. /*
  413. * Check if the given path is a mountpoint.
  414. *
  415. * If we are supplied with the file descriptor of an autofs
  416. * mount we're looking for a specific mount. In this case
  417. * the path is considered a mountpoint if it is itself a
  418. * mountpoint or contains a mount, such as a multi-mount
  419. * without a root mount. In this case we return 1 if the
  420. * path is a mount point and the super magic of the covering
  421. * mount if there is one or 0 if it isn't a mountpoint.
  422. *
  423. * If we aren't supplied with a file descriptor then we
  424. * lookup the path and check if it is the root of a mount.
  425. * If a type is given we are looking for a particular autofs
  426. * mount and if we don't find a match we return fail. If the
  427. * located path is the root of a mount we return 1 along with
  428. * the super magic of the mount or 0 otherwise.
  429. *
  430. * In both cases the device number (as returned by
  431. * new_encode_dev()) is also returned.
  432. */
  433. static int autofs_dev_ioctl_ismountpoint(struct file *fp,
  434. struct autofs_sb_info *sbi,
  435. struct autofs_dev_ioctl *param)
  436. {
  437. struct path path;
  438. const char *name;
  439. unsigned int type;
  440. unsigned int devid, magic;
  441. int err = -ENOENT;
  442. /* param->path has been checked in validate_dev_ioctl() */
  443. name = param->path;
  444. type = param->ismountpoint.in.type;
  445. param->ismountpoint.out.devid = devid = 0;
  446. param->ismountpoint.out.magic = magic = 0;
  447. if (!fp || param->ioctlfd == -1) {
  448. if (autofs_type_any(type))
  449. err = kern_path(name, LOOKUP_FOLLOW | LOOKUP_MOUNTPOINT,
  450. &path);
  451. else
  452. err = find_autofs_mount(name, &path,
  453. test_by_type, &type);
  454. if (err)
  455. goto out;
  456. devid = new_encode_dev(path.dentry->d_sb->s_dev);
  457. err = 0;
  458. if (path.mnt->mnt_root == path.dentry) {
  459. err = 1;
  460. magic = path.dentry->d_sb->s_magic;
  461. }
  462. } else {
  463. dev_t dev = sbi->sb->s_dev;
  464. err = find_autofs_mount(name, &path, test_by_dev, &dev);
  465. if (err)
  466. goto out;
  467. devid = new_encode_dev(dev);
  468. err = path_has_submounts(&path);
  469. if (follow_down_one(&path))
  470. magic = path.dentry->d_sb->s_magic;
  471. }
  472. param->ismountpoint.out.devid = devid;
  473. param->ismountpoint.out.magic = magic;
  474. path_put(&path);
  475. out:
  476. return err;
  477. }
  478. /*
  479. * Our range of ioctl numbers isn't 0 based so we need to shift
  480. * the array index by _IOC_NR(AUTOFS_CTL_IOC_FIRST) for the table
  481. * lookup.
  482. */
  483. #define cmd_idx(cmd) (cmd - _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST))
  484. static ioctl_fn lookup_dev_ioctl(unsigned int cmd)
  485. {
  486. static const ioctl_fn _ioctls[] = {
  487. autofs_dev_ioctl_version,
  488. autofs_dev_ioctl_protover,
  489. autofs_dev_ioctl_protosubver,
  490. autofs_dev_ioctl_openmount,
  491. autofs_dev_ioctl_closemount,
  492. autofs_dev_ioctl_ready,
  493. autofs_dev_ioctl_fail,
  494. autofs_dev_ioctl_setpipefd,
  495. autofs_dev_ioctl_catatonic,
  496. autofs_dev_ioctl_timeout,
  497. autofs_dev_ioctl_requester,
  498. autofs_dev_ioctl_expire,
  499. autofs_dev_ioctl_askumount,
  500. autofs_dev_ioctl_ismountpoint,
  501. };
  502. unsigned int idx = cmd_idx(cmd);
  503. if (idx >= ARRAY_SIZE(_ioctls))
  504. return NULL;
  505. idx = array_index_nospec(idx, ARRAY_SIZE(_ioctls));
  506. return _ioctls[idx];
  507. }
  508. /* ioctl dispatcher */
  509. static int _autofs_dev_ioctl(unsigned int command,
  510. struct autofs_dev_ioctl __user *user)
  511. {
  512. struct autofs_dev_ioctl *param;
  513. struct file *fp;
  514. struct autofs_sb_info *sbi;
  515. unsigned int cmd_first, cmd;
  516. ioctl_fn fn = NULL;
  517. int err = 0;
  518. cmd_first = _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST);
  519. cmd = _IOC_NR(command);
  520. if (_IOC_TYPE(command) != _IOC_TYPE(AUTOFS_DEV_IOCTL_IOC_FIRST) ||
  521. cmd - cmd_first > AUTOFS_DEV_IOCTL_IOC_COUNT) {
  522. return -ENOTTY;
  523. }
  524. /* Only root can use ioctls other than AUTOFS_DEV_IOCTL_VERSION_CMD
  525. * and AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD
  526. */
  527. if (cmd != AUTOFS_DEV_IOCTL_VERSION_CMD &&
  528. cmd != AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD &&
  529. !capable(CAP_SYS_ADMIN))
  530. return -EPERM;
  531. /* Copy the parameters into kernel space. */
  532. param = copy_dev_ioctl(user);
  533. if (IS_ERR(param))
  534. return PTR_ERR(param);
  535. err = validate_dev_ioctl(command, param);
  536. if (err)
  537. goto out;
  538. fn = lookup_dev_ioctl(cmd);
  539. if (!fn) {
  540. pr_warn("unknown command 0x%08x\n", command);
  541. err = -ENOTTY;
  542. goto out;
  543. }
  544. fp = NULL;
  545. sbi = NULL;
  546. /*
  547. * For obvious reasons the openmount can't have a file
  548. * descriptor yet. We don't take a reference to the
  549. * file during close to allow for immediate release,
  550. * and the same for retrieving ioctl version.
  551. */
  552. if (cmd != AUTOFS_DEV_IOCTL_VERSION_CMD &&
  553. cmd != AUTOFS_DEV_IOCTL_OPENMOUNT_CMD &&
  554. cmd != AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD) {
  555. struct super_block *sb;
  556. fp = fget(param->ioctlfd);
  557. if (!fp) {
  558. if (cmd == AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD)
  559. goto cont;
  560. err = -EBADF;
  561. goto out;
  562. }
  563. sb = file_inode(fp)->i_sb;
  564. if (sb->s_type != &autofs_fs_type) {
  565. err = -EINVAL;
  566. fput(fp);
  567. goto out;
  568. }
  569. sbi = autofs_sbi(sb);
  570. /*
  571. * Admin needs to be able to set the mount catatonic in
  572. * order to be able to perform the re-open.
  573. */
  574. if (!autofs_oz_mode(sbi) &&
  575. cmd != AUTOFS_DEV_IOCTL_CATATONIC_CMD) {
  576. err = -EACCES;
  577. fput(fp);
  578. goto out;
  579. }
  580. }
  581. cont:
  582. err = fn(fp, sbi, param);
  583. if (fp)
  584. fput(fp);
  585. if (err >= 0 && copy_to_user(user, param, AUTOFS_DEV_IOCTL_SIZE))
  586. err = -EFAULT;
  587. out:
  588. free_dev_ioctl(param);
  589. return err;
  590. }
  591. static long autofs_dev_ioctl(struct file *file, unsigned int command,
  592. unsigned long u)
  593. {
  594. int err;
  595. err = _autofs_dev_ioctl(command, (struct autofs_dev_ioctl __user *) u);
  596. return (long) err;
  597. }
  598. #ifdef CONFIG_COMPAT
  599. static long autofs_dev_ioctl_compat(struct file *file, unsigned int command,
  600. unsigned long u)
  601. {
  602. return autofs_dev_ioctl(file, command, (unsigned long) compat_ptr(u));
  603. }
  604. #else
  605. #define autofs_dev_ioctl_compat NULL
  606. #endif
  607. static const struct file_operations _dev_ioctl_fops = {
  608. .unlocked_ioctl = autofs_dev_ioctl,
  609. .compat_ioctl = autofs_dev_ioctl_compat,
  610. .owner = THIS_MODULE,
  611. .llseek = noop_llseek,
  612. };
  613. static struct miscdevice _autofs_dev_ioctl_misc = {
  614. .minor = AUTOFS_MINOR,
  615. .name = AUTOFS_DEVICE_NAME,
  616. .fops = &_dev_ioctl_fops,
  617. .mode = 0644,
  618. };
  619. MODULE_ALIAS_MISCDEV(AUTOFS_MINOR);
  620. MODULE_ALIAS("devname:autofs");
  621. /* Register/deregister misc character device */
  622. int __init autofs_dev_ioctl_init(void)
  623. {
  624. int r;
  625. r = misc_register(&_autofs_dev_ioctl_misc);
  626. if (r) {
  627. pr_err("misc_register failed for control device\n");
  628. return r;
  629. }
  630. return 0;
  631. }
  632. void autofs_dev_ioctl_exit(void)
  633. {
  634. misc_deregister(&_autofs_dev_ioctl_misc);
  635. }