README.md

kprune #

Generate a minimal hardened kernel config for what a live system actually uses.

One static binary, three subcommands. Run collect once on the server to capture a manifest of everything the kernel is doing, run generate once where the kernel is built to produce a .config that covers only that, and run check to audit any config against the hardening rules.

It is a spiritual successor to undertaker/tailor (module pruning) and kernel-hardening-checker (config auditing). tailor (NDSS 2013) traces every executed kernel function with ftrace and SAT-solves the exact preconditions of the touched source locations; kprune uses the same ftrace execution evidence but is cruder on purpose. It prunes around what actually ran with a kconfiglib-style fixpoint and, in Kconfig mode, maps the executed built-in functions back to their CONFIG symbols through the source tree's Makefiles โ€” no SAT solver, no kernel rebuild, no C++ toolchain, one static binary. On top of the pruning, it applies hardening rules โ€” KASLR, hardened usercopy, init-on-alloc, CFI, and the rest โ€” as data shared between generate (enforce) and check (audit).

Why #

A generic distro kernel enables thousands of drivers for hardware you do not have, and a minimal config written by hand is how people ship kernels that do not boot. kprune observes the live system โ€” what actually executed (ftrace), syscalls in flight, kernel stack frames, loaded modules, mounted filesystems, crypto algorithms, network protocols โ€” and prunes around the evidence instead of guessing.

Hardening is applied on top: KASLR, hardened usercopy, init-on-alloc, CFI, and the rest, as data-driven rules shared between generate (enforce) and check (audit).

Quick start #

# build (docker, produces out/kprune, a static binary)
make

# on the live server, as root:
./out/kprune collect -o manifest.tar.gz -s 30
# ...or also capture ftrace execution evidence for the same window:
./out/kprune collect -o manifest.tar.gz -s 30 --trace 30

# where the kernel is built:
./out/kprune generate manifest.tar.gz --kernel-src /path/to/linux -o config.min
# or, without kernel sources (surgery mode):
./out/kprune generate manifest.tar.gz -o config.min

# audit any config or manifest:
./out/kprune check /boot/config-$(uname -r)
./out/kprune check config.min

generate prints the config to stdout and the report (JSON) to stderr by default; -o and --report redirect them. check exits 1 when a non-low rule fails.

The subcommands #

kprune collect #

Run as root on the live server. Samples /proc for a few seconds (default 30, -s), recording for every process the syscall in flight and the kernel function on top of its stack, then gathers the rest of the evidence:

  • loaded modules (/proc/modules)
  • filesystems (/proc/filesystems, /proc/mounts)
  • crypto algorithms (/proc/crypto)
  • network protocols (/proc/net/protocols)
  • LSMs, cgroups, hypervisor, EFI/ACPI/PCI/NUMA/USB/I2C boot facts
  • PCI and USB vendor:device IDs
  • the running config (/boot/config-<release>, else /proc/config.gz)

With --trace SECS it additionally runs the ftrace function tracer for that window and records every kernel function that executed. Each function is resolved, via kallsyms' [module] tag, to either a loadable module (trace_modules) or a built-in symbol (trace_builtins); anything it cannot resolve is reported as trace_unresolved. This is the strongest evidence kprune has โ€” not "this module is present" but "this code actually ran" โ€” and it is what generate uses to prune down to the executed surface. The tracer is restored to nop on exit, even if the trace read fails. Tracing is best-effort: on a kernel without ftrace, or without permission, the manifest is still produced without the trace fields. The evidence is bounded by the trace window: code that did not run in that window is not seen, so a longer window (or several over the system's daily cycle) is safer. Unresolved and unmapped symbols are always reported, never guessed.

The result is a tarball (or bare directory) with a manifest.json plus the raw files, so a pruned decision is always explainable from the evidence.

Collection is best-effort: on a locked-down box a missing /proc entry produces a warning, not a failure.

kprune generate #

