ioctl.c 14 KB

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