mitigation-patching.sh 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. TIMEOUT=10
  4. function do_one
  5. {
  6. local mitigation="$1"
  7. local orig
  8. local start
  9. local now
  10. orig=$(cat "$mitigation")
  11. start=$(date +%s)
  12. now=$start
  13. while [[ $((now-start)) -lt "$TIMEOUT" ]]
  14. do
  15. echo 0 > "$mitigation"
  16. echo 1 > "$mitigation"
  17. now=$(date +%s)
  18. done
  19. echo "$orig" > "$mitigation"
  20. }
  21. rc=0
  22. cd /sys/kernel/debug/powerpc || rc=1
  23. if [[ "$rc" -ne 0 ]]; then
  24. echo "Error: couldn't cd to /sys/kernel/debug/powerpc" >&2
  25. exit 1
  26. fi
  27. tainted=$(cat /proc/sys/kernel/tainted)
  28. if [[ "$tainted" -ne 0 ]]; then
  29. echo "Error: kernel already tainted!" >&2
  30. exit 1
  31. fi
  32. mitigations="barrier_nospec stf_barrier count_cache_flush rfi_flush entry_flush uaccess_flush"
  33. for m in $mitigations
  34. do
  35. if [[ -f /sys/kernel/debug/powerpc/$m ]]
  36. then
  37. do_one "$m" &
  38. fi
  39. done
  40. echo "Spawned threads enabling/disabling mitigations ..."
  41. if stress-ng > /dev/null 2>&1; then
  42. stress="stress-ng"
  43. elif stress > /dev/null 2>&1; then
  44. stress="stress"
  45. else
  46. stress=""
  47. fi
  48. if [[ -n "$stress" ]]; then
  49. "$stress" -m "$(nproc)" -t "$TIMEOUT" &
  50. echo "Spawned VM stressors ..."
  51. fi
  52. echo "Waiting for timeout ..."
  53. wait
  54. tainted=$(cat /proc/sys/kernel/tainted)
  55. if [[ "$tainted" -ne 0 ]]; then
  56. echo "Error: kernel became tainted!" >&2
  57. exit 1
  58. fi
  59. echo "OK"
  60. exit 0