brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: fs.protected_regular stops tricks with files other users leave in Linux /tmp

Linux
~/tips/tip-linux-protected-regular-sticky-dirs

sysctl fs.protected_regular fs.protected_fifos fs.protected_symlinks fs.protected_hardlinks

Make sure a program cannot be fooled into writing to a file another user planted in /tmp.

Shared folders like /tmp are a classic place for attacks: one user creates a file with a predictable name, and a program run by another user, often root, opens and writes to it. The kernel has switches that block this pattern.

sysctl fs.protected_regular fs.protected_fifos fs.protected_symlinks fs.protected_hardlinks

The kernel documentation describes protected_regular like this: with 1, a program may not open a regular file with O_CREAT in a world writable sticky folder when someone else owns that file, unless that person also owns the folder. With 2 the same rule covers group writable sticky folders too. protected_fifos does the same for named pipes. The symlink and hardlink switches block the older trick of planting links in those folders.

Most current distributions turn all four on through systemd’s defaults. I would still check them on an older server, a minimal image or a custom kernel:

echo 'fs.protected_regular = 2' | sudo tee /etc/sysctl.d/90-protected.conf
sudo sysctl -p /etc/sysctl.d/90-protected.conf

This can break badly written scripts. A root cron job that writes to /tmp/report.txt, a file first created by another user, now fails with Permission denied. That failure is exactly the risky pattern being blocked. The better fix is in the script: create temp files with mktemp, so the name is unpredictable and the file is new and yours.

Further reading: Documentation for /proc/sys/fs/

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link