Part 3 of 7 · 2 min read
Walk the path
Think of a corridor with several doors in a row, and the visitor needs to pass them all to reach a room. Only the last door has a sign, and the visitor reads that sign and sees "open to all". The one locked door in the middle is not on the sign. The analogy stops in one place: in a building a locked door is obvious, while a directory's missing search bit shows only in its own mode.
In plain words: To open a path, the kernel checks the search permission (x on a directory, page 2) on every directory from the root down, and then the read permission on the file. A file with mode 644 is world-readable only if every directory above it can be searched. namei -l prints the owner, group and mode of every component of a path, so the refusing component stands out.
A worked example. The commands and output on the test machine:
$ setpriv --reuid=65534 --regid=65534 --clear-groups cat /tmp/l8/l10/cs/proj/private/report.txt
cat: /tmp/l8/l10/cs/proj/private/report.txt: Permission denied
$ namei -l /tmp/l8/l10/cs/proj/private/report.txt
drwxr-xr-x root root /
drwxr-xr-x root root tmp
drwxr-xr-x root root l8
drwxr-xr-x root root l10
drwxr-xr-x root root cs
drwxr-xr-x root root proj
drwx------ root root private
-rw-r--r-- root root report.txt
$ strace -e trace=openat cat .../report.txt (run as nobody)
openat(AT_FDCWD, ".../report.txt", O_RDONLY) = -1 EACCES (Permission denied)
The file itself has mode 644, which lets every user read it. The directory private has mode 700 and belongs to root, so for nobody it falls in the third class "other" (page 2) with no rights at all. nobody cannot search private, so the lookup of report.txt fails before the file's own mode is looked at. Every directory above private has x for all, so the break is in exactly one place.
The key lines. The file's own mode matters only if the whole path can be searched. Read the path from the top, and stop at the first component without x for the process's class.
Search permission
Search permission is the x bit on a directory. It lets a process look up a name inside it. Without it the process cannot reach any file below the directory, whatever the file's own mode is. It is separate from read permission on a directory, which lets the process list the names.
Example: a directory with mode 700 owned by root refuses nobody the lookup of every file inside.
Check yourself:
1. Why can a mode-644 file be unreadable?
2. What does namei -l print for each component?
3. Which class does nobody fall in for a root-owned directory with mode 700?