functions.sh 11 KB

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