Notes
The bits
Every file has an owner, a group and nine permission bits: read, write and execute, for the owner, the group and everyone else. Each set is a number. Read is four, write two, execute one, so read plus write is six. Mode six four zero means the owner can read and write, the group can read, and others get nothing. Six zero zero keeps a file to its owner alone, which is what you want for anything holding a secret.
- rw- r-- ---
owner group other
r=4 w=2 x=1
rw-=6 r--=4 ---=0
640 = owner rw, group r,
others nothing
600 = owner only
$ chmod 600 secret.txt The check
When you open a file the kernel checks in a fixed order and stops at the first match. Are you the owner? It uses the owner bits and stops. Otherwise, are you in the file's group? It uses the group bits. Otherwise the others bits. Because it stops early, a strange mode like six zero four lets everyone else read a file while its own group cannot, which surprises people and occasionally leaks data. Root skips all of this and can read anything.
- are you the owner? use the owner bits, stop
- else in the group? use the group bits, stop
- else use the others bits
- so a file 604 lets *others* read even if the group cannot: order matters
What attackers look for
Once an attacker has any shell, the first thing they do is look for permission mistakes. A config file that holds a database password and is readable by everyone. A private key left at six four four instead of six zero zero. A script that a root service runs on a schedule but that any user can edit, so they put their own commands in it. One find command lists every world-writable file. As a defender, you run that command first, and fix what it shows.
- world-readable config with passwords or API keys
- private keys readable by all (should be 600)
- world-writable scripts a service runs as root
- a writable cron job or systemd unit
- find / -perm -0002 -type f
The SUID bit
Now the dangerous part. The SUID bit, shown as an s where the owner's execute bit would be, makes a program run as its owner rather than as whoever started it. passwd is owned by root and SUID, so when you change your password, the program runs as root and can edit the shadow file, which you cannot touch directly. sudo, su and a few others work the same way. SUID is useful and necessary, and it is also the single most abused feature in Linux privilege escalation.
$ ls -l /usr/bin/passwd
-rwsr-xr-x root root
^ s = SUID
you run it, it runs as root
that is how you change your
own password in a
root-only file
sudo, su, ping: also SUID Why SUID is risky
Here is the pattern, without a live demo. If a program is SUID root and can be made to run another command, open a shell, or write to any file, then a normal user can borrow its root powers and become root. Attackers keep a catalogue of standard tools that can be tricked this way when they are SUID. The usual cause is an engineer who set SUID on a custom script to avoid a permissions headache. Defenders list every SUID file with one find command, and remove the bit from anything that does not strictly need it.
- a SUID-root program that can run another command, open a shell, or write any file = instant root
- classic: a custom tool left SUID "to make it easy"
- defenders audit the list: find / -perm -4000 -type f
- remove SUID you do not need
sudo
sudo lets a user run a command as root, and records it. The right way is least privilege: grant one exact command, not full root. Here the deploy user may restart one service and nothing else. The wrong way is to grant a broad command, or a program with a shell escape, or all commands. Each of those quietly becomes full root for that user. And read the logs: every sudo use is written to the auth log, which is both an audit trail and an early warning.
# /etc/sudoers.d/deploy deploy ALL=(root) NOPASSWD: \ /usr/bin/systemctl restart shoprung one exact command, nothing else every use is logged to /var/log/auth.log
sudo mistakes
The traps in sudo are rules that look narrow but are not. Allowing an editor, because many editors can launch a shell. Allowing a language interpreter, because it runs any code. Allowing an archive tool with a run-a-command option. Or a wildcard matching more than intended. Each hands over full root. A public reference, GTFOBins, lists which binaries become root when granted through sudo or SUID; defenders check their sudoers against it.
- ALL=(ALL) ALL everything
- NOPASSWD on an editor, find, a scripting language, or tar/zip with a run option
- NOPASSWD on vim: :!sh
- a wildcard that matches more than you think
- grant the exact binary and arguments, read GTFOBins
Defender checklist
A short routine closes most of this. Keep secrets and private keys at six zero zero or six four zero. Remove others' write access from scripts and configs, and audit for world-writable files. List every SUID and SGID file and strip the bit from anything that does not need it. Write sudo rules for exact commands, never editors or interpreters. Mount scratch filesystems like slash tmp so programs there cannot run or be SUID, and read the auth log.
- secrets and keys at 600, 640
- no world-writable files or scripts; audit -perm -0002
- list SUID/SGID, remove what is not needed
- sudo rules: exact commands, no editors or interpreters
- mount /tmp noexec,nosuid
- read auth.log for sudo use
Common mistakes
Mistakes. Running chmod seven seven seven to make an error go away, which makes the file writable by everyone. Setting SUID on a custom script to dodge a permissions issue. Leaving secrets world-readable. Granting full sudo for convenience. Running services as root when a dedicated low-privilege user would do. Never auditing the SUID list, so a planted SUID shell sits unnoticed. And never reading the auth log, where every escalation attempt is recorded.
- chmod 777 to "fix" a problem
- SUID on a custom script
- secrets world-readable
- sudo ALL=(ALL) for convenience
- services running as root
- ignoring the SUID inventory
- never reading auth.log
Say it like this
In the interview: Linux privilege escalation usually comes from permission mistakes. A SUID root binary that can run another command. A sudo rule that grants an editor, an interpreter or an archive tool, each of which can spawn a shell. A world-writable file or script that a root process trusts. I defend by auditing the SUID list and the sudoers rules against references like GTFOBins, keeping least privilege, running services as their own users, and mounting scratch directories without SUID or execute.
- "Through permission mistakes: a SUID-root binary that can
- run a command, a sudo rule
- granting an editor or interpreter, a world-writable
- file a root process uses. I
- audit SUID and sudoers, keep
- least privilege, and mount
- scratch dirs nosuid."