//nefariousplan

Borrowed Pages As Scratch

A subsystem performs fixed-size scratch writes into a destination buffer under an internal contract that it owns the memory. Another subsystem supplies that buffer with foreign-owned pages (page cache, mapped device, peer process). The contract is documentation; the legitimate scratch becomes a write primitive across a trust boundary nobody guards.

A subsystem writes a small, fixed-size scratch value into a buffer it intends to transform in place. The destination offset is computed from the subsystem's own accounting, the width is a constant the subsystem's code picks, and the bytes get overwritten by the real output later in the same routine. Nothing about the write looks like an attack primitive when read inside the file that performs it.

A second subsystem supplies that buffer. The suppliers the first subsystem's authors imagined were in-tree callers who allocated the buffer themselves and owned every byte in it. The supplier that actually reaches production is one that attaches the buffer to pages whose ownership lives outside the first subsystem's trust domain: the page cache of a file an unprivileged process opened read-only, a user-mapped guest_memfd region, an io_uring-pinned mapping of someone else's data, a peer process's buffer surfaced through splice. Each subsystem honors its own contract. Neither owns the one that says the destination pages belong to the subsystem doing the writing.

The scratch write crosses the trust boundary at the moment the first subsystem decides it owns a buffer the second subsystem decided it only needed to carry. The narration inside each function is correct. The invariant that holds them apart is a sentence in a comment, a convention in a design document, a flag the kernel sets on one transport and forgets on the other.

Mechanism

Three parties participate. None of them, in isolation, is a bug.

The first party is the writer. A crypto template running in place over a destination scatter-gather list. An exec-path routine arranging arguments. A packet parser rewriting the associated data before HMAC. The writer's authors designed the routine when its callers were a closed set of in-tree, kernel-privileged consumers who had allocated their own buffers and owned every byte. The scratch write is fixed-size, deterministic, documented in a comment above the function when it is documented at all, and invisible to every other party because the real output overwrites it a few microseconds later. The crypto_authenc_esn_decrypt line scatterwalk_map_and_copy(tmp + 1, dst, assoclen + cryptlen, 4, 1) is the canonical shape: four bytes written one past the plaintext end, as a save-and-restore around the IPsec Extended Sequence Number splice into associated data, unchanged across nine years of mainline Linux kernels (CopyFail). Correctness is a property of the function alone.

The second party is the supplier. A userspace-facing socket interface that accepts a scatterlist from user code (algif_aead with req->src == req->dst). A datagram encap path that splices pipe pages into an outgoing or incoming skb (MSG_SPLICE_PAGES on UDP, attaching page-cache frags to an skb that then reaches esp_input or rxkad_verify_packet_1 (Dirty Frag)). An mmap of guest_memfd into a virtual-address range the host subsystem recognises as writable. The supplier's threat model is that the pages it attaches are either allocated by itself or carry permissions that reflect their origin. It does not take responsibility for whether downstream subsystems will treat those pages as scratch; downstream subsystems are, in the supplier's model, forward consumers of the buffer the supplier populated.

The third party is the trust boundary the two subsystems share without discussing. The writer thinks it owns the destination because its in-tree callers used to. The supplier thinks the buffer is a read-forward carrier because that is what it carries elsewhere. The attacker sits between them, choosing what pages the supplier attaches: by opening a file read-only and splicing its page-cache pages into the supplier's SGL, by mmap'ing guest_memfd into the host's view, by writing bytes to a pipe and sending them through MSG_SPLICE_PAGES. When the writer executes its legitimate scratch, the destination offset is bytes the supplier owes to those attacker-chosen pages, and the write lands there. /usr/bin/su's page cache ends up holding 160 bytes of setuid-root shellcode, written four bytes at a time by a crypto template doing exactly what it has always done.

