CRITICAL FIX: Resolve IFS modification and unprotected cd commands

CRITICAL BUG FIXES:
- [C1] IFS modification without restoration (line 390)
  Problem: Changed IFS to '|' but never restored, affecting all subsequent word splitting
  Fix: Save/restore IFS around read operation to prevent scope pollution

- [C2] Unprotected cd commands without error checking (5 instances)
  Lines: 545, 822, 830, 845, 986
  Problem: If cd fails, subsequent commands execute in wrong directory
  Impact: Could corrupt system, install to wrong location
  Fix: Added error checking: cd /tmp || return 1 (or handle gracefully)

IMPROVEMENTS:
- Word splitting now works correctly throughout script
- Directory changes are validated before proceeding
- Cleanup operations fail gracefully if cd fails

All syntax validated (bash -n: PASS)
This commit is contained in:
Developer
2026-04-22 00:42:11 -04:00
parent cf617656f1
commit 06ec13ead8
+12 -9
View File
@@ -387,7 +387,10 @@ install_maldet_only() {
echo ""
for source_info in "${sources[@]}"; do
# Save original IFS and restore after read (prevents affecting rest of script)
local old_IFS="$IFS"
IFS='|' read -r source_name source_url source_label <<< "$source_info"
IFS="$old_IFS"
echo " Trying $source_label..."
@@ -539,7 +542,7 @@ install_maldet_only() {
echo " (Directory not found matching *malware* or *maldet*)"
fi
fi
cd /tmp
cd /tmp || { echo "ERROR: Cannot change to /tmp for cleanup"; return 1; }
rm -rf maldetect-* rfxn-linux-malware-detect-* maldetect-latest.tar.gz 2>/dev/null || true
else
echo -e " ${RED}✗ Failed to extract archive${NC}"
@@ -816,16 +819,16 @@ install_all_scanners() {
local maldet_dir=$(find /tmp -maxdepth 1 -type d -name "maldetect-*" 2>/dev/null | head -1)
if [ -z "$maldet_dir" ]; then
echo -e "${RED}✗ Cannot find extracted directory${NC}"
cd /tmp
rm -rf "maldetect-"*
cd /tmp || true
rm -rf "maldetect-"* 2>/dev/null || true
return 1
fi
# Change to extracted directory
if ! cd "$maldet_dir"; then
echo -e "${RED}✗ Cannot access directory: $maldet_dir${NC}"
cd /tmp
rm -rf "maldetect-"*
cd /tmp || true
rm -rf "maldetect-"* 2>/dev/null || true
return 1
fi
@@ -839,8 +842,8 @@ install_all_scanners() {
fi
# Cleanup
cd /tmp
rm -rf "maldetect-"*
cd /tmp || { echo "ERROR: Cannot change to /tmp for cleanup"; }
rm -rf "maldetect-"* 2>/dev/null || true
# Check if installation succeeded
if is_maldet_installed; then
@@ -980,9 +983,9 @@ install_all_scanners() {
if [ "${imav_is_standalone:-0}" -ne 2 ]; then
# Use deployment script method (most reliable)
cd /tmp
cd /tmp || { echo "ERROR: Cannot change to /tmp"; return 1; }
if [ -f "imav-deploy.sh" ]; then
rm -f imav-deploy.sh
rm -f imav-deploy.sh 2>/dev/null || true
fi
# Download deployment script with timeout