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