The failure repeats because the kernel's internal contracts about memory ownership are not representable in any type system the kernel compiles. struct scatterlist is bytes. struct sk_buff * is bytes. struct iovec is bytes. The contract that says a particular SGL is in-place-writable, or that an skb's frags are kernel-private, exists in prose above the function that reads the SGL and in the mental model of the reviewer who approved the commit that made the operation in-place. The patch is always the kernel teaching itself to carry the contract as a bit: SKBFL_SHARED_FRAG on UDP append, checked in esp_input's skip_cow branch; a reversion of the 2017 algif_aead memory-management overhaul that made the AEAD in-place, restoring kernel-allocated destination buffers whose pages the algorithm's caller owns. The fix lifts the contract from documentation into a bit the two subsystems both read; the class stays open everywhere the lift did not happen, which is every other in-place operation the writer side of the kernel ships.

What distinguishes the pattern from neighbouring classes is the shape of the bug inside the writer. The writer has no out-of-bounds arithmetic, no refcount race, no uninitialised read, no type confusion. The writer does exactly what it has always done, and what it has always done is a write across a trust boundary now that the supplier changed. Pool Retains Prior Writes describes the same page-ownership failure in reverse: a prior caller's bytes persist into the next caller's view because the pool does not zero on free. Borrowed Pages As Scratch is the forward direction: the current caller's legitimate scratch persists into a page the next reader did not expect to be modified. Emergent Primitive is the broader family whose ownership vacuum this pattern inherits; here the composition unit is a memory buffer and the two subsystems disagree about who owns the backing pages.

The economics are the reason the pattern recurs. Every exhibit traces back to a commit that converted an out-of-place operation to an in-place one, motivated by a single copy saved per call on a hot path. The 2017 72548b093ee3 commit that made algif_aead in-place. The 2017 cac2661c53f3 and 03e2a30f6a27 commits that removed skb_cow_data from esp4 and esp6. Each conversion was correct against the supplier set of its day. Each conversion silently inherits every supplier that lands afterwards, and the kernel ships new suppliers on every release: splice(), vmsplice(), MSG_SPLICE_PAGES, io_uring fixed buffers, guest_memfd. The reviewer who approves the next in-place conversion is the one who owes the audit of today's supplier set. The pattern's live exhibits are nine years of latency on exactly that kind of review.

Exhibits

CVE-2026-43284 + CVE-2026-43500: The Flag Was on TCP. UDP Did Not Set It.. Dirty Frag is the network-stack instance of borrowed-pages-as-scratch, twice. ESP and RxRPC both perform in-place AEAD or skcipher decryption on sg, sg SGLs built directly from skb->frag via skb_to_sgvec(). MSG_SPLICE_PAGES gave userspace the primitive to populate those frags with pages owned by the page cache of any readable file. The metadata for the trust boundary already existed: TCP marked splice-sourced frags with SKBFL_SHARED_FRAG and TCP's receivers honored it. UDP did not propagate the flag, ESP did not check it, RxRPC gated on the wrong field (skb_cloned). The mainline patch lifts the contract into enforcement: it sets SKBFL_SHARED_FRAG on UDP append and checks it in esp_input's skip_cow branch. The pattern's "contract is documentation, not enforcement" sentence is what the kernel maintainers wrote in C.

CVE-2026-31431: authencesn Has Been Writing Those Four Bytes for Nine Years. The Patch Is Not in authencesn.. CopyFail is the textbook instance. authencesn writes four bytes of relocated AAD past its ciphertext under the contract that the destination is the same scratch buffer the caller allocated. algif_aead, since 2017, runs that operation in-place with req->src == req->dst. splice() then lets userspace populate that source SGL with read-only page-cache pages owned by an unrelated file. The four bytes of legitimate scratch land in /usr/bin/su. Neither subsystem is wrong on its own. The contract that holds them apart is decoration.

Boundaries

