sqpoll-disable-exit.c 5.2 KB

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