// https://syzkaller.appspot.com/bug?id=25ca058ab5cd43064985ff5bde64a573a308bf54 // 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 static unsigned long long procid; static void kill_and_wait(int pid, int* status) { kill(pid, SIGKILL); while (waitpid(-1, status, 0) != pid) { } } 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 void reset_flags(const char* filename) { struct stat st; if (lstat(filename, &st)) exit(1); st.st_flags &= ~(SF_NOUNLINK | UF_NOUNLINK | SF_IMMUTABLE | UF_IMMUTABLE | SF_APPEND | UF_APPEND); if (lchflags(filename, st.st_flags)) exit(1); } static void __attribute__((noinline)) remove_dir(const char* dir) { DIR* dp = opendir(dir); if (dp == NULL) { if (errno == EACCES) { if (rmdir(dir)) exit(1); return; } 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); struct stat st; if (lstat(filename, &st)) exit(1); if (S_ISDIR(st.st_mode)) { remove_dir(filename); continue; } if (unlink(filename)) { if (errno == EPERM) { reset_flags(filename); reset_flags(dir); if (unlink(filename) == 0) continue; } exit(1); } } closedir(dp); while (rmdir(dir)) { if (errno == EPERM) { reset_flags(dir); if (rmdir(dir) == 0) break; } exit(1); } } static void execute_one(void); #define WAIT_FLAGS 0 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); 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); } } uint64_t r[1] = {0xffffffffffffffff}; void execute_one(void) { intptr_t res = 0; if (write(1, "executing program\n", sizeof("executing program\n") - 1)) { } // openat$acpi arguments: [ // fd: const = 0xffffffffffffff9c (8 bytes) // file: ptr[in, buffer] { // buffer: {2f 64 65 76 2f 61 63 70 69 00} (length 0xa) // } // flags: open_flags = 0x1 (8 bytes) // mode: const = 0x0 (8 bytes) // ] // returns fd_acpi memcpy((void*)0x200000000ac0, "/dev/acpi\000", 10); res = syscall(SYS_openat, /*fd=*/0xffffffffffffff9cul, /*file=*/0x200000000ac0ul, /*flags=O_WRONLY*/ 1ul, /*mode=*/0ul); if (res != -1) r[0] = res; // mmap arguments: [ // addr: VMA[0x400000] // len: len = 0x400000 (8 bytes) // prot: mmap_prot = 0x2 (8 bytes) // flags: mmap_flags = 0x11 (8 bytes) // fd: fd (resource) // offset: intptr = 0x0 (8 bytes) // ] syscall(SYS_mmap, /*addr=*/0x200000000000ul, /*len=*/0x400000ul, /*prot=PROT_WRITE*/ 2ul, /*flags=MAP_FIXED|MAP_SHARED*/ 0x11ul, /*fd=*/r[0], /*offset=*/0ul); // mmap arguments: [ // addr: VMA[0x400000] // len: len = 0x400000 (8 bytes) // prot: mmap_prot = 0x2 (8 bytes) // flags: mmap_flags = 0x11 (8 bytes) // fd: fd (resource) // offset: intptr = 0x0 (8 bytes) // ] syscall(SYS_mmap, /*addr=*/0x200000000000ul, /*len=*/0x400000ul, /*prot=PROT_WRITE*/ 2ul, /*flags=MAP_FIXED|MAP_SHARED*/ 0x11ul, /*fd=*/r[0], /*offset=*/0ul); // setsockopt$sock_int arguments: [ // fd: sock (resource) // level: const = 0xffff (4 bytes) // optname: sockopt_opt_sock_int = 0x10000 (4 bytes) // optval: ptr[in, int32] { // int32 = 0x10001 (4 bytes) // } // optlen: len = 0x4 (8 bytes) // ] *(uint32_t*)0x2000000009c0 = 0x10001; syscall(SYS_setsockopt, /*fd=*/(intptr_t)-1, /*level=*/0xffff, /*optname=SO_REUSEPORT_LB*/ 0x10000, /*optval=*/0x2000000009c0ul, /*optlen=*/4ul); } int main(void) { syscall(SYS_mmap, /*addr=*/0x200000000000ul, /*len=*/0x1000000ul, /*prot=PROT_WRITE|PROT_READ|PROT_EXEC*/ 7ul, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x1012ul, /*fd=*/(intptr_t)-1, /*offset=*/0ul); const char* reason; (void)reason; for (procid = 0; procid < 4; procid++) { if (fork() == 0) { use_temporary_dir(); loop(); } } sleep(1000000); return 0; }