PID Namespace Doppelgänger
PID Namespace Doppelgänger: Mapping Host PID to Namespace PID
See also: Book chapter with investigation notes · POC code on GitHub
This one is almost embarrassingly straightforward once you know where to look. The primitive — recovering the host-visible PID of a task together with its namespace-local PID — has been shipping in bpftrace examples for years, and academic work on PID-namespace side channels via sched_process_fork goes back to at least 2020. The POC’s contribution is narrow: package the observation as a CO-RE BPF program, confirm the mapping end-to-end by terminating a container process from the host using only the PID recovered from a BPF map, and document the hook choice.
Mechanism
The key fact is that struct task_struct carries both views. task->pid and task->tgid are the host PIDs. The namespace-local PID lives off task->thread_pid, which is a struct pid *, and within it numbers[level].nr gives you the per-level PID. The namespace inode comes from task->nsproxy->pid_ns_for_children->ns.inum.
The numbers[] indexing is subtle. struct pid has a numbers[1] flexible array at tail that extends based on nesting depth. A task at level 1 (first container) has numbers[0] for the init ns and numbers[1] for the container ns. For a task at depth level, the innermost PID lives at numbers[level], not numbers[0]. I got this wrong the first pass and was logging identical numbers for host and container PID; ten minutes of rereading kernel/pid.c fixed it.
Hook points
The POC attaches two programs and picks the one that fires:
raw_tp/sched_process_fork— primary hook. Fires on every task creation, including the commonunshare --pid/clone(CLONE_NEWPID)path. The raw tracepoint gives access to both parent and childtask_struct *as typed args; I compare theirpid_ns_for_children->ns.inumand only emit when the child entered a fresh namespace.kprobe/copy_namespaces— secondary hook, preflighted against/proc/kallsymsat load time.copy_namespacescan be inlined on some kernels; the loader disables autoload for this program when the symbol is absent, so the load stays clean.
A factual correction from the earlier draft: switch_task_namespaces fires only on the setns() path, which misses the common fork-into-new-ns case. sched_process_fork is the right hook for general coverage.
The per-event read chain in BPF:
1
2
3
4
5
6
7
8
9
10
11
struct task_struct *child = (struct task_struct *)ctx[1];
e->host_pid = BPF_CORE_READ(child, pid);
e->host_tgid = BPF_CORE_READ(child, tgid);
struct pid *pp = BPF_CORE_READ(child, thread_pid);
unsigned int level = BPF_CORE_READ(pp, level);
struct upid u = {};
bpf_probe_read_kernel(&u, sizeof(u),
(void *)&pp->numbers[0] + level * sizeof(struct upid));
e->ns_pid = u.nr;
e->ns_inum = BPF_CORE_READ(child, nsproxy, pid_ns_for_children, ns.inum);
Events land in a ringbuf for live streaming, and a BPF_MAP_TYPE_HASH keyed by host PID retains the mapping so the loader can dump a summary table on SIGINT.
1
2
3
4
5
6
7
8
9
SEC("raw_tp/sched_process_fork")
int BPF_PROG(rt_fork, struct task_struct *parent, struct task_struct *child)
{
unsigned long pi = BPF_CORE_READ(parent, nsproxy, pid_ns_for_children, ns.inum);
unsigned long ci = BPF_CORE_READ(child, nsproxy, pid_ns_for_children, ns.inum);
if (pi == ci) return 0; // no namespace transition; ignore
capture(child, 1);
return 0;
}
Acting on the mapping
Observation is useful on its own, but the POC also shows the mapping can be acted on from inside the kernel. Gated by a one-entry cfg control map, the program can call bpf_send_signal(SIGUSR1) the moment a task crosses a PID-namespace boundary, in addition to emitting the event. From sched_process_fork the raw tracepoint’s current is the parent, so that path signals the parent; from kprobe/copy_namespaces the current task is the one being copied into the new namespace, so the same helper hits the child directly. The struct evt’s signal_sent field lets the loader tell “armed and delivered” from “observer only,” and the harness accepts SIGUSR1_SENT alongside CH09_PROVEN / PID_NS_ESCAPE_PROVEN as proof.
Reproduction
The bundled trigger.sh drives a BEFORE/AFTER:
- Spawn a victim inside a fresh user+pid namespace:
unshare -Upf --mount-proc bash -c 'echo "ns_side_pid=$$"; sleep 15'. The victim reports its own ns-side PID (typically 1), but no unprivileged userspace API tells an outside observer which host PID backs that ns_pid. - Start the BPF loader and wait for it to report
status=ready. - Fire a second
unshare -Upfso the tracepoint observes it post-attach. - Parse the loader’s stdout for an event whose
host_pid != ns_pid— that line is the mapping. - Confirm with
/proc/<host_pid>/status— theNSpid:line shows both numbers (host pid, ns pid) and matches what BPF reported. kill -TERM <host_pid>from the host. The victim dies; the container sees its own PID receive SIGTERM.
A representative exit summary from a run:
1
2
3
4
5
6
[ch09] src=fork host_pid=481203 ns_pid=1 level=1 ns_inum=4026532567 comm=bash
[ch09] === post-run mapping table ===
[ch09] host_pid host_tgid ns_pid level ns_inum comm
[ch09] 481203 481203 1 1 4026532567 bash
[ch09] === 1 entries ===
=== CH09_PROVEN host_pid=481203 mapped=yes kill_from_outside=ok ===
This is not novel — ps -ef on the host already shows the same thing, as does /proc/<pid>/status. The useful property is that the BPF map lookup is O(1) and requires no /proc walking, which matters when you want to do it at scale or inside another BPF program.
Detection
The attach is visible. bpftool prog show lists the raw tracepoint and kprobe programs, and /sys/kernel/debug/tracing/events/sched/sched_process_fork reflects the tracepoint registration. The map dump (bpftool map dump name mapping) reveals both host and ns PIDs of every observed namespace entry in cleartext — the loudest tell, because the attacker has to keep the map populated for the primitive to be useful.
The read itself produces no syscall-level signal. If you want to catch a host process using BPF-derived namespace PIDs to send cross-namespace signals, the right place is the kill audit trail: a host uid sending SIGTERM to a container’s init process is already flagged by most container-runtime monitors regardless of where the PID came from.
Prior art worth citing: bpftrace ships pidnss.bt and similar scripts that do essentially this lookup, and Cilium/Tetragon uses the same pattern for PID tracking in its observation code.
Related material
- Full chapter with investigation notes: Chapter 9 — PID Namespace Doppelgänger
- POC source: dBPF-pocs/pocs/ch09-pid-doppel/
- Harness entry in
proof.py:Poc("ch09", ...)