xref: /linux/tools/testing/selftests/livepatch/test-livepatch.sh (revision e7d759f31ca295d589f7420719c311870bb3166f)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3# Copyright (C) 2018 Joe Lawrence <joe.lawrence@redhat.com>
4
5. $(dirname $0)/functions.sh
6
7MOD_LIVEPATCH=test_klp_livepatch
8MOD_REPLACE=test_klp_atomic_replace
9
10setup_config
11
12
13# - load a livepatch that modifies the output from /proc/cmdline and
14#   verify correct behavior
15# - unload the livepatch and make sure the patch was removed
16
17start_test "basic function patching"
18
19load_lp $MOD_LIVEPATCH
20
21if [[ "$(cat /proc/cmdline)" != "$MOD_LIVEPATCH: this has been live patched" ]] ; then
22	echo -e "FAIL\n\n"
23	die "livepatch kselftest(s) failed"
24fi
25
26disable_lp $MOD_LIVEPATCH
27unload_lp $MOD_LIVEPATCH
28
29if [[ "$(cat /proc/cmdline)" == "$MOD_LIVEPATCH: this has been live patched" ]] ; then
30	echo -e "FAIL\n\n"
31	die "livepatch kselftest(s) failed"
32fi
33
34check_result "% modprobe $MOD_LIVEPATCH
35livepatch: enabling patch '$MOD_LIVEPATCH'
36livepatch: '$MOD_LIVEPATCH': initializing patching transition
37livepatch: '$MOD_LIVEPATCH': starting patching transition
38livepatch: '$MOD_LIVEPATCH': completing patching transition
39livepatch: '$MOD_LIVEPATCH': patching complete
40% echo 0 > /sys/kernel/livepatch/$MOD_LIVEPATCH/enabled
41livepatch: '$MOD_LIVEPATCH': initializing unpatching transition
42livepatch: '$MOD_LIVEPATCH': starting unpatching transition
43livepatch: '$MOD_LIVEPATCH': completing unpatching transition
44livepatch: '$MOD_LIVEPATCH': unpatching complete
45% rmmod $MOD_LIVEPATCH"
46
47
48# - load a livepatch that modifies the output from /proc/cmdline and
49#   verify correct behavior
50# - load another livepatch and verify that both livepatches are active
51# - unload the second livepatch and verify that the first is still active
52# - unload the first livepatch and verify none are active
53
54start_test "multiple livepatches"
55
56load_lp $MOD_LIVEPATCH
57
58grep 'live patched' /proc/cmdline > /dev/kmsg
59grep 'live patched' /proc/meminfo > /dev/kmsg
60
61load_lp $MOD_REPLACE replace=0
62
63grep 'live patched' /proc/cmdline > /dev/kmsg
64grep 'live patched' /proc/meminfo > /dev/kmsg
65
66disable_lp $MOD_REPLACE
67unload_lp $MOD_REPLACE
68
69grep 'live patched' /proc/cmdline > /dev/kmsg
70grep 'live patched' /proc/meminfo > /dev/kmsg
71
72disable_lp $MOD_LIVEPATCH
73unload_lp $MOD_LIVEPATCH
74
75grep 'live patched' /proc/cmdline > /dev/kmsg
76grep 'live patched' /proc/meminfo > /dev/kmsg
77
78check_result "% modprobe $MOD_LIVEPATCH
79livepatch: enabling patch '$MOD_LIVEPATCH'
80livepatch: '$MOD_LIVEPATCH': initializing patching transition
81livepatch: '$MOD_LIVEPATCH': starting patching transition
82livepatch: '$MOD_LIVEPATCH': completing patching transition
83livepatch: '$MOD_LIVEPATCH': patching complete
84$MOD_LIVEPATCH: this has been live patched
85% modprobe $MOD_REPLACE replace=0
86livepatch: enabling patch '$MOD_REPLACE'
87livepatch: '$MOD_REPLACE': initializing patching transition
88livepatch: '$MOD_REPLACE': starting patching transition
89livepatch: '$MOD_REPLACE': completing patching transition
90livepatch: '$MOD_REPLACE': patching complete
91$MOD_LIVEPATCH: this has been live patched
92$MOD_REPLACE: this has been live patched
93% echo 0 > /sys/kernel/livepatch/$MOD_REPLACE/enabled
94livepatch: '$MOD_REPLACE': initializing unpatching transition
95livepatch: '$MOD_REPLACE': starting unpatching transition
96livepatch: '$MOD_REPLACE': completing unpatching transition
97livepatch: '$MOD_REPLACE': unpatching complete
98% rmmod $MOD_REPLACE
99$MOD_LIVEPATCH: this has been live patched
100% echo 0 > /sys/kernel/livepatch/$MOD_LIVEPATCH/enabled
101livepatch: '$MOD_LIVEPATCH': initializing unpatching transition
102livepatch: '$MOD_LIVEPATCH': starting unpatching transition
103livepatch: '$MOD_LIVEPATCH': completing unpatching transition
104livepatch: '$MOD_LIVEPATCH': unpatching complete
105% rmmod $MOD_LIVEPATCH"
106
107
108# - load a livepatch that modifies the output from /proc/cmdline and
109#   verify correct behavior
110# - load an atomic replace livepatch and verify that only the second is active
111# - remove the first livepatch and verify that the atomic replace livepatch
112#   is still active
113# - remove the atomic replace livepatch and verify that none are active
114
115start_test "atomic replace livepatch"
116
117load_lp $MOD_LIVEPATCH
118
119grep 'live patched' /proc/cmdline > /dev/kmsg
120grep 'live patched' /proc/meminfo > /dev/kmsg
121
122load_lp $MOD_REPLACE replace=1
123
124grep 'live patched' /proc/cmdline > /dev/kmsg
125grep 'live patched' /proc/meminfo > /dev/kmsg
126
127unload_lp $MOD_LIVEPATCH
128
129grep 'live patched' /proc/cmdline > /dev/kmsg
130grep 'live patched' /proc/meminfo > /dev/kmsg
131
132disable_lp $MOD_REPLACE
133unload_lp $MOD_REPLACE
134
135grep 'live patched' /proc/cmdline > /dev/kmsg
136grep 'live patched' /proc/meminfo > /dev/kmsg
137
138check_result "% modprobe $MOD_LIVEPATCH
139livepatch: enabling patch '$MOD_LIVEPATCH'
140livepatch: '$MOD_LIVEPATCH': initializing patching transition
141livepatch: '$MOD_LIVEPATCH': starting patching transition
142livepatch: '$MOD_LIVEPATCH': completing patching transition
143livepatch: '$MOD_LIVEPATCH': patching complete
144$MOD_LIVEPATCH: this has been live patched
145% modprobe $MOD_REPLACE replace=1
146livepatch: enabling patch '$MOD_REPLACE'
147livepatch: '$MOD_REPLACE': initializing patching transition
148livepatch: '$MOD_REPLACE': starting patching transition
149livepatch: '$MOD_REPLACE': completing patching transition
150livepatch: '$MOD_REPLACE': patching complete
151$MOD_REPLACE: this has been live patched
152% rmmod $MOD_LIVEPATCH
153$MOD_REPLACE: this has been live patched
154% echo 0 > /sys/kernel/livepatch/$MOD_REPLACE/enabled
155livepatch: '$MOD_REPLACE': initializing unpatching transition
156livepatch: '$MOD_REPLACE': starting unpatching transition
157livepatch: '$MOD_REPLACE': completing unpatching transition
158livepatch: '$MOD_REPLACE': unpatching complete
159% rmmod $MOD_REPLACE"
160
161
162exit 0
163