Section 13.3

Cryptographic digest versus path identity

Definition

A cryptographic digest identifies content: it is a short, fixed-length value that a cryptographic hash function §5.1 computes from bytes, and equal digests mean equal bytes in practice. A path identity identifies a file by name: a pathname such as /data/run1.bin designates whatever file the file system holds under that name when the name is looked up. The digest says what the bytes are; the path says where to look, and can lead to a different file later. Neither identifies the file object itself, which the operating system tracks separately.

The idea

A measurement program writes its raw data §13.16 to the file /data/run1.bin, makes the file read-only and records its SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output) digest. A user later copies the file to /tmp/run1.bin. The two files hold exactly the same bytes and have the same digest, yet they are two different files. The copy belongs to the user, the user can change it at any time, and the measurement program never vouched for it. A rule that says “use the file the measurement program wrote” is broken by using the copy, although the content is identical.

The opposite case is just as real. A program checks the file at a path, then opens the same path again to use the file. Between the two moments, another program renames a different file onto that path. The path is unchanged; the file behind it, and its content, are not. This mistake is called a time-of-check to time-of-use §9.26 (TOCTOU) error.

A file therefore has three identities, and each answers a different question. The digest answers “which bytes?”. The path answers “which name?”. The file system’s own record of the file, described below, answers “which object?”. An input rule may need one, two or all three, and each needs its own check.

How it works

On Unix-like operating systems, which largely follow the POSIX (Portable Operating System Interface) standard, the three identities work as follows.

A program that needs both a particular object and particular content checks both on one open file.

  1. Open the path once. The call returns a file descriptor §15.21, a small number through which the process refers to the opened file itself. The descriptor keeps referring to that file even if the path is later renamed, deleted or pointed at another file. The flag O_NOFOLLOW makes the open fail if the last component of the path is a symbolic link.

  2. Call fstat on the descriptor to read the device number, inode number, owner and permissions, and compare them with what the rule requires.

  3. Read the bytes through the same descriptor, compute their digest and compare it with the expected digest.

  4. Use exactly the bytes that were read and checked.

Checking a path with one call and then opening it with another separates the check from the use, and the file can be swapped in between.

Worked example

A measurement service, running under the account meas, writes a 4-byte file, seals it by making it read-only, and records its device and inode numbers. A user, Alice, copies it. The device and inode numbers are invented for the example; the digest is the real SHA-256 digest of the bytes ABCD, shortened to its first 8 hexadecimal digits.

File A File B
Path /data/run1.bin /tmp/run1.bin
Device, inode 2049, 131074 2049, 262150
Owner meas alice
Permissions read-only for all Alice may write
Content ABCD ABCD
SHA-256, first 8 digits e12e115a e12e115a

Rule 1 says: use bytes whose SHA-256 digest equals the digest recorded when the data were acquired. Both files satisfy rule 1, because rule 1 concerns content only.

Rule 2 says: use the file the measurement service wrote and sealed. File B breaks rule 2. Its pair of device and inode numbers, (2049, 262150), differs from the recorded (2049, 131074), and its owner is alice, not meas.

Rule 2 has a reason. At time t1t_1 a consumer checks file B and finds the digest beginning e12e115a. At time t2t_2 Alice writes ABCE into file B. A consumer that reopens /tmp/run1.bin at time t3t_3 reads ABCE, whose digest begins 7ff1c830. Alice cannot change file A in this way, because Alice does not own it and it is read-only. The equal digests at t1t_1 said nothing about the content at t3t_3.

Stated precisely

Let HH be a cryptographic hash function. For a file ff, let Ct(f)C_t(f) be its content at time tt and I(f)I(f) its pair of device number and inode number. For a path pp, let Rt(p)R_t(p) be the file that pp resolves to at time tt. Then three statements hold.

A rule that requires a particular object with particular content is the conjunction of two conditions, I(f)=i0I(f) = i_0 and H(Ct(f))=h0H(C_t(f)) = h_0, where i0i_0 is the required pair of device and inode numbers and h0h_0 the required digest. Neither condition implies the other, so both must be checked. Both must also concern the file at the time it is used, which in practice means checking and reading through one open file descriptor and using the bytes that were checked. The guarantee assumes that the file system reports device and inode numbers as POSIX requires, and that the hash function is collision resistant.

Limits and common mistakes

An inode number alone is not an identity. Inode numbers are unique only within one file system, so the device number must go with it. Once a file is deleted, its inode number can be given to a new file, so a pair recorded yesterday may designate a different file today. Keeping the file descriptor open, or recording the digest as well, closes that gap.

Different paths do not mean different files: two hard links are two names for one inode. Renaming a file within one file system keeps its inode; copying it, or moving it to another file system, creates a new file with a new inode.

Equal digests do not carry over the owner, the permissions, the timestamps or the right to use the file. Those are metadata of the object, which the digest of the content does not cover.

Object identity does not cross machines. A file sent to another computer becomes a new object there. Across machines only the content digest survives, and a record of every handover of the file, a chain of custody §13.5, has to stand in for object identity.

Object identity should be required only for a reason. If a rule needs only certain bytes, demanding a particular inode adds failures, for example after a legitimate copy or a restore from backup, and adds no protection.

Questions answered

Can identical bytes at a different inode violate an input contract?

Yes: identical bytes at a different inode violate any input contract that requires a particular file object, not only particular content. An input contract is the rule that says which input a program may consume, and an inode is the file system’s record of one file object. Contracts that require an object include “use the file the acquisition service wrote and sealed”, “use a file owned by the service account and writable by nobody else”, and “use the file that was checked, held open since the check”. A copy at a different inode has the same bytes now, but it can have a different owner and permissions, it can be changed later by whoever may write it, and it was not produced by the step the contract trusts.

Identical bytes do not violate a contract that asks only for content with a given digest; then any copy with that digest is acceptable. To decide, read what the contract requires, then check every required property on the file actually consumed, through one open file descriptor, so that the check and the use concern the same file.

Related entries: Content-addressed identity (§13.2), File descriptor versus pathname (§15.21), Inode reuse (§15.24), File metadata versus content (§13.11), Time-of-check to time-of-use (TOCTOU) (§9.26), Hard link versus symbolic link (§13.10). Further reading: [139].