1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3# Copyright (C) 2019 SUSE
4
5. $(dirname $0)/functions.sh
6
7MOD_LIVEPATCH=test_klp_state
8MOD_LIVEPATCH2=test_klp_state2
9MOD_LIVEPATCH3=test_klp_state3
10
11setup_config
12
13
14# Load and remove a module that modifies the system state
15
16start_test "system state modification"
17
18load_lp $MOD_LIVEPATCH
19disable_lp $MOD_LIVEPATCH
20unload_lp $MOD_LIVEPATCH
21
22check_result "% modprobe $MOD_LIVEPATCH
23livepatch: enabling patch '$MOD_LIVEPATCH'
24livepatch: '$MOD_LIVEPATCH': initializing patching transition
25$MOD_LIVEPATCH: pre_patch_callback: vmlinux
26$MOD_LIVEPATCH: allocate_loglevel_state: allocating space to store console_loglevel
27livepatch: '$MOD_LIVEPATCH': starting patching transition
28livepatch: '$MOD_LIVEPATCH': completing patching transition
29$MOD_LIVEPATCH: post_patch_callback: vmlinux
30$MOD_LIVEPATCH: fix_console_loglevel: fixing console_loglevel
31livepatch: '$MOD_LIVEPATCH': patching complete
32% echo 0 > /sys/kernel/livepatch/$MOD_LIVEPATCH/enabled
33livepatch: '$MOD_LIVEPATCH': initializing unpatching transition
34$MOD_LIVEPATCH: pre_unpatch_callback: vmlinux
35$MOD_LIVEPATCH: restore_console_loglevel: restoring console_loglevel
36livepatch: '$MOD_LIVEPATCH': starting unpatching transition
37livepatch: '$MOD_LIVEPATCH': completing unpatching transition
38$MOD_LIVEPATCH: post_unpatch_callback: vmlinux
39$MOD_LIVEPATCH: free_loglevel_state: freeing space for the stored console_loglevel
40livepatch: '$MOD_LIVEPATCH': unpatching complete
41% rmmod $MOD_LIVEPATCH"
42
43
44# Take over system state change by a cumulative patch
45
46start_test "taking over system state modification"
47
48load_lp $MOD_LIVEPATCH
49load_lp $MOD_LIVEPATCH2
50unload_lp $MOD_LIVEPATCH
51disable_lp $MOD_LIVEPATCH2
52unload_lp $MOD_LIVEPATCH2
53
54check_result "% modprobe $MOD_LIVEPATCH
55livepatch: enabling patch '$MOD_LIVEPATCH'
56livepatch: '$MOD_LIVEPATCH': initializing patching transition
57$MOD_LIVEPATCH: pre_patch_callback: vmlinux
58$MOD_LIVEPATCH: allocate_loglevel_state: allocating space to store console_loglevel
59livepatch: '$MOD_LIVEPATCH': starting patching transition
60livepatch: '$MOD_LIVEPATCH': completing patching transition
61$MOD_LIVEPATCH: post_patch_callback: vmlinux
62$MOD_LIVEPATCH: fix_console_loglevel: fixing console_loglevel
63livepatch: '$MOD_LIVEPATCH': patching complete
64% modprobe $MOD_LIVEPATCH2
65livepatch: enabling patch '$MOD_LIVEPATCH2'
66livepatch: '$MOD_LIVEPATCH2': initializing patching transition
67$MOD_LIVEPATCH2: pre_patch_callback: vmlinux
68$MOD_LIVEPATCH2: allocate_loglevel_state: space to store console_loglevel already allocated
69livepatch: '$MOD_LIVEPATCH2': starting patching transition
70livepatch: '$MOD_LIVEPATCH2': completing patching transition
71$MOD_LIVEPATCH2: post_patch_callback: vmlinux
72$MOD_LIVEPATCH2: fix_console_loglevel: taking over the console_loglevel change
73livepatch: '$MOD_LIVEPATCH2': patching complete
74% rmmod $MOD_LIVEPATCH
75% echo 0 > /sys/kernel/livepatch/$MOD_LIVEPATCH2/enabled
76livepatch: '$MOD_LIVEPATCH2': initializing unpatching transition
77$MOD_LIVEPATCH2: pre_unpatch_callback: vmlinux
78$MOD_LIVEPATCH2: restore_console_loglevel: restoring console_loglevel
79livepatch: '$MOD_LIVEPATCH2': starting unpatching transition
80livepatch: '$MOD_LIVEPATCH2': completing unpatching transition
81$MOD_LIVEPATCH2: post_unpatch_callback: vmlinux
82$MOD_LIVEPATCH2: free_loglevel_state: freeing space for the stored console_loglevel
83livepatch: '$MOD_LIVEPATCH2': unpatching complete
84% rmmod $MOD_LIVEPATCH2"
85
86
87# Take over system state change by a cumulative patch
88
89start_test "compatible cumulative livepatches"
90
91load_lp $MOD_LIVEPATCH2
92load_lp $MOD_LIVEPATCH3
93unload_lp $MOD_LIVEPATCH2
94load_lp $MOD_LIVEPATCH2
95disable_lp $MOD_LIVEPATCH2
96unload_lp $MOD_LIVEPATCH2
97unload_lp $MOD_LIVEPATCH3
98
99check_result "% modprobe $MOD_LIVEPATCH2
100livepatch: enabling patch '$MOD_LIVEPATCH2'
101livepatch: '$MOD_LIVEPATCH2': initializing patching transition
102$MOD_LIVEPATCH2: pre_patch_callback: vmlinux
103$MOD_LIVEPATCH2: allocate_loglevel_state: allocating space to store console_loglevel
104livepatch: '$MOD_LIVEPATCH2': starting patching transition
105livepatch: '$MOD_LIVEPATCH2': completing patching transition
106$MOD_LIVEPATCH2: post_patch_callback: vmlinux
107$MOD_LIVEPATCH2: fix_console_loglevel: fixing console_loglevel
108livepatch: '$MOD_LIVEPATCH2': patching complete
109% modprobe $MOD_LIVEPATCH3
110livepatch: enabling patch '$MOD_LIVEPATCH3'
111livepatch: '$MOD_LIVEPATCH3': initializing patching transition
112$MOD_LIVEPATCH3: pre_patch_callback: vmlinux
113$MOD_LIVEPATCH3: allocate_loglevel_state: space to store console_loglevel already allocated
114livepatch: '$MOD_LIVEPATCH3': starting patching transition
115livepatch: '$MOD_LIVEPATCH3': completing patching transition
116$MOD_LIVEPATCH3: post_patch_callback: vmlinux
117$MOD_LIVEPATCH3: fix_console_loglevel: taking over the console_loglevel change
118livepatch: '$MOD_LIVEPATCH3': patching complete
119% rmmod $MOD_LIVEPATCH2
120% modprobe $MOD_LIVEPATCH2
121livepatch: enabling patch '$MOD_LIVEPATCH2'
122livepatch: '$MOD_LIVEPATCH2': initializing patching transition
123$MOD_LIVEPATCH2: pre_patch_callback: vmlinux
124$MOD_LIVEPATCH2: allocate_loglevel_state: space to store console_loglevel already allocated
125livepatch: '$MOD_LIVEPATCH2': starting patching transition
126livepatch: '$MOD_LIVEPATCH2': completing patching transition
127$MOD_LIVEPATCH2: post_patch_callback: vmlinux
128$MOD_LIVEPATCH2: fix_console_loglevel: taking over the console_loglevel change
129livepatch: '$MOD_LIVEPATCH2': patching complete
130% echo 0 > /sys/kernel/livepatch/$MOD_LIVEPATCH2/enabled
131livepatch: '$MOD_LIVEPATCH2': initializing unpatching transition
132$MOD_LIVEPATCH2: pre_unpatch_callback: vmlinux
133$MOD_LIVEPATCH2: restore_console_loglevel: restoring console_loglevel
134livepatch: '$MOD_LIVEPATCH2': starting unpatching transition
135livepatch: '$MOD_LIVEPATCH2': completing unpatching transition
136$MOD_LIVEPATCH2: post_unpatch_callback: vmlinux
137$MOD_LIVEPATCH2: free_loglevel_state: freeing space for the stored console_loglevel
138livepatch: '$MOD_LIVEPATCH2': unpatching complete
139% rmmod $MOD_LIVEPATCH2
140% rmmod $MOD_LIVEPATCH3"
141
142
143# Failure caused by incompatible cumulative livepatches
144
145start_test "incompatible cumulative livepatches"
146
147load_lp $MOD_LIVEPATCH2
148load_failing_mod $MOD_LIVEPATCH
149disable_lp $MOD_LIVEPATCH2
150unload_lp $MOD_LIVEPATCH2
151
152check_result "% modprobe $MOD_LIVEPATCH2
153livepatch: enabling patch '$MOD_LIVEPATCH2'
154livepatch: '$MOD_LIVEPATCH2': initializing patching transition
155$MOD_LIVEPATCH2: pre_patch_callback: vmlinux
156$MOD_LIVEPATCH2: allocate_loglevel_state: allocating space to store console_loglevel
157livepatch: '$MOD_LIVEPATCH2': starting patching transition
158livepatch: '$MOD_LIVEPATCH2': completing patching transition
159$MOD_LIVEPATCH2: post_patch_callback: vmlinux
160$MOD_LIVEPATCH2: fix_console_loglevel: fixing console_loglevel
161livepatch: '$MOD_LIVEPATCH2': patching complete
162% modprobe $MOD_LIVEPATCH
163livepatch: Livepatch patch ($MOD_LIVEPATCH) is not compatible with the already installed livepatches.
164modprobe: ERROR: could not insert '$MOD_LIVEPATCH': Invalid argument
165% echo 0 > /sys/kernel/livepatch/$MOD_LIVEPATCH2/enabled
166livepatch: '$MOD_LIVEPATCH2': initializing unpatching transition
167$MOD_LIVEPATCH2: pre_unpatch_callback: vmlinux
168$MOD_LIVEPATCH2: restore_console_loglevel: restoring console_loglevel
169livepatch: '$MOD_LIVEPATCH2': starting unpatching transition
170livepatch: '$MOD_LIVEPATCH2': completing unpatching transition
171$MOD_LIVEPATCH2: post_unpatch_callback: vmlinux
172$MOD_LIVEPATCH2: free_loglevel_state: freeing space for the stored console_loglevel
173livepatch: '$MOD_LIVEPATCH2': unpatching complete
174% rmmod $MOD_LIVEPATCH2"
175
176exit 0
177