/* * Local VM reproducer for the eventfd<->epoll recursion fixed by * cd305ee3633a ("io_uring: defer eventfd signaling when queued from a * wakeup handler") * * Runs as PID 1 (init) inside a throwaway initramfs. Builds the graph: * * io_uring(DEFER_TASKRUN) --poll--> epoll fd --watches--> eventfd * ^-- registered as this ring's CQ eventfd --' * * Triggering a non-eventfd source (a pipe) makes the io_uring poll wake * fire while ep->poll_wait.lock is held; on a buggy kernel the CQ-eventfd * signal goes out inline and re-enters epoll, re-acquiring the same lock on * the same CPU -> spinlock recursion -> the CPU wedges. * * Privilege model: PID 1 starts as root only to mount devtmpfs / open the * console and, at the end, to power the VM off (reboot() needs CAP_SYS_BOOT). * The io_uring/epoll/pipe graph and the trigger run in a CHILD that first * drops to uid/gid 65534 and proves the drop is irreversible. So the bug is * reached entirely from an unprivileged process; the root parent only reaps * the child and shuts the machine down. */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* --- io_uring uapi (vendored, exact 64-byte sqe layout) --- */ #define IORING_SETUP_SINGLE_ISSUER (1U << 12) #define IORING_SETUP_DEFER_TASKRUN (1U << 13) #define IORING_OP_POLL_ADD 6 #define IORING_POLL_ADD_MULTI (1U << 0) #define IORING_REGISTER_EVENTFD 4 #define IORING_OFF_SQ_RING 0ULL #define IORING_OFF_CQ_RING 0x8000000ULL #define IORING_OFF_SQES 0x10000000ULL #define IORING_FEAT_SINGLE_MMAP (1U << 0) struct io_uring_sqe { uint8_t opcode; uint8_t flags; uint16_t ioprio; int32_t fd; uint64_t off; uint64_t addr; uint32_t len; uint32_t poll32_events; uint64_t user_data; uint16_t buf_index; uint16_t personality; int32_t splice_fd_in; uint64_t addr3; uint64_t __pad2; }; struct io_sqring_offsets { uint32_t head, tail, ring_mask, ring_entries, flags, dropped, array, resv1; uint64_t user_addr; }; struct io_cqring_offsets { uint32_t head, tail, ring_mask, ring_entries, overflow, cqes, flags, resv1; uint64_t user_addr; }; struct io_uring_params { uint32_t sq_entries, cq_entries, flags, sq_thread_cpu, sq_thread_idle, features, wq_fd, resv[3]; struct io_sqring_offsets sq_off; struct io_cqring_offsets cq_off; }; static int io_uring_setup(unsigned e, struct io_uring_params *p) { return (int)syscall(__NR_io_uring_setup, e, p); } static int io_uring_enter(int fd, unsigned to_submit, unsigned min_complete, unsigned flags) { return (int)syscall(__NR_io_uring_enter, fd, to_submit, min_complete, flags, (void *)0, (size_t)0); } static int io_uring_register(int fd, unsigned op, void *arg, unsigned nr) { return (int)syscall(__NR_io_uring_register, fd, op, arg, nr); } static void say(const char *s) { write(1, s, strlen(s)); } static void sayn(const char *s, long n) { char b[32]; int i = 0; unsigned long v; say(s); if (n < 0) { write(1, "-", 1); v = (unsigned long)(-n); } else v = (unsigned long)n; char t[24]; int j = 0; if (v == 0) t[j++] = '0'; while (v) { t[j++] = '0' + (v % 10); v /= 10; } while (j) b[i++] = t[--j]; b[i++] = '\n'; write(1, b, i); } /* The whole trigger, run by the UNPRIVILEGED child. Returns on survival. */ static void run_repro(void) { struct io_uring_params p; memset(&p, 0, sizeof(p)); p.flags = IORING_SETUP_SINGLE_ISSUER | IORING_SETUP_DEFER_TASKRUN; int ring = io_uring_setup(8, &p); if (ring < 0) { sayn("ERR io_uring_setup errno=", errno); return; } sayn("ring fd=", ring); sayn("params.flags=", p.flags); sayn("features=", p.features); size_t sqsz = p.sq_off.array + p.sq_entries * sizeof(uint32_t); size_t cqsz = p.cq_off.cqes + p.cq_entries * sizeof(uint64_t) * 2; size_t rsz = sqsz > cqsz ? sqsz : cqsz; void *sq = mmap(0, rsz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, ring, IORING_OFF_SQ_RING); if (sq == MAP_FAILED) { sayn("ERR mmap sq errno=", errno); return; } void *cq = (p.features & IORING_FEAT_SINGLE_MMAP) ? sq : mmap(0, cqsz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, ring, IORING_OFF_CQ_RING); if (cq == MAP_FAILED) { sayn("ERR mmap cq errno=", errno); return; } struct io_uring_sqe *sqes = mmap(0, p.sq_entries * sizeof(*sqes), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, ring, IORING_OFF_SQES); if (sqes == MAP_FAILED) { sayn("ERR mmap sqes errno=", errno); return; } uint32_t *sq_tail = (uint32_t *)((char *)sq + p.sq_off.tail); uint32_t *sq_mask = (uint32_t *)((char *)sq + p.sq_off.ring_mask); uint32_t *sq_array = (uint32_t *)((char *)sq + p.sq_off.array); uint32_t *cq_head = (uint32_t *)((char *)cq + p.cq_off.head); uint32_t *cq_tail = (uint32_t *)((char *)cq + p.cq_off.tail); uint32_t *cq_mask = (uint32_t *)((char *)cq + p.cq_off.ring_mask); struct cqe { uint64_t user_data; int32_t res; uint32_t flags; }; struct cqe *cqes = (struct cqe *)((char *)cq + p.cq_off.cqes); /* eventfd, registered as this ring's CQ eventfd */ int efd = eventfd(0, 0); if (efd < 0) { sayn("ERR eventfd errno=", errno); return; } if (io_uring_register(ring, IORING_REGISTER_EVENTFD, &efd, 1) < 0) { sayn("ERR register eventfd errno=", errno); return; } /* A non-eventfd trigger source: a pipe. Writing it kicks epoll without * setting current->in_eventfd, so the buggy inline signal is not masked. */ int pfd[2]; if (pipe(pfd) < 0) { sayn("ERR pipe errno=", errno); return; } /* epoll instance that watches BOTH the pipe (trigger) and the eventfd * (the CQ-notification target that feeds back in). */ int ep = epoll_create1(0); if (ep < 0) { sayn("ERR epoll_create1 errno=", errno); return; } struct epoll_event ev; memset(&ev, 0, sizeof(ev)); ev.events = EPOLLIN; ev.data.fd = efd; if (epoll_ctl(ep, EPOLL_CTL_ADD, efd, &ev) < 0) { sayn("ERR epoll_ctl efd errno=", errno); return; } ev.events = EPOLLIN; ev.data.fd = pfd[0]; if (epoll_ctl(ep, EPOLL_CTL_ADD, pfd[0], &ev) < 0) { sayn("ERR epoll_ctl pipe errno=", errno); return; } /* arm an io_uring poll on the epoll fd itself */ uint32_t idx = 0; struct io_uring_sqe *s = &sqes[idx]; memset(s, 0, sizeof(*s)); s->opcode = IORING_OP_POLL_ADD; s->fd = ep; s->poll32_events = POLLIN; s->len = IORING_POLL_ADD_MULTI; /* multishot: waiter stays armed on ep->poll_wait */ s->user_data = 0x1234; uint32_t tail = *sq_tail; sq_array[tail & *sq_mask] = idx; __atomic_store_n(sq_tail, tail + 1, __ATOMIC_RELEASE); int r = io_uring_enter(ring, 1, 0, 0); if (r < 0) { sayn("ERR io_uring_enter errno=", errno); return; } sayn("io_uring_enter submit ret=", r); sayn("after arm: cq_tail=", (long)*cq_tail); if (*cq_tail != *cq_head) { struct cqe *e = &cqes[*cq_head & *cq_mask]; sayn(" INLINE completion at arm, res=", (long)e->res); } say("ARMED poll on epoll fd; triggering via pipe...\n"); /* Kick the pipe (not the eventfd). On a buggy kernel this write never * returns: epoll wakes the io_uring poll while holding ep->poll_wait.lock, * the poll's first task_work add signals the CQ eventfd inline, that * re-enters epoll and recurses on ep->poll_wait.lock on this same CPU. */ char one = 'x'; ssize_t w = write(pfd[1], &one, 1); sayn("pipe write returned ", (long)w); sayn("after write: cq_tail=", (long)*cq_tail); /* Did the inline task_work-add signal the CQ eventfd? Peek its counter. */ { int fl = fcntl(efd, F_GETFL); fcntl(efd, F_SETFL, fl | O_NONBLOCK); uint64_t cnt = 0; ssize_t rr = read(efd, &cnt, sizeof(cnt)); if (rr == sizeof(cnt)) sayn("efd counter after write = ", (long)cnt); else sayn("efd not signaled (read rr=", (long)rr); fcntl(efd, F_SETFL, fl); } /* Run deferred task_work and reap to see the poll result/res. */ io_uring_enter(ring, 0, 0, 1 /*GETEVENTS*/); sayn("after getevents: cq_tail=", (long)*cq_tail); if (*cq_tail != *cq_head) { struct cqe *e = &cqes[*cq_head & *cq_mask]; sayn(" poll CQE res=", (long)e->res); } say("REPRO-SURVIVED\n"); } /* Drop every privilege to uid/gid 65534 and refuse to continue if the drop * did not fully take. Order matters: setgroups + setgid must precede setuid. */ static void drop_privs(void) { sayn("child uid before drop=", (long)getuid()); sayn("child gid before drop=", (long)getgid()); gid_t ng = 65534; /* nogroup */ uid_t nb = 65534; /* nobody */ if (setgroups(0, (const gid_t *)0) < 0) sayn("WARN setgroups errno=", errno); if (setgid(ng) < 0) { sayn("ERR setgid errno=", errno); _exit(2); } if (setuid(nb) < 0) { sayn("ERR setuid errno=", errno); _exit(2); } sayn("child uid after drop=", (long)getuid()); sayn("child gid after drop=", (long)getgid()); if (getuid() != nb || getgid() != ng) { say("ERR drop incomplete\n"); _exit(2); } if (geteuid() == 0 || getegid() == 0) { say("ERR euid/egid still 0\n"); _exit(2); } /* The drop must be irreversible: regaining root has to fail. */ if (setuid(0) == 0) { say("ERR regained root via setuid(0)\n"); _exit(2); } sayn("setuid(0) correctly refused, errno=", errno); /* A sanity cross-check: a non-root task cannot reboot(). */ if (reboot(LINUX_REBOOT_CMD_CAD_OFF) == 0) { say("ERR non-root reboot() succeeded\n"); _exit(2); } sayn("reboot() from child correctly refused, errno=", errno); say("PRIV-DROPPED: running trigger as uid 65534\n"); } int main(void) { /* As init (root): working /dev and console on fd 1/2. */ mount("devtmpfs", "/dev", "devtmpfs", 0, 0); int c = open("/dev/console", O_WRONLY); if (c >= 0) { dup2(c, 1); dup2(c, 2); } say("REPRO-START\n"); sayn("init uid=", (long)getuid()); pid_t pid = fork(); if (pid == 0) { /* Unprivileged child: no capabilities, then hit the bug. */ drop_privs(); run_repro(); _exit(0); } if (pid < 0) { sayn("ERR fork errno=", errno); } /* Root parent: reap the child (on a buggy kernel it wedges a CPU inside * write() and never exits -> the box stalls and qemu times out), then * power off cleanly on a fixed kernel. */ int status = 0; if (pid > 0) { waitpid(pid, &status, 0); sayn("child exited, status=", (long)status); } sync(); say("REPRO-POWEROFF\n"); reboot(LINUX_REBOOT_CMD_POWER_OFF); for (;;) pause(); return 0; }