Section 15.17

Build fingerprint

Definition

A build fingerprint is a short value that identifies a build, computed by a fixed rule from a declared set of the build’s inputs, such as source files, generated files, configuration, compiler version and compiler flags §12.3, or from the bytes of its output. The rule is usually a cryptographic hash function §5.1, which maps data of any length to a fixed-length digest that changes unpredictably when any input byte changes. Equal fingerprints show that the covered items are equal; they say nothing about any item the rule leaves out.

The idea

Two firmware images behave differently, and someone must find out whether they came from the same build inputs. Comparing every input file by hand is slow and easy to get wrong. A fingerprint reduces the question to comparing two short values: compute one value over the inputs of each build, and compare the values. Build caches rely on the same idea to skip work: if the fingerprint of a compile step’s inputs matches one seen before, the stored output of that earlier step is reused.

The name fits in one way and misleads in another. Like a human fingerprint, the value is small and, in practice, unique to what it was taken from. Unlike a human fingerprint, it is taken only from what the rule chose to cover. A fingerprint over the source files is blind to the compiler, just as the print of one finger says nothing about the other hand. The first question to ask of any build fingerprint is therefore what it covers.

How it works

Making a fingerprint takes four decisions, and each one limits what equal fingerprints can show.

  1. Coverage: the list of items hashed. For a fingerprint of the inputs, the goal is the source closure §13.6, the set of every input that can affect the output: source files, headers, generated files together with the generators and data that produced them, configuration, libraries, the linker script §12.5, the identity and version of each tool in the toolchain §12.2, all compiler and linker §12.4 flags, and the environment variables that the tools read. For a fingerprint of the output, the item is the bytes of the image.

  2. Encoding: how the items become one byte string. The encoding must be injective, meaning that different lists of items always give different byte strings. Writing each item’s name and length before its bytes makes a prefix-free encoding §15.38, one in which no encoded item is the beginning of another, so the boundaries between items are unambiguous. Together with a fixed order of the items, such as sorted by name, this makes the encoding injective; plain concatenation is not injective.

  3. Hash: a collision-resistant §5.5 hash function such as SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output), for which finding two different inputs with the same digest is infeasible.

  4. Length: the full digest or a shortened prefix of it. A shorter value is easier to read and type, but two builds can then share a value by chance.

A fingerprint of the inputs and a fingerprint of the output answer different questions. A fingerprint of the inputs says which inputs a build was meant to use. It shows that a particular image came from those inputs only if the build is a hermetic build §13.7, one that can read nothing outside its declared inputs, and the build itself computed the fingerprint from what it actually read. A fingerprint of the output identifies the bytes of an image but says nothing, by itself, about the inputs that produced them; many linkers can store such a value inside the file as a build ID. A reproducible build §13.8 connects the two: rebuilding from the same inputs gives bit-for-bit the same output, so anyone can rebuild and compare output fingerprints.

A Git commit §13.19 identifier is often used as a fingerprint. It covers the files committed to the repository. Changes not yet committed in the working tree, files that version control ignores, files generated during the build, and the toolchain all lie outside it.

Worked example

An omitted input. A build reads three files: main.c, config.h and generated.h, which a script writes from a table of allowed commands. The fingerprint rule covers only main.c and config.h. Build 1 runs the script with two allowed commands. Before build 2, someone adds a third command to the table. main.c and config.h are byte-for-byte the same in both builds, so the encoded input is the same byte string, and SHA-256, which always gives the same digest for the same input, returns the same digest. The fingerprints are equal, yet build 2 accepts a command that build 1 rejects. No collision occurred, because the hash function received identical input. The rule simply did not cover generated.h, the script or the table.

An ambiguous encoding. Now let the rule cover config.h and generated.h but encode them by plain concatenation. Shrink the files to show the effect. In build A, config.h holds the 2 bytes ab and generated.h the 1 byte c; in build B, they hold a and bc. Both concatenations are the 3 bytes abc, and both fingerprints are the SHA-256 digest of abc:

ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad

The inputs differ and the fingerprints are equal, again with no collision: the encoding merged the two inputs before the hash function saw them. Writing each file’s length in decimal and a colon before its bytes gives 2:ab1:c for build A and 1:a2:bc for build B. These strings differ, and so do their digests:

build A:
430fb1b4ac43316eca81fab27a1930ab8eff8fef6a1dc7903dce44bbc2790dc5
build B:
5310a58788781ab25d5ad7c3f85035824b4eb7bdfa394e0ac2186271472b5492

