io_uring_try_cancel_requests() arms cq_wait_nr on every DEFER_TASKRUN ring it visits, so that io_req_local_work_add() wakes the caller. The wakeup goes to submitter_task, which need not be the caller. A cancelling task can visit a ring whose local work belongs to another task, and ring release and io_ring_exit_work() call in as well. A cancelling task can also visit a ring that is still disabled, where submitter_task is NULL. A later ZCRX event then passes it to wake_up_state(): BUG: kernel NULL pointer dereference, address: 00000000000008e4 RIP: 0010:_raw_spin_lock_irqsave+0x1b/0x50 Call Trace: try_to_wake_up+0x51/0x790 io_req_local_work_add+0xbb/0x1b0 zcrx_send_notif+0xf9/0x120 On an enabled ring, the same arm can replace the submitter's cq_wait_nr batch threshold and cause an early wakeup. If the submitter is not waiting on the ring, a later local work add can still find the count armed and wake the submitter from an unrelated interruptible sleep. Arm cq_wait_nr only when the caller is allowed to run the ring's local work. The submitter-only rearm before cancellation sleeps already uses the same ownership check. ZCRX could skip disabled rings, as msg_ring does, but that leaves the wrong arm on enabled rings. Work that ZCRX queues on a disabled ring waits there until the ring is enabled, or until io_ring_exit_work() drains it. ZCRX events are the only local work a disabled ring can receive, so the NULL pointer dereference needs commit 0719e10d826a ("io_uring/zcrx: notify user when out of buffers"). Older kernels only see the early wakeups. Tested in a 2-vCPU x86-64 KVM guest with a reproducer that execs with an io_uring request pending while ZCRX copy events target a disabled DEFER_TASKRUN ring. v7.4-rc5 hit the NULL pointer dereference above. With this patch, 1000 runs completed. The bug was found with the help of an LLM, which was also used to write the fix, the changelog and the tests. Fixes: 360cd42c4e95 ("io_uring: optimise io_req_local_work_add") Cc: stable@vger.kernel.org Signed-off-by: Andrea Parri --- Reproducer below. It needs liburing, virtme-ng and QEMU with KVM. The program carries local copies of the ZCRX uAPI it uses (event descriptors, ZCRX_CTRL_ARM_EVENT), which older system headers lack. Build the kernel with: vng --kconfig --skip-modules \ --configitem CONFIG_IO_URING=y \ --configitem CONFIG_INET=y \ --configitem CONFIG_NET_RX_BUSY_POLL=y \ --configitem CONFIG_PAGE_POOL=y \ --configitem CONFIG_IO_URING_ZCRX=y make -j$(nproc) LOCALVERSION=-cancel-wake-owner bzImage Save the two files below as /tmp/io_uring-cancel-zcrx.c and /tmp/run-io_uring-cancel-zcrx.sh, then build and run them in a guest: cc -O2 -Wall -Wextra -Werror -pthread /tmp/io_uring-cancel-zcrx.c \ -luring -o /tmp/io_uring-cancel-zcrx chmod +x /tmp/run-io_uring-cancel-zcrx.sh vng --verbose --skip-modules --run ./arch/x86/boot/bzImage \ --user root --cpus 2 --memory 2G --rodir=/tmp \ --append oops=panic --append panic=1 \ --exec /tmp/run-io_uring-cancel-zcrx.sh Without this patch the guest panics with the NULL pointer dereference quoted above. With it, the script prints: 7.4.0-rc5-cancel-wake-owner+ completed=1000 ==> io_uring-cancel-zcrx.c <== // SPDX-License-Identifier: GPL-2.0 #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define AREA_PAGES 16384 #define RQ_ENTRIES 16384 #define ZCRX_REG_IMPORT 1 #define ZCRX_REG_NODEV 2 #define ZCRX_EVENT_COPY 1 #define ZCRX_CTRL_EXPORT 1 #define ZCRX_CTRL_ARM_EVENT 2 #define IORING_REGISTER_ZCRX_CTRL 36 struct current_zcrx_event_desc { __u64 user_data; __u32 type_mask; __u32 flags; __u64 stats_offset; __u64 reserved[9]; }; struct current_zcrx_ifq_reg { __u32 if_idx; __u32 if_rxq; __u32 rq_entries; __u32 flags; __u64 area_ptr; __u64 region_ptr; struct io_uring_zcrx_offsets offsets; __u32 zcrx_id; __u32 rx_buf_len; __u64 event_desc; __u64 reserved[2]; }; struct current_zcrx_ctrl { __u32 zcrx_id; __u32 op; __u64 reserved[2]; union { struct { __u32 zcrx_fd; __u32 reserved[11]; } export; struct { __u32 event_type; __u32 reserved[11]; } arm; }; }; static struct io_uring owner_ring; static pthread_barrier_t ready; static int exported_fd; static int receiver_fd; static int sender_fd; static char blocked_byte; static int register_op(int fd, unsigned int op, void *arg, unsigned int nr) { int ret = syscall(__NR_io_uring_register, fd, op, arg, nr); return ret < 0 ? -errno : ret; } static void tcp_pair(int *rxfd, int *txfd) { struct sockaddr_in addr = { .sin_family = AF_INET, .sin_addr.s_addr = htonl(INADDR_LOOPBACK), }; socklen_t addrlen = sizeof(addr); int listener; listener = socket(AF_INET, SOCK_STREAM, 0); if (listener < 0 || bind(listener, (void *)&addr, sizeof(addr)) < 0 || listen(listener, 1) < 0 || getsockname(listener, (void *)&addr, &addrlen) < 0) error(1, errno, "create listener"); *txfd = socket(AF_INET, SOCK_STREAM, 0); if (*txfd < 0 || connect(*txfd, (void *)&addr, sizeof(addr)) < 0) error(1, errno, "connect"); *rxfd = accept(listener, NULL, NULL); if (*rxfd < 0) error(1, errno, "accept"); close(listener); } static void *send_copy_events(void *arg) { struct current_zcrx_ifq_reg import = { .if_idx = exported_fd, .flags = ZCRX_REG_IMPORT, }; struct io_uring_params params = { .flags = IORING_SETUP_SINGLE_ISSUER | IORING_SETUP_DEFER_TASKRUN | IORING_SETUP_CQE32, }; struct current_zcrx_ctrl arm = { .op = ZCRX_CTRL_ARM_EVENT, .arm.event_type = ZCRX_EVENT_COPY, }; struct io_uring_cqe *cqe; struct io_uring_sqe *sqe; struct io_uring ring; char byte = 0; int ret; (void)arg; ret = io_uring_queue_init_params(8, &ring, ¶ms); if (ret) error(1, -ret, "receiver ring setup"); ret = register_op(ring.ring_fd, IORING_REGISTER_ZCRX_IFQ, &import, 1); if (ret) error(1, -ret, "import zcrx"); arm.zcrx_id = import.zcrx_id; sqe = io_uring_get_sqe(&ring); io_uring_prep_rw(IORING_OP_RECV_ZC, sqe, receiver_fd, NULL, 0, 0); sqe->ioprio = IORING_RECV_MULTISHOT; sqe->zcrx_ifq_idx = import.zcrx_id; ret = io_uring_submit(&ring); if (ret != 1) error(1, ret < 0 ? -ret : 0, "submit recvzc"); pthread_barrier_wait(&ready); for (;;) { if (write(sender_fd, &byte, 1) != 1) error(1, errno, "send"); ret = io_uring_wait_cqe(&ring, &cqe); if (ret || cqe->res != 1) error(1, ret ? -ret : 0, "recvzc: %d", cqe->res); io_uring_cqe_seen(&ring, cqe); ret = register_op(owner_ring.ring_fd, IORING_REGISTER_ZCRX_CTRL, &arm, 0); if (ret && ret != -EINVAL) error(1, -ret, "rearm copy event"); } return NULL; } static void setup_owner(void) { long page_size = sysconf(_SC_PAGESIZE); size_t area_size = AREA_PAGES * page_size; size_t ring_size = RQ_ENTRIES * sizeof(struct io_uring_zcrx_rqe) + page_size; struct current_zcrx_event_desc event = { .user_data = 0x1234, .type_mask = 1U << ZCRX_EVENT_COPY, }; struct io_uring_zcrx_area_reg area = { .len = area_size, }; struct io_uring_region_desc region = { .size = ring_size, .flags = IORING_MEM_REGION_TYPE_USER, }; struct current_zcrx_ifq_reg reg = { .rq_entries = RQ_ENTRIES, .flags = ZCRX_REG_NODEV, .area_ptr = (uintptr_t)&area, .region_ptr = (uintptr_t)®ion, .event_desc = (uintptr_t)&event, }; struct current_zcrx_ctrl export = { .op = ZCRX_CTRL_EXPORT, }; struct io_uring_params params = { .flags = IORING_SETUP_R_DISABLED | IORING_SETUP_SINGLE_ISSUER | IORING_SETUP_DEFER_TASKRUN | IORING_SETUP_CQE32, }; int ret; area.addr = (uintptr_t)mmap(NULL, area_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); region.user_addr = (uintptr_t)mmap(NULL, ring_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if ((void *)(uintptr_t)area.addr == MAP_FAILED || (void *)(uintptr_t)region.user_addr == MAP_FAILED) error(1, errno, "mmap"); ret = io_uring_queue_init_params(8, &owner_ring, ¶ms); if (ret) error(1, -ret, "owner ring setup"); ret = register_op(owner_ring.ring_fd, IORING_REGISTER_ZCRX_IFQ, ®, 1); if (ret) error(1, -ret, "register nodev zcrx"); export.zcrx_id = reg.zcrx_id; ret = register_op(owner_ring.ring_fd, IORING_REGISTER_ZCRX_CTRL, &export, 0); if (ret) error(1, -ret, "export zcrx"); exported_fd = export.export.zcrx_fd; } static void submit_blocked_read(struct io_uring *ring) { struct io_uring_sqe *sqe; int pipefd[2]; int ret; if (pipe(pipefd)) error(1, errno, "pipe"); sqe = io_uring_get_sqe(ring); io_uring_prep_read(sqe, pipefd[0], &blocked_byte, 1, 0); ret = io_uring_submit(ring); if (ret != 1) error(1, ret < 0 ? -ret : 0, "submit blocked read"); } int main(void) { struct io_uring cancel_ring; pthread_t thread; int ret; setup_owner(); tcp_pair(&receiver_fd, &sender_fd); pthread_barrier_init(&ready, NULL, 2); ret = pthread_create(&thread, NULL, send_copy_events, NULL); if (ret) error(1, ret, "pthread_create"); pthread_barrier_wait(&ready); ret = io_uring_queue_init(8, &cancel_ring, 0); if (ret) error(1, -ret, "cancel ring setup"); submit_blocked_read(&cancel_ring); usleep(1000); execl("/bin/true", "true", NULL); error(1, errno, "exec true"); } ==> run-io_uring-cancel-zcrx.sh <== #!/bin/sh uname -r i=0 while test "$i" -lt 1000; do /tmp/io_uring-cancel-zcrx || exit $? i=$((i + 1)) done echo "completed=$i" io_uring/cancel.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/io_uring/cancel.c b/io_uring/cancel.c index 5b57d27225acd..e6be39161fbe0 100644 --- a/io_uring/cancel.c +++ b/io_uring/cancel.c @@ -544,7 +544,8 @@ __cold bool io_uring_try_cancel_requests(struct io_ring_ctx *ctx, bool ret = false; /* set it so io_req_local_work_add() would wake us up */ - if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) { + if ((ctx->flags & IORING_SETUP_DEFER_TASKRUN) && + io_allowed_defer_tw_run(ctx)) { atomic_set(&ctx->cq_wait_nr, 1); smp_mb(); } -- 2.53.0