๐ก๏ธ Shell Security Hardening
Shell scripts often run with elevated privileges, manipulate system resources, or handle sensitive data. Because the shell environment is inherently permissive and prone to injection, hardening is essential for safe, predictable, productionโgrade automation.
๐ Who This Is For
- DevOps/SRE engineers maintaining production systems
- Sysadmins writing privileged automation
- Developers handling secrets, tokens, or user input
- Anyone needing to reduce attack surface in shell scripts
๐งฉ Role in the Ecosystem
Shell scripts frequently:
- Run as root
- Execute external commands
- Parse user input
- Handle secrets
- Interact with system services
- Modify files, permissions, and devices
Without hardening, they are vulnerable to:
- Command injection
- Path hijacking
- Unsafe temporary files
- Leaked secrets
- Privilege escalation
- Race conditions
Hardening reduces these risks through defensive coding practices.
๐งฉ Key Concepts
1. Safe Defaults
Shells do not fail fast, sanitize input, or protect secrets by default.
2. Attack Surface
Every external command, file write, or environment variable is a potential vector.
3. Trust Boundaries
Scripts must assume all input is untrusted unless explicitly validated.
4. Privilege Minimization
Run with the lowest privileges required.
๐ง Techniques
Safe PATH
1 2 | |
Avoid including . or untrusted directories.
Safe Temporary Files
1 2 | |
Never use predictable filenames.
Avoid eval
1 2 3 4 5 | |
Quote Everything
1 | |
Prevents word splitting and glob expansion.
Validate Input
1 2 3 4 | |
Drop Privileges
1 | |
Use Absolute Paths
1 | |
Prevents PATH hijacking.
Disable Globs When Needed
1 | |
Use umask
1 | |
Restricts default file permissions.
Avoid Leaking Secrets
Disable tracing around sensitive code:
1 2 3 | |
See also: Secure Handling of Secrets
Use Traps for Cleanup
1 | |
See also: Traps & Signals
โ ๏ธ Limitations & Pitfalls
- Shell cannot enforce strict typing
- No sandboxing by default
- No builtโin input validation
set -ebehaves inconsistently across shells- Temporary files may persist after crashes
- PATH hijacking is common in misconfigured systems
- Globs and word splitting cause silent bugs
๐ง When to Use Hardening
- Scripts running as root
- CI/CD pipelines handling secrets
- Automation modifying system state
- Multiโuser environments
- Scripts exposed to user input
- Productionโgrade tooling
โ When Not to Use Shell
- Complex securityโsensitive applications
- Cryptographic operations
- Multiโthreaded or asynchronous workloads
- Highโperformance data processing
Use a real language (Python, Go, Rust) for these cases.
โ Best Practices
- Always quote variables
- Use absolute paths
- Restrict PATH
- Validate all input
- Use
mktempfor temporary files - Avoid
eval - Use traps for cleanup
- Drop privileges when possible
- Disable tracing around secrets
- Use
umask 077for sensitive operations - Test scripts under multiple shells
๐งช Testing Hardening
Check PATH safety:
1 | |
Check file permissions:
1 | |
Check for unquoted variables:
1 | |
Check for eval usage:
1 | |
๐ง Summary
Shell security hardening reduces attack surface by enforcing safe defaults, validating input, avoiding dangerous constructs, and protecting secrets. These practices are essential for writing safe, predictable, productionโready automation.