hpc-security-hardener.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/bin/bash
  2. # ==============================================================================
  3. # Script Name: hpc-security-hardener.sh
  4. # Description: Mitigates CopyFail (AF_ALG) and DirtyFrag (ESP/RXRPC) vulnerabilities.
  5. # Architect Note: This script ensures idempotency for HPC cluster deployment.
  6. # ==============================================================================
  7. set -euo pipefail
  8. # --- Professional Logging ---
  9. log_info() { echo -e "\033[32m[INFO]\033[0m $1"; }
  10. log_warn() { echo -e "\033[33m[WARN]\033[0m $1"; }
  11. # --- Ensure Root Privileges ---
  12. if [[ $EUID -ne 0 ]]; then
  13. echo "[ERROR] This script must be run as root"
  14. exit 1
  15. fi
  16. # --- Function: Fix CopyFail (AF_ALG) ---
  17. fix_copy_fail() {
  18. log_info "Starting CopyFail (AF_ALG) mitigation..."
  19. local mod_conf="/etc/modprobe.d/disable-algif-aead.conf"
  20. local blacklist_args="initcall_blacklist=algif_aead_init,af_alg_init"
  21. # 1. Update Kernel Args via grubby (Idempotent check)
  22. # Check if the arguments already exist in the current default kernel info
  23. if ! grubby --info=DEFAULT | grep -q "initcall_blacklist=algif_aead_init"; then
  24. log_info "Appending initcall_blacklist to kernel arguments..."
  25. sudo grubby --update-kernel=ALL --args="$blacklist_args"
  26. else
  27. log_info "Kernel arguments for CopyFail already present. Skipping."
  28. fi
  29. # 2. Disable module loading via modprobe
  30. if [[ ! -f "$mod_conf" ]] || ! grep -q "algif_aead" "$mod_conf"; then
  31. log_info "Creating $mod_conf..."
  32. echo "install algif_aead /bin/false" > "$mod_conf"
  33. else
  34. log_info "Module algif_aead already disabled in modprobe. Skipping."
  35. fi
  36. # 3. Attempt to unload module if currently loaded
  37. if lsmod | grep -q "algif_aead"; then
  38. log_info "Unloading algif_aead module..."
  39. rmmod algif_aead 2>/dev/null || log_warn "Failed to unload algif_aead (it might be in use)."
  40. fi
  41. }
  42. # --- Function: Fix DirtyFrag (ESP/RXRPC) ---
  43. fix_dirty_frag() {
  44. log_info "Starting DirtyFrag (ESP/RXRPC) mitigation..."
  45. local mod_conf="/etc/modprobe.d/dirtyfrag.conf"
  46. local config_content="install esp4 /bin/false\ninstall esp6 /bin/false\ninstall rxrpc /bin/false"
  47. # 1. Disable modules via modprobe
  48. if [[ ! -f "$mod_conf" ]]; then
  49. log_info "Creating $mod_conf..."
  50. printf "$config_content\n" > "$mod_conf"
  51. else
  52. log_info "DirtyFrag modprobe config already exists. Skipping."
  53. fi
  54. # 2. Attempt to unload modules
  55. for mod in esp4 esp6 rxrpc; do
  56. if lsmod | grep -q "^$mod"; then
  57. log_info "Unloading $mod module..."
  58. rmmod "$mod" 2>/dev/null || log_warn "Failed to unload $mod (it might be in use)."
  59. fi
  60. done
  61. }
  62. # --- Function: Memory Sanitization with Timeout ---
  63. clear_caches() {
  64. log_info "Attempting to drop system caches safely..."
  65. # 1. First, try to sync dirty buffers to disk with a timeout.
  66. # If sync hangs, it indicates heavy I/O or storage issues.
  67. if ! timeout 15s sync; then
  68. log_warn "System sync timed out. There might be heavy I/O load. Skipping drop_caches."
  69. return 0
  70. fi
  71. # 2. Write to drop_caches with a timeout.
  72. # We use a subshell to prevent the main script from hanging if the kernel is unresponsive.
  73. if ! timeout 5s sh -c "echo 3 > /proc/sys/vm/drop_caches" 2>/dev/null; then
  74. log_warn "drop_caches operation timed out. Kernel is likely busy reclaiming Slabs."
  75. else
  76. log_info "Caches dropped successfully."
  77. fi
  78. }
  79. # --- Main Execution ---
  80. main() {
  81. log_info "HPC Security Hardening initiated on $(hostname)"
  82. fix_copy_fail
  83. fix_dirty_frag
  84. clear_caches
  85. log_info "Mitigation complete. Note: Some changes (grubby) require a reboot to take effect."
  86. }
  87. main "$@"