Skip to content

Commit ff1b80e

Browse files
liu-song-6pmladek
authored andcommitted
selftests/livepatch: add sysfs test
Add a test for livepatch sysfs entries. Signed-off-by: Song Liu <[email protected]> Reviewed-by: Petr Mladek <[email protected]> Signed-off-by: Petr Mladek <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent bb26cfd commit ff1b80e

File tree

3 files changed

+122
-1
lines changed

3 files changed

+122
-1
lines changed

tools/testing/selftests/livepatch/Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ TEST_PROGS := \
66
test-callbacks.sh \
77
test-shadow-vars.sh \
88
test-state.sh \
9-
test-ftrace.sh
9+
test-ftrace.sh \
10+
test-sysfs.sh
1011

1112
TEST_FILES := settings
1213

tools/testing/selftests/livepatch/functions.sh

+34
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
MAX_RETRIES=600
88
RETRY_INTERVAL=".1" # seconds
9+
KLP_SYSFS_DIR="/sys/kernel/livepatch"
910

1011
# Kselftest framework requirement - SKIP code is 4
1112
ksft_skip=4
@@ -308,3 +309,36 @@ function check_result {
308309

309310
cleanup_dmesg_file
310311
}
312+
313+
# check_sysfs_rights(modname, rel_path, expected_rights) - check sysfs
314+
# path permissions
315+
# modname - livepatch module creating the sysfs interface
316+
# rel_path - relative path of the sysfs interface
317+
# expected_rights - expected access rights
318+
function check_sysfs_rights() {
319+
local mod="$1"; shift
320+
local rel_path="$1"; shift
321+
local expected_rights="$1"; shift
322+
323+
local path="$KLP_SYSFS_DIR/$mod/$rel_path"
324+
local rights=$(/bin/stat --format '%A' "$path")
325+
if test "$rights" != "$expected_rights" ; then
326+
die "Unexpected access rights of $path: $expected_rights vs. $rights"
327+
fi
328+
}
329+
330+
# check_sysfs_value(modname, rel_path, expected_value) - check sysfs value
331+
# modname - livepatch module creating the sysfs interface
332+
# rel_path - relative path of the sysfs interface
333+
# expected_value - expected value read from the file
334+
function check_sysfs_value() {
335+
local mod="$1"; shift
336+
local rel_path="$1"; shift
337+
local expected_value="$1"; shift
338+
339+
local path="$KLP_SYSFS_DIR/$mod/$rel_path"
340+
local value=`cat $path`
341+
if test "$value" != "$expected_value" ; then
342+
die "Unexpected value in $path: $expected_value vs. $value"
343+
fi
344+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/bin/bash
2+
# SPDX-License-Identifier: GPL-2.0
3+
# Copyright (C) 2022 Song Liu <[email protected]>
4+
5+
. $(dirname $0)/functions.sh
6+
7+
MOD_LIVEPATCH=test_klp_livepatch
8+
9+
setup_config
10+
11+
# - load a livepatch and verifies the sysfs entries work as expected
12+
13+
start_test "sysfs test"
14+
15+
load_lp $MOD_LIVEPATCH
16+
17+
check_sysfs_rights "$MOD_LIVEPATCH" "" "drwxr-xr-x"
18+
check_sysfs_rights "$MOD_LIVEPATCH" "enabled" "-rw-r--r--"
19+
check_sysfs_value "$MOD_LIVEPATCH" "enabled" "1"
20+
check_sysfs_rights "$MOD_LIVEPATCH" "force" "--w-------"
21+
check_sysfs_rights "$MOD_LIVEPATCH" "transition" "-r--r--r--"
22+
check_sysfs_value "$MOD_LIVEPATCH" "transition" "0"
23+
check_sysfs_rights "$MOD_LIVEPATCH" "vmlinux/patched" "-r--r--r--"
24+
check_sysfs_value "$MOD_LIVEPATCH" "vmlinux/patched" "1"
25+
26+
disable_lp $MOD_LIVEPATCH
27+
28+
unload_lp $MOD_LIVEPATCH
29+
30+
check_result "% modprobe $MOD_LIVEPATCH
31+
livepatch: enabling patch '$MOD_LIVEPATCH'
32+
livepatch: '$MOD_LIVEPATCH': initializing patching transition
33+
livepatch: '$MOD_LIVEPATCH': starting patching transition
34+
livepatch: '$MOD_LIVEPATCH': completing patching transition
35+
livepatch: '$MOD_LIVEPATCH': patching complete
36+
% echo 0 > /sys/kernel/livepatch/$MOD_LIVEPATCH/enabled
37+
livepatch: '$MOD_LIVEPATCH': initializing unpatching transition
38+
livepatch: '$MOD_LIVEPATCH': starting unpatching transition
39+
livepatch: '$MOD_LIVEPATCH': completing unpatching transition
40+
livepatch: '$MOD_LIVEPATCH': unpatching complete
41+
% rmmod $MOD_LIVEPATCH"
42+
43+
start_test "sysfs test object/patched"
44+
45+
MOD_LIVEPATCH=test_klp_callbacks_demo
46+
MOD_TARGET=test_klp_callbacks_mod
47+
load_lp $MOD_LIVEPATCH
48+
49+
# check the "patch" file changes as target module loads/unloads
50+
check_sysfs_value "$MOD_LIVEPATCH" "$MOD_TARGET/patched" "0"
51+
load_mod $MOD_TARGET
52+
check_sysfs_value "$MOD_LIVEPATCH" "$MOD_TARGET/patched" "1"
53+
unload_mod $MOD_TARGET
54+
check_sysfs_value "$MOD_LIVEPATCH" "$MOD_TARGET/patched" "0"
55+
56+
disable_lp $MOD_LIVEPATCH
57+
unload_lp $MOD_LIVEPATCH
58+
59+
check_result "% modprobe test_klp_callbacks_demo
60+
livepatch: enabling patch 'test_klp_callbacks_demo'
61+
livepatch: 'test_klp_callbacks_demo': initializing patching transition
62+
test_klp_callbacks_demo: pre_patch_callback: vmlinux
63+
livepatch: 'test_klp_callbacks_demo': starting patching transition
64+
livepatch: 'test_klp_callbacks_demo': completing patching transition
65+
test_klp_callbacks_demo: post_patch_callback: vmlinux
66+
livepatch: 'test_klp_callbacks_demo': patching complete
67+
% modprobe test_klp_callbacks_mod
68+
livepatch: applying patch 'test_klp_callbacks_demo' to loading module 'test_klp_callbacks_mod'
69+
test_klp_callbacks_demo: pre_patch_callback: test_klp_callbacks_mod -> [MODULE_STATE_COMING] Full formed, running module_init
70+
test_klp_callbacks_demo: post_patch_callback: test_klp_callbacks_mod -> [MODULE_STATE_COMING] Full formed, running module_init
71+
test_klp_callbacks_mod: test_klp_callbacks_mod_init
72+
% rmmod test_klp_callbacks_mod
73+
test_klp_callbacks_mod: test_klp_callbacks_mod_exit
74+
test_klp_callbacks_demo: pre_unpatch_callback: test_klp_callbacks_mod -> [MODULE_STATE_GOING] Going away
75+
livepatch: reverting patch 'test_klp_callbacks_demo' on unloading module 'test_klp_callbacks_mod'
76+
test_klp_callbacks_demo: post_unpatch_callback: test_klp_callbacks_mod -> [MODULE_STATE_GOING] Going away
77+
% echo 0 > /sys/kernel/livepatch/test_klp_callbacks_demo/enabled
78+
livepatch: 'test_klp_callbacks_demo': initializing unpatching transition
79+
test_klp_callbacks_demo: pre_unpatch_callback: vmlinux
80+
livepatch: 'test_klp_callbacks_demo': starting unpatching transition
81+
livepatch: 'test_klp_callbacks_demo': completing unpatching transition
82+
test_klp_callbacks_demo: post_unpatch_callback: vmlinux
83+
livepatch: 'test_klp_callbacks_demo': unpatching complete
84+
% rmmod test_klp_callbacks_demo"
85+
86+
exit 0

0 commit comments

Comments
 (0)