A shortened fingerprint. A team prints only the first 8 hexadecimal digits of each digest, which is 8×4=328 \times 4 = 32 bits, and keeps 10,000 builds. Treat each shortened value as a random 32-bit number. There are 10,000×9,999/2=49,995,00010{,}000 \times 9{,}999 / 2 = 49{,}995{,}000 pairs of builds, and each pair matches with probability 2−32=1/4,294,967,2962^{-32} = 1 / 4{,}294{,}967{,}296. By the union bound §15.42, which says that the probability that at least one of several events happens is at most the sum of their probabilities, the probability that some pair matches is at most 49,995,000/4,294,967,296≈0.011649{,}995{,}000 / 4{,}294{,}967{,}296 \approx 0.0116, about 1.2%: small, but too large for a value used to tell builds apart. With all 256 bits the same bound is 49,995,000/2256≈4×10−7049{,}995{,}000 / 2^{256} \approx 4 \times 10^{-70}.

Stated precisely

Let I=(i1,…,ik)I = (i_1, \ldots, i_k) be the covered items in their fixed order, EE the encoding, HH the hash function, and tt the number of digest bits kept. Write trunct(d)\mathrm{trunc}_t(d) for the first tt bits of a digest dd. The fingerprint is F(I)=trunct(H(E(I))).F(I) = \mathrm{trunc}_t(H(E(I))). Three statements hold.

  1. If F(I)≠F(I′)F(I) \ne F(I'), then I≠I′I \ne I'. This needs only that EE, HH and trunct\mathrm{trunc}_t always give the same output for the same input.

  2. If F(I)=F(I′)F(I) = F(I') and I≠I′I \ne I', then two different encodings share the first tt bits of their digests: a collision. This needs EE to be injective; otherwise an ambiguous encoding produces equal fingerprints without any collision. Whether such a collision can be ignored depends on tt. Finding one by brute force takes about 2t/22^{t/2} hash computations, so t=32t = 32 gives about 216=65,5362^{16} = 65{,}536 tries, which is no protection, while for full-length SHA-256, with t=256t = 256, no collision is known.

  3. If an item jj is not in II, then changing jj leaves F(I)F(I) unchanged, so F(I)F(I) says nothing about jj.

Equal fingerprints therefore support the claim that two builds used the same inputs only under two further assumptions. Coverage: every input that can affect the output is in II. Binding: each image was really produced from the items that its fingerprint covers, which holds when the build is hermetic and computed the fingerprint from the inputs it actually read, not from a separate list.

Limits and common mistakes

A fingerprint identifies; it does not authenticate. SHA-256 uses no secret key, so anyone can compute a fingerprint for any content. An attacker who can replace an image can usually replace the recorded fingerprint as well. To show who produced a build, protect the fingerprint with a digital signature §5.14 or a message authentication code §5.7, or keep it where the attacker cannot write.

Equal fingerprints say nothing about correctness or safety. Two builds with equal fingerprints are equally right or equally wrong.

A declared list of inputs, such as a manifest §13.4, is only as complete as its author’s inventory. A fingerprint computed from the list inherits every gap in the list.

A fingerprint of the output changes when the build is not reproducible, even if no input changed. Compilers can embed the time of the build, for example through the C macros __DATE__ and __TIME__, or the path of the build directory, so two builds from equal inputs can differ in their output bytes.

Shortened fingerprints collide by chance as the number of builds grows, as the example shows, and give no protection against someone who searches for a collision on purpose. The entry on truncated identifiers §19.3.1 treats this risk in general.

Questions answered

Which omitted input could change behavior without changing the fingerprint?

Any input that affects the build’s output but is not covered by the fingerprint can change behavior without changing the fingerprint, because the hash function never sees that input. A build fingerprint is a short value, usually a cryptographic hash, computed over a declared set of a build’s inputs or over its output, and equal fingerprints show only that the covered items are equal. Inputs that are commonly left out include:

To close the gap, derive the coverage from what the build actually reads, encode the items so that their boundaries are unambiguous, hash with a full-length collision-resistant hash function, and also compare fingerprints of the output images.

Related entries: Content-addressed identity (§13.2), Manifest (§13.4), Source closure (§13.6), Cryptographic hash function (§5.1), Prefix-free encoding (§15.38), Hermetic build (§13.7), Reproducible build (§13.8), Collision resistance (§5.5), Git commit and working tree (§13.19), Public key fingerprints and truncated identifiers (§19.3.1). Further reading: [7].