The Threat Lurking in Temp Directories: A Classic Malware Drop Scenario
Linux system administrators face this scenario all too often: an arbitrary file upload or Remote Code Execution (RCE) vulnerability is exploited in a PHP, Node.js, or Python application. The attacker immediately downloads an ELF malware binary (such as Mirai or an XMRig cryptominer) into /tmp, /var/tmp, or /dev/shm. Next, they assign execute permissions via chmod +x and spawn the malicious process.
By default, Linux is very permissive. The operating system allows executing any file as long as the user owning it has execution rights. Within just five seconds, your production server can be transformed into a zombie botnet node.
Weighing the Options: Three Approaches to Blocking Malware Execution
When looking to prevent unauthorized file execution, sysadmins generally consider three common approaches:
1. Mounting Partitions with the noexec Flag
The traditional method involves isolating /tmp onto its own partition and mounting it with the noexec,nosuid,nodev flags in /etc/fstab.
- Pros: Takes two minutes to set up with virtually zero CPU or RAM overhead.
- Fatal Flaw: Trivial to bypass. An attacker simply invokes the dynamic linker:
/lib64/ld-linux-x86-64.so.2 /tmp/malware, and the binary executes without issue. Furthermore, attackers can still drop files into web application directories (such as/var/www/uploads/) and run them from there.
2. Using SELinux or AppArmor
Mandatory Access Control (MAC) mechanisms enforce permissions based on policies and security contexts.
- Pros: Built directly into the kernel, providing robust process isolation for services.
- Cons: Not originally designed for comprehensive Application Whitelisting (AWL). SELinux governs access by process domain. Writing and maintaining custom policies for dozens of internal scripts and custom-built binaries requires substantial overhead.
3. FAPolicyd (File Access Policy Daemon)
FAPolicyd operates on top of the Linux kernel’s fanotify subsystem. Its mechanism is straightforward: only binaries, scripts, or libraries registered in a trusted list are allowed to execute or open for reading.
- Pros: Blocks execution directly at the kernel level before a process can fork. Automatically synchronizes its whitelist whenever packages are installed via DNF/RPM. Completely neutralizes dynamic linker bypass techniques.
- Cons: Requires deliberate management of trusted lists for custom-built applications. Consumes around 80MB to 120MB of RAM to maintain the hash database for 30,000 to 50,000 system files, though practical CPU overhead remains under 2%.
Why FAPolicyd Is an Ideal Fit for CentOS Stream 9 and AlmaLinux 9
FAPolicyd has been officially supported by Red Hat since RHEL 8.4 and is mature on RHEL/CentOS Stream 9. Rather than requiring you to manually catalog tens of thousands of binaries, the daemon reads directly from the RPM database (rpmdb) to build its initial trust baseline. This delivers a defense layer compliant with NIST SP 800-167 without disrupting CI/CD pipelines or package-managed deployments.
Step-by-Step Practical Deployment of FAPolicyd
Step 1: Install the Package
The package is available in the default repositories of CentOS Stream 9. Run:
sudo dnf install -y fapolicyd
Step 2: Enable Permissive Mode
The biggest mistake is jumping straight into enforcing mode. If your server relies on cron jobs, Go daemons, or custom-compiled binaries, the system may block legitimate files and disrupt services.
Edit the configuration file /etc/fapolicyd/fapolicyd.conf:
sudo vi /etc/fapolicyd/fapolicyd.conf
Change the permissive setting from 0 to 1:
permissive = 1
In this mode, FAPolicyd logs policy violation warnings without actually blocking file access.
Step 3: Start the Service and Verify the Database
Enable and start the service:
sudo systemctl enable --now fapolicyd
sudo systemctl status fapolicyd
Verify the number of files populated into the trust database:
sudo fapolicyd-cli --list
The command outputs all files managed by RPM. This count typically falls between 35,000 and 60,000 objects, depending on the packages installed on the system.
Step 4: Register In-House Applications in the Trust List
Suppose you have a custom-compiled Go application located at /opt/myapp/bin/worker. Because this file did not originate from an RPM package, FAPolicyd treats it as untrusted by default.
Add the file to the trust list:
sudo fapolicyd-cli --file add /opt/myapp/bin/worker
If the entire application resides within a directory, you can trust the whole directory path:
sudo fapolicyd-cli --file add /opt/myapp/
Update the database to compute and store SHA256 hashes:
sudo fapolicyd-cli --update
Manually registered trust definitions are stored in /etc/fapolicyd/fapolicyd.trust.
Step 5: Switch to Enforcing Mode
After running in permissive mode for 3 to 5 days, review the logs to check for false positives on legitimate workloads (journalctl -u fapolicyd | grep 'rule=deny'). Once everything looks clean, switch to active enforcement.
Open /etc/fapolicyd/fapolicyd.conf and restore the setting:
permissive = 0
Restart the service:
sudo systemctl restart fapolicyd
Testing: Simulating an Attack in /tmp
Assume the role of an attacker who has just successfully exploited an RCE vulnerability. Write a simple C program and compile it directly inside /tmp:
cat << 'EOF' > /tmp/exploit.c
#include <stdio.h>
int main() {
printf("Hacked! Payload executed.\n");
return 0;
}
EOF
gcc /tmp/exploit.c -o /tmp/exploit
chmod +x /tmp/exploit
/tmp/exploit
Upon execution, the kernel immediately halts the process:
bash: /tmp/exploit: Operation not permitted
Next, attempt the dynamic linker bypass technique:
/lib64/ld-linux-x86-64.so.2 /tmp/exploit
# Output: /lib64/ld-linux-x86-64.so.2: error while loading shared libraries: /tmp/exploit: Operation not permitted
Even when executed under root privileges, the action is blocked. Inspect the real-time logs:
sudo journalctl -u fapolicyd -n 20 --no-pager
The logs capture precise details: invoking process, user UID, SHA256 hash of the payload, and the rejection rule (rule=deny).
Field-Tested Tips for Production Operations
- Updating the OS via DNF: The
dnf-plugin-post-transaction-actionsplugin typically triggers automated hash updates. However, after substantialdnf updateruns, it is good practice to executesudo fapolicyd-cli --updateto ensure immediate database synchronization. - Troubleshooting Blocked Applications: If an in-house app mysteriously crashes after enabling fapolicyd, stop the service and run it directly in debug mode:
sudo fapolicyd --debug 2>&1 | grep -i deny. You will immediately pinpoint which shared library (.so) or child script is being blocked. - CI/CD Pipelines: On servers with continuous deployments, create dedicated trust files inside
/etc/fapolicyd/trust.d/rather than modifying the base operating system configuration files directly.
