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_LIVEPATCH1=test_klp_livepatch
8MOD_LIVEPATCH2=test_klp_syscall
9MOD_LIVEPATCH3=test_klp_callbacks_demo
10MOD_REPLACE=test_klp_atomic_replace
11
12setup_config
13
14
15# - load a livepatch that modifies the output from /proc/cmdline and
16#   verify correct behavior
17# - unload the livepatch and make sure the patch was removed
18
19start_test "basic function patching"
20
21load_lp $MOD_LIVEPATCH1
22
23if [[ "$(cat /proc/cmdline)" != "$MOD_LIVEPATCH1: this has been live patched" ]] ; then
24	echo -e "FAIL\n\n"
25	die "livepatch kselftest(s) failed"
26fi
27
28disable_lp $MOD_LIVEPATCH1
29unload_lp $MOD_LIVEPATCH1
30
31if [[ "$(cat /proc/cmdline)" == "$MOD_LIVEPATCH1: this has been live patched" ]] ; then
32	echo -e "FAIL\n\n"
33	die "livepatch kselftest(s) failed"
34fi
35
36check_result "% insmod test_modules/$MOD_LIVEPATCH1.ko
37livepatch: enabling patch '$MOD_LIVEPATCH1'
38livepatch: '$MOD_LIVEPATCH1': initializing patching transition
39livepatch: '$MOD_LIVEPATCH1': starting patching transition
40livepatch: '$MOD_LIVEPATCH1': completing patching transition
41livepatch: '$MOD_LIVEPATCH1': patching complete
42% echo 0 > /sys/kernel/livepatch/$MOD_LIVEPATCH1/enabled
43livepatch: '$MOD_LIVEPATCH1': initializing unpatching transition
44livepatch: '$MOD_LIVEPATCH1': starting unpatching transition
45livepatch: '$MOD_LIVEPATCH1': completing unpatching transition
46livepatch: '$MOD_LIVEPATCH1': unpatching complete
47% rmmod $MOD_LIVEPATCH1"
48
49
50# - load a livepatch that modifies the output from /proc/cmdline and
51#   verify correct behavior
52# - load another livepatch and verify that both livepatches are active
53# - unload the second livepatch and verify that the first is still active
54# - unload the first livepatch and verify none are active
55
56start_test "multiple livepatches"
57
58load_lp $MOD_LIVEPATCH1
59
60grep 'live patched' /proc/cmdline > /dev/kmsg
61grep 'live patched' /proc/meminfo > /dev/kmsg
62
63load_lp $MOD_REPLACE replace=0
64
65grep 'live patched' /proc/cmdline > /dev/kmsg
66grep 'live patched' /proc/meminfo > /dev/kmsg
67
68disable_lp $MOD_REPLACE
69unload_lp $MOD_REPLACE
70
71grep 'live patched' /proc/cmdline > /dev/kmsg
72grep 'live patched' /proc/meminfo > /dev/kmsg
73
74disable_lp $MOD_LIVEPATCH1
75unload_lp $MOD_LIVEPATCH1
76
77grep 'live patched' /proc/cmdline > /dev/kmsg
78grep 'live patched' /proc/meminfo > /dev/kmsg
79
80check_result "% insmod test_modules/$MOD_LIVEPATCH1.ko
81livepatch: enabling patch '$MOD_LIVEPATCH1'
82livepatch: '$MOD_LIVEPATCH1': initializing patching transition
83livepatch: '$MOD_LIVEPATCH1': starting patching transition
84livepatch: '$MOD_LIVEPATCH1': completing patching transition
85livepatch: '$MOD_LIVEPATCH1': patching complete
86$MOD_LIVEPATCH1: this has been live patched
87% insmod test_modules/$MOD_REPLACE.ko replace=0
88livepatch: enabling patch '$MOD_REPLACE'
89livepatch: '$MOD_REPLACE': initializing patching transition
90livepatch: '$MOD_REPLACE': starting patching transition
91livepatch: '$MOD_REPLACE': completing patching transition
92livepatch: '$MOD_REPLACE': patching complete
93$MOD_LIVEPATCH1: this has been live patched
94$MOD_REPLACE: this has been live patched
95% echo 0 > /sys/kernel/livepatch/$MOD_REPLACE/enabled
96livepatch: '$MOD_REPLACE': initializing unpatching transition
97livepatch: '$MOD_REPLACE': starting unpatching transition
98livepatch: '$MOD_REPLACE': completing unpatching transition
99livepatch: '$MOD_REPLACE': unpatching complete
100% rmmod $MOD_REPLACE
101$MOD_LIVEPATCH1: this has been live patched
102% echo 0 > /sys/kernel/livepatch/$MOD_LIVEPATCH1/enabled
103livepatch: '$MOD_LIVEPATCH1': initializing unpatching transition
104livepatch: '$MOD_LIVEPATCH1': starting unpatching transition
105livepatch: '$MOD_LIVEPATCH1': completing unpatching transition
106livepatch: '$MOD_LIVEPATCH1': unpatching complete
107% rmmod $MOD_LIVEPATCH1"
108
109
110# - load a livepatch that modifies the output from /proc/cmdline and
111#   verify correct behavior
112# - load two additional livepatches and check the number of livepatch modules
113#   applied
114# - load an atomic replace livepatch and check that the other three modules were
115#   disabled
116# - remove all livepatches besides the atomic replace one and verify that the
117#   atomic replace livepatch is still active
118# - remove the atomic replace livepatch and verify that none are active
119
120start_test "atomic replace livepatch"
121
122load_lp $MOD_LIVEPATCH1
123
124grep 'live patched' /proc/cmdline > /dev/kmsg
125grep 'live patched' /proc/meminfo > /dev/kmsg
126
127for mod in $MOD_LIVEPATCH2 $MOD_LIVEPATCH3; do
128	load_lp "$mod"
129done
130
131mods=(/sys/kernel/livepatch/*)
132nmods=${#mods[@]}
133if [ "$nmods" -ne 3 ]; then
134	die "Expecting three modules listed, found $nmods"
135fi
136
137load_lp $MOD_REPLACE replace=1
138
139grep 'live patched' /proc/cmdline > /dev/kmsg
140grep 'live patched' /proc/meminfo > /dev/kmsg
141
142loop_until 'mods=(/sys/kernel/livepatch/*); nmods=${#mods[@]}; [[ "$nmods" -eq 1 ]]' ||
143        die "Expecting only one moduled listed, found $nmods"
144
145# These modules were disabled by the atomic replace
146for mod in $MOD_LIVEPATCH3 $MOD_LIVEPATCH2 $MOD_LIVEPATCH1; do
147	unload_lp "$mod"
148done
149
150grep 'live patched' /proc/cmdline > /dev/kmsg
151grep 'live patched' /proc/meminfo > /dev/kmsg
152
153disable_lp $MOD_REPLACE
154unload_lp $MOD_REPLACE
155
156grep 'live patched' /proc/cmdline > /dev/kmsg
157grep 'live patched' /proc/meminfo > /dev/kmsg
158
159check_result "% insmod test_modules/$MOD_LIVEPATCH1.ko
160livepatch: enabling patch '$MOD_LIVEPATCH1'
161livepatch: '$MOD_LIVEPATCH1': initializing patching transition
162livepatch: '$MOD_LIVEPATCH1': starting patching transition
163livepatch: '$MOD_LIVEPATCH1': completing patching transition
164livepatch: '$MOD_LIVEPATCH1': patching complete
165$MOD_LIVEPATCH1: this has been live patched
166% insmod test_modules/$MOD_LIVEPATCH2.ko
167livepatch: enabling patch '$MOD_LIVEPATCH2'
168livepatch: '$MOD_LIVEPATCH2': initializing patching transition
169livepatch: '$MOD_LIVEPATCH2': starting patching transition
170livepatch: '$MOD_LIVEPATCH2': completing patching transition
171livepatch: '$MOD_LIVEPATCH2': patching complete
172% insmod test_modules/$MOD_LIVEPATCH3.ko
173livepatch: enabling patch '$MOD_LIVEPATCH3'
174livepatch: '$MOD_LIVEPATCH3': initializing patching transition
175$MOD_LIVEPATCH3: pre_patch_callback: vmlinux
176livepatch: '$MOD_LIVEPATCH3': starting patching transition
177livepatch: '$MOD_LIVEPATCH3': completing patching transition
178$MOD_LIVEPATCH3: post_patch_callback: vmlinux
179livepatch: '$MOD_LIVEPATCH3': patching complete
180% insmod test_modules/$MOD_REPLACE.ko replace=1
181livepatch: enabling patch '$MOD_REPLACE'
182livepatch: '$MOD_REPLACE': initializing patching transition
183livepatch: '$MOD_REPLACE': starting patching transition
184livepatch: '$MOD_REPLACE': completing patching transition
185livepatch: '$MOD_REPLACE': patching complete
186$MOD_REPLACE: this has been live patched
187% rmmod $MOD_LIVEPATCH3
188% rmmod $MOD_LIVEPATCH2
189% rmmod $MOD_LIVEPATCH1
190$MOD_REPLACE: this has been live patched
191% echo 0 > /sys/kernel/livepatch/$MOD_REPLACE/enabled
192livepatch: '$MOD_REPLACE': initializing unpatching transition
193livepatch: '$MOD_REPLACE': starting unpatching transition
194livepatch: '$MOD_REPLACE': completing unpatching transition
195livepatch: '$MOD_REPLACE': unpatching complete
196% rmmod $MOD_REPLACE"
197
198
199exit 0
200