sqpoll-disable-exit.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. // https://syzkaller.appspot.com/bug?id=99f4ea77bb9b9ef24cefb66469be319f4aa9f162
  4. // autogenerated by syzkaller (https://github.com/google/syzkaller)
  5. #include <dirent.h>
  6. #include <endian.h>
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <signal.h>
  10. #include <stdarg.h>
  11. #include <stdbool.h>
  12. #include <stdint.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <sys/mman.h>
  17. #include <sys/prctl.h>
  18. #include <sys/stat.h>
  19. #include <sys/types.h>
  20. #include <sys/wait.h>
  21. #include <time.h>
  22. #include <unistd.h>
  23. #include "liburing.h"
  24. #include "../src/syscall.h"
  25. static void sleep_ms(uint64_t ms)
  26. {
  27. usleep(ms * 1000);
  28. }
  29. static uint64_t current_time_ms(void)
  30. {
  31. struct timespec ts;
  32. if (clock_gettime(CLOCK_MONOTONIC, &ts))
  33. exit(1);
  34. return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
  35. }
  36. static bool write_file(const char* file, const char* what, ...)
  37. {
  38. char buf[1024];
  39. va_list args;
  40. va_start(args, what);
  41. vsnprintf(buf, sizeof(buf), what, args);
  42. va_end(args);
  43. buf[sizeof(buf) - 1] = 0;
  44. int len = strlen(buf);
  45. int fd = open(file, O_WRONLY | O_CLOEXEC);
  46. if (fd == -1)
  47. return false;
  48. if (write(fd, buf, len) != len) {
  49. int err = errno;
  50. close(fd);
  51. errno = err;
  52. return false;
  53. }
  54. close(fd);
  55. return true;
  56. }
  57. #define SIZEOF_IO_URING_SQE 64
  58. #define SIZEOF_IO_URING_CQE 16
  59. #define SQ_HEAD_OFFSET 0
  60. #define SQ_TAIL_OFFSET 64
  61. #define SQ_RING_MASK_OFFSET 256
  62. #define SQ_RING_ENTRIES_OFFSET 264
  63. #define SQ_FLAGS_OFFSET 276
  64. #define SQ_DROPPED_OFFSET 272
  65. #define CQ_HEAD_OFFSET 128
  66. #define CQ_TAIL_OFFSET 192
  67. #define CQ_RING_MASK_OFFSET 260
  68. #define CQ_RING_ENTRIES_OFFSET 268
  69. #define CQ_RING_OVERFLOW_OFFSET 284
  70. #define CQ_FLAGS_OFFSET 280
  71. #define CQ_CQES_OFFSET 320
  72. static long syz_io_uring_setup(volatile long a0, volatile long a1,
  73. volatile long a2, volatile long a3,
  74. volatile long a4, volatile long a5)
  75. {
  76. uint32_t entries = (uint32_t)a0;
  77. struct io_uring_params* setup_params = (struct io_uring_params*)a1;
  78. void* vma1 = (void*)a2;
  79. void* vma2 = (void*)a3;
  80. void** ring_ptr_out = (void**)a4;
  81. void** sqes_ptr_out = (void**)a5;
  82. uint32_t fd_io_uring = __sys_io_uring_setup(entries, setup_params);
  83. uint32_t sq_ring_sz =
  84. setup_params->sq_off.array + setup_params->sq_entries * sizeof(uint32_t);
  85. uint32_t cq_ring_sz = setup_params->cq_off.cqes +
  86. setup_params->cq_entries * SIZEOF_IO_URING_CQE;
  87. uint32_t ring_sz = sq_ring_sz > cq_ring_sz ? sq_ring_sz : cq_ring_sz;
  88. *ring_ptr_out = mmap(vma1, ring_sz, PROT_READ | PROT_WRITE,
  89. MAP_SHARED | MAP_POPULATE | MAP_FIXED, fd_io_uring,
  90. IORING_OFF_SQ_RING);
  91. uint32_t sqes_sz = setup_params->sq_entries * SIZEOF_IO_URING_SQE;
  92. *sqes_ptr_out =
  93. mmap(vma2, sqes_sz, PROT_READ | PROT_WRITE,
  94. MAP_SHARED | MAP_POPULATE | MAP_FIXED, fd_io_uring, IORING_OFF_SQES);
  95. return fd_io_uring;
  96. }
  97. static void kill_and_wait(int pid, int* status)
  98. {
  99. kill(-pid, SIGKILL);
  100. kill(pid, SIGKILL);
  101. for (int i = 0; i < 100; i++) {
  102. if (waitpid(-1, status, WNOHANG | __WALL) == pid)
  103. return;
  104. usleep(1000);
  105. }
  106. DIR* dir = opendir("/sys/fs/fuse/connections");
  107. if (dir) {
  108. for (;;) {
  109. struct dirent* ent = readdir(dir);
  110. if (!ent)
  111. break;
  112. if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
  113. continue;
  114. char abort[300];
  115. snprintf(abort, sizeof(abort), "/sys/fs/fuse/connections/%s/abort",
  116. ent->d_name);
  117. int fd = open(abort, O_WRONLY);
  118. if (fd == -1) {
  119. continue;
  120. }
  121. if (write(fd, abort, 1) < 0) {
  122. }
  123. close(fd);
  124. }
  125. closedir(dir);
  126. } else {
  127. }
  128. while (waitpid(-1, status, __WALL) != pid) {
  129. }
  130. }
  131. static void setup_test(void)
  132. {
  133. prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
  134. setpgrp();
  135. write_file("/proc/self/oom_score_adj", "1000");
  136. }
  137. static void execute_one(void);
  138. #define WAIT_FLAGS __WALL
  139. static void loop(void)
  140. {
  141. int iter = 0;
  142. for (; iter < 100; iter++) {
  143. int pid = fork();
  144. if (pid < 0)
  145. exit(1);
  146. if (pid == 0) {
  147. setup_test();
  148. execute_one();
  149. exit(0);
  150. }
  151. int status = 0;
  152. uint64_t start = current_time_ms();
  153. for (;;) {
  154. if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
  155. break;
  156. sleep_ms(1);
  157. if (current_time_ms() - start < 5000) {
  158. continue;
  159. }
  160. kill_and_wait(pid, &status);
  161. break;
  162. }
  163. }
  164. }
  165. void execute_one(void)
  166. {
  167. *(uint32_t*)0x20000044 = 0;
  168. *(uint32_t*)0x20000048 = 0x42;
  169. *(uint32_t*)0x2000004c = 0;
  170. *(uint32_t*)0x20000050 = 0;
  171. *(uint32_t*)0x20000058 = -1;
  172. *(uint32_t*)0x2000005c = 0;
  173. *(uint32_t*)0x20000060 = 0;
  174. *(uint32_t*)0x20000064 = 0;
  175. syz_io_uring_setup(0x74bc, 0x20000040, 0x20ffb000, 0x20ffc000, 0, 0);
  176. }
  177. int main(void)
  178. {
  179. mmap((void *)0x1ffff000ul, 0x1000ul, 0ul, MAP_ANON|MAP_PRIVATE, -1, 0ul);
  180. mmap((void *)0x20000000ul, 0x1000000ul, 7ul, MAP_ANON|MAP_PRIVATE, -1, 0ul);
  181. mmap((void *)0x21000000ul, 0x1000ul, 0ul, MAP_ANON|MAP_PRIVATE, -1, 0ul);
  182. loop();
  183. return 0;
  184. }