Advertisement
xiaoy1

Untitled

May 15th, 2025
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.61 KB | None | 0 0
  1. // autogenerated by syzkaller (https://github.com/google/syzkaller)
  2.  
  3. #define _GNU_SOURCE
  4.  
  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/prctl.h>
  18. #include <sys/stat.h>
  19. #include <sys/syscall.h>
  20. #include <sys/types.h>
  21. #include <sys/wait.h>
  22. #include <time.h>
  23. #include <unistd.h>
  24.  
  25. #include <linux/futex.h>
  26.  
  27. static void sleep_ms(uint64_t ms)
  28. {
  29. usleep(ms * 1000);
  30. }
  31.  
  32. static uint64_t current_time_ms(void)
  33. {
  34. struct timespec ts;
  35. if (clock_gettime(CLOCK_MONOTONIC, &ts))
  36. exit(1);
  37. return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
  38. }
  39.  
  40. static void thread_start(void* (*fn)(void*), void* arg)
  41. {
  42. pthread_t th;
  43. pthread_attr_t attr;
  44. pthread_attr_init(&attr);
  45. pthread_attr_setstacksize(&attr, 128 << 10);
  46. int i = 0;
  47. for (; i < 100; i++) {
  48. if (pthread_create(&th, &attr, fn, arg) == 0) {
  49. pthread_attr_destroy(&attr);
  50. return;
  51. }
  52. if (errno == EAGAIN) {
  53. usleep(50);
  54. continue;
  55. }
  56. break;
  57. }
  58. exit(1);
  59. }
  60.  
  61. typedef struct {
  62. int state;
  63. } event_t;
  64.  
  65. static void event_init(event_t* ev)
  66. {
  67. ev->state = 0;
  68. }
  69.  
  70. static void event_reset(event_t* ev)
  71. {
  72. ev->state = 0;
  73. }
  74.  
  75. static void event_set(event_t* ev)
  76. {
  77. if (ev->state)
  78. exit(1);
  79. __atomic_store_n(&ev->state, 1, __ATOMIC_RELEASE);
  80. syscall(SYS_futex, &ev->state, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1000000);
  81. }
  82.  
  83. static void event_wait(event_t* ev)
  84. {
  85. while (!__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE))
  86. syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, 0);
  87. }
  88.  
  89. static int event_isset(event_t* ev)
  90. {
  91. return __atomic_load_n(&ev->state, __ATOMIC_ACQUIRE);
  92. }
  93.  
  94. static int event_timedwait(event_t* ev, uint64_t timeout)
  95. {
  96. uint64_t start = current_time_ms();
  97. uint64_t now = start;
  98. for (;;) {
  99. uint64_t remain = timeout - (now - start);
  100. struct timespec ts;
  101. ts.tv_sec = remain / 1000;
  102. ts.tv_nsec = (remain % 1000) * 1000 * 1000;
  103. syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, &ts);
  104. if (__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE))
  105. return 1;
  106. now = current_time_ms();
  107. if (now - start > timeout)
  108. return 0;
  109. }
  110. }
  111.  
  112. static bool write_file(const char* file, const char* what, ...)
  113. {
  114. char buf[1024];
  115. va_list args;
  116. va_start(args, what);
  117. vsnprintf(buf, sizeof(buf), what, args);
  118. va_end(args);
  119. buf[sizeof(buf) - 1] = 0;
  120. int len = strlen(buf);
  121. int fd = open(file, O_WRONLY | O_CLOEXEC);
  122. if (fd == -1)
  123. return false;
  124. if (write(fd, buf, len) != len) {
  125. int err = errno;
  126. close(fd);
  127. errno = err;
  128. return false;
  129. }
  130. close(fd);
  131. return true;
  132. }
  133.  
  134. static void kill_and_wait(int pid, int* status)
  135. {
  136. kill(-pid, SIGKILL);
  137. kill(pid, SIGKILL);
  138. for (int i = 0; i < 100; i++) {
  139. if (waitpid(-1, status, WNOHANG | __WALL) == pid)
  140. return;
  141. usleep(1000);
  142. }
  143. DIR* dir = opendir("/sys/fs/fuse/connections");
  144. if (dir) {
  145. for (;;) {
  146. struct dirent* ent = readdir(dir);
  147. if (!ent)
  148. break;
  149. if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
  150. continue;
  151. char abort[300];
  152. snprintf(abort, sizeof(abort), "/sys/fs/fuse/connections/%s/abort",
  153. ent->d_name);
  154. int fd = open(abort, O_WRONLY);
  155. if (fd == -1) {
  156. continue;
  157. }
  158. if (write(fd, abort, 1) < 0) {
  159. }
  160. close(fd);
  161. }
  162. closedir(dir);
  163. } else {
  164. }
  165. while (waitpid(-1, status, __WALL) != pid) {
  166. }
  167. }
  168.  
  169. static void setup_test()
  170. {
  171. prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
  172. setpgrp();
  173. write_file("/proc/self/oom_score_adj", "1000");
  174. }
  175.  
  176. struct thread_t {
  177. int created, call;
  178. event_t ready, done;
  179. };
  180.  
  181. static struct thread_t threads[16];
  182. static void execute_call(int call);
  183. static int running;
  184.  
  185. static void* thr(void* arg)
  186. {
  187. struct thread_t* th = (struct thread_t*)arg;
  188. for (;;) {
  189. event_wait(&th->ready);
  190. event_reset(&th->ready);
  191. execute_call(th->call);
  192. __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED);
  193. event_set(&th->done);
  194. }
  195. return 0;
  196. }
  197.  
  198. static void execute_one(void)
  199. {
  200. if (write(1, "executing program\n", sizeof("executing program\n") - 1)) {
  201. }
  202. int i, call, thread;
  203. for (call = 0; call < 1; call++) {
  204. for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0]));
  205. thread++) {
  206. struct thread_t* th = &threads[thread];
  207. if (!th->created) {
  208. th->created = 1;
  209. event_init(&th->ready);
  210. event_init(&th->done);
  211. event_set(&th->done);
  212. thread_start(thr, th);
  213. }
  214. if (!event_isset(&th->done))
  215. continue;
  216. event_reset(&th->done);
  217. th->call = call;
  218. __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED);
  219. event_set(&th->ready);
  220. event_timedwait(&th->done, 50);
  221. break;
  222. }
  223. }
  224. for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++)
  225. sleep_ms(1);
  226. }
  227.  
  228. static void execute_one(void);
  229.  
  230. #define WAIT_FLAGS __WALL
  231.  
  232. static void loop(void)
  233. {
  234. int iter = 0;
  235. for (;; iter++) {
  236. int pid = fork();
  237. if (pid < 0)
  238. exit(1);
  239. if (pid == 0) {
  240. setup_test();
  241. execute_one();
  242. exit(0);
  243. }
  244. int status = 0;
  245. uint64_t start = current_time_ms();
  246. for (;;) {
  247. sleep_ms(10);
  248. if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
  249. break;
  250. if (current_time_ms() - start < 5000)
  251. continue;
  252. kill_and_wait(pid, &status);
  253. break;
  254. }
  255. }
  256. }
  257.  
  258. void execute_call(int call)
  259. {
  260. switch (call) {
  261. case 0:
  262. syscall(__NR_unshare, /*flags=CLONE_NEWNET*/ 0x40000000ul);
  263. for (int i = 0; i < 64; i++) {
  264. syscall(__NR_unshare, /*flags=CLONE_NEWNET*/ 0x40000000ul);
  265. }
  266. break;
  267. }
  268. }
  269. int main(void)
  270. {
  271. syscall(__NR_mmap, /*addr=*/0x1ffffffff000ul, /*len=*/0x1000ul, /*prot=*/0ul,
  272. /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/-1,
  273. /*offset=*/0ul);
  274. syscall(__NR_mmap, /*addr=*/0x200000000000ul, /*len=*/0x1000000ul,
  275. /*prot=PROT_WRITE|PROT_READ|PROT_EXEC*/ 7ul,
  276. /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/-1,
  277. /*offset=*/0ul);
  278. syscall(__NR_mmap, /*addr=*/0x200001000000ul, /*len=*/0x1000ul, /*prot=*/0ul,
  279. /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/-1,
  280. /*offset=*/0ul);
  281. const char* reason;
  282. (void)reason;
  283. loop();
  284. return 0;
  285. }
  286.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement