-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lkdtm: Add REPORT_STACK for checking stack offsets
For validating the stack offset behavior, report the offset from a given process's first seen stack address. Add s script to calculate the results to the LKDTM kselftests. Signed-off-by: Kees Cook <keescook@chromium.org> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Link: https://lore.kernel.org/r/20210401232347.2791257-7-keescook@chromium.org
- Loading branch information
Kees Cook
authored and
Thomas Gleixner
committed
Apr 8, 2021
1 parent
fe950f6
commit 68ef873
Showing
6 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
*.sh | ||
!run.sh | ||
!stack-entropy.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/bin/sh | ||
# SPDX-License-Identifier: GPL-2.0 | ||
# | ||
# Measure kernel stack entropy by sampling via LKDTM's REPORT_STACK test. | ||
set -e | ||
samples="${1:-1000}" | ||
|
||
# Capture dmesg continuously since it may fill up depending on sample size. | ||
log=$(mktemp -t stack-entropy-XXXXXX) | ||
dmesg --follow >"$log" & pid=$! | ||
report=-1 | ||
for i in $(seq 1 $samples); do | ||
echo "REPORT_STACK" >/sys/kernel/debug/provoke-crash/DIRECT | ||
if [ -t 1 ]; then | ||
percent=$(( 100 * $i / $samples )) | ||
if [ "$percent" -ne "$report" ]; then | ||
/bin/echo -en "$percent%\r" | ||
report="$percent" | ||
fi | ||
fi | ||
done | ||
kill "$pid" | ||
|
||
# Count unique offsets since last run. | ||
seen=$(tac "$log" | grep -m1 -B"$samples"0 'Starting stack offset' | \ | ||
grep 'Stack offset' | awk '{print $NF}' | sort | uniq -c | wc -l) | ||
bits=$(echo "obase=2; $seen" | bc | wc -L) | ||
echo "Bits of stack entropy: $bits" | ||
rm -f "$log" | ||
|
||
# We would expect any functional stack randomization to be at least 5 bits. | ||
if [ "$bits" -lt 5 ]; then | ||
exit 1 | ||
else | ||
exit 0 | ||
fi |