zzqq0103

Untitled

Feb 8th, 2025
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.18 KB | None | 0 0
  1. #define _GNU_SOURCE
  2.  
  3. #include <dirent.h>
  4. #include <endian.h>
  5. #include <errno.h>
  6. #include <fcntl.h>
  7. #include <pthread.h>
  8. #include <signal.h>
  9. #include <stdarg.h>
  10. #include <stdbool.h>
  11. #include <stdint.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <sys/prctl.h>
  16. #include <sys/stat.h>
  17. #include <sys/syscall.h>
  18. #include <sys/types.h>
  19. #include <sys/wait.h>
  20. #include <time.h>
  21. #include <unistd.h>
  22.  
  23. #include <linux/futex.h>
  24.  
  25. static unsigned long long procid;
  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. #define BITMASK(bf_off, bf_len) (((1ull << (bf_len)) - 1) << (bf_off))
  62. #define STORE_BY_BITMASK(type, htobe, addr, val, bf_off, bf_len) \
  63. *(type*)(addr) = \
  64. htobe((htobe(*(type*)(addr)) & ~BITMASK((bf_off), (bf_len))) | \
  65. (((type)(val) << (bf_off)) & BITMASK((bf_off), (bf_len))))
  66.  
  67. typedef struct {
  68. int state;
  69. } event_t;
  70.  
  71. static void event_init(event_t* ev)
  72. {
  73. ev->state = 0;
  74. }
  75.  
  76. static void event_reset(event_t* ev)
  77. {
  78. ev->state = 0;
  79. }
  80.  
  81. static void event_set(event_t* ev)
  82. {
  83. if (ev->state)
  84. exit(1);
  85. __atomic_store_n(&ev->state, 1, __ATOMIC_RELEASE);
  86. syscall(SYS_futex, &ev->state, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1000000);
  87. }
  88.  
  89. static void event_wait(event_t* ev)
  90. {
  91. while (!__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE))
  92. syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, 0);
  93. }
  94.  
  95. static int event_isset(event_t* ev)
  96. {
  97. return __atomic_load_n(&ev->state, __ATOMIC_ACQUIRE);
  98. }
  99.  
  100. static int event_timedwait(event_t* ev, uint64_t timeout)
  101. {
  102. uint64_t start = current_time_ms();
  103. uint64_t now = start;
  104. for (;;) {
  105. uint64_t remain = timeout - (now - start);
  106. struct timespec ts;
  107. ts.tv_sec = remain / 1000;
  108. ts.tv_nsec = (remain % 1000) * 1000 * 1000;
  109. syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, &ts);
  110. if (__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE))
  111. return 1;
  112. now = current_time_ms();
  113. if (now - start > timeout)
  114. return 0;
  115. }
  116. }
  117.  
  118. static bool write_file(const char* file, const char* what, ...)
  119. {
  120. char buf[1024];
  121. va_list args;
  122. va_start(args, what);
  123. vsnprintf(buf, sizeof(buf), what, args);
  124. va_end(args);
  125. buf[sizeof(buf) - 1] = 0;
  126. int len = strlen(buf);
  127. int fd = open(file, O_WRONLY | O_CLOEXEC);
  128. if (fd == -1)
  129. return false;
  130. if (write(fd, buf, len) != len) {
  131. int err = errno;
  132. close(fd);
  133. errno = err;
  134. return false;
  135. }
  136. close(fd);
  137. return true;
  138. }
  139.  
  140. static void kill_and_wait(int pid, int* status)
  141. {
  142. kill(-pid, SIGKILL);
  143. kill(pid, SIGKILL);
  144. for (int i = 0; i < 100; i++) {
  145. if (waitpid(-1, status, WNOHANG | __WALL) == pid)
  146. return;
  147. usleep(1000);
  148. }
  149. DIR* dir = opendir("/sys/fs/fuse/connections");
  150. if (dir) {
  151. for (;;) {
  152. struct dirent* ent = readdir(dir);
  153. if (!ent)
  154. break;
  155. if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
  156. continue;
  157. char abort[300];
  158. snprintf(abort, sizeof(abort), "/sys/fs/fuse/connections/%s/abort",
  159. ent->d_name);
  160. int fd = open(abort, O_WRONLY);
  161. if (fd == -1) {
  162. continue;
  163. }
  164. if (write(fd, abort, 1) < 0) {
  165. }
  166. close(fd);
  167. }
  168. closedir(dir);
  169. } else {
  170. }
  171. while (waitpid(-1, status, __WALL) != pid) {
  172. }
  173. }
  174.  
  175. static void setup_test()
  176. {
  177. prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
  178. setpgrp();
  179. write_file("/proc/self/oom_score_adj", "1000");
  180. }
  181.  
  182. struct thread_t {
  183. int created, call;
  184. event_t ready, done;
  185. };
  186.  
  187. static struct thread_t threads[16];
  188. static void execute_call(int call);
  189. static int running;
  190.  
  191. static void* thr(void* arg)
  192. {
  193. struct thread_t* th = (struct thread_t*)arg;
  194. for (;;) {
  195. event_wait(&th->ready);
  196. event_reset(&th->ready);
  197. execute_call(th->call);
  198. __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED);
  199. event_set(&th->done);
  200. }
  201. return 0;
  202. }
  203.  
  204. static void execute_one(void)
  205. {
  206. int i, call, thread;
  207. for (call = 0; call < 4; call++) {
  208. for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0]));
  209. thread++) {
  210. struct thread_t* th = &threads[thread];
  211. if (!th->created) {
  212. th->created = 1;
  213. event_init(&th->ready);
  214. event_init(&th->done);
  215. event_set(&th->done);
  216. thread_start(thr, th);
  217. }
  218. if (!event_isset(&th->done))
  219. continue;
  220. event_reset(&th->done);
  221. th->call = call;
  222. __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED);
  223. event_set(&th->ready);
  224. if (call == 1)
  225. break;
  226. event_timedwait(&th->done, 50);
  227. break;
  228. }
  229. }
  230. for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++)
  231. sleep_ms(1);
  232. }
  233.  
  234. static void execute_one(void);
  235.  
  236. #define WAIT_FLAGS __WALL
  237.  
  238. static void loop(void)
  239. {
  240. int iter = 0;
  241. for (;; iter++) {
  242. int pid = fork();
  243. if (pid < 0)
  244. exit(1);
  245. if (pid == 0) {
  246. setup_test();
  247. execute_one();
  248. exit(0);
  249. }
  250. int status = 0;
  251. uint64_t start = current_time_ms();
  252. for (;;) {
  253. if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
  254. break;
  255. sleep_ms(1);
  256. if (current_time_ms() - start < 5000)
  257. continue;
  258. kill_and_wait(pid, &status);
  259. break;
  260. }
  261. }
  262. }
  263.  
  264. uint64_t r[1] = {0xffffffffffffffff};
  265.  
  266. void execute_call(int call)
  267. {
  268. intptr_t res = 0;
  269. switch (call) {
  270. case 0:
  271. *(uint32_t*)0x20000100 = 0;
  272. *(uint32_t*)0x20000104 = 0x80;
  273. *(uint8_t*)0x20000108 = 0;
  274. *(uint8_t*)0x20000109 = 0;
  275. *(uint8_t*)0x2000010a = 0;
  276. *(uint8_t*)0x2000010b = 0;
  277. *(uint32_t*)0x2000010c = 0;
  278. *(uint64_t*)0x20000110 = 0;
  279. *(uint64_t*)0x20000118 = 0;
  280. *(uint64_t*)0x20000120 = 0;
  281. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 0, 1);
  282. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 1, 1);
  283. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 2, 1);
  284. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 3, 1);
  285. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 4, 1);
  286. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 5, 1);
  287. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 6, 1);
  288. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 7, 1);
  289. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 8, 1);
  290. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 9, 1);
  291. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 10, 1);
  292. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 11, 1);
  293. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 12, 1);
  294. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 13, 1);
  295. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 14, 1);
  296. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 15, 2);
  297. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 17, 1);
  298. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 18, 1);
  299. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 19, 1);
  300. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 20, 1);
  301. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 21, 1);
  302. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 22, 1);
  303. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 23, 1);
  304. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 24, 1);
  305. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 25, 1);
  306. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 26, 1);
  307. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 27, 1);
  308. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 28, 1);
  309. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 29, 1);
  310. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 30, 1);
  311. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 31, 1);
  312. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 32, 1);
  313. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 33, 1);
  314. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 34, 1);
  315. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 35, 1);
  316. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 36, 1);
  317. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 37, 1);
  318. STORE_BY_BITMASK(uint64_t, , 0x20000128, 0, 38, 26);
  319. *(uint32_t*)0x20000130 = 0;
  320. *(uint32_t*)0x20000134 = 0;
  321. *(uint64_t*)0x20000138 = 0;
  322. *(uint64_t*)0x20000140 = 0;
  323. *(uint64_t*)0x20000148 = 0;
  324. *(uint64_t*)0x20000150 = 0;
  325. *(uint32_t*)0x20000158 = 0;
  326. *(uint32_t*)0x2000015c = 0;
  327. *(uint64_t*)0x20000160 = 0;
  328. *(uint32_t*)0x20000168 = 0;
  329. *(uint16_t*)0x2000016c = 0;
  330. *(uint16_t*)0x2000016e = 0;
  331. *(uint32_t*)0x20000170 = 0;
  332. *(uint32_t*)0x20000174 = 0;
  333. *(uint64_t*)0x20000178 = 0;
  334. res = syscall(__NR_perf_event_open, /*attr=*/0x20000100ul, /*pid=*/0,
  335. /*cpu=*/0ul, /*group=*/-1, /*flags=*/0ul);
  336. if (res != -1)
  337. r[0] = res;
  338. break;
  339. case 1:
  340. syscall(__NR_ioctl, /*fd=*/r[0], /*cmd=*/0x2405, /*other=*/r[0]);
  341. break;
  342. case 2:
  343. syscall(__NR_mmap, /*addr=*/0x20ffc000ul, /*len=*/0x1000ul, /*prot=*/0ul,
  344. /*flags=MAP_SHARED_VALIDATE|MAP_FIXED*/ 0x13ul, /*fd=*/r[0],
  345. /*offset=*/0ul);
  346. {
  347. int i;
  348. for (i = 0; i < 64; i++) {
  349. syscall(__NR_mmap, /*addr=*/0x20ffc000ul, /*len=*/0x1000ul,
  350. /*prot=*/0ul, /*flags=MAP_SHARED_VALIDATE|MAP_FIXED*/ 0x13ul,
  351. /*fd=*/r[0], /*offset=*/0ul);
  352. }
  353. }
  354. break;
  355. case 3:
  356. *(uint32_t*)0x20000000 = 2;
  357. *(uint32_t*)0x20000004 = 0x80;
  358. *(uint8_t*)0x20000008 = 0;
  359. *(uint8_t*)0x20000009 = 1;
  360. *(uint8_t*)0x2000000a = 0;
  361. *(uint8_t*)0x2000000b = 0;
  362. *(uint32_t*)0x2000000c = 0;
  363. *(uint64_t*)0x20000010 = 0;
  364. *(uint64_t*)0x20000018 = 0;
  365. *(uint64_t*)0x20000020 = 0;
  366. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 0, 1);
  367. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 1, 1);
  368. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 2, 1);
  369. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 3, 1);
  370. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 4, 1);
  371. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 5, 1);
  372. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 6, 1);
  373. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 7, 1);
  374. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 8, 1);
  375. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 9, 1);
  376. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 10, 1);
  377. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 11, 1);
  378. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 12, 1);
  379. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 13, 1);
  380. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 14, 1);
  381. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 15, 2);
  382. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 17, 1);
  383. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 18, 1);
  384. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 19, 1);
  385. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 20, 1);
  386. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 21, 1);
  387. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 22, 1);
  388. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 23, 1);
  389. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 24, 1);
  390. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 25, 1);
  391. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 26, 1);
  392. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 27, 1);
  393. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 28, 1);
  394. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 29, 1);
  395. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 30, 1);
  396. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 31, 1);
  397. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 32, 1);
  398. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 33, 1);
  399. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 34, 1);
  400. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 35, 1);
  401. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 36, 1);
  402. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 37, 1);
  403. STORE_BY_BITMASK(uint64_t, , 0x20000028, 0, 38, 26);
  404. *(uint32_t*)0x20000030 = 0;
  405. *(uint32_t*)0x20000034 = 0;
  406. *(uint64_t*)0x20000038 = 0;
  407. *(uint64_t*)0x20000040 = 0;
  408. *(uint64_t*)0x20000048 = 0;
  409. *(uint64_t*)0x20000050 = 0;
  410. *(uint32_t*)0x20000058 = 0;
  411. *(uint32_t*)0x2000005c = 0;
  412. *(uint64_t*)0x20000060 = 0;
  413. *(uint32_t*)0x20000068 = 0;
  414. *(uint16_t*)0x2000006c = 0;
  415. *(uint16_t*)0x2000006e = 0;
  416. *(uint32_t*)0x20000070 = 0x800;
  417. *(uint32_t*)0x20000074 = 0;
  418. *(uint64_t*)0x20000078 = 0;
  419. syscall(__NR_perf_event_open, /*attr=*/0x20000000ul, /*pid=*/0, /*cpu=*/0ul,
  420. /*group=*/r[0],
  421. /*flags=PERF_FLAG_FD_CLOEXEC|PERF_FLAG_FD_OUTPUT*/ 0xaul);
  422. break;
  423. }
  424. }
  425. int main(void)
  426. {
  427. syscall(__NR_mmap, /*addr=*/0x1ffff000ul, /*len=*/0x1000ul, /*prot=*/0ul,
  428. /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/-1,
  429. /*offset=*/0ul);
  430. syscall(__NR_mmap, /*addr=*/0x20000000ul, /*len=*/0x1000000ul,
  431. /*prot=PROT_WRITE|PROT_READ|PROT_EXEC*/ 7ul,
  432. /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/-1,
  433. /*offset=*/0ul);
  434. syscall(__NR_mmap, /*addr=*/0x21000000ul, /*len=*/0x1000ul, /*prot=*/0ul,
  435. /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/-1,
  436. /*offset=*/0ul);
  437. for (procid = 0; procid < 6; procid++) {
  438. if (fork() == 0) {
  439. loop();
  440. }
  441. }
  442. sleep(1000000);
  443. return 0;
  444. }
  445.  
Advertisement
Add Comment
Please, Sign In to add comment