hpc-security-hardener.sh 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 ---
  63. clear_caches() {
  64. log_info "Dropping system caches to ensure memory purity..."
  65. sync
  66. echo 3 > /proc/sys/vm/drop_caches
  67. }
  68. # --- Main Execution ---
  69. main() {
  70. log_info "HPC Security Hardening initiated on $(hostname)"
  71. fix_copy_fail
  72. fix_dirty_frag
  73. clear_caches
  74. log_info "Mitigation complete. Note: Some changes (grubby) require a reboot to take effect."
  75. }
  76. main "$@"