Turns a manifest into a minimal hardened config, in two modes:

  • Kconfig mode (--kernel-src <linux tree>): parses the tree's Kconfig files (including source, if, menu, choice), seeds values from the manifest (architecture, modules, filesystems, crypto, protocols, boot facts), forces the hardening rules, and resolves dependencies with a kconfiglib-style fixpoint. The output is a real dependency-consistent config. When the manifest carries ftrace evidence, executed modules are seeded as =m and executed built-in functions are mapped back to their CONFIG symbols by scanning the tree's Kbuild Makefiles and C sources; those CONFIGs are forced on and the count of built-ins that could not be mapped is reported as traced_builtins_unmapped.
  • Surgery mode (no kernel sources): prunes the running config directly. Only =m symbols whose module name kprune can identify are disabled; everything else is kept and reported. Disabling a driver we cannot name is how you break a boot. Executed modules are protected from pruning: a module that ran is kept even if it was unloaded by the time collect finished.

Evidence precedence, highest first: a module that is loaded or was executed wins m; a filesystem/crypto/protocol that is in use is forced on only if not already m (execution beats presence); then Kconfig defaults. --whitelist and --blacklist override everything: user intent wins.

--no-hardening skips the hardening rules.

The JSON report lists rules applied/skipped/failed, dependency conflicts, every symbol added or removed relative to the running config, and โ€” when the manifest carries ftrace evidence โ€” the executed modules (traced_modules) and the number of executed built-ins that could not be mapped to a CONFIG symbol (traced_builtins_unmapped).

kprune check #

Audits a kernel .config (or a manifest, using its embedded running config) against the hardening rules, per architecture. Each rule is PASS/FAIL/SKIP/N-A with a severity; check exits 1 if any non-low rule fails.

The rules:

rule direction why
RANDOMIZE_BASE on KASLR
HARDENED_USERCOPY on bounds-check user copies
STACKPROTECTOR_STRONG on stack canaries
DEBUG_STRICT_USER_COPY_CHECKS on strict user-copy checks
INIT_ON_ALLOC_DEFAULT_ON on zero memory on alloc
INIT_ON_FREE_DEFAULT_ON on zero memory on free
INIT_STACKALL on initialize the whole stack
HARDENED_BRANCH_PREDICTOR on pad indirect branches
STRICT_KERNEL_RWX on strict rwx on kernel memory
CFI_CLANG, LTO on (clang) control flow integrity, LTO
SHADOW_CALL_STACK on (aarch64) shadow call stack
RANDOMIZE_MEMORY, LEGACY_VSYSCALL_NONE, X86_X32 off on/off (x86_64) layout randomization, vsyscall, x32
ARM64_BTI, ARM64_PAC, ARM64_MTE on (aarch64) pointer auth, PAC, MTE
USER_NS, BPF_SYSCALL off (low) unprivileged namespaces, BPF

Building #

The default make target builds in docker from src/Containerfile (a locked pallet-rust image) and extracts the static binary to out/kprune. Only src/ is copied into the build stage; the final image is FROM scratch containing nothing but the binary.

make            # docker build -> out/kprune
make test       # cargo test
make fmt        # cargo fmt
make clippy     # cargo clippy
make clean      # rm -rf out

The crate lives in src/ (manifest src/Cargo.toml, sources src/src/), because the build copies only src/ into the container.

Data, not magic #

The module-to-symbol, filesystem-to-symbol, crypto-to-symbol, and protocol-to-symbol tables are const data in src/src/map.rs. Syscall number tables for x86_64 and aarch64 are generated from the real uapi asm/unistd_64.h headers, not hand-written. Only modules with a known name are ever auto-pruned; unknowns are kept and reported.

The built-in mapping in Kconfig mode is heuristic and honest about it: it parses the tree's Kbuild Makefiles to learn which CONFIG guards each object, then scans the C sources for the definition of each executed function and takes the object's guards. A function that maps to nothing is reported as unmapped, not invented.

License #

MIT