Self-Hosted eBPF-Based Kernel Live Patching at Fleet Scale: Quick Fix Cheat Sheet
Quick Summary (TL;DR)
- Goal: Ship CVE hotfixes to thousands of kernels without reboots using kpatch/livepatch, validated by eBPF.
- Fast path: Build module → sign it → verify consistency with
kpatch list+ bpftrace → roll out via Ansible/Salt → watch Prometheus.- Golden rule: Never roll out a patch you can’t roll back in under 60 seconds. Test unload in staging first.
- Watch for: kernel taint, version drift, busy-state unload failures, and unsigned-module rejections.
- 30-second health check:
kpatch list+/sys/kernel/livepatch/*/enabled+ one bpftrace probe.
This cheat sheet is for SREs and platform engineers running self-hosted live patching across large Linux fleets. It assumes you already have a kernel with CONFIG_LIVEPATCH=y and want rapid troubleshooting, fast fixes, and copy-paste configs — not theory.
1. Prerequisites & Quick Environment Check
Run this on any suspect host before touching anything else:
# Kernel livepatching + kpatch present?
grep CONFIG_LIVEPATCH /boot/config-$(uname -r)
which kpatch kpatch-build bpftrace
# Taint state (live patches set a taint flag)
cat /proc/sys/kernel/tainted
# Are any patches currently loaded?
kpatch list
Expected results:
| Check | Healthy | Broken → Fix |
|---|---|---|
CONFIG_LIVEPATCH | =y | Rebuild kernel or use distro kernel |
kpatch in PATH | found | dnf install kpatch kpatch-dnf / apt install kpatch |
tainted | 0 or 1 (proprietary) | Odd taint = investigate before patching |
kpatch list | lists modules | Empty = nothing loaded (may be fine) |
2. Build Pipeline: From CVE to Livepatch Module (Fast Track)
The pipeline below is the minimum viable flow. Cache the base source tree — it’s the biggest time sink.
flowchart TD
A[CVE published] --> B[Fetch patched source + base vmlinux]
B --> C[kpatch-build --sourcedir . patch.patch]
C --> D{Module built?}
D -- No --> E[Check symbol mismatch / .config drift]
E --> C
D -- Yes --> F[Sign module with MOK key]
F --> G[Publish to internal repo]
G --> H[Rollout via config mgmt]
Fast build snippet:
# Build against the exact running kernel
kpatch-build -t vmlinux -s /usr/src/kernels/$(uname -r) \
CVE-2024-XXXX.patch -o cve-2024-xxxx.ko
# Sign (required if Secure Boot / module sig enforcement is on)
/usr/src/kernels/$(uname -r)/scripts/sign-file sha256 \
MOK.key MOK.crt cve-2024-xxxx.ko
Instant fixes for build failures:
symbol not found→ source tree doesn’t match running kernel. Use-spointing at the exactuname -rtree.CONFIG mismatch→ copy/boot/config-$(uname -r)into the source tree andmake olddefconfig.no changed functions→ patch already applied; checkgit apply --checkfirst.
3. Patch Consistency Verification: The 30-Second Audit
Consistency = every host has the same patch set enabled. Audit with three commands:
# 1. What's loaded?
kpatch list
# 2. Is each enabled?
for p in /sys/kernel/livepatch/*/; do
echo "$(basename $p): $(cat $p/enabled)"
done
# 3. Prove the code path is patched (eBPF runtime probe)
bpftrace -e 'kprobe:__x64_sys_<patched_syscall> { @hits = count(); }'
Fleet drift check (Ansible one-liner):
- name: Assert expected patch present
ansible.builtin.shell: kpatch list | grep -q '{{ expected_patch }}'
changed_when: false
register: patch_state
failed_when: patch_state.rc != 0
If a host fails, it’s drifted — reapply from the manifest, don’t debug in place.
4. Rollback Safety: The 3-Step Emergency Brake
flowchart LR
A[Alert: patch misbehaving] --> B[Disable: echo 0 > enabled]
B --> C{Unloaded cleanly?}
C -- Yes --> D[Verify original function restored]
C -- No --> E[Busy state → schedule reboot]
D --> F[Remove from manifest]
E --> F
Step 1 — Disable immediately (non-destructive):
echo 0 > /sys/kernel/livepatch/<patchname>/enabled
# or
kpatch unload <patchname>
Step 2 — Verify restoration:
kpatch list # patch should be gone
bpftrace -e 'kprobe:__x64_sys_<syscall> { @hits = count(); }' # confirm original path
Step 3 — Prevent re-apply: remove the patch from your config-management manifest/pillar before the next run, or the fleet will re-load it within minutes.
⚠️ Busy-state unload failure means a task is sitting in the patched function. You cannot force it — schedule the reboot window and let it drain.
5. Fleet Rollout: Config Management & Observability Snippets
Ansible (idempotent load):
- name: Install livepatch package
ansible.builtin.package:
name: "kpatch-patch-{{ cve_id }}"
state: present
- name: Load patch if not loaded
ansible.builtin.command: kpatch load /lib/livepatch/{{ cve_id }}.ko
args:
creates: /sys/kernel/livepatch/{{ cve_id }}
Prometheus textfile collector (per host):
cat > /var/lib/node_exporter/livepatch.prom <<EOF
# HELP livepatch_loaded Patch loaded (1=yes)
livepatch_loaded{name="cve-2024-xxxx"} $(test -d /sys/kernel/livepatch/cve-2024-xxxx && echo 1 || echo 0)
EOF
Alert on: livepatch_loaded == 0 for any host where the manifest expects 1, plus any tainted flag change.
6. Common Failure Modes & Instant Fixes
| Symptom | Cause | Instant Fix |
|---|---|---|
kpatch load → “module verification failed” | Unsigned module + Secure Boot | Re-sign with MOK key; enroll cert |
| Patch loads but no effect | Wrong kernel version / stale .ko | Rebuild against exact uname -r |
Unload hangs / EBUSY | Task in patched function | Drain + reboot in window |
| Fleet drift after hours | Manifest out of sync | Re-run Ansible with --check first |
tainted jumps unexpectedly | Third-party module | Isolate; don’t patch until clean |
| bpftrace probe = 0 hits | Wrong symbol name | bpftrace -l 'kprobe:*<name>*' to find it |
7. Cheat Sheet: Essential Commands & Paths
kpatch list # loaded patches
kpatch load <file.ko> # load
kpatch unload <name> # unload
cat /sys/kernel/livepatch/*/enabled # per-patch enable state
cat /proc/sys/kernel/tainted # taint flags
bpftrace -l 'kprobe:*<symbol>*' # find probe symbols
| Path | Purpose |
|---|---|
/sys/kernel/livepatch/ | Livepatch control interface |
/lib/livepatch/ | Installed patch modules |
/boot/config-$(uname -r) | Kernel config (build input) |
/var/lib/node_exporter/ | Prometheus textfile metrics |
FAQ
Q: kpatch vs. livepatch — which for fleet scale?
kpatch is the tooling/infrastructure (module generation, CLI, packaging); livepatch is the upstream kernel feature (CONFIG_LIVEPATCH) providing the consistency model. Use kpatch for automation-friendly fleet delivery — it builds on livepatch. eBPF (bpftrace) complements both as a runtime validation layer.
Q: How do I verify a patch is consistently applied fleet-wide without rebooting?
Combine kpatch list, cat /sys/kernel/livepatch/*/enabled, and a bpftrace probe on the patched symbol. Push a check script via Ansible/Salt that diffs output against an expected manifest and reports drift; emit the result as a Prometheus metric.
Q: Safest rollback steps?
kpatch unload <name>(orecho 0 > enabled), 2) verify original function restored viakpatch list+ bpftrace, 3) remove from the manifest so it isn’t re-applied. If unload fails on busy state, schedule a reboot and block re-apply. Always rehearse in staging.
Q: How do I integrate with Ansible/Salt?
Wrap download → package install → kpatch load → kpatch list verify in an idempotent module. Use creates:/unless: guards to avoid reloading. Store CVE ID and target kernel version in inventory/pillar and enforce via group vars.
Q: Which metrics matter? Patch load status per host, patch version + CVE ID, kernel taint flags, patched-function latency/error rates (eBPF), rollback events, and expected-vs-actual drift. Alert on any host missing a critical patch or showing inconsistent state.