functions.sh 9.0 KB

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