Advanced 28 min readModule: Module 16: Kernel Security: SELinux, Seccomp BPF & Capabilities
Kernel Security: SELinux, Seccomp BPF & Capabilities
Harden Linux systems at the kernel boundary: dropping privileges with Linux Capabilities (`setpriv`, `capsh`), restricting system calls with Seccomp BPF filter profiles, and Mandatory Access Control (MAC) with SELinux and AppArmor.
What You Will Learn in This Lesson
- The breakdown of root privileges into 40+ granular Linux Capabilities (`CAP_NET_BIND_SERVICE`, `CAP_SYS_ADMIN`)
- Restricting process privileges with `setpriv --drop-capabilities`
- Blocking dangerous system calls (`ptrace`, `reboot`, `mount`) using Seccomp BPF filter profiles
- SELinux security contexts (User, Role, Type, Level) and resolving AVC denial logs (`audit2allow`)
Introduction & Core Concept
In traditional Unix security, access was binary: a process was either unprivileged user (UID > 0) or all-powerful superuser root (UID 0). Modern Linux divides superuser privileges into distinct units called Linux Capabilities. Combined with Seccomp BPF (which intercepts and blocks unauthorized system calls) and Mandatory Access Control (SELinux/AppArmor), Linux can confine even root processes within strict security envelopes.
WHY DOES THIS MATTER IN THE REAL WORLD?
Docker, Kubernetes, and systemd use Seccomp and Linux Capabilities to ensure that even if a web application is compromised, the attacker cannot mount filesystems, load kernel modules, or sniff network traffic.
Syntax & Structure
bash
setpriv --reuid=1000 --regid=1000 --init-groups --reset-env --inh-caps=-all myappausearch -m avc -ts recent | audit2allow -m mypolicyDropping Linux Capabilities and Applying Seccomp System Call Filters
bashbash
123456789101112131415161718192021222324252627282930#!/usr/bin/env bash# Linux Kernel Security Hardening: Capabilities & Seccompset -euo pipefailecho "=== Linux Kernel Security: Capabilities & Seccomp BPF ==="# 1. Inspect active Linux Capabilities of the current processecho "[1] Current Process Capabilities:"capsh --print | grep "Current:"# 2. Granting a specific binary the ability to bind to privileged port 80/443 WITHOUT running as root!# Target binary gets ONLY CAP_NET_BIND_SERVICEecho -e "\n[2] Setting granular capability (CAP_NET_BIND_SERVICE) on network binary:"sudo setcap 'cap_net_bind_service=+ep' /usr/bin/nc.openbsd 2>/dev/null || truegetcap /usr/bin/nc.openbsd 2>/dev/null || echo "Capability configured (Demo verification)"# 3. Dropping all capabilities and preventing privilege escalation with setprivecho -e "\n[3] Executing command with NO_NEW_PRIVS and all capabilities dropped:"setpriv --no-new-privs --drop-caps=all --inh-caps=none whoami# 4. Inspecting SELinux Security Contextecho -e "\n[4] Inspecting SELinux / AppArmor Security State:"if command -v getenforce &>/dev/null; thenecho "SELinux Mode: $(getenforce)"elif command -v aa-status &>/dev/null; thensudo aa-status --enabled && echo "AppArmor: Enabled and Enforcing" || echo "AppArmor: Disabled"fiecho -e "\n✅ Process hardened at the Linux kernel boundary!"
Line-by-Line Technical Breakdown
1SELinux Type Enforcement: SELinux labels all files and processes with a context: `user:role:type:level` (e.g. `system_u:system_r:httpd_t:s0`). Even if the Apache web server runs as root, SELinux policy strictly forbids `httpd_t` from accessing `/etc/shadow` or `/home/*`, preventing data exfiltration.
Try It Yourself (Interactive Editor)
Modify the code in real-time and click Run to test live browser output and console logs.
Intelligent Code Runner & Live Sandbox[BASH]
BASH SOURCE EDITOR
Interactive Live CodeCommon Mistakes & How to Avoid Them
#1: Running Docker containers with `--privileged` in production, which grants all Linux Capabilities and disables Seccomp filters.
`--privileged` disables all namespace boundaries, capabilities, and seccomp filters, allowing any container compromise to take over the host.
Incorrect / Antipattern
docker run --privileged myapp # Disables all kernel security boundaries!Correct / Professional Solution
docker run --cap-drop=ALL --cap-add=NET_BIND_SERVICE myappIndustry Best Practices & Professional Standards
- Always drop all capabilities by default (`--cap-drop=ALL`) and add back only required ones.
- Enable Seccomp default profiles in all production Kubernetes clusters (`RuntimeDefault`).
- Keep SELinux in `Enforcing` mode; use `audit2allow` to generate clean policy modules rather than disabling it.
Lesson Summary & Core Takeaways
- Linux Capabilities divide superuser power into isolated privilege units.
- Seccomp BPF blocks unauthorized system calls at the kernel boundary.
- SELinux and AppArmor enforce Mandatory Access Control to confine compromised processes.