Definition
A reproducible build is a build that anyone can repeat with an identical result: given the same source code, the same declared build environment and the same build instructions, any party obtains bit-for-bit identical copies of every artifact the project specifies, such as a program file or an archive. The Reproducible Builds project gives this definition. Identity is checked by comparing digests, short fingerprints of the bytes computed by a cryptographic hash function §5.1. Reproducibility §11.43 concerns the output bytes; a hermetic build §13.7 concerns which inputs can reach the build.
The idea
A software project publishes its source code and a ready-built program. Users run the program, not the source, and nothing in the program file shows that it was built from that source. A compromised build server could have added a back door, and the source would still look clean. If the build is reproducible, anyone can rebuild from the published source and compare digests. Equal digests show that the published program corresponds to the source. A different digest exposes a difference that someone must explain. Software distributions such as Debian rebuild their packages independently for this purpose.
The obstacle is that ordinary builds record facts about the moment and the machine in their output: the time of the build, the directory it ran in, the user’s name, random choices, the order in which the file system listed a directory, and the versions of the tools. Each such fact makes two honest builds of the same source differ. Making a build reproducible means finding every such byte and either removing it or fixing it to a value that the declared inputs determine.
How it works
Name the outputs. State exactly which files are compared, and whether each whole file is compared or only a named part of it.
Declare the inputs. Fix the source revision, the exact tool versions, the build options and the environment values, as a hermetic build does.
Remove variation at its source. Find every byte that depends on something outside the declared inputs, and make it depend on the declared inputs only, with the remedies listed below.
Rebuild in varied environments. Build at least twice with deliberately different time, time zone, locale, user, build directory, directory listing order and number of processors, preferably on different machines run by different people.
Compare. Compare the digests. If they differ, compare the files with a tool that understands their structure, such as diffoscope, which reports which field inside the file differs.
The usual remedies, by source of difference:
Timestamps: take every date from
SOURCE_DATE_EPOCH, an environment variable defined by the Reproducible Builds project [404] that holds a time in seconds since 1 January 1970 UTC (Coordinated Universal Time), usually the time of the last change to the source; or leave the date out.Paths: build in a fixed directory, or tell the compiler to rewrite the directory prefix that it writes into debug information §12.9 and assertion messages (the option
-ffile-prefix-mapof common C compilers).Random values and order: derive seeds and identifiers from the inputs, and sort file lists and the contents of hash tables §14.7 by name, in byte order §4.21, before writing them. (Python §12.23, for example, randomizes the hashing of text strings in each process unless the variable
PYTHONHASHSEEDfixes it, so the order of a set of names changes between runs.) Sign as a separate step, because many signature algorithms §14.1 draw fresh random numbers.Tools and their settings: pin the toolchain §12.2, fix the locale and time zone, and set the ownership and permission bits recorded in archives to fixed values.
Archives need extra care, because an archive stores metadata about each stored file, called a member, next to its content. A tar archive (the name comes from tape archive) stores each member as a 512-byte header followed by the content. The header holds the member’s name, permission bits, numeric user and group identifiers, user and group names, size and modification time, and a checksum §4.32 over the header bytes. The pax format, defined by POSIX (Portable Operating System Interface, the standard for Unix-like systems) [405], adds extended header records of the form keyword=value, which can carry access times, status-change times and modification times finer than one second. Two packagings of identical files therefore differ when the packagers’ user, group, permission mask (the set of permission bits that newly created files do not get), clock or directory order differ, or when extended headers carry access times. POSIX even recommends a name for each extended header that contains the archiver’s process number, which differs in every run, so a reproducible archive must not follow that recommendation. A normalized archive fixes all of this: members sorted by path in byte order; user and group identifiers 0 and names empty; fixed permission bits; one declared modification time for every member; no access or status-change times. A compressor needs the same treatment: the gzip format stores a modification time and the original file name in its own header, and gzip -n leaves both out.
Worked example
Alice and Bob each put a copy of the same 3-byte file report.txt, holding the bytes 68 69 0a (the text hi and a line end), into a tar archive in the POSIX ustar format, whose header for each member is 512 bytes long. Alice has user identifier 1000 and user name alice; Bob has 1001 and bob. Both belong to group 100, named users, and both copies have permission bits 644 in octal. Alice’s copy was last modified at Unix time 1,700,000,000, counted in seconds since 1 January 1970; Bob’s copy, written one hour later, at 1,700,003,600.
The header stores numbers as octal digits written as text. The user identifier field holds 0001750 for Alice and 0001751 for Bob, since . The modification time field holds 14524770400 for Alice and 14524777420 for Bob. The user name field holds alice or bob, followed by zero bytes. Apart from these three fields and the checksum, the two headers are equal.
The checksum is the sum of all 512 header bytes, with the checksum field itself counted as 8 space characters. Compare the two sums field by field.
User identifier: the texts differ only in the last digit. The character
1has byte value 49 and0has 48, so Bob’s sum is 1 higher.User name:
aliceadds andbobadds , so Bob’s sum is lower.Modification time: each digit character has byte value 48 plus the digit, and both texts have 11 digits. Alice’s digits add up to and Bob’s to , so Bob’s sum is higher.
In total Bob’s checksum differs from Alice’s by : it is 5041 against 5234, stored as the octal texts 011661 and 012162.
So three header fields carry facts about the packager, and a fourth, the checksum, changes because they do. The content bytes are identical, yet the two archives have different SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output) digests. Normalizing removes all four differences: both packagers set the user and group identifiers to 0, the user and group names to empty text, and the modification time to one declared value, for example 1,700,000,000. The two headers, and therefore the two archives and their digests, are then identical.
Stated precisely
Let be the source, the declared toolchain and environment values, and the specified artifacts. Write for artifact as produced by build run . The build is reproducible when, for every two runs and made by any parties from under ,
In practice the check compares digests, , where is a cryptographic hash function such as SHA-256. Equal digests imply equal bytes under the assumption that is collision resistant §5.5: nobody can feasibly find two different byte strings with the same digest. The reference digest must itself come from a trusted place, or an attacker can replace an artifact and its digest together.
Two matching runs establish that those two runs agreed. A claim about every permitted environment rests on rebuilds that varied the environment, made by parties independent of the original builder. A weaker claim compares with , where is a stated normalization, a function that deletes or overwrites named fields such as an embedded signature. Such a claim must name ; calling it byte-for-byte identity is false.
Regenerate-and-compare checks
Some repositories store files that a program, called a generator, produces from other inputs, such as a table of constants generated from a specification. A stored generated file goes stale when its inputs or the generator change and nobody runs the generator again. A staleness check runs the generator again, writes the fresh output to a temporary place and compares it byte for byte with the stored copy; the check fails if they differ. Generators often offer this as an option named --check.
The check is possible only if the generator is itself reproducible: every byte it writes must be fixed by its declared inputs. A generator that writes the current date, a random identifier or a newly assigned serial number produces different bytes in every run, so its check fails every time. Each such value must be pinned, meaning read from a declared input, such as an identifier stored in the specification, instead of created during the run.
A passing check proves only that the stored copy equals what the current generator writes from the current inputs. It does not prove that the generator or its inputs are correct, nor that a program built later contains the checked copy: a build can compile a different copy, reuse a stale output left in its build directory, or select another configuration. To tie a built program to the checked file, rebuild from an empty build directory, or, where the program embeds the generated content unchanged, extract that content and compare its digest with the digest of the checked copy.
Limits and common mistakes
A reproducible artifact can be malicious or wrong. Reproducibility shows that the artifact corresponds to its source; it says nothing about what the source does.
Independence matters. If every rebuilder uses the same compromised compiler, all of them get the same compromised output. Ken Thompson’s 1984 lecture “Reflections on Trusting Trust” described a compiler that inserts a back door into the login program it compiles and inserts the same trick into every new copy of the compiler it compiles, so the back door survives even when the compiler’s source is clean [239]. Rebuilding with different toolchains, as in David A. Wheeler’s method of diverse double-compiling [406], counters this.
A rebuild must really rebuild. If the second build takes its output from a cache, or the comparison checks an artifact against a copy of itself, equality is automatic and proves nothing.
Comparing only part of an artifact, such as the code section of a program, supports a claim about that part only.
A signature cannot be reproduced without the signing key. Rebuilders compare the unsigned artifact, then check that the published signature verifies over the rebuilt bytes.
A reproducible build is not scientific reproducibility. Rebuilding software from the same inputs is different from repeating an experiment with new measurements and reaching the same conclusion.
Questions answered
Which timestamps, paths, random values, and tools prevent byte-for-byte identity?
Every timestamp, path, random value or tool property that the build writes into its output prevents byte-for-byte identity between two builds of the same source.
Timestamps: the compiler’s date and time macros, build dates in version strings, modification times stored in tar, zip and other archives, and the time field in a gzip header.
Paths: the absolute build directory written into debug information and assertion messages, the user’s home directory, and randomly named temporary directories.
Random values: identifiers generated afresh in each run, symbol names that the compiler generates from random seeds, hash-table iteration orders that change from run to run, and signatures made with fresh random numbers.
Tools: the versions of the compiler, linker §12.4, archiver and compression library, and the settings they inherit: locale, time zone, the builder’s user and group, the permission mask that decides the permission bits of new files, and the order in which the file system lists a directory.
Each one is removed by leaving it out of the output or by fixing it to a value that the declared inputs determine, such as one declared time for every date, a fixed build directory, seeds derived from the inputs, and pinned tools with fixed settings. To find the differences that remain, build twice in deliberately different environments and compare the two outputs field by field. Explain or remove every differing byte rather than ignoring it.
Related entries: Hermetic build (§13.7), Toolchain (§12.2), Deterministic replay (§10.37), Source closure (§13.6), Content-addressed identity (§13.2), tarfile module (§15.30). Further reading: [239, 404, 405, 406].