ioctl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2017 Red Hat, Inc.
  4. */
  5. #include "fuse_i.h"
  6. #include <linux/uio.h>
  7. #include <linux/compat.h>
  8. #include <linux/fileattr.h>
  9. #include <linux/fsverity.h>
  10. static ssize_t fuse_send_ioctl(struct fuse_mount *fm, struct fuse_args *args,
  11. struct fuse_ioctl_out *outarg)
  12. {
  13. ssize_t ret;
  14. args->out_args[0].size = sizeof(*outarg);
  15. args->out_args[0].value = outarg;
  16. ret = fuse_simple_request(fm, args);
  17. /* Translate ENOSYS, which shouldn't be returned from fs */
  18. if (ret == -ENOSYS)
  19. ret = -ENOTTY;
  20. if (ret >= 0 && outarg->result == -ENOSYS)
  21. outarg->result = -ENOTTY;
  22. return ret;
  23. }
  24. /*
  25. * CUSE servers compiled on 32bit broke on 64bit kernels because the
  26. * ABI was defined to be 'struct iovec' which is different on 32bit
  27. * and 64bit. Fortunately we can determine which structure the server
  28. * used from the size of the reply.
  29. */
  30. static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
  31. size_t transferred, unsigned count,
  32. bool is_compat)
  33. {
  34. #ifdef CONFIG_COMPAT
  35. if (count * sizeof(struct compat_iovec) == transferred) {
  36. struct compat_iovec *ciov = src;
  37. unsigned i;
  38. /*
  39. * With this interface a 32bit server cannot support
  40. * non-compat (i.e. ones coming from 64bit apps) ioctl
  41. * requests
  42. */
  43. if (!is_compat)
  44. return -EINVAL;
  45. for (i = 0; i < count; i++) {
  46. dst[i].iov_base = compat_ptr(ciov[i].iov_base);
  47. dst[i].iov_len = ciov[i].iov_len;
  48. }
  49. return 0;
  50. }
  51. #endif
  52. if (count * sizeof(struct iovec) != transferred)
  53. return -EIO;
  54. memcpy(dst, src, transferred);
  55. return 0;
  56. }
  57. /* Make sure iov_length() won't overflow */
  58. static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov,
  59. size_t count)
  60. {
  61. size_t n;
  62. u32 max = fc->max_pages << PAGE_SHIFT;
  63. for (n = 0; n < count; n++, iov++) {
  64. if (iov->iov_len > (size_t) max)
  65. return -ENOMEM;
  66. max -= iov->iov_len;
  67. }
  68. return 0;
  69. }
  70. static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
  71. void *src, size_t transferred, unsigned count,
  72. bool is_compat)
  73. {
  74. unsigned i;
  75. struct fuse_ioctl_iovec *fiov = src;
  76. if (fc->minor < 16) {
  77. return fuse_copy_ioctl_iovec_old(dst, src, transferred,
  78. count, is_compat);
  79. }
  80. if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
  81. return -EIO;
  82. for (i = 0; i < count; i++) {
  83. /* Did the server supply an inappropriate value? */
  84. if (fiov[i].base != (unsigned long) fiov[i].base ||
  85. fiov[i].len != (unsigned long) fiov[i].len)
  86. return -EIO;
  87. dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
  88. dst[i].iov_len = (size_t) fiov[i].len;
  89. #ifdef CONFIG_COMPAT
  90. if (is_compat &&
  91. (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
  92. (compat_size_t) dst[i].iov_len != fiov[i].len))
  93. return -EIO;
  94. #endif
  95. }
  96. return 0;
  97. }
  98. /* For fs-verity, determine iov lengths from input */
  99. static int fuse_setup_measure_verity(unsigned long arg, struct iovec *iov)
  100. {
  101. __u16 digest_size;
  102. struct fsverity_digest __user *uarg = (void __user *)arg;
  103. if (copy_from_user(&digest_size, &uarg->digest_size, sizeof(digest_size)))
  104. return -EFAULT;
  105. if (digest_size > SIZE_MAX - sizeof(struct fsverity_digest))
  106. return -EINVAL;
  107. iov->iov_len = sizeof(struct fsverity_digest) + digest_size;
  108. return 0;
  109. }
  110. static int fuse_setup_enable_verity(unsigned long arg, struct iovec *iov,
  111. unsigned int *in_iovs)
  112. {
  113. struct fsverity_enable_arg enable;
  114. struct fsverity_enable_arg __user *uarg = (void __user *)arg;
  115. const __u32 max_buffer_len = FUSE_MAX_MAX_PAGES * PAGE_SIZE;
  116. if (copy_from_user(&enable, uarg, sizeof(enable)))
  117. return -EFAULT;
  118. if (enable.salt_size > max_buffer_len || enable.sig_size > max_buffer_len)
  119. return -ENOMEM;
  120. if (enable.salt_size > 0) {
  121. iov++;
  122. (*in_iovs)++;
  123. iov->iov_base = u64_to_user_ptr(enable.salt_ptr);
  124. iov->iov_len = enable.salt_size;
  125. }
  126. if (enable.sig_size > 0) {
  127. iov++;
  128. (*in_iovs)++;
  129. iov->iov_base = u64_to_user_ptr(enable.sig_ptr);
  130. iov->iov_len = enable.sig_size;
  131. }
  132. return 0;
  133. }
  134. /*
  135. * For ioctls, there is no generic way to determine how much memory
  136. * needs to be read and/or written. Furthermore, ioctls are allowed
  137. * to dereference the passed pointer, so the parameter requires deep
  138. * copying but FUSE has no idea whatsoever about what to copy in or
  139. * out.
  140. *
  141. * This is solved by allowing FUSE server to retry ioctl with
  142. * necessary in/out iovecs. Let's assume the ioctl implementation
  143. * needs to read in the following structure.
  144. *
  145. * struct a {
  146. * char *buf;
  147. * size_t buflen;
  148. * }
  149. *
  150. * On the first callout to FUSE server, inarg->in_size and
  151. * inarg->out_size will be NULL; then, the server completes the ioctl
  152. * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
  153. * the actual iov array to
  154. *
  155. * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
  156. *
  157. * which tells FUSE to copy in the requested area and retry the ioctl.
  158. * On the second round, the server has access to the structure and
  159. * from that it can tell what to look for next, so on the invocation,
  160. * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
  161. *
  162. * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
  163. * { .iov_base = a.buf, .iov_len = a.buflen } }
  164. *
  165. * FUSE will copy both struct a and the pointed buffer from the
  166. * process doing the ioctl and retry ioctl with both struct a and the
  167. * buffer.
  168. *
  169. * This time, FUSE server has everything it needs and completes ioctl
  170. * without FUSE_IOCTL_RETRY which finishes the ioctl call.
  171. *
  172. * Copying data out works the same way.
  173. *
  174. * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
  175. * automatically initializes in and out iovs by decoding @cmd with
  176. * _IOC_* macros and the server is not allowed to request RETRY. This
  177. * limits ioctl data transfers to well-formed ioctls and is the forced
  178. * behavior for all FUSE servers.
  179. */
  180. long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
  181. unsigned int flags)
  182. {
  183. struct fuse_file *ff = file->private_data;
  184. struct fuse_mount *fm = ff->fm;
  185. struct fuse_ioctl_in inarg = {
  186. .fh = ff->fh,
  187. .cmd = cmd,
  188. .arg = arg,
  189. .flags = flags
  190. };
  191. struct fuse_ioctl_out outarg;
  192. struct iovec *iov_page = NULL;
  193. struct iovec *in_iov = NULL, *out_iov = NULL;
  194. unsigned int in_iovs = 0, out_iovs = 0, max_pages;
  195. size_t in_size, out_size, c;
  196. ssize_t transferred;
  197. int err, i;
  198. struct iov_iter ii;
  199. struct fuse_args_pages ap = {};
  200. #if BITS_PER_LONG == 32
  201. inarg.flags |= FUSE_IOCTL_32BIT;
  202. #else
  203. if (flags & FUSE_IOCTL_COMPAT) {
  204. inarg.flags |= FUSE_IOCTL_32BIT;
  205. #ifdef CONFIG_X86_X32_ABI
  206. if (in_x32_syscall())
  207. inarg.flags |= FUSE_IOCTL_COMPAT_X32;
  208. #endif
  209. }
  210. #endif
  211. /* assume all the iovs returned by client always fits in a page */
  212. BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
  213. err = -ENOMEM;
  214. ap.pages = fuse_pages_alloc(fm->fc->max_pages, GFP_KERNEL, &ap.descs);
  215. iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
  216. if (!ap.pages || !iov_page)
  217. goto out;
  218. fuse_page_descs_length_init(ap.descs, 0, fm->fc->max_pages);
  219. /*
  220. * If restricted, initialize IO parameters as encoded in @cmd.
  221. * RETRY from server is not allowed.
  222. */
  223. if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
  224. struct iovec *iov = iov_page;
  225. iov->iov_base = (void __user *)arg;
  226. iov->iov_len = _IOC_SIZE(cmd);
  227. if (_IOC_DIR(cmd) & _IOC_WRITE) {
  228. in_iov = iov;
  229. in_iovs = 1;
  230. }
  231. if (_IOC_DIR(cmd) & _IOC_READ) {
  232. out_iov = iov;
  233. out_iovs = 1;
  234. }
  235. err = 0;
  236. switch (cmd) {
  237. case FS_IOC_MEASURE_VERITY:
  238. err = fuse_setup_measure_verity(arg, iov);
  239. break;
  240. case FS_IOC_ENABLE_VERITY:
  241. err = fuse_setup_enable_verity(arg, iov, &in_iovs);
  242. break;
  243. }
  244. if (err)
  245. goto out;
  246. }
  247. retry:
  248. inarg.in_size = in_size = iov_length(in_iov, in_iovs);
  249. inarg.out_size = out_size = iov_length(out_iov, out_iovs);
  250. /*
  251. * Out data can be used either for actual out data or iovs,
  252. * make sure there always is at least one page.
  253. */
  254. out_size = max_t(size_t, out_size, PAGE_SIZE);
  255. max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
  256. /* make sure there are enough buffer pages and init request with them */
  257. err = -ENOMEM;
  258. if (max_pages > fm->fc->max_pages)
  259. goto out;
  260. while (ap.num_pages < max_pages) {
  261. ap.pages[ap.num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
  262. if (!ap.pages[ap.num_pages])
  263. goto out;
  264. ap.num_pages++;
  265. }
  266. /* okay, let's send it to the client */
  267. ap.args.opcode = FUSE_IOCTL;
  268. ap.args.nodeid = ff->nodeid;
  269. ap.args.in_numargs = 1;
  270. ap.args.in_args[0].size = sizeof(inarg);
  271. ap.args.in_args[0].value = &inarg;
  272. if (in_size) {
  273. ap.args.in_numargs++;
  274. ap.args.in_args[1].size = in_size;
  275. ap.args.in_pages = true;
  276. err = -EFAULT;
  277. iov_iter_init(&ii, ITER_SOURCE, in_iov, in_iovs, in_size);
  278. for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
  279. c = copy_page_from_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
  280. if (c != PAGE_SIZE && iov_iter_count(&ii))
  281. goto out;
  282. }
  283. }
  284. ap.args.out_numargs = 2;
  285. ap.args.out_args[1].size = out_size;
  286. ap.args.out_pages = true;
  287. ap.args.out_argvar = true;
  288. transferred = fuse_send_ioctl(fm, &ap.args, &outarg);
  289. err = transferred;
  290. if (transferred < 0)
  291. goto out;
  292. /* did it ask for retry? */
  293. if (outarg.flags & FUSE_IOCTL_RETRY) {
  294. void *vaddr;
  295. /* no retry if in restricted mode */
  296. err = -EIO;
  297. if (!(flags & FUSE_IOCTL_UNRESTRICTED))
  298. goto out;
  299. in_iovs = outarg.in_iovs;
  300. out_iovs = outarg.out_iovs;
  301. /*
  302. * Make sure things are in boundary, separate checks
  303. * are to protect against overflow.
  304. */
  305. err = -ENOMEM;
  306. if (in_iovs > FUSE_IOCTL_MAX_IOV ||
  307. out_iovs > FUSE_IOCTL_MAX_IOV ||
  308. in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
  309. goto out;
  310. vaddr = kmap_local_page(ap.pages[0]);
  311. err = fuse_copy_ioctl_iovec(fm->fc, iov_page, vaddr,
  312. transferred, in_iovs + out_iovs,
  313. (flags & FUSE_IOCTL_COMPAT) != 0);
  314. kunmap_local(vaddr);
  315. if (err)
  316. goto out;
  317. in_iov = iov_page;
  318. out_iov = in_iov + in_iovs;
  319. err = fuse_verify_ioctl_iov(fm->fc, in_iov, in_iovs);
  320. if (err)
  321. goto out;
  322. err = fuse_verify_ioctl_iov(fm->fc, out_iov, out_iovs);
  323. if (err)
  324. goto out;
  325. goto retry;
  326. }
  327. err = -EIO;
  328. if (transferred > inarg.out_size)
  329. goto out;
  330. err = -EFAULT;
  331. iov_iter_init(&ii, ITER_DEST, out_iov, out_iovs, transferred);
  332. for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
  333. c = copy_page_to_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
  334. if (c != PAGE_SIZE && iov_iter_count(&ii))
  335. goto out;
  336. }
  337. err = 0;
  338. out:
  339. free_page((unsigned long) iov_page);
  340. while (ap.num_pages)
  341. __free_page(ap.pages[--ap.num_pages]);
  342. kfree(ap.pages);
  343. return err ? err : outarg.result;
  344. }
  345. EXPORT_SYMBOL_GPL(fuse_do_ioctl);
  346. long fuse_ioctl_common(struct file *file, unsigned int cmd,
  347. unsigned long arg, unsigned int flags)
  348. {
  349. struct inode *inode = file_inode(file);
  350. struct fuse_conn *fc = get_fuse_conn(inode);
  351. if (!fuse_allow_current_process(fc))
  352. return -EACCES;
  353. if (fuse_is_bad(inode))
  354. return -EIO;
  355. return fuse_do_ioctl(file, cmd, arg, flags);
  356. }
  357. long fuse_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  358. {
  359. return fuse_ioctl_common(file, cmd, arg, 0);
  360. }
  361. long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
  362. unsigned long arg)
  363. {
  364. return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
  365. }
  366. static int fuse_priv_ioctl(struct inode *inode, struct fuse_file *ff,
  367. unsigned int cmd, void *ptr, size_t size)
  368. {
  369. struct fuse_mount *fm = ff->fm;
  370. struct fuse_ioctl_in inarg;
  371. struct fuse_ioctl_out outarg;
  372. FUSE_ARGS(args);
  373. int err;
  374. memset(&inarg, 0, sizeof(inarg));
  375. inarg.fh = ff->fh;
  376. inarg.cmd = cmd;
  377. #if BITS_PER_LONG == 32
  378. inarg.flags |= FUSE_IOCTL_32BIT;
  379. #endif
  380. if (S_ISDIR(inode->i_mode))
  381. inarg.flags |= FUSE_IOCTL_DIR;
  382. if (_IOC_DIR(cmd) & _IOC_READ)
  383. inarg.out_size = size;
  384. if (_IOC_DIR(cmd) & _IOC_WRITE)
  385. inarg.in_size = size;
  386. args.opcode = FUSE_IOCTL;
  387. args.nodeid = ff->nodeid;
  388. args.in_numargs = 2;
  389. args.in_args[0].size = sizeof(inarg);
  390. args.in_args[0].value = &inarg;
  391. args.in_args[1].size = inarg.in_size;
  392. args.in_args[1].value = ptr;
  393. args.out_numargs = 2;
  394. args.out_args[1].size = inarg.out_size;
  395. args.out_args[1].value = ptr;
  396. err = fuse_send_ioctl(fm, &args, &outarg);
  397. if (!err) {
  398. if (outarg.result < 0)
  399. err = outarg.result;
  400. else if (outarg.flags & FUSE_IOCTL_RETRY)
  401. err = -EIO;
  402. }
  403. return err;
  404. }
  405. static struct fuse_file *fuse_priv_ioctl_prepare(struct inode *inode)
  406. {
  407. struct fuse_mount *fm = get_fuse_mount(inode);
  408. bool isdir = S_ISDIR(inode->i_mode);
  409. if (!fuse_allow_current_process(fm->fc))
  410. return ERR_PTR(-EACCES);
  411. if (fuse_is_bad(inode))
  412. return ERR_PTR(-EIO);
  413. if (!S_ISREG(inode->i_mode) && !isdir)
  414. return ERR_PTR(-ENOTTY);
  415. return fuse_file_open(fm, get_node_id(inode), O_RDONLY, isdir);
  416. }
  417. static void fuse_priv_ioctl_cleanup(struct inode *inode, struct fuse_file *ff)
  418. {
  419. fuse_file_release(inode, ff, O_RDONLY, NULL, S_ISDIR(inode->i_mode));
  420. }
  421. int fuse_fileattr_get(struct dentry *dentry, struct fileattr *fa)
  422. {
  423. struct inode *inode = d_inode(dentry);
  424. struct fuse_file *ff;
  425. unsigned int flags;
  426. struct fsxattr xfa;
  427. int err;
  428. ff = fuse_priv_ioctl_prepare(inode);
  429. if (IS_ERR(ff))
  430. return PTR_ERR(ff);
  431. if (fa->flags_valid) {
  432. err = fuse_priv_ioctl(inode, ff, FS_IOC_GETFLAGS,
  433. &flags, sizeof(flags));
  434. if (err)
  435. goto cleanup;
  436. fileattr_fill_flags(fa, flags);
  437. } else {
  438. err = fuse_priv_ioctl(inode, ff, FS_IOC_FSGETXATTR,
  439. &xfa, sizeof(xfa));
  440. if (err)
  441. goto cleanup;
  442. fileattr_fill_xflags(fa, xfa.fsx_xflags);
  443. fa->fsx_extsize = xfa.fsx_extsize;
  444. fa->fsx_nextents = xfa.fsx_nextents;
  445. fa->fsx_projid = xfa.fsx_projid;
  446. fa->fsx_cowextsize = xfa.fsx_cowextsize;
  447. }
  448. cleanup:
  449. fuse_priv_ioctl_cleanup(inode, ff);
  450. return err;
  451. }
  452. int fuse_fileattr_set(struct mnt_idmap *idmap,
  453. struct dentry *dentry, struct fileattr *fa)
  454. {
  455. struct inode *inode = d_inode(dentry);
  456. struct fuse_file *ff;
  457. unsigned int flags = fa->flags;
  458. struct fsxattr xfa;
  459. int err;
  460. ff = fuse_priv_ioctl_prepare(inode);
  461. if (IS_ERR(ff))
  462. return PTR_ERR(ff);
  463. if (fa->flags_valid) {
  464. err = fuse_priv_ioctl(inode, ff, FS_IOC_SETFLAGS,
  465. &flags, sizeof(flags));
  466. if (err)
  467. goto cleanup;
  468. } else {
  469. memset(&xfa, 0, sizeof(xfa));
  470. xfa.fsx_xflags = fa->fsx_xflags;
  471. xfa.fsx_extsize = fa->fsx_extsize;
  472. xfa.fsx_nextents = fa->fsx_nextents;
  473. xfa.fsx_projid = fa->fsx_projid;
  474. xfa.fsx_cowextsize = fa->fsx_cowextsize;
  475. err = fuse_priv_ioctl(inode, ff, FS_IOC_FSSETXATTR,
  476. &xfa, sizeof(xfa));
  477. }
  478. cleanup:
  479. fuse_priv_ioctl_cleanup(inode, ff);
  480. return err;
  481. }