2 # SPDX-License-Identifier: GPL-2.0
4 # Measure kernel stack entropy by sampling via LKDTM's REPORT_STACK test.
8 # Capture dmesg continuously since it may fill up depending on sample size.
9 log=$(mktemp -t stack-entropy-XXXXXX)
10 dmesg --follow >"$log" & pid=$!
12 for i in $(seq 1 $samples); do
13 echo "REPORT_STACK" >/sys/kernel/debug/provoke-crash/DIRECT
15 percent=$(( 100 * $i / $samples ))
16 if [ "$percent" -ne "$report" ]; then
17 /bin/echo -en "$percent%\r"
24 # Count unique offsets since last run.
25 seen=$(tac "$log" | grep -m1 -B"$samples"0 'Starting stack offset' | \
26 grep 'Stack offset' | awk '{print $NF}' | sort | uniq -c | wc -l)
27 bits=$(echo "obase=2; $seen" | bc | wc -L)
28 echo "Bits of stack entropy: $bits"
31 # We would expect any functional stack randomization to be at least 5 bits.
32 if [ "$bits" -lt 5 ]; then