Navigate files and permission boundaries
Why you care
A wrong permission bit is the most common reason a job that works on your laptop fails silently, or leaks, on a shared build box: the pipeline user can read the repo but not the .pgpass a teammate copied back from a backup, or a fresh clone inherits a umask that leaves credentials group-readable. You do not get a helpful exception for this. You get Permission denied on someone else's line 40, or worse, no error at all until a security scan flags it three weeks later. Interviewers ask "what does 644 mean" and "what does umask do" not to test trivia, but because a candidate who can read ls -l output and reason about who can touch a file has clearly operated a real multi-user box, not just their own laptop. Everything else in this chapter assumes you can answer "who owns this, and who can write to it" before you touch it.
Explain like I'm 5
A path is an address in an apartment building, and every door has three keyrings: one for the resident (owner), one for their flat's family (group), and one for everyone else in the building (other). Read means you can see what's inside, write means you can rearrange the furniture, execute means you can use the door at all — walk through it, or for a directory, walk through it to reach the rooms behind it. When the building is constructed, the landlord's default lock policy is stamped onto every new door the moment it is installed. That policy is the umask. Changing the landlord's policy tomorrow does not reach into the door that was fitted yesterday.
Core concept
- A path is either absolute (starts at
/) or relative to your current directory (pwd);.and..are the current and parent directory, and a symlink is a signpost to another path, while a hard link is a second name for the same underlying file. - Ownership is a user and a group, shown by
ls -land changed withchown user:group path(only root, or the owner handing off to a group they belong to, can do this). - Permissions are three triplets, owner/group/other, each
rwx.ls -lshows them as text (-rw-r--r--);statandchmoduse the octal form, where each digit isr(4)+w(2)+x(1):750is owner all, group read+execute, other nothing. umaskis a bitmask subtracted from the kernel's own defaults at creation time: new files start at666, new directories at777, and the actual mode isdefault & ~umask. A umask of022turns a new file into644and a new directory into755.- On a directory, execute means something different from a file: it lets you traverse into it and reach entries by name. Read on a directory only lets you list names; without execute you cannot
stator open anything inside, even ones you can "see".
Decision rule: use stat when a script needs to compare an exact octal mode; use ls -l for a fast human read across many files. Change ownership only when the process that must access the file genuinely runs as a different user; otherwise change only the mode. Never chmod -R 777 to make an error go away — that is a diagnosis substitute, it grants write to every user on the box, and it is exactly the kind of change a security review calls out by name. Inspect first, always: ls -l or stat before any chmod or chown.
Under the hood
umask is not consulted by chmod, and chmod does not touch the process umask. The kernel applies umask exactly once, inside the open(2) and mkdir(2) syscalls that create a new inode, computing mode & ~umask at that instant. Once the inode exists, its mode lives on disk and is changed only by an explicit chmod(2) call. That is why umask 077 run in your shell right now has zero effect on a file created an hour ago: there is no syscall left to consult it. It only shapes files your shell (or a script inheriting its umask) creates from this point forward.
Minimal example Umask arithmetic and the resulting rwx string
import stat
def new_file_mode(umask_val):
return 0o666 & ~umask_val
def new_dir_mode(umask_val):
return 0o777 & ~umask_val
for umask_val in (0o022, 0o002, 0o077):
fmode = new_file_mode(umask_val)
dmode = new_dir_mode(umask_val)
print(f"umask {oct(umask_val)}: file {oct(fmode)} {stat.filemode(stat.S_IFREG | fmode)} "
f"dir {oct(dmode)} {stat.filemode(stat.S_IFDIR | dmode)}")umask 0o22: file 0o644 -rw-r--r-- dir 0o755 drwxr-xr-x
umask 0o2: file 0o664 -rw-rw-r-- dir 0o775 drwxrwxr-x
umask 0o77: file 0o600 -rw------- dir 0o700 drwx------In production Flag secret-named files readable outside their owner
On the real build box this manifest would come from stat.S_IMODE(os.stat(p).st_mode) per file; this sandbox has no real Unix permission bits, so the audit logic runs against a provisioning manifest instead, which is exactly the shape of a pre-deploy permission gate.
import stat
provisioned = {
".pgpass": 0o600,
"config/app.yaml": 0o644,
"orders_dump.csv": 0o664,
"settlement_secrets.env": 0o644,
}
SECRET_HINTS = ("secret", ".pgpass", "credential", "password")
LEAK_BITS = stat.S_IRGRP | stat.S_IROTH | stat.S_IWGRP | stat.S_IWOTH
violations = [
(name, oct(mode))
for name, mode in provisioned.items()
if any(hint in name.lower() for hint in SECRET_HINTS) and mode & LEAK_BITS
]
print("checked:", sorted(provisioned))
print("violations:", violations)checked: ['.pgpass', 'config/app.yaml', 'orders_dump.csv', 'settlement_secrets.env']
violations: [('settlement_secrets.env', '0o644')]In your world
The situation
Your ETL job's Jenkins agent runs as svc-etl on a shared box. A teammate re-clones the repo for a hotfix and restores .pgpass and settlement_secrets.env from an old backup tarball, which lands at the shell's default umask instead of the mode they were created with.
What actually breaks
Jenkins stays green — nothing in the job ever checked file modes. Three weeks later a security scan run by another team flags settlement_secrets.env as group-readable and asks who else on the box could have read it. You cannot answer, because nothing recorded it.
What you'd do on Monday
Add a startup check that refuses to run instead of trusting the filesystem, and correct the mode on the spot:
for f in .pgpass settlement_secrets.env; do
mode=$(stat -c '%a' "$f")
if [ "$mode" != "600" ]; then
echo "refusing to run: $f is $mode, expected 600" >&2
chmod 600 "$f"
exit 1
fi
doneImagine you work at DigitalOcean
The numbers
Illustrative: a platform provisioning 50,000 fresh build-agent droplets a month from a base image. If a bad image variant ships with umask 002 instead of 022 for even 5% of that month's boots while two image versions are active during a rollout, that is 2,500 machines a month with the same secrets directory left group-writable — invisible until a weekly fleet scanner catches it, by which time roughly a week's worth, ~580 machines, have already run with the wider mode.
What the Scenario-A answer does here
The Scenario A fix (a startup check that refuses to run) still catches this per machine, but "per machine" is the problem: relying on 50,000 individual boot scripts to each notice and self-correct means you find out from the scanner's weekly report, not before the machine ever took traffic.
What flips at this scale
At this scale the umask and every secrets-file mode belong in the base image and are asserted once at boot, so a bad image variant fails cloud-init loudly on machine one instead of silently drifting on machine 2,500. The fleet scanner becomes a second, independent layer that proves the control worked, not the primary control itself.
The trade-off you'd defend
I would defend baking the mode into the image and asserting it at boot over trusting per-machine chmod, accepting one more boot-time check that can itself fail, in exchange for every one of 50,000 machines starting identical and a bad rollout failing on machine one instead of machine 2,500.
Illustrative figures chosen to make the arithmetic concrete; not DigitalOcean-published numbers.
Gotchas
An unquoted path splits into more than one argument
chmod 600 $secret_path either errors with "No such file or directory" for two garbled paths, or silently changes the wrong file, when the path contains a space.
Unquoted variable expansion in the shell undergoes word splitting on whitespace before the command ever sees it.
Quote every path expansion — chmod 600 "$secret_path" — and use arrays (for f in "${files[@]}") when you genuinely have more than one path.
Umask changes do not reach files that already exist
umask 077 in your shell, then ls -l secrets.env still shows -rw-r--r--.
The kernel consults umask only inside the open/mkdir syscall that creates a new inode; it never revisits an existing one.
chmod the existing file explicitly. Use umask to fix files created from now on, and recreate a file if you need it to pick up a new umask.
A "read-only" check misses the write bit
A secrets audit that only checks "is it world-readable" passes a file that is 664 — not readable by other, but writable by the whole group.
Leak checks that test a single bit instead of the full triplet miss the other two ways a boundary can be crossed.
Check group and other read and write together, as the production example does with LEAK_BITS.
Tricky questions
chown a file away to another user, do I keep my read access?Yes, since I created the file originally.
No. Ownership determines which triplet applies to you. Once a file is chowned to another user, your access comes only from the group or other triplet, unless you happen to be a member of the new owning group.
Yes, it means you can run it.
On a file, execute means "can be run as a program." On a directory, it means "can be traversed" — you can cd into it and open files inside by name. A directory with read but no execute lets you list names with ls, but you cannot stat or open anything you see there.
Scenario question
You're handed a nightly script that today fails with PermissionError: [Errno 13] Permission denied on /data/incoming/vendor.csv, which worked yesterday. ls -l shows -rw-r----- 1 vendor-svc data-team vendor.csv, and your job runs as etl-svc, which is not in data-team. What do you check, and what do you actually do?
Model answer
I would run id etl-svc to see its groups, and ls -ld on the parent directories too, because a denied traversal on a directory looks identical to a denied read on the file. If etl-svc genuinely needs read access, I would not chmod o+r the vendor's file — that grants read to every user on the box, including ones with no reason to see vendor data. I would ask an admin to add etl-svc to data-team, a one-line, auditable change, or route the handoff through a location with a group made for exactly this. The trade-off is speed versus blast radius: widening the file's mode fixes the job in ten seconds and quietly widens access forever; fixing group membership takes a request, but the access boundary stays intentional.
Interview corner
CommonWhat does chmod 750 mean?
TrickYou just ran umask 077. Does that fix the permissions on files you created earlier?
Experience checkTell me about a permissions bug you've actually hit.
.pgpass file back from a backup at 644 instead of 600, and it sat like that until a scanner flagged it weeks later. Now our setup script asserts the mode of every secrets file at startup and refuses to run if it's wrong, instead of trusting whatever the filesystem happens to have.”DesignHow would you stop 50,000 machines in a fleet from drifting on default file permissions?
Memory hook
Every new door in the building is stamped with the landlord's lock policy the moment it's built. Changing the policy tomorrow doesn't reach the door fitted yesterday.
Inspect the actual owner and mode before you touch either — never chmod blind.
Self-check
What does umask 022 do to a newly created file, and why doesn't it touch files created earlier?
It computes
0o666 & ~0o022 = 0o644; it only applies at creation time, insideopen/mkdir, so it can never reach a file that already exists.Why can a directory be readable but still block access to files inside it?
Read on a directory only lets you list names. Traversal — opening or
stat-ing something inside — needs the execute bit too.Name the exact syscall-level moment umask is applied.
Inside
open(2)ormkdir(2), asmode & ~umask, when the inode is created.
Extend the production example's manifest to five files, including at least one secrets-style name with a leaking mode and one with a correct mode. Run the audit, then change the leaking file's mode to 600 in the manifest and rerun. Paste both printed violation lists and confirm the second one is exactly one shorter.