Definition
A pathname is a text string, such as /safe/input, that names a file by a route through directories; the operating system looks the route up again each time a program passes the string to it. A file descriptor is a small non-negative integer that a process receives when it opens a file; the integer refers to that opened file, and operations through it look up no name. The difference: one pathname can lead to different files at different moments, while a file descriptor refers to one file until closed.
The idea
A program running with administrator rights copies a report that an ordinary user supplies, but only if the user owns the file. It checks the path /home/user/report with one call, finds a regular file owned by the user, and opens the same path with a second call. Between the two calls the user replaces report with a symbolic link §13.10, a small file whose content is another path, pointing to /etc/shadow, the file of password hashes. The check approved one file; the open reaches another. This gap between check and use is a time-of-check to time-of-use §9.26 (TOCTOU) race. The Linux manual page for access, a POSIX (Portable Operating System Interface) call made for such checks, warns against exactly this pattern.
The intuition: a pathname is a street address, and a file descriptor is a key to one particular room. The address is looked up on every trip, so if someone moves the house-number signs overnight, the same address leads to a different building. The key keeps fitting the room it was cut for. The picture stops at one point: the key does not stop anyone else with their own key from rearranging the furniture. A file descriptor fixes which file, not what the file contains.
How it works
The objects on a Unix-like system.
A file is the stored object: contents plus metadata such as size, owner and permissions, kept in a record called an inode (index node), numbered within its file system.
A directory is a file whose contents are entries, each pairing a name with an inode number. A file has no name of its own; it has as many names as entries point to it.
A pathname lists names separated by
/, starting at the root directory if it begins with/and at the process’s current working directory otherwise.An open file description is the kernel’s record of one act of opening: which file, the current read and write position, and the access mode.
A file descriptor is an index into the process’s table of open files that points to an open file description.
Opening. The call open(path, flags) performs pathname resolution §13.9: for each name in turn it looks up the entry in the current directory, follows a symbolic link if it meets one, and moves on. It checks permissions on the file it reaches, creates an open file description and returns the lowest-numbered unused descriptor, for example 3. Every later read, write or fstat on descriptor 3 goes straight to that file without looking at any name.
What later changes affect. Renaming, deleting or replacing the entry /safe/input changes the namespace, the mapping from names to files. A descriptor obtained earlier still refers to the original file, which keeps existing, even with no name at all, until the last descriptor and the last name referring to it are gone. The access granted at open time also stays with the descriptor; a later permission change does not revoke it. In this sense a file descriptor behaves like a capability, a reference whose possession is itself the right to use it.
The safe pattern is to open first, check the opened file, then use the same descriptor:
fd = open(path, O_RDONLY | O_NOFOLLOW); the flagO_NOFOLLOWmakes the open fail if the last name in the path is a symbolic link.fstat(fd)reports the type, owner and size of the file actually opened, and its identity: the device numberst_devand inode numberst_ino, which together identify one existing file on one machine.Check those facts.
Read through
fd.
Python §12.23 offers the same calls as os.open and os.fstat, and many of its file functions accept a directory descriptor through a dir_fd argument.
Worked example
The directory /home/user belongs to an ordinary user. In it, report names inode 1001, a regular file owned by the user that holds the 4 bytes ABCD. The file /etc/shadow is inode 2002, owned by the administrator account.
Check by name, then open by name. At time 1, stat on /home/user/report resolves to inode 1001, a regular file owned by the user, so the check passes. At time 2 the user, who may change entries in their own directory, deletes report and creates a symbolic link named report that points to /etc/shadow. At time 3, open on the same string follows the link and reaches inode 2002, and the program copies the password hashes to where the user can read them. The string never changed; what it named did.
Open, then check through the descriptor. At time 1, open on /home/user/report with O_NOFOLLOW reaches inode 1001 and returns descriptor 3. At time 2, fstat on descriptor 3 reports inode 1001, owned by the user, so the check passes. At time 3 the user swaps in the link. At time 4, reading 4 bytes from descriptor 3 returns ABCD from inode 1001. Had the user swapped in the link before time 1, the open would have failed, because the last name was a symbolic link. Had the user made report a hard link instead, a second directory entry for inode 2002, where the system permits such a link, the open would have succeeded, but fstat would have reported the administrator as owner, and the check would have refused the file.
What the descriptor does not freeze. At time 3 the user could instead write WXYZ into inode 1001 through the user’s own descriptor. The read at time 4 would then return WXYZ. The descriptor kept the same file, not the same contents. If the contents must also be the checked ones, the program copies them once into its own memory, then checks and uses the copy.
Stated precisely
Model the namespace at time as a function from pathnames to files, determined by the directory entries and symbolic links that exist at time . A successful open of pathname at time returns a descriptor whose file is . Then:
is the same file at every later time until is closed, whatever happens to the namespace.
at a later time holds if no process changed, between and , any directory entry or symbolic link that the resolution of passed through. A program can rely on this only when no less trusted process may write to any of those directories.
The contents of may change between any two reads unless no other process can write to the file.
So same file? and same bytes? are separate questions. A check and a use through one open descriptor concern the same file. Same bytes needs more: a private copy, a file no other process can write, or a cryptographic hash §5.1 of the copy compared with a known value. Comparing st_dev and st_ino from two separate calls also identifies a file, with one caveat: a deleted file’s inode number can be given to a new file, as the Inode reuse §15.24 entry explains. An open descriptor prevents that reuse on an ordinary local file system, because a file that is still open is not deleted. These statements assume POSIX file semantics, a local file system and a kernel that the attacker cannot change.
Limits and common mistakes
A descriptor proves nothing about the opening itself. If the program followed an attacker’s symbolic link in a directory along the way, the descriptor faithfully refers to the wrong file. O_NOFOLLOW guards only the last name; the entry on O_NOFOLLOW and descriptor-relative access §15.23 covers the earlier ones.
Descriptor numbers are reused. After descriptor 3 is closed, the next open can return 3 for a different file. A thread that keeps using the old number after another thread closed it reads or writes the wrong file, a bug similar to a use-after-free §4.6.
A descriptor keeps the access granted at open time, also when passed to another process or inherited by a child process. Close descriptors a child should not receive, or open them with the flag O_CLOEXEC, which closes them when the process starts another program.
Checking a directory and then opening a file in it by full pathname has the same race one level up. Open the directory and resolve the file relative to its descriptor. Network file systems and special files, such as those under /proc, can behave differently from an ordinary local file.
Questions answered
Does checking a path guarantee that a later open reaches the same inode?
No. A pathname is looked up afresh on every call, so a check of the path at one moment and an open of the same path a moment later can reach different inodes, the file system’s records of individual files. Any process allowed to change a directory along the path can rename, delete or replace an entry, or put a symbolic link there, between the two calls. This gap is a time-of-check to time-of-use (TOCTOU) race.
The reliable pattern reverses the order. Open the path once, with O_NOFOLLOW to refuse a symbolic link as the last name; check the file through the returned file descriptor with fstat; and do all later reads through that same descriptor. The descriptor refers to the inode it opened for as long as it stays open, whatever later happens to the name. It does not freeze the file’s contents, which another writer can still change; if the bytes must match the checked ones, copy them into private memory, then check and use the copy.
Related entries: pathlib (§12.39), O_NOFOLLOW and descriptor-relative access (§15.23), Inode reuse (§15.24), Cryptographic digest versus path identity (§13.3), Time-of-check to time-of-use (TOCTOU) (§9.26), Namespace and symlink resolution (§13.9), Hard link versus symbolic link (§13.10). Further reading: [50, 139, 141].