Sub topic 18
File permissions and system maintenance checks
This chapter covers the CIS checks that confirm sensitive files have correct owners and modes, and that no stray files or accounts have crept onto the system. It assumes you can read ls -l output, understand setuid bits and the sticky bit, and have scanned a host with OpenSCAP as shown in the assessment chapter.
What this section of the benchmark checks
The "System Maintenance" section of the CIS benchmarks for Linux is mostly audit work. Many recommendations are marked manual or have no safe automatic fix, because the right answer depends on what the server runs. The checks fall into three groups:
- Permissions on account and authentication files such as
/etc/passwd,/etc/shadow,/etc/group,/etc/gshadowand their backup copies ending in-. - Filesystem hygiene: world-writable files, files with no owner, and setuid or setgid programs.
- Account consistency: duplicate UIDs, GIDs and names, groups referenced but not defined, and accounts with an empty password field.
Exact numbering and expected modes differ between benchmarks and versions. Treat the numbers below as typical values and confirm them in the benchmark for your distribution.
Expected permissions on account files
The table lists typical expectations. On Debian and Ubuntu, /etc/shadow is usually root:shadow with mode 0640. On RHEL-family systems it is root:root with mode 0000, because root bypasses permission checks.
| File | Typical owner | Typical maximum mode | Why |
|---|---|---|---|
| `/etc/passwd` | root:root | 0644 | Many programs must read it, so it cannot be private. |
| `/etc/group` | root:root | 0644 | Same as `/etc/passwd`. |
| `/etc/shadow` | root:root or root:shadow | 0000 or 0640 | Holds password hashes. |
| `/etc/gshadow` | root:root or root:shadow | 0000 or 0640 | Holds group password hashes. |
| `/etc/passwd-` and the other backup files | root | same as the original or stricter | Backups hold the same data as the originals. |
| `/etc/ssh/sshd_config` | root:root | 0600 | Limits who can read server settings. |
A "maximum mode" means the file may be stricter than the value but not looser. A script should therefore compare bits, not test for equality.
#!/usr/bin/env bash
# check-account-perms.sh: report account files that are looser than expected.
set -u
check() {
local file=$1 maxmode=$2 owner=$3
[ -e "$file" ] || { echo "MISSING $file"; return; }
local mode own
mode=$(stat -c '%a' "$file")
own=$(stat -c '%U' "$file")
# Any permission bit set in the file but not allowed by maxmode is a failure.
if [ $(( 8#$mode & ~8#$maxmode & 8#7777 )) -ne 0 ]; then
echo "FAIL $file mode=$mode (allowed $maxmode)"
elif [ "$own" != "$owner" ]; then
echo "FAIL $file owner=$own (expected $owner)"
else
echo "ok $file"
fi
}
check /etc/passwd 644 root
check /etc/group 644 root
check /etc/shadow 640 root
check /etc/gshadow 640 rootRun it as root. Here 640 for /etc/shadow is the looser Debian-style limit. On RHEL, pass 000 instead, and note that the script checks only the owner name, not the group.
To fix a failure, change the mode and then re-run the check:
sudo chmod 0640 /etc/shadow
sudo chown root:shadow /etc/shadow # use root:root on RHEL-family systems
stat -c '%a %U:%G %n' /etc/shadowFinding risky files
Three searches find the files most likely to cause trouble. Each one stays on a single filesystem with -xdev, so it does not walk into /proc, network mounts or other large trees. Run the search once per mounted local filesystem, or list the mount points explicitly.
# World-writable files, excluding directories that have the sticky bit
sudo find / -xdev -type f -perm -0002 -ls
# World-writable directories without the sticky bit
sudo find / -xdev -type d -perm -0002 ! -perm -1000 -ls
# Files with no valid owner or group
sudo find / -xdev \( -nouser -o -nogroup \) -lsA world-writable directory with the sticky bit, such as /tmp, is normal. The sticky bit means that only a file's owner (or root) can delete or rename it. A world-writable directory without that bit lets any user remove other users' files, and CIS flags it.
Files with no owner usually come from a deleted account whose files were left behind, or from a restored backup. Do not delete them blindly. Identify them, then either assign them to a proper owner or remove them after confirming they are not needed.
Setuid and setgid programs
A setuid program runs with its owner's privileges, normally root. Each one is a possible route to privilege escalation. CIS asks you to review the list, not to remove every entry, since programs like sudo, passwd and su need the bit.
sudo find / -xdev -type f \( -perm -4000 -o -perm -2000 \) -lsSave this output as a baseline. When you re-run it later and the list differs, you have found something worth investigating: a new package, or an unexpected binary. The AIDE chapter covers automated change tracking, but a saved list and diff already catches most surprises.
sudo find / -xdev -type f \( -perm -4000 -o -perm -2000 \) | sort > /var/tmp/suid-baseline.txt
# Later:
sudo find / -xdev -type f \( -perm -4000 -o -perm -2000 \) | sort | diff /var/tmp/suid-baseline.txt -Keep the baseline somewhere an attacker with root cannot quietly edit it, such as a central log server or a read-only store. A baseline on the same host protects only against accidents.
Account consistency checks
These checks look for mistakes in the account databases. Each command prints nothing when the system is healthy.
# Accounts with an empty password field
sudo awk -F: '($2 == "") { print $1 " has an empty password" }' /etc/shadow
# Duplicate UIDs
cut -d: -f3 /etc/passwd | sort -n | uniq -d
# Duplicate GIDs
cut -d: -f3 /etc/group | sort -n | uniq -d
# Duplicate user names
cut -d: -f1 /etc/passwd | sort | uniq -d
# UID 0 accounts other than root
awk -F: '($3 == 0 && $1 != "root") { print $1 }' /etc/passwdDuplicate UIDs matter because the kernel sees only the number. Two names sharing UID 1001 have the same file access, which defeats auditing and per-user limits. Only root should have UID 0.
Linux also ships tools that verify the databases for you. pwck checks /etc/passwd and /etc/shadow, and grpck checks the group files. Run them in read-only mode first:
sudo pwck -r
sudo grpck -rWhere these checks fit in the workflow
Run them in the same order each time: scan, review findings, fix, then re-scan.
For an unexpected setuid binary, ask the package manager who owns it: rpm -qf /path/to/file on RHEL-family systems, or dpkg -S /path/to/file on Debian and Ubuntu. A file that no package owns deserves the closest look.
Common mistakes
- Setting
/etc/shadowto0000on a Debian system. Some services that run as theshadowgroup, such as certain authentication helpers, then fail. Match your distribution's default or its benchmark. - Running
findwithout-xdev. It crawls/proc, NFS mounts and container overlay layers, which is slow and generates false findings. - Removing the setuid bit from
sudo,suorpasswdto pass a check. Logins and privilege changes then break. - Changing permissions on a package-owned file and forgetting that a package update can reset it. Use
rpm -Vordpkg --verifyto see what the package manager thinks has changed, and use the distribution's override mechanism, such asdpkg-statoverride, when a change must persist.
Exercises
Exercise
A scan reports /var/log/app as a world-writable directory without the sticky bit. List two safe responses, and one unsafe response.
Show answer
Safe: remove the world-write bit with chmod o-w /var/log/app after confirming the application runs as its own user or group, or set the sticky bit with chmod +t if shared writing is really required. Unsafe: running chmod -R 777 or chmod -R 000 on /var/log, which changes unrelated files and breaks logging.
Exercise
Your find for setuid files shows /usr/local/bin/backup-helper, which no package owns. What do you do next?
Show answer
Identify what it is and who installed it: check ls -l, the file's modification time, and file or strings output, and ask the team that manages backups. If it is needed, record it as an approved exception and add it to the setuid baseline. If nobody can explain it, remove the setuid bit or the file, and treat it as a possible compromise by checking logs and AIDE reports.
Exercise
Why does a permission check script compare permission bits instead of checking that the mode equals 0640?
Show answer
CIS limits are maximums. A file with mode 0600 or 0000 is stricter than 0640 and passes. Testing for equality would report safe files as failures, while bitwise checking flags only modes that grant a permission the limit does not allow.