Browse by type
System Process Integrity & Cross-view Analysis

"I'm going to sing, so shine bright, SPiCa..."
SPiCa is an eBPF-based Linux rootkit detector written in Rust. The name comes from the Hatsune Miku song SPiCa and the star it references — Spica (Alpha Virginis), the brightest point in Virgo. What looks like a single star to the naked eye is actually a spectroscopic binary: two stars in mutual orbit, indistinguishable as separate objects without measuring their spectra. SPiCa applies the same principle to kernel observation: multiple independent channels measure the same kernel state from physically distinct mechanisms, and a rootkit that suppresses one is exposed by the others.
Disclaimer: Significant portions of this codebase were generated or refactored with GLM assistance. Rigorous testing and iterative design were applied, but review the code for security and performance before production use.
bpf()SPiCa is designed to defeat the eBPF-constrained adversary — an attacker with elevated privileges (CAP_BPF or CAP_SYS_ADMIN) who loads a privileged eBPF program into the kernel. This adversary is fundamentally weaker than an LKM rootkit because the BPF verifier imposes hard constraints:
| Constraint | Effect on adversary |
|---|---|
| Bounded loops (~1M instructions per program) | Cannot scan all of kernel memory |
| Pointer safety (direct dereference requires provably valid, bounded target) | Cannot follow arbitrary pointers |
| Restricted helpers (per-program-type allowlist) | Cannot call kmalloc, vmalloc, or any kernel allocation function |
No arbitrary kernel write (bpf_probe_write_kernel heavily restricted) |
Cannot modify task_struct, tracepoint funcs arrays, or BPF program bytecode |
| Map access scoped to loaded references | Cannot access maps belonging to other programs (ID enumeration is closed by the deny-by-default bpf() gate — see §7) |
With LKMs blocked by the LSM gate after boot, this constrained adversary is the realistic remaining threat. SPiCa's anti-evasion machinery is calibrated for this threat — every defense is honest about what it covers and what it doesn't.
init_module. SPiCa raises the floor by blocking the easier LKM vector but does not ceiling the adversary.SPiCa is the last-resort layer in a defense-in-depth stack, not a substitute for the layers above.
SPiCa runs seven eBPF programs attached to kernel hooks, plus a userspace detection engine that cross-correlates their output against the system's view of itself (/proc).
| Channel | Mechanism | What suppresses it | Cost of suppression |
|---|---|---|---|
/proc |
procfs directory enumeration | DKOM (unlink process from task list) | Low — well-understood rootkit technique |
Ledger writers (sched_switch, fork, exit) |
eBPF tracepoints appending to per-CPU hash chains | Detach programs; filter context | Medium — requires kernel write to funcs array, and the chain freezes → TAMPER |
| NMI notary | Hardware perf counter interrupt → eBPF perf event | Hook the IDT/NMI handler | Near-certain kernel panic — hardware interrupt, not maskable |
The key architectural property: a rootkit cannot suppress the ledger and the notary simultaneously without the suppression itself becoming detectable or destabilizing. Suppressing NMI requires patching the IDT (Interrupt Descriptor Table), which panics on most kernels. Suppressing the ledger writers stops the chains advancing — and the NMI notary (which snapshots chain position from hardware interrupt context) is still running to notice. This is the "live bomb" — the attacker's only path to total blindness is one that likely crashes the system.
Every tick takes exactly three snapshots and classifies every TGID against a single differential anchored on the ledger — no separate check paths, no second loops:
| View | What it is | What spoofs it |
|---|---|---|
| L | ledger sighting — chain-verified kernel scheduler truth (frozen, NMI-notarized) | only kernel-level suppression → TAMPER/SILENT |
| R | /proc via raw getdents64 |
DKOM (kernel unlinks the task) |
| C | /proc via libc readdir — the view every tool (ps/ls/top) gets |
LD_PRELOAD hook or DKOM |
L R C → verdict
1 0 0 → DKOM — scheduler runs it, kernel won't list it (both /proc paths blind)
x 1 0 → HOOK — kernel lists it, libc hides it (LD_PRELOAD interception)
0 1 x → GHOST — /proc lists what the scheduler has never run (fake/idle entry)
0 0 1 → INJECT — libc reports a process the kernel doesn't have (fabricated entry)
Every deviation from the honest 111 pattern is a detection, and the pattern itself localizes the layer — kernel, libc, or entry-fabrication. Channel-level integrity rides above the table: chain gap/divergence/notary mismatch → TAMPER; global silence → SILENT.
Each detection class is a differential verdict: a discrepancy between two or more channels. The detection engine is a pure function over the registry + /proc snapshot + channel timestamps — no I/O, no side effects, fully unit-testable.
The original architecture tried to hide state (XOR key in .bss, "verifier-bounded address secrecy"). That assumption was wrong: .bss globals are backed by an ordinary BPF_MAP_TYPE_ARRAY map, enumerable with BPF_MAP_GET_NEXT_ID → BPF_MAP_GET_FD_BY_ID → BPF_MAP_LOOKUP_ELEM by any CAP_SYS_ADMIN process — the key was readable, and worse, BPF_MAP_UPDATE_ELEM could spoof the heartbeat itself from userspace, defeating the NMI integrity check without touching the kernel.
The redesign inverts the game: the ledger is public; the security property is append-only-by-construction and tamper-evidence. Records are hash-chained in-kernel, chain state is sealed with bpf_map_freeze (syscall writes → EPERM for the map's lifetime; program writes unaffected), and the NMI program notarizes the chain from hardware interrupt context. Nothing needs to be secret. Every suppression or edit of evidence is provable — a chain gap, a divergent head, or a notary mismatch.
eBPF programs attached to sched_switch, sched_process_fork, and sched_process_exit tracepoints fire on process lifecycle events. They read task identity from the tracepoint arguments using traditional (non-BTF) fixed-offset reads:
sched_switch: ctx.read_at::<u32>(56) → next_pid, ctx.read_at::<[u8;16]>(40) → next_comm
sched_process_fork: child_comm @ 28, child_pid @ 44
sched_process_exit: comm @ 8, pid @ 24
Deliberately not BTF/CO-RE. The tracepoint argument layout is stable across kernel versions (it's part of the tracepoint ABI). Using hardcoded offsets avoids the kernel-version fragility of BTF-resolved struct navigation. This is a deliberate design choice documented in §11.
Each writer appends a LedgerRecord through the shared append_ledger path:
seq = per_cpu_seq + 1 and head = chain_hash(prev_head, seq, record) (FNV-1a-64 over the previous head, the seq, and every content field — same function compiled into both the eBPF side and the userspace verifier, one definition of continuity).sc_ledger — a frozen 65536-slot ARRAY map, the kernel-side evidence ring (~3.6 MB, bounded for the lifetime of the host).sc_sched ring buffer (transport to userspace).LEDGER_SEQ/LEDGER_HEAD (a notary snapshot racing an append sees the old or the new pair — never a half-written record).Per-CPU chains, no locks. Each CPU owns LEDGER_HEAD[cpu]/LEDGER_SEQ[cpu] and only its own tracepoints write them (they don't nest; the NMI program only reads). This removes every cross-CPU write race without spinlocks.
Dedupe + heartbeat. sched_switch fires thousands of times per second; the dominant pattern is the same TGID rescheduling itself. A per-CPU LAST_TGID cache suppresses records until the scheduled TGID actually changes — ring traffic drops by orders of magnitude versus per-switch emission. Idle switches and long single-task runs emit at most one KIND_HEARTBEAT record per CPU per second, so a busy or idle CPU can never look like a detached probe.
LRU saturation is dead by construction. The kernel evidence ring is a fixed recent window; the authoritative chain is the daemon's unbounded userspace copy. Evicting a slot from the kernel ring deletes nothing — the record is already committed by its successor's prev_head. Flood-churn (fork bombs) is itself a signal, and the notary bounds any suppression window to one second.
The detection engine uses a single monotonic time base: nanoseconds since SPiCa's process startup (Instant::now() since run_detection() entry). When a record arrives, the event handler stores the caller-provided now (process-local nanos), not the eBPF bpf_ktime_get_ns() value embedded in the record. This avoids the time-base mismatch that would occur if kernel boot time were mixed with process-local time — a bug that was present in earlier versions and caused all liveness predicates to silently fail.
The NMI program is attached to a hardware perf counter (HW_CPU_CYCLES) that fires at hardware interrupt level. NMIs cannot be disabled with cli — suppressing them requires reprogramming the PMU or hooking the IDT, both of which are loud and destabilizing.
The notary never appends to the ledger — a single writer per CPU is what makes the chains race-free. Once per second per CPU it snapshots its CPU's (seq, head) pair into a NotaryEvent on the sc_nmi ring buffer. Userspace cross-checks every snapshot against the chain it reconstructed from the transport ring:
| Notary observation | Meaning | Alert |
|---|---|---|
ev.seq ahead of the userspace chain |
kernel appended records that userspace never received — records suppressed in transit | [TAMPER] |
same ev.seq found in history, but ev.head differs |
the kernel chain and the reconstructed chain disagree — records edited in transit | [TAMPER] |
ev.seq stale beyond the 256-entry history window |
aged out legitimately under burst load — not a provable anomaly | ignored |
| no seq progress on any CPU, records stopped | writer programs detached, suppressed, or never attached (BTF bug) | registry stalls → [SILENT] |
Notary events also prove the NMI channel itself is alive: no notary for >5 s → [SILENT] on the NMI channel, which covers detachment of any link, including the notary's own.
| Attack | What happens | What userspace sees |
|---|---|---|
| Tracepoint detached from funcs array | writers stop, chains freeze | records stop → registry stalls → [SILENT]; notaries keep reporting a frozen seq |
| BTF bug / attach failure (silent) | writers never execute | chains never start; notaries report seq 0; [SILENT] after grace |
Records suppressed in ring buffer pages (/proc/pid/mem edit) |
userspace chain lags the kernel chain | notary ahead of chain → [TAMPER] |
| Records edited in ring buffer pages | userspace recomputed head diverges | notary head mismatch at same seq → [TAMPER] |
Chain state spoofed via BPF_MAP_UPDATE_ELEM on .bss |
EPERM — the map is frozen | nothing (attack impossible via syscall) |
bpf_override_return on a scheduler function |
if it prevents writers from firing | chains freeze → [SILENT]/[TAMPER] |
| Bytecode patched in-place | requires arbitrary kernel write (LKM-level — blocked by the module gate; raw ROP out of scope) | outside the eBPF threat model |
The notary is structurally immune to BPF override attacks because of a fundamental property: bpf_override_return intercepts function calls, but the notary doesn't
browse all types & interfaces →
$ claude mcp add SPiCa \
-- python -m otcore.mcp_server <graph>