// https://syzkaller.appspot.com/bug?id=2388239bd70accc3407dca8392d4250fda22114e // autogenerated by syzkaller (https://github.com/google/syzkaller) #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef __NR_fsconfig #define __NR_fsconfig 431 #endif #ifndef __NR_fsopen #define __NR_fsopen 430 #endif static void sleep_ms(uint64_t ms) { usleep(ms * 1000); } static uint64_t current_time_ms(void) { struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts)) exit(1); return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000; } static void use_temporary_dir(void) { char tmpdir_template[] = "./syzkaller.XXXXXX"; char* tmpdir = mkdtemp(tmpdir_template); if (!tmpdir) exit(1); if (chmod(tmpdir, 0777)) exit(1); if (chdir(tmpdir)) exit(1); } static bool write_file(const char* file, const char* what, ...) { char buf[1024]; va_list args; va_start(args, what); vsnprintf(buf, sizeof(buf), what, args); va_end(args); buf[sizeof(buf) - 1] = 0; int len = strlen(buf); int fd = open(file, O_WRONLY | O_CLOEXEC); if (fd == -1) return false; if (write(fd, buf, len) != len) { int err = errno; close(fd); errno = err; return false; } close(fd); return true; } #define FS_IOC_SETFLAGS _IOW('f', 2, long) static void remove_dir(const char* dir) { int iter = 0; DIR* dp = 0; const int umount_flags = MNT_FORCE | UMOUNT_NOFOLLOW; retry: while (umount2(dir, umount_flags) == 0) { } dp = opendir(dir); if (dp == NULL) { if (errno == EMFILE) { exit(1); } exit(1); } struct dirent* ep = 0; while ((ep = readdir(dp))) { if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0) continue; char filename[FILENAME_MAX]; snprintf(filename, sizeof(filename), "%s/%s", dir, ep->d_name); while (umount2(filename, umount_flags) == 0) { } struct stat st; if (lstat(filename, &st)) exit(1); if (S_ISDIR(st.st_mode)) { remove_dir(filename); continue; } int i; for (i = 0;; i++) { if (unlink(filename) == 0) break; if (errno == EPERM) { int fd = open(filename, O_RDONLY); if (fd != -1) { long flags = 0; if (ioctl(fd, FS_IOC_SETFLAGS, &flags) == 0) { } close(fd); continue; } } if (errno == EROFS) { break; } if (errno != EBUSY || i > 100) exit(1); if (umount2(filename, umount_flags)) exit(1); } } closedir(dp); for (int i = 0;; i++) { if (rmdir(dir) == 0) break; if (i < 100) { if (errno == EPERM) { int fd = open(dir, O_RDONLY); if (fd != -1) { long flags = 0; if (ioctl(fd, FS_IOC_SETFLAGS, &flags) == 0) { } close(fd); continue; } } if (errno == EROFS) { break; } if (errno == EBUSY) { if (umount2(dir, umount_flags)) exit(1); continue; } if (errno == ENOTEMPTY) { if (iter < 100) { iter++; goto retry; } } } exit(1); } } static void kill_and_wait(int pid, int* status) { kill(-pid, SIGKILL); kill(pid, SIGKILL); for (int i = 0; i < 100; i++) { if (waitpid(-1, status, WNOHANG | __WALL) == pid) return; usleep(1000); } DIR* dir = opendir("/sys/fs/fuse/connections"); if (dir) { for (;;) { struct dirent* ent = readdir(dir); if (!ent) break; if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) continue; char abort[300]; snprintf(abort, sizeof(abort), "/sys/fs/fuse/connections/%s/abort", ent->d_name); int fd = open(abort, O_WRONLY); if (fd == -1) { continue; } if (write(fd, abort, 1) < 0) { } close(fd); } closedir(dir); } else { } while (waitpid(-1, status, __WALL) != pid) { } } static void setup_test() { prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0); setpgrp(); write_file("/proc/self/oom_score_adj", "1000"); if (symlink("/dev/binderfs", "./binderfs")) { } } #define KMEMLEAK_FILE "/sys/kernel/debug/kmemleak" static const char* setup_leak() { if (!write_file(KMEMLEAK_FILE, "scan=off")) { if (errno == EBUSY) return "KMEMLEAK disabled: increase CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE" " or unset CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF"; return "failed to write(kmemleak, \"scan=off\")"; } if (!write_file(KMEMLEAK_FILE, "scan")) return "failed to write(kmemleak, \"scan\")"; sleep(5); if (!write_file(KMEMLEAK_FILE, "scan")) return "failed to write(kmemleak, \"scan\")"; if (!write_file(KMEMLEAK_FILE, "clear")) return "failed to write(kmemleak, \"clear\")"; return NULL; } static void check_leaks(void) { int fd = open(KMEMLEAK_FILE, O_RDWR); if (fd == -1) exit(1); uint64_t start = current_time_ms(); if (write(fd, "scan", 4) != 4) exit(1); sleep(1); while (current_time_ms() - start < 4 * 1000) sleep(1); if (write(fd, "scan", 4) != 4) exit(1); static char buf[128 << 10]; ssize_t n = read(fd, buf, sizeof(buf) - 1); if (n < 0) exit(1); int nleaks = 0; if (n != 0) { sleep(1); if (write(fd, "scan", 4) != 4) exit(1); if (lseek(fd, 0, SEEK_SET) < 0) exit(1); n = read(fd, buf, sizeof(buf) - 1); if (n < 0) exit(1); buf[n] = 0; char* pos = buf; char* end = buf + n; while (pos < end) { char* next = strstr(pos + 1, "unreferenced object"); if (!next) next = end; char prev = *next; *next = 0; fprintf(stderr, "BUG: memory leak\n%s\n", pos); *next = prev; pos = next; nleaks++; } } if (write(fd, "clear", 5) != 5) exit(1); close(fd); if (nleaks) exit(1); } static void setup_sysctl() { int cad_pid = fork(); if (cad_pid < 0) exit(1); if (cad_pid == 0) { for (;;) sleep(100); } char tmppid[32]; snprintf(tmppid, sizeof(tmppid), "%d", cad_pid); struct { const char* name; const char* data; } files[] = { {"/sys/kernel/debug/x86/nmi_longest_ns", "10000000000"}, {"/proc/sys/kernel/hung_task_check_interval_secs", "20"}, {"/proc/sys/net/core/bpf_jit_kallsyms", "1"}, {"/proc/sys/net/core/bpf_jit_harden", "0"}, {"/proc/sys/kernel/kptr_restrict", "0"}, {"/proc/sys/kernel/softlockup_all_cpu_backtrace", "1"}, {"/proc/sys/fs/mount-max", "100"}, {"/proc/sys/vm/oom_dump_tasks", "0"}, {"/proc/sys/debug/exception-trace", "0"}, {"/proc/sys/kernel/printk", "7 4 1 3"}, {"/proc/sys/kernel/keys/gc_delay", "1"}, {"/proc/sys/vm/oom_kill_allocating_task", "1"}, {"/proc/sys/kernel/ctrl-alt-del", "0"}, {"/proc/sys/kernel/cad_pid", tmppid}, }; for (size_t i = 0; i < sizeof(files) / sizeof(files[0]); i++) { if (!write_file(files[i].name, files[i].data)) { } } kill(cad_pid, SIGKILL); while (waitpid(cad_pid, NULL, 0) != cad_pid) ; } static void execute_one(void); #define WAIT_FLAGS __WALL static void loop(void) { int iter = 0; for (;; iter++) { char cwdbuf[32]; sprintf(cwdbuf, "./%d", iter); if (mkdir(cwdbuf, 0777)) exit(1); int pid = fork(); if (pid < 0) exit(1); if (pid == 0) { if (chdir(cwdbuf)) exit(1); setup_test(); execute_one(); exit(0); } int status = 0; uint64_t start = current_time_ms(); for (;;) { sleep_ms(10); if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid) break; if (current_time_ms() - start < 5000) continue; kill_and_wait(pid, &status); break; } remove_dir(cwdbuf); check_leaks(); } } uint64_t r[1] = {0xffffffffffffffff}; void execute_one(void) { intptr_t res = 0; if (write(1, "executing program\n", sizeof("executing program\n") - 1)) { } // fsopen arguments: [ // type: ptr[in, buffer] { // buffer: {63 69 66 73 00} (length 0x5) // } // flags: fsopen_flags = 0x0 (8 bytes) // ] // returns fd_fscontext memcpy((void*)0x200000000100, "cifs\000", 5); res = syscall(__NR_fsopen, /*type=*/0x200000000100ul, /*flags=*/0ul); if (res != -1) r[0] = res; // fsconfig$FSCONFIG_SET_STRING arguments: [ // fd: fd_fscontext (resource) // cmd: const = 0x1 (8 bytes) // key: ptr[in, buffer] { // buffer: {73 6f 75 72 63 65} (length 0x6) // } // value: ptr[in, buffer] { // buffer: {2f 2f f2 62 06 08 ba df 58 6f dc ea 95 9a 9b 2f 51 39 f9 0d // 6d 44 94 29 55 db 15 58 2e 49 0a 7d f3 9d e4 5f 05 9c 71 66 34 49 5e // 23 62 3f 39 de af 75 27 83 4c e0 97 e1 6e 5f a4 25 b1 97 93 af 76 ce // 14 2f 38 5c 00 00 a7 fb f4 84 1f 41 ea 73 5e ef a2 85 a3 21 fb 93 d7 // 52 ab 32 1e 57 e9 68 9b f7 75 6c f9 44 d4 82 58 35 13 aa 87 f9 ba a9 // 6d 14 14 52 5f 9a 5c 3e 34 ce 8e 5f 23 f8 44 b1 de 70 01 cc 3a a6 c5 // 6e eb ab f7 30 99 ef 8b 3c 4d 0a c0 60 5b 82 f6 24 a4 be 1e d3 e4 d9 // 4c 14 ed cf 4b cc eb 2c 1a 31 a6 f3 65 c2 46 c3 00 aa d5 fc 94 3d a9 // 8c b4 26 9f a2 24 06 1a b0 57 23 f4 63 a0 6c d5 64 e5 cd b2 c1 30 97 // 77 87 e5 06 91 57 0d 72 f5 97 25 e8 70 a1 aa 80 49 a5 c6 f3 f2 a0 b3 // 5c 99 7b cd 52 92 94 f7 1d 01 51 1a bd 15 62 15 68 e2 21 00 b9 7a 29 // 19 00 ee d2 29 5b 70 60 b3 03 a7 70 27 58 ec cd 6f 58 05 ff ff 2f 6f // b2 ad b8 89 69 40 0c ff 53 26 8a c9 fe 7a c2 90 e7 46 a6 db 0d 03 6a // 2c 4e f1 6c 77 0a ad e8 f0 bd a1 98 ce f9 1e 52 9c 63 c5 6b 65 5f a7 // 11 22 04 d8 2e a0 15 83 f1 db 79 e9 dc 40 2e c1 67 c6 0c 63 a2 36 d8 // df ef f7 9c 1a cc 8a 6d 8b 37 cf c5 a6 f4 62 e3 6a 25 59 a9 dd 0e 39 // 65 b5 ec 99 40 d2 09 0a b1 6f e5 a8 51 c9 e7 35 51 ea d8 a2 3d 46 9b // 8f 8d fc 27 20 7b 65 94 39 58 da e8 48 1a 85 d7 25 93 3f 77 7d fb d9 // 74 ea bb be 9b 5c d7 d7 d4 e2 d6 63 28 60 28 72 d2 b1 47 e3 da 26 ca // aa ba 0b c5 a3 48 b1 11 47 ab a2 1f a0 c3 d9 cd fb 1a 2c 13 53 f1 fe // e5 46 f4 fb c8 7c 89 0f 4e 71 97 4a b9 4a 88 53 c5 b8 12 93 be b9 62 // c7 bc 03 03 2e 45 ab 82 1c bc 31 9d 14 fa 1f dd 1e 2a 03 60 91 e8 78 // 0f 05 1e f5 bf 4e 6f d4 0f 5d 53 85 89 40 a9 66 05 fe 9f 6b 4d a5 f6 // c2 04 bf c7 00 3c 9b d5 76 5a 31 a2 6a 9a eb 36 a8 b4 d9 0b 76 94 4f // b1 09 53 7f f2 fe 8d 6c f8 00 99 0c 35 b7 ee 95 f3 a5 f1 f2 5a 3f cc // 8d 9d 79 96 39 d8 7d 75 88 22 62 69 59 4f ac 3d 6a 5f f6 36 7b f0 ec // dc 31 6e 1b 15 85 4e 1d 80 5a 1b 86 db 79 34 cf 90 08 f6 1b e6 f8 55 // e9 46 1c 2b 14 26 00 42 b0 6a 55 53 aa 39 e5 1a 67 4f 93 fe 98 00 00 // 00 00 00 04} (length 0x285) // } // aux: const = 0x0 (8 bytes) // ] memcpy((void*)0x200000000040, "source", 6); memcpy( (void*)0x200000000380, "//\362b\006\b\272\337Xo\334\352\225\232\233/" "Q9\371\rmD\224)U\333\025X.I\n}\363\235\344_\005\234qf4I^#b?" "9\336\257u\'\203L\340\227\341n_\244%\261\227\223\257v\316\024/" "8\\\000\000\247\373\364\204\037A\352s^\357\242\205\243!" "\373\223\327R\2532\036W\351h\233\367ul\371D\324\202X5\023\252\207\371" "\272\251m\024\024R_\232\\>4\316\216_#\370D\261\336p\001\314:" "\246\305n\353\253\3670\231\357\2134\316\216_#\370D\261\336p\001\314:" "\246\305n\353\253\3670\231\357\213