35fa71a030ca.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. // autogenerated by syzkaller (https://github.com/google/syzkaller)
  4. #include <dirent.h>
  5. #include <endian.h>
  6. #include <errno.h>
  7. #include <fcntl.h>
  8. #include <pthread.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/prctl.h>
  17. #include <sys/stat.h>
  18. #include <sys/syscall.h>
  19. #include <sys/types.h>
  20. #include <sys/wait.h>
  21. #include <time.h>
  22. #include <unistd.h>
  23. #include <sys/mman.h>
  24. #include <linux/futex.h>
  25. #include "liburing.h"
  26. #include "helpers.h"
  27. #include "../src/syscall.h"
  28. #if !defined(SYS_futex) && defined(SYS_futex_time64)
  29. # define SYS_futex SYS_futex_time64
  30. #endif
  31. static void sleep_ms(uint64_t ms)
  32. {
  33. usleep(ms * 1000);
  34. }
  35. static uint64_t current_time_ms(void)
  36. {
  37. struct timespec ts;
  38. if (clock_gettime(CLOCK_MONOTONIC, &ts))
  39. exit(1);
  40. return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
  41. }
  42. static void thread_start(void* (*fn)(void*), void* arg)
  43. {
  44. pthread_t th;
  45. pthread_attr_t attr;
  46. pthread_attr_init(&attr);
  47. pthread_attr_setstacksize(&attr, 128 << 10);
  48. int i;
  49. for (i = 0; i < 100; i++) {
  50. if (pthread_create(&th, &attr, fn, arg) == 0) {
  51. pthread_attr_destroy(&attr);
  52. return;
  53. }
  54. if (errno == EAGAIN) {
  55. usleep(50);
  56. continue;
  57. }
  58. break;
  59. }
  60. exit(1);
  61. }
  62. typedef struct {
  63. int state;
  64. } event_t;
  65. static void event_init(event_t* ev)
  66. {
  67. ev->state = 0;
  68. }
  69. static void event_reset(event_t* ev)
  70. {
  71. ev->state = 0;
  72. }
  73. static void event_set(event_t* ev)
  74. {
  75. if (ev->state)
  76. exit(1);
  77. __atomic_store_n(&ev->state, 1, __ATOMIC_RELEASE);
  78. syscall(SYS_futex, &ev->state, FUTEX_WAKE | FUTEX_PRIVATE_FLAG);
  79. }
  80. static void event_wait(event_t* ev)
  81. {
  82. while (!__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE))
  83. syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, 0);
  84. }
  85. static int event_isset(event_t* ev)
  86. {
  87. return __atomic_load_n(&ev->state, __ATOMIC_ACQUIRE);
  88. }
  89. static int event_timedwait(event_t* ev, uint64_t timeout)
  90. {
  91. uint64_t start = current_time_ms();
  92. uint64_t now = start;
  93. for (;;) {
  94. uint64_t remain = timeout - (now - start);
  95. struct timespec ts;
  96. ts.tv_sec = remain / 1000;
  97. ts.tv_nsec = (remain % 1000) * 1000 * 1000;
  98. syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, &ts);
  99. if (__atomic_load_n(&ev->state, __ATOMIC_RELAXED))
  100. return 1;
  101. now = current_time_ms();
  102. if (now - start > timeout)
  103. return 0;
  104. }
  105. }
  106. static bool write_file(const char* file, const char* what, ...)
  107. {
  108. char buf[1024];
  109. va_list args;
  110. va_start(args, what);
  111. vsnprintf(buf, sizeof(buf), what, args);
  112. va_end(args);
  113. buf[sizeof(buf) - 1] = 0;
  114. int len = strlen(buf);
  115. int fd = open(file, O_WRONLY | O_CLOEXEC);
  116. if (fd == -1)
  117. return false;
  118. if (write(fd, buf, len) != len) {
  119. int err = errno;
  120. close(fd);
  121. errno = err;
  122. return false;
  123. }
  124. close(fd);
  125. return true;
  126. }
  127. static void kill_and_wait(int pid, int* status)
  128. {
  129. kill(-pid, SIGKILL);
  130. kill(pid, SIGKILL);
  131. int i;
  132. for (i = 0; i < 100; i++) {
  133. if (waitpid(-1, status, WNOHANG | __WALL) == pid)
  134. return;
  135. usleep(1000);
  136. }
  137. DIR* dir = opendir("/sys/fs/fuse/connections");
  138. if (dir) {
  139. for (;;) {
  140. struct dirent* ent = readdir(dir);
  141. if (!ent)
  142. break;
  143. if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
  144. continue;
  145. char abort[300];
  146. snprintf(abort, sizeof(abort), "/sys/fs/fuse/connections/%s/abort",
  147. ent->d_name);
  148. int fd = open(abort, O_WRONLY);
  149. if (fd == -1) {
  150. continue;
  151. }
  152. if (write(fd, abort, 1) < 0) {
  153. }
  154. close(fd);
  155. }
  156. closedir(dir);
  157. } else {
  158. }
  159. while (waitpid(-1, status, __WALL) != pid) {
  160. }
  161. }
  162. #define SYZ_HAVE_SETUP_TEST 1
  163. static void setup_test(void)
  164. {
  165. prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
  166. setpgrp();
  167. write_file("/proc/self/oom_score_adj", "1000");
  168. }
  169. struct thread_t {
  170. int created, call;
  171. event_t ready, done;
  172. };
  173. static struct thread_t threads[16];
  174. static void execute_call(int call);
  175. static int running;
  176. static void* thr(void* arg)
  177. {
  178. struct thread_t* th = (struct thread_t*)arg;
  179. for (;;) {
  180. event_wait(&th->ready);
  181. event_reset(&th->ready);
  182. execute_call(th->call);
  183. __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED);
  184. event_set(&th->done);
  185. }
  186. return 0;
  187. }
  188. static void execute_one(void)
  189. {
  190. int i, call, thread;
  191. for (call = 0; call < 3; call++) {
  192. for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0]));
  193. thread++) {
  194. struct thread_t* th = &threads[thread];
  195. if (!th->created) {
  196. th->created = 1;
  197. event_init(&th->ready);
  198. event_init(&th->done);
  199. event_set(&th->done);
  200. thread_start(thr, th);
  201. }
  202. if (!event_isset(&th->done))
  203. continue;
  204. event_reset(&th->done);
  205. th->call = call;
  206. __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED);
  207. event_set(&th->ready);
  208. event_timedwait(&th->done, 45);
  209. break;
  210. }
  211. }
  212. for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++)
  213. sleep_ms(1);
  214. }
  215. static void execute_one(void);
  216. #define WAIT_FLAGS __WALL
  217. static void loop(void)
  218. {
  219. for (;;) {
  220. int pid = fork();
  221. if (pid < 0)
  222. exit(1);
  223. if (pid == 0) {
  224. setup_test();
  225. execute_one();
  226. exit(0);
  227. }
  228. int status = 0;
  229. uint64_t start = current_time_ms();
  230. for (;;) {
  231. if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
  232. break;
  233. sleep_ms(1);
  234. if (current_time_ms() - start < 5 * 1000)
  235. continue;
  236. kill_and_wait(pid, &status);
  237. break;
  238. }
  239. }
  240. }
  241. static uint64_t r[1] = {0xffffffffffffffff};
  242. void execute_call(int call)
  243. {
  244. long res;
  245. switch (call) {
  246. case 0:
  247. *(uint32_t*)0x20000040 = 0;
  248. *(uint32_t*)0x20000044 = 0;
  249. *(uint32_t*)0x20000048 = 0;
  250. *(uint32_t*)0x2000004c = 0;
  251. *(uint32_t*)0x20000050 = 0;
  252. *(uint32_t*)0x20000054 = 0;
  253. *(uint32_t*)0x20000058 = 0;
  254. *(uint32_t*)0x2000005c = 0;
  255. *(uint32_t*)0x20000060 = 0;
  256. *(uint32_t*)0x20000064 = 0;
  257. *(uint32_t*)0x20000068 = 0;
  258. *(uint32_t*)0x2000006c = 0;
  259. *(uint32_t*)0x20000070 = 0;
  260. *(uint32_t*)0x20000074 = 0;
  261. *(uint32_t*)0x20000078 = 0;
  262. *(uint32_t*)0x2000007c = 0;
  263. *(uint32_t*)0x20000080 = 0;
  264. *(uint32_t*)0x20000084 = 0;
  265. *(uint64_t*)0x20000088 = 0;
  266. *(uint32_t*)0x20000090 = 0;
  267. *(uint32_t*)0x20000094 = 0;
  268. *(uint32_t*)0x20000098 = 0;
  269. *(uint32_t*)0x2000009c = 0;
  270. *(uint32_t*)0x200000a0 = 0;
  271. *(uint32_t*)0x200000a4 = 0;
  272. *(uint32_t*)0x200000a8 = 0;
  273. *(uint32_t*)0x200000ac = 0;
  274. *(uint64_t*)0x200000b0 = 0;
  275. res = __sys_io_uring_setup(0x64, (struct io_uring_params *) 0x20000040UL);
  276. if (res != -1)
  277. r[0] = res;
  278. break;
  279. case 1:
  280. __sys_io_uring_register((long)r[0], 0, 0, 0);
  281. break;
  282. case 2:
  283. __sys_io_uring_register((long)r[0], 0, 0, 0);
  284. break;
  285. }
  286. }
  287. static void sig_int(int sig)
  288. {
  289. exit(0);
  290. }
  291. int main(int argc, char *argv[])
  292. {
  293. if (argc > 1)
  294. return T_EXIT_SKIP;
  295. signal(SIGINT, sig_int);
  296. mmap((void *) 0x20000000, 0x1000000, 3, MAP_ANON|MAP_PRIVATE, -1, 0);
  297. signal(SIGALRM, sig_int);
  298. alarm(5);
  299. loop();
  300. return T_EXIT_PASS;
  301. }