functions.sh 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. # Copyright (C) 2018 Joe Lawrence <[email protected]>
  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. # die(msg) - game over, man
  31. # msg - dying words
  32. function die() {
  33. log "ERROR: $1"
  34. echo "ERROR: $1" >&2
  35. exit 1
  36. }
  37. # save existing dmesg so we can detect new content
  38. function save_dmesg() {
  39. SAVED_DMESG=$(mktemp --tmpdir -t klp-dmesg-XXXXXX)
  40. dmesg > "$SAVED_DMESG"
  41. }
  42. # cleanup temporary dmesg file from save_dmesg()
  43. function cleanup_dmesg_file() {
  44. rm -f "$SAVED_DMESG"
  45. }
  46. function push_config() {
  47. DYNAMIC_DEBUG=$(grep '^kernel/livepatch' /sys/kernel/debug/dynamic_debug/control | \
  48. awk -F'[: ]' '{print "file " $1 " line " $2 " " $4}')
  49. FTRACE_ENABLED=$(sysctl --values kernel.ftrace_enabled)
  50. }
  51. function pop_config() {
  52. if [[ -n "$DYNAMIC_DEBUG" ]]; then
  53. echo -n "$DYNAMIC_DEBUG" > /sys/kernel/debug/dynamic_debug/control
  54. fi
  55. if [[ -n "$FTRACE_ENABLED" ]]; then
  56. sysctl kernel.ftrace_enabled="$FTRACE_ENABLED" &> /dev/null
  57. fi
  58. }
  59. function set_dynamic_debug() {
  60. cat <<-EOF > /sys/kernel/debug/dynamic_debug/control
  61. file kernel/livepatch/* +p
  62. func klp_try_switch_task -p
  63. EOF
  64. }
  65. function set_ftrace_enabled() {
  66. local can_fail=0
  67. if [[ "$1" == "--fail" ]] ; then
  68. can_fail=1
  69. shift
  70. fi
  71. local err=$(sysctl -q kernel.ftrace_enabled="$1" 2>&1)
  72. local result=$(sysctl --values kernel.ftrace_enabled)
  73. if [[ "$result" != "$1" ]] ; then
  74. if [[ $can_fail -eq 1 ]] ; then
  75. echo "livepatch: $err" | sed 's#/proc/sys/kernel/#kernel.#' > /dev/kmsg
  76. return
  77. fi
  78. skip "failed to set kernel.ftrace_enabled = $1"
  79. fi
  80. echo "livepatch: kernel.ftrace_enabled = $result" > /dev/kmsg
  81. }
  82. function cleanup() {
  83. pop_config
  84. cleanup_dmesg_file
  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. push_config
  93. set_dynamic_debug
  94. set_ftrace_enabled 1
  95. trap cleanup EXIT INT TERM HUP
  96. }
  97. # loop_until(cmd) - loop a command until it is successful or $MAX_RETRIES,
  98. # sleep $RETRY_INTERVAL between attempts
  99. # cmd - command and its arguments to run
  100. function loop_until() {
  101. local cmd="$*"
  102. local i=0
  103. while true; do
  104. eval "$cmd" && return 0
  105. [[ $((i++)) -eq $MAX_RETRIES ]] && return 1
  106. sleep $RETRY_INTERVAL
  107. done
  108. }
  109. function assert_mod() {
  110. local mod="$1"
  111. modprobe --dry-run "$mod" &>/dev/null
  112. }
  113. function is_livepatch_mod() {
  114. local mod="$1"
  115. if [[ $(modinfo "$mod" | 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="% modprobe $mod $*"
  123. log "${msg%% }"
  124. ret=$(modprobe "$mod" "$@" 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 modprobe
  135. function load_mod() {
  136. local mod="$1"; shift
  137. assert_mod "$mod" ||
  138. skip "unable to load module ${mod}, verify CONFIG_TEST_LIVEPATCH=m and run self-tests as root"
  139. is_livepatch_mod "$mod" &&
  140. die "use load_lp() to load the livepatch module $mod"
  141. __load_mod "$mod" "$@"
  142. }
  143. # load_lp_nowait(modname, params) - load a kernel module with a livepatch
  144. # but do not wait on until the transition finishes
  145. # modname - module name to load
  146. # params - module parameters to pass to modprobe
  147. function load_lp_nowait() {
  148. local mod="$1"; shift
  149. assert_mod "$mod" ||
  150. skip "unable to load module ${mod}, verify CONFIG_TEST_LIVEPATCH=m and run self-tests as root"
  151. is_livepatch_mod "$mod" ||
  152. die "module $mod is not a livepatch"
  153. __load_mod "$mod" "$@"
  154. # Wait for livepatch in sysfs ...
  155. loop_until '[[ -e "/sys/kernel/livepatch/$mod" ]]' ||
  156. die "failed to load module $mod (sysfs)"
  157. }
  158. # load_lp(modname, params) - load a kernel module with a livepatch
  159. # modname - module name to load
  160. # params - module parameters to pass to modprobe
  161. function load_lp() {
  162. local mod="$1"; shift
  163. load_lp_nowait "$mod" "$@"
  164. # Wait until the transition finishes ...
  165. loop_until 'grep -q '^0$' /sys/kernel/livepatch/$mod/transition' ||
  166. die "failed to complete transition"
  167. }
  168. # load_failing_mod(modname, params) - load a kernel module, expect to fail
  169. # modname - module name to load
  170. # params - module parameters to pass to modprobe
  171. function load_failing_mod() {
  172. local mod="$1"; shift
  173. local msg="% modprobe $mod $*"
  174. log "${msg%% }"
  175. ret=$(modprobe "$mod" "$@" 2>&1)
  176. if [[ "$ret" == "" ]]; then
  177. die "$mod unexpectedly loaded"
  178. fi
  179. log "$ret"
  180. }
  181. # unload_mod(modname) - unload a kernel module
  182. # modname - module name to unload
  183. function unload_mod() {
  184. local mod="$1"
  185. # Wait for module reference count to clear ...
  186. loop_until '[[ $(cat "/sys/module/$mod/refcnt") == "0" ]]' ||
  187. die "failed to unload module $mod (refcnt)"
  188. log "% rmmod $mod"
  189. ret=$(rmmod "$mod" 2>&1)
  190. if [[ "$ret" != "" ]]; then
  191. die "$ret"
  192. fi
  193. # Wait for module in sysfs ...
  194. loop_until '[[ ! -e "/sys/module/$mod" ]]' ||
  195. die "failed to unload module $mod (/sys/module)"
  196. }
  197. # unload_lp(modname) - unload a kernel module with a livepatch
  198. # modname - module name to unload
  199. function unload_lp() {
  200. unload_mod "$1"
  201. }
  202. # disable_lp(modname) - disable a livepatch
  203. # modname - module name to unload
  204. function disable_lp() {
  205. local mod="$1"
  206. log "% echo 0 > /sys/kernel/livepatch/$mod/enabled"
  207. echo 0 > /sys/kernel/livepatch/"$mod"/enabled
  208. # Wait until the transition finishes and the livepatch gets
  209. # removed from sysfs...
  210. loop_until '[[ ! -e "/sys/kernel/livepatch/$mod" ]]' ||
  211. die "failed to disable livepatch $mod"
  212. }
  213. # set_pre_patch_ret(modname, pre_patch_ret)
  214. # modname - module name to set
  215. # pre_patch_ret - new pre_patch_ret value
  216. function set_pre_patch_ret {
  217. local mod="$1"; shift
  218. local ret="$1"
  219. log "% echo $ret > /sys/module/$mod/parameters/pre_patch_ret"
  220. echo "$ret" > /sys/module/"$mod"/parameters/pre_patch_ret
  221. # Wait for sysfs value to hold ...
  222. loop_until '[[ $(cat "/sys/module/$mod/parameters/pre_patch_ret") == "$ret" ]]' ||
  223. die "failed to set pre_patch_ret parameter for $mod module"
  224. }
  225. function start_test {
  226. local test="$1"
  227. save_dmesg
  228. echo -n "TEST: $test ... "
  229. log "===== TEST: $test ====="
  230. }
  231. # check_result() - verify dmesg output
  232. # TODO - better filter, out of order msgs, etc?
  233. function check_result {
  234. local expect="$*"
  235. local result
  236. # Note: when comparing dmesg output, the kernel log timestamps
  237. # help differentiate repeated testing runs. Remove them with a
  238. # post-comparison sed filter.
  239. result=$(dmesg | comm --nocheck-order -13 "$SAVED_DMESG" - | \
  240. grep -e 'livepatch:' -e 'test_klp' | \
  241. grep -v '\(tainting\|taints\) kernel' | \
  242. sed 's/^\[[ 0-9.]*\] //')
  243. if [[ "$expect" == "$result" ]] ; then
  244. echo "ok"
  245. else
  246. echo -e "not ok\n\n$(diff -upr --label expected --label result <(echo "$expect") <(echo "$result"))\n"
  247. die "livepatch kselftest(s) failed"
  248. fi
  249. cleanup_dmesg_file
  250. }
  251. # check_sysfs_rights(modname, rel_path, expected_rights) - check sysfs
  252. # path permissions
  253. # modname - livepatch module creating the sysfs interface
  254. # rel_path - relative path of the sysfs interface
  255. # expected_rights - expected access rights
  256. function check_sysfs_rights() {
  257. local mod="$1"; shift
  258. local rel_path="$1"; shift
  259. local expected_rights="$1"; shift
  260. local path="$KLP_SYSFS_DIR/$mod/$rel_path"
  261. local rights=$(/bin/stat --format '%A' "$path")
  262. if test "$rights" != "$expected_rights" ; then
  263. die "Unexpected access rights of $path: $expected_rights vs. $rights"
  264. fi
  265. }
  266. # check_sysfs_value(modname, rel_path, expected_value) - check sysfs value
  267. # modname - livepatch module creating the sysfs interface
  268. # rel_path - relative path of the sysfs interface
  269. # expected_value - expected value read from the file
  270. function check_sysfs_value() {
  271. local mod="$1"; shift
  272. local rel_path="$1"; shift
  273. local expected_value="$1"; shift
  274. local path="$KLP_SYSFS_DIR/$mod/$rel_path"
  275. local value=`cat $path`
  276. if test "$value" != "$expected_value" ; then
  277. die "Unexpected value in $path: $expected_value vs. $value"
  278. fi
  279. }