// https://syzkaller.appspot.com/bug?id=f9c94b10e49ae0433f27c4838c7e0f0a321606f5 // 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 __attribute__((noreturn)) static void doexit(int status) { volatile unsigned i; syscall(__NR_exit_group, status); for (i = 0;; i++) { } } #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 __thread int skip_segv; static __thread jmp_buf segv_env; static void segv_handler(int sig, siginfo_t* info, void* uctx) { uintptr_t addr = (uintptr_t)info->si_addr; const uintptr_t prog_start = 1 << 20; const uintptr_t prog_end = 100 << 20; if (__atomic_load_n(&skip_segv, __ATOMIC_RELAXED) && (addr < prog_start || addr > prog_end)) { _longjmp(segv_env, 1); } doexit(sig); } static void install_segv_handler() { struct sigaction sa; memset(&sa, 0, sizeof(sa)); sa.sa_handler = SIG_IGN; syscall(SYS_rt_sigaction, 0x20, &sa, NULL, 8); syscall(SYS_rt_sigaction, 0x21, &sa, NULL, 8); memset(&sa, 0, sizeof(sa)); sa.sa_sigaction = segv_handler; sa.sa_flags = SA_NODEFER | SA_SIGINFO; sigaction(SIGSEGV, &sa, NULL); sigaction(SIGBUS, &sa, NULL); } #define NONFAILING(...) \ { \ __atomic_fetch_add(&skip_segv, 1, __ATOMIC_SEQ_CST); \ if (_setjmp(segv_env) == 0) { \ __VA_ARGS__; \ } \ __atomic_fetch_sub(&skip_segv, 1, __ATOMIC_SEQ_CST); \ } static uint64_t current_time_ms() { struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts)) fail("clock_gettime failed"); return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000; } struct ipt_getinfo { char name[32]; unsigned int valid_hooks; unsigned int hook_entry[5]; unsigned int underflow[5]; unsigned int num_entries; unsigned int size; }; struct ipt_get_entries { char name[32]; unsigned int size; void* entrytable[1024 / sizeof(void*)]; }; struct xt_counters { uint64_t pcnt, bcnt; }; struct ipt_replace { char name[32]; unsigned int valid_hooks; unsigned int num_entries; unsigned int size; unsigned int hook_entry[5]; unsigned int underflow[5]; unsigned int num_counters; struct xt_counters* counters; char entrytable[1024]; }; struct ipt_table_desc { const char* name; struct ipt_getinfo info; struct ipt_get_entries entries; struct ipt_replace replace; struct xt_counters counters[10]; }; static struct ipt_table_desc ipv4_tables[] = { {.name = "filter"}, {.name = "nat"}, {.name = "mangle"}, {.name = "raw"}, {.name = "security"}, }; #define IPT_BASE_CTL 64 #define IPT_SO_SET_REPLACE (IPT_BASE_CTL) #define IPT_SO_GET_INFO (IPT_BASE_CTL) #define IPT_SO_GET_ENTRIES (IPT_BASE_CTL + 1) static void checkpoint_net_namespace(void) { socklen_t optlen; unsigned i; int fd; fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (fd == -1) fail("socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)"); for (i = 0; i < sizeof(ipv4_tables) / sizeof(ipv4_tables[0]); i++) { struct ipt_table_desc* table = &ipv4_tables[i]; strcpy(table->info.name, table->name); strcpy(table->entries.name, table->name); strcpy(table->replace.name, table->name); optlen = sizeof(table->info); if (getsockopt(fd, SOL_IP, IPT_SO_GET_INFO, &table->info, &optlen)) { switch (errno) { case EPERM: case ENOENT: case ENOPROTOOPT: continue; } fail("getsockopt(IPT_SO_GET_INFO)"); } if (table->info.size > sizeof(table->entries.entrytable)) fail("table size is too large: %u", table->info.size); if (table->info.num_entries > sizeof(table->counters) / sizeof(table->counters[0])) fail("too many counters: %u", table->info.num_entries); table->entries.size = table->info.size; optlen = sizeof(table->entries) - sizeof(table->entries.entrytable) + table->info.size; if (getsockopt(fd, SOL_IP, IPT_SO_GET_ENTRIES, &table->entries, &optlen)) fail("getsockopt(IPT_SO_GET_ENTRIES)"); table->replace.valid_hooks = table->info.valid_hooks; table->replace.num_entries = table->info.num_entries; table->replace.counters = table->counters; table->replace.size = table->info.size; memcpy(table->replace.hook_entry, table->info.hook_entry, sizeof(table->replace.hook_entry)); memcpy(table->replace.underflow, table->info.underflow, sizeof(table->replace.underflow)); memcpy(table->replace.entrytable, table->entries.entrytable, table->info.size); } close(fd); } static void reset_net_namespace(void) { struct ipt_get_entries entries; struct ipt_getinfo info; socklen_t optlen; unsigned i; int fd; memset(&info, 0, sizeof(info)); memset(&entries, 0, sizeof(entries)); fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (fd == -1) fail("socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)"); for (i = 0; i < sizeof(ipv4_tables) / sizeof(ipv4_tables[0]); i++) { struct ipt_table_desc* table = &ipv4_tables[i]; if (table->info.valid_hooks == 0) continue; strcpy(info.name, table->name); optlen = sizeof(info); if (getsockopt(fd, SOL_IP, IPT_SO_GET_INFO, &info, &optlen)) fail("getsockopt(IPT_SO_GET_INFO)"); if (memcmp(&table->info, &info, sizeof(table->info)) == 0) { strcpy(entries.name, table->name); entries.size = table->info.size; optlen = sizeof(entries) - sizeof(entries.entrytable) + entries.size; if (getsockopt(fd, SOL_IP, IPT_SO_GET_ENTRIES, &entries, &optlen)) fail("getsockopt(IPT_SO_GET_ENTRIES)"); if (memcmp(&table->entries, &entries, optlen) == 0) continue; } table->replace.num_counters = info.num_entries; optlen = sizeof(table->replace) - sizeof(table->replace.entrytable) + table->replace.size; if (setsockopt(fd, SOL_IP, IPT_SO_SET_REPLACE, &table->replace, optlen)) fail("setsockopt(IPT_SO_SET_REPLACE)"); } close(fd); } static void test(); void loop() { int iter; checkpoint_net_namespace(); for (iter = 0;; iter++) { int pid = fork(); if (pid < 0) fail("loop fork failed"); if (pid == 0) { prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0); setpgrp(); test(); doexit(0); } int status = 0; uint64_t start = current_time_ms(); for (;;) { int res = waitpid(-1, &status, __WALL | WNOHANG); if (res == pid) break; usleep(1000); if (current_time_ms() - start > 5 * 1000) { kill(-pid, SIGKILL); kill(pid, SIGKILL); while (waitpid(-1, &status, __WALL) != pid) { } break; } } reset_net_namespace(); } } long r[3]; void test() { memset(r, -1, sizeof(r)); syscall(__NR_mmap, 0x20000000, 0xfff000, 3, 0x32, -1, 0); r[0] = syscall(__NR_socket, 0xa, 0x80002, 0x88); NONFAILING(*(uint16_t*)0x208a8000 = 0xa); NONFAILING(*(uint16_t*)0x208a8002 = htobe16(0x4e23)); NONFAILING(*(uint32_t*)0x208a8004 = 0); NONFAILING(*(uint8_t*)0x208a8008 = 0); NONFAILING(*(uint8_t*)0x208a8009 = 0); NONFAILING(*(uint8_t*)0x208a800a = 0); NONFAILING(*(uint8_t*)0x208a800b = 0); NONFAILING(*(uint8_t*)0x208a800c = 0); NONFAILING(*(uint8_t*)0x208a800d = 0); NONFAILING(*(uint8_t*)0x208a800e = 0); NONFAILING(*(uint8_t*)0x208a800f = 0); NONFAILING(*(uint8_t*)0x208a8010 = 0); NONFAILING(*(uint8_t*)0x208a8011 = 0); NONFAILING(*(uint8_t*)0x208a8012 = 0); NONFAILING(*(uint8_t*)0x208a8013 = 0); NONFAILING(*(uint8_t*)0x208a8014 = 0); NONFAILING(*(uint8_t*)0x208a8015 = 0); NONFAILING(*(uint8_t*)0x208a8016 = 0); NONFAILING(*(uint8_t*)0x208a8017 = 0); NONFAILING(*(uint32_t*)0x208a8018 = 0); syscall(__NR_bind, r[0], 0x208a8000, 0x1c); r[1] = syscall(__NR_socket, 0xa, 0x8000000000000802, 0); syscall(__NR_dup3, r[0], r[1], 0); NONFAILING(*(uint64_t*)0x20a29000 = 0x20685ff0); NONFAILING(*(uint32_t*)0x20a29008 = 0x10); NONFAILING(*(uint64_t*)0x20a29010 = 0x20fc5fc0); NONFAILING(*(uint64_t*)0x20a29018 = 0); NONFAILING(*(uint64_t*)0x20a29020 = 0x20000000); NONFAILING(*(uint64_t*)0x20a29028 = 0); NONFAILING(*(uint32_t*)0x20a29030 = 0); NONFAILING(*(uint16_t*)0x20685ff0 = 2); NONFAILING(*(uint16_t*)0x20685ff2 = htobe16(0x4e23)); NONFAILING(*(uint32_t*)0x20685ff4 = htobe32(0)); NONFAILING(*(uint8_t*)0x20685ff8 = 0); NONFAILING(*(uint8_t*)0x20685ff9 = 0); NONFAILING(*(uint8_t*)0x20685ffa = 0); NONFAILING(*(uint8_t*)0x20685ffb = 0); NONFAILING(*(uint8_t*)0x20685ffc = 0); NONFAILING(*(uint8_t*)0x20685ffd = 0); NONFAILING(*(uint8_t*)0x20685ffe = 0); NONFAILING(*(uint8_t*)0x20685fff = 0); syscall(__NR_sendmsg, r[1], 0x20a29000, 0x8000); NONFAILING(*(uint16_t*)0x200dcfe4 = 0xa); NONFAILING(*(uint16_t*)0x200dcfe6 = 0); NONFAILING(*(uint32_t*)0x200dcfe8 = 0); NONFAILING(*(uint8_t*)0x200dcfec = 0xfe); NONFAILING(*(uint8_t*)0x200dcfed = 0x80); NONFAILING(*(uint8_t*)0x200dcfee = 0); NONFAILING(*(uint8_t*)0x200dcfef = 0); NONFAILING(*(uint8_t*)0x200dcff0 = 0); NONFAILING(*(uint8_t*)0x200dcff1 = 0); NONFAILING(*(uint8_t*)0x200dcff2 = 0); NONFAILING(*(uint8_t*)0x200dcff3 = 0); NONFAILING(*(uint8_t*)0x200dcff4 = 0); NONFAILING(*(uint8_t*)0x200dcff5 = 0); NONFAILING(*(uint8_t*)0x200dcff6 = 0); NONFAILING(*(uint8_t*)0x200dcff7 = 0); NONFAILING(*(uint8_t*)0x200dcff8 = 0); NONFAILING(*(uint8_t*)0x200dcff9 = 0); NONFAILING(*(uint8_t*)0x200dcffa = 0); NONFAILING(*(uint8_t*)0x200dcffb = 0xbb); NONFAILING(*(uint32_t*)0x200dcffc = 0); syscall(__NR_sendto, r[1], 0x20ebcfa5, 0xfe21, 0, 0x200dcfe4, 0x76); NONFAILING(memcpy((void*)0x20005faa, "\xcd\x39\x97\xfb\x0f\x00", 6)); NONFAILING(*(uint8_t*)0x20005fb0 = 0); NONFAILING(*(uint8_t*)0x20005fb1 = 0); NONFAILING(*(uint8_t*)0x20005fb2 = 0); NONFAILING(*(uint8_t*)0x20005fb3 = 0); NONFAILING(*(uint8_t*)0x20005fb4 = 0); NONFAILING(*(uint8_t*)0x20005fb5 = 0); NONFAILING(*(uint16_t*)0x20005fb6 = htobe16(0x86dd)); NONFAILING(STORE_BY_BITMASK(uint8_t, 0x20005fb8, 0, 0, 4)); NONFAILING(STORE_BY_BITMASK(uint8_t, 0x20005fb8, 6, 4, 4)); NONFAILING(memcpy((void*)0x20005fb9, "\xc8\xfb\xa2", 3)); NONFAILING(*(uint16_t*)0x20005fbc = htobe16(0x18)); NONFAILING(*(uint8_t*)0x20005fbe = 0); NONFAILING(*(uint8_t*)0x20005fbf = 0); NONFAILING(*(uint8_t*)0x20005fc0 = 0); NONFAILING(*(uint8_t*)0x20005fc1 = 0); NONFAILING(*(uint8_t*)0x20005fc2 = 0); NONFAILING(*(uint8_t*)0x20005fc3 = 0); NONFAILING(*(uint8_t*)0x20005fc4 = 0); NONFAILING(*(uint8_t*)0x20005fc5 = 0); NONFAILING(*(uint8_t*)0x20005fc6 = 0); NONFAILING(*(uint8_t*)0x20005fc7 = 0); NONFAILING(*(uint8_t*)0x20005fc8 = 0); NONFAILING(*(uint8_t*)0x20005fc9 = 0); NONFAILING(*(uint8_t*)0x20005fca = 0); NONFAILING(*(uint8_t*)0x20005fcb = 0); NONFAILING(*(uint8_t*)0x20005fcc = 0); NONFAILING(*(uint8_t*)0x20005fcd = 0); NONFAILING(*(uint8_t*)0x20005fce = 0); NONFAILING(*(uint8_t*)0x20005fcf = 0); NONFAILING(*(uint8_t*)0x20005fd0 = -1); NONFAILING(*(uint8_t*)0x20005fd1 = 2); NONFAILING(*(uint8_t*)0x20005fd2 = 0); NONFAILING(*(uint8_t*)0x20005fd3 = 0); NONFAILING(*(uint8_t*)0x20005fd4 = 0); NONFAILING(*(uint8_t*)0x20005fd5 = 0); NONFAILING(*(uint8_t*)0x20005fd6 = 0); NONFAILING(*(uint8_t*)0x20005fd7 = 0); NONFAILING(*(uint8_t*)0x20005fd8 = 0); NONFAILING(*(uint8_t*)0x20005fd9 = 0); NONFAILING(*(uint8_t*)0x20005fda = 0); NONFAILING(*(uint8_t*)0x20005fdb = 0); NONFAILING(*(uint8_t*)0x20005fdc = 0); NONFAILING(*(uint8_t*)0x20005fdd = 0); NONFAILING(*(uint8_t*)0x20005fde = 0); NONFAILING(*(uint8_t*)0x20005fdf = 1); NONFAILING(*(uint8_t*)0x20005fe0 = 0); NONFAILING(*(uint8_t*)0x20005fe1 = 0); NONFAILING(*(uint16_t*)0x20005fe2 = 0); NONFAILING(*(uint16_t*)0x20005fe4 = htobe16(0)); NONFAILING(*(uint16_t*)0x20005fe6 = 0); NONFAILING(*(uint8_t*)0x20005fe8 = 0xfe); NONFAILING(*(uint8_t*)0x20005fe9 = 0x80); NONFAILING(*(uint8_t*)0x20005fea = 0); NONFAILING(*(uint8_t*)0x20005feb = 0); NONFAILING(*(uint8_t*)0x20005fec = 0); NONFAILING(*(uint8_t*)0x20005fed = 0); NONFAILING(*(uint8_t*)0x20005fee = 0); NONFAILING(*(uint8_t*)0x20005fef = 0); NONFAILING(*(uint8_t*)0x20005ff0 = 0); NONFAILING(*(uint8_t*)0x20005ff1 = 0); NONFAILING(*(uint8_t*)0x20005ff2 = 0); NONFAILING(*(uint8_t*)0x20005ff3 = 0); NONFAILING(*(uint8_t*)0x20005ff4 = 0); NONFAILING(*(uint8_t*)0x20005ff5 = 0); NONFAILING(*(uint8_t*)0x20005ff6 = 0); NONFAILING(*(uint8_t*)0x20005ff7 = 0xaa); struct csum_inet csum_1; csum_inet_init(&csum_1); NONFAILING(csum_inet_update(&csum_1, (const uint8_t*)0x20005fc0, 16)); NONFAILING(csum_inet_update(&csum_1, (const uint8_t*)0x20005fd0, 16)); uint32_t csum_1_chunk_2 = 0x18000000; csum_inet_update(&csum_1, (const uint8_t*)&csum_1_chunk_2, 4); uint32_t csum_1_chunk_3 = 0x3a000000; csum_inet_update(&csum_1, (const uint8_t*)&csum_1_chunk_3, 4); NONFAILING(csum_inet_update(&csum_1, (const uint8_t*)0x20005fe0, 24)); NONFAILING(*(uint16_t*)0x20005fe2 = csum_inet_digest(&csum_1)); r[2] = syscall(__NR_socket, 0x10, 3, 6); NONFAILING(*(uint64_t*)0x20629000 = 0x2059cff4); NONFAILING(*(uint32_t*)0x20629008 = 0xc); NONFAILING(*(uint64_t*)0x20629010 = 0x20f9bff0); NONFAILING(*(uint64_t*)0x20629018 = 1); NONFAILING(*(uint64_t*)0x20629020 = 0); NONFAILING(*(uint64_t*)0x20629028 = 0); NONFAILING(*(uint32_t*)0x20629030 = 0); NONFAILING(*(uint16_t*)0x2059cff4 = 0x10); NONFAILING(*(uint16_t*)0x2059cff6 = 0); NONFAILING(*(uint32_t*)0x2059cff8 = 0); NONFAILING(*(uint32_t*)0x2059cffc = 0); NONFAILING(*(uint64_t*)0x20f9bff0 = 0x20e45000); NONFAILING(*(uint64_t*)0x20f9bff8 = 0xb8); NONFAILING(*(uint32_t*)0x20e45000 = 0xb8); NONFAILING(*(uint16_t*)0x20e45004 = 0x19); NONFAILING(*(uint16_t*)0x20e45006 = 0x401); NONFAILING(*(uint32_t*)0x20e45008 = 0); NONFAILING(*(uint32_t*)0x20e4500c = 0); NONFAILING(*(uint32_t*)0x20e45010 = htobe32(0xe0000001)); NONFAILING(*(uint8_t*)0x20e45020 = 0xac); NONFAILING(*(uint8_t*)0x20e45021 = 0x14); NONFAILING(*(uint8_t*)0x20e45022 = 0); NONFAILING(*(uint8_t*)0x20e45023 = 0); NONFAILING(*(uint16_t*)0x20e45030 = 0); NONFAILING(*(uint16_t*)0x20e45032 = htobe16(0)); NONFAILING(*(uint16_t*)0x20e45034 = 0); NONFAILING(*(uint16_t*)0x20e45036 = htobe16(0)); NONFAILING(*(uint16_t*)0x20e45038 = 0xa); NONFAILING(*(uint8_t*)0x20e4503a = 0); NONFAILING(*(uint8_t*)0x20e4503b = 0); NONFAILING(*(uint8_t*)0x20e4503c = 0); NONFAILING(*(uint32_t*)0x20e45040 = 0); NONFAILING(*(uint32_t*)0x20e45044 = 0); NONFAILING(*(uint64_t*)0x20e45048 = 0); NONFAILING(*(uint64_t*)0x20e45050 = 0); NONFAILING(*(uint64_t*)0x20e45058 = 0); NONFAILING(*(uint64_t*)0x20e45060 = 0); NONFAILING(*(uint64_t*)0x20e45068 = 0); NONFAILING(*(uint64_t*)0x20e45070 = 0); NONFAILING(*(uint64_t*)0x20e45078 = 0); NONFAILING(*(uint64_t*)0x20e45080 = 0); NONFAILING(*(uint64_t*)0x20e45088 = 0); NONFAILING(*(uint64_t*)0x20e45090 = 0); NONFAILING(*(uint64_t*)0x20e45098 = 0); NONFAILING(*(uint64_t*)0x20e450a0 = 0); NONFAILING(*(uint32_t*)0x20e450a8 = 0); NONFAILING(*(uint32_t*)0x20e450ac = 0); NONFAILING(*(uint8_t*)0x20e450b0 = 0); NONFAILING(*(uint8_t*)0x20e450b1 = 0); NONFAILING(*(uint8_t*)0x20e450b2 = 0); NONFAILING(*(uint8_t*)0x20e450b3 = 0); syscall(__NR_sendmsg, r[2], 0x20629000, 0); } int main() { install_segv_handler(); for (;;) { loop(); } }