// https://syzkaller.appspot.com/bug?id=d770685c526dba78a4c4ee5881d05c2042a68868 // 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 #include #include #include #include #include #include #include #include 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 __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)) { exit(1); } } closedir(dp); while (rmdir(dir)) { exit(1); } } 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 = 0; for (; 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; } #define CAST static void sandbox_common() { struct rlimit 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); rlim.rlim_cur = rlim.rlim_max = 256; setrlimit(RLIMIT_NOFILE, &rlim); } static void loop(); static int do_sandbox_none(void) { sandbox_common(); loop(); return 0; } 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; for (call = 0; call < 10; 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 (call == 4 || call == 5 || call == 6 || call == 7 || call == 8) break; event_timedwait(&th->done, 50); break; } } for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++) sleep_ms(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 (;;) { if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid) break; sleep_ms(1); if (current_time_ms() - start < 5000) continue; kill_and_wait(pid, &status); break; } remove_dir(cwdbuf); } } uint64_t r[2] = {0x0, 0x0}; void execute_call(int call) { intptr_t res = 0; switch (call) { case 0: res = -1; res = ((intptr_t(*)(intptr_t, intptr_t, intptr_t))CAST(semget))( /*key=*/0, /*nsems=*/2, /*flags=*/0x148); if (res != -1) r[0] = res; break; case 1: *(uint32_t*)0x20000100 = 0x20; *(uint32_t*)0x20000104 = -1; *(uint32_t*)0x20000108 = 0; *(uint32_t*)0x2000010c = 0; *(uint32_t*)0x20000110 = -1; *(uint32_t*)0x20000114 = 0x1a2; *(uint16_t*)0x20000118 = 1; *(uint16_t*)0x2000011a = 0; *(uint64_t*)0x20000120 = 0; *(uint64_t*)0x20000128 = 0; *(uint64_t*)0x20000130 = 0x51e; *(uint64_t*)0x20000138 = 9; *(uint64_t*)0x20000140 = 0x80000001; *(uint64_t*)0x20000148 = 0; *(uint64_t*)0x20000150 = 0; ((intptr_t(*)(intptr_t, intptr_t, intptr_t, intptr_t))CAST(semctl))( /*semid=*/r[0], /*semnum=*/0, /*cmd=*/1, /*arg=*/0x20000100); break; case 2: ((intptr_t(*)())CAST(getpid))(); break; case 3: res = -1; res = ((intptr_t(*)(intptr_t, intptr_t))CAST(msgget))(/*key=*/0, /*flags=*/0); if (res != -1) r[1] = res; break; case 4: sprintf((char*)0x20000100, "0x%016llx", (long long)r[1]); ((intptr_t(*)(intptr_t, intptr_t, intptr_t, intptr_t))CAST(msgsnd))( /*msqid=*/0, /*msgp=*/0x20000100, /*sz=*/0x401, /*flags=*/0); break; case 5: ((intptr_t(*)(intptr_t, intptr_t, intptr_t, intptr_t))CAST(msgsnd))( /*msqid=*/r[1], /*msgp=*/0, /*sz=*/0xb5, /*flags=*/0x800); break; case 6: ((intptr_t(*)(intptr_t, intptr_t, intptr_t, intptr_t, intptr_t))CAST( msgrcv))(/*msqid=*/r[1], /*msgp=*/0x20002700, /*sz=*/0x108, /*typ=*/3, /*flags=*/0); break; case 7: memcpy( (void*)0x20000980, "\x03\x00\x00\x00\x00\x00\x00\x00\x67\xe6\x39\xe4\x0a\xb5\xa0\xf7\xc4" "\xdf\x13\x98\x64\x9a\x06\x0b\xce\x0d\xb1\x09\x35\x5a\xb6\x07\x5f\x78" "\x9c\x79\xe8\x2c\x39\xc7\x15\xe2\x06\x15\x45\xb6\x32\x25\x71\xe2\xe8" "\x94\xe2\xb3\x72\x7b\x7a\x36\xca\x31\x45\xfa\xb9\x98\xcf\xbf\x77\x89" "\xab\x14\x8f\xc2\x6e\xa6\x51\x21\x9b\x2f\xc2\x5c\x34\xaf\xcb\x3a\x7a" "\xe4\x30\x24\x37\xd3\xfc\x09\xee\x8d\x78\x9b\x4d\x03\x61\x05\x60\xdd" "\x6c\x38\x67\x01\xa7\x82\xc2\xce\x0d\x7b\xee\xe2\x40\xa9\xef\x27\x78" "\x8a\x69\xbe\xe6\xcf\x80\x4f\x49\x00\xa0\xbd\x5b\xbe\x72\xd0\xd9\xd6" "\xcb\x1e\x55\x81\x81\xb1\xaa\x06\xe3\x0a\x5b\xf6\x19\xb5\x80\xda\xb7" "\x96\x00\xf8\x63\x71\x47\x11\xab\x56\x6e\x03\x58\x00\x00\x06\x0a\xee" "\x8d\xd9\xa3\x9a\x94\x92\xce\x6a\x4b\x69\x62\x6d\x89\x94\x4a\x89\xd1" "\x16\xff\x68\x0c\xd3\xb7\x2e\x06\xcf\x13\x5a\x99\xf5\xec\x0a\xbe\x6a" "\x66\xfd\x7f\x56\xfc\x69\xde\x7e\x92\xca\x20\xae\x63\xc4\x7b\xa9\x60" "\xcc\x37\xd0\xde\x48\x58\xff\x75\x09\x31\xc0\xac\xb3\x1a\x83\x4e\x00" "\x17\x1b\x00\x85\x57\xa9\x8c\x12\xc3\x00\x84\xb2\xa1\x2d\x0f\x68\x70" "\xba\x27\x90\xcc\x00\xeb\x2a\x06\x00\x00\x00\x4a\x37\xea\xd1\x11\xb8" "\x1c\xf3\x07\x2d\x80\xa2", 278); ((intptr_t(*)(intptr_t, intptr_t, intptr_t, intptr_t))CAST(msgsnd))( /*msqid=*/r[1], /*msgp=*/0x20000980, /*sz=*/0xee, /*flags=*/0); break; case 8: memcpy( (void*)0x20000240, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xee" "\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0a\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\xe4\xac\xd9\xb1\x50\x29\x94\x57\x9a\x69\xc4" "\x61\x91\x67\xf4\x27\xce\x19\x81\xa2\xb7\x79\x55\xc1\xdd\xab\x8f\x10" "\xd6\x87\xd0\x89\xe3\x47\x29\xd0\x27\xf2\xe9\xba\xf7\xd4\x11\x06\x5f" "\x3f\x6a\xa9\x8b\x4b\x2a\x36\xbc\x81\x09\xb8\xeb\xde\x20\x59\xea\xe5" "\x65\xe3\x0b\x1f\x82\x53\xbf\x11\xe4\xfe\xba\xa7\xb9\x59\xab\x15\x3a" "\xaa\x00\xe1\x4c\xa0\x5d\xa1\xc8\x01\x00\x39\x2a\x3e\x4e\x1e\xd2\x5d" "\x13\xc1\xf2\xed\xf6\xda\x4e\xcb\x0e\x25\xec\x60\x2b\x39\xd2\x0c\xfe" "\x7f\x85\x18\x1d\x36\x70\xc7\xff\x81\x59\x59\x9e\x78\x3e\x56\x42\x61" "\x1d\x40\x03\xe4\xfe\xec\x73\x1d\xbd\xd0\xe6\x8a\xa0\xea\xb9\x3d\xf7" "\xa3\x16\xf3\x06\x44\x19\xf3\x81\xda\x87\x19\x6f\x6d\x0d\x72\x43\x66" "\xca\x4f\x5e\xcf\xd5\x9f\xc8\xfe\x41\x9b\x0c\x42\x34\xa0\x8d\x08\x39" "\xe3\xf7\x59\x7b\x18\xc4\x18\x1d\x93\x50\x25\x18\xe9\x0c\x68\x39\x2b" "\xda\x21\x18\x6e\x18\x25\x77\x51\x37\x9f\x3d\x8c\xa9\x23\xc5\xe0\x68" "\x84\xc2\x3d\xff\x49\xed\xec\xbd\xde\x41\xb5\x7c\x8e\x2a\x7a\x7a\xc6" "\x54\x91\x0d\xd6\xae\x53\x72\x09\xec\x89\x98\xda\xa4\xf3\x5c\xe1\x6b" "\xe9\xc8\x39\xb7\xd4\x5a\xa7\xd8\x6b\x82\xa7\x0f\xc8\x9d\x61\x00\xee" "\x6c\xe1\x3a\xc0\x8b\x90\x93\x31\x82\xeb\x6d\x40\xcb\x80\x38\xcf\xb7" "\x8f\x7e\xe2\x7d\xf2\x43\x52\x70\xda\xdb\xd0\x19\xa0\x70\x62\xeb\x5c" "\xcd", 426); ((intptr_t(*)(intptr_t, intptr_t, intptr_t, intptr_t, intptr_t))CAST( msgrcv))(/*msqid=*/r[1], /*msgp=*/0x20000240, /*sz=*/0x7d, /*typ=*/0, /*flags=*/0x1000); break; case 9: ((intptr_t(*)())CAST(getgid))(); break; } } int main(void) { ((intptr_t(*)(intptr_t, intptr_t, intptr_t, intptr_t, intptr_t, intptr_t))CAST(mmap))(/*addr=*/0x20000000, /*len=*/0x1000000, /*prot=*/3, /*flags=*/0x1012, /*fd=*/-1, /*offset=*/0); use_temporary_dir(); do_sandbox_none(); return 0; }