Section 13.9

Namespace and symlink resolution

Full name: Namespace and symbolic link resolution.

Definition

A file namespace is the tree of names through which a process reaches files: directories, their entries and the file systems mounted into the tree. Symbolic link resolution, or symlink resolution, is the part of pathname lookup that handles links. Lookup turns a name such as /data/run1/out.csv into a file one component at a time; at a symbolic link §13.10, a small file that stores a pathname, it continues along the stored pathname. Because entries and links can change, one pathname can name different files at different times.

Where it fits

Main entry: File descriptor versus pathname (§15.21).

A pathname that begins with a slash is looked up from the process’s root directory; any other pathname from its current working directory. A symbolic link in the middle of a pathname is always followed. Whether a link in the last component is followed depends on the call: open and stat follow it, lstat and unlink do not. A relative stored pathname is looked up from the directory that holds the link. Linux follows at most 40 links in one lookup and then fails with the error ELOOP.

Lookup reads the namespace as it is at that instant. Anyone with write permission on a directory can rename, remove or replace its entries, and a privileged process can mount another file system over a directory, which makes that file system’s contents appear under the directory’s name. A check on a pathname followed by a separate use of the same pathname can therefore examine one file and use another, a time-of-check to time-of-use §9.26 (TOCTOU) race. The File descriptor versus pathname entry gives the remedy: open once, which binds a file descriptor, the handle through which the process then reaches the file, to the file found at that moment; then check and read through that descriptor.

For evidence this gives three rules. State which components may be links: none, only the last, or any. Enforce that rule in the call that opens the file: the flag O_NOFOLLOW makes open fail if the last component is a link, and the Linux call openat2 refuses links in every component with RESOLVE_NO_SYMLINKS and refuses to leave a given directory with RESOLVE_BENEATH. Record the digest of the bytes read through the descriptor and the file identity that fstat reports for it (the device number and the inode number, the file system’s own number for the file), not only the pathname.

Limits and common mistakes

Tidying a pathname as text is not resolution. Python’s §12.23 os.path.normpath turns a/link/../b into a/b without looking at the disk; if link is a symbolic link to /x/y, the kernel resolves the same name to /x/b. A call that consults the file system, such as os.path.realpath, follows links, but its answer is again only a snapshot.

Questions answered

Which components may be links, and can any change between checks?

Any component of a pathname may be a symbolic link (a small file that stores another pathname), not only the last one, unless the call that opens the file forbids links. Any component can also change between two checks, or between a check and the use: whoever can write to the directory that holds an entry can rename, remove or replace that entry, for example with a link, and a privileged process can mount another file system over a directory, so that the name leads into that file system. A program that relies on a pathname should state which components may be links, enforce that rule while opening (O_NOFOLLOW covers the last component; on Linux, openat2 with RESOLVE_NO_SYMLINKS covers all of them), and then examine and read the file only through the returned file descriptor, which keeps referring to the same file whatever happens to the names.

Related entries: pathlib (§12.39), O_NOFOLLOW and descriptor-relative access (§15.23), Inode reuse (§15.24), Hard link versus symbolic link (§13.10), Time-of-check to time-of-use (TOCTOU) (§9.26). Further reading: [141].