// https://syzkaller.appspot.com/bug?id=4462682cd32aee8ff03a43c8b9be3963743bc506 // autogenerated by syzkaller (http://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 __attribute__((noreturn)) static void doexit(int status) { volatile unsigned i; syscall(__NR_exit_group, status); for (i = 0;; i++) { } } #include #include #include #include #include #include #include const int kFailStatus = 67; const int kRetryStatus = 69; static void fail(const char* msg, ...) { int e = errno; va_list args; va_start(args, msg); vfprintf(stderr, msg, args); va_end(args); fprintf(stderr, " (errno %d)\n", e); doexit((e == ENOMEM || e == EAGAIN) ? kRetryStatus : kFailStatus); } #define BITMASK_LEN(type, bf_len) (type)((1ull << (bf_len)) - 1) #define BITMASK_LEN_OFF(type, bf_off, bf_len) \ (type)(BITMASK_LEN(type, (bf_len)) << (bf_off)) #define STORE_BY_BITMASK(type, addr, val, bf_off, bf_len) \ if ((bf_off) == 0 && (bf_len) == 0) { \ *(type*)(addr) = (type)(val); \ } else { \ type new_val = *(type*)(addr); \ new_val &= ~BITMASK_LEN_OFF(type, (bf_off), (bf_len)); \ new_val |= ((type)(val)&BITMASK_LEN(type, (bf_len))) << (bf_off); \ *(type*)(addr) = new_val; \ } struct csum_inet { uint32_t acc; }; static void csum_inet_init(struct csum_inet* csum) { csum->acc = 0; } static void csum_inet_update(struct csum_inet* csum, const uint8_t* data, size_t length) { if (length == 0) return; size_t i; for (i = 0; i < length - 1; i += 2) csum->acc += *(uint16_t*)&data[i]; if (length & 1) csum->acc += (uint16_t)data[length - 1]; while (csum->acc > 0xffff) csum->acc = (csum->acc & 0xffff) + (csum->acc >> 16); } static uint16_t csum_inet_digest(struct csum_inet* csum) { return ~csum->acc; } static void use_temporary_dir() { char tmpdir_template[] = "./syzkaller.XXXXXX"; char* tmpdir = mkdtemp(tmpdir_template); if (!tmpdir) fail("failed to mkdtemp"); if (chmod(tmpdir, 0777)) fail("failed to chmod"); if (chdir(tmpdir)) fail("failed to chdir"); } static uintptr_t syz_open_dev(uintptr_t a0, uintptr_t a1, uintptr_t a2) { if (a0 == 0xc || a0 == 0xb) { char buf[128]; sprintf(buf, "/dev/%s/%d:%d", a0 == 0xc ? "char" : "block", (uint8_t)a1, (uint8_t)a2); return open(buf, O_RDWR, 0); } else { char buf[1024]; char* hash; strncpy(buf, (char*)a0, sizeof(buf)); buf[sizeof(buf) - 1] = 0; while ((hash = strchr(buf, '#'))) { *hash = '0' + (char)(a1 % 10); a1 /= 10; } return open(buf, a2, 0); } } static void loop(); static void sandbox_common() { prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0); setpgrp(); setsid(); struct rlimit rlim; rlim.rlim_cur = rlim.rlim_max = 128 << 20; setrlimit(RLIMIT_AS, &rlim); rlim.rlim_cur = rlim.rlim_max = 8 << 20; setrlimit(RLIMIT_MEMLOCK, &rlim); rlim.rlim_cur = rlim.rlim_max = 1 << 20; setrlimit(RLIMIT_FSIZE, &rlim); rlim.rlim_cur = rlim.rlim_max = 1 << 20; setrlimit(RLIMIT_STACK, &rlim); rlim.rlim_cur = rlim.rlim_max = 0; setrlimit(RLIMIT_CORE, &rlim); unshare(CLONE_NEWNS); unshare(CLONE_NEWIPC); unshare(CLONE_IO); } 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) { close(fd); return false; } close(fd); return true; } static int real_uid; static int real_gid; __attribute__((aligned(64 << 10))) static char sandbox_stack[1 << 20]; static int namespace_sandbox_proc(void* arg) { sandbox_common(); write_file("/proc/self/setgroups", "deny"); if (!write_file("/proc/self/uid_map", "0 %d 1\n", real_uid)) fail("write of /proc/self/uid_map failed"); if (!write_file("/proc/self/gid_map", "0 %d 1\n", real_gid)) fail("write of /proc/self/gid_map failed"); if (mkdir("./syz-tmp", 0777)) fail("mkdir(syz-tmp) failed"); if (mount("", "./syz-tmp", "tmpfs", 0, NULL)) fail("mount(tmpfs) failed"); if (mkdir("./syz-tmp/newroot", 0777)) fail("mkdir failed"); if (mkdir("./syz-tmp/newroot/dev", 0700)) fail("mkdir failed"); if (mount("/dev", "./syz-tmp/newroot/dev", NULL, MS_BIND | MS_REC | MS_PRIVATE, NULL)) fail("mount(dev) failed"); if (mkdir("./syz-tmp/newroot/proc", 0700)) fail("mkdir failed"); if (mount(NULL, "./syz-tmp/newroot/proc", "proc", 0, NULL)) fail("mount(proc) failed"); if (mkdir("./syz-tmp/pivot", 0777)) fail("mkdir failed"); if (syscall(SYS_pivot_root, "./syz-tmp", "./syz-tmp/pivot")) { if (chdir("./syz-tmp")) fail("chdir failed"); } else { if (chdir("/")) fail("chdir failed"); if (umount2("./pivot", MNT_DETACH)) fail("umount failed"); } if (chroot("./newroot")) fail("chroot failed"); if (chdir("/")) fail("chdir failed"); 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)) fail("capget failed"); cap_data[0].effective &= ~(1 << CAP_SYS_PTRACE); cap_data[0].permitted &= ~(1 << CAP_SYS_PTRACE); cap_data[0].inheritable &= ~(1 << CAP_SYS_PTRACE); if (syscall(SYS_capset, &cap_hdr, &cap_data)) fail("capset failed"); loop(); doexit(1); } static int do_sandbox_namespace(int executor_pid, bool enable_tun) { real_uid = getuid(); real_gid = getgid(); mprotect(sandbox_stack, 4096, PROT_NONE); return clone( namespace_sandbox_proc, &sandbox_stack[sizeof(sandbox_stack) - 64], CLONE_NEWUSER | CLONE_NEWPID | CLONE_NEWUTS | CLONE_NEWNET, NULL); } long r[94]; void* thr(void* arg) { switch ((long)arg) { case 0: r[0] = syscall(__NR_mmap, 0x20000000ul, 0xfff000ul, 0x3ul, 0x32ul, 0xfffffffffffffffful, 0x0ul); break; case 1: memcpy((void*)0x2002a000, "\x2f\x64\x65\x76\x2f\x73\x67\x23\x00", 9); r[2] = syz_open_dev(0x2002a000ul, 0x0ul, 0x142ul); break; case 2: *(uint8_t*)0x205c3000 = (uint8_t)0x0; *(uint8_t*)0x205c3001 = (uint8_t)0x0; *(uint16_t*)0x205c3002 = (uint16_t)0x0; *(uint16_t*)0x205c3004 = (uint16_t)0x0; *(uint16_t*)0x205c3006 = (uint16_t)0x2; *(uint16_t*)0x205c3008 = (uint16_t)0x0; STORE_BY_BITMASK(uint8_t, 0x205c300a, 0x0, 0, 4); STORE_BY_BITMASK(uint8_t, 0x205c300a, 0x6, 4, 4); memcpy((void*)0x205c300b, "\xde\x57\x29", 3); *(uint16_t*)0x205c300e = (uint16_t)0x6000; *(uint8_t*)0x205c3010 = (uint8_t)0x0; *(uint8_t*)0x205c3011 = (uint8_t)0x0; *(uint8_t*)0x205c3012 = (uint8_t)0xfe; *(uint8_t*)0x205c3013 = (uint8_t)0x80; *(uint8_t*)0x205c3014 = (uint8_t)0x0; *(uint8_t*)0x205c3015 = (uint8_t)0x0; *(uint8_t*)0x205c3016 = (uint8_t)0x0; *(uint8_t*)0x205c3017 = (uint8_t)0x0; *(uint8_t*)0x205c3018 = (uint8_t)0x0; *(uint8_t*)0x205c3019 = (uint8_t)0x0; *(uint8_t*)0x205c301a = (uint8_t)0x0; *(uint8_t*)0x205c301b = (uint8_t)0x0; *(uint8_t*)0x205c301c = (uint8_t)0x0; *(uint8_t*)0x205c301d = (uint8_t)0x0; *(uint8_t*)0x205c301e = (uint8_t)0x0; *(uint8_t*)0x205c301f = (uint8_t)0x0; *(uint8_t*)0x205c3020 = (uint8_t)0x0; *(uint8_t*)0x205c3021 = (uint8_t)0xbb; *(uint8_t*)0x205c3022 = (uint8_t)0xfe; *(uint8_t*)0x205c3023 = (uint8_t)0x80; *(uint8_t*)0x205c3024 = (uint8_t)0x0; *(uint8_t*)0x205c3025 = (uint8_t)0x0; *(uint8_t*)0x205c3026 = (uint8_t)0x0; *(uint8_t*)0x205c3027 = (uint8_t)0x0; *(uint8_t*)0x205c3028 = (uint8_t)0x0; *(uint8_t*)0x205c3029 = (uint8_t)0x0; *(uint8_t*)0x205c302a = (uint8_t)0x0; *(uint8_t*)0x205c302b = (uint8_t)0x0; *(uint8_t*)0x205c302c = (uint8_t)0x0; *(uint8_t*)0x205c302d = (uint8_t)0x0; *(uint8_t*)0x205c302e = (uint8_t)0x0; *(uint8_t*)0x205c302f = (uint8_t)0x0; *(uint8_t*)0x205c3030 = (uint8_t)0x0; *(uint8_t*)0x205c3031 = (uint8_t)0xaa; *(uint8_t*)0x205c3032 = (uint8_t)0x0; *(uint8_t*)0x205c3033 = (uint8_t)0x0; *(uint8_t*)0x205c3034 = (uint8_t)0x0; *(uint8_t*)0x205c3035 = (uint8_t)0x0; *(uint8_t*)0x205c3036 = (uint8_t)0x0; *(uint8_t*)0x205c3037 = (uint8_t)0x0; *(uint8_t*)0x205c3038 = (uint8_t)0x0; *(uint8_t*)0x205c3039 = (uint8_t)0x0; *(uint16_t*)0x205c303a = (uint16_t)0x204e; *(uint16_t*)0x205c303c = (uint16_t)0x214e; *(uint32_t*)0x205c303e = (uint32_t)0x42424242; *(uint32_t*)0x205c3042 = (uint32_t)0x42424242; STORE_BY_BITMASK(uint8_t, 0x205c3046, 0x0, 0, 1); STORE_BY_BITMASK(uint8_t, 0x205c3046, 0x0, 1, 3); STORE_BY_BITMASK(uint8_t, 0x205c3046, 0x16, 4, 4); *(uint8_t*)0x205c3047 = (uint8_t)0x0; *(uint16_t*)0x205c3048 = (uint16_t)0x0; *(uint16_t*)0x205c304a = (uint16_t)0x0; *(uint16_t*)0x205c304c = (uint16_t)0x0; *(uint8_t*)0x205c304e = (uint8_t)0x5; *(uint8_t*)0x205c304f = (uint8_t)0x26; *(uint32_t*)0x205c3050 = (uint32_t)0x80000; *(uint32_t*)0x205c3054 = (uint32_t)0x9000000; *(uint32_t*)0x205c3058 = (uint32_t)0x1000000; *(uint32_t*)0x205c305c = (uint32_t)0x1f000000; *(uint32_t*)0x205c3060 = (uint32_t)0x41000000; *(uint32_t*)0x205c3064 = (uint32_t)0x5000000; *(uint32_t*)0x205c3068 = (uint32_t)0x1000100; *(uint32_t*)0x205c306c = (uint32_t)0x4000000; *(uint32_t*)0x205c3070 = (uint32_t)0x1010000; *(uint8_t*)0x205c3074 = (uint8_t)0x2; *(uint8_t*)0x205c3075 = (uint8_t)0x4; *(uint16_t*)0x205c3076 = (uint16_t)0xac1; *(uint8_t*)0x205c3078 = (uint8_t)0xfe; *(uint8_t*)0x205c3079 = (uint8_t)0x2; *(uint8_t*)0x205c307a = (uint8_t)0x0; *(uint8_t*)0x205c307b = (uint8_t)0x13; *(uint8_t*)0x205c307c = (uint8_t)0x12; memcpy((void*)0x205c307d, "\x40\x08\x07\xe3\xee\xe9\xb1\x73\xb4\x08" "\x11\x5e\xf4\x5c\x5e\x18", 16); *(uint8_t*)0x205c308d = (uint8_t)0x3; *(uint8_t*)0x205c308e = (uint8_t)0x3; *(uint8_t*)0x205c308f = (uint8_t)0x6; struct csum_inet csum_89; csum_inet_init(&csum_89); csum_inet_update(&csum_89, (const uint8_t*)0x205c3012, 16); csum_inet_update(&csum_89, (const uint8_t*)0x205c3022, 16); uint32_t csum_89_chunk_2 = 0x58000000; csum_inet_update(&csum_89, (const uint8_t*)&csum_89_chunk_2, 4); uint32_t csum_89_chunk_3 = 0x6000000; csum_inet_update(&csum_89, (const uint8_t*)&csum_89_chunk_3, 4); csum_inet_update(&csum_89, (const uint8_t*)0x205c303a, 88); *(uint16_t*)0x205c304a = csum_inet_digest(&csum_89); r[90] = syscall(__NR_write, r[2], 0x205c3000ul, 0x92ul); break; case 3: *(uint64_t*)0x206e4000 = (uint64_t)0x20c7d000; *(uint64_t*)0x206e4008 = (uint64_t)0xb7; r[93] = syscall(__NR_readv, r[2], 0x206e4000ul, 0x1ul); break; } return 0; } void loop() { long i; pthread_t th[8]; memset(r, -1, sizeof(r)); srand(getpid()); for (i = 0; i < 4; i++) { pthread_create(&th[i], 0, thr, (void*)i); usleep(rand() % 10000); } for (i = 0; i < 4; i++) { pthread_create(&th[4 + i], 0, thr, (void*)i); if (rand() % 2) usleep(rand() % 10000); } usleep(rand() % 100000); } int main() { use_temporary_dir(); int pid = do_sandbox_namespace(0, false); int status = 0; while (waitpid(pid, &status, __WALL) != pid) { } return 0; }