fd-install.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Description: test installing a direct descriptor into the regular
  5. * file table
  6. *
  7. */
  8. #include <errno.h>
  9. #include <stdio.h>
  10. #include <unistd.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <fcntl.h>
  14. #include "liburing.h"
  15. #include "helpers.h"
  16. static int no_fd_install;
  17. /* test that O_CLOEXEC is accepted, and others are not */
  18. static int test_flags(struct io_uring *ring, int async)
  19. {
  20. struct io_uring_sqe *sqe;
  21. struct io_uring_cqe *cqe;
  22. int ret, fds[2], fd;
  23. if (pipe(fds) < 0) {
  24. perror("pipe");
  25. return T_EXIT_FAIL;
  26. }
  27. ret = io_uring_register_files(ring, &fds[0], 1);
  28. if (ret) {
  29. fprintf(stderr, "failed register files %d\n", ret);
  30. return T_EXIT_FAIL;
  31. }
  32. /* check that setting an invalid flag fails */
  33. sqe = io_uring_get_sqe(ring);
  34. io_uring_prep_fixed_fd_install(sqe, 0, 1U << 17);
  35. io_uring_submit(ring);
  36. ret = io_uring_wait_cqe(ring, &cqe);
  37. if (ret) {
  38. fprintf(stderr, "wait cqe %d\n", ret);
  39. return T_EXIT_FAIL;
  40. }
  41. if (cqe->res != -EINVAL) {
  42. fprintf(stderr, "unexpected cqe res %d\n", cqe->res);
  43. return T_EXIT_FAIL;
  44. }
  45. io_uring_cqe_seen(ring, cqe);
  46. /* check that IORING_FIXED_FD_NO_CLOEXEC is accepted */
  47. sqe = io_uring_get_sqe(ring);
  48. io_uring_prep_fixed_fd_install(sqe, 0, IORING_FIXED_FD_NO_CLOEXEC);
  49. if (async)
  50. sqe->flags |= IOSQE_ASYNC;
  51. io_uring_submit(ring);
  52. ret = io_uring_wait_cqe(ring, &cqe);
  53. if (ret) {
  54. fprintf(stderr, "wait cqe %d\n", ret);
  55. return T_EXIT_FAIL;
  56. }
  57. if (cqe->res < 0) {
  58. fprintf(stderr, "unexpected cqe res %d\n", cqe->res);
  59. return T_EXIT_FAIL;
  60. }
  61. fd = cqe->res;
  62. io_uring_cqe_seen(ring, cqe);
  63. close(fds[0]);
  64. close(fds[1]);
  65. close(fd);
  66. io_uring_unregister_files(ring);
  67. return T_EXIT_PASS;
  68. }
  69. static int test_linked(struct io_uring *ring)
  70. {
  71. struct io_uring_sqe *sqe;
  72. struct io_uring_cqe *cqe;
  73. int ret, fds[2], fd, i;
  74. if (pipe(fds) < 0) {
  75. perror("pipe");
  76. return T_EXIT_FAIL;
  77. }
  78. ret = io_uring_register_files(ring, &fds[0], 1);
  79. if (ret) {
  80. fprintf(stderr, "failed register files %d\n", ret);
  81. return T_EXIT_FAIL;
  82. }
  83. sqe = io_uring_get_sqe(ring);
  84. io_uring_prep_nop(sqe);
  85. sqe->flags |= IOSQE_IO_LINK;
  86. sqe->user_data = 1;
  87. sqe = io_uring_get_sqe(ring);
  88. io_uring_prep_fixed_fd_install(sqe, 0, 0);
  89. sqe->user_data = 2;
  90. ret = io_uring_submit(ring);
  91. if (ret != 2) {
  92. fprintf(stderr, "submit: %d\n", ret);
  93. return T_EXIT_FAIL;
  94. }
  95. fd = -1;
  96. for (i = 0; i < 2; i++) {
  97. ret = io_uring_wait_cqe(ring, &cqe);
  98. if (ret) {
  99. fprintf(stderr, "wait cqe %d\n", ret);
  100. return T_EXIT_FAIL;
  101. }
  102. if (cqe->res < 0) {
  103. fprintf(stderr, "unexpected cqe res %d\n", cqe->res);
  104. return T_EXIT_FAIL;
  105. }
  106. if (cqe->user_data == 2)
  107. fd = cqe->res;
  108. io_uring_cqe_seen(ring, cqe);
  109. }
  110. close(fds[0]);
  111. close(fds[1]);
  112. if (fd != -1)
  113. close(fd);
  114. io_uring_unregister_files(ring);
  115. return T_EXIT_PASS;
  116. }
  117. /* test not setting IOSQE_FIXED_FILE */
  118. static int test_not_fixed(struct io_uring *ring)
  119. {
  120. struct io_uring_sqe *sqe;
  121. struct io_uring_cqe *cqe;
  122. int ret, fds[2];
  123. if (pipe(fds) < 0) {
  124. perror("pipe");
  125. return T_EXIT_FAIL;
  126. }
  127. ret = io_uring_register_files(ring, &fds[0], 1);
  128. if (ret) {
  129. fprintf(stderr, "failed register files %d\n", ret);
  130. return T_EXIT_FAIL;
  131. }
  132. sqe = io_uring_get_sqe(ring);
  133. io_uring_prep_fixed_fd_install(sqe, 0, 0);
  134. sqe->flags &= ~IOSQE_FIXED_FILE;
  135. io_uring_submit(ring);
  136. ret = io_uring_wait_cqe(ring, &cqe);
  137. if (ret) {
  138. fprintf(stderr, "wait cqe %d\n", ret);
  139. return T_EXIT_FAIL;
  140. }
  141. if (cqe->res != -EBADF) {
  142. fprintf(stderr, "unexpected cqe res %d\n", cqe->res);
  143. return T_EXIT_FAIL;
  144. }
  145. io_uring_cqe_seen(ring, cqe);
  146. close(fds[0]);
  147. close(fds[1]);
  148. io_uring_unregister_files(ring);
  149. return T_EXIT_PASS;
  150. }
  151. /* test invalid direct descriptor indexes */
  152. static int test_bad_fd(struct io_uring *ring, int some_fd)
  153. {
  154. struct io_uring_sqe *sqe;
  155. struct io_uring_cqe *cqe;
  156. int ret;
  157. sqe = io_uring_get_sqe(ring);
  158. io_uring_prep_fixed_fd_install(sqe, some_fd, 0);
  159. io_uring_submit(ring);
  160. ret = io_uring_wait_cqe(ring, &cqe);
  161. if (ret) {
  162. fprintf(stderr, "wait cqe %d\n", ret);
  163. return T_EXIT_FAIL;
  164. }
  165. if (cqe->res != -EBADF) {
  166. fprintf(stderr, "unexpected cqe res %d\n", cqe->res);
  167. return T_EXIT_FAIL;
  168. }
  169. io_uring_cqe_seen(ring, cqe);
  170. return T_EXIT_PASS;
  171. }
  172. /* test basic functionality of shifting a direct descriptor to a normal file */
  173. static int test_working(struct io_uring *ring)
  174. {
  175. struct io_uring_sqe *sqe;
  176. struct io_uring_cqe *cqe;
  177. int ret, fds[2];
  178. char buf[32];
  179. if (pipe(fds) < 0) {
  180. perror("pipe");
  181. return T_EXIT_FAIL;
  182. }
  183. /* register read side */
  184. ret = io_uring_register_files(ring, &fds[0], 1);
  185. if (ret) {
  186. fprintf(stderr, "failed register files %d\n", ret);
  187. return T_EXIT_FAIL;
  188. }
  189. /* close normal descriptor */
  190. close(fds[0]);
  191. /* normal read should fail */
  192. ret = read(fds[0], buf, 1);
  193. if (ret != -1) {
  194. fprintf(stderr, "unexpected read ret %d\n", ret);
  195. return T_EXIT_FAIL;
  196. }
  197. if (errno != EBADF) {
  198. fprintf(stderr, "unexpected read failure %d\n", errno);
  199. return T_EXIT_FAIL;
  200. }
  201. /* verify we can read the data */
  202. sqe = io_uring_get_sqe(ring);
  203. io_uring_prep_read(sqe, 0, buf, sizeof(buf), 0);
  204. sqe->flags |= IOSQE_FIXED_FILE;
  205. io_uring_submit(ring);
  206. /* put some data in the pipe */
  207. ret = write(fds[1], "Hello", 5);
  208. if (ret < 0) {
  209. perror("write");
  210. return T_EXIT_FAIL;
  211. } else if (ret != 5) {
  212. fprintf(stderr, "short write %d\n", ret);
  213. return T_EXIT_FAIL;
  214. }
  215. ret = io_uring_wait_cqe(ring, &cqe);
  216. if (ret) {
  217. fprintf(stderr, "wait cqe %d\n", ret);
  218. return T_EXIT_FAIL;
  219. }
  220. if (cqe->res != 5) {
  221. fprintf(stderr, "weird pipe read ret %d\n", cqe->res);
  222. return T_EXIT_FAIL;
  223. }
  224. io_uring_cqe_seen(ring, cqe);
  225. /* fixed pipe read worked, now re-install as a regular fd */
  226. sqe = io_uring_get_sqe(ring);
  227. io_uring_prep_fixed_fd_install(sqe, 0, 0);
  228. io_uring_submit(ring);
  229. ret = io_uring_wait_cqe(ring, &cqe);
  230. if (ret) {
  231. fprintf(stderr, "wait cqe %d\n", ret);
  232. return T_EXIT_FAIL;
  233. }
  234. if (cqe->res == -EINVAL) {
  235. no_fd_install = 1;
  236. return T_EXIT_SKIP;
  237. }
  238. if (cqe->res < 0) {
  239. fprintf(stderr, "failed install fd: %d\n", cqe->res);
  240. return T_EXIT_FAIL;
  241. }
  242. /* stash new pipe read side fd in old spot */
  243. fds[0] = cqe->res;
  244. io_uring_cqe_seen(ring, cqe);
  245. ret = write(fds[1], "Hello", 5);
  246. if (ret < 0) {
  247. perror("write");
  248. return T_EXIT_FAIL;
  249. } else if (ret != 5) {
  250. fprintf(stderr, "short write %d\n", ret);
  251. return T_EXIT_FAIL;
  252. }
  253. /* normal pipe read should now work with new fd */
  254. ret = read(fds[0], buf, sizeof(buf));
  255. if (ret != 5) {
  256. fprintf(stderr, "unexpected read ret %d\n", ret);
  257. return T_EXIT_FAIL;
  258. }
  259. /* close fixed file */
  260. sqe = io_uring_get_sqe(ring);
  261. io_uring_prep_close_direct(sqe, 0);
  262. io_uring_submit(ring);
  263. ret = io_uring_wait_cqe(ring, &cqe);
  264. if (ret) {
  265. fprintf(stderr, "wait cqe %d\n", ret);
  266. return T_EXIT_FAIL;
  267. }
  268. if (cqe->res) {
  269. fprintf(stderr, "close fixed fd %d\n", cqe->res);
  270. return T_EXIT_FAIL;
  271. }
  272. io_uring_cqe_seen(ring, cqe);
  273. ret = write(fds[1], "Hello", 5);
  274. if (ret < 0) {
  275. perror("write");
  276. return T_EXIT_FAIL;
  277. } else if (ret != 5) {
  278. fprintf(stderr, "short write %d\n", ret);
  279. return T_EXIT_FAIL;
  280. }
  281. /* normal pipe read should still work with new fd */
  282. ret = read(fds[0], buf, sizeof(buf));
  283. if (ret != 5) {
  284. fprintf(stderr, "unexpected read ret %d\n", ret);
  285. return T_EXIT_FAIL;
  286. }
  287. /* fixed fd pipe read should now fail */
  288. sqe = io_uring_get_sqe(ring);
  289. io_uring_prep_read(sqe, 0, buf, sizeof(buf), 0);
  290. sqe->flags = IOSQE_FIXED_FILE;
  291. io_uring_submit(ring);
  292. /* put some data in the pipe */
  293. ret = write(fds[1], "Hello", 5);
  294. if (ret < 0) {
  295. perror("write");
  296. return T_EXIT_FAIL;
  297. } else if (ret != 5) {
  298. fprintf(stderr, "short write %d\n", ret);
  299. return T_EXIT_FAIL;
  300. }
  301. ret = io_uring_wait_cqe(ring, &cqe);
  302. if (ret) {
  303. fprintf(stderr, "wait cqe %d\n", ret);
  304. return T_EXIT_FAIL;
  305. }
  306. if (cqe->res != -EBADF) {
  307. fprintf(stderr, "weird pipe read ret %d\n", cqe->res);
  308. return T_EXIT_FAIL;
  309. }
  310. io_uring_cqe_seen(ring, cqe);
  311. close(fds[0]);
  312. close(fds[1]);
  313. io_uring_unregister_files(ring);
  314. return T_EXIT_PASS;
  315. }
  316. static int test_creds(struct io_uring *ring, int async)
  317. {
  318. struct io_uring_sqe *sqe;
  319. struct io_uring_cqe *cqe;
  320. int cred_id, ret, fds[2];
  321. if (pipe(fds) < 0) {
  322. perror("pipe");
  323. return T_EXIT_FAIL;
  324. }
  325. ret = io_uring_register_files(ring, &fds[0], 1);
  326. if (ret) {
  327. fprintf(stderr, "failed register files %d\n", ret);
  328. return T_EXIT_FAIL;
  329. }
  330. cred_id = io_uring_register_personality(ring);
  331. if (cred_id < 0) {
  332. fprintf(stderr, "Failed registering creds: %d\n", cred_id);
  333. return T_EXIT_FAIL;
  334. }
  335. /* check that asking for creds fails */
  336. sqe = io_uring_get_sqe(ring);
  337. io_uring_prep_fixed_fd_install(sqe, 0, 0);
  338. if (async)
  339. sqe->flags |= IOSQE_ASYNC;
  340. sqe->personality = cred_id;
  341. io_uring_submit(ring);
  342. ret = io_uring_wait_cqe(ring, &cqe);
  343. if (ret) {
  344. fprintf(stderr, "wait cqe %d\n", ret);
  345. return T_EXIT_FAIL;
  346. }
  347. if (cqe->res > 0) {
  348. fprintf(stderr, "install succeeded with creds\n");
  349. return T_EXIT_FAIL;
  350. }
  351. if (cqe->res != -EPERM) {
  352. fprintf(stderr, "unexpected cqe res %d\n", cqe->res);
  353. return T_EXIT_FAIL;
  354. }
  355. io_uring_cqe_seen(ring, cqe);
  356. close(fds[0]);
  357. close(fds[1]);
  358. io_uring_unregister_files(ring);
  359. io_uring_unregister_personality(ring, cred_id);
  360. return T_EXIT_PASS;
  361. }
  362. int main(int argc, char *argv[])
  363. {
  364. struct io_uring ring;
  365. int ret;
  366. if (argc > 1)
  367. return T_EXIT_SKIP;
  368. ret = io_uring_queue_init(4, &ring, 0);
  369. if (ret) {
  370. fprintf(stderr, "ring setup failed: %d\n", ret);
  371. return T_EXIT_FAIL;
  372. }
  373. ret = test_working(&ring);
  374. if (ret != T_EXIT_PASS) {
  375. if (ret == T_EXIT_FAIL)
  376. fprintf(stderr, "test_working failed\n");
  377. return ret;
  378. }
  379. if (no_fd_install)
  380. return T_EXIT_SKIP;
  381. ret = test_bad_fd(&ring, 0);
  382. if (ret != T_EXIT_PASS) {
  383. if (ret == T_EXIT_FAIL)
  384. fprintf(stderr, "test_bad_fd 0 failed\n");
  385. return ret;
  386. }
  387. ret = test_bad_fd(&ring, 500);
  388. if (ret != T_EXIT_PASS) {
  389. if (ret == T_EXIT_FAIL)
  390. fprintf(stderr, "test_bad_fd 500 failed\n");
  391. return ret;
  392. }
  393. ret = test_not_fixed(&ring);
  394. if (ret != T_EXIT_PASS) {
  395. if (ret == T_EXIT_FAIL)
  396. fprintf(stderr, "test_not_fixed failed\n");
  397. return ret;
  398. }
  399. ret = test_flags(&ring, 0);
  400. if (ret != T_EXIT_PASS) {
  401. if (ret == T_EXIT_FAIL)
  402. fprintf(stderr, "test_flags 0 failed\n");
  403. return ret;
  404. }
  405. ret = test_flags(&ring, 1);
  406. if (ret != T_EXIT_PASS) {
  407. if (ret == T_EXIT_FAIL)
  408. fprintf(stderr, "test_flags 1 failed\n");
  409. return ret;
  410. }
  411. ret = test_creds(&ring, 0);
  412. if (ret != T_EXIT_PASS) {
  413. if (ret == T_EXIT_FAIL)
  414. fprintf(stderr, "test_creds 0 failed\n");
  415. return ret;
  416. }
  417. ret = test_creds(&ring, 1);
  418. if (ret != T_EXIT_PASS) {
  419. if (ret == T_EXIT_FAIL)
  420. fprintf(stderr, "test_creds 1 failed\n");
  421. return ret;
  422. }
  423. ret = test_linked(&ring);
  424. if (ret != T_EXIT_PASS) {
  425. if (ret == T_EXIT_FAIL)
  426. fprintf(stderr, "test_linked failed\n");
  427. return ret;
  428. }
  429. return T_EXIT_PASS;
  430. }