Tang-ZW

Untitled

Mar 2nd, 2026
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.00 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/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.  
  24. static void sleep_ms(uint64_t ms)
  25. {
  26. usleep(ms * 1000);
  27. }
  28.  
  29. static uint64_t current_time_ms(void)
  30. {
  31. struct timespec ts;
  32. if (clock_gettime(CLOCK_MONOTONIC, &ts))
  33. exit(1);
  34. return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
  35. }
  36.  
  37. #define BITMASK(bf_off, bf_len) (((1ull << (bf_len)) - 1) << (bf_off))
  38. #define STORE_BY_BITMASK(type, htobe, addr, val, bf_off, bf_len) \
  39. *(type*)(addr) = \
  40. htobe((htobe(*(type*)(addr)) & ~BITMASK((bf_off), (bf_len))) | \
  41. (((type)(val) << (bf_off)) & BITMASK((bf_off), (bf_len))))
  42.  
  43. static bool write_file(const char* file, const char* what, ...)
  44. {
  45. char buf[1024];
  46. va_list args;
  47. va_start(args, what);
  48. vsnprintf(buf, sizeof(buf), what, args);
  49. va_end(args);
  50. buf[sizeof(buf) - 1] = 0;
  51. int len = strlen(buf);
  52. int fd = open(file, O_WRONLY | O_CLOEXEC);
  53. if (fd == -1)
  54. return false;
  55. if (write(fd, buf, len) != len) {
  56. int err = errno;
  57. close(fd);
  58. errno = err;
  59. return false;
  60. }
  61. close(fd);
  62. return true;
  63. }
  64.  
  65. static int inject_fault(int nth)
  66. {
  67. int fd;
  68. fd = open("/proc/thread-self/fail-nth", O_RDWR);
  69. if (fd == -1)
  70. exit(1);
  71. char buf[16];
  72. sprintf(buf, "%d", nth);
  73. if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf))
  74. exit(1);
  75. return fd;
  76. }
  77.  
  78. static void kill_and_wait(int pid, int* status)
  79. {
  80. kill(-pid, SIGKILL);
  81. kill(pid, SIGKILL);
  82. for (int i = 0; i < 100; i++) {
  83. if (waitpid(-1, status, WNOHANG | __WALL) == pid)
  84. return;
  85. usleep(1000);
  86. }
  87. DIR* dir = opendir("/sys/fs/fuse/connections");
  88. if (dir) {
  89. for (;;) {
  90. struct dirent* ent = readdir(dir);
  91. if (!ent)
  92. break;
  93. if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
  94. continue;
  95. char abort[300];
  96. snprintf(abort, sizeof(abort), "/sys/fs/fuse/connections/%s/abort",
  97. ent->d_name);
  98. int fd = open(abort, O_WRONLY);
  99. if (fd == -1) {
  100. continue;
  101. }
  102. if (write(fd, abort, 1) < 0) {
  103. }
  104. close(fd);
  105. }
  106. closedir(dir);
  107. } else {
  108. }
  109. while (waitpid(-1, status, __WALL) != pid) {
  110. }
  111. }
  112.  
  113. static void setup_test()
  114. {
  115. prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
  116. setpgrp();
  117. write_file("/proc/self/oom_score_adj", "1000");
  118. }
  119.  
  120. static const char* setup_fault()
  121. {
  122. int fd = open("/proc/self/make-it-fail", O_WRONLY);
  123. if (fd == -1)
  124. return "CONFIG_FAULT_INJECTION is not enabled";
  125. close(fd);
  126. fd = open("/proc/thread-self/fail-nth", O_WRONLY);
  127. if (fd == -1)
  128. return "kernel does not have systematic fault injection support";
  129. close(fd);
  130. static struct {
  131. const char* file;
  132. const char* val;
  133. bool fatal;
  134. } files[] = {
  135. {"/sys/kernel/debug/failslab/ignore-gfp-wait", "N", true},
  136. {"/sys/kernel/debug/fail_futex/ignore-private", "N", false},
  137. {"/sys/kernel/debug/fail_page_alloc/ignore-gfp-highmem", "N", false},
  138. {"/sys/kernel/debug/fail_page_alloc/ignore-gfp-wait", "N", false},
  139. {"/sys/kernel/debug/fail_page_alloc/min-order", "0", false},
  140. };
  141. unsigned i;
  142. for (i = 0; i < sizeof(files) / sizeof(files[0]); i++) {
  143. if (!write_file(files[i].file, files[i].val)) {
  144. if (files[i].fatal)
  145. return "failed to write fault injection file";
  146. }
  147. }
  148. return NULL;
  149. }
  150.  
  151. static void execute_one(void);
  152.  
  153. #define WAIT_FLAGS __WALL
  154.  
  155. static void loop(void)
  156. {
  157. int iter = 0;
  158. for (;; iter++) {
  159. int pid = fork();
  160. if (pid < 0)
  161. exit(1);
  162. if (pid == 0) {
  163. setup_test();
  164. execute_one();
  165. exit(0);
  166. }
  167. int status = 0;
  168. uint64_t start = current_time_ms();
  169. for (;;) {
  170. sleep_ms(10);
  171. if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
  172. break;
  173. if (current_time_ms() - start < 5000)
  174. continue;
  175. kill_and_wait(pid, &status);
  176. break;
  177. }
  178. }
  179. }
  180.  
  181. void execute_one(void)
  182. {
  183. if (write(1, "executing program\n", sizeof("executing program\n") - 1)) {
  184. }
  185. // perf_event_open$cgroup arguments: [
  186. // attr: ptr[in, perf_event_attr] {
  187. // perf_event_attr {
  188. // type: perf_event_type = 0x8 (4 bytes)
  189. // size: len = 0x80 (4 bytes)
  190. // config0: int8 = 0x0 (1 bytes)
  191. // config1: int8 = 0x0 (1 bytes)
  192. // config2: int8 = 0x0 (1 bytes)
  193. // config3: int8 = 0x0 (1 bytes)
  194. // config4: const = 0x0 (4 bytes)
  195. // sample_freq: int64 = 0x0 (8 bytes)
  196. // sample_type: perf_sample_type = 0x0 (8 bytes)
  197. // read_format: perf_read_format = 0x9 (8 bytes)
  198. // disabled: int64 = 0x0 (0 bytes)
  199. // inherit: int64 = 0x0 (0 bytes)
  200. // pinned: int64 = 0x0 (0 bytes)
  201. // exclusive: int64 = 0x0 (0 bytes)
  202. // exclude_user: int64 = 0x0 (0 bytes)
  203. // exclude_kernel: int64 = 0x0 (0 bytes)
  204. // exclude_hv: int64 = 0x0 (0 bytes)
  205. // exclude_idle: int64 = 0x0 (0 bytes)
  206. // mmap: int64 = 0x0 (0 bytes)
  207. // comm: int64 = 0x0 (0 bytes)
  208. // freq: int64 = 0x0 (0 bytes)
  209. // inherit_stat: int64 = 0x0 (0 bytes)
  210. // enable_on_exec: int64 = 0x0 (0 bytes)
  211. // task: int64 = 0x0 (0 bytes)
  212. // watermark: int64 = 0x0 (0 bytes)
  213. // precise_ip: int64 = 0x0 (0 bytes)
  214. // mmap_data: int64 = 0x0 (0 bytes)
  215. // sample_id_all: int64 = 0x0 (0 bytes)
  216. // exclude_host: int64 = 0x0 (0 bytes)
  217. // exclude_guest: int64 = 0x0 (0 bytes)
  218. // exclude_callchain_kernel: int64 = 0x0 (0 bytes)
  219. // exclude_callchain_user: int64 = 0x0 (0 bytes)
  220. // mmap2: int64 = 0x0 (0 bytes)
  221. // comm_exec: int64 = 0x0 (0 bytes)
  222. // use_clockid: int64 = 0x0 (0 bytes)
  223. // context_switch: int64 = 0x0 (0 bytes)
  224. // write_backward: int64 = 0x0 (0 bytes)
  225. // namespaces: int64 = 0x0 (0 bytes)
  226. // ksymbol: int64 = 0x0 (0 bytes)
  227. // bpf_event: int64 = 0x0 (0 bytes)
  228. // aux_output: int64 = 0x0 (0 bytes)
  229. // cgroup: int64 = 0x0 (0 bytes)
  230. // text_poke: int64 = 0x0 (0 bytes)
  231. // build_id: int64 = 0x0 (0 bytes)
  232. // inherit_thread: int64 = 0x0 (0 bytes)
  233. // remove_on_exec: int64 = 0x0 (0 bytes)
  234. // sigtrap: int64 = 0x0 (0 bytes)
  235. // __reserved_1: const = 0x0 (8 bytes)
  236. // wakeup_events: int32 = 0x0 (4 bytes)
  237. // bp_type: perf_bp_type = 0x0 (4 bytes)
  238. // bp_config: union perf_bp_config {
  239. // perf_bp: perf_bp {
  240. // bp_addr: nil
  241. // bp_len: perf_bp_lens = 0xffffffff81200004 (8 bytes)
  242. // }
  243. // }
  244. // branch_sample_type: perf_branch_sample_type = 0x0 (8 bytes)
  245. // sample_regs_user: int64 = 0x9f7 (8 bytes)
  246. // sample_stack_user: int32 = 0x0 (4 bytes)
  247. // clockid: clock_type = 0x0 (4 bytes)
  248. // sample_regs_intr: int64 = 0x0 (8 bytes)
  249. // aux_watermark: int32 = 0x0 (4 bytes)
  250. // sample_max_stack: int16 = 0x0 (2 bytes)
  251. // __reserved_2: const = 0x0 (2 bytes)
  252. // aux_sample_size: int32 = 0x0 (4 bytes)
  253. // __reserved_3: const = 0x0 (4 bytes)
  254. // sig_data: int64 = 0x4003 (8 bytes)
  255. // }
  256. // }
  257. // fd: fd_cgroup (resource)
  258. // cpu: intptr = 0x0 (8 bytes)
  259. // group: fd_perf (resource)
  260. // flags: perf_flags_cgroup = 0x8 (8 bytes)
  261. // ]
  262. // returns fd_perf
  263. *(uint32_t*)0x200000000140 = 8;
  264. *(uint32_t*)0x200000000144 = 0x80;
  265. *(uint8_t*)0x200000000148 = 0;
  266. *(uint8_t*)0x200000000149 = 0;
  267. *(uint8_t*)0x20000000014a = 0;
  268. *(uint8_t*)0x20000000014b = 0;
  269. *(uint32_t*)0x20000000014c = 0;
  270. *(uint64_t*)0x200000000150 = 0;
  271. *(uint64_t*)0x200000000158 = 0;
  272. *(uint64_t*)0x200000000160 = 9;
  273. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 0, 1);
  274. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 1, 1);
  275. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 2, 1);
  276. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 3, 1);
  277. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 4, 1);
  278. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 5, 1);
  279. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 6, 1);
  280. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 7, 1);
  281. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 8, 1);
  282. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 9, 1);
  283. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 10, 1);
  284. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 11, 1);
  285. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 12, 1);
  286. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 13, 1);
  287. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 14, 1);
  288. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 15, 2);
  289. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 17, 1);
  290. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 18, 1);
  291. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 19, 1);
  292. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 20, 1);
  293. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 21, 1);
  294. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 22, 1);
  295. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 23, 1);
  296. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 24, 1);
  297. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 25, 1);
  298. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 26, 1);
  299. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 27, 1);
  300. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 28, 1);
  301. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 29, 1);
  302. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 30, 1);
  303. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 31, 1);
  304. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 32, 1);
  305. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 33, 1);
  306. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 34, 1);
  307. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 35, 1);
  308. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 36, 1);
  309. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 37, 1);
  310. STORE_BY_BITMASK(uint64_t, , 0x200000000168, 0, 38, 26);
  311. *(uint32_t*)0x200000000170 = 0;
  312. *(uint32_t*)0x200000000174 = 0;
  313. *(uint64_t*)0x200000000178 = 0;
  314. *(uint64_t*)0x200000000180 = 0xffffffff81200004;
  315. *(uint64_t*)0x200000000188 = 0;
  316. *(uint64_t*)0x200000000190 = 0x9f7;
  317. *(uint32_t*)0x200000000198 = 0;
  318. *(uint32_t*)0x20000000019c = 0;
  319. *(uint64_t*)0x2000000001a0 = 0;
  320. *(uint32_t*)0x2000000001a8 = 0;
  321. *(uint16_t*)0x2000000001ac = 0;
  322. *(uint16_t*)0x2000000001ae = 0;
  323. *(uint32_t*)0x2000000001b0 = 0;
  324. *(uint32_t*)0x2000000001b4 = 0;
  325. *(uint64_t*)0x2000000001b8 = 0x4003;
  326. inject_fault(7);
  327. syscall(__NR_perf_event_open, /*attr=*/0x200000000140ul, /*fd=*/(intptr_t)-1,
  328. /*cpu=*/0ul, /*group=*/(intptr_t)-1, /*flags=*/8ul);
  329. }
  330. int main(void)
  331. {
  332. syscall(__NR_mmap, /*addr=*/0x1ffffffff000ul, /*len=*/0x1000ul, /*prot=*/0ul,
  333. /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul,
  334. /*fd=*/(intptr_t)-1, /*offset=*/0ul);
  335. syscall(__NR_mmap, /*addr=*/0x200000000000ul, /*len=*/0x1000000ul,
  336. /*prot=PROT_WRITE|PROT_READ|PROT_EXEC*/ 7ul,
  337. /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul,
  338. /*fd=*/(intptr_t)-1, /*offset=*/0ul);
  339. syscall(__NR_mmap, /*addr=*/0x200001000000ul, /*len=*/0x1000ul, /*prot=*/0ul,
  340. /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul,
  341. /*fd=*/(intptr_t)-1, /*offset=*/0ul);
  342. const char* reason;
  343. (void)reason;
  344. if ((reason = setup_fault()))
  345. printf("the reproducer may not work as expected: fault injection setup "
  346. "failed: %s\n",
  347. reason);
  348. loop();
  349. return 0;
  350. }
  351.  
Advertisement
Add Comment
Please, Sign In to add comment