Not every cross-subsystem memory bug is this pattern. Pool Retains Prior Writes is the mirror: a pool's residue from an earlier caller surfaces into a later caller's view because the pool does not zero on free. Both patterns live at the boundary between subsystems that share a page without sharing a trust model, and the direction is opposite. Borrowed Pages As Scratch names the forward direction, where the current caller's legitimate write crosses into an attacker-staged destination. Pool Retains Prior Writes names the reverse, where the attacker-staged source reaches the current caller's buffer unchanged. The FreeBSD exec-pool exhibit is a Pool Retains Prior Writes instance for exactly this reason: the 2,024 OOB bytes that root sshd were not written by the trigger exec, they were left in the pool by an earlier unprivileged process.

Not every emergent-primitive memory bug is this pattern. Emergent Primitive is the general class of individually-safe components composing into an attack. Borrowed Pages As Scratch is the specific composition where the buffer the two subsystems share is the composition unit, and they disagree about who owns the pages behind it. A memory bug that involves multiple subsystems but whose primitive is a use-after-free, a refcount leak, a type-confusion gadget, a slab-cache cross-wire, or an allocator-metadata corruption belongs under Emergent Primitive, not here. PinTheft (net/rds leaking a FOLL_PIN'd page into the page cache via the per-CPU LIFO) is Emergent Primitive, not Borrowed Pages As Scratch, because the primitive is a page-refcount leak that lands a stolen page into the wrong trust tier, not a legitimate scratch write into an attacker-chosen destination.

Not every out-of-bounds write is this pattern. The writer in Borrowed Pages As Scratch writes exactly the bytes the function was authored to write, at exactly the offset its accounting computes. The out-of-bounds-ness is a property of the destination's trust domain, not of the arithmetic. A classic buffer-overflow bug where a length check is missing and the write runs past its allocation is not this class, even if the allocation happens to be a pool entry or a page-cache page. The distinguishing marker is: if the function's local correctness holds and the only thing that changed is who owns the destination, Borrowed Pages As Scratch applies. If the function would be a bug even against a kernel-allocated destination of its own, the class is an ordinary memory-safety bug and the patch belongs inside the function.

Not every splice-abuse bug is this pattern. Dirty Pipe (CVE-2022-0847) used splice() to inject bytes directly into a pipe_buffer the kernel then wrote into the page cache. The writer and the supplier in Dirty Pipe were effectively the same subsystem, with the bug in the pipe code's own merge logic. Borrowed Pages As Scratch requires two distinct subsystems that each think the other is honoring the ownership contract. If the splice-shaped bug lives inside the splice path's own merge, append, or flag-propagation logic, it is a splice bug and the fix belongs there. If the splice path is just the supplier and the write happens inside an unrelated receive-path or decode-path function that assumed its destination was kernel-private, Borrowed Pages As Scratch applies. The CopyFail family tree (Dirty Pipe to CopyFail to Dirty Frag) is useful for exactly this reason: the first instance is a splice bug, the next two are this pattern.

Defender playbook

Audit every in-place or scratch-writing subsystem against the set of suppliers that can now reach it. Any function whose core loop sets req->src == req->dst, writes a header back onto the buffer it just copied in, relocates a few bytes past the output during legitimate processing, or scratches state into its destination before the real result overwrites it is a candidate. For each, enumerate every call site, follow the SGL or buffer to its allocator, and ask whether any userspace or guest-reachable supplier has become reachable in the function's lifetime. splice(), vmsplice(), MSG_SPLICE_PAGES, io_uring registered buffers, guest_memfd, AF_ALG sockets, PCIe peer-to-peer DMA are the current suppliers. New ones ship in each release. The output of the exercise is a list of in-place writers whose destination can be attacker-chosen pages today, not a list of locally buggy functions.

Install the trust contract as a bit, not as a comment. Whichever subsystem the kernel chose to stamp with "these pages are foreign" (SKBFL_SHARED_FRAG on skbs, user-origin flags on scatterlists, PageSlab, GUP pin state, io_uring fixed-buffer markers) is the right place to encode the invariant that downstream scratch writes depend on. Every receive-path or decode-path function that writes back into its own SGL must branch on the flag and divert to an out-of-place copy, or refuse the fast path. A flag the supplier sets but no consumer checks is not enforcement; a check at only one of the receivers that reads the SGL leaves the class open on the others. The ESP patch is the shape: UDP append now sets SKBFL_SHARED_FRAG, and esp_input's skip_cow branch checks it. The RxRPC patch widens the unshare gate from skb_cloned(skb) to (skb_cloned(skb) || skb->data_len) for the same reason: the previous gate was reading the wrong field.

Test every in-place transform with foreign-backed destinations. Build the test with the actual suppliers the class exploits: splice() a read-only file's page cache into the function's input SGL, send guest_memfd-backed pages across a host-side driver, construct an skb with MSG_SPLICE_PAGES on UDP and send it through the receive path, submit an io_uring op whose fixed buffer is a mmap of an executable the test process cannot write to. The regression test name should be the one-sentence invariant the subsystem's comment used to carry, for example "this function may not scratch into its destination when the destination is a shared page." The test fails the day a new supplier lands that attaches unmarked foreign pages, before the next CVE's PoC is written against it.

Review post-optimization commits that convert out-of-place operations to in-place. The 2017 72548b093ee3 algif_aead memory-management overhaul and the 2017 cac2661c53f3 / 03e2a30f6a27 esp4/esp6 skb_cow_data optimizations are the two commit families the CopyFail and Dirty Frag patch messages cite. The economics are permanent: an in-place conversion saves a copy per call on a hot path, and the reviewer who approves it does so against the supplier set of its day. Any future reviewer of a similar conversion owes the audit of today's supplier set, and the question to ask at review is not "does this function do its own job correctly" but "which subsystems today can plant foreign-owned pages in this function's destination, and does the function's local correctness still hold when the destination is one of theirs."

Kinship

Emergent Primitive. Parent class. Emergent Primitive names the general shape of individually-safe components composing into an attack nobody owns. Borrowed Pages As Scratch is the specific composition where the shared unit is a memory buffer: a writer whose scratch logic is correct against its original callers, a supplier whose page-attachment logic is correct within its own threat model, and a trust boundary that exists only in the prose between the two. The relationship is the same shape as Emergent Primitive's link to Unauth Write To Execution Path: a named recurring composition with its own patch shape, its own supplier set, and enough instances to earn its own entry rather than being filed under the parent class every time it recurs.

Pool Retains Prior Writes. Mirror class across the page-ownership boundary. Pool Retains Prior Writes is the reverse direction: a prior unprivileged caller's bytes persist into a later caller's view because the pool does not zero on free, and a bounded OOB inside the later caller turns the residue into a cross-process staging area. Borrowed Pages As Scratch is the forward direction: the current caller's legitimate scratch write lands in a page an attacker chose. Both patterns describe trust boundaries between subsystems that share a page without sharing an ownership model; the two will frequently appear in the same kernel audit, because the audit question (what trust tier owns the pages at this call site) is the same and the answers run in opposite directions.

Design Debt Driver. Meta-relationship. The in-place optimization pattern is itself a design-debt driver: every out-of-place-to-in-place conversion the kernel accepts for throughput inherits every supplier that lands in the following years without re-review. The 2017 algif_aead memory overhaul and the 2017 esp4/esp6 skb_cow_data optimizations each enabled a CopyFail-shaped primitive nine years after they shipped, because their reviewer was reasoning against the supplier set of 2017 and MSG_SPLICE_PAGES did not land on UDP until 2023. The recurring class is not an in-place operation. The recurring class is the review discipline that treats a function's local correctness as sufficient when the function's destination is supplied by subsystems the reviewer does not inspect.

The function that wrote the bytes did not change. The pages under the bytes did.