openclose.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/kernel.h>
  3. #include <linux/errno.h>
  4. #include <linux/fs.h>
  5. #include <linux/file.h>
  6. #include <linux/fdtable.h>
  7. #include <linux/fsnotify.h>
  8. #include <linux/namei.h>
  9. #include <linux/pipe_fs_i.h>
  10. #include <linux/watch_queue.h>
  11. #include <linux/io_uring.h>
  12. #include <uapi/linux/io_uring.h>
  13. #include "../fs/internal.h"
  14. #include "filetable.h"
  15. #include "io_uring.h"
  16. #include "rsrc.h"
  17. #include "openclose.h"
  18. struct io_open {
  19. struct file *file;
  20. int dfd;
  21. u32 file_slot;
  22. struct delayed_filename filename;
  23. struct open_how how;
  24. unsigned long nofile;
  25. };
  26. struct io_close {
  27. struct file *file;
  28. int fd;
  29. u32 file_slot;
  30. };
  31. struct io_fixed_install {
  32. struct file *file;
  33. unsigned int o_flags;
  34. };
  35. static bool io_openat_force_async(struct io_open *open)
  36. {
  37. /*
  38. * Don't bother trying for O_TRUNC, O_CREAT, or O_TMPFILE open,
  39. * it'll always -EAGAIN. Note that we test for __O_TMPFILE because
  40. * O_TMPFILE includes O_DIRECTORY, which isn't a flag we need to force
  41. * async for.
  42. */
  43. return open->how.flags & (O_TRUNC | O_CREAT | __O_TMPFILE);
  44. }
  45. static int __io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  46. {
  47. struct io_open *open = io_kiocb_to_cmd(req, struct io_open);
  48. const char __user *fname;
  49. int ret;
  50. if (unlikely(sqe->buf_index))
  51. return -EINVAL;
  52. if (unlikely(req->flags & REQ_F_FIXED_FILE))
  53. return -EBADF;
  54. /* open.how should be already initialised */
  55. if (!(open->how.flags & O_PATH) && force_o_largefile())
  56. open->how.flags |= O_LARGEFILE;
  57. open->dfd = READ_ONCE(sqe->fd);
  58. fname = u64_to_user_ptr(READ_ONCE(sqe->addr));
  59. ret = delayed_getname(&open->filename, fname);
  60. if (unlikely(ret))
  61. return ret;
  62. req->flags |= REQ_F_NEED_CLEANUP;
  63. open->file_slot = READ_ONCE(sqe->file_index);
  64. if (open->file_slot && (open->how.flags & O_CLOEXEC))
  65. return -EINVAL;
  66. open->nofile = rlimit(RLIMIT_NOFILE);
  67. if (io_openat_force_async(open))
  68. req->flags |= REQ_F_FORCE_ASYNC;
  69. return 0;
  70. }
  71. void io_openat_bpf_populate(struct io_uring_bpf_ctx *bctx, struct io_kiocb *req)
  72. {
  73. struct io_open *open = io_kiocb_to_cmd(req, struct io_open);
  74. bctx->open.flags = open->how.flags;
  75. bctx->open.mode = open->how.mode;
  76. bctx->open.resolve = open->how.resolve;
  77. }
  78. int io_openat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  79. {
  80. struct io_open *open = io_kiocb_to_cmd(req, struct io_open);
  81. u64 mode = READ_ONCE(sqe->len);
  82. u64 flags = READ_ONCE(sqe->open_flags);
  83. open->how = build_open_how(flags, mode);
  84. return __io_openat_prep(req, sqe);
  85. }
  86. int io_openat2_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  87. {
  88. struct io_open *open = io_kiocb_to_cmd(req, struct io_open);
  89. struct open_how __user *how;
  90. size_t len;
  91. int ret;
  92. how = u64_to_user_ptr(READ_ONCE(sqe->addr2));
  93. len = READ_ONCE(sqe->len);
  94. if (len < OPEN_HOW_SIZE_VER0)
  95. return -EINVAL;
  96. ret = copy_struct_from_user(&open->how, sizeof(open->how), how, len);
  97. if (ret)
  98. return ret;
  99. return __io_openat_prep(req, sqe);
  100. }
  101. int io_openat2(struct io_kiocb *req, unsigned int issue_flags)
  102. {
  103. struct io_open *open = io_kiocb_to_cmd(req, struct io_open);
  104. struct open_flags op;
  105. struct file *file;
  106. bool resolve_nonblock, nonblock_set;
  107. bool fixed = !!open->file_slot;
  108. CLASS(filename_complete_delayed, name)(&open->filename);
  109. int ret;
  110. ret = build_open_flags(&open->how, &op);
  111. if (ret)
  112. goto err;
  113. nonblock_set = op.open_flag & O_NONBLOCK;
  114. resolve_nonblock = open->how.resolve & RESOLVE_CACHED;
  115. if (issue_flags & IO_URING_F_NONBLOCK) {
  116. WARN_ON_ONCE(io_openat_force_async(open));
  117. op.lookup_flags |= LOOKUP_CACHED;
  118. op.open_flag |= O_NONBLOCK;
  119. }
  120. if (!fixed) {
  121. ret = __get_unused_fd_flags(open->how.flags, open->nofile);
  122. if (ret < 0)
  123. goto err;
  124. }
  125. file = do_file_open(open->dfd, name, &op);
  126. if (IS_ERR(file)) {
  127. /*
  128. * We could hang on to this 'fd' on retrying, but seems like
  129. * marginal gain for something that is now known to be a slower
  130. * path. So just put it, and we'll get a new one when we retry.
  131. */
  132. if (!fixed)
  133. put_unused_fd(ret);
  134. ret = PTR_ERR(file);
  135. /* only retry if RESOLVE_CACHED wasn't already set by application */
  136. if (ret == -EAGAIN && !resolve_nonblock &&
  137. (issue_flags & IO_URING_F_NONBLOCK)) {
  138. ret = putname_to_delayed(&open->filename,
  139. no_free_ptr(name));
  140. if (likely(!ret))
  141. return -EAGAIN;
  142. }
  143. goto err;
  144. }
  145. if ((issue_flags & IO_URING_F_NONBLOCK) && !nonblock_set)
  146. file->f_flags &= ~O_NONBLOCK;
  147. if (!fixed)
  148. fd_install(ret, file);
  149. else
  150. ret = io_fixed_fd_install(req, issue_flags, file,
  151. open->file_slot);
  152. err:
  153. req->flags &= ~REQ_F_NEED_CLEANUP;
  154. if (ret < 0)
  155. req_set_fail(req);
  156. io_req_set_res(req, ret, 0);
  157. return IOU_COMPLETE;
  158. }
  159. int io_openat(struct io_kiocb *req, unsigned int issue_flags)
  160. {
  161. return io_openat2(req, issue_flags);
  162. }
  163. void io_open_cleanup(struct io_kiocb *req)
  164. {
  165. struct io_open *open = io_kiocb_to_cmd(req, struct io_open);
  166. dismiss_delayed_filename(&open->filename);
  167. }
  168. int __io_close_fixed(struct io_ring_ctx *ctx, unsigned int issue_flags,
  169. unsigned int offset)
  170. {
  171. int ret;
  172. io_ring_submit_lock(ctx, issue_flags);
  173. ret = io_fixed_fd_remove(ctx, offset);
  174. io_ring_submit_unlock(ctx, issue_flags);
  175. return ret;
  176. }
  177. static inline int io_close_fixed(struct io_kiocb *req, unsigned int issue_flags)
  178. {
  179. struct io_close *close = io_kiocb_to_cmd(req, struct io_close);
  180. return __io_close_fixed(req->ctx, issue_flags, close->file_slot - 1);
  181. }
  182. int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  183. {
  184. struct io_close *close = io_kiocb_to_cmd(req, struct io_close);
  185. if (sqe->off || sqe->addr || sqe->len || sqe->rw_flags || sqe->buf_index)
  186. return -EINVAL;
  187. if (req->flags & REQ_F_FIXED_FILE)
  188. return -EBADF;
  189. close->fd = READ_ONCE(sqe->fd);
  190. close->file_slot = READ_ONCE(sqe->file_index);
  191. if (close->file_slot && close->fd)
  192. return -EINVAL;
  193. return 0;
  194. }
  195. int io_close(struct io_kiocb *req, unsigned int issue_flags)
  196. {
  197. struct files_struct *files = current->files;
  198. struct io_close *close = io_kiocb_to_cmd(req, struct io_close);
  199. struct file *file;
  200. int ret = -EBADF;
  201. if (close->file_slot) {
  202. ret = io_close_fixed(req, issue_flags);
  203. goto err;
  204. }
  205. spin_lock(&files->file_lock);
  206. file = files_lookup_fd_locked(files, close->fd);
  207. if (!file || io_is_uring_fops(file)) {
  208. spin_unlock(&files->file_lock);
  209. goto err;
  210. }
  211. /* if the file has a flush method, be safe and punt to async */
  212. if (file->f_op->flush && (issue_flags & IO_URING_F_NONBLOCK)) {
  213. spin_unlock(&files->file_lock);
  214. return -EAGAIN;
  215. }
  216. file = file_close_fd_locked(files, close->fd);
  217. spin_unlock(&files->file_lock);
  218. if (!file)
  219. goto err;
  220. /* No ->flush() or already async, safely close from here */
  221. ret = filp_close(file, current->files);
  222. err:
  223. if (ret < 0)
  224. req_set_fail(req);
  225. io_req_set_res(req, ret, 0);
  226. return IOU_COMPLETE;
  227. }
  228. int io_install_fixed_fd_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  229. {
  230. struct io_fixed_install *ifi;
  231. unsigned int flags;
  232. if (sqe->off || sqe->addr || sqe->len || sqe->buf_index ||
  233. sqe->splice_fd_in || sqe->addr3)
  234. return -EINVAL;
  235. /* must be a fixed file */
  236. if (!(req->flags & REQ_F_FIXED_FILE))
  237. return -EBADF;
  238. flags = READ_ONCE(sqe->install_fd_flags);
  239. if (flags & ~IORING_FIXED_FD_NO_CLOEXEC)
  240. return -EINVAL;
  241. /* ensure the task's creds are used when installing/receiving fds */
  242. if (req->flags & REQ_F_CREDS)
  243. return -EPERM;
  244. /* default to O_CLOEXEC, disable if IORING_FIXED_FD_NO_CLOEXEC is set */
  245. ifi = io_kiocb_to_cmd(req, struct io_fixed_install);
  246. ifi->o_flags = O_CLOEXEC;
  247. if (flags & IORING_FIXED_FD_NO_CLOEXEC)
  248. ifi->o_flags = 0;
  249. return 0;
  250. }
  251. int io_install_fixed_fd(struct io_kiocb *req, unsigned int issue_flags)
  252. {
  253. struct io_fixed_install *ifi;
  254. int ret;
  255. ifi = io_kiocb_to_cmd(req, struct io_fixed_install);
  256. ret = receive_fd(req->file, NULL, ifi->o_flags);
  257. if (ret < 0)
  258. req_set_fail(req);
  259. io_req_set_res(req, ret, 0);
  260. return IOU_COMPLETE;
  261. }
  262. struct io_pipe {
  263. struct file *file;
  264. int __user *fds;
  265. int flags;
  266. int file_slot;
  267. unsigned long nofile;
  268. };
  269. int io_pipe_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  270. {
  271. struct io_pipe *p = io_kiocb_to_cmd(req, struct io_pipe);
  272. if (sqe->fd || sqe->off || sqe->addr3)
  273. return -EINVAL;
  274. p->fds = u64_to_user_ptr(READ_ONCE(sqe->addr));
  275. p->flags = READ_ONCE(sqe->pipe_flags);
  276. if (p->flags & ~(O_CLOEXEC | O_NONBLOCK | O_DIRECT | O_NOTIFICATION_PIPE))
  277. return -EINVAL;
  278. p->file_slot = READ_ONCE(sqe->file_index);
  279. p->nofile = rlimit(RLIMIT_NOFILE);
  280. return 0;
  281. }
  282. static int io_pipe_fixed(struct io_kiocb *req, struct file **files,
  283. unsigned int issue_flags)
  284. {
  285. struct io_pipe *p = io_kiocb_to_cmd(req, struct io_pipe);
  286. struct io_ring_ctx *ctx = req->ctx;
  287. bool alloc_slot;
  288. int ret, fds[2] = { -1, -1 };
  289. int slot = p->file_slot;
  290. if (p->flags & O_CLOEXEC)
  291. return -EINVAL;
  292. alloc_slot = slot == IORING_FILE_INDEX_ALLOC;
  293. io_ring_submit_lock(ctx, issue_flags);
  294. ret = __io_fixed_fd_install(ctx, files[0], slot);
  295. if (ret < 0)
  296. goto err;
  297. fds[0] = alloc_slot ? ret : slot - 1;
  298. files[0] = NULL;
  299. /*
  300. * If a specific slot is given, next one will be used for
  301. * the write side.
  302. */
  303. if (!alloc_slot)
  304. slot++;
  305. ret = __io_fixed_fd_install(ctx, files[1], slot);
  306. if (ret < 0)
  307. goto err;
  308. fds[1] = alloc_slot ? ret : slot - 1;
  309. files[1] = NULL;
  310. io_ring_submit_unlock(ctx, issue_flags);
  311. if (!copy_to_user(p->fds, fds, sizeof(fds)))
  312. return 0;
  313. ret = -EFAULT;
  314. io_ring_submit_lock(ctx, issue_flags);
  315. err:
  316. if (fds[0] != -1)
  317. io_fixed_fd_remove(ctx, fds[0]);
  318. if (fds[1] != -1)
  319. io_fixed_fd_remove(ctx, fds[1]);
  320. io_ring_submit_unlock(ctx, issue_flags);
  321. return ret;
  322. }
  323. static int io_pipe_fd(struct io_kiocb *req, struct file **files)
  324. {
  325. struct io_pipe *p = io_kiocb_to_cmd(req, struct io_pipe);
  326. int ret, fds[2] = { -1, -1 };
  327. ret = __get_unused_fd_flags(p->flags, p->nofile);
  328. if (ret < 0)
  329. goto err;
  330. fds[0] = ret;
  331. ret = __get_unused_fd_flags(p->flags, p->nofile);
  332. if (ret < 0)
  333. goto err;
  334. fds[1] = ret;
  335. if (!copy_to_user(p->fds, fds, sizeof(fds))) {
  336. fd_install(fds[0], files[0]);
  337. fd_install(fds[1], files[1]);
  338. return 0;
  339. }
  340. ret = -EFAULT;
  341. err:
  342. if (fds[0] != -1)
  343. put_unused_fd(fds[0]);
  344. if (fds[1] != -1)
  345. put_unused_fd(fds[1]);
  346. return ret;
  347. }
  348. int io_pipe(struct io_kiocb *req, unsigned int issue_flags)
  349. {
  350. struct io_pipe *p = io_kiocb_to_cmd(req, struct io_pipe);
  351. struct file *files[2];
  352. int ret;
  353. ret = create_pipe_files(files, p->flags);
  354. if (ret)
  355. return ret;
  356. if (!!p->file_slot)
  357. ret = io_pipe_fixed(req, files, issue_flags);
  358. else
  359. ret = io_pipe_fd(req, files);
  360. io_req_set_res(req, ret, 0);
  361. if (!ret)
  362. return IOU_COMPLETE;
  363. req_set_fail(req);
  364. if (files[0])
  365. fput(files[0]);
  366. if (files[1])
  367. fput(files[1]);
  368. return ret;
  369. }