// https://syzkaller.appspot.com/bug?id=32f11d64c176b73705b604fb61cdf239a403c027 // 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 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 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) { int err = errno; close(fd); errno = err; return false; } close(fd); return true; } #define SIZEOF_IO_URING_SQE 64 #define SIZEOF_IO_URING_CQE 16 #define IORING_SETUP_SQE128 (1U << 10) #define IORING_SETUP_CQE32 (1U << 11) struct io_sqring_offsets { uint32_t head; uint32_t tail; uint32_t ring_mask; uint32_t ring_entries; uint32_t flags; uint32_t dropped; uint32_t array; uint32_t resv1; uint64_t user_addr; }; struct io_cqring_offsets { uint32_t head; uint32_t tail; uint32_t ring_mask; uint32_t ring_entries; uint32_t overflow; uint32_t cqes; uint32_t flags; uint32_t resv1; uint64_t user_addr; }; struct io_uring_params { uint32_t sq_entries; uint32_t cq_entries; uint32_t flags; uint32_t sq_thread_cpu; uint32_t sq_thread_idle; uint32_t features; uint32_t resv[4]; struct io_sqring_offsets sq_off; struct io_cqring_offsets cq_off; }; static long io_uring_sqe_size(struct io_uring_params* params) { return SIZEOF_IO_URING_SQE << !!(params->flags & IORING_SETUP_SQE128); } static long io_uring_cqe_size(struct io_uring_params* params) { return SIZEOF_IO_URING_CQE << !!(params->flags & IORING_SETUP_CQE32); } #define IORING_OFF_SQ_RING 0ULL #define IORING_OFF_SQES 0x10000000ULL static long syz_io_uring_setup(volatile long a0, volatile long a1, volatile long a2, volatile long a3, volatile long a4) { uint32_t entries = (uint32_t)a0; struct io_uring_params* setup_params = (struct io_uring_params*)a1; void** ring_params_ptr_out = (void**)a2; void** ring_ptr_out = (void**)a3; void** sqes_ptr_out = (void**)a4; uint32_t fd_io_uring = syscall(__NR_io_uring_setup, entries, setup_params); *ring_params_ptr_out = (void*)setup_params; uint32_t sq_ring_sz = setup_params->sq_off.array + setup_params->sq_entries * sizeof(uint32_t); uint32_t cq_ring_sz = setup_params->cq_off.cqes + setup_params->cq_entries * io_uring_cqe_size(setup_params); uint32_t ring_sz = sq_ring_sz > cq_ring_sz ? sq_ring_sz : cq_ring_sz; *ring_ptr_out = mmap(0, ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd_io_uring, IORING_OFF_SQ_RING); uint32_t sqes_sz = setup_params->sq_entries * io_uring_sqe_size(setup_params); *sqes_ptr_out = mmap(0, sqes_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd_io_uring, IORING_OFF_SQES); uint32_t* array = (uint32_t*)((uintptr_t)*ring_ptr_out + setup_params->sq_off.array); for (uint32_t index = 0; index < setup_params->sq_entries; index++) array[index] = index; return fd_io_uring; } static long syz_io_uring_submit(volatile long a0, volatile long a1, volatile long a2, volatile long a3) { struct io_uring_params* params = (struct io_uring_params*)a0; char* ring_ptr = (char*)a1; char* sqes_ptr = (char*)a2; char* sqe = (char*)a3; uint32_t sq_ring_mask = *(uint32_t*)(ring_ptr + params->sq_off.ring_mask); uint32_t* sq_tail_ptr = (uint32_t*)(ring_ptr + params->sq_off.tail); uint32_t sq_tail = *sq_tail_ptr & sq_ring_mask; uint32_t sqe_size = io_uring_sqe_size(params); char* sqe_dest = sqes_ptr + sq_tail * sqe_size; memcpy(sqe_dest, sqe, sqe_size); uint32_t sq_tail_next = *sq_tail_ptr + 1; __atomic_store_n(sq_tail_ptr, sq_tail_next, __ATOMIC_RELEASE); return 0; } static long syz_ublk_setup_io_uring(volatile long a0, volatile long a1, volatile long a2, volatile long a3, volatile long a4) { struct io_uring_params* params = (struct io_uring_params*)a1; params->flags |= IORING_SETUP_SQE128 | IORING_SETUP_CQE32; return syz_io_uring_setup(a0, (long)params, a2, a3, a4); } #define UBLK_F_USER_COPY (1UL << 7) #define UBLK_IO_RES_OK 0 #define UBLK_IO_RES_NEED_GET_DATA 1 #define UBLK_IO_COMMIT_AND_FETCH_REQ 0x21 #define UBLK_IO_NEED_GET_DATA 0x22 #define UBLK_U_IO_COMMIT_AND_FETCH_REQ \ _IOWR('u', UBLK_IO_COMMIT_AND_FETCH_REQ, struct ublksrv_io_cmd) #define UBLK_U_IO_NEED_GET_DATA \ _IOWR('u', UBLK_IO_NEED_GET_DATA, struct ublksrv_io_cmd) struct ublksrv_ctrl_cmd { __u32 dev_id; __u16 queue_id; __u16 len; __u64 addr; __u64 data[1]; __u16 dev_path_len; __u16 pad; __u32 reserved; }; struct ublksrv_ctrl_dev_info { __u16 nr_hw_queues; __u16 queue_depth; __u16 state; __u16 pad0; __u32 max_io_buf_bytes; __u32 dev_id; __s32 ublksrv_pid; __u32 pad1; __u64 flags; __u64 ublksrv_flags; __u32 owner_uid; __u32 owner_gid; __u64 reserved1; __u64 reserved2; }; struct ublksrv_io_desc { __u32 op_flags; union { __u32 nr_sectors; __u32 nr_zones; }; __u64 start_sector; __u64 addr; }; struct ublksrv_io_cmd { __u16 q_id; __u16 tag; __s32 result; union { __u64 addr; __u64 zone_append_lba; }; }; struct io_uring_sqe { __u8 opcode; __u8 flags; __u16 ioprio; __s32 fd; union { __u64 off; __u64 addr2; struct { __u32 cmd_op; __u32 __pad1; }; }; union { __u64 addr; __u64 splice_off_in; struct { __u32 level; __u32 optname; }; }; __u32 len; union { __kernel_rwf_t rw_flags; __u32 fsync_flags; __u16 poll_events; __u32 poll32_events; __u32 sync_range_flags; __u32 msg_flags; __u32 timeout_flags; __u32 accept_flags; __u32 cancel_flags; __u32 open_flags; __u32 statx_flags; __u32 fadvise_advice; __u32 splice_flags; __u32 rename_flags; __u32 unlink_flags; __u32 hardlink_flags; __u32 xattr_flags; __u32 msg_ring_flags; __u32 uring_cmd_flags; __u32 waitid_flags; __u32 futex_flags; __u32 install_fd_flags; __u32 nop_flags; __u32 pipe_flags; }; __u64 user_data; union { __u16 buf_index; __u16 buf_group; } __attribute__((packed)); __u16 personality; union { __s32 splice_fd_in; __u32 file_index; __u32 zcrx_ifq_idx; __u32 optlen; struct { __u16 addr_len; __u16 __pad3[1]; }; }; union { struct { __u64 addr3; __u64 __pad2[1]; }; struct { __u64 attr_ptr; __u64 attr_type_mask; }; __u64 optval; __u8 cmd[0]; }; }; static long syz_ublk_add_dev(volatile long a0, volatile long a1, volatile long a2, volatile long a3, volatile long a4, volatile long a5) { __u32 ring_fd = (__u32)a0; struct io_uring_params* params = (struct io_uring_params*)a1; char* ring_ptr = (char*)a2; char* sqes_ptr = (char*)a3; struct io_uring_sqe* sqe = (struct io_uring_sqe*)a4; void** dev_info_ptr_out = (void**)a5; struct ublksrv_ctrl_cmd* cmd = (struct ublksrv_ctrl_cmd*)(sqe->cmd); struct ublksrv_ctrl_dev_info* dev_info = (struct ublksrv_ctrl_dev_info*)(unsigned long)(cmd->addr); dev_info->flags |= UBLK_F_USER_COPY; int ret = syz_io_uring_submit((long)params, (long)ring_ptr, (long)sqes_ptr, (long)sqe); if (ret < 0) return (long)ret; syscall(__NR_io_uring_enter, ring_fd, 1, 1, 1, NULL, _NSIG / 8); *dev_info_ptr_out = (void*)dev_info; return dev_info->dev_id; } static void kill_and_wait(int pid, int* status) { kill(-pid, SIGKILL); kill(pid, SIGKILL); for (int i = 0; i < 100; i++) { if (waitpid(-1, status, WNOHANG | __WALL) == pid) return; usleep(1000); } DIR* dir = opendir("/sys/fs/fuse/connections"); if (dir) { for (;;) { struct dirent* ent = readdir(dir); if (!ent) break; if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) continue; char abort[300]; snprintf(abort, sizeof(abort), "/sys/fs/fuse/connections/%s/abort", ent->d_name); int fd = open(abort, O_WRONLY); if (fd == -1) { continue; } if (write(fd, abort, 1) < 0) { } close(fd); } closedir(dir); } else { } while (waitpid(-1, status, __WALL) != pid) { } } static void setup_test() { prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0); setpgrp(); write_file("/proc/self/oom_score_adj", "1000"); } #define KMEMLEAK_FILE "/sys/kernel/debug/kmemleak" static const char* setup_leak() { if (!write_file(KMEMLEAK_FILE, "scan=off")) { if (errno == EBUSY) return "KMEMLEAK disabled: increase CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE" " or unset CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF"; return "failed to write(kmemleak, \"scan=off\")"; } if (!write_file(KMEMLEAK_FILE, "scan")) return "failed to write(kmemleak, \"scan\")"; sleep(5); if (!write_file(KMEMLEAK_FILE, "scan")) return "failed to write(kmemleak, \"scan\")"; if (!write_file(KMEMLEAK_FILE, "clear")) return "failed to write(kmemleak, \"clear\")"; return NULL; } static void check_leaks(void) { int fd = open(KMEMLEAK_FILE, O_RDWR); if (fd == -1) exit(1); uint64_t start = current_time_ms(); if (write(fd, "scan", 4) != 4) exit(1); sleep(1); while (current_time_ms() - start < 4 * 1000) sleep(1); if (write(fd, "scan", 4) != 4) exit(1); static char buf[128 << 10]; ssize_t n = read(fd, buf, sizeof(buf) - 1); if (n < 0) exit(1); int nleaks = 0; if (n != 0) { sleep(1); if (write(fd, "scan", 4) != 4) exit(1); if (lseek(fd, 0, SEEK_SET) < 0) exit(1); n = read(fd, buf, sizeof(buf) - 1); if (n < 0) exit(1); buf[n] = 0; char* pos = buf; char* end = buf + n; while (pos < end) { char* next = strstr(pos + 1, "unreferenced object"); if (!next) next = end; char prev = *next; *next = 0; fprintf(stderr, "BUG: memory leak\n%s\n", pos); *next = prev; pos = next; nleaks++; } } if (write(fd, "clear", 5) != 5) exit(1); close(fd); if (nleaks) exit(1); } static void execute_one(void); #define WAIT_FLAGS __WALL static void loop(void) { int iter = 0; for (;; iter++) { int pid = fork(); if (pid < 0) exit(1); if (pid == 0) { setup_test(); execute_one(); exit(0); } int status = 0; uint64_t start = current_time_ms(); for (;;) { sleep_ms(10); if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid) break; if (current_time_ms() - start < 5000) continue; kill_and_wait(pid, &status); break; } check_leaks(); } } uint64_t r[5] = {0xffffffffffffffff, 0xffffffffffffffff, 0x0, 0x0, 0x0}; void execute_one(void) { intptr_t res = 0; if (write(1, "executing program\n", sizeof("executing program\n") - 1)) { } // openat$ublk_ctrl arguments: [ // fd: const = 0xffffffffffffff9c (8 bytes) // file: ptr[in, buffer] { // buffer: {2f 64 65 76 2f 75 62 6c 6b 2d 63 6f 6e 74 72 6f 6c 00} // (length 0x12) // } // flags: const = 0x2 (4 bytes) // mode: const = 0x0 (2 bytes) // ] // returns fd_ublk_ctrl memcpy((void*)0x200000000000, "/dev/ublk-control\000", 18); res = syscall(__NR_openat, /*fd=*/0xffffffffffffff9cul, /*file=*/0x200000000000ul, /*flags=*/2, /*mode=*/0); if (res != -1) r[0] = res; // syz_ublk_setup_io_uring arguments: [ // entries: int32 = 0x1d (4 bytes) // params: ptr[inout, io_uring_params] { // io_uring_params { // sq_entries: int32 = 0x0 (4 bytes) // cq_entries: int32 = 0x0 (4 bytes) // flags: io_uring_setup_flags = 0x0 (4 bytes) // sq_thread_cpu: int32 = 0x0 (4 bytes) // sq_thread_idle: int32 = 0x0 (4 bytes) // features: int32 = 0x0 (4 bytes) // wq_fd: fd_io_uring (resource) // resv: buffer: {00 00 00 00 00 00 00 00 00 00 00 00} (length 0xc) // sq_off: io_sqring_offsets { // head: int32 = 0x0 (4 bytes) // tail: int32 = 0x0 (4 bytes) // ring_mask: int32 = 0x0 (4 bytes) // ring_entries: int32 = 0x0 (4 bytes) // flags: int32 = 0x0 (4 bytes) // dropped: int32 = 0x0 (4 bytes) // array: int32 = 0x0 (4 bytes) // resv1: int32 = 0x0 (4 bytes) // user_addr: int64 = 0x0 (8 bytes) // } // cq_off: io_cqring_offsets { // head: int32 = 0x0 (4 bytes) // tail: int32 = 0x0 (4 bytes) // ring_mask: int32 = 0x0 (4 bytes) // ring_entries: int32 = 0x0 (4 bytes) // overflow: int32 = 0x0 (4 bytes) // cqes: int32 = 0x0 (4 bytes) // flags: int32 = 0x0 (4 bytes) // resv1: int32 = 0x0 (4 bytes) // user_addr: int64 = 0x0 (8 bytes) // } // } // } // ring_params_ptr: ptr[out, ring_params_ptr] { // ring_params_ptr (resource) // } // ring_ptr: ptr[out, ring_ptr] { // ring_ptr (resource) // } // sqes_ptr: ptr[out, sqes_ptr] { // sqes_ptr (resource) // } // ] // returns fd_io_uring *(uint32_t*)0x200000000044 = 0; *(uint32_t*)0x200000000048 = 0; *(uint32_t*)0x20000000004c = 0; *(uint32_t*)0x200000000050 = 0; *(uint32_t*)0x200000000058 = -1; memset((void*)0x20000000005c, 0, 12); res = -1; res = syz_ublk_setup_io_uring(/*entries=*/0x1d, /*params=*/0x200000000040, /*ring_params_ptr=*/0x2000000000c0, /*ring_ptr=*/0x200000000100, /*sqes_ptr=*/0x200000000140); if (res != -1) { r[1] = res; r[2] = *(uint64_t*)0x2000000000c0; r[3] = *(uint64_t*)0x200000000100; r[4] = *(uint64_t*)0x200000000140; } // syz_ublk_add_dev arguments: [ // fd_io_uring: fd_io_uring (resource) // ring_params_ptr: ring_params_ptr (resource) // ring_ptr: ring_ptr (resource) // sqes_ptr: sqes_ptr (resource) // sqe: ptr[inout, io_uring_sqe_uring[fd_ublk_ctrl, // const[UBLK_U_CMD_ADD_DEV, int32], ublk_ctrl_cmd$add_dev, int64]] { // io_uring_sqe_uring[fd_ublk_ctrl, const[UBLK_U_CMD_ADD_DEV, int32], // ublk_ctrl_cmd$add_dev, int64] { // opcode: const = 0x2e (1 bytes) // flags: iosqe_flags = 0x20 (1 bytes) // ioprio: const = 0x0 (2 bytes) // fd: fd_ublk_ctrl (resource) // cmd_op: const = 0xc0207504 (4 bytes) // pad1: const = 0x0 (4 bytes) // addr: const = 0x0 (8 bytes) // len: const = 0x0 (4 bytes) // uring_flags: uring_cmd_flags = 0x2 (4 bytes) // user_data: int64 = 0x3 (8 bytes) // buf_index_unused: const = 0x0 (2 bytes) // ioring_personality_id: ioring_personality_id (resource) // pad_unused: buffer: {00 00 00 00} (length 0x4) // cmd: ublk_ctrl_cmd[const[-1, int32], len[addr, int16], ptr64[in, // ublk_ctrl_dev_info_new], const[0, int64]] { // dev_id: const = 0xffffffff (4 bytes) // queue_id: const = 0xffff (2 bytes) // len: len = 0x40 (2 bytes) // addr: ptr[in, ublk_ctrl_dev_info_new] { // union ublk_ctrl_dev_info_new { // new_dev: ublk_ctrl_dev_info[const[-1, int32]] { // nr_hw_queues: int16 = 0x2 (2 bytes) // queue_depth: int16 = 0xf4 (2 bytes) // state: const = 0x0 (2 bytes) // pad0: const = 0x0 (2 bytes) // max_io_buf_bytes: const = 0x1000 (4 bytes) // dev_id: const = 0xffffffff (4 bytes) // ublk_pid: pid (resource) // pad1: const = 0x0 (4 bytes) // flags: ublk_f_flags = 0x2 (8 bytes) // ublk_flags: const = 0x0 (8 bytes) // owner_uid: const = 0x0 (4 bytes) // owner_gid: const = 0x0 (4 bytes) // reserved1: const = 0x0 (8 bytes) // reserved2: const = 0x0 (8 bytes) // } // } // } // data: const = 0x0 (8 bytes) // dev_path_len: const = 0x0 (2 bytes) // pad: const = 0x0 (2 bytes) // reserved: const = 0x0 (4 bytes) // pad2_unused: buffer: {00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 // 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 // 00 00 00 00 00 00 00 00 00 00 00} (length 0x30) // } // } // } // dev_info_ptr: nil // ] // returns ublk_dev_id *(uint8_t*)0x200000001ec0 = 0x2e; *(uint8_t*)0x200000001ec1 = 0x20; *(uint16_t*)0x200000001ec2 = 0; *(uint32_t*)0x200000001ec4 = r[0]; *(uint32_t*)0x200000001ec8 = 0xc0207504; *(uint32_t*)0x200000001ecc = 0; *(uint64_t*)0x200000001ed0 = 0; *(uint32_t*)0x200000001ed8 = 0; *(uint32_t*)0x200000001edc = 2; *(uint64_t*)0x200000001ee0 = 3; *(uint16_t*)0x200000001ee8 = 0; *(uint16_t*)0x200000001eea = 0; memset((void*)0x200000001eec, 0, 4); *(uint32_t*)0x200000001ef0 = -1; *(uint16_t*)0x200000001ef4 = -1; *(uint16_t*)0x200000001ef6 = 0x40; *(uint64_t*)0x200000001ef8 = 0x200000001e80; *(uint16_t*)0x200000001e80 = 2; *(uint16_t*)0x200000001e82 = 0xf4; *(uint16_t*)0x200000001e84 = 0; *(uint16_t*)0x200000001e86 = 0; *(uint32_t*)0x200000001e88 = 0x1000; *(uint32_t*)0x200000001e8c = -1; *(uint32_t*)0x200000001e90 = 0; *(uint32_t*)0x200000001e94 = 0; *(uint64_t*)0x200000001e98 = 2; *(uint64_t*)0x200000001ea0 = 0; *(uint32_t*)0x200000001ea8 = 0; *(uint32_t*)0x200000001eac = 0; *(uint64_t*)0x200000001eb0 = 0; *(uint64_t*)0x200000001eb8 = 0; *(uint64_t*)0x200000001f00 = 0; *(uint16_t*)0x200000001f08 = 0; *(uint16_t*)0x200000001f0a = 0; *(uint32_t*)0x200000001f0c = 0; memset((void*)0x200000001f10, 0, 48); syz_ublk_add_dev(/*fd_io_uring=*/r[1], /*ring_params_ptr=*/r[2], /*ring_ptr=*/r[3], /*sqes_ptr=*/r[4], /*sqe=*/0x200000001ec0, /*dev_info_ptr=*/0); } 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; if ((reason = setup_leak())) { fprintf(stderr, "reproducer setup failed: leak checking: %s\n", reason); exit(1); } loop(); return 0; }