TL;DR — Dirty Pipe is a Linux kernel vulnerability (CVE-2022-0847) that lets an unprivileged user overwrite data in read-only files by abusing an uninitialised flag in the kernel's pipe/page-cache plumbing. No memory-corruption gymnastics, no ROP, no ASLR bypass — just a logic bug that grants arbitrary writes into the page cache of files you can only read. This article explains the underlying machinery (pipes,splice, page cache, thePIPE_BUF_FLAG_CAN_MERGEflag), the precise root cause, how it escalates to root via/etc/passwdoverwrite or SUID-binary hijack, its container-escape implications, and the one-line patch that fixed it.
Executive Summary
Most kernel privilege-escalation exploits are intricate memory-corruption chains. Dirty Pipe is the opposite, and that is exactly why it became famous: it is a logic bug, trivially reliable, requiring no exotic primitives. Disclosed by Max Kellermann in March 2022, it affected Linux kernels from 5.8 onward and gave any local unprivileged user the ability to inject bytes into files they have only read access to.
The bug lives at the intersection of three kernel subsystems that most developers treat as black boxes: pipes, the page cache, and the zero-copy `splice()` syscall. The page cache is the kernel's in-memory copy of file contents; when you read a file, you're usually reading page-cache pages. splice() moves data between a file and a pipe without copying it through user space — for efficiency, it makes the pipe reference the page cache pages directly. A pipe buffer carries flags describing what may be done with its pages. The vulnerability is that one of those flags — PIPE_BUF_FLAG_CAN_MERGE, meaning "new writes may be merged into this page" — could be left set from a previous operation and not reinitialised, so a subsequent write into a pipe buffer backed by a read-only file's page-cache page would happily merge attacker data straight into the cached file contents.
The consequence is enormous and simple: arbitrary overwrite of any readable file's contents in the page cache, which the kernel then treats as the file's data. From there, root is a formality — overwrite /etc/passwd, or hijack a SUID binary. The name (a nod to the older "Dirty COW") captures it: dirty pages in a pipe.
Prerequisite Concepts
flowchart LR
A["File on disk"] --> B["Page Cache<br/>(kernel's in-memory copy)"]
B --> C["read() serves from here"]
B --> D["splice(file → pipe)<br/>makes pipe reference<br/>these cache pages directly"]
D --> E["Pipe buffer points at<br/>read-only file's page"]
style E fill:#2a0c14,stroke:#ef233c,color:#fee2e2- Page cache. When any process reads a file, the kernel loads its pages into the page cache. Multiple readers share these pages. Writing to the page cache (via a legitimate writable mapping) marks the page dirty, and it's later flushed to disk.
- Pipes. A pipe is a kernel ring of
pipe_bufferstructures, each pointing to a page of data plus aflagsfield. Writing to a pipe appends data; if the last buffer's page has room and theCAN_MERGEflag is set, the new write is merged into that existing page rather than allocating a new one — an optimisation. - `splice()`. A zero-copy syscall:
splice(file_fd, ..., pipe_fd, ...)moves data from a file into a pipe without copying. To avoid the copy, the kernel makes the pipe buffer point directly at the file's page-cache page. Now the pipe and the page cache share the same physical page.
The danger is now visible: if a pipe buffer pointing at a read-only file's page-cache page has CAN_MERGE set, then writing into the pipe will merge attacker bytes into the shared page — i.e., into the file's cached contents — even though the attacker only opened the file O_RDONLY.
The Root Cause: A Flag That Wasn't Reset
The bug is a missing initialisation. Two relevant kernel changes set the stage:
- A 2016 refactor introduced per-buffer flags, including
PIPE_BUF_FLAG_CAN_MERGE. - A 2020 change (kernel 5.8) reworked the pipe
anonpage handling but failed to clear `PIPE_BUF_FLAG_CAN_MERGE` when a pipe buffer was reused.
So the flags field of a pipe_buffer could retain CAN_MERGE from a previous write, and then be populated — via splice() — with a page-cache page from a read-only file. The kernel, seeing CAN_MERGE set, permitted a subsequent pipe write to merge into that page.
THE EXPLOIT SEQUENCE (conceptual) 1. Create a pipe.2. Fill the pipe completely with arbitrary data (write() until full), then drain it (read() it all). This leaves pipe_buffers with the PIPE_BUF_FLAG_CAN_MERGE flag SET and the buffers marked empty/reusable.3. splice() from the TARGET read-only file into the pipe, starting just BEFORE the offset you want to overwrite. This makes a pipe_buffer point at the file's page-cache page — and it INHERITS the stale CAN_MERGE flag.4. write() your malicious bytes into the pipe. Because CAN_MERGE is set, the kernel MERGES them into the page-cache page of the read-only file.5. The file's cached contents now contain attacker data — no write permission was ever required.The decisive subtlety: step 2 primes the flag, and step 3's splice reuses a buffer without clearing it. The write in step 4 then lands in the file's page cache.
Constraints (and Why They Don't Save You)
The vulnerability has three limitations — none of which meaningfully blunt it:
CONSTRAINT WHY IT DOESN'T MATTER1. Cannot overwrite the page at offset 0 Start your overwrite at offset 1+; (the first byte of the first page is target text that lives mid-file not mergeable into) (e.g. a field in /etc/passwd line)2. Cannot extend file size / add new pages You're overwriting existing bytes (write stays within existing page) in place — plenty for hijacking3. Page-cache changes are not flushed to The IN-MEMORY copy is corrupted, which disk (file on disk unchanged) is exactly what the kernel SERVES to readers — including SUID executionConstraint 3 sounds reassuring but is the opposite: because the kernel serves the page cache to anyone reading or executing the file, corrupting the cache is sufficient. A SUID binary executed while its page cache is poisoned runs the attacker's modified bytes; an su/sshd reading /etc/passwd from cache reads the modified line. The disk copy being clean only means the corruption is transient (until the page is evicted/reloaded) — which is irrelevant to an attacker who just needs one privileged read or execution.
Escalation Path 1 — Overwrite /etc/passwd
The cleanest root: modify a privileged account's entry. /etc/passwd is world-readable (so splice works) but root-owned (so you "can't" write it). Dirty Pipe writes anyway.
ORIGINAL line: root:x:0:0:root:/root:/bin/bash The 'x' means "password is in /etc/shadow". OVERWRITE the field after 'root:' so the password hash is INLINE,swapping 'x' for a known hash (e.g. of "password"):MODIFIED line: root:$1$abc$<hash>:0:0:... (length-matched in place)Because you can only overwrite existing bytes (no size change), the exploit picks a field and writes a hash of matching length, then su root with the chosen password. The public PoC dirtypipez automated exactly this. Root in seconds, no crash, works on every affected kernel.
Escalation Path 2 — Hijack a SUID Binary
Even more elegant and used by the most popular PoC: you don't need to find a writable file at all. Pick any SUID-root binary (e.g. /usr/bin/su, passwd), which you can read and execute but not write. Dirty Pipe overwrites a portion of its page-cache pages with a tiny payload that, when the SUID binary is executed, drops a root shell — then restores the original bytes.
sequenceDiagram
participant U as Unprivileged user
participant K as Kernel page cache
participant S as SUID binary (root-owned)
U->>K: splice(SUID binary → pipe) at chosen offset
U->>K: write shellcode (CAN_MERGE merges into page cache)
Note over K: SUID binary's cached code now patched
U->>S: execute the SUID binary
S-->>U: runs patched code AS ROOT → root shell
U->>K: restore original bytes (cleanup)This needs no world-writable target and no /etc/passwd quirks — any readable SUID-root binary on the system is a launchpad. It's the reason Dirty Pipe is considered near-universal local root on affected kernels.
Container and Android Implications
- Containers. Containers share the host kernel. A Dirty Pipe exploit inside a container that can read a host file mapped into it — or that can reach a vulnerable code path — can corrupt page-cache pages shared with the host, enabling container escape to host root. This made Dirty Pipe a serious cloud/Kubernetes concern, not just a single-host LPE.
- Android. Android runs the Linux kernel; affected devices (kernels 5.8+) were vulnerable, raising the prospect of app-to-root escalation on phones with the affected version — significant given Android's slow patch cadence.
Detection and the Fix
The patch is as small as the bug: the kernel now explicitly clears the buffer flags when a pipe buffer is allocated/reused, so a splice-populated buffer no longer inherits a stale CAN_MERGE:
// fix: initialise flags to 0 in pipe buffer setup// (lib/iov_iter.c / fs/splice.c — clear buf->flags so CAN_MERGE// cannot persist into a splice-backed, file-cache buffer)buf->flags = 0;Fixed in 5.16.11, 5.15.25, 5.10.102 and backported widely. The remediation is unambiguous:
DEFENCE1. PATCH the kernel — the only real fix. Update to a fixed point release.2. For unpatchable systems: minimise local access, since the bug needs a local unprivileged shell to trigger.3. Reduce SUID-binary footprint (limits the hijack targets) and monitor for unexpected /etc/passwd or SUID binary modifications.4. In container environments, patch the HOST kernel (containers can't fix it themselves — they share the kernel) and apply seccomp/restrict splice where feasible.There is no meaningful userland mitigation that fully closes it; because the flaw is in core kernel plumbing, patching is mandatory. Detection focuses on the effects (anomalous /etc/passwd or SUID binary content/integrity changes, unexpected root shells from unprivileged processes) since the exploit itself is quiet and leaves no crash.
Why Dirty Pipe Is Pedagogically Important
Dirty Pipe is a master class in a category often underestimated: kernel logic bugs. There is no overflow, no use-after-free, no spray, no leak, no ROP. The "exploit primitive" is handed to you directly by a missing = 0. It demonstrates that:
- Subsystem interactions (pipes × page cache × splice) are a rich, under-audited bug surface — each component is fine alone; the bug is in how they compose.
- Uninitialised/stale state is as dangerous as memory corruption when the state controls a security decision (here, "may I merge into this page?").
- Reliability beats sophistication: Dirty Pipe's deterministic, mitigation-agnostic nature made it instantly weaponised worldwide, far faster than any heap exploit.
For researchers, the takeaway is to hunt where subsystems meet and where flags/permissions are set, copied, and reused — not just where pointers are dereferenced.
Container Escape via Dirty Pipe
The most dramatic application of Dirty Pipe was not on bare-metal Linux — it was the container escape that made cloud operators scramble. Containers share the host kernel; if the host kernel is vulnerable, any process inside any container with read access to a suitable file can exploit the bug against host-kernel page cache, regardless of container isolation settings.
The Attack: Overwriting a Host SUID Binary from Inside a Container
The key insight is that a container running on an affected kernel shares the same page cache as the host. If a container can read a file that also exists on the host's filesystem (via a shared /proc, overlayfs layers, or a mounted volume), it can corrupt the host's page-cache copy of that file. The most impactful target: any SUID-root binary on the host whose pages are also accessible from within the container.
# Inside a container on a vulnerable kernel# The /proc/sched_debug or /proc/<pid>/mem path gives indirect page cache access,# but the direct path is: if the container shares overlayfs with the host,# the lower-layer files of the container IMAGE are the same inodes as the host. # Step 1: Confirm vulnerability — check kernel versionuname -r# Output: 5.10.x (between 5.8 and fix: 5.16.11 / 5.15.25 / 5.10.102) # Step 2: Use the dirtypipez PoC targeting a readable SUID binary# The PoC reads the binary, overwrites key bytes with shellcode,# executes the SUID binary (runs as root), then restores original bytes # The PoC is ~80 lines of C; core loop:# open(target, O_RDONLY) → fd# create pipe, fill/drain to set CAN_MERGE# splice(fd, offset, pipe, ...) → pipe now points at file's page cache# write(pipe, payload) → CAN_MERGE merges payload into page cache# execute the SUID binary → root shell# write(pipe, original_bytes) → restore for stealth # Step 3: After gaining root inside the container, escape to host:# With root in the container + CAP_SYS_ADMIN (common in privileged pods):mkdir /tmp/hostmntmount /dev/sda1 /tmp/hostmnt # mount host diskchroot /tmp/hostmnt # chroot to host filesystem# Now operating as root on the HOST, outside the containerOverwriting /etc/passwd Inside the Container to Affect the Host
An alternative path that does not require a SUID binary: if /etc/passwd is backed by the same inode on the host and container (e.g. a bind-mounted or overlayfs-shared path), overwriting it from inside the container modifies the host's cached version:
/* Conceptual C exploit fragment — /etc/passwd overwrite */int fd = open("/etc/passwd", O_RDONLY);// prime CAN_MERGE via pipe fill/drain cycle// splice from offset 1 to avoid offset-0 restrictionsplice(fd, &off, pipefd[1], NULL, 1, 0);// write new password hash at root's fieldwrite(pipefd[1], "root:$1$attacker$hash:", 22);// now: su root (with attacker password) → root shellThis technique was demonstrated publicly by the security researcher who discovered the CVE and was included in Dirty Pipe PoC tools (dirtypipez, dirty_pipe_shell) circulating within days of disclosure.
Dirty Pipe vs. Dirty COW: Comparison
Dirty Pipe inevitably draws comparison to Dirty COW (CVE-2016-5195) — both give unprivileged users write access to read-only files. The two bugs are mechanistically distinct and differ significantly in reliability, affected versions, and the fix complexity.
| Property | Dirty Pipe (CVE-2022-0847) | Dirty COW (CVE-2016-5195) |
|---|---|---|
| Mechanism | Logic flaw: stale PIPE_BUF_FLAG_CAN_MERGE in pipe/splice interaction | Race condition in copy-on-write handling of write faults on read-only mappings |
| Exploit type | Deterministic, no race — single-threaded exploit works reliably | Race condition — requires two competing threads; timing-sensitive |
| Reliability | Near-100%; exploit is deterministic on affected kernels | Variable; race makes it unreliable on some hardware/loads; can cause kernel panic |
| Kernel crash risk | Very low — no memory corruption, clean logic path | Moderate — race condition can corrupt kernel state and panic the system |
| Affected kernel versions | Linux 5.8 – 5.16.10 (also 5.15.x < 5.15.25, 5.10.x < 5.10.102) | Linux kernel ≤ 4.8.3 (present since early 2.x; patched Oct 2016) |
| Can overwrite offset 0? | No — first page-offset byte cannot be targeted | Yes — any byte position writable |
| Can extend file size? | No — constrained to existing page boundaries | No — same constraint |
| Disk persistence | No — page cache only; disk unchanged | Possible with /proc/self/mem variant (can flush to disk) |
| Fix complexity | One-line flag initialisation (buf->flags = 0) | Multi-commit fix across mm/ subsystem |
| Container escape | Yes — shared page cache across container/host boundary | Yes — shared kernel, same race exploitable from container |
| Android affected | Yes — devices running 5.8+ kernel | Yes — affected most pre-4.8 Android devices for years |
The key practical difference for an attacker: Dirty Pipe is significantly more reliable because it requires no race condition. Dirty COW exploits sometimes need dozens or hundreds of attempts before the race is won, and on a production system under load, a failed race can crash the kernel — an obvious operational concern. Dirty Pipe exploits succeed on the first attempt, every time, with no crash risk.
Detecting Dirty Pipe Exploitation
Dirty Pipe is intentionally quiet — it produces no crash, no segfault, and no visible output. However, several detection signals can identify exploitation attempts.
Kernel Audit Events
The Linux audit subsystem (auditd) can be configured to log splice() syscalls from unprivileged processes, especially when targeting sensitive files. Add rules to monitor pipe-related syscall patterns:
# Audit splice() calls from non-root processesauditctl -a always,exit -F arch=b64 -S splice -F uid!=0 -k dirty_pipe_splice # Monitor writes to pipe file descriptors that were splice-populated# (harder to distinguish, but watch for write() on pipe fds shortly after splice())auditctl -a always,exit -F arch=b64 -S write -F uid!=0 \ -F path=/etc/passwd -k passwd_write_attemptUnusual Writes to Read-Only Files
File integrity monitoring (FIM) tools detect page-cache corruption by comparing running-system file hashes against known-good baselines. If Dirty Pipe modifies /etc/passwd in the page cache, an in-memory hash check will diverge from the on-disk hash:
# Detect in-memory vs on-disk divergence for sensitive files# On-disk hash (unchanged by Dirty Pipe)sha256sum /etc/passwd # Force page cache flush and re-read from disk, then compareecho 3 > /proc/sys/vm/drop_cachessha256sum /etc/passwd # should match if no disk write # If a process reads /etc/passwd before cache flush, it gets the poisoned version# → IDS/FIM that hashes the in-memory copy will catch the divergenceTools like AIDE, Tripwire, and osquery with real-time file monitoring detect unexpected modifications to /etc/passwd, /etc/shadow, and SUID binaries — the primary exploitation targets. The Falco runtime security tool can be configured to alert on:
- Any write to
/etc/passwdor SUID binaries from non-root processes splice()syscalls followed immediately bywrite()on the same pipe FD- Unexpected UID=0 shells spawned from processes owned by non-root users
Syslog Patterns
A successful Dirty Pipe root escalation followed by privilege use generates syslog entries from the root-owned shell or SUID binary execution. Patterns to alert on:
# syslog: sudden root session from non-root user without sudoauth.info su: pam_unix(su:session): session opened for user root by user(uid=1000) # syslog: SUID binary executed, root shell spawnedkernel: audit: type=1400 msg=... uid=1000 exe="/usr/bin/su" ... # No prior sudo, no prior authentication → suspicious root sessionIn containerised environments, the clearest signal is a root process appearing outside expected container namespaces — detectable via nsenter state checks, cgroup membership, and Falco container-aware rules that flag privilege escalation crossing namespace boundaries.
MITRE ATT&CK Mapping
| ATT&CK ID | Technique | Dirty Pipe Relevance |
|---|---|---|
| T1068 | Exploitation for Privilege Escalation | CVE-2022-0847 directly enables local root via kernel exploitation |
| T1611 | Escape to Host | Container escape via Dirty Pipe corrupting host page cache from within a container |
| T1222.002 | File and Directory Permissions Modification: Linux | Overwriting SUID binaries or /etc/passwd to modify permissions/authentication |
| T1003.008 | OS Credential Dumping: /etc/passwd and /etc/shadow | Reading or modifying /etc/passwd to inject a known password hash |
| T1055 | Process Injection | Patching a SUID binary's in-memory code before execution is analogous to process injection |
| T1548.001 | Abuse Elevation Control Mechanism: Setuid and Setgid | Exploiting the SUID binary execution path after page-cache poisoning |
Conclusion and Takeaways
CVE-2022-0847, Dirty Pipe, is the rare kernel vulnerability that any practitioner can fully understand in a single sitting and exploit in a single afternoon. Its power comes not from defeating a stack of mitigations but from a clean logic flaw: a pipe buffer's PIPE_BUF_FLAG_CAN_MERGE flag survived buffer reuse, so a splice() that pointed a pipe buffer at a read-only file's page-cache page inherited a stale "you may merge writes here" permission — and a subsequent pipe write merged attacker bytes directly into the cached contents of a file the user could only read. The page cache is what the kernel serves to every reader and executor, so corrupting it is enough: overwrite a field in /etc/passwd, or transiently patch a SUID-root binary's code and execute it, and you are root in seconds, on every affected kernel, with no crash and no forensic noise. In containerised and Android environments the same primitive threatened host escape and device root.
The defensive story is short and absolute: this is a kernel-internals bug with no robust userland workaround, so patch the kernel — the fix is a one-line flag initialisation, shipped in 5.16.11/5.15.25/5.10.102 and backported broadly, and in container fleets it must be applied to the host. Beyond patching, minimise local access, trim the SUID footprint that provides hijack targets, and monitor for the tell-tale effects. More broadly, Dirty Pipe should recalibrate where researchers and defenders look: the seams between trusted subsystems, and the lifecycle of the flags and permissions that flow across them, are every bit as fertile — and as dangerous — as the memory-corruption bugs that get most of the attention.