// https://syzkaller.appspot.com/bug?id=a5d8e444291fcb11e460df8ca71ed10f42e57027 #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include static void write_file(const char *path, const char *val) { int fd = open(path, O_WRONLY); if (fd < 0) { printf("[-] Notice: failed to open %s: %s\n", path, strerror(errno)); return; } int len = strlen(val); int res = write(fd, val, len); if (res != len) { printf("[-] Notice: failed to write '%s' to %s: %s\n", val, path, strerror(errno)); } else { printf("[+] Configured %s = %s\n", path, val); } close(fd); } static void arm_fail_nth(int fd, int n) { char buf[32]; int len = snprintf(buf, sizeof(buf), "%d\n", n); for (;;) { errno = 0; int res = pwrite(fd, buf, len, 0); if (res == len) break; if (errno == EFAULT || errno == EINTR) continue; printf("[-] Failed to arm fail-nth (%d): %s\n", n, strerror(errno)); exit(1); } } static void disarm_fail_nth(int fd) { for (;;) { errno = 0; int res = pwrite(fd, "0\n", 2, 0); if (res == 2) break; if (errno == EFAULT || errno == EINTR) continue; printf("[-] Failed to disarm fail-nth: %s\n", strerror(errno)); exit(1); } } static void set_make_it_fail(int fd, int val) { const char *str = val ? "1\n" : "0\n"; for (;;) { errno = 0; int res = pwrite(fd, str, 2, 0); if (res == 2) break; if (errno == EFAULT || errno == EINTR) continue; printf("[-] Failed to set make-it-fail to %d: %s\n", val, strerror(errno)); exit(1); } } int main(void) { setvbuf(stdout, NULL, _IONBF, 0); printf("[*] Starting reproducer for WARNING in free_percpu...\n"); /* Step 1: Mount debugfs if not already mounted */ int res = mount("debugfs", "/sys/kernel/debug", "debugfs", 0, NULL); if (res < 0) { if (errno == EBUSY) { printf("[+] debugfs is already mounted.\n"); } else { printf("[-] Failed to mount debugfs: %s\n", strerror(errno)); exit(1); } } else { printf("[+] Successfully mounted debugfs.\n"); } /* Step 2: Configure failslab and fail_page_alloc fault injection parameters */ write_file("/sys/kernel/debug/failslab/ignore-gfp-wait", "N"); write_file("/sys/kernel/debug/failslab/verbose", "0"); write_file("/sys/kernel/debug/failslab/cache-filter", "N"); write_file("/sys/kernel/debug/failslab/times", "-1"); write_file("/sys/kernel/debug/failslab/probability", "0"); write_file("/sys/kernel/debug/failslab/task-filter", "N"); /* Configure fail_page_alloc to catch order-0 vmalloc fallback pages */ write_file("/sys/kernel/debug/fail_page_alloc/min-order", "0"); write_file("/sys/kernel/debug/fail_page_alloc/ignore-gfp-wait", "N"); write_file("/sys/kernel/debug/fail_page_alloc/ignore-gfp-highmem", "N"); write_file("/sys/kernel/debug/fail_page_alloc/task-filter", "Y"); write_file("/sys/kernel/debug/fail_page_alloc/times", "-1"); write_file("/sys/kernel/debug/fail_page_alloc/verbose", "0"); write_file("/sys/kernel/debug/fail_page_alloc/probability", "100"); /* Step 3: Open fail-nth and make-it-fail interfaces */ int fd_fail = open("/proc/thread-self/fail-nth", O_RDWR); if (fd_fail < 0) { printf("[-] Notice: /proc/thread-self/fail-nth unavailable (%s), trying /proc/self/fail-nth\n", strerror(errno)); fd_fail = open("/proc/self/fail-nth", O_RDWR); if (fd_fail < 0) { printf("[-] Failed to open fail-nth: %s\n", strerror(errno)); exit(1); } } printf("[+] Successfully opened fail-nth interface (fd=%d).\n", fd_fail); int fd_make = open("/proc/thread-self/make-it-fail", O_RDWR); if (fd_make < 0) { printf("[-] Notice: /proc/thread-self/make-it-fail unavailable (%s), trying /proc/self/make-it-fail\n", strerror(errno)); fd_make = open("/proc/self/make-it-fail", O_RDWR); if (fd_make < 0) { printf("[-] Failed to open make-it-fail: %s\n", strerror(errno)); exit(1); } } printf("[+] Successfully opened make-it-fail interface (fd=%d).\n", fd_make); /* Ensure fault injection starts fully disarmed */ set_make_it_fail(fd_make, 0); disarm_fail_nth(fd_fail); printf("[+] Fault injection interfaces initialized and disarmed.\n"); /* Step 4: Discover and open writable SCSI host scan attributes */ DIR *dir = opendir("/sys/class/scsi_host"); if (!dir) { printf("[-] Failed to open /sys/class/scsi_host: %s\n", strerror(errno)); exit(1); } int scan_fds[32]; char scan_names[32][64]; int num_hosts = 0; struct dirent *de; while ((de = readdir(dir)) != NULL) { if (strncmp(de->d_name, "host", 4) == 0 && num_hosts < 32) { char host_scan_path[256]; snprintf(host_scan_path, sizeof(host_scan_path), "/sys/class/scsi_host/%s/scan", de->d_name); int sfd = open(host_scan_path, O_WRONLY); if (sfd < 0) { printf("[-] Notice: could not open %s: %s\n", host_scan_path, strerror(errno)); continue; } scan_fds[num_hosts] = sfd; strncpy(scan_names[num_hosts], de->d_name, sizeof(scan_names[0]) - 1); scan_names[num_hosts][sizeof(scan_names[0]) - 1] = '\0'; printf("[+] Opened SCSI host scan attribute: %s (fd=%d)\n", host_scan_path, sfd); num_hosts++; } } closedir(dir); if (num_hosts == 0) { printf("[-] No writable SCSI host scan attribute found in /sys/class/scsi_host\n"); exit(1); } printf("[+] Found and opened %d SCSI host scan target(s).\n", num_hosts); /* Step 5: Pre-warm allocations so caches and percpu chunks are stable */ printf("[*] Pre-warming SCSI scan allocations...\n"); for (int h = 0; h < num_hosts; h++) { pwrite(scan_fds[h], "- 0 -", 5, 0); pwrite(scan_fds[h], "- 0 -", 5, 0); } printf("[+] Pre-warming complete.\n"); /* Step 6: Phase 1 - Targeted Coupled Sweep (N=1..50) * * Writing "- 0 -" specifies target ID 0, executing in sub-millisecond time. * Combining fail_nth with fail_page_alloc catches both kmalloc and the * order-0 vmalloc fallback inside kvzalloc_node(), forcing sbitmap_init_node() * to fail after allocating alloc_hint and triggering a double free_percpu. */ printf("[*] Phase 1: Targeted coupled sweep (N=1..50)...\n"); for (int h = 0; h < num_hosts; h++) { printf("[*] Sweeping host '%s'...\n", scan_names[h]); for (int n = 1; n <= 50; n++) { set_make_it_fail(fd_make, 1); arm_fail_nth(fd_fail, n); pwrite(scan_fds[h], "- 0 -", 5, 0); set_make_it_fail(fd_make, 0); disarm_fail_nth(fd_fail); if (n % 10 == 0 || n == 1) { printf("[+] Host '%s': iteration %d/50 completed\n", scan_names[h], n); } usleep(2000); } printf("[+] Host '%s' sweep completed.\n", scan_names[h]); } /* Step 7: Phase 2 - Probabilistic Fallback */ printf("[*] Phase 2: Probabilistic fallback...\n"); write_file("/sys/kernel/debug/failslab/task-filter", "Y"); write_file("/sys/kernel/debug/failslab/probability", "25"); write_file("/sys/kernel/debug/fail_page_alloc/probability", "50"); for (int h = 0; h < num_hosts; h++) { printf("[*] Running probabilistic iterations on host '%s'...\n", scan_names[h]); for (int i = 1; i <= 150; i++) { set_make_it_fail(fd_make, 1); pwrite(scan_fds[h], "- 0 -", 5, 0); set_make_it_fail(fd_make, 0); if (i % 50 == 0 || i == 1) { printf("[+] Host '%s': probabilistic iteration %d/150 completed\n", scan_names[h], i); } usleep(2000); } } /* Step 8: Cleanup and wait for asynchronous events */ printf("[*] Closing file descriptors and waiting for asynchronous events...\n"); for (int h = 0; h < num_hosts; h++) { close(scan_fds[h]); } close(fd_fail); close(fd_make); sleep(2); printf("[+] Reproducer finished execution.\n"); return 0; }