Skip to content

eBPF-based security platform

kernel-obs: Add user namespace unshare counter

Michi Mutsuzaki80dd25bda578
diff --git a/upatches/kernel-obs/Beefile b/upatches/kernel-obs/Beefile
index 975e7d31..b50827b7 100644
--- a/upatches/kernel-obs/Beefile
+++ b/upatches/kernel-obs/Beefile
@@ -19,6 +19,9 @@ input = "watch.list"
generator = ["python3", "gen_watch_rodata.py"]
update = ["python3", "gen_watch_rodata.py"]
+[[upatches]]
+source = "userns-unshare-counter.bpf.c"
+
[[files]]
source = "gen_watch_rodata.py"
target = "gen_watch_rodata.py"
diff --git a/upatches/kernel-obs/README.md b/upatches/kernel-obs/README.md
index e654df35..db71643e 100644
--- a/upatches/kernel-obs/README.md
+++ b/upatches/kernel-obs/README.md
@@ -1,10 +1,13 @@
# kernel-obs
-The `kernel-obs` package installs the `kernel-obs` upatch: host
+The `kernel-obs` package installs the `kernel-obs` upatch and the
+`userns-unshare-counter` upatch. `kernel-obs` provides host
observability for executed programs, network peers and volumes,
listeners, DNS answers, watched destinations, privilege transitions
and access to credential and persistence files. Every hook observes;
-nothing in this package enforces.
+nothing in this package enforces. `userns-unshare-counter` counts calls
+to `unshare()` that request a new user namespace; it does not block or
+change them. Its `userns_unshare_calls` per-CPU map holds the count.
Each table lives in one BPF arena and holds one export interval: the
export flips to a fresh buffer, drains the previous one and clears it,
@@ -46,6 +49,10 @@ sudo shirudo upatch list
## Tables
+To inspect the unshare counter, use `shirudo upatch map
+userns-unshare-counter userns_unshare_calls`. Running `unshare -U true`
+triggers the traced path.
+
| Map | Key | Records |
| --- | --- | --- |
| `exec` | executable, calling image, cgroup, euid | name, resolved path, uid, effective capabilities, pid, exec count; scripts yield one record for the script and a flagged one for the interpreter |
diff --git a/upatches/kernel-obs/userns-unshare-counter.bpf.c b/upatches/kernel-obs/userns-unshare-counter.bpf.c
new file mode 100644
index 00000000..432eb881
--- /dev/null
+++ b/upatches/kernel-obs/userns-unshare-counter.bpf.c
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
+/*
+ * Copyright (c) 2026 Authors of shirudo
+ * Count calls to unshare that request a new user namespace.
+ */
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+#define CLONE_NEWUSER 0x10000000UL
+
+char _license[] SEC("license") = "Dual BSD/GPL";
+
+struct {
+ __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
+ __uint(max_entries, 1);
+ __type(key, __u32);
+ __type(value, __u64);
+} userns_unshare_calls SEC(".maps");
+
+SEC("fentry/ksys_unshare")
+int BPF_PROG(count_userns_unshare, unsigned long unshare_flags)
+{
+ __u32 zero = 0;
+ __u64 *count;
+
+ if (!(unshare_flags & CLONE_NEWUSER))
+ return 0;
+
+ count = bpf_map_lookup_elem(&userns_unshare_calls, &zero);
+ if (count)
+ (*count)++;
+
+ return 0;
+}