// https://syzkaller.appspot.com/bug?id=9b035afe38a2757dc2bc11a96bce2a210221e500 // autogenerated by syzkaller (https://github.com/google/syzkaller) #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include 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 = 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 { int state; } event_t; static void event_init(event_t* ev) { ev->state = 0; } static void event_reset(event_t* ev) { ev->state = 0; } static void event_set(event_t* ev) { if (ev->state) exit(1); __atomic_store_n(&ev->state, 1, __ATOMIC_RELEASE); syscall(SYS_futex, &ev->state, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1000000); } static void event_wait(event_t* ev) { while (!__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE)) syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, 0); } static int event_isset(event_t* ev) { return __atomic_load_n(&ev->state, __ATOMIC_ACQUIRE); } static int event_timedwait(event_t* ev, uint64_t timeout) { uint64_t start = current_time_ms(); uint64_t now = start; for (;;) { uint64_t remain = timeout - (now - start); struct timespec ts; ts.tv_sec = remain / 1000; ts.tv_nsec = (remain % 1000) * 1000 * 1000; syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, &ts); if (__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE)) return 1; now = current_time_ms(); if (now - start > timeout) 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 loop(void) { if (write(1, "executing program\n", sizeof("executing program\n") - 1)) { } int i, call, thread; for (call = 0; call < 8; 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); event_timedwait(&th->done, 50); break; } } for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++) sleep_ms(1); } uint64_t r[3] = {0xffffffffffffffff, 0x0, 0xffffffffffffffff}; void execute_call(int call) { intptr_t res = 0; switch (call) { case 0: // openat$ptmx arguments: [ // fd: const = 0xffffffffffffff9c (8 bytes) // file: ptr[in, buffer] { // buffer: {2f 64 65 76 2f 70 74 6d 78 00} (length 0xa) // } // flags: open_flags = 0xa82 (4 bytes) // mode: const = 0x0 (2 bytes) // ] // returns fd_tty memcpy((void*)0x200000000180, "/dev/ptmx\000", 10); res = syscall(__NR_openat, /*fd=*/0xffffffffffffff9cul, /*file=*/0x200000000180ul, /*flags=O_TRUNC|O_NONBLOCK|O_EXCL|O_RDWR*/ 0xa82, /*mode=*/0); if (res != -1) r[0] = res; break; case 1: // ioctl$TIOCSETD arguments: [ // fd: fd_tty (resource) // cmd: const = 0x5423 (4 bytes) // arg: ptr[in, int32] { // int32 = 0x2 (4 bytes) // } // ] *(uint32_t*)0x200000000040 = 2; syscall(__NR_ioctl, /*fd=*/r[0], /*cmd=*/0x5423, /*arg=*/0x200000000040ul); break; case 2: // io_setup arguments: [ // n: int32 = 0x7 (4 bytes) // ctx: ptr[out, io_ctx] { // io_ctx (resource) // } // ] res = syscall(__NR_io_setup, /*n=*/7, /*ctx=*/0x200000000280ul); if (res != -1) r[1] = *(uint64_t*)0x200000000280; break; case 3: // io_submit arguments: [ // ctx: io_ctx (resource) // nr: len = 0x2 (8 bytes) // iocbpp: ptr[in, array[ptr[in, iocb]]] { // array[ptr[in, iocb]] { // ptr[in, iocb] { // iocb { // aio_data: const = 0x0 (8 bytes) // aio_key: const = 0x0 (4 bytes) // aio_rw_flags: const = 0x0 (4 bytes) // aio_lio_opcode: lio_opcode = 0x7 (2 bytes) // aio_reqprio: int16 = 0xfffe (2 bytes) // aio_fildes: fd (resource) // aio_buf: nil // aio_nbytes: len = 0x0 (8 bytes) // aio_offset: int64 = 0x0 (8 bytes) // aio_reserved2: const = 0x0 (8 bytes) // aio_flags: iocb_flags = 0x0 (4 bytes) // aio_resfd: fd_event (resource) // } // } // nil // } // } // ] *(uint64_t*)0x200000000200 = 0x200000000000; *(uint64_t*)0x200000000000 = 0; *(uint32_t*)0x200000000008 = 0; *(uint32_t*)0x20000000000c = 0; *(uint16_t*)0x200000000010 = 7; *(uint16_t*)0x200000000012 = 0xfffe; *(uint32_t*)0x200000000014 = r[0]; *(uint64_t*)0x200000000018 = 0; *(uint64_t*)0x200000000020 = 0; *(uint64_t*)0x200000000028 = 0; *(uint64_t*)0x200000000030 = 0; *(uint32_t*)0x200000000038 = 0; *(uint32_t*)0x20000000003c = -1; *(uint64_t*)0x200000000208 = 0; syscall(__NR_io_submit, /*ctx=*/r[1], /*nr=*/2ul, /*iocbpp=*/0x200000000200ul); break; case 4: // mkdirat arguments: [ // fd: fd_dir (resource) // path: ptr[in, buffer] { // buffer: {2e 2f 73 79 73 00} (length 0x6) // } // mode: open_mode = 0x1ff (8 bytes) // ] memcpy((void*)0x200000000000, "./sys\000", 6); syscall( __NR_mkdirat, /*fd=*/0xffffff9c, /*path=*/0x200000000000ul, /*mode=S_IXOTH|S_IWOTH|S_IROTH|S_IXGRP|S_IWGRP|S_IRGRP|S_IXUSR|S_IWUSR|0x100*/ 0x1fful); break; case 5: // mount arguments: [ // src: nil // dst: ptr[in, buffer] { // buffer: {2e 2f 73 79 73 00} (length 0x6) // } // type: ptr[in, buffer] { // buffer: {73 79 73 66 73 00} (length 0x6) // } // flags: mount_flags = 0x0 (8 bytes) // data: nil // ] memcpy((void*)0x200000000080, "./sys\000", 6); memcpy((void*)0x2000000000c0, "sysfs\000", 6); syscall(__NR_mount, /*src=*/0ul, /*dst=*/0x200000000080ul, /*type=*/0x2000000000c0ul, /*flags=*/0ul, /*data=*/0ul); break; case 6: // openat arguments: [ // fd: fd_dir (resource) // file: ptr[in, buffer] { // buffer: {2e 2f 73 79 73 2f 62 75 73 2f 73 65 72 69 6f 2f 64 65 76 69 // 63 65 73 2f 73 65 72 69 6f 30 2f 64 72 76 63 74 6c 00} (length 0x26) // } // flags: open_flags = 0x1 (4 bytes) // mode: open_mode = 0x0 (2 bytes) // ] // returns fd memcpy((void*)0x200000000100, "./sys/bus/serio/devices/serio0/drvctl\000", 38); res = syscall(__NR_openat, /*fd=*/0xffffff9c, /*file=*/0x200000000100ul, /*flags=O_WRONLY*/ 1, /*mode=*/0); if (res != -1) r[2] = res; break; case 7: // write arguments: [ // fd: fd (resource) // buf: ptr[in, buffer] { // buffer: {61 74 6b 62 64} (length 0x5) // } // count: len = 0x5 (8 bytes) // ] memcpy((void*)0x200000000200, "atkbd", 5); syscall(__NR_write, /*fd=*/r[2], /*buf=*/0x200000000200ul, /*count=*/5ul); break; } } 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); const char* reason; (void)reason; loop(); return 0; }