ring-leak.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * Based on description from Al Viro - this demonstrates a leak of the
  5. * io_uring instance, by sending the io_uring fd over a UNIX socket.
  6. *
  7. * See:
  8. *
  9. * https://lore.kernel.org/linux-block/20190129192702.3605-1-axboe@kernel.dk/T/#m6c87fc64e4d063786af6ec6fadce3ac1e95d3184
  10. *
  11. */
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <stddef.h>
  15. #include <signal.h>
  16. #include <inttypes.h>
  17. #include <sys/types.h>
  18. #include <sys/syscall.h>
  19. #include <sys/socket.h>
  20. #include <sys/wait.h>
  21. #include <fcntl.h>
  22. #include <unistd.h>
  23. #include <string.h>
  24. #include <linux/fs.h>
  25. #include "liburing.h"
  26. #include "../src/syscall.h"
  27. static int __io_uring_register_files(int ring_fd, int fd1, int fd2)
  28. {
  29. __s32 fds[2] = { fd1, fd2 };
  30. return __sys_io_uring_register(ring_fd, IORING_REGISTER_FILES, fds, 2);
  31. }
  32. static int get_ring_fd(void)
  33. {
  34. struct io_uring_params p;
  35. int fd;
  36. memset(&p, 0, sizeof(p));
  37. fd = __sys_io_uring_setup(2, &p);
  38. if (fd < 0) {
  39. perror("io_uring_setup");
  40. return -1;
  41. }
  42. return fd;
  43. }
  44. static void send_fd(int socket, int fd)
  45. {
  46. char buf[CMSG_SPACE(sizeof(fd))];
  47. struct cmsghdr *cmsg;
  48. struct msghdr msg;
  49. memset(buf, 0, sizeof(buf));
  50. memset(&msg, 0, sizeof(msg));
  51. msg.msg_control = buf;
  52. msg.msg_controllen = sizeof(buf);
  53. cmsg = CMSG_FIRSTHDR(&msg);
  54. cmsg->cmsg_level = SOL_SOCKET;
  55. cmsg->cmsg_type = SCM_RIGHTS;
  56. cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
  57. memmove(CMSG_DATA(cmsg), &fd, sizeof(fd));
  58. msg.msg_controllen = CMSG_SPACE(sizeof(fd));
  59. if (sendmsg(socket, &msg, 0) < 0)
  60. perror("sendmsg");
  61. }
  62. static int test_iowq_request_cancel(void)
  63. {
  64. char buffer[128];
  65. struct io_uring ring;
  66. struct io_uring_sqe *sqe;
  67. int ret, fds[2];
  68. ret = io_uring_queue_init(8, &ring, 0);
  69. if (ret < 0) {
  70. fprintf(stderr, "failed to init io_uring: %s\n", strerror(-ret));
  71. return ret;
  72. }
  73. if (pipe(fds)) {
  74. perror("pipe");
  75. return -1;
  76. }
  77. ret = io_uring_register_files(&ring, fds, 2);
  78. if (ret) {
  79. fprintf(stderr, "file_register: %d\n", ret);
  80. return ret;
  81. }
  82. close(fds[1]);
  83. sqe = io_uring_get_sqe(&ring);
  84. if (!sqe) {
  85. fprintf(stderr, "%s: failed to get sqe\n", __FUNCTION__);
  86. return 1;
  87. }
  88. /* potentially sitting in internal polling */
  89. io_uring_prep_read(sqe, 0, buffer, 10, 0);
  90. sqe->flags |= IOSQE_FIXED_FILE;
  91. sqe = io_uring_get_sqe(&ring);
  92. if (!sqe) {
  93. fprintf(stderr, "%s: failed to get sqe\n", __FUNCTION__);
  94. return 1;
  95. }
  96. /* staying in io-wq */
  97. io_uring_prep_read(sqe, 0, buffer, 10, 0);
  98. sqe->flags |= IOSQE_FIXED_FILE | IOSQE_ASYNC;
  99. ret = io_uring_submit(&ring);
  100. if (ret != 2) {
  101. fprintf(stderr, "%s: got %d, wanted 1\n", __FUNCTION__, ret);
  102. return 1;
  103. }
  104. /* should unregister files and close the write fd */
  105. io_uring_queue_exit(&ring);
  106. /*
  107. * We're trying to wait for the ring to "really" exit, that will be
  108. * done async. For that rely on the registered write end to be closed
  109. * after ring quiesce, so failing read from the other pipe end.
  110. */
  111. ret = read(fds[0], buffer, 10);
  112. if (ret < 0)
  113. perror("read");
  114. close(fds[0]);
  115. return 0;
  116. }
  117. static void trigger_unix_gc(void)
  118. {
  119. int fd;
  120. fd = socket(AF_UNIX, SOCK_DGRAM, 0);
  121. if (fd < 0)
  122. perror("socket dgram");
  123. else
  124. close(fd);
  125. }
  126. static int test_scm_cycles(bool update)
  127. {
  128. char buffer[128];
  129. struct io_uring ring;
  130. int i, ret;
  131. int sp[2], fds[2], reg_fds[4];
  132. if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sp) != 0) {
  133. perror("Failed to create Unix-domain socket pair\n");
  134. return 1;
  135. }
  136. ret = io_uring_queue_init(8, &ring, 0);
  137. if (ret < 0) {
  138. fprintf(stderr, "failed to init io_uring: %s\n", strerror(-ret));
  139. return ret;
  140. }
  141. if (pipe(fds)) {
  142. perror("pipe");
  143. return -1;
  144. }
  145. send_fd(sp[0], ring.ring_fd);
  146. /* register an empty set for updates */
  147. if (update) {
  148. for (i = 0; i < 4; i++)
  149. reg_fds[i] = -1;
  150. ret = io_uring_register_files(&ring, reg_fds, 4);
  151. if (ret) {
  152. fprintf(stderr, "file_register: %d\n", ret);
  153. return ret;
  154. }
  155. }
  156. reg_fds[0] = fds[0];
  157. reg_fds[1] = fds[1];
  158. reg_fds[2] = sp[0];
  159. reg_fds[3] = sp[1];
  160. if (update) {
  161. ret = io_uring_register_files_update(&ring, 0, reg_fds, 4);
  162. if (ret != 4) {
  163. fprintf(stderr, "file_register: %d\n", ret);
  164. return ret;
  165. }
  166. } else {
  167. ret = io_uring_register_files(&ring, reg_fds, 4);
  168. if (ret) {
  169. fprintf(stderr, "file_register: %d\n", ret);
  170. return ret;
  171. }
  172. }
  173. close(fds[1]);
  174. close(sp[0]);
  175. close(sp[1]);
  176. /* should unregister files and close the write fd */
  177. io_uring_queue_exit(&ring);
  178. trigger_unix_gc();
  179. /*
  180. * We're trying to wait for the ring to "really" exit, that will be
  181. * done async. For that rely on the registered write end to be closed
  182. * after ring quiesce, so failing read from the other pipe end.
  183. */
  184. ret = read(fds[0], buffer, 10);
  185. if (ret < 0)
  186. perror("read");
  187. close(fds[0]);
  188. return 0;
  189. }
  190. int main(int argc, char *argv[])
  191. {
  192. int sp[2], pid, ring_fd, ret;
  193. int i;
  194. if (argc > 1)
  195. return 0;
  196. ret = test_iowq_request_cancel();
  197. if (ret) {
  198. fprintf(stderr, "test_iowq_request_cancel() failed\n");
  199. return 1;
  200. }
  201. for (i = 0; i < 2; i++) {
  202. bool update = !!(i & 1);
  203. ret = test_scm_cycles(update);
  204. if (ret) {
  205. fprintf(stderr, "test_scm_cycles() failed %i\n",
  206. update);
  207. return 1;
  208. }
  209. }
  210. if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sp) != 0) {
  211. perror("Failed to create Unix-domain socket pair\n");
  212. return 1;
  213. }
  214. ring_fd = get_ring_fd();
  215. if (ring_fd < 0)
  216. return 1;
  217. ret = __io_uring_register_files(ring_fd, sp[0], sp[1]);
  218. if (ret < 0) {
  219. perror("register files");
  220. return 1;
  221. }
  222. pid = fork();
  223. if (pid)
  224. send_fd(sp[0], ring_fd);
  225. close(ring_fd);
  226. close(sp[0]);
  227. close(sp[1]);
  228. return 0;
  229. }