Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <keescook@chromium.org>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

authored by

Kees Cook and committed by
Shuah Khan
46d1a0f0 192c197c

+178
+1
MAINTAINERS
··· 9581 9581 M: Kees Cook <keescook@chromium.org> 9582 9582 S: Maintained 9583 9583 F: drivers/misc/lkdtm/* 9584 + F: tools/testing/selftests/lkdtm/* 9584 9585 9585 9586 LINUX KERNEL MEMORY CONSISTENCY MODEL (LKMM) 9586 9587 M: Alan Stern <stern@rowland.harvard.edu>
+1
tools/testing/selftests/Makefile
··· 26 26 TARGETS += kvm 27 27 TARGETS += lib 28 28 TARGETS += livepatch 29 + TARGETS += lkdtm 29 30 TARGETS += membarrier 30 31 TARGETS += memfd 31 32 TARGETS += memory-hotplug
+12
tools/testing/selftests/lkdtm/Makefile
··· 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 $@
+1
tools/testing/selftests/lkdtm/config
··· 1 + CONFIG_LKDTM=y
+92
tools/testing/selftests/lkdtm/run.sh
··· 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
+71
tools/testing/selftests/lkdtm/tests.txt
··· 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