// https://syzkaller.appspot.com/bug?id=8d705ef49608625166519bdf9d6761afc1bcc8d5 // 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 #include #include #include 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; } static int runcmdline(char* cmdline) { int ret = system(cmdline); if (ret) { } return ret; } static void setup_gadgetfs(); static void setup_binderfs(); static void setup_fusectl(); static void sandbox_common_mount_tmpfs(void) { write_file("/proc/sys/fs/mount-max", "100000"); if (mkdir("./syz-tmp", 0777)) exit(1); if (mount("", "./syz-tmp", "tmpfs", 0, NULL)) exit(1); if (mkdir("./syz-tmp/newroot", 0777)) exit(1); if (mkdir("./syz-tmp/newroot/dev", 0700)) exit(1); unsigned bind_mount_flags = MS_BIND | MS_REC | MS_PRIVATE; if (mount("/dev", "./syz-tmp/newroot/dev", NULL, bind_mount_flags, NULL)) exit(1); if (mkdir("./syz-tmp/newroot/proc", 0700)) exit(1); if (mount("syz-proc", "./syz-tmp/newroot/proc", "proc", 0, NULL)) exit(1); if (mkdir("./syz-tmp/newroot/selinux", 0700)) exit(1); const char* selinux_path = "./syz-tmp/newroot/selinux"; if (mount("/selinux", selinux_path, NULL, bind_mount_flags, NULL)) { if (errno != ENOENT) exit(1); if (mount("/sys/fs/selinux", selinux_path, NULL, bind_mount_flags, NULL) && errno != ENOENT) exit(1); } if (mkdir("./syz-tmp/newroot/sys", 0700)) exit(1); if (mount("/sys", "./syz-tmp/newroot/sys", 0, bind_mount_flags, NULL)) exit(1); if (mount("/sys/kernel/debug", "./syz-tmp/newroot/sys/kernel/debug", NULL, bind_mount_flags, NULL) && errno != ENOENT) exit(1); if (mount("/sys/fs/smackfs", "./syz-tmp/newroot/sys/fs/smackfs", NULL, bind_mount_flags, NULL) && errno != ENOENT) exit(1); if (mount("/proc/sys/fs/binfmt_misc", "./syz-tmp/newroot/proc/sys/fs/binfmt_misc", NULL, bind_mount_flags, NULL) && errno != ENOENT) exit(1); if (mkdir("./syz-tmp/newroot/syz-inputs", 0700)) exit(1); if (mount("/syz-inputs", "./syz-tmp/newroot/syz-inputs", NULL, bind_mount_flags | MS_RDONLY, NULL) && errno != ENOENT) exit(1); if (mkdir("./syz-tmp/pivot", 0777)) exit(1); if (syscall(SYS_pivot_root, "./syz-tmp", "./syz-tmp/pivot")) { if (chdir("./syz-tmp")) exit(1); } else { if (chdir("/")) exit(1); if (umount2("./pivot", MNT_DETACH)) exit(1); } if (chroot("./newroot")) exit(1); if (chdir("/")) exit(1); setup_gadgetfs(); setup_binderfs(); setup_fusectl(); } static void setup_gadgetfs() { if (mkdir("/dev/gadgetfs", 0777)) { } if (mount("gadgetfs", "/dev/gadgetfs", "gadgetfs", 0, NULL)) { } } static void setup_fusectl() { if (mount(0, "/sys/fs/fuse/connections", "fusectl", 0, 0)) { } } static void setup_binderfs() { if (mkdir("/dev/binderfs", 0777)) { } if (mount("binder", "/dev/binderfs", "binder", 0, "stats=global")) { if (mount("binder", "/dev/binderfs", "binder", 0, NULL)) { } } if (symlink("/dev/binderfs", "./binderfs")) { } } static void loop(); static void sandbox_common() { prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0); if (getppid() == 1) exit(1); struct rlimit rlim; rlim.rlim_cur = rlim.rlim_max = (200 << 20); setrlimit(RLIMIT_AS, &rlim); rlim.rlim_cur = rlim.rlim_max = 32 << 20; setrlimit(RLIMIT_MEMLOCK, &rlim); rlim.rlim_cur = rlim.rlim_max = 136 << 20; setrlimit(RLIMIT_FSIZE, &rlim); rlim.rlim_cur = rlim.rlim_max = 1 << 20; setrlimit(RLIMIT_STACK, &rlim); rlim.rlim_cur = rlim.rlim_max = 128 << 20; setrlimit(RLIMIT_CORE, &rlim); rlim.rlim_cur = rlim.rlim_max = 256; setrlimit(RLIMIT_NOFILE, &rlim); if (unshare(CLONE_NEWNS)) { } if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL)) { } if (unshare(CLONE_NEWIPC)) { } if (unshare(0x02000000)) { } if (unshare(CLONE_NEWUTS)) { } if (unshare(CLONE_SYSVSEM)) { } typedef struct { const char* name; const char* value; } sysctl_t; static const sysctl_t sysctls[] = { {"/proc/sys/kernel/shmmax", "16777216"}, {"/proc/sys/kernel/shmall", "536870912"}, {"/proc/sys/kernel/shmmni", "1024"}, {"/proc/sys/kernel/msgmax", "8192"}, {"/proc/sys/kernel/msgmni", "1024"}, {"/proc/sys/kernel/msgmnb", "1024"}, {"/proc/sys/kernel/sem", "1024 1048576 500 1024"}, }; unsigned i; for (i = 0; i < sizeof(sysctls) / sizeof(sysctls[0]); i++) write_file(sysctls[i].name, sysctls[i].value); } static int wait_for_loop(int pid) { if (pid < 0) exit(1); int status = 0; while (waitpid(-1, &status, __WALL) != pid) { } return WEXITSTATUS(status); } static void drop_caps(void) { struct __user_cap_header_struct cap_hdr = {}; struct __user_cap_data_struct cap_data[2] = {}; cap_hdr.version = _LINUX_CAPABILITY_VERSION_3; cap_hdr.pid = getpid(); if (syscall(SYS_capget, &cap_hdr, &cap_data)) exit(1); const int drop = (1 << CAP_SYS_PTRACE) | (1 << CAP_SYS_NICE); cap_data[0].effective &= ~drop; cap_data[0].permitted &= ~drop; cap_data[0].inheritable &= ~drop; if (syscall(SYS_capset, &cap_hdr, &cap_data)) exit(1); } static int do_sandbox_none(void) { if (unshare(CLONE_NEWPID)) { } int pid = fork(); if (pid != 0) return wait_for_loop(pid); sandbox_common(); drop_caps(); if (unshare(CLONE_NEWNET)) { } write_file("/proc/sys/net/ipv4/ping_group_range", "0 65535"); sandbox_common_mount_tmpfs(); loop(); 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"}, {"/sys/kernel/debug/failslab/task-filter", "Y"}, {"/sys/kernel/debug/fail_page_alloc/task-filter", "Y"}, {"/sys/kernel/debug/fail_futex/task-filter", "Y"}, {"/sys/kernel/debug/fail_usercopy/task-filter", "Y"}, {"/sys/kernel/debug/fail_make_request/task-filter", "Y"}, {"/sys/kernel/debug/fail_io_timeout/task-filter", "Y"}, {"/sys/kernel/debug/fail_iommufd/task-filter", "Y"}, {"/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) ; } #define SWAP_FILE "./swap-file" #define SWAP_FILE_SIZE (128 * 1000 * 1000) static const char* setup_swap() { swapoff(SWAP_FILE); unlink(SWAP_FILE); int fd = open(SWAP_FILE, O_CREAT | O_WRONLY | O_CLOEXEC, 0600); if (fd == -1) return "swap file open failed"; fallocate(fd, FALLOC_FL_ZERO_RANGE, 0, SWAP_FILE_SIZE); close(fd); char cmdline[64]; sprintf(cmdline, "mkswap %s", SWAP_FILE); if (runcmdline(cmdline)) return "mkswap failed"; if (swapon(SWAP_FILE, SWAP_FLAG_PREFER) == 1) return "swapon failed"; return NULL; } uint64_t r[3] = {0xffffffffffffffff, 0x0, 0xffffffffffffffff}; void loop(void) { intptr_t res = 0; if (write(1, "executing program\n", sizeof("executing program\n") - 1)) { } // openat$dm_control arguments: [ // fd: const = 0xffffff9c (8 bytes) // file: ptr[in, buffer] { // buffer: {2f 64 65 76 2f 6d 61 70 70 65 72 2f 63 6f 6e 74 72 6f 6c 00} // (length 0x14) // } // flags: open_flags = 0x2 (4 bytes) // mode: const = 0x0 (2 bytes) // ] // returns fd_dm_ctl memcpy((void*)0x2000000001c0, "/dev/mapper/control\000", 20); res = syscall(__NR_openat, /*fd=*/0xffffff9cul, /*file=*/0x2000000001c0ul, /*flags=O_RDWR*/ 2, /*mode=*/0); if (res != -1) r[0] = res; // ioctl$DM_DEV_CREATE arguments: [ // fd: fd_dm_ctl (resource) // cmd: const = 0xc138fd03 (4 bytes) // arg: ptr[inout, dm_ioctl_t[dm_id_create, void, const[0, int32], const[0, // int32]]] { // dm_ioctl_t[dm_id_create, void, const[0, int32], const[0, int32]] { // version_major: const = 0x4 (4 bytes) // version_minor: const = 0x0 (4 bytes) // version_patch: const = 0x0 (4 bytes) // data_size: bytesize = 0x138 (4 bytes) // data_start: offsetof = 0x138 (4 bytes) // target_count: const = 0x0 (4 bytes) // open_count: const = 0x0 (4 bytes) // flags: dm_ioctl_flags = 0x0 (4 bytes) // event_nr: const = 0x0 (4 bytes) // padding: const = 0x0 (4 bytes) // id: dm_id_create { // dev: dm_dev_number (resource) // name: dm_dev_name { // prefix: buffer: {73 79 7a} (length 0x3) // proc: proc = 0x0 (1 bytes) // slot: buffer: {31 00} (length 0x2) // pad = 0x0 (122 bytes) // } // uuid: union dm_dev_uuid_opt { // none: const = 0x0 (1 bytes) // } // } // data: buffer: {} (length 0x0) // } // } // ] *(uint32_t*)0x200000000200 = 4; *(uint32_t*)0x200000000204 = 0; *(uint32_t*)0x200000000208 = 0; *(uint32_t*)0x20000000020c = 0x138; *(uint32_t*)0x200000000210 = 0x138; *(uint32_t*)0x200000000214 = 0; *(uint32_t*)0x200000000218 = 0; *(uint32_t*)0x20000000021c = 0; *(uint32_t*)0x200000000220 = 0; *(uint32_t*)0x200000000224 = 0; memcpy((void*)0x200000000230, "syz", 3); *(uint8_t*)0x200000000233 = 0x61; memcpy((void*)0x200000000234, "1\000", 2); *(uint8_t*)0x2000000002b0 = 0; res = syscall(__NR_ioctl, /*fd=*/r[0], /*cmd=*/0xc138fd03, /*arg=*/0x200000000200ul); if (res != -1) r[1] = *(uint64_t*)0x200000000228; // ioctl$DM_TABLE_LOAD arguments: [ // fd: fd_dm_ctl (resource) // cmd: const = 0xc138fd09 (4 bytes) // arg: ptr[inout, dm_ioctl_t[dm_id_lookup, array[dm_target, 1], len[data, // int32], const[0, int32]]] { // dm_ioctl_t[dm_id_lookup, array[dm_target, 1], len[data, int32], // const[0, int32]] { // version_major: const = 0x4 (4 bytes) // version_minor: const = 0x0 (4 bytes) // version_patch: const = 0x0 (4 bytes) // data_size: bytesize = 0x1d8 (4 bytes) // data_start: offsetof = 0x138 (4 bytes) // target_count: len = 0x1 (4 bytes) // open_count: const = 0x0 (4 bytes) // flags: dm_ioctl_flags = 0x0 (4 bytes) // event_nr: const = 0x0 (4 bytes) // padding: const = 0x0 (4 bytes) // id: union dm_id_lookup { // by_name: dm_id_by_name { // dev: dm_dev_number (resource) // name: dm_dev_name { // prefix: buffer: {73 79 7a} (length 0x3) // proc: proc = 0x0 (1 bytes) // slot: buffer: {31 00} (length 0x2) // pad = 0x0 (122 bytes) // } // pad = 0x0 (136 bytes) // } // } // data: array[dm_target] { // union dm_target { // crypt: dm_target_spec_t[dm_target_crypt, dm_crypt_params] { // sector_start: const = 0x0 (8 bytes) // length: dm_lengths = 0x2000 (8 bytes) // status: const = 0x0 (4 bytes) // next: bytesize = 0xa0 (4 bytes) // target_type: buffer: {63 72 79 70 74 00 00 00 00 00 00 00 00 // 00 00 00} (length 0x10) params: dm_crypt_params { // cipher: buffer: {61 65 73 2d 78 74 73 2d 70 6c 61 69 6e 36 // 34} (length 0xf) sp0: const = 0x20 (1 bytes) key: union // dm_crypt_key { // fixed: buffer: {30 31 32 33 34 35 36 37 38 39 61 62 63 64 // 65 66 30 31 32 33 34 35 36 37 38 39 61 62 63 64 65 66 30 // 31 32 33 34 35 36 37 38 39 61 62 63 64 65 66 30 31 32 33 // 34 35 36 37 38 39 61 62 63 64 65 66} (length 0x40) // } // sp1: const = 0x20 (1 bytes) // iv_off: buffer: {30} (length 0x1) // sp2: const = 0x20 (1 bytes) // dev: dm_dev_off { // dev: union dm_dev { // ram: dm_dev_proc_t["/dev/ram", 2] { // prefix: buffer: {2f 64 65 76 2f 72 61 6d} (length 0x8) // id: proc = 0x1 (1 bytes) // } // } // sp: const = 0x20 (1 bytes) // off: buffer: {30} (length 0x1) // } // opts: buffer: {20 31 20 69 6e 74 65 67 72 69 74 79 3a 38 3a // 6e 6f 6e 65} (length 0x13) // } // z: const = 0x0 (1 bytes) // } // } // } // } // } // ] *(uint32_t*)0x200000000840 = 4; *(uint32_t*)0x200000000844 = 0; *(uint32_t*)0x200000000848 = 0; *(uint32_t*)0x20000000084c = 0x1d8; *(uint32_t*)0x200000000850 = 0x138; *(uint32_t*)0x200000000854 = 1; *(uint32_t*)0x200000000858 = 0; *(uint32_t*)0x20000000085c = 0; *(uint32_t*)0x200000000860 = 0; *(uint32_t*)0x200000000864 = 0; memcpy((void*)0x200000000870, "syz", 3); *(uint8_t*)0x200000000873 = 0x61; memcpy((void*)0x200000000874, "1\000", 2); *(uint64_t*)0x200000000978 = 0; *(uint64_t*)0x200000000980 = 0x2000; *(uint32_t*)0x200000000988 = 0; *(uint32_t*)0x20000000098c = 0xa0; memcpy((void*)0x200000000990, "crypt\000\000\000\000\000\000\000\000\000\000\000", 16); memcpy((void*)0x2000000009a0, "aes-xts-plain64", 15); *(uint8_t*)0x2000000009af = 0x20; memcpy((void*)0x2000000009b0, "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", 64); *(uint8_t*)0x2000000009f0 = 0x20; memset((void*)0x2000000009f1, 48, 1); *(uint8_t*)0x2000000009f2 = 0x20; memcpy((void*)0x2000000009f3, "/dev/ram", 8); *(uint8_t*)0x2000000009fb = 0x31; *(uint8_t*)0x2000000009fc = 0x20; memset((void*)0x2000000009fd, 48, 1); memcpy((void*)0x2000000009fe, " 1 integrity:8:none", 19); *(uint8_t*)0x200000000a11 = 0; syscall(__NR_ioctl, /*fd=*/r[0], /*cmd=*/0xc138fd09, /*arg=*/0x200000000840ul); // ioctl$DM_DEV_SUSPEND arguments: [ // fd: fd_dm_ctl (resource) // cmd: const = 0xc138fd06 (4 bytes) // arg: ptr[inout, dm_ioctl_t[dm_id_lookup, void, const[0, int32], const[0, // int32]]] { // dm_ioctl_t[dm_id_lookup, void, const[0, int32], const[0, int32]] { // version_major: const = 0x4 (4 bytes) // version_minor: const = 0x0 (4 bytes) // version_patch: const = 0x0 (4 bytes) // data_size: bytesize = 0x138 (4 bytes) // data_start: offsetof = 0x138 (4 bytes) // target_count: const = 0x0 (4 bytes) // open_count: const = 0x0 (4 bytes) // flags: dm_ioctl_flags = 0x0 (4 bytes) // event_nr: const = 0x0 (4 bytes) // padding: const = 0x0 (4 bytes) // id: union dm_id_lookup { // by_name: dm_id_by_name { // dev: dm_dev_number (resource) // name: dm_dev_name { // prefix: buffer: {73 79 7a} (length 0x3) // proc: proc = 0x0 (1 bytes) // slot: buffer: {31 00} (length 0x2) // pad = 0x0 (122 bytes) // } // pad = 0x0 (136 bytes) // } // } // data: buffer: {} (length 0x0) // } // } // ] *(uint32_t*)0x200000000500 = 4; *(uint32_t*)0x200000000504 = 0; *(uint32_t*)0x200000000508 = 0; *(uint32_t*)0x20000000050c = 0x138; *(uint32_t*)0x200000000510 = 0x138; *(uint32_t*)0x200000000514 = 0; *(uint32_t*)0x200000000518 = 0; *(uint32_t*)0x20000000051c = 0; *(uint32_t*)0x200000000520 = 0; *(uint32_t*)0x200000000524 = 0; memcpy((void*)0x200000000530, "syz", 3); *(uint8_t*)0x200000000533 = 0x61; memcpy((void*)0x200000000534, "1\000", 2); syscall(__NR_ioctl, /*fd=*/r[0], /*cmd=*/0xc138fd06, /*arg=*/0x200000000500ul); // mknodat$dm arguments: [ // dirfd: const = 0xffffff9c (8 bytes) // file: ptr[in, buffer] { // buffer: {2e 2f 64 6d 30 00} (length 0x6) // } // mode: const = 0x6180 (8 bytes) // dev: dm_dev_number (resource) // ] memcpy((void*)0x200000000640, "./dm0\000", 6); syscall(__NR_mknodat, /*dirfd=*/0xffffff9cul, /*file=*/0x200000000640ul, /*mode=*/0x6180ul, /*dev=*/r[1]); // openat$dm_dev arguments: [ // fd: const = 0xffffff9c (8 bytes) // file: ptr[in, buffer] { // buffer: {2e 2f 64 6d 30 00} (length 0x6) // } // flags: dm_open_flags = 0x2 (4 bytes) // mode: const = 0x0 (2 bytes) // ] // returns fd_dm memcpy((void*)0x200000000680, "./dm0\000", 6); res = syscall(__NR_openat, /*fd=*/0xffffff9cul, /*file=*/0x200000000680ul, /*flags=O_RDWR*/ 2, /*mode=*/0); if (res != -1) r[2] = res; // pwrite64 arguments: [ // fd: fd (resource) // buf: ptr[in, buffer] { // buffer: {01 02 03 04 05 06 07 08} (length 0x8) // } // count: len = 0x8 (8 bytes) // pos: intptr = 0x0 (8 bytes) // ] memcpy((void*)0x2000000006c0, "\x01\x02\x03\x04\x05\x06\x07\x08", 8); syscall(__NR_pwrite64, /*fd=*/r[2], /*buf=*/0x2000000006c0ul, /*count=*/8ul, /*pos=*/0ul); } int main(void) { syscall(__NR_mmap, /*addr=*/0x1ffffffff000ul, /*len=*/0x1000, /*prot=*/0ul, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/(intptr_t)-1, /*offset=*/0ul); syscall(__NR_mmap, /*addr=*/0x200000000000ul, /*len=*/0x1000000, /*prot=PROT_WRITE|PROT_READ|PROT_EXEC*/ 7ul, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/(intptr_t)-1, /*offset=*/0ul); syscall(__NR_mmap, /*addr=*/0x200001000000ul, /*len=*/0x1000, /*prot=*/0ul, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/(intptr_t)-1, /*offset=*/0ul); setup_sysctl(); const char* reason; (void)reason; if ((reason = setup_swap())) printf("the reproducer may not work as expected: swap setup failed: %s\n", reason); do_sandbox_none(); return 0; }