Operating System CentOS

Secure /tmp and /dev/shm on AlmaLinux 9 and Rocky Linux 9 (noexec, nosuid, nodev)

Harden /tmp, /var/tmp and /dev/shm on the RHEL 9 family with noexec, nosuid and nodev mount options in /etc/fstab, verify the result, and know what can break.

World-writable directories such as /tmp, /var/tmp and /dev/shm are the first place an attacker with a foothold drops a script or binary. Mounting them with noexec, nosuid and nodev stops files there from being executed directly, from gaining privileges through set-UID bits and from acting as device nodes. This guide applies those options on AlmaLinux 9, Rocky Linux 9 and CentOS Stream 9 using /etc/fstab, and explains what can break.

CentOS 6 reached end of life in November 2020. Older tutorials for it create a loopback image file for /tmp and symlink /var/tmp to it; on a systemd-based release neither trick is needed, and the symlink is a bad idea.

Requirements

  • A server from the RHEL 9 family and a user with sudo rights.
  • Console access through your provider's web console, in case a mistake in /etc/fstab stops the system from booting.
  • A backup copy of the file before you touch it:
sudo cp /etc/fstab /etc/fstab.bak

Check how the directories are mounted now

findmnt /dev/shm
findmnt /tmp
findmnt /var/tmp

/dev/shm is always a tmpfs that systemd mounts early in boot, normally with nosuid,nodev but without noexec. If findmnt /tmp prints nothing, /tmp is an ordinary directory on the root file system and has no mount options of its own. That is the usual situation on a cloud image. If it prints a line, /tmp is already a separate partition or a tmpfs.

Harden /dev/shm

systemd mounts /dev/shm itself, but it applies whatever options you list for it in /etc/fstab. Open the file:

sudo nano /etc/fstab

Add this line (or, if a /dev/shm line already exists, add the three options to it):

tmpfs  /dev/shm  tmpfs  defaults,nosuid,nodev,noexec  0 0

Tell systemd that the file changed and apply the options without a reboot:

sudo systemctl daemon-reload
sudo mount -o remount /dev/shm

Check the result:

findmnt -no OPTIONS /dev/shm

The list should now contain nosuid,nodev,noexec.

Harden /tmp

Which change you make depends on what the check above showed.

/tmp is already a separate partition

Find its line in /etc/fstab and add the options to the fourth field, for example changing defaults to defaults,nosuid,nodev,noexec. Then:

sudo systemctl daemon-reload
sudo mount -o remount /tmp

/tmp is a plain directory: mount a tmpfs on it

The simplest way to give /tmp its own mount options is a memory-backed tmpfs. Add this line to /etc/fstab:

tmpfs  /tmp  tmpfs  defaults,nosuid,nodev,noexec,size=2G,mode=1777  0 0

mode=1777 keeps the directory world-writable with the sticky bit, and size= caps how much it may hold. Set the size deliberately: a tmpfs lives in the page cache and swap, so every file in /tmp counts against memory, and without size= the limit is half of your RAM. On a 1 GB server choose something much smaller than 2G, such as size=256M.

Validate the file before relying on it:

sudo findmnt --verify

Then reboot, so that no running service is left holding sockets or files in the old, now hidden, /tmp directory:

sudo systemctl reboot

Anything that was in the old directory stays on the root file system underneath the mount, invisible, and everything in the new /tmp is lost at each reboot.

systemd also ships a unit called tmp.mount that mounts a tmpfs on /tmp, with nosuid,nodev but not noexec. You do not need to enable it: when a mount point is configured both in /etc/fstab and in a unit file under /usr, the fstab entry takes precedence. See systemctl status tmp.mount for its current state on your system.

What about /var/tmp?

/var/tmp is meant for temporary files that survive a reboot, so do not put it on a tmpfs and do not replace it with a symlink to /tmp. If findmnt /var/tmp shows that it is a separate partition or logical volume, add nosuid,nodev,noexec to its fstab line and remount it exactly as for /tmp. If it is a plain directory on the root file system, the options cannot be applied without giving it its own file system; the security benchmarks for RHEL 9 recommend doing that at installation time or by adding a logical volume.

Verify that execution is blocked

Create a small script in /tmp and try to run it:

printf '#!/bin/sh\necho executed\n' > /tmp/exec-test.sh
chmod +x /tmp/exec-test.sh
/tmp/exec-test.sh

You should get "Permission denied". Clean up afterwards:

rm /tmp/exec-test.sh

Confirm the options survive a reboot with findmnt -no OPTIONS /tmp and findmnt -no OPTIONS /dev/shm.

Side effects to expect

  • Software that executes from /tmp. Some installers, build tools and applications unpack a helper program or native library into /tmp and run it from there. With noexec they fail with "Permission denied". Most honour the TMPDIR environment variable, so point them at a directory that allows execution, or lift the restriction for the duration of the job with sudo mount -o remount,exec /tmp and restore it with sudo mount -o remount,noexec /tmp.
  • Memory pressure. With /tmp on tmpfs, a process that writes large temporary files consumes RAM and swap. When the tmpfs is full, writes fail with "No space left on device" even though the disk has room.
  • Nothing persists. Files in a tmpfs /tmp vanish at reboot. Software that needs them to survive should use /var/tmp.
  • /dev/shm and noexec. A few programs map shared memory from /dev/shm as executable. If something stops working right after this change, test it with sudo mount -o remount,exec /dev/shm before looking elsewhere.

To undo everything, restore /etc/fstab.bak over /etc/fstab and reboot. The systemd project describes how it treats these mounts in its API file systems document.

Frequently asked questions

Does noexec stop every script in /tmp from running?

No. It blocks executing a file directly. Anyone who can already run an interpreter can still type sh /tmp/script.sh or python3 /tmp/script.py, because the interpreter lives elsewhere and only reads the file. Treat noexec as one layer that defeats common automated attacks, alongside SELinux, updates and a firewall.

Can I change the size of a tmpfs /tmp later?

Yes. Edit the size= value in /etc/fstab, run sudo systemctl daemon-reload and then sudo mount -o remount /tmp. A tmpfs can be resized while in use, though not below the amount of data it currently holds.

The server no longer boots after I edited fstab. What now?

Open your provider's web console, log in to the emergency shell the system offers (or boot a rescue image), and copy /etc/fstab.bak back over /etc/fstab. A typo in the options field is the usual cause, which is why running findmnt --verify before rebooting is worth the few seconds.

Share story:

Continue reading other guides on How VPS.