openat2.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: run various openat(2) tests
  5. *
  6. */
  7. #include <errno.h>
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <fcntl.h>
  13. #include <sys/uio.h>
  14. #include "helpers.h"
  15. #include "liburing.h"
  16. static int test_openat2(struct io_uring *ring, const char *path, int dfd,
  17. bool direct, int fixed_index)
  18. {
  19. struct io_uring_cqe *cqe;
  20. struct io_uring_sqe *sqe;
  21. struct open_how how;
  22. int ret;
  23. sqe = io_uring_get_sqe(ring);
  24. if (!sqe) {
  25. fprintf(stderr, "get sqe failed\n");
  26. return -1;
  27. }
  28. memset(&how, 0, sizeof(how));
  29. how.flags = O_RDWR;
  30. if (!direct)
  31. io_uring_prep_openat2(sqe, dfd, path, &how);
  32. else
  33. io_uring_prep_openat2_direct(sqe, dfd, path, &how, fixed_index);
  34. ret = io_uring_submit(ring);
  35. if (ret <= 0) {
  36. fprintf(stderr, "sqe submit failed: %d\n", ret);
  37. return -1;
  38. }
  39. ret = io_uring_wait_cqe(ring, &cqe);
  40. if (ret < 0) {
  41. fprintf(stderr, "wait completion %d\n", ret);
  42. return -1;
  43. }
  44. ret = cqe->res;
  45. io_uring_cqe_seen(ring, cqe);
  46. if (direct && ret > 0) {
  47. close(ret);
  48. return -EINVAL;
  49. }
  50. return ret;
  51. }
  52. static int test_open_fixed(const char *path, int dfd)
  53. {
  54. struct io_uring_cqe *cqe;
  55. struct io_uring_sqe *sqe;
  56. struct io_uring ring;
  57. const char pattern = 0xac;
  58. char buffer[] = { 0, 0 };
  59. int i, ret, fd = -1;
  60. ret = io_uring_queue_init(8, &ring, 0);
  61. if (ret) {
  62. fprintf(stderr, "ring setup failed\n");
  63. return -1;
  64. }
  65. ret = io_uring_register_files(&ring, &fd, 1);
  66. if (ret) {
  67. fprintf(stderr, "%s: register ret=%d\n", __FUNCTION__, ret);
  68. return -1;
  69. }
  70. ret = test_openat2(&ring, path, dfd, true, 0);
  71. if (ret == -EINVAL) {
  72. printf("fixed open isn't supported\n");
  73. return 1;
  74. } else if (ret) {
  75. fprintf(stderr, "direct open failed %d\n", ret);
  76. return -1;
  77. }
  78. sqe = io_uring_get_sqe(&ring);
  79. io_uring_prep_write(sqe, 0, &pattern, 1, 0);
  80. sqe->user_data = 1;
  81. sqe->flags |= IOSQE_FIXED_FILE | IOSQE_IO_LINK;
  82. sqe = io_uring_get_sqe(&ring);
  83. io_uring_prep_read(sqe, 0, buffer, 1, 0);
  84. sqe->user_data = 2;
  85. sqe->flags |= IOSQE_FIXED_FILE;
  86. ret = io_uring_submit(&ring);
  87. if (ret != 2) {
  88. fprintf(stderr, "%s: got %d, wanted 2\n", __FUNCTION__, ret);
  89. return -1;
  90. }
  91. for (i = 0; i < 2; i++) {
  92. ret = io_uring_wait_cqe(&ring, &cqe);
  93. if (ret < 0) {
  94. fprintf(stderr, "wait completion %d\n", ret);
  95. return -1;
  96. }
  97. if (cqe->res != 1) {
  98. fprintf(stderr, "unexpectetd ret %d\n", cqe->res);
  99. return -1;
  100. }
  101. io_uring_cqe_seen(&ring, cqe);
  102. }
  103. if (memcmp(&pattern, buffer, 1) != 0) {
  104. fprintf(stderr, "buf validation failed\n");
  105. return -1;
  106. }
  107. io_uring_queue_exit(&ring);
  108. return 0;
  109. }
  110. static int test_open_fixed_fail(const char *path, int dfd)
  111. {
  112. struct io_uring ring;
  113. int ret, fd = -1;
  114. ret = io_uring_queue_init(8, &ring, 0);
  115. if (ret) {
  116. fprintf(stderr, "ring setup failed\n");
  117. return -1;
  118. }
  119. ret = test_openat2(&ring, path, dfd, true, 0);
  120. if (ret != -ENXIO) {
  121. fprintf(stderr, "install into not existing table, %i\n", ret);
  122. return 1;
  123. }
  124. ret = io_uring_register_files(&ring, &fd, 1);
  125. if (ret) {
  126. fprintf(stderr, "%s: register ret=%d\n", __FUNCTION__, ret);
  127. return -1;
  128. }
  129. ret = test_openat2(&ring, path, dfd, true, 1);
  130. if (ret != -EINVAL) {
  131. fprintf(stderr, "install out of bounds, %i\n", ret);
  132. return -1;
  133. }
  134. ret = test_openat2(&ring, path, dfd, true, (1u << 16));
  135. if (ret != -EINVAL) {
  136. fprintf(stderr, "install out of bounds or u16 overflow, %i\n", ret);
  137. return -1;
  138. }
  139. ret = test_openat2(&ring, path, dfd, true, (1u << 16) + 1);
  140. if (ret != -EINVAL) {
  141. fprintf(stderr, "install out of bounds or u16 overflow, %i\n", ret);
  142. return -1;
  143. }
  144. io_uring_queue_exit(&ring);
  145. return 0;
  146. }
  147. static int test_direct_reinstall(const char *path, int dfd)
  148. {
  149. struct io_uring_cqe *cqe;
  150. struct io_uring_sqe *sqe;
  151. char buf[1] = { 0xfa };
  152. struct io_uring ring;
  153. int ret, pipe_fds[2];
  154. ssize_t ret2;
  155. if (pipe2(pipe_fds, O_NONBLOCK)) {
  156. fprintf(stderr, "pipe() failed\n");
  157. return -1;
  158. }
  159. ret = io_uring_queue_init(8, &ring, 0);
  160. if (ret) {
  161. fprintf(stderr, "ring setup failed\n");
  162. return -1;
  163. }
  164. ret = io_uring_register_files(&ring, pipe_fds, 2);
  165. if (ret) {
  166. fprintf(stderr, "%s: register ret=%d\n", __FUNCTION__, ret);
  167. return -1;
  168. }
  169. /* reinstall into the second slot */
  170. ret = test_openat2(&ring, path, dfd, true, 1);
  171. if (ret != 0) {
  172. fprintf(stderr, "reinstall failed, %i\n", ret);
  173. return -1;
  174. }
  175. /* verify it's reinstalled, first write into the slot... */
  176. sqe = io_uring_get_sqe(&ring);
  177. io_uring_prep_write(sqe, 1, buf, sizeof(buf), 0);
  178. sqe->flags |= IOSQE_FIXED_FILE;
  179. ret = io_uring_submit(&ring);
  180. if (ret != 1) {
  181. fprintf(stderr, "sqe submit failed: %d\n", ret);
  182. return -1;
  183. }
  184. ret = io_uring_wait_cqe(&ring, &cqe);
  185. if (ret < 0) {
  186. fprintf(stderr, "wait completion %d\n", ret);
  187. return ret;
  188. }
  189. ret = cqe->res;
  190. io_uring_cqe_seen(&ring, cqe);
  191. if (ret != 1) {
  192. fprintf(stderr, "invalid write %i\n", ret);
  193. return -1;
  194. }
  195. /* ... and make sure nothing has been written to the pipe */
  196. ret2 = read(pipe_fds[0], buf, 1);
  197. if (ret2 != 0 && !(ret2 < 0 && errno == EAGAIN)) {
  198. fprintf(stderr, "invalid pipe read, %d %d\n", errno, (int)ret2);
  199. return -1;
  200. }
  201. close(pipe_fds[0]);
  202. close(pipe_fds[1]);
  203. io_uring_queue_exit(&ring);
  204. return 0;
  205. }
  206. int main(int argc, char *argv[])
  207. {
  208. struct io_uring ring;
  209. const char *path, *path_rel;
  210. int ret, do_unlink;
  211. ret = io_uring_queue_init(8, &ring, 0);
  212. if (ret) {
  213. fprintf(stderr, "ring setup failed\n");
  214. return 1;
  215. }
  216. if (argc > 1) {
  217. path = "/tmp/.open.at2";
  218. path_rel = argv[1];
  219. do_unlink = 0;
  220. } else {
  221. path = "/tmp/.open.at2";
  222. path_rel = ".open.at2";
  223. do_unlink = 1;
  224. }
  225. t_create_file(path, 4096);
  226. if (do_unlink)
  227. t_create_file(path_rel, 4096);
  228. ret = test_openat2(&ring, path, -1, false, 0);
  229. if (ret < 0) {
  230. if (ret == -EINVAL) {
  231. fprintf(stdout, "openat2 not supported, skipping\n");
  232. goto done;
  233. }
  234. fprintf(stderr, "test_openat2 absolute failed: %d\n", ret);
  235. goto err;
  236. }
  237. ret = test_openat2(&ring, path_rel, AT_FDCWD, false, 0);
  238. if (ret < 0) {
  239. fprintf(stderr, "test_openat2 relative failed: %d\n", ret);
  240. goto err;
  241. }
  242. ret = test_open_fixed(path, -1);
  243. if (ret > 0)
  244. goto done;
  245. if (ret) {
  246. fprintf(stderr, "test_open_fixed failed\n");
  247. goto err;
  248. }
  249. ret = test_open_fixed_fail(path, -1);
  250. if (ret) {
  251. fprintf(stderr, "test_open_fixed_fail failed\n");
  252. goto err;
  253. }
  254. ret = test_direct_reinstall(path, -1);
  255. if (ret) {
  256. fprintf(stderr, "test_direct_reinstall failed\n");
  257. goto err;
  258. }
  259. done:
  260. unlink(path);
  261. if (do_unlink)
  262. unlink(path_rel);
  263. return 0;
  264. err:
  265. unlink(path);
  266. if (do_unlink)
  267. unlink(path_rel);
  268. return 1;
  269. }