dev-ioctl.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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. #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. unsigned int inr = _IOC_NR(cmd);
  96. int err;
  97. err = check_dev_ioctl_version(cmd, param);
  98. if (err) {
  99. pr_warn("invalid device control module version "
  100. "supplied for cmd(0x%08x)\n", cmd);
  101. goto out;
  102. }
  103. if (param->size > AUTOFS_DEV_IOCTL_SIZE) {
  104. err = invalid_str(param->path, param->size - AUTOFS_DEV_IOCTL_SIZE);
  105. if (err) {
  106. pr_warn(
  107. "path string terminator missing for cmd(0x%08x)\n",
  108. cmd);
  109. goto out;
  110. }
  111. /* Setting the per-dentry expire timeout requires a trailing
  112. * path component, ie. no '/', so invert the logic of the
  113. * check_name() return for AUTOFS_DEV_IOCTL_TIMEOUT_CMD.
  114. */
  115. err = check_name(param->path);
  116. if (inr == AUTOFS_DEV_IOCTL_TIMEOUT_CMD)
  117. err = err ? 0 : -EINVAL;
  118. if (err) {
  119. pr_warn("invalid path supplied for cmd(0x%08x)\n",
  120. cmd);
  121. goto out;
  122. }
  123. } else {
  124. if (inr == AUTOFS_DEV_IOCTL_OPENMOUNT_CMD ||
  125. inr == AUTOFS_DEV_IOCTL_REQUESTER_CMD ||
  126. inr == AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD) {
  127. err = -EINVAL;
  128. goto out;
  129. }
  130. }
  131. err = 0;
  132. out:
  133. return err;
  134. }
  135. /* Return autofs dev ioctl version */
  136. static int autofs_dev_ioctl_version(struct file *fp,
  137. struct autofs_sb_info *sbi,
  138. struct autofs_dev_ioctl *param)
  139. {
  140. /* This should have already been set. */
  141. param->ver_major = AUTOFS_DEV_IOCTL_VERSION_MAJOR;
  142. param->ver_minor = AUTOFS_DEV_IOCTL_VERSION_MINOR;
  143. return 0;
  144. }
  145. /* Return autofs module protocol version */
  146. static int autofs_dev_ioctl_protover(struct file *fp,
  147. struct autofs_sb_info *sbi,
  148. struct autofs_dev_ioctl *param)
  149. {
  150. param->protover.version = sbi->version;
  151. return 0;
  152. }
  153. /* Return autofs module protocol sub version */
  154. static int autofs_dev_ioctl_protosubver(struct file *fp,
  155. struct autofs_sb_info *sbi,
  156. struct autofs_dev_ioctl *param)
  157. {
  158. param->protosubver.sub_version = sbi->sub_version;
  159. return 0;
  160. }
  161. /* Find the topmost mount satisfying test() */
  162. static int find_autofs_mount(const char *pathname,
  163. struct path *res,
  164. int test(const struct path *path, void *data),
  165. void *data)
  166. {
  167. struct path path;
  168. int err;
  169. err = kern_path(pathname, LOOKUP_MOUNTPOINT, &path);
  170. if (err)
  171. return err;
  172. err = -ENOENT;
  173. while (path.dentry == path.mnt->mnt_root) {
  174. if (path.dentry->d_sb->s_magic == AUTOFS_SUPER_MAGIC) {
  175. if (test(&path, data)) {
  176. path_get(&path);
  177. *res = path;
  178. err = 0;
  179. break;
  180. }
  181. }
  182. if (!follow_up(&path))
  183. break;
  184. }
  185. path_put(&path);
  186. return err;
  187. }
  188. static int test_by_dev(const struct path *path, void *p)
  189. {
  190. return path->dentry->d_sb->s_dev == *(dev_t *)p;
  191. }
  192. static int test_by_type(const struct path *path, void *p)
  193. {
  194. struct autofs_info *ino = autofs_dentry_ino(path->dentry);
  195. return ino && ino->sbi->type & *(unsigned *)p;
  196. }
  197. /*
  198. * Open a file descriptor on the autofs mount point corresponding
  199. * to the given path and device number (aka. new_encode_dev(sb->s_dev)).
  200. */
  201. static int autofs_dev_ioctl_open_mountpoint(const char *name, dev_t devid)
  202. {
  203. int err, fd;
  204. fd = get_unused_fd_flags(O_CLOEXEC);
  205. if (likely(fd >= 0)) {
  206. struct file *filp;
  207. struct path path;
  208. err = find_autofs_mount(name, &path, test_by_dev, &devid);
  209. if (err)
  210. goto out;
  211. filp = dentry_open(&path, O_RDONLY, current_cred());
  212. path_put(&path);
  213. if (IS_ERR(filp)) {
  214. err = PTR_ERR(filp);
  215. goto out;
  216. }
  217. fd_install(fd, filp);
  218. }
  219. return fd;
  220. out:
  221. put_unused_fd(fd);
  222. return err;
  223. }
  224. /* Open a file descriptor on an autofs mount point */
  225. static int autofs_dev_ioctl_openmount(struct file *fp,
  226. struct autofs_sb_info *sbi,
  227. struct autofs_dev_ioctl *param)
  228. {
  229. const char *path;
  230. dev_t devid;
  231. int err, fd;
  232. /* param->path has been checked in validate_dev_ioctl() */
  233. if (!param->openmount.devid)
  234. return -EINVAL;
  235. param->ioctlfd = -1;
  236. path = param->path;
  237. devid = new_decode_dev(param->openmount.devid);
  238. err = 0;
  239. fd = autofs_dev_ioctl_open_mountpoint(path, devid);
  240. if (unlikely(fd < 0)) {
  241. err = fd;
  242. goto out;
  243. }
  244. param->ioctlfd = fd;
  245. out:
  246. return err;
  247. }
  248. /* Close file descriptor allocated above (user can also use close(2)). */
  249. static int autofs_dev_ioctl_closemount(struct file *fp,
  250. struct autofs_sb_info *sbi,
  251. struct autofs_dev_ioctl *param)
  252. {
  253. return close_fd(param->ioctlfd);
  254. }
  255. /*
  256. * Send "ready" status for an existing wait (either a mount or an expire
  257. * request).
  258. */
  259. static int autofs_dev_ioctl_ready(struct file *fp,
  260. struct autofs_sb_info *sbi,
  261. struct autofs_dev_ioctl *param)
  262. {
  263. autofs_wqt_t token;
  264. token = (autofs_wqt_t) param->ready.token;
  265. return autofs_wait_release(sbi, token, 0);
  266. }
  267. /*
  268. * Send "fail" status for an existing wait (either a mount or an expire
  269. * request).
  270. */
  271. static int autofs_dev_ioctl_fail(struct file *fp,
  272. struct autofs_sb_info *sbi,
  273. struct autofs_dev_ioctl *param)
  274. {
  275. autofs_wqt_t token;
  276. int status;
  277. token = (autofs_wqt_t) param->fail.token;
  278. status = param->fail.status < 0 ? param->fail.status : -ENOENT;
  279. return autofs_wait_release(sbi, token, status);
  280. }
  281. /*
  282. * Set the pipe fd for kernel communication to the daemon.
  283. *
  284. * Normally this is set at mount using an option but if we
  285. * are reconnecting to a busy mount then we need to use this
  286. * to tell the autofs mount about the new kernel pipe fd. In
  287. * order to protect mounts against incorrectly setting the
  288. * pipefd we also require that the autofs mount be catatonic.
  289. *
  290. * This also sets the process group id used to identify the
  291. * controlling process (eg. the owning automount(8) daemon).
  292. */
  293. static int autofs_dev_ioctl_setpipefd(struct file *fp,
  294. struct autofs_sb_info *sbi,
  295. struct autofs_dev_ioctl *param)
  296. {
  297. int pipefd;
  298. int err = 0;
  299. struct pid *new_pid = NULL;
  300. if (param->setpipefd.pipefd == -1)
  301. return -EINVAL;
  302. pipefd = param->setpipefd.pipefd;
  303. mutex_lock(&sbi->wq_mutex);
  304. if (!(sbi->flags & AUTOFS_SBI_CATATONIC)) {
  305. mutex_unlock(&sbi->wq_mutex);
  306. return -EBUSY;
  307. } else {
  308. struct file *pipe;
  309. new_pid = get_task_pid(current, PIDTYPE_PGID);
  310. if (ns_of_pid(new_pid) != ns_of_pid(sbi->oz_pgrp)) {
  311. pr_warn("not allowed to change PID namespace\n");
  312. err = -EINVAL;
  313. goto out;
  314. }
  315. pipe = fget(pipefd);
  316. if (!pipe) {
  317. err = -EBADF;
  318. goto out;
  319. }
  320. if (autofs_prepare_pipe(pipe) < 0) {
  321. err = -EPIPE;
  322. fput(pipe);
  323. goto out;
  324. }
  325. swap(sbi->oz_pgrp, new_pid);
  326. sbi->pipefd = pipefd;
  327. sbi->pipe = pipe;
  328. sbi->flags &= ~AUTOFS_SBI_CATATONIC;
  329. }
  330. out:
  331. put_pid(new_pid);
  332. mutex_unlock(&sbi->wq_mutex);
  333. return err;
  334. }
  335. /*
  336. * Make the autofs mount point catatonic, no longer responsive to
  337. * mount requests. Also closes the kernel pipe file descriptor.
  338. */
  339. static int autofs_dev_ioctl_catatonic(struct file *fp,
  340. struct autofs_sb_info *sbi,
  341. struct autofs_dev_ioctl *param)
  342. {
  343. autofs_catatonic_mode(sbi);
  344. return 0;
  345. }
  346. /*
  347. * Set the autofs mount expire timeout.
  348. *
  349. * There are two places an expire timeout can be set, in the autofs
  350. * super block info. (this is all that's needed for direct and offset
  351. * mounts because there's a distinct mount corresponding to each of
  352. * these) and per-dentry within within the dentry info. If a per-dentry
  353. * timeout is set it will override the expire timeout set in the parent
  354. * autofs super block info.
  355. *
  356. * If setting the autofs super block expire timeout the autofs_dev_ioctl
  357. * size field will be equal to the autofs_dev_ioctl structure size. If
  358. * setting the per-dentry expire timeout the mount point name is passed
  359. * in the autofs_dev_ioctl path field and the size field updated to
  360. * reflect this.
  361. *
  362. * Setting the autofs mount expire timeout sets the timeout in the super
  363. * block info. struct. Setting the per-dentry timeout does a little more.
  364. * If the timeout is equal to -1 the per-dentry timeout (and flag) is
  365. * cleared which reverts to using the super block timeout, otherwise if
  366. * timeout is 0 the timeout is set to this value and the flag is left
  367. * set which disables expiration for the mount point, lastly the flag
  368. * and the timeout are set enabling the dentry to use this timeout.
  369. */
  370. static int autofs_dev_ioctl_timeout(struct file *fp,
  371. struct autofs_sb_info *sbi,
  372. struct autofs_dev_ioctl *param)
  373. {
  374. unsigned long timeout = param->timeout.timeout;
  375. /* If setting the expire timeout for an individual indirect
  376. * mount point dentry the mount trailing component path is
  377. * placed in param->path and param->size adjusted to account
  378. * for it otherwise param->size it is set to the structure
  379. * size.
  380. */
  381. if (param->size == AUTOFS_DEV_IOCTL_SIZE) {
  382. param->timeout.timeout = sbi->exp_timeout / HZ;
  383. sbi->exp_timeout = timeout * HZ;
  384. } else {
  385. struct dentry *base = fp->f_path.dentry;
  386. struct inode *inode = base->d_inode;
  387. int path_len = param->size - AUTOFS_DEV_IOCTL_SIZE - 1;
  388. struct dentry *dentry;
  389. struct autofs_info *ino;
  390. if (!autofs_type_indirect(sbi->type))
  391. return -EINVAL;
  392. /* An expire timeout greater than the superblock timeout
  393. * could be a problem at shutdown but the super block
  394. * timeout itself can change so all we can really do is
  395. * warn the user.
  396. */
  397. if (timeout >= sbi->exp_timeout)
  398. pr_warn("per-mount expire timeout is greater than "
  399. "the parent autofs mount timeout which could "
  400. "prevent shutdown\n");
  401. inode_lock_shared(inode);
  402. dentry = try_lookup_one_len(param->path, base, path_len);
  403. inode_unlock_shared(inode);
  404. if (IS_ERR_OR_NULL(dentry))
  405. return dentry ? PTR_ERR(dentry) : -ENOENT;
  406. ino = autofs_dentry_ino(dentry);
  407. if (!ino) {
  408. dput(dentry);
  409. return -ENOENT;
  410. }
  411. if (ino->exp_timeout && ino->flags & AUTOFS_INF_EXPIRE_SET)
  412. param->timeout.timeout = ino->exp_timeout / HZ;
  413. else
  414. param->timeout.timeout = sbi->exp_timeout / HZ;
  415. if (timeout == -1) {
  416. /* Revert to using the super block timeout */
  417. ino->flags &= ~AUTOFS_INF_EXPIRE_SET;
  418. ino->exp_timeout = 0;
  419. } else {
  420. /* Set the dentry expire flag and timeout.
  421. *
  422. * If timeout is 0 it will prevent the expire
  423. * of this particular automount.
  424. */
  425. ino->flags |= AUTOFS_INF_EXPIRE_SET;
  426. ino->exp_timeout = timeout * HZ;
  427. }
  428. dput(dentry);
  429. }
  430. return 0;
  431. }
  432. /*
  433. * Return the uid and gid of the last request for the mount
  434. *
  435. * When reconstructing an autofs mount tree with active mounts
  436. * we need to re-connect to mounts that may have used the original
  437. * process uid and gid (or string variations of them) for mount
  438. * lookups within the map entry.
  439. */
  440. static int autofs_dev_ioctl_requester(struct file *fp,
  441. struct autofs_sb_info *sbi,
  442. struct autofs_dev_ioctl *param)
  443. {
  444. struct autofs_info *ino;
  445. struct path path;
  446. dev_t devid;
  447. int err = -ENOENT;
  448. /* param->path has been checked in validate_dev_ioctl() */
  449. devid = sbi->sb->s_dev;
  450. param->requester.uid = param->requester.gid = -1;
  451. err = find_autofs_mount(param->path, &path, test_by_dev, &devid);
  452. if (err)
  453. goto out;
  454. ino = autofs_dentry_ino(path.dentry);
  455. if (ino) {
  456. err = 0;
  457. autofs_expire_wait(&path, 0);
  458. spin_lock(&sbi->fs_lock);
  459. param->requester.uid =
  460. from_kuid_munged(current_user_ns(), ino->uid);
  461. param->requester.gid =
  462. from_kgid_munged(current_user_ns(), ino->gid);
  463. spin_unlock(&sbi->fs_lock);
  464. }
  465. path_put(&path);
  466. out:
  467. return err;
  468. }
  469. /*
  470. * Call repeatedly until it returns -EAGAIN, meaning there's nothing
  471. * more that can be done.
  472. */
  473. static int autofs_dev_ioctl_expire(struct file *fp,
  474. struct autofs_sb_info *sbi,
  475. struct autofs_dev_ioctl *param)
  476. {
  477. struct vfsmount *mnt;
  478. int how;
  479. how = param->expire.how;
  480. mnt = fp->f_path.mnt;
  481. return autofs_do_expire_multi(sbi->sb, mnt, sbi, how);
  482. }
  483. /* Check if autofs mount point is in use */
  484. static int autofs_dev_ioctl_askumount(struct file *fp,
  485. struct autofs_sb_info *sbi,
  486. struct autofs_dev_ioctl *param)
  487. {
  488. param->askumount.may_umount = 0;
  489. if (may_umount(fp->f_path.mnt))
  490. param->askumount.may_umount = 1;
  491. return 0;
  492. }
  493. /*
  494. * Check if the given path is a mountpoint.
  495. *
  496. * If we are supplied with the file descriptor of an autofs
  497. * mount we're looking for a specific mount. In this case
  498. * the path is considered a mountpoint if it is itself a
  499. * mountpoint or contains a mount, such as a multi-mount
  500. * without a root mount. In this case we return 1 if the
  501. * path is a mount point and the super magic of the covering
  502. * mount if there is one or 0 if it isn't a mountpoint.
  503. *
  504. * If we aren't supplied with a file descriptor then we
  505. * lookup the path and check if it is the root of a mount.
  506. * If a type is given we are looking for a particular autofs
  507. * mount and if we don't find a match we return fail. If the
  508. * located path is the root of a mount we return 1 along with
  509. * the super magic of the mount or 0 otherwise.
  510. *
  511. * In both cases the device number (as returned by
  512. * new_encode_dev()) is also returned.
  513. */
  514. static int autofs_dev_ioctl_ismountpoint(struct file *fp,
  515. struct autofs_sb_info *sbi,
  516. struct autofs_dev_ioctl *param)
  517. {
  518. struct path path;
  519. const char *name;
  520. unsigned int type;
  521. unsigned int devid, magic;
  522. int err = -ENOENT;
  523. /* param->path has been checked in validate_dev_ioctl() */
  524. name = param->path;
  525. type = param->ismountpoint.in.type;
  526. param->ismountpoint.out.devid = devid = 0;
  527. param->ismountpoint.out.magic = magic = 0;
  528. if (!fp || param->ioctlfd == -1) {
  529. if (autofs_type_any(type))
  530. err = kern_path(name, LOOKUP_FOLLOW | LOOKUP_MOUNTPOINT,
  531. &path);
  532. else
  533. err = find_autofs_mount(name, &path,
  534. test_by_type, &type);
  535. if (err)
  536. goto out;
  537. devid = new_encode_dev(path.dentry->d_sb->s_dev);
  538. err = 0;
  539. if (path.mnt->mnt_root == path.dentry) {
  540. err = 1;
  541. magic = path.dentry->d_sb->s_magic;
  542. }
  543. } else {
  544. dev_t dev = sbi->sb->s_dev;
  545. err = find_autofs_mount(name, &path, test_by_dev, &dev);
  546. if (err)
  547. goto out;
  548. devid = new_encode_dev(dev);
  549. err = path_has_submounts(&path);
  550. if (follow_down_one(&path))
  551. magic = path.dentry->d_sb->s_magic;
  552. }
  553. param->ismountpoint.out.devid = devid;
  554. param->ismountpoint.out.magic = magic;
  555. path_put(&path);
  556. out:
  557. return err;
  558. }
  559. /*
  560. * Our range of ioctl numbers isn't 0 based so we need to shift
  561. * the array index by _IOC_NR(AUTOFS_CTL_IOC_FIRST) for the table
  562. * lookup.
  563. */
  564. #define cmd_idx(cmd) (cmd - _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST))
  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. if (idx >= ARRAY_SIZE(_ioctls))
  585. return NULL;
  586. idx = array_index_nospec(idx, ARRAY_SIZE(_ioctls));
  587. return _ioctls[idx];
  588. }
  589. /* ioctl dispatcher */
  590. static int _autofs_dev_ioctl(unsigned int command,
  591. struct autofs_dev_ioctl __user *user)
  592. {
  593. struct autofs_dev_ioctl *param;
  594. struct file *fp;
  595. struct autofs_sb_info *sbi;
  596. unsigned int cmd_first, cmd;
  597. ioctl_fn fn = NULL;
  598. int err = 0;
  599. cmd_first = _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST);
  600. cmd = _IOC_NR(command);
  601. if (_IOC_TYPE(command) != _IOC_TYPE(AUTOFS_DEV_IOCTL_IOC_FIRST) ||
  602. cmd - cmd_first > AUTOFS_DEV_IOCTL_IOC_COUNT) {
  603. return -ENOTTY;
  604. }
  605. /* Only root can use ioctls other than AUTOFS_DEV_IOCTL_VERSION_CMD
  606. * and AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD
  607. */
  608. if (cmd != AUTOFS_DEV_IOCTL_VERSION_CMD &&
  609. cmd != AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD &&
  610. !capable(CAP_SYS_ADMIN))
  611. return -EPERM;
  612. /* Copy the parameters into kernel space. */
  613. param = copy_dev_ioctl(user);
  614. if (IS_ERR(param))
  615. return PTR_ERR(param);
  616. err = validate_dev_ioctl(command, param);
  617. if (err)
  618. goto out;
  619. fn = lookup_dev_ioctl(cmd);
  620. if (!fn) {
  621. pr_warn("unknown command 0x%08x\n", command);
  622. err = -ENOTTY;
  623. goto out;
  624. }
  625. fp = NULL;
  626. sbi = NULL;
  627. /*
  628. * For obvious reasons the openmount can't have a file
  629. * descriptor yet. We don't take a reference to the
  630. * file during close to allow for immediate release,
  631. * and the same for retrieving ioctl version.
  632. */
  633. if (cmd != AUTOFS_DEV_IOCTL_VERSION_CMD &&
  634. cmd != AUTOFS_DEV_IOCTL_OPENMOUNT_CMD &&
  635. cmd != AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD) {
  636. struct super_block *sb;
  637. fp = fget(param->ioctlfd);
  638. if (!fp) {
  639. if (cmd == AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD)
  640. goto cont;
  641. err = -EBADF;
  642. goto out;
  643. }
  644. sb = file_inode(fp)->i_sb;
  645. if (sb->s_type != &autofs_fs_type) {
  646. err = -EINVAL;
  647. fput(fp);
  648. goto out;
  649. }
  650. sbi = autofs_sbi(sb);
  651. /*
  652. * Admin needs to be able to set the mount catatonic in
  653. * order to be able to perform the re-open.
  654. */
  655. if (!autofs_oz_mode(sbi) &&
  656. cmd != AUTOFS_DEV_IOCTL_CATATONIC_CMD) {
  657. err = -EACCES;
  658. fput(fp);
  659. goto out;
  660. }
  661. }
  662. cont:
  663. err = fn(fp, sbi, param);
  664. if (fp)
  665. fput(fp);
  666. if (err >= 0 && copy_to_user(user, param, AUTOFS_DEV_IOCTL_SIZE))
  667. err = -EFAULT;
  668. out:
  669. free_dev_ioctl(param);
  670. return err;
  671. }
  672. static long autofs_dev_ioctl(struct file *file, unsigned int command,
  673. unsigned long u)
  674. {
  675. int err;
  676. err = _autofs_dev_ioctl(command, (struct autofs_dev_ioctl __user *) u);
  677. return (long) err;
  678. }
  679. #ifdef CONFIG_COMPAT
  680. static long autofs_dev_ioctl_compat(struct file *file, unsigned int command,
  681. unsigned long u)
  682. {
  683. return autofs_dev_ioctl(file, command, (unsigned long) compat_ptr(u));
  684. }
  685. #else
  686. #define autofs_dev_ioctl_compat NULL
  687. #endif
  688. static const struct file_operations _dev_ioctl_fops = {
  689. .unlocked_ioctl = autofs_dev_ioctl,
  690. .compat_ioctl = autofs_dev_ioctl_compat,
  691. .owner = THIS_MODULE,
  692. .llseek = noop_llseek,
  693. };
  694. static struct miscdevice _autofs_dev_ioctl_misc = {
  695. .minor = AUTOFS_MINOR,
  696. .name = AUTOFS_DEVICE_NAME,
  697. .fops = &_dev_ioctl_fops,
  698. .mode = 0644,
  699. };
  700. MODULE_ALIAS_MISCDEV(AUTOFS_MINOR);
  701. MODULE_ALIAS("devname:autofs");
  702. /* Register/deregister misc character device */
  703. int __init autofs_dev_ioctl_init(void)
  704. {
  705. int r;
  706. r = misc_register(&_autofs_dev_ioctl_misc);
  707. if (r) {
  708. pr_err("misc_register failed for control device\n");
  709. return r;
  710. }
  711. return 0;
  712. }
  713. void autofs_dev_ioctl_exit(void)
  714. {
  715. misc_deregister(&_autofs_dev_ioctl_misc);
  716. }