// https://syzkaller.appspot.com/bug?id=68432c6ea11f9bb57f39995aaf6bf58eb9d46b17 // 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 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 thread_start(void* (*fn)(void*), void* arg) { pthread_t th; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setstacksize(&attr, 128 << 10); int i; for (i = 0; i < 100; i++) { if (pthread_create(&th, &attr, fn, arg) == 0) { pthread_attr_destroy(&attr); return; } if (errno == EAGAIN) { usleep(50); continue; } break; } exit(1); } typedef struct { pthread_mutex_t mu; pthread_cond_t cv; int state; } event_t; static void event_init(event_t* ev) { if (pthread_mutex_init(&ev->mu, 0)) exit(1); if (pthread_cond_init(&ev->cv, 0)) exit(1); ev->state = 0; } static void event_reset(event_t* ev) { ev->state = 0; } static void event_set(event_t* ev) { pthread_mutex_lock(&ev->mu); if (ev->state) exit(1); ev->state = 1; pthread_mutex_unlock(&ev->mu); pthread_cond_broadcast(&ev->cv); } static void event_wait(event_t* ev) { pthread_mutex_lock(&ev->mu); while (!ev->state) pthread_cond_wait(&ev->cv, &ev->mu); pthread_mutex_unlock(&ev->mu); } static int event_isset(event_t* ev) { pthread_mutex_lock(&ev->mu); int res = ev->state; pthread_mutex_unlock(&ev->mu); return res; } static int event_timedwait(event_t* ev, uint64_t timeout) { uint64_t start = current_time_ms(); uint64_t now = start; pthread_mutex_lock(&ev->mu); for (;;) { if (ev->state) break; uint64_t remain = timeout - (now - start); struct timespec ts; ts.tv_sec = remain / 1000; ts.tv_nsec = (remain % 1000) * 1000 * 1000; pthread_cond_timedwait(&ev->cv, &ev->mu, &ts); now = current_time_ms(); if (now - start > timeout) break; } int res = ev->state; pthread_mutex_unlock(&ev->mu); return res; } struct thread_t { int created, call; event_t ready, done; }; static struct thread_t threads[16]; static void execute_call(int call); static int running; static void* thr(void* arg) { struct thread_t* th = (struct thread_t*)arg; for (;;) { event_wait(&th->ready); event_reset(&th->ready); execute_call(th->call); __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED); event_set(&th->done); } return 0; } static void execute_one(void) { int i, call, thread; int collide = 0; again: for (call = 0; call < 6; call++) { for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0])); thread++) { struct thread_t* th = &threads[thread]; if (!th->created) { th->created = 1; event_init(&th->ready); event_init(&th->done); event_set(&th->done); thread_start(thr, th); } if (!event_isset(&th->done)) continue; event_reset(&th->done); th->call = call; __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED); event_set(&th->ready); if (collide && (call % 2) == 0) break; event_timedwait(&th->done, 45); break; } } for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++) sleep_ms(1); if (!collide) { collide = 1; goto again; } } static void execute_one(void); #define WAIT_FLAGS 0 static void loop(void) { int iter; for (iter = 0;; iter++) { int pid = fork(); if (pid < 0) exit(1); if (pid == 0) { execute_one(); exit(0); } int status = 0; uint64_t start = current_time_ms(); for (;;) { if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid) break; sleep_ms(1); if (current_time_ms() - start < 5 * 1000) continue; kill_and_wait(pid, &status); break; } } } #ifndef SYS_mmap #define SYS_mmap 197 #endif #ifndef SYS_open #define SYS_open 5 #endif #ifndef SYS_writev #define SYS_writev 121 #endif uint64_t r[3] = {0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff}; void execute_call(int call) { intptr_t res = 0; switch (call) { case 0: memcpy((void*)0x20000040, "./file0\000", 8); res = syscall(SYS_open, 0x20000040ul, 0x615ul, 0ul); if (res != -1) r[0] = res; break; case 1: memcpy((void*)0x20000100, "./file0\000", 8); res = syscall(SYS_open, 0x20000100ul, 0x40000400000002c2ul, 0ul); if (res != -1) r[1] = res; break; case 2: *(uint64_t*)0x20000340 = 0x20000180; *(uint64_t*)0x20000348 = 0x81700; syscall(SYS_writev, r[1], 0x20000340ul, 0x1000000000000013ul); break; case 3: syscall(SYS_mmap, 0x20000000ul, 0x3000ul, 2ul, 0x11ul, r[0], 0ul, 0ul); break; case 4: memcpy((void*)0x20000100, "./file0\000", 8); res = syscall(SYS_open, 0x20000100ul, 0x40000400000002c2ul, 0ul); if (res != -1) r[2] = res; break; case 5: *(uint64_t*)0x20000640 = 0; *(uint64_t*)0x20000648 = 0; *(uint64_t*)0x20000650 = 0; *(uint64_t*)0x20000658 = 0; *(uint64_t*)0x20000660 = 0x20000200; memcpy((void*)0x20000200, "\x96\x39\x58\x2f\x8d\xca\x02\xc5\x51\x32\x6f\xdb\xfe\xbf\x28\x92" "\x69\x4d\x6d\x01\x83\x4e\x6e\x90\x5d\x56\xec\x43\xd6\x60\xd1\x36" "\x0c\x7c\x4c\xc1\xf9\x80\x6d\xdb\x0d\xd7\x09\xf5\xc7\x48\xfe\xf7" "\xc8\x56\xec\xe9\xa0\x99\x12\x23\x44\xf1\x7a\xc6\xee\x99\x56\xaa" "\x35\x37\x26\xe5\x10\xad\xd8\x49\x5c\xc4\x81\x15\xab\xdf\x19\x03" "\x68\xb9\x4b\x63\x28\x2a\x24\xb9\xa1\x69\x6a\x26\x65\xde\x0b\xe8" "\x63\x41\x35\x7d\xc7\x15\x47\xe5\x13\x6a\x44\x0b\x93\x8f\x50\x0b" "\xa3\xc6\xff\x0e\x2a\xd5\x84\xa9\x13\x36\x40\xca\xef\xef\x48\xac" "\x32\x0b\x0c\x04\x77\x73\x3b\xcc\x06\x78\x70\x31\x9c\x8a\x89\xaa" "\x6f\x15\xb4\xb6\xcc\xbf\x15\x1a\x37\x5a\xa4\x3b\x0b\x7a\xa2\x78" "\x90\x73\x21\x32\xd9\x9a\x67\x73\xa2\xe1\x33\x23\xf4\x49\x41\x27" "\x54\x7b\x97\x97\x10\x88\xa1\x23\xb0\x54\x90\x99\x86\x59\xfd\x29" "\x8d\xb4\x4e\x47\x8b\x44\x72\xa2\x9e\x4a\x15\xbc\xdd\x62\x9b\xff" "\x4c\x88\x85\x57\x73\xd3\x4c\x94\x17\x59\x56\xd5\xca\xae\x12\x76" "\xaa", 225); *(uint64_t*)0x20000668 = 0xe1; *(uint64_t*)0x20000670 = 0; *(uint64_t*)0x20000678 = 0; *(uint64_t*)0x20000680 = 0; *(uint64_t*)0x20000688 = 0; *(uint64_t*)0x20000690 = 0; *(uint64_t*)0x20000698 = 0; *(uint64_t*)0x200006a0 = 0; *(uint64_t*)0x200006a8 = 0; *(uint64_t*)0x200006b0 = 0; *(uint64_t*)0x200006b8 = 0; *(uint64_t*)0x200006c0 = 0; *(uint64_t*)0x200006c8 = 0; *(uint64_t*)0x200006d0 = 0; *(uint64_t*)0x200006d8 = 0; *(uint64_t*)0x200006e0 = 0; *(uint64_t*)0x200006e8 = 0; *(uint64_t*)0x200006f0 = 0; *(uint64_t*)0x200006f8 = 0; syscall(SYS_writev, r[2], 0x20000640ul, 0xcul); break; } } int main(void) { syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 3ul, 0x1012ul, -1, 0ul, 0ul); for (procid = 0; procid < 6; procid++) { if (fork() == 0) { loop(); } } sleep(1000000); return 0; }