35fa71a030ca.c 7.2 KB

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