TL;DR: Each file has an owner, a group and nine bits: read, write and execute for owner, group and others. The kernel picks exactly one of those three sets (owner if you own it, else group if you are in it, else others) and checks the operation against it. On a directory, read lists names, write creates and deletes entries, and execute lets you traverse it to reach anything inside. The special bits change identity or deletion rules: setuid runs an executable with the file owner's user ID, setgid does the same for the group (and on directories makes new files inherit the group), and the sticky bit on a directory stops users deleting each other's files, as in
/tmp.
How to approach it
Explain the evaluation rule first, because candidates who know the bits often get the rule wrong. Then directories, because that is where most surprises live. Then the special bits and the attack surface they create. Mention ACLs, capabilities and mandatory access control as the layers above.
A strong answer
Request
open for write
Owner?
use owner bits
In group?
use group bits
Others
use other bits
Allow or deny
on that set only
Rules people forget
Directory bits
- Write on a directory deletes others' files unless sticky
- Execute is needed to traverse
Owner with fewer rights
- Owner bits r-- deny write even if others have rw-
setuid and setgid
- Run as the file owner: every setuid root binary is attack surface
Reading the bits. -rw-r----- alice finance report.csv means alice can read and write, members of finance can read, everyone else gets nothing. In octal, read is 4, write 2, execute 1, so that file is 640. A new file's mode is the requested mode with the process's umask bits removed; a umask of 027 turns a requested 666 into 640.
The evaluation rule. The kernel checks the process's effective user ID against the owner; if it matches, only the owner bits apply, even if the group or other bits would allow more. Otherwise, if any of the process's groups matches the file's group, only the group bits apply. Otherwise the other bits apply. So ----rwxrwx owned by alice denies alice everything while granting strangers full access. Root bypasses read and write checks (and execute, if any execute bit is set), which is why so much of Linux security is about not being root, and about capabilities that split root apart.
Directories.
| Bit | On a directory it lets you... |
|---|---|
| read | List the names inside |
| write | Create, rename and delete entries, including files you do not own and cannot read |
| execute | Traverse: use the directory in a path to reach what is inside |
Two consequences that come up in reviews. First, deleting a file needs write on the directory, not on the file. A world-writable directory lets anyone delete or replace anyone's files, which is why shared directories need the sticky bit. Second, to read /a/b/c.txt you need execute on /, /a and /a/b, plus read on the file; a directory with execute but not read lets people open files whose names they already know.
The special bits.
- setuid on an executable: the process runs with an effective user ID equal to the file's owner.
passwdis setuid root so an ordinary user can update the password database it could not otherwise write. Every setuid-root program is privileged code that reads attacker-controlled input (arguments, environment, files, signals), so a bug in it is a privilege escalation. Linux ignores setuid on interpreted scripts because of race conditions in starting the interpreter. - setgid on an executable runs with the file's group; on a directory it makes new files inherit the directory's group, which is useful for shared project directories.
- sticky bit on a directory (
drwxrwxrwton/tmp): only the file's owner, the directory's owner or root may delete or rename an entry. It stops deletion and replacement of other users' files. It does not stop someone from creating a file with a name your program is about to use, which is why predictable temporary file names remain dangerous.
The layers above. POSIX ACLs add named users and groups beyond the single owner and group. Linux capabilities split root's powers into units such as CAP_DAC_OVERRIDE. Mandatory access control (SELinux, AppArmor) applies policy that even the file's owner, and often root, cannot override. Permissions are discretionary: the owner can loosen them, which is exactly what a compromised process running as the owner will do.
How this is audited. find / -perm -4000 -type f lists setuid files; each one should be justified and patched. World-writable files and directories without the sticky bit, writable files in PATH, and writable scripts run by root's cron jobs are the classic findings.
What interviewers probe next
- "A file is mode 000 and owned by you. Can you read it?" Not until you change its mode, which you can, because the owner can always chmod. Root can read it regardless.
- "Why is a setuid-root program that calls
system()dangerous?" It runs a shell that honours attacker-influenced environment andPATH, so the attacker chooses what runs as root. - "What is the difference between real and effective user ID?" Real is who started the process; effective is what permission checks use. A setuid program can drop back to the real ID once it has done its privileged step.
What if?
What if · interview follow-ups
Hold your answer as the constraints change
A deployment script leaves /opt/app/config.yaml with mode 0644 and /opt/app/bin owned by the deploy user, while the service runs as root.
Start here
How does the kernel choose which permission bits apply?
Does a user get the best of the three sets?
It picks exactly one set: owner if you own the file, else group if you are a member, else others, and checks the operation against that set only.
Carry it forwardExactly one permission set applies.
Weak answerAssuming permissions combine.
Constraint changes
What do directory permissions mean?
Can you replace a file in a writable directory?
Read lists names, write creates and deletes entries, and execute lets you traverse into it. Write on a directory lets you replace files inside it, even ones you do not own, unless the sticky bit is set.
Carry it forwardDirectory write means control of its entries.
Weak answerChecking file permissions only.
Constraint changes
What is wrong with config.yaml at 0644?
Who can read a 0644 file?
Every user on the system can read it. If it contains database passwords or API keys, any compromised process can read them. Secrets should be readable only by the service account.
Carry it forwardWorld-readable secrets leak to every account.
Weak answerThinking 0644 is a safe default for config.
Constraint changes
Why is the bin directory a privilege escalation risk?
Who can change what root runs?
The deploy user can modify binaries the root service executes, so compromising the deploy account means running code as root at the next start. Root-executed files must be owned and writable only by root.
Carry it forwardWritable paths executed by root escalate privilege.
Weak answerFocusing only on the service's own user.
Constraint changes
How would you fix the layout?
Which account should own what?
Run the service as a dedicated non-root user, keep binaries root-owned and not writable by others, make secrets readable only by the service user (0600 or 0640 with a dedicated group), and audit for writable paths in anything privileged.
Carry it forwardRoot owns code; the service user owns only its data.
Weak answerchmod 777 to fix deploy errors.
Now defend it
How would you fix the deployment?
Run the service as a dedicated non-root user; make binaries and their directories root-owned and non-writable by others; restrict config with secrets to the service user; deploy through a process that does not leave deploy-user ownership on executed paths; and audit for writable files used by privileged processes.
How it's scored
Weak: Recites rwx and octal, cannot explain directory execute or the sticky bit.
Hire: States the single-set evaluation rule, explains directory semantics, setuid and the sticky bit, and knows setuid programs are an escalation surface.
Strong Hire: Adds umask, real versus effective IDs, why deletion depends on the directory, what the sticky bit does not stop, how to audit setuid and writable paths, and where ACLs, capabilities and MAC sit.
Common mistakes
- Thinking owner, group and other permissions add together.
- Believing write on a file is needed to delete it.
- Treating the sticky bit as protection against temporary-file attacks.
- Forgetting that the owner can always change permissions.