Skip to content

Commit a3bfa88

Browse files
committed
extra: restore the hardening sysctls immediately on a trapped signal
A shell defers a trapped signal until the current foreground command finishes, and weaken-kernel-hardening held its relaxed state in a `sleep 86400` loop. A SIGTERM therefore left kptr_restrict, dmesg_restrict, perf_event_paranoid and unprivileged_bpf_disabled at 0 for up to a day, host-wide rather than for the testing process alone, and SIGHUP was not trapped at all, so closing the terminal never restored them. Run the sleep in the background with the shell blocked in `wait`, which the signal interrupts, and add HUP to the trap list. restore() disarms the traps before running, so the exit that follows a signal no longer restores a second time. sudo-proc-kallsyms takes the same trap fix for its shorter kptr_restrict window, and both scripts record that the sysctls they touch are system-wide. The README claimed the weakened state never outlives the process; it now names the signals that restore and states that SIGKILL cannot be trapped.
1 parent 1a325f4 commit a3bfa88

3 files changed

Lines changed: 28 additions & 7 deletions

File tree

extra/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,11 @@ and the same change reads as an improvement (`OK: no posture regression`).
6868

6969
`weaken-kernel-hardening` relaxes `kptr_restrict`, `dmesg_restrict`,
7070
`perf_event_paranoid`, and `unprivileged_bpf_disabled` — a **testing** convenience,
71-
never for production. It restores the saved values on `Ctrl+C`, `SIGTERM`, or normal
72-
exit, so the weakened state never outlives the process.
71+
never for production. The sysctls are system-wide, so every process on the host
72+
runs unrestricted while it holds them down. It restores the saved values on
73+
`Ctrl+C`, `SIGTERM`, `SIGHUP`, or normal exit. `SIGKILL` cannot be trapped: a hard
74+
kill leaves the sysctls relaxed until they are restored by hand or the host
75+
reboots.
7376

7477
`posture-diff` compares only the **boot-stable** posture — guaranteed entropy
7578
(virtual and physical), KASLR state, unpatched CVE-class leaks, and disabled

extra/sudo-proc-kallsyms

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
# the matching kallsyms lines go to stdout. Restoration runs from a trap, so an
1010
# interrupted or failing read still returns kptr_restrict to its original value.
1111
#
12+
# kptr_restrict is a system-wide setting: for the few reads below, every process
13+
# on the host sees unhashed kernel pointers, not just this one. A SIGKILL leaves
14+
# it at 0 until it is set back by hand or the machine reboots.
15+
#
1216
# Requires:
1317
# - sudo privileges (to set kptr_restrict and read unhashed kallsyms)
1418
# - CONFIG_KALLSYMS=y
@@ -23,6 +27,9 @@ lowered=0
2327
saved_kptr=$(sysctl -n kernel.kptr_restrict 2>/dev/null) || saved_kptr=""
2428

2529
restore() {
30+
# Disarm first: the EXIT trap would otherwise restore a second time when a
31+
# signal trap returns.
32+
trap - INT TERM HUP EXIT
2633
[ "$lowered" = "1" ] || return
2734
if sudo sysctl -q kernel.kptr_restrict="$saved_kptr" 2>/dev/null; then
2835
echo "[*] kernel.kptr_restrict: 0 -> $saved_kptr (restored)" >&2
@@ -31,7 +38,7 @@ restore() {
3138
fi
3239
}
3340

34-
trap restore INT TERM EXIT
41+
trap restore INT TERM HUP EXIT
3542

3643
if [ -n "$saved_kptr" ]; then
3744
if sudo sysctl -q kernel.kptr_restrict=0 2>/dev/null; then

extra/weaken-kernel-hardening

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
# This file is part of KASLD - https://github.com/bcoles/kasld
33
#
44
# Temporarily weaken kernel hardening settings for KASLD testing.
5-
# Saves current values and restores them on exit (or SIGINT/SIGTERM).
5+
# Saves current values and restores them on exit (or SIGINT/SIGTERM/SIGHUP).
6+
#
7+
# The settings are system-wide: every process on the host runs unrestricted for
8+
# as long as this script holds them down, not just KASLD.
69
#
710
# This script is intended for testing on systems you control.
811
# DO NOT run this on production systems.
@@ -55,6 +58,9 @@ saved_perf=$(sysctl -n kernel.perf_event_paranoid 2>/dev/null) || saved_perf=""
5558
saved_bpf=$(sysctl -n kernel.unprivileged_bpf_disabled 2>/dev/null) || saved_bpf=""
5659

5760
restore() {
61+
# Disarm first: the EXIT trap would otherwise fire again on the exit below and
62+
# restore a second time.
63+
trap - INT TERM HUP EXIT
5864
echo
5965
echo "[*] Restoring kernel settings ..."
6066
[ -n "$saved_dmesg" ] && sysctl -q kernel.dmesg_restrict="$saved_dmesg"
@@ -68,7 +74,7 @@ restore() {
6874
exit 0
6975
}
7076

71-
trap restore INT TERM EXIT
77+
trap restore INT TERM HUP EXIT
7278

7379
echo "[*] Current settings:"
7480
[ -n "$saved_dmesg" ] && echo " kernel.dmesg_restrict = $saved_dmesg"
@@ -103,7 +109,12 @@ echo "[*] Hardening weakened. Run KASLD in another terminal."
103109
echo "[*] Press Ctrl+C to restore settings and exit."
104110
echo
105111

106-
# Wait until interrupted
112+
# Wait until interrupted. The sleep runs in the background with the shell
113+
# blocked in `wait`, because a shell defers a trapped signal until the current
114+
# foreground command finishes: behind a day-long foreground sleep, a signal that
115+
# does not also reach the sleep would leave the settings weakened for the rest
116+
# of the day. `wait` is interrupted by the signal, so restore() runs at once.
107117
while true; do
108-
sleep 86400
118+
sleep 86400 &
119+
wait "$!" || true
109120
done

0 commit comments

Comments
 (0)