Section 15.23

O_NOFOLLOW and descriptor-relative access

Full name: The open flag O_NOFOLLOW, read as open with no follow, and access relative to a directory file descriptor §15.21 through openat and openat2.

Definition

O_NOFOLLOW is a flag for the POSIX (Portable Operating System Interface) open call that makes the call fail when the last name in the pathname is a symbolic link §13.10, a file whose content is another path; symbolic links earlier in the pathname are still followed. Descriptor-relative access resolves a relative pathname from a directory the program has already opened, identified by a file descriptor, instead of from the current working directory; openat (POSIX) and openat2 (Linux) do this. Only openat2 can also restrict every step of the walk.

The idea

A service keeps each user’s files under /srv/data, one directory per user, and must never open anything outside that tree. Its programmer opens /srv/data/alice/a/passwd with O_NOFOLLOW, believing that no symbolic link can redirect the open. But the user alice made a a symbolic link to /etc. The flag looks only at the last name, passwd, an ordinary file, so the kernel follows a without objection and opens /etc/passwd.

Each tool in this family guards one specific thing. O_NOFOLLOW guards the last name. A directory descriptor fixes the starting point of the walk, so that renaming or replacing a parent directory cannot move it. Neither stops the walk from leaving the starting directory through .., an absolute path or a symbolic link in the middle. Linux 5.6, released in 2020, added openat2, whose resolution flags the kernel enforces at every step of the walk. In a picture: the walk passes a sequence of doors; O_NOFOLLOW inspects only the last door, a starting descriptor fixes the room where the walk begins, and openat2 with RESOLVE_BENEATH posts a guard at every door who stops the walk the moment it would leave that room’s part of the building.

How it works

Pathname resolution §13.9 in brief. The kernel splits the pathname at each / into names, starting at the root directory for an absolute pathname and at a starting directory for a relative one. For each name it looks up the entry in the current directory. A .. moves to the parent directory. A symbolic link is replaced by its content, and the walk continues from there: from the root if the content is an absolute path, from the link’s own directory if it is relative.

The flags and calls.

Walking without openat2. A program can enforce the same policy itself, one name at a time:

  1. Open the starting directory and keep its descriptor.

  2. Split the relative path into names, and refuse an empty name, . and ...

  3. For each name except the last, call openat on the current directory’s descriptor with O_NOFOLLOW and O_DIRECTORY, so that the call fails if the name is a symbolic link or not a directory; close the previous descriptor and continue from the new one.

  4. Open the last name with openat and O_NOFOLLOW.

Every step resolves one name inside a directory the program already holds, so renaming or replacing a directory along the way cannot redirect the walk.

Worked example

The directory /srv/data is opened as descriptor DD. Inside it, a is a symbolic link whose content is /other, and /other/b is an ordinary file. The relative path a/b has 2 names, of which O_NOFOLLOW checks only the last. Eight read-only calls:

  1. open of /srv/data/a/b with O_NOFOLLOW: the last name, b, is an ordinary file, so the call opens /other/b, outside /srv/data.

  2. open of /srv/data/a with O_NOFOLLOW: the last name is now the link, so the call fails with ELOOP. The flag works where it applies.

  3. openat of a/b from DD with O_NOFOLLOW: the walk starts at /srv/data, follows a to /other and opens /other/b. The descriptor changed the starting point, not the policy.

  4. openat of ../../etc/passwd from DD: the first .. leads to /srv, the second to the root, so the call opens /etc/passwd. A starting directory is not a boundary.

  5. openat2 of a/b from DD with RESOLVE_NO_SYMLINKS fails with ELOOP at a.

  6. openat2 of a/b from DD with RESOLVE_BENEATH fails with EXDEV, because a is an absolute link.

  7. openat2 of ../../etc/passwd from DD with RESOLVE_BENEATH fails with EXDEV, because the first .. would leave /srv/data.

  8. openat2 of a/b from DD with RESOLVE_IN_ROOT reads the link content /other as /srv/data/other, so it opens /srv/data/other/b if that exists and otherwise fails with ENOENT. The walk stays inside.

Stated precisely

Let a pathname pp consist of the names c1,…,ckc_1, \dots, c_k, and let SS be the directory where the walk starts: the root if pp is absolute, otherwise the current working directory for open and the directory of dirfd for openat and openat2.

The kernel enforces a resolution flag during the same walk that opens the file, so for that call no gap exists between check and use. With RESOLVE_BENEATH or RESOLVE_IN_ROOT, the call fails with EAGAIN when a concurrent rename might have let a .. escape; the kernel refuses rather than guess, and the program may retry the same call. Each guarantee assumes that the kernel is Linux 5.6 or later (for openat2), that the program treats every error as a refusal and never retries with a weaker call, that dirfd refers to the intended directory, and that every mount point below SS is trusted or RESOLVE_NO_XDEV is set.

Limits and common mistakes

O_NOFOLLOW alone is a rule about the last name. It suits refusing a link in the final position, for example when opening a file inside a directory the program already holds, and it does not protect a whole path. A directory descriptor is an anchor, not a fence: openat from it still accepts .., absolute paths and links that lead out.

Hard links are not symbolic links. A hard link, a second directory entry for an existing file, inside the starting directory can name a file that also has a name elsewhere, and no resolution flag detects it, because the walk never leaves the directory. Check the opened file with fstat: its owner, its type and its link count st_nlink.

Falling back silently weakens the policy. If openat2 fails because the kernel is older than 5.6 (error ENOSYS) or because a security filter blocks the call, use the name-by-name walk above or refuse; never retry with plain open.

A resolution flag constrains the walk, not the file: it does not authenticate the contents or make the file fit for a purpose. It also restricts only the call that carries it, so a process that elsewhere calls plain open is not confined, and these flags do not make a sandbox §2.30. RESOLVE_NO_SYMLINKS also refuses legitimate links; the Linux manual page advises making its use configurable unless it serves a specific security purpose.

Questions answered

Does O_NOFOLLOW by itself forbid every intermediate symbolic link?

No. O_NOFOLLOW makes open fail only when the last name in the pathname is a symbolic link, a file whose content is another path; symbolic links among the earlier names, the intermediate directories, are followed as usual. Opening a/b with O_NOFOLLOW when a is a symbolic link to /other succeeds and opens /other/b.

To forbid symbolic links at every step on Linux, use openat2 (Linux 5.6 and later) with the resolution flag RESOLVE_NO_SYMLINKS; to forbid leaving a starting directory by any route, including .., absolute paths and links, use RESOLVE_BENEATH. Without openat2, walk the path one name at a time with openat, opening each directory relative to the previous one with O_NOFOLLOW and O_DIRECTORY. Plain openat from a directory descriptor fixes only the starting point and forbids nothing by itself.

Related entries: File descriptor versus pathname (§15.21), Archive path traversal (§15.18), pathlib (§12.39), Namespace and symlink resolution (§13.9), Hard link versus symbolic link (§13.10). Further reading: [50, 139, 141].