Tang-ZW

Untitled

Feb 3rd, 2026
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.83 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 <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/mman.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. #ifndef __NR_io_uring_setup
  26. #define __NR_io_uring_setup 425
  27. #endif
  28.  
  29. static void sleep_ms(uint64_t ms)
  30. {
  31. usleep(ms * 1000);
  32. }
  33.  
  34. static uint64_t current_time_ms(void)
  35. {
  36. struct timespec ts;
  37. if (clock_gettime(CLOCK_MONOTONIC, &ts))
  38. exit(1);
  39. return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
  40. }
  41.  
  42. static bool write_file(const char* file, const char* what, ...)
  43. {
  44. char buf[1024];
  45. va_list args;
  46. va_start(args, what);
  47. vsnprintf(buf, sizeof(buf), what, args);
  48. va_end(args);
  49. buf[sizeof(buf) - 1] = 0;
  50. int len = strlen(buf);
  51. int fd = open(file, O_WRONLY | O_CLOEXEC);
  52. if (fd == -1)
  53. return false;
  54. if (write(fd, buf, len) != len) {
  55. int err = errno;
  56. close(fd);
  57. errno = err;
  58. return false;
  59. }
  60. close(fd);
  61. return true;
  62. }
  63.  
  64. #define SIZEOF_IO_URING_SQE 64
  65. #define SIZEOF_IO_URING_CQE 16
  66. #define SQ_HEAD_OFFSET 0
  67. #define SQ_TAIL_OFFSET 64
  68. #define SQ_RING_MASK_OFFSET 256
  69. #define SQ_RING_ENTRIES_OFFSET 264
  70. #define SQ_FLAGS_OFFSET 276
  71. #define SQ_DROPPED_OFFSET 272
  72. #define CQ_HEAD_OFFSET 128
  73. #define CQ_TAIL_OFFSET 192
  74. #define CQ_RING_MASK_OFFSET 260
  75. #define CQ_RING_ENTRIES_OFFSET 268
  76. #define CQ_RING_OVERFLOW_OFFSET 284
  77. #define CQ_FLAGS_OFFSET 280
  78. #define CQ_CQES_OFFSET 320
  79.  
  80. struct io_sqring_offsets {
  81. uint32_t head;
  82. uint32_t tail;
  83. uint32_t ring_mask;
  84. uint32_t ring_entries;
  85. uint32_t flags;
  86. uint32_t dropped;
  87. uint32_t array;
  88. uint32_t resv1;
  89. uint64_t resv2;
  90. };
  91.  
  92. struct io_cqring_offsets {
  93. uint32_t head;
  94. uint32_t tail;
  95. uint32_t ring_mask;
  96. uint32_t ring_entries;
  97. uint32_t overflow;
  98. uint32_t cqes;
  99. uint64_t resv[2];
  100. };
  101.  
  102. struct io_uring_params {
  103. uint32_t sq_entries;
  104. uint32_t cq_entries;
  105. uint32_t flags;
  106. uint32_t sq_thread_cpu;
  107. uint32_t sq_thread_idle;
  108. uint32_t features;
  109. uint32_t resv[4];
  110. struct io_sqring_offsets sq_off;
  111. struct io_cqring_offsets cq_off;
  112. };
  113.  
  114. #define IORING_OFF_SQ_RING 0
  115. #define IORING_OFF_SQES 0x10000000ULL
  116. #define IORING_SETUP_SQE128 (1U << 10)
  117. #define IORING_SETUP_CQE32 (1U << 11)
  118.  
  119. static long syz_io_uring_setup(volatile long a0, volatile long a1,
  120. volatile long a2, volatile long a3)
  121. {
  122. uint32_t entries = (uint32_t)a0;
  123. struct io_uring_params* setup_params = (struct io_uring_params*)a1;
  124. void** ring_ptr_out = (void**)a2;
  125. void** sqes_ptr_out = (void**)a3;
  126. setup_params->flags &= ~(IORING_SETUP_CQE32 | IORING_SETUP_SQE128);
  127. uint32_t fd_io_uring = syscall(__NR_io_uring_setup, entries, setup_params);
  128. uint32_t sq_ring_sz =
  129. setup_params->sq_off.array + setup_params->sq_entries * sizeof(uint32_t);
  130. uint32_t cq_ring_sz = setup_params->cq_off.cqes +
  131. setup_params->cq_entries * SIZEOF_IO_URING_CQE;
  132. uint32_t ring_sz = sq_ring_sz > cq_ring_sz ? sq_ring_sz : cq_ring_sz;
  133. *ring_ptr_out =
  134. mmap(0, ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE,
  135. fd_io_uring, IORING_OFF_SQ_RING);
  136. uint32_t sqes_sz = setup_params->sq_entries * SIZEOF_IO_URING_SQE;
  137. *sqes_ptr_out = mmap(0, sqes_sz, PROT_READ | PROT_WRITE,
  138. MAP_SHARED | MAP_POPULATE, fd_io_uring, IORING_OFF_SQES);
  139. uint32_t* array =
  140. (uint32_t*)((uintptr_t)*ring_ptr_out + setup_params->sq_off.array);
  141. for (uint32_t index = 0; index < entries; index++)
  142. array[index] = index;
  143. return fd_io_uring;
  144. }
  145.  
  146. static long syz_open_procfs(volatile long a0, volatile long a1)
  147. {
  148. char buf[128];
  149. memset(buf, 0, sizeof(buf));
  150. if (a0 == 0) {
  151. snprintf(buf, sizeof(buf), "/proc/self/%s", (char*)a1);
  152. } else if (a0 == -1) {
  153. snprintf(buf, sizeof(buf), "/proc/thread-self/%s", (char*)a1);
  154. } else {
  155. snprintf(buf, sizeof(buf), "/proc/self/task/%d/%s", (int)a0, (char*)a1);
  156. }
  157. int fd = open(buf, O_RDWR);
  158. if (fd == -1)
  159. fd = open(buf, O_RDONLY);
  160. return fd;
  161. }
  162.  
  163. static void kill_and_wait(int pid, int* status)
  164. {
  165. kill(-pid, SIGKILL);
  166. kill(pid, SIGKILL);
  167. for (int i = 0; i < 100; i++) {
  168. if (waitpid(-1, status, WNOHANG | __WALL) == pid)
  169. return;
  170. usleep(1000);
  171. }
  172. DIR* dir = opendir("/sys/fs/fuse/connections");
  173. if (dir) {
  174. for (;;) {
  175. struct dirent* ent = readdir(dir);
  176. if (!ent)
  177. break;
  178. if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
  179. continue;
  180. char abort[300];
  181. snprintf(abort, sizeof(abort), "/sys/fs/fuse/connections/%s/abort",
  182. ent->d_name);
  183. int fd = open(abort, O_WRONLY);
  184. if (fd == -1) {
  185. continue;
  186. }
  187. if (write(fd, abort, 1) < 0) {
  188. }
  189. close(fd);
  190. }
  191. closedir(dir);
  192. } else {
  193. }
  194. while (waitpid(-1, status, __WALL) != pid) {
  195. }
  196. }
  197.  
  198. static void setup_test()
  199. {
  200. prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
  201. setpgrp();
  202. write_file("/proc/self/oom_score_adj", "1000");
  203. }
  204.  
  205. static void execute_one(void);
  206.  
  207. #define WAIT_FLAGS __WALL
  208.  
  209. static void loop(void)
  210. {
  211. int iter = 0;
  212. for (;; iter++) {
  213. int pid = fork();
  214. if (pid < 0)
  215. exit(1);
  216. if (pid == 0) {
  217. setup_test();
  218. execute_one();
  219. exit(0);
  220. }
  221. int status = 0;
  222. uint64_t start = current_time_ms();
  223. for (;;) {
  224. sleep_ms(10);
  225. if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
  226. break;
  227. if (current_time_ms() - start < 5000)
  228. continue;
  229. kill_and_wait(pid, &status);
  230. break;
  231. }
  232. }
  233. }
  234.  
  235. uint64_t r[1] = {0xffffffffffffffff};
  236.  
  237. void execute_one(void)
  238. {
  239. intptr_t res = 0;
  240. if (write(1, "executing program\n", sizeof("executing program\n") - 1)) {
  241. }
  242. // mbind arguments: [
  243. // addr: VMA[0x2000]
  244. // len: len = 0x2000 (8 bytes)
  245. // mode: mbind_mode = 0x2 (8 bytes)
  246. // nodemask: ptr[in, int64] {
  247. // int64 = 0x3 (8 bytes)
  248. // }
  249. // maxnode: intptr = 0x9 (8 bytes)
  250. // flags: mbind_flags = 0x0 (8 bytes)
  251. // ]
  252. *(uint64_t*)0x200000000040 = 3;
  253. syscall(__NR_mbind, /*addr=*/0x200000960000ul, /*len=*/0x2000ul,
  254. /*mode=MPOL_BIND*/ 2ul, /*nodemask=*/0x200000000040ul,
  255. /*maxnode=*/9ul, /*flags=*/0ul);
  256. // syz_io_uring_setup arguments: [
  257. // entries: int32 = 0x30c2 (4 bytes)
  258. // params: ptr[inout, io_uring_params] {
  259. // io_uring_params {
  260. // sq_entries: int32 = 0x0 (4 bytes)
  261. // cq_entries: int32 = 0x0 (4 bytes)
  262. // flags: io_uring_setup_flags = 0x13250 (4 bytes)
  263. // sq_thread_cpu: int32 = 0x0 (4 bytes)
  264. // sq_thread_idle: int32 = 0x0 (4 bytes)
  265. // features: int32 = 0x0 (4 bytes)
  266. // wq_fd: fd_io_uring (resource)
  267. // resv: buffer: {00 00 00 00 00 00 00 00 00 00 00 00} (length 0xc)
  268. // sq_off: array[int32] {
  269. // int32 = 0x0 (4 bytes)
  270. // int32 = 0x0 (4 bytes)
  271. // int32 = 0x0 (4 bytes)
  272. // int32 = 0x0 (4 bytes)
  273. // int32 = 0x0 (4 bytes)
  274. // int32 = 0x0 (4 bytes)
  275. // int32 = 0x0 (4 bytes)
  276. // int32 = 0x0 (4 bytes)
  277. // int32 = 0x0 (4 bytes)
  278. // int32 = 0x0 (4 bytes)
  279. // }
  280. // cq_off: array[int32] {
  281. // int32 = 0x0 (4 bytes)
  282. // int32 = 0x0 (4 bytes)
  283. // int32 = 0x0 (4 bytes)
  284. // int32 = 0x0 (4 bytes)
  285. // int32 = 0x0 (4 bytes)
  286. // int32 = 0x0 (4 bytes)
  287. // int32 = 0x0 (4 bytes)
  288. // int32 = 0x0 (4 bytes)
  289. // int32 = 0x0 (4 bytes)
  290. // int32 = 0x0 (4 bytes)
  291. // }
  292. // }
  293. // }
  294. // ring_ptr: ptr[out, ring_ptr] {
  295. // ring_ptr (resource)
  296. // }
  297. // sqes_ptr: ptr[out, sqes_ptr] {
  298. // sqes_ptr (resource)
  299. // }
  300. // ]
  301. // returns fd_io_uring
  302. *(uint32_t*)0x200000000004 = 0;
  303. *(uint32_t*)0x200000000008 = 0x13250;
  304. *(uint32_t*)0x20000000000c = 0;
  305. *(uint32_t*)0x200000000010 = 0;
  306. *(uint32_t*)0x200000000018 = -1;
  307. memset((void*)0x20000000001c, 0, 12);
  308. syz_io_uring_setup(/*entries=*/0x30c2, /*params=*/0x200000000000,
  309. /*ring_ptr=*/0x200000000080, /*sqes_ptr=*/0x2000000000c0);
  310. // syz_open_procfs arguments: [
  311. // pid: pid (resource)
  312. // file: ptr[in, buffer] {
  313. // buffer: {66 64 69 6e 66 6f 2f 33 00} (length 0x9)
  314. // }
  315. // ]
  316. // returns fd
  317. memcpy((void*)0x200000000000, "fdinfo/3\000", 9);
  318. res = -1;
  319. res = syz_open_procfs(/*pid=*/-1, /*file=*/0x200000000000);
  320. if (res != -1)
  321. r[0] = res;
  322. // read$char_usb arguments: [
  323. // fd: fd_char_usb (resource)
  324. // buf: ptr[out, buffer] {
  325. // buffer: (DirOut)
  326. // }
  327. // count: len = 0xf1 (8 bytes)
  328. // ]
  329. syscall(__NR_read, /*fd=*/r[0], /*buf=*/0x200000000040ul, /*count=*/0xf1ul);
  330. }
  331. int main(void)
  332. {
  333. syscall(__NR_mmap, /*addr=*/0x1ffffffff000ul, /*len=*/0x1000ul, /*prot=*/0ul,
  334. /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul,
  335. /*fd=*/(intptr_t)-1, /*offset=*/0ul);
  336. syscall(__NR_mmap, /*addr=*/0x200000000000ul, /*len=*/0x1000000ul,
  337. /*prot=PROT_WRITE|PROT_READ|PROT_EXEC*/ 7ul,
  338. /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul,
  339. /*fd=*/(intptr_t)-1, /*offset=*/0ul);
  340. syscall(__NR_mmap, /*addr=*/0x200001000000ul, /*len=*/0x1000ul, /*prot=*/0ul,
  341. /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul,
  342. /*fd=*/(intptr_t)-1, /*offset=*/0ul);
  343. const char* reason;
  344. (void)reason;
  345. loop();
  346. return 0;
  347. }
  348.  
Advertisement
Add Comment
Please, Sign In to add comment