Protecting the Kernel from Your Script
Since SystemTap loads a kernel module, a buggy script could theoretically trigger a kernel panic and crash the server. To prevent accidental disasters, SystemTap enforces several safety constraints by default:
- Loop bounds: Loops are limited in iteration count. An infinite loop in a probe handler would deadlock the kernel — SystemTap prevents this at compile time.
- Memory safety: You cannot dereference arbitrary pointers. Accesses to kernel and user memory go through checked helper functions.
- Read-only by default: You can read kernel variables, but you cannot modify them. This prevents probes from accidentally corrupting kernel state.
- Recursion prevention: Probe handlers cannot recursively trigger themselves.
Guru Mode (-g):
Passing the -g flag when invoking stap disables these safety constraints.
Guru mode unlocks:
- Variable modification: You can change the value of a kernel variable mid-execution. For example, you could intercept
sys_openand redirect the filename to a different path — a technique used in some sandboxing and fault injection tools. - Raw C embedding: You can embed raw C code blocks directly in your script using
%{ ... %}syntax, giving you full kernel programming power. - Privilege escalation research: Modifying a
uidfield to 0 inside a running kernel function is a classic privilege escalation technique. Guru mode makes this trivially scriptable — which is exactly why it requires root and why you should never run untrusted.stpscripts with-g.
The production rule: Never run guru mode scripts on production systems unless you have tested them exhaustively in a staging environment with an identical kernel version. A single bad memory write in a probe handler can corrupt kernel data structures and cause a delayed, hard-to-diagnose panic.
A common SRE interview question is: "What's the difference between SystemTap and eBPF from a safety perspective?" The key answer: eBPF uses a kernel-side verifier that statically proves safety before loading — no loops that don't terminate, no invalid memory access, bounded execution time. SystemTap relies on compile-time checks and runtime guards that can be bypassed with -g. eBPF's safety model is fundamentally stronger because it's enforced by the kernel itself, not by the userspace compiler.