Skip to content

Commit 46d1a0f

Browse files
keesshuahkh
authored andcommitted
selftests/lkdtm: Add tests for LKDTM targets
This adds a basic framework for running all the "safe" LKDTM tests. This will allow easy introspection into any selftest logs to examine the results of most LKDTM tests. Signed-off-by: Kees Cook <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent 192c197 commit 46d1a0f

File tree

6 files changed

+178
-0
lines changed

6 files changed

+178
-0
lines changed

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9581,6 +9581,7 @@ LINUX KERNEL DUMP TEST MODULE (LKDTM)
95819581
M: Kees Cook <[email protected]>
95829582
S: Maintained
95839583
F: drivers/misc/lkdtm/*
9584+
F: tools/testing/selftests/lkdtm/*
95849585

95859586
LINUX KERNEL MEMORY CONSISTENCY MODEL (LKMM)
95869587
M: Alan Stern <[email protected]>

tools/testing/selftests/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ TARGETS += kexec
2626
TARGETS += kvm
2727
TARGETS += lib
2828
TARGETS += livepatch
29+
TARGETS += lkdtm
2930
TARGETS += membarrier
3031
TARGETS += memfd
3132
TARGETS += memory-hotplug
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
# Makefile for LKDTM regression tests
3+
4+
include ../lib.mk
5+
6+
# NOTE: $(OUTPUT) won't get default value if used before lib.mk
7+
TEST_FILES := tests.txt
8+
TEST_GEN_PROGS = $(patsubst %,$(OUTPUT)/%.sh,$(shell awk '{print $$1}' tests.txt | sed -e 's/\#//'))
9+
all: $(TEST_GEN_PROGS)
10+
11+
$(OUTPUT)/%: run.sh tests.txt
12+
install -m 0744 run.sh $@

tools/testing/selftests/lkdtm/config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_LKDTM=y

tools/testing/selftests/lkdtm/run.sh

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/bin/sh
2+
# SPDX-License-Identifier: GPL-2.0
3+
#
4+
# This reads tests.txt for the list of LKDTM tests to invoke. Any marked
5+
# with a leading "#" are skipped. The rest of the line after the
6+
# test name is either the text to look for in dmesg for a "success",
7+
# or the rationale for why a test is marked to be skipped.
8+
#
9+
set -e
10+
TRIGGER=/sys/kernel/debug/provoke-crash/DIRECT
11+
KSELFTEST_SKIP_TEST=4
12+
13+
# Verify we have LKDTM available in the kernel.
14+
if [ ! -r $TRIGGER ] ; then
15+
/sbin/modprobe -q lkdtm || true
16+
if [ ! -r $TRIGGER ] ; then
17+
echo "Cannot find $TRIGGER (missing CONFIG_LKDTM?)"
18+
else
19+
echo "Cannot write $TRIGGER (need to run as root?)"
20+
fi
21+
# Skip this test
22+
exit $KSELFTEST_SKIP_TEST
23+
fi
24+
25+
# Figure out which test to run from our script name.
26+
test=$(basename $0 .sh)
27+
# Look up details about the test from master list of LKDTM tests.
28+
line=$(egrep '^#?'"$test"'\b' tests.txt)
29+
if [ -z "$line" ]; then
30+
echo "Skipped: missing test '$test' in tests.txt"
31+
exit $KSELFTEST_SKIP_TEST
32+
fi
33+
# Check that the test is known to LKDTM.
34+
if ! egrep -q '^'"$test"'$' "$TRIGGER" ; then
35+
echo "Skipped: test '$test' missing in $TRIGGER!"
36+
exit $KSELFTEST_SKIP_TEST
37+
fi
38+
39+
# Extract notes/expected output from test list.
40+
test=$(echo "$line" | cut -d" " -f1)
41+
if echo "$line" | grep -q ' ' ; then
42+
expect=$(echo "$line" | cut -d" " -f2-)
43+
else
44+
expect=""
45+
fi
46+
47+
# If the test is commented out, report a skip
48+
if echo "$test" | grep -q '^#' ; then
49+
test=$(echo "$test" | cut -c2-)
50+
if [ -z "$expect" ]; then
51+
expect="crashes entire system"
52+
fi
53+
echo "Skipping $test: $expect"
54+
exit $KSELFTEST_SKIP_TEST
55+
fi
56+
57+
# If no expected output given, assume an Oops with back trace is success.
58+
if [ -z "$expect" ]; then
59+
expect="call trace:"
60+
fi
61+
62+
# Clear out dmesg for output reporting
63+
dmesg -c >/dev/null
64+
65+
# Prepare log for report checking
66+
LOG=$(mktemp --tmpdir -t lkdtm-XXXXXX)
67+
cleanup() {
68+
rm -f "$LOG"
69+
}
70+
trap cleanup EXIT
71+
72+
# Most shells yell about signals and we're expecting the "cat" process
73+
# to usually be killed by the kernel. So we have to run it in a sub-shell
74+
# and silence errors.
75+
($SHELL -c 'cat <(echo '"$test"') >'"$TRIGGER" 2>/dev/null) || true
76+
77+
# Record and dump the results
78+
dmesg -c >"$LOG"
79+
cat "$LOG"
80+
# Check for expected output
81+
if egrep -qi "$expect" "$LOG" ; then
82+
echo "$test: saw '$expect': ok"
83+
exit 0
84+
else
85+
if egrep -qi XFAIL: "$LOG" ; then
86+
echo "$test: saw 'XFAIL': [SKIP]"
87+
exit $KSELFTEST_SKIP_TEST
88+
else
89+
echo "$test: missing '$expect': [FAIL]"
90+
exit 1
91+
fi
92+
fi
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#PANIC
2+
BUG kernel BUG at
3+
WARNING WARNING:
4+
WARNING_MESSAGE message trigger
5+
EXCEPTION
6+
#LOOP Hangs the system
7+
#EXHAUST_STACK Corrupts memory on failure
8+
#CORRUPT_STACK Crashes entire system on success
9+
#CORRUPT_STACK_STRONG Crashes entire system on success
10+
CORRUPT_LIST_ADD list_add corruption
11+
CORRUPT_LIST_DEL list_del corruption
12+
CORRUPT_USER_DS Invalid address limit on user-mode return
13+
STACK_GUARD_PAGE_LEADING
14+
STACK_GUARD_PAGE_TRAILING
15+
UNSET_SMEP CR4 bits went missing
16+
DOUBLE_FAULT
17+
UNALIGNED_LOAD_STORE_WRITE
18+
#OVERWRITE_ALLOCATION Corrupts memory on failure
19+
#WRITE_AFTER_FREE Corrupts memory on failure
20+
READ_AFTER_FREE
21+
#WRITE_BUDDY_AFTER_FREE Corrupts memory on failure
22+
READ_BUDDY_AFTER_FREE
23+
SLAB_FREE_DOUBLE
24+
SLAB_FREE_CROSS
25+
SLAB_FREE_PAGE
26+
#SOFTLOCKUP Hangs the system
27+
#HARDLOCKUP Hangs the system
28+
#SPINLOCKUP Hangs the system
29+
#HUNG_TASK Hangs the system
30+
EXEC_DATA
31+
EXEC_STACK
32+
EXEC_KMALLOC
33+
EXEC_VMALLOC
34+
EXEC_RODATA
35+
EXEC_USERSPACE
36+
EXEC_NULL
37+
ACCESS_USERSPACE
38+
ACCESS_NULL
39+
WRITE_RO
40+
WRITE_RO_AFTER_INIT
41+
WRITE_KERN
42+
REFCOUNT_INC_OVERFLOW
43+
REFCOUNT_ADD_OVERFLOW
44+
REFCOUNT_INC_NOT_ZERO_OVERFLOW
45+
REFCOUNT_ADD_NOT_ZERO_OVERFLOW
46+
REFCOUNT_DEC_ZERO
47+
REFCOUNT_DEC_NEGATIVE Negative detected: saturated
48+
REFCOUNT_DEC_AND_TEST_NEGATIVE Negative detected: saturated
49+
REFCOUNT_SUB_AND_TEST_NEGATIVE Negative detected: saturated
50+
REFCOUNT_INC_ZERO
51+
REFCOUNT_ADD_ZERO
52+
REFCOUNT_INC_SATURATED Saturation detected: still saturated
53+
REFCOUNT_DEC_SATURATED Saturation detected: still saturated
54+
REFCOUNT_ADD_SATURATED Saturation detected: still saturated
55+
REFCOUNT_INC_NOT_ZERO_SATURATED
56+
REFCOUNT_ADD_NOT_ZERO_SATURATED
57+
REFCOUNT_DEC_AND_TEST_SATURATED Saturation detected: still saturated
58+
REFCOUNT_SUB_AND_TEST_SATURATED Saturation detected: still saturated
59+
#REFCOUNT_TIMING timing only
60+
#ATOMIC_TIMING timing only
61+
USERCOPY_HEAP_SIZE_TO
62+
USERCOPY_HEAP_SIZE_FROM
63+
USERCOPY_HEAP_WHITELIST_TO
64+
USERCOPY_HEAP_WHITELIST_FROM
65+
USERCOPY_STACK_FRAME_TO
66+
USERCOPY_STACK_FRAME_FROM
67+
USERCOPY_STACK_BEYOND
68+
USERCOPY_KERNEL
69+
USERCOPY_KERNEL_DS
70+
STACKLEAK_ERASING OK: the rest of the thread stack is properly erased
71+
CFI_FORWARD_PROTO

0 commit comments

Comments
 (0)