kernel-security
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseKernel Security
Linux 内核安全
Purpose
目的
Guide agents through Linux kernel security: LSM frameworks (SELinux, AppArmor), seccomp-bpf with libseccomp, KASLR and bypass mitigations, Intel CET (Shadow Stack + IBT), ARM PAC and BTI, kernel sanitizers (KASAN, KMSAN), and CVE triage for kernel vulnerabilities.
指导 Agent 掌握 Linux 内核安全:LSM 框架(SELinux、AppArmor)、使用 libseccomp 实现 seccomp-bpf、KASLR 及其绕过缓解措施、Intel CET(Shadow Stack + IBT)、ARM PAC 与 BTI、内核 sanitizer(KASAN、KMSAN),以及内核漏洞的 CVE 研判。
When to Use
适用场景
- Writing SELinux or AppArmor policies for confined services
- Sandboxing processes with seccomp-bpf filters
- Hardening binaries with CET, PAC, or BTI
- Enabling KASAN on kernel builds for vulnerability research
- Triaging kernel CVE impact on your distro/kernel version
- Designing container or microservice security boundaries
- 为受限服务编写 SELinux 或 AppArmor 策略
- 使用 seccomp-bpf 过滤器对进程进行沙箱隔离
- 使用 CET、PAC 或 BTI 对二进制文件进行加固
- 在内核构建中启用 KASAN 以开展漏洞研究
- 研判内核 CVE 对当前发行版或内核版本的影响
- 设计容器或微服务的安全边界
Workflow
工作流
1. LSM framework overview
1. LSM 框架概览
Application syscall
→ DAC (uid/gid, file mode)
→ LSM hook (SELinux/AppArmor/Yama/...)
→ Capability check
→ seccomp filter
→ Kernelbash
undefinedApplication syscall
→ DAC (uid/gid, file mode)
→ LSM hook (SELinux/AppArmor/Yama/...)
→ Capability check
→ seccomp filter
→ Kernelbash
undefinedActive LSM
Active LSM
cat /sys/kernel/security/lsm
cat /sys/kernel/security/lsm
common: lockdown,capability,yama,apparmor,safesetid
common: lockdown,capability,yama,apparmor,safesetid
undefinedundefined2. SELinux policy
2. SELinux 策略
bash
undefinedbash
undefinedCheck SELinux status
Check SELinux status
getenforce
sestatus
getenforce
sestatus
Context of file/process
Context of file/process
ls -Z /usr/sbin/nginx
ps -eZ | grep nginx
ls -Z /usr/sbin/nginx
ps -eZ | grep nginx
Audit denials
Audit denials
ausearch -m avc -ts recent
sealert -a /var/log/audit/audit.log
Policy module example:
```teausearch -m avc -ts recent
sealert -a /var/log/audit/audit.log
策略模块示例:
```temyapp.te
myapp.te
policy_module(myapp, 1.0.0)
type myapp_t;
type myapp_exec_t;
type myapp_log_t;
init_daemon_domain(myapp_t, myapp_exec_t)
allow myapp_t myapp_log_t:file { create write append open };
allow myapp_t self:tcp_socket { create bind listen accept };
```bash
checkmodule -M -m -o myapp.mod myapp.te
semodule_package -o myapp.pp -m myapp.mod
semodule -i myapp.pppolicy_module(myapp, 1.0.0)
type myapp_t;
type myapp_exec_t;
type myapp_log_t;
init_daemon_domain(myapp_t, myapp_exec_t)
allow myapp_t myapp_log_t:file { create write append open };
allow myapp_t self:tcp_socket { create bind listen accept };
```bash
checkmodule -M -m -o myapp.mod myapp.te
semodule_package -o myapp.pp -m myapp.mod
semodule -i myapp.pp3. AppArmor profiles
3. AppArmor profiles
bash
undefinedbash
undefinedGenerate complain-mode profile
Generate complain-mode profile
aa-genprof /usr/bin/myapp
aa-genprof /usr/bin/myapp
Enforce
Enforce
aa-enforce /etc/apparmor.d/usr.bin.myapp
aa-enforce /etc/apparmor.d/usr.bin.myapp
Check status
Check status
aa-status
```apparmoraa-status
```apparmor/etc/apparmor.d/usr.bin.myapp
/etc/apparmor.d/usr.bin.myapp
#include <tunables/global>
/usr/bin/myapp {
#include <abstractions/base>
/usr/bin/myapp mr,
/var/log/myapp.log w,
/etc/myapp/config r,
network bind tcp,
deny /etc/shadow r,
}
undefined#include <tunables/global>
/usr/bin/myapp {
#include <abstractions/base>
/usr/bin/myapp mr,
/var/log/myapp.log w,
/etc/myapp/config r,
network bind tcp,
deny /etc/shadow r,
}
undefined4. seccomp-bpf with libseccomp
4. 使用 libseccomp 实现 seccomp-bpf
c
#include <seccomp.h>
int sandbox(void) {
scmp_filter_ctx ctx = seccomp_init(SCMP_ACT_KILL_PROCESS);
// Allow read/write/exit/mmap
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(read), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(exit_group), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(brk), 0);
// Return EPERM instead of kill for open
seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EPERM), SCMP_SYS(open), 0);
seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EPERM), SCMP_SYS(openat), 0);
return seccomp_load(ctx);
}bash
undefinedc
#include <seccomp.h>
int sandbox(void) {
scmp_filter_ctx ctx = seccomp_init(SCMP_ACT_KILL_PROCESS);
// Allow read/write/exit/mmap
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(read), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(exit_group), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(mmap), 0);
seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(brk), 0);
// Return EPERM instead of kill for open
seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EPERM), SCMP_SYS(open), 0);
seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EPERM), SCMP_SYS(openat), 0);
return seccomp_load(ctx);
}bash
undefinedExport BPF filter for audit
Export BPF filter for audit
scmp_filter_ctx ctx = ...;
seccomp_export_bpf(ctx, fd);
scmp_filter_ctx ctx = ...;
seccomp_export_bpf(ctx, fd);
strace to find needed syscalls before tightening
strace to find needed syscalls before tightening
strace -c ./myapp
undefinedstrace -c ./myapp
undefined5. KASLR
5. KASLR
bash
undefinedbash
undefinedCheck KASLR enabled
Check KASLR enabled
cat /proc/sys/kernel/randomize_va_space # 2 = full
dmesg | grep KASLR
cat /proc/sys/kernel/randomize_va_space # 2 = full
dmesg | grep KASLR
Kernel cmdline
Kernel cmdline
grep kaslr /proc/cmdline
Mitigations against KASLR leaks:
- No `/proc/<pid>/maps` to untrusted
- Pointer hashing in `%pK` printk
- eBPF restricted on unprivilegedgrep kaslr /proc/cmdline
防止 KASLR 信息泄露的缓解措施:
- 不向不受信任方开放 `/proc/<pid>/maps`
- 在 `%pK` printk 中进行指针哈希处理
- 限制非特权用户使用 eBPF6. Intel CET
6. Intel CET
bash
undefinedbash
undefinedCompile with CET
Compile with CET
gcc -fcf-protection=full -o app app.c
gcc -fcf-protection=full -o app app.c
Verify shadow stack and IBT
Verify shadow stack and IBT
readelf -n app | grep -E 'SHSTK|IBT'
readelf --notes app | grep -A2 GNU_PROPERTY
| Feature | Protects against |
|---------|------------------|
| SHSTK (Shadow Stack) | ROP return address overwrites |
| IBT (Indirect Branch Tracking) | CALL/JMP to non-ENDBR targets |
Requires CPU with CET (Intel Tiger Lake+; AMD Zen 3+ on CPUs with shadow-stack support) and kernel CET support.readelf -n app | grep -E 'SHSTK|IBT'
readelf --notes app | grep -A2 GNU_PROPERTY
| 功能 | 防护目标 |
|---------|------------------|
| SHSTK(Shadow Stack) | 防止 ROP 返回地址被覆写 |
| IBT(Indirect Branch Tracking) | 防止 CALL/JMP 跳转到非 ENDBR 目标 |
需要支持 CET 的 CPU(Intel Tiger Lake+;支持 Shadow Stack 的 AMD Zen 3+ CPU)以及内核对 CET 的支持。7. ARM PAC and BTI
7. ARM PAC 与 BTI
bash
undefinedbash
undefinedGCC/Clang branch protection
GCC/Clang branch protection
gcc -mbranch-protection=standard -o app app.c
gcc -mbranch-protection=standard -o app app.c
PAC (pointer authentication) + BTI (branch target identification)
PAC (pointer authentication) + BTI (branch target identification)
Verify
Verify
readelf -n app | grep -E 'GNU_PROPERTY_AARCH64_FEATURE_1'
llvm-objdump -d app | grep bti
PAC signs return addresses and pointers with cryptographic keys (ARMv8.3+).
BTI marks valid branch targets — invalid jumps fault.readelf -n app | grep -E 'GNU_PROPERTY_AARCH64_FEATURE_1'
llvm-objdump -d app | grep bti
PAC 使用加密密钥对返回地址和指针进行签名(ARMv8.3+)。
BTI 标记有效的分支目标——跳转至无效目标时将触发异常。8. Kernel memory tagging
8. 内核内存标记
bash
undefinedbash
undefinedKASAN kernel build
KASAN kernel build
CONFIG_KASAN=y in kernel .config
CONFIG_KASAN=y in kernel .config
make menuconfig # Kernel hacking → KASAN
make menuconfig # Kernel hacking → KASAN
Boot with KASAN kernel
Boot with KASAN kernel
Reports use-after-free, OOB with stack trace
Reports use-after-free, OOB with stack trace
KMSAN (uninitialized memory)
KMSAN (uninitialized memory)
CONFIG_KMSAN=y — kernel equivalent of MSan
CONFIG_KMSAN=y — kernel equivalent of MSan
```bash
```bashKASAN report example fields
KASAN report example fields
BUG: KASAN: slab-out-of-bounds in ...
BUG: KASAN: slab-out-of-bounds in ...
Call trace: ...
Call trace: ...
undefinedundefined9. CVE triage workflow
9. CVE 研判工作流
bash
undefinedbash
undefinedCheck kernel version
Check kernel version
uname -r
uname -r
Distro security tracker
Distro security tracker
Ubuntu: ubuntu-security-notices
Ubuntu: ubuntu-security-notices
RHEL: errata
RHEL: errata
NVD lookup
NVD lookup
Is patch backported?
Is patch backported?
zgrep -l CVE-2024-XXXX /usr/share/doc/linux-*/changelog.Debian.gz
Triage checklist:
1. Affected subsystem (net, fs, drivers)?
2. Local or remote exploit?
3. Fixed in your kernel version?
4. Mitigation without patch (disable module, sysctl)?zgrep -l CVE-2024-XXXX /usr/share/doc/linux-*/changelog.Debian.gz
研判清单:
1. 受影响的子系统(net、fs、drivers)?
2. 本地漏洞利用还是远程漏洞利用?
3. 是否已在所使用的内核版本中修复?
4. 是否存在无需补丁的缓解措施(禁用模块、sysctl)?Common Problems
常见问题
| Symptom | Cause | Fix |
|---|---|---|
| SELinux denials | Missing allow rule | |
| AppArmor profile break | Path mismatch | Update profile paths; use globs |
| seccomp kills app | Missing syscall | |
| CET not active | Old CPU/kernel | Check |
| KASAN kernel slow | 2-5x overhead | Use only in test VMs |
| False sense of security | LSM bypass via kernel bug | Defense in depth; keep kernel updated |
| 症状 | 原因 | 修复方法 |
|---|---|---|
| SELinux 拒绝 | 缺少 allow 规则 | 使用 |
| AppArmor profile 失效 | 路径不匹配 | 更新 profile 路径;使用 glob |
| seccomp 终止应用 | 缺少 syscall | 使用 |
| CET 未生效 | CPU 或内核过旧 | 检查 |
| KASAN 内核运行缓慢 | 2–5 倍开销 | 仅在测试 VM 中使用 |
| 产生虚假的安全感 | 通过内核 bug 绕过 LSM | 纵深防御;及时更新内核 |
Related Skills
相关 Skills
- — container seccomp and caps
skills/virtualization/containers-internals - — userspace CET, RELRO, PIE
skills/runtimes/binary-hardening - — ASan/HWASan userspace counterparts
skills/runtimes/sanitizers - — LSM BPF programs
skills/observability/ebpf - — analyze KASAN reports
skills/kernel/kernel-debugging - — exploit analysis
skills/security/reverse-engineering
- ——容器 seccomp 与 caps
skills/virtualization/containers-internals - ——用户空间 CET、RELRO、PIE
skills/runtimes/binary-hardening - ——对应的用户空间 ASan/HWASan 工具
skills/runtimes/sanitizers - ——LSM BPF 程序
skills/observability/ebpf - ——分析 KASAN 报告
skills/kernel/kernel-debugging - ——漏洞利用分析
skills/security/reverse-engineering