
Backports the CVE-2016-5195 (Dirty COW) kernel fix to Linux 4.4.20, with conflict-resolved patches, a QEMU race regression harness, and build environment notes.
My second kernel-backport exercise, and a deliberately harder one than the first. Where the Dirty Pipe backport was a clean two-line fix, this one is about the parts that actually make backporting difficult: an upstream fix that spans more than one commit, real merge conflicts that have to be resolved by hand, and a 2016 kernel that won't build or boot with a modern toolchain until you rebuild the toolchain itself. By far more interesting and entertaining than the first.
The point of this repository is the judgement: deciding which commits a target kernel actually needs, resolving conflicts correctly rather than mechanically, and reconstructing a build/boot environment for an old kernel from scratch.
Same probabilistic race test, same harness, two kernels built from the same 4.4.20 base, yet two opposite outcomes:
| Kernel | Dirty COW race (3 s budget) | Verdict |
|---|---|---|
| 4.4.20 (unpatched) | RO file corrupted | VULNERABLE |
| 4.4.20 + backported fix | RO file intact | NOT_VULNERABLE |
RESULT vulnerable: VULNERABLE # race won in ~1s, victim 0444 "AAAA" -> "AXAA"
RESULT patched: NOT_VULNERABLE # full 3s budget exhausted, victim "AAAA" unchanged
CVE-2016-5195 ("Dirty COW") is a race condition in the kernel's copy-on-write
handling of private, read-only memory mappings. By racing a write through
/proc/self/mem against madvise(MADV_DONTNEED), an unprivileged user can land
a write on the original page-cache page of a read-only file instead of on
the private COW copy, hence corrupting a file they only have read access to,
which leads to local privilege escalation. It was found being actively exploited
in the wild (reported by Phil Oester).
The fix introduces an internal FOLL_COW flag: instead of racily clearing
FOLL_WRITE after a COW, the kernel marks that the COW already happened and
validates it with the pte dirty bit, closing the window.
The upstream fix (19be0eaffa3a, by Linus Torvalds) has no Fixes: tag, and
the commit message explains why: this is a design-level flaw that was first
(badly) patched in 2005 (4ceb5db9757a), reverted due to s390 problems
(f33ea7f404e5), and only properly fixed eleven years later. There is no clean
"the commit that introduced it", actually, the bug predates any 4.x kernel,
so a 4.4.20 target is unambiguously exposed. Exposure analysis here isn't about a
version window (as it was for Dirty Pipe), but rather about the bug's archaeology.
One dependency from the commit message matters for the backport: the fix relies
on pte_dirty(), and the message notes the s390 software dirty bit only landed
in v3.9. My target is 4.4 (post-3.9), so the precondition holds on all
architectures, but nonetheless the kind of dependency check a backporter has to
make before applying.
Searching the history for the FOLL_COW flag (git log -S'FOLL_COW') surfaces
not one but four distinct changes. The backporting skill is deciding which
of them a 4.4.20 target actually needs:
| Change | Needed for 4.4.20? | Why |
|---|---|---|
19be0eaffa3a - FOLL_COW core fix in mm/gup.c | Yes | The fix itself |
8310d48b125d - respect FOLL_COW for THP in huge_memory.c | Yes | 4.4 has THP, so without it the fix is incomplete and introduces a kernel hang (see below) |
mm, gup: close FOLL MAP_PRIVATE race | No | Only ever backported to 3.2/3.4 branches; never reached any 4.x - not applicable |
mm/gup: fix FOLL_FORCE COW … and remove FOLL_COW | No | A later, separate CVE (2022); out of scope for Dirty COW |
The THP follow-up is the interesting one. Its own commit message
(8310d48b125d, Keno Fischer) states that the original fix updated the check in
gup.c but a "similar check in huge_memory.c was forgotten", and that remote
writes to RO transparent-huge-page regions then cause an infinite loop in the
kernel. So applying only the main fix to 4.4.20 (which has THP) would leave the
huge-page path exploitable and also introduce a DoS. I confirmed
follow_trans_huge_pmd exists in 4.4.20 before concluding the follow-up was
required, so the evidence has been taken directly from the source tree rather than
assuming from the version number or something else.
I distinguished mainline commits from their stable backports with
git merge-base --is-ancestor <sha> origin/master rather than guessing from
git describe output, and cherry-picked the mainline versions to force real
adaptation onto 4.4.
Both commits conflicted against 4.4.20 since the mainline fixes landed around 4.9. I resolved both conflicts manually first, then as a sanity check, cross-referenced my changes against the official 4.4 stable backport, and it was great to see my manual resolution matched upstream perfectly.
Main fix (gup.c + mm.h). Clean auto-merge in gup.c; the conflict was in
include/linux/mm.h, over the value of the new FOLL_COW flag. The upstream diff
also wanted to add FOLL_REMOTE 0x2000 (which didn't exist in 4.4) as context. I
kept only FOLL_COW and set it to 0x4000 so it matches the upstream's value,
which is what's done in the stable patch as well, leaving 0x2000 reserved, a
deliberate choice of upstream-consistency, even if that leaves a gap.
THP follow-up (huge_memory.c). The conflict dumped a large block of
upstream context - touch_pmd, follow_devmap_pmd, an EXPORT_SYMBOL, none of
which exists in 4.4.20. Only two pieces are the actual security fix:
can_follow_write_pmd() and the changed check in follow_trans_huge_pmd. I kept
those and omitted the rest, including the WARN_ONCE in follow_devmap_pmd whose
target function doesn't exist in 4.4.
Dirty COW is a race, so the test is probabilistic rather than deterministic. The
regression test (initramfs/dirtycow-test.c) runs two threads: a writer through
/proc/self/mem and an madvise(MADV_DONTNEED) loop, against a root-owned
0444 file mapped MAP_PRIVATE, for a wall-clock budget (3 s by default,
overridable). It reports VULNERABLE if the file changes even once in that
budget, NOT_VULNERABLE otherwise. The asymmetry makes the verdict robust: a
vulnerable kernel wins the race in milliseconds (the run exits early), while a
patched kernel exhausts the full budget and never wins. It asserts a behavior,
not whether a syscall appeared to succeed.
The harness (run-matrix.sh) boots each kernel in QEMU, scrapes a single
POC_RESULT: sentinel line from the serial console, and maps it to an exit code.
The run log shows the asymmetry directly: the vulnerable kernel powers down at
~1.2 s (won early), the patched kernel at ~3.5 s (full budget, never won).
A note on the test as written: it runs as root (PID 1 in the initramfs), so it isolates the page-cache corruption primitive (the exact invariant the fix restores) rather than end-to-end privilege escalation. For a backport regression test, the primitive is the correct and sufficient assertion. It also exercises the normal-page path (the classic Dirty COW); the THP path backported in commit 2 is covered by analysis and could be exercised with a huge-page variant of the test.