Sub topic 5
Filesystem hardening: kernel modules, partitions, and mount options
This chapter covers the first technical section of the CIS benchmark for a Linux server: restricting which filesystems the kernel can load, separating key directories onto their own partitions, and applying restrictive mount options. It assumes you can edit files as root, read /etc/fstab, and have a test machine or snapshot to work on. The examples target RHEL 9, but the same ideas apply to Debian and Ubuntu benchmarks.
Why the filesystem comes first
Attackers who get a foothold usually need somewhere to write files and something to run. A world-writable directory such as /tmp is the easiest place to do both. Filesystem hardening removes capabilities the server does not need. It does this in three layers:
- Kernel modules: stop rarely used filesystem drivers from loading.
- Partitions: put directories that grow or accept user data on separate filesystems.
- Mount options: tell the kernel what may happen on each mounted filesystem.
Disabling unused filesystem modules
The kernel can load drivers for many filesystems on demand. If a user mounts a USB stick, or an attacker triggers a mount, the kernel may load a driver nobody reviewed. Old or exotic drivers such as cramfs, squashfs, udf, and freevxfs have had vulnerabilities, and a typical server never needs them.
The CIS pattern uses two directives in a file under /etc/modprobe.d/:
# /etc/modprobe.d/cramfs.conf
install cramfs /bin/false
blacklist cramfs
# Verify: the module should not be loaded, and a dry run should show the override
lsmod | grep cramfs
modprobe -n -v cramfsThe two lines do different jobs:
install cramfs /bin/falsereplaces the load command. If anything asks for the module,modproberuns/bin/false, which fails, so the module stays out.blacklist cramfsstops automatic loading by alias. It does not block an explicitmodprobe cramfsby itself, which is why CIS pairs it withinstall.
If lsmod shows the module is already loaded, the config file does not unload it. Run sudo modprobe -r cramfs first, or reboot. If the module is in use by a mounted filesystem, unmount that first.
Separate partitions
CIS recommends separate filesystems for /tmp, /var, /var/tmp, /var/log, /var/log/audit, and /home, plus /dev/shm handled with mount options. The reasons are practical:
- A full
/var/logcannot fill the root filesystem and crash the system. - Each separate filesystem can have its own mount options.
- User-writable areas such as
/homeand/tmpcan be kept away from system binaries.
Check the current layout first:
findmnt --real
df -h /tmp /var /var/log /var/log/audit /homeRepartitioning an existing server is the most disruptive change in this chapter. On a new build, set the layout in the installer or kickstart. On an existing server with LVM, you can often add a logical volume and migrate data. On a server without free space, the OpenSCAP rule for a separate partition is a good candidate for a documented exception. A systemd tmpfs mount for /tmp is a cheap alternative that satisfies the /tmp rule without repartitioning.
Mount options
Mount options restrict what the kernel allows on a filesystem. The three that CIS uses most:
| Option | Effect | Typical use |
|---|---|---|
| `nodev` | Device files on this filesystem are ignored | Everything except `/` and `/dev` |
| `nosuid` | The setuid and setgid bits are not honored | Everything user-writable |
| `noexec` | Files cannot be executed directly | `/tmp`, `/var/tmp`, `/dev/shm` |
This /etc/fstab line mounts /tmp as a size-limited tmpfs with all three:
# /etc/fstab entry for /tmp with restrictive mount options
tmpfs /tmp tmpfs defaults,rw,nosuid,nodev,noexec,relatime,size=2G 0 0
# Apply without rebooting, then check the options
mount -o remount /tmp
findmnt --target /tmpfindmnt --target /tmp prints the options the kernel is actually using. Always trust that output rather than the contents of /etc/fstab, because a typo or an unit override can make them differ.
Some points to check when you apply these options:
size=2Gcaps usage of thetmpfs. Atmpfsuses RAM and swap, so size it against available memory.- On systems where systemd manages
/tmpthroughtmp.mount, edit that unit (systemctl edit tmp.mount) instead of/etc/fstab, or the two will conflict. /var/tmpoften needs the same options. Many administrators bind-mount it to/tmpor give it its own line./dev/shmis already atmpfs. Add a/etc/fstabline for it withnosuid,nodev,noexecand remount.
noexec is not a complete defense. A user can still run /lib64/ld-linux-x86-64.so.2 /tmp/binary, or run scripts through an interpreter (bash /tmp/script.sh). It raises the cost of simple attacks. It does not stop a determined one, so treat it as one layer among several.
Verifying with a scan
After changes, scan the host again and compare. The following command runs the CIS Server Level 1 profile and writes an HTML report:
# List the profiles available in the SCAP content, then run the CIS Server Level 1 profile
oscap info /usr/share/xml/scap/ssg/content/ssg-rhel9-ds.xml
sudo oscap xccdf eval \
--profile xccdf_org.ssgproject.content_profile_cis_server_l1 \
--results results.xml \
--report report.html \
/usr/share/xml/scap/ssg/content/ssg-rhel9-ds.xmlIn the report, filter for rules whose names contain kernel_module, partition, or mount_option. A rule that passes shows pass. A rule that fails shows the check output, which names the missing option or module. Remember that passing the scan only means the configuration matches the rule. Test the workload as well.
Common errors
- Editing
/etc/fstaband rebooting without testing. Runsudo mount -aafter every edit. It reports syntax errors and mount failures while you can still fix them. Also runsystemctl daemon-reloadso systemd regenerates its mount units. - Putting the
installline in the wrong place. Files in/etc/modprobe.d/must end in.conf. Others are ignored. - Forgetting the initramfs. If a module is loaded early at boot, rebuild the initramfs (
sudo dracut -fon RHEL,sudo update-initramfs -uon Debian) so the setting applies at boot. - Remounting a filesystem that is busy.
mount -o remountchanges options in place and usually works, but changing a partition's layout needs an unmount and a maintenance window.
Exercise
You add noexec to the /tmp line in /etc/fstab and run mount -o remount /tmp. A colleague says the rule is applied because the file is correct. What command do you run to confirm the kernel is enforcing it, and what in the output do you look for?
Show answer
Run findmnt --target /tmp. In the OPTIONS column, look for noexec (along with nosuid and nodev). The file only shows the intent. findmnt shows the options the running kernel uses, so it catches typos, overrides from a systemd tmp.mount unit, and failed remounts.
Exercise
A server's /etc/modprobe.d/cramfs.conf contains only blacklist cramfs. Why does CIS also require the install cramfs /bin/false line, and how do you check the result?
Show answer
blacklist only stops automatic loading by alias, so a direct modprobe cramfs still loads the module. The install line replaces the load command with /bin/false, so any load attempt fails. Check with modprobe -n -v cramfs, which should show it running /bin/false, and with lsmod | grep cramfs, which should print nothing.