pollfree.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. // https://syzkaller.appspot.com/bug?id=5f5a44abb4cba056fe24255c4fcb7e7bbe13de7a
  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 <pthread.h>
  10. #include <signal.h>
  11. #include <stdarg.h>
  12. #include <stdbool.h>
  13. #include <stdint.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <sys/mman.h>
  18. #include <sys/prctl.h>
  19. #include <sys/stat.h>
  20. #include <sys/syscall.h>
  21. #include <sys/types.h>
  22. #include <sys/wait.h>
  23. #include <time.h>
  24. #include <unistd.h>
  25. #include <linux/futex.h>
  26. #ifdef __NR_futex
  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 void thread_start(void* (*fn)(void*), void* arg)
  39. {
  40. pthread_t th;
  41. pthread_attr_t attr;
  42. pthread_attr_init(&attr);
  43. pthread_attr_setstacksize(&attr, 128 << 10);
  44. int i = 0;
  45. for (; i < 100; i++) {
  46. if (pthread_create(&th, &attr, fn, arg) == 0) {
  47. pthread_attr_destroy(&attr);
  48. return;
  49. }
  50. if (errno == EAGAIN) {
  51. usleep(50);
  52. continue;
  53. }
  54. break;
  55. }
  56. exit(1);
  57. }
  58. typedef struct {
  59. int state;
  60. } event_t;
  61. static void event_init(event_t* ev)
  62. {
  63. ev->state = 0;
  64. }
  65. static void event_reset(event_t* ev)
  66. {
  67. ev->state = 0;
  68. }
  69. static void event_set(event_t* ev)
  70. {
  71. if (ev->state)
  72. exit(1);
  73. __atomic_store_n(&ev->state, 1, __ATOMIC_RELEASE);
  74. syscall(__NR_futex, &ev->state, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1000000);
  75. }
  76. static void event_wait(event_t* ev)
  77. {
  78. while (!__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE))
  79. syscall(__NR_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, 0);
  80. }
  81. static int event_isset(event_t* ev)
  82. {
  83. return __atomic_load_n(&ev->state, __ATOMIC_ACQUIRE);
  84. }
  85. static int event_timedwait(event_t* ev, uint64_t timeout)
  86. {
  87. uint64_t start = current_time_ms();
  88. uint64_t now = start;
  89. for (;;) {
  90. uint64_t remain = timeout - (now - start);
  91. struct timespec ts;
  92. ts.tv_sec = remain / 1000;
  93. ts.tv_nsec = (remain % 1000) * 1000 * 1000;
  94. syscall(__NR_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, &ts);
  95. if (__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE))
  96. return 1;
  97. now = current_time_ms();
  98. if (now - start > timeout)
  99. return 0;
  100. }
  101. }
  102. #define SIZEOF_IO_URING_SQE 64
  103. #define SIZEOF_IO_URING_CQE 16
  104. #define SQ_HEAD_OFFSET 0
  105. #define SQ_TAIL_OFFSET 64
  106. #define SQ_RING_MASK_OFFSET 256
  107. #define SQ_RING_ENTRIES_OFFSET 264
  108. #define SQ_FLAGS_OFFSET 276
  109. #define SQ_DROPPED_OFFSET 272
  110. #define CQ_HEAD_OFFSET 128
  111. #define CQ_TAIL_OFFSET 192
  112. #define CQ_RING_MASK_OFFSET 260
  113. #define CQ_RING_ENTRIES_OFFSET 268
  114. #define CQ_RING_OVERFLOW_OFFSET 284
  115. #define CQ_FLAGS_OFFSET 280
  116. #define CQ_CQES_OFFSET 320
  117. struct io_sqring_offsets {
  118. uint32_t head;
  119. uint32_t tail;
  120. uint32_t ring_mask;
  121. uint32_t ring_entries;
  122. uint32_t flags;
  123. uint32_t dropped;
  124. uint32_t array;
  125. uint32_t resv1;
  126. uint64_t resv2;
  127. };
  128. struct io_cqring_offsets {
  129. uint32_t head;
  130. uint32_t tail;
  131. uint32_t ring_mask;
  132. uint32_t ring_entries;
  133. uint32_t overflow;
  134. uint32_t cqes;
  135. uint64_t resv[2];
  136. };
  137. struct io_uring_params {
  138. uint32_t sq_entries;
  139. uint32_t cq_entries;
  140. uint32_t flags;
  141. uint32_t sq_thread_cpu;
  142. uint32_t sq_thread_idle;
  143. uint32_t features;
  144. uint32_t resv[4];
  145. struct io_sqring_offsets sq_off;
  146. struct io_cqring_offsets cq_off;
  147. };
  148. #define IORING_OFF_SQ_RING 0
  149. #define IORING_OFF_SQES 0x10000000ULL
  150. #define sys_io_uring_setup 425
  151. static long syz_io_uring_setup(volatile long a0, volatile long a1,
  152. volatile long a2, volatile long a3,
  153. volatile long a4, volatile long a5)
  154. {
  155. uint32_t entries = (uint32_t)a0;
  156. struct io_uring_params* setup_params = (struct io_uring_params*)a1;
  157. void* vma1 = (void*)a2;
  158. void* vma2 = (void*)a3;
  159. void** ring_ptr_out = (void**)a4;
  160. void** sqes_ptr_out = (void**)a5;
  161. uint32_t fd_io_uring = syscall(sys_io_uring_setup, entries, setup_params);
  162. uint32_t sq_ring_sz =
  163. setup_params->sq_off.array + setup_params->sq_entries * sizeof(uint32_t);
  164. uint32_t cq_ring_sz = setup_params->cq_off.cqes +
  165. setup_params->cq_entries * SIZEOF_IO_URING_CQE;
  166. uint32_t ring_sz = sq_ring_sz > cq_ring_sz ? sq_ring_sz : cq_ring_sz;
  167. *ring_ptr_out = mmap(vma1, ring_sz, PROT_READ | PROT_WRITE,
  168. MAP_SHARED | MAP_POPULATE | MAP_FIXED, fd_io_uring,
  169. IORING_OFF_SQ_RING);
  170. uint32_t sqes_sz = setup_params->sq_entries * SIZEOF_IO_URING_SQE;
  171. *sqes_ptr_out =
  172. mmap(vma2, sqes_sz, PROT_READ | PROT_WRITE,
  173. MAP_SHARED | MAP_POPULATE | MAP_FIXED, fd_io_uring, IORING_OFF_SQES);
  174. return fd_io_uring;
  175. }
  176. static long syz_io_uring_submit(volatile long a0, volatile long a1,
  177. volatile long a2, volatile long a3)
  178. {
  179. char* ring_ptr = (char*)a0;
  180. char* sqes_ptr = (char*)a1;
  181. char* sqe = (char*)a2;
  182. uint32_t sqes_index = (uint32_t)a3;
  183. uint32_t sq_ring_entries = *(uint32_t*)(ring_ptr + SQ_RING_ENTRIES_OFFSET);
  184. uint32_t cq_ring_entries = *(uint32_t*)(ring_ptr + CQ_RING_ENTRIES_OFFSET);
  185. uint32_t sq_array_off =
  186. (CQ_CQES_OFFSET + cq_ring_entries * SIZEOF_IO_URING_CQE + 63) & ~63;
  187. if (sq_ring_entries)
  188. sqes_index %= sq_ring_entries;
  189. char* sqe_dest = sqes_ptr + sqes_index * SIZEOF_IO_URING_SQE;
  190. memcpy(sqe_dest, sqe, SIZEOF_IO_URING_SQE);
  191. uint32_t sq_ring_mask = *(uint32_t*)(ring_ptr + SQ_RING_MASK_OFFSET);
  192. uint32_t* sq_tail_ptr = (uint32_t*)(ring_ptr + SQ_TAIL_OFFSET);
  193. uint32_t sq_tail = *sq_tail_ptr & sq_ring_mask;
  194. uint32_t sq_tail_next = *sq_tail_ptr + 1;
  195. uint32_t* sq_array = (uint32_t*)(ring_ptr + sq_array_off);
  196. *(sq_array + sq_tail) = sqes_index;
  197. __atomic_store_n(sq_tail_ptr, sq_tail_next, __ATOMIC_RELEASE);
  198. return 0;
  199. }
  200. static void kill_and_wait(int pid, int* status)
  201. {
  202. kill(-pid, SIGKILL);
  203. kill(pid, SIGKILL);
  204. for (int i = 0; i < 100; i++) {
  205. if (waitpid(-1, status, WNOHANG | __WALL) == pid)
  206. return;
  207. usleep(1000);
  208. }
  209. DIR* dir = opendir("/sys/fs/fuse/connections");
  210. if (dir) {
  211. for (;;) {
  212. struct dirent* ent = readdir(dir);
  213. if (!ent)
  214. break;
  215. if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
  216. continue;
  217. char abort[300];
  218. snprintf(abort, sizeof(abort), "/sys/fs/fuse/connections/%s/abort",
  219. ent->d_name);
  220. int fd = open(abort, O_WRONLY);
  221. if (fd == -1) {
  222. continue;
  223. }
  224. if (write(fd, abort, 1) < 0) {
  225. }
  226. close(fd);
  227. }
  228. closedir(dir);
  229. } else {
  230. }
  231. while (waitpid(-1, status, __WALL) != pid) {
  232. }
  233. }
  234. static void setup_test(void)
  235. {
  236. prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
  237. setpgrp();
  238. }
  239. struct thread_t {
  240. int created, call;
  241. event_t ready, done;
  242. };
  243. static struct thread_t threads[16];
  244. static void execute_call(int call);
  245. static int running;
  246. static void* thr(void* arg)
  247. {
  248. struct thread_t* th = (struct thread_t*)arg;
  249. for (;;) {
  250. event_wait(&th->ready);
  251. event_reset(&th->ready);
  252. execute_call(th->call);
  253. __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED);
  254. event_set(&th->done);
  255. }
  256. return 0;
  257. }
  258. static void execute_one(void)
  259. {
  260. int i, call, thread;
  261. for (call = 0; call < 4; call++) {
  262. for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0]));
  263. thread++) {
  264. struct thread_t* th = &threads[thread];
  265. if (!th->created) {
  266. th->created = 1;
  267. event_init(&th->ready);
  268. event_init(&th->done);
  269. event_set(&th->done);
  270. thread_start(thr, th);
  271. }
  272. if (!event_isset(&th->done))
  273. continue;
  274. event_reset(&th->done);
  275. th->call = call;
  276. __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED);
  277. event_set(&th->ready);
  278. event_timedwait(&th->done, 50);
  279. break;
  280. }
  281. }
  282. for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++)
  283. sleep_ms(1);
  284. }
  285. static void execute_one(void);
  286. #define WAIT_FLAGS __WALL
  287. static void loop(void)
  288. {
  289. int iter = 0;
  290. for (; iter < 5000; iter++) {
  291. int pid = fork();
  292. if (pid < 0)
  293. exit(1);
  294. if (pid == 0) {
  295. setup_test();
  296. execute_one();
  297. exit(0);
  298. }
  299. int status = 0;
  300. uint64_t start = current_time_ms();
  301. for (;;) {
  302. if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
  303. break;
  304. sleep_ms(1);
  305. if (current_time_ms() - start < 5000)
  306. continue;
  307. kill_and_wait(pid, &status);
  308. break;
  309. }
  310. }
  311. }
  312. #ifndef __NR_io_uring_enter
  313. #define __NR_io_uring_enter 426
  314. #endif
  315. static uint64_t r[4] = {0xffffffffffffffff, 0xffffffffffffffff, 0x0, 0x0};
  316. void execute_call(int call)
  317. {
  318. intptr_t res = 0;
  319. switch (call) {
  320. case 0:
  321. *(uint64_t*)0x200000c0 = 0;
  322. res = syscall(__NR_signalfd4, -1, 0x200000c0ul, 8ul, 0ul);
  323. if (res != -1)
  324. r[0] = res;
  325. break;
  326. case 1:
  327. *(uint32_t*)0x20000a84 = 0;
  328. *(uint32_t*)0x20000a88 = 0;
  329. *(uint32_t*)0x20000a8c = 0;
  330. *(uint32_t*)0x20000a90 = 0;
  331. *(uint32_t*)0x20000a98 = -1;
  332. memset((void*)0x20000a9c, 0, 12);
  333. res = -1;
  334. res = syz_io_uring_setup(0x87, 0x20000a80, 0x206d6000, 0x206d7000,
  335. 0x20000000, 0x20000040);
  336. if (res != -1) {
  337. r[1] = res;
  338. r[2] = *(uint64_t*)0x20000000;
  339. r[3] = *(uint64_t*)0x20000040;
  340. }
  341. break;
  342. case 2:
  343. *(uint8_t*)0x20002240 = 6;
  344. *(uint8_t*)0x20002241 = 0;
  345. *(uint16_t*)0x20002242 = 0;
  346. *(uint32_t*)0x20002244 = r[0];
  347. *(uint64_t*)0x20002248 = 0;
  348. *(uint64_t*)0x20002250 = 0;
  349. *(uint32_t*)0x20002258 = 0;
  350. *(uint16_t*)0x2000225c = 0;
  351. *(uint16_t*)0x2000225e = 0;
  352. *(uint64_t*)0x20002260 = 0;
  353. *(uint16_t*)0x20002268 = 0;
  354. *(uint16_t*)0x2000226a = 0;
  355. memset((void*)0x2000226c, 0, 20);
  356. syz_io_uring_submit(r[2], r[3], 0x20002240, 0);
  357. break;
  358. case 3:
  359. syscall(__NR_io_uring_enter, r[1], 0x1523a, 0, 0ul, 0ul, 0xaul);
  360. break;
  361. }
  362. }
  363. int main(int argc, char *argv[])
  364. {
  365. void *ret;
  366. #if !defined(__i386) && !defined(__x86_64__)
  367. return 0;
  368. #endif
  369. if (argc > 1)
  370. return 0;
  371. ret = mmap((void *)0x1ffff000ul, 0x1000ul, 0ul, MAP_ANON|MAP_PRIVATE, -1, 0ul);
  372. if (ret == MAP_FAILED)
  373. return 0;
  374. ret = mmap((void *)0x20000000ul, 0x1000000ul, 7ul, MAP_ANON|MAP_PRIVATE, -1, 0ul);
  375. if (ret == MAP_FAILED)
  376. return 0;
  377. ret = mmap((void *)0x21000000ul, 0x1000ul, 0ul, MAP_ANON|MAP_PRIVATE, -1, 0ul);
  378. if (ret == MAP_FAILED)
  379. return 0;
  380. loop();
  381. return 0;
  382. }
  383. #else /* __NR_futex */
  384. int main(int argc, char *argv[])
  385. {
  386. return 0;
  387. }
  388. #endif /* __NR_futex */