xattr.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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/mm.h>
  7. #include <linux/slab.h>
  8. #include <linux/namei.h>
  9. #include <linux/io_uring.h>
  10. #include <linux/xattr.h>
  11. #include <uapi/linux/io_uring.h>
  12. #include "../fs/internal.h"
  13. #include "io_uring.h"
  14. #include "xattr.h"
  15. struct io_xattr {
  16. struct file *file;
  17. struct xattr_ctx ctx;
  18. struct filename *filename;
  19. };
  20. void io_xattr_cleanup(struct io_kiocb *req)
  21. {
  22. struct io_xattr *ix = io_kiocb_to_cmd(req, struct io_xattr);
  23. if (ix->filename)
  24. putname(ix->filename);
  25. kfree(ix->ctx.kname);
  26. kvfree(ix->ctx.kvalue);
  27. }
  28. static void io_xattr_finish(struct io_kiocb *req, int ret)
  29. {
  30. req->flags &= ~REQ_F_NEED_CLEANUP;
  31. io_xattr_cleanup(req);
  32. io_req_set_res(req, ret, 0);
  33. }
  34. static int __io_getxattr_prep(struct io_kiocb *req,
  35. const struct io_uring_sqe *sqe)
  36. {
  37. struct io_xattr *ix = io_kiocb_to_cmd(req, struct io_xattr);
  38. const char __user *name;
  39. int ret;
  40. if (unlikely(req->flags & REQ_F_FIXED_FILE))
  41. return -EBADF;
  42. ix->filename = NULL;
  43. ix->ctx.kvalue = NULL;
  44. name = u64_to_user_ptr(READ_ONCE(sqe->addr));
  45. ix->ctx.cvalue = u64_to_user_ptr(READ_ONCE(sqe->addr2));
  46. ix->ctx.size = READ_ONCE(sqe->len);
  47. ix->ctx.flags = READ_ONCE(sqe->xattr_flags);
  48. if (ix->ctx.flags)
  49. return -EINVAL;
  50. ix->ctx.kname = kmalloc(sizeof(*ix->ctx.kname), GFP_KERNEL);
  51. if (!ix->ctx.kname)
  52. return -ENOMEM;
  53. ret = strncpy_from_user(ix->ctx.kname->name, name,
  54. sizeof(ix->ctx.kname->name));
  55. if (!ret || ret == sizeof(ix->ctx.kname->name))
  56. ret = -ERANGE;
  57. if (ret < 0) {
  58. kfree(ix->ctx.kname);
  59. return ret;
  60. }
  61. req->flags |= REQ_F_NEED_CLEANUP;
  62. return 0;
  63. }
  64. int io_fgetxattr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  65. {
  66. return __io_getxattr_prep(req, sqe);
  67. }
  68. int io_getxattr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  69. {
  70. struct io_xattr *ix = io_kiocb_to_cmd(req, struct io_xattr);
  71. const char __user *path;
  72. int ret;
  73. ret = __io_getxattr_prep(req, sqe);
  74. if (ret)
  75. return ret;
  76. path = u64_to_user_ptr(READ_ONCE(sqe->addr3));
  77. ix->filename = getname_flags(path, LOOKUP_FOLLOW, NULL);
  78. if (IS_ERR(ix->filename)) {
  79. ret = PTR_ERR(ix->filename);
  80. ix->filename = NULL;
  81. }
  82. return ret;
  83. }
  84. int io_fgetxattr(struct io_kiocb *req, unsigned int issue_flags)
  85. {
  86. struct io_xattr *ix = io_kiocb_to_cmd(req, struct io_xattr);
  87. int ret;
  88. if (issue_flags & IO_URING_F_NONBLOCK)
  89. return -EAGAIN;
  90. ret = do_getxattr(mnt_user_ns(req->file->f_path.mnt),
  91. req->file->f_path.dentry,
  92. &ix->ctx);
  93. io_xattr_finish(req, ret);
  94. return IOU_OK;
  95. }
  96. int io_getxattr(struct io_kiocb *req, unsigned int issue_flags)
  97. {
  98. struct io_xattr *ix = io_kiocb_to_cmd(req, struct io_xattr);
  99. unsigned int lookup_flags = LOOKUP_FOLLOW;
  100. struct path path;
  101. int ret;
  102. if (issue_flags & IO_URING_F_NONBLOCK)
  103. return -EAGAIN;
  104. retry:
  105. ret = filename_lookup(AT_FDCWD, ix->filename, lookup_flags, &path, NULL);
  106. if (!ret) {
  107. ret = do_getxattr(mnt_user_ns(path.mnt),
  108. path.dentry,
  109. &ix->ctx);
  110. path_put(&path);
  111. if (retry_estale(ret, lookup_flags)) {
  112. lookup_flags |= LOOKUP_REVAL;
  113. goto retry;
  114. }
  115. }
  116. io_xattr_finish(req, ret);
  117. return IOU_OK;
  118. }
  119. static int __io_setxattr_prep(struct io_kiocb *req,
  120. const struct io_uring_sqe *sqe)
  121. {
  122. struct io_xattr *ix = io_kiocb_to_cmd(req, struct io_xattr);
  123. const char __user *name;
  124. int ret;
  125. if (unlikely(req->flags & REQ_F_FIXED_FILE))
  126. return -EBADF;
  127. ix->filename = NULL;
  128. name = u64_to_user_ptr(READ_ONCE(sqe->addr));
  129. ix->ctx.cvalue = u64_to_user_ptr(READ_ONCE(sqe->addr2));
  130. ix->ctx.kvalue = NULL;
  131. ix->ctx.size = READ_ONCE(sqe->len);
  132. ix->ctx.flags = READ_ONCE(sqe->xattr_flags);
  133. ix->ctx.kname = kmalloc(sizeof(*ix->ctx.kname), GFP_KERNEL);
  134. if (!ix->ctx.kname)
  135. return -ENOMEM;
  136. ret = setxattr_copy(name, &ix->ctx);
  137. if (ret) {
  138. kfree(ix->ctx.kname);
  139. return ret;
  140. }
  141. req->flags |= REQ_F_NEED_CLEANUP;
  142. return 0;
  143. }
  144. int io_setxattr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  145. {
  146. struct io_xattr *ix = io_kiocb_to_cmd(req, struct io_xattr);
  147. const char __user *path;
  148. int ret;
  149. ret = __io_setxattr_prep(req, sqe);
  150. if (ret)
  151. return ret;
  152. path = u64_to_user_ptr(READ_ONCE(sqe->addr3));
  153. ix->filename = getname_flags(path, LOOKUP_FOLLOW, NULL);
  154. if (IS_ERR(ix->filename)) {
  155. ret = PTR_ERR(ix->filename);
  156. ix->filename = NULL;
  157. }
  158. return ret;
  159. }
  160. int io_fsetxattr_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  161. {
  162. return __io_setxattr_prep(req, sqe);
  163. }
  164. static int __io_setxattr(struct io_kiocb *req, unsigned int issue_flags,
  165. const struct path *path)
  166. {
  167. struct io_xattr *ix = io_kiocb_to_cmd(req, struct io_xattr);
  168. int ret;
  169. ret = mnt_want_write(path->mnt);
  170. if (!ret) {
  171. ret = do_setxattr(mnt_user_ns(path->mnt), path->dentry, &ix->ctx);
  172. mnt_drop_write(path->mnt);
  173. }
  174. return ret;
  175. }
  176. int io_fsetxattr(struct io_kiocb *req, unsigned int issue_flags)
  177. {
  178. int ret;
  179. if (issue_flags & IO_URING_F_NONBLOCK)
  180. return -EAGAIN;
  181. ret = __io_setxattr(req, issue_flags, &req->file->f_path);
  182. io_xattr_finish(req, ret);
  183. return IOU_OK;
  184. }
  185. int io_setxattr(struct io_kiocb *req, unsigned int issue_flags)
  186. {
  187. struct io_xattr *ix = io_kiocb_to_cmd(req, struct io_xattr);
  188. unsigned int lookup_flags = LOOKUP_FOLLOW;
  189. struct path path;
  190. int ret;
  191. if (issue_flags & IO_URING_F_NONBLOCK)
  192. return -EAGAIN;
  193. retry:
  194. ret = filename_lookup(AT_FDCWD, ix->filename, lookup_flags, &path, NULL);
  195. if (!ret) {
  196. ret = __io_setxattr(req, issue_flags, &path);
  197. path_put(&path);
  198. if (retry_estale(ret, lookup_flags)) {
  199. lookup_flags |= LOOKUP_REVAL;
  200. goto retry;
  201. }
  202. }
  203. io_xattr_finish(req, ret);
  204. return IOU_OK;
  205. }