Loading...
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3# Copyright (C) 2018 Joe Lawrence <joe.lawrence@redhat.com>
4
5# Shell functions for the rest of the scripts.
6
7MAX_RETRIES=600
8RETRY_INTERVAL=".1" # seconds
9
10# log(msg) - write message to kernel log
11# msg - insightful words
12function log() {
13 echo "$1" > /dev/kmsg
14}
15
16# skip(msg) - testing can't proceed
17# msg - explanation
18function skip() {
19 log "SKIP: $1"
20 echo "SKIP: $1" >&2
21 exit 4
22}
23
24# die(msg) - game over, man
25# msg - dying words
26function die() {
27 log "ERROR: $1"
28 echo "ERROR: $1" >&2
29 exit 1
30}
31
32function push_dynamic_debug() {
33 DYNAMIC_DEBUG=$(grep '^kernel/livepatch' /sys/kernel/debug/dynamic_debug/control | \
34 awk -F'[: ]' '{print "file " $1 " line " $2 " " $4}')
35}
36
37function pop_dynamic_debug() {
38 if [[ -n "$DYNAMIC_DEBUG" ]]; then
39 echo -n "$DYNAMIC_DEBUG" > /sys/kernel/debug/dynamic_debug/control
40 fi
41}
42
43# set_dynamic_debug() - save the current dynamic debug config and tweak
44# it for the self-tests. Set a script exit trap
45# that restores the original config.
46function set_dynamic_debug() {
47 push_dynamic_debug
48 trap pop_dynamic_debug EXIT INT TERM HUP
49 cat <<-EOF > /sys/kernel/debug/dynamic_debug/control
50 file kernel/livepatch/* +p
51 func klp_try_switch_task -p
52 EOF
53}
54
55# loop_until(cmd) - loop a command until it is successful or $MAX_RETRIES,
56# sleep $RETRY_INTERVAL between attempts
57# cmd - command and its arguments to run
58function loop_until() {
59 local cmd="$*"
60 local i=0
61 while true; do
62 eval "$cmd" && return 0
63 [[ $((i++)) -eq $MAX_RETRIES ]] && return 1
64 sleep $RETRY_INTERVAL
65 done
66}
67
68function assert_mod() {
69 local mod="$1"
70
71 modprobe --dry-run "$mod" &>/dev/null
72}
73
74function is_livepatch_mod() {
75 local mod="$1"
76
77 if [[ $(modinfo "$mod" | awk '/^livepatch:/{print $NF}') == "Y" ]]; then
78 return 0
79 fi
80
81 return 1
82}
83
84function __load_mod() {
85 local mod="$1"; shift
86
87 local msg="% modprobe $mod $*"
88 log "${msg%% }"
89 ret=$(modprobe "$mod" "$@" 2>&1)
90 if [[ "$ret" != "" ]]; then
91 die "$ret"
92 fi
93
94 # Wait for module in sysfs ...
95 loop_until '[[ -e "/sys/module/$mod" ]]' ||
96 die "failed to load module $mod"
97}
98
99
100# load_mod(modname, params) - load a kernel module
101# modname - module name to load
102# params - module parameters to pass to modprobe
103function load_mod() {
104 local mod="$1"; shift
105
106 assert_mod "$mod" ||
107 skip "unable to load module ${mod}, verify CONFIG_TEST_LIVEPATCH=m and run self-tests as root"
108
109 is_livepatch_mod "$mod" &&
110 die "use load_lp() to load the livepatch module $mod"
111
112 __load_mod "$mod" "$@"
113}
114
115# load_lp_nowait(modname, params) - load a kernel module with a livepatch
116# but do not wait on until the transition finishes
117# modname - module name to load
118# params - module parameters to pass to modprobe
119function load_lp_nowait() {
120 local mod="$1"; shift
121
122 assert_mod "$mod" ||
123 skip "unable to load module ${mod}, verify CONFIG_TEST_LIVEPATCH=m and run self-tests as root"
124
125 is_livepatch_mod "$mod" ||
126 die "module $mod is not a livepatch"
127
128 __load_mod "$mod" "$@"
129
130 # Wait for livepatch in sysfs ...
131 loop_until '[[ -e "/sys/kernel/livepatch/$mod" ]]' ||
132 die "failed to load module $mod (sysfs)"
133}
134
135# load_lp(modname, params) - load a kernel module with a livepatch
136# modname - module name to load
137# params - module parameters to pass to modprobe
138function load_lp() {
139 local mod="$1"; shift
140
141 load_lp_nowait "$mod" "$@"
142
143 # Wait until the transition finishes ...
144 loop_until 'grep -q '^0$' /sys/kernel/livepatch/$mod/transition' ||
145 die "failed to complete transition"
146}
147
148# load_failing_mod(modname, params) - load a kernel module, expect to fail
149# modname - module name to load
150# params - module parameters to pass to modprobe
151function load_failing_mod() {
152 local mod="$1"; shift
153
154 local msg="% modprobe $mod $*"
155 log "${msg%% }"
156 ret=$(modprobe "$mod" "$@" 2>&1)
157 if [[ "$ret" == "" ]]; then
158 die "$mod unexpectedly loaded"
159 fi
160 log "$ret"
161}
162
163# unload_mod(modname) - unload a kernel module
164# modname - module name to unload
165function unload_mod() {
166 local mod="$1"
167
168 # Wait for module reference count to clear ...
169 loop_until '[[ $(cat "/sys/module/$mod/refcnt") == "0" ]]' ||
170 die "failed to unload module $mod (refcnt)"
171
172 log "% rmmod $mod"
173 ret=$(rmmod "$mod" 2>&1)
174 if [[ "$ret" != "" ]]; then
175 die "$ret"
176 fi
177
178 # Wait for module in sysfs ...
179 loop_until '[[ ! -e "/sys/module/$mod" ]]' ||
180 die "failed to unload module $mod (/sys/module)"
181}
182
183# unload_lp(modname) - unload a kernel module with a livepatch
184# modname - module name to unload
185function unload_lp() {
186 unload_mod "$1"
187}
188
189# disable_lp(modname) - disable a livepatch
190# modname - module name to unload
191function disable_lp() {
192 local mod="$1"
193
194 log "% echo 0 > /sys/kernel/livepatch/$mod/enabled"
195 echo 0 > /sys/kernel/livepatch/"$mod"/enabled
196
197 # Wait until the transition finishes and the livepatch gets
198 # removed from sysfs...
199 loop_until '[[ ! -e "/sys/kernel/livepatch/$mod" ]]' ||
200 die "failed to disable livepatch $mod"
201}
202
203# set_pre_patch_ret(modname, pre_patch_ret)
204# modname - module name to set
205# pre_patch_ret - new pre_patch_ret value
206function set_pre_patch_ret {
207 local mod="$1"; shift
208 local ret="$1"
209
210 log "% echo $ret > /sys/module/$mod/parameters/pre_patch_ret"
211 echo "$ret" > /sys/module/"$mod"/parameters/pre_patch_ret
212
213 # Wait for sysfs value to hold ...
214 loop_until '[[ $(cat "/sys/module/$mod/parameters/pre_patch_ret") == "$ret" ]]' ||
215 die "failed to set pre_patch_ret parameter for $mod module"
216}
217
218# check_result() - verify dmesg output
219# TODO - better filter, out of order msgs, etc?
220function check_result {
221 local expect="$*"
222 local result
223
224 result=$(dmesg | grep -v 'tainting' | grep -e 'livepatch:' -e 'test_klp' | sed 's/^\[[ 0-9.]*\] //')
225
226 if [[ "$expect" == "$result" ]] ; then
227 echo "ok"
228 else
229 echo -e "not ok\n\n$(diff -upr --label expected --label result <(echo "$expect") <(echo "$result"))\n"
230 die "livepatch kselftest(s) failed"
231 fi
232}
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3# Copyright (C) 2018 Joe Lawrence <joe.lawrence@redhat.com>
4
5# Shell functions for the rest of the scripts.
6
7MAX_RETRIES=600
8RETRY_INTERVAL=".1" # seconds
9KLP_SYSFS_DIR="/sys/kernel/livepatch"
10
11# Kselftest framework requirement - SKIP code is 4
12ksft_skip=4
13
14# log(msg) - write message to kernel log
15# msg - insightful words
16function log() {
17 echo "$1" > /dev/kmsg
18}
19
20# skip(msg) - testing can't proceed
21# msg - explanation
22function skip() {
23 log "SKIP: $1"
24 echo "SKIP: $1" >&2
25 exit $ksft_skip
26}
27
28# root test
29function is_root() {
30 uid=$(id -u)
31 if [ $uid -ne 0 ]; then
32 echo "skip all tests: must be run as root" >&2
33 exit $ksft_skip
34 fi
35}
36
37# die(msg) - game over, man
38# msg - dying words
39function die() {
40 log "ERROR: $1"
41 echo "ERROR: $1" >&2
42 exit 1
43}
44
45function push_config() {
46 DYNAMIC_DEBUG=$(grep '^kernel/livepatch' /sys/kernel/debug/dynamic_debug/control | \
47 awk -F'[: ]' '{print "file " $1 " line " $2 " " $4}')
48 FTRACE_ENABLED=$(sysctl --values kernel.ftrace_enabled)
49}
50
51function pop_config() {
52 if [[ -n "$DYNAMIC_DEBUG" ]]; then
53 echo -n "$DYNAMIC_DEBUG" > /sys/kernel/debug/dynamic_debug/control
54 fi
55 if [[ -n "$FTRACE_ENABLED" ]]; then
56 sysctl kernel.ftrace_enabled="$FTRACE_ENABLED" &> /dev/null
57 fi
58}
59
60function set_dynamic_debug() {
61 cat <<-EOF > /sys/kernel/debug/dynamic_debug/control
62 file kernel/livepatch/* +p
63 func klp_try_switch_task -p
64 EOF
65}
66
67function set_ftrace_enabled() {
68 local can_fail=0
69 if [[ "$1" == "--fail" ]] ; then
70 can_fail=1
71 shift
72 fi
73
74 local err=$(sysctl -q kernel.ftrace_enabled="$1" 2>&1)
75 local result=$(sysctl --values kernel.ftrace_enabled)
76
77 if [[ "$result" != "$1" ]] ; then
78 if [[ $can_fail -eq 1 ]] ; then
79 echo "livepatch: $err" | sed 's#/proc/sys/kernel/#kernel.#' > /dev/kmsg
80 return
81 fi
82
83 skip "failed to set kernel.ftrace_enabled = $1"
84 fi
85
86 echo "livepatch: kernel.ftrace_enabled = $result" > /dev/kmsg
87}
88
89function cleanup() {
90 pop_config
91}
92
93# setup_config - save the current config and set a script exit trap that
94# restores the original config. Setup the dynamic debug
95# for verbose livepatching output and turn on
96# the ftrace_enabled sysctl.
97function setup_config() {
98 is_root
99 push_config
100 set_dynamic_debug
101 set_ftrace_enabled 1
102 trap cleanup EXIT INT TERM HUP
103}
104
105# loop_until(cmd) - loop a command until it is successful or $MAX_RETRIES,
106# sleep $RETRY_INTERVAL between attempts
107# cmd - command and its arguments to run
108function loop_until() {
109 local cmd="$*"
110 local i=0
111 while true; do
112 eval "$cmd" && return 0
113 [[ $((i++)) -eq $MAX_RETRIES ]] && return 1
114 sleep $RETRY_INTERVAL
115 done
116}
117
118function assert_mod() {
119 local mod="$1"
120
121 modprobe --dry-run "$mod" &>/dev/null
122}
123
124function is_livepatch_mod() {
125 local mod="$1"
126
127 if [[ $(modinfo "$mod" | awk '/^livepatch:/{print $NF}') == "Y" ]]; then
128 return 0
129 fi
130
131 return 1
132}
133
134function __load_mod() {
135 local mod="$1"; shift
136
137 local msg="% modprobe $mod $*"
138 log "${msg%% }"
139 ret=$(modprobe "$mod" "$@" 2>&1)
140 if [[ "$ret" != "" ]]; then
141 die "$ret"
142 fi
143
144 # Wait for module in sysfs ...
145 loop_until '[[ -e "/sys/module/$mod" ]]' ||
146 die "failed to load module $mod"
147}
148
149
150# load_mod(modname, params) - load a kernel module
151# modname - module name to load
152# params - module parameters to pass to modprobe
153function load_mod() {
154 local mod="$1"; shift
155
156 assert_mod "$mod" ||
157 skip "unable to load module ${mod}, verify CONFIG_TEST_LIVEPATCH=m and run self-tests as root"
158
159 is_livepatch_mod "$mod" &&
160 die "use load_lp() to load the livepatch module $mod"
161
162 __load_mod "$mod" "$@"
163}
164
165# load_lp_nowait(modname, params) - load a kernel module with a livepatch
166# but do not wait on until the transition finishes
167# modname - module name to load
168# params - module parameters to pass to modprobe
169function load_lp_nowait() {
170 local mod="$1"; shift
171
172 assert_mod "$mod" ||
173 skip "unable to load module ${mod}, verify CONFIG_TEST_LIVEPATCH=m and run self-tests as root"
174
175 is_livepatch_mod "$mod" ||
176 die "module $mod is not a livepatch"
177
178 __load_mod "$mod" "$@"
179
180 # Wait for livepatch in sysfs ...
181 loop_until '[[ -e "/sys/kernel/livepatch/$mod" ]]' ||
182 die "failed to load module $mod (sysfs)"
183}
184
185# load_lp(modname, params) - load a kernel module with a livepatch
186# modname - module name to load
187# params - module parameters to pass to modprobe
188function load_lp() {
189 local mod="$1"; shift
190
191 load_lp_nowait "$mod" "$@"
192
193 # Wait until the transition finishes ...
194 loop_until 'grep -q '^0$' /sys/kernel/livepatch/$mod/transition' ||
195 die "failed to complete transition"
196}
197
198# load_failing_mod(modname, params) - load a kernel module, expect to fail
199# modname - module name to load
200# params - module parameters to pass to modprobe
201function load_failing_mod() {
202 local mod="$1"; shift
203
204 local msg="% modprobe $mod $*"
205 log "${msg%% }"
206 ret=$(modprobe "$mod" "$@" 2>&1)
207 if [[ "$ret" == "" ]]; then
208 die "$mod unexpectedly loaded"
209 fi
210 log "$ret"
211}
212
213# unload_mod(modname) - unload a kernel module
214# modname - module name to unload
215function unload_mod() {
216 local mod="$1"
217
218 # Wait for module reference count to clear ...
219 loop_until '[[ $(cat "/sys/module/$mod/refcnt") == "0" ]]' ||
220 die "failed to unload module $mod (refcnt)"
221
222 log "% rmmod $mod"
223 ret=$(rmmod "$mod" 2>&1)
224 if [[ "$ret" != "" ]]; then
225 die "$ret"
226 fi
227
228 # Wait for module in sysfs ...
229 loop_until '[[ ! -e "/sys/module/$mod" ]]' ||
230 die "failed to unload module $mod (/sys/module)"
231}
232
233# unload_lp(modname) - unload a kernel module with a livepatch
234# modname - module name to unload
235function unload_lp() {
236 unload_mod "$1"
237}
238
239# disable_lp(modname) - disable a livepatch
240# modname - module name to unload
241function disable_lp() {
242 local mod="$1"
243
244 log "% echo 0 > /sys/kernel/livepatch/$mod/enabled"
245 echo 0 > /sys/kernel/livepatch/"$mod"/enabled
246
247 # Wait until the transition finishes and the livepatch gets
248 # removed from sysfs...
249 loop_until '[[ ! -e "/sys/kernel/livepatch/$mod" ]]' ||
250 die "failed to disable livepatch $mod"
251}
252
253# set_pre_patch_ret(modname, pre_patch_ret)
254# modname - module name to set
255# pre_patch_ret - new pre_patch_ret value
256function set_pre_patch_ret {
257 local mod="$1"; shift
258 local ret="$1"
259
260 log "% echo $ret > /sys/module/$mod/parameters/pre_patch_ret"
261 echo "$ret" > /sys/module/"$mod"/parameters/pre_patch_ret
262
263 # Wait for sysfs value to hold ...
264 loop_until '[[ $(cat "/sys/module/$mod/parameters/pre_patch_ret") == "$ret" ]]' ||
265 die "failed to set pre_patch_ret parameter for $mod module"
266}
267
268function start_test {
269 local test="$1"
270
271 # Dump something unique into the dmesg log, then stash the entry
272 # in LAST_DMESG. The check_result() function will use it to
273 # find new kernel messages since the test started.
274 local last_dmesg_msg="livepatch kselftest timestamp: $(date --rfc-3339=ns)"
275 log "$last_dmesg_msg"
276 loop_until 'dmesg | grep -q "$last_dmesg_msg"' ||
277 die "buffer busy? can't find canary dmesg message: $last_dmesg_msg"
278 LAST_DMESG=$(dmesg | grep "$last_dmesg_msg")
279
280 echo -n "TEST: $test ... "
281 log "===== TEST: $test ====="
282}
283
284# check_result() - verify dmesg output
285# TODO - better filter, out of order msgs, etc?
286function check_result {
287 local expect="$*"
288 local result
289
290 # Test results include any new dmesg entry since LAST_DMESG, then:
291 # - include lines matching keywords
292 # - exclude lines matching keywords
293 # - filter out dmesg timestamp prefixes
294 result=$(dmesg | awk -v last_dmesg="$LAST_DMESG" 'p; $0 == last_dmesg { p=1 }' | \
295 grep -e 'livepatch:' -e 'test_klp' | \
296 grep -v '\(tainting\|taints\) kernel' | \
297 sed 's/^\[[ 0-9.]*\] //')
298
299 if [[ "$expect" == "$result" ]] ; then
300 echo "ok"
301 elif [[ "$result" == "" ]] ; then
302 echo -e "not ok\n\nbuffer overrun? can't find canary dmesg entry: $LAST_DMESG\n"
303 die "livepatch kselftest(s) failed"
304 else
305 echo -e "not ok\n\n$(diff -upr --label expected --label result <(echo "$expect") <(echo "$result"))\n"
306 die "livepatch kselftest(s) failed"
307 fi
308}
309
310# check_sysfs_rights(modname, rel_path, expected_rights) - check sysfs
311# path permissions
312# modname - livepatch module creating the sysfs interface
313# rel_path - relative path of the sysfs interface
314# expected_rights - expected access rights
315function check_sysfs_rights() {
316 local mod="$1"; shift
317 local rel_path="$1"; shift
318 local expected_rights="$1"; shift
319
320 local path="$KLP_SYSFS_DIR/$mod/$rel_path"
321 local rights=$(/bin/stat --format '%A' "$path")
322 if test "$rights" != "$expected_rights" ; then
323 die "Unexpected access rights of $path: $expected_rights vs. $rights"
324 fi
325}
326
327# check_sysfs_value(modname, rel_path, expected_value) - check sysfs value
328# modname - livepatch module creating the sysfs interface
329# rel_path - relative path of the sysfs interface
330# expected_value - expected value read from the file
331function check_sysfs_value() {
332 local mod="$1"; shift
333 local rel_path="$1"; shift
334 local expected_value="$1"; shift
335
336 local path="$KLP_SYSFS_DIR/$mod/$rel_path"
337 local value=`cat $path`
338 if test "$value" != "$expected_value" ; then
339 die "Unexpected value in $path: $expected_value vs. $value"
340 fi
341}