Compiler & Toolchain Attacks
Supply Chain & Binary Attacks · Advanced Exploitation track
8 min read
In this module
The Tool You Cannot Inspect
In 1984, Ken Thompson — co-creator of Unix and C — delivered his Turing Award lecture, "Reflections on Trusting Trust." It's one of the most unsettling papers in computer security. Thompson posed a deceptively simple question: can you trust the compiler you use to build your software?
His answer was no. The implications haven't aged.
The Thompson Hack
Thompson described a two-stage attack on the C compiler. In the first stage, he modified the compiler's source code to recognise when it was compiling the Unix login program. When it detected the login program, the compiler would inject a backdoor: an extra username and password that would grant access to any system running the compiled login binary.
This alone is interesting but detectable - anyone reading the compiler source code would see the malicious modification. Thompson's second stage made it far more insidious.
In the second stage, he modified the compiler to also recognise when it was compiling itself. When the compiler detected that it was compiling a new version of the compiler, it would inject the code for both backdoors into the new compiler. Then he compiled the modified compiler, producing a backdoored compiler binary.
Here is the critical insight: he then reverted the source code changes. The compiler source code was now clean, with no trace of the backdoor. But the compiled compiler binary still contained the backdoor logic. When this clean source code was compiled using the backdoored compiler binary, the resulting compiler also contained the backdoor - because the running compiler injected the malicious code during compilation.
Step 1: Modify compiler source to inject backdoorsStep 2: Compile the modified source → backdoored compiler binaryStep 3: Revert source changes → source is cleanStep 4: Compile clean source with backdoored binary → STILL backdoored
The backdoor propagates through the binary, not the source.Ken Thompson's "Trusting Trust" attack demonstrates that a compiler binary can contain backdoors not present in its source code. The backdoored compiler recognises when it is compiling itself and injects the backdoor into the new version. Since you need a compiler to build a compiler, the backdoor can propagate indefinitely through binary lineage, invisible to source code inspection.
Step through the attack, then the check that catches it. The last three steps preview diverse double-compilation, which comes up later in this lesson.
Trusting Trust, then diverse double-compilation
Why This Matters
Thompson's attack reveals a structural problem: you cannot fully verify a program by reading its source code if you don't also trust the compiler that produced the binary. And trusting the compiler requires trusting the compiler that compiled the compiler — an infinite regression.
This isn't an abstract thought experiment. Every developer relies on tools they didn't build: GCC, Clang, MSVC, linkers, assemblers, Python, Node.js, the OS itself. If any of them is compromised, every program they touch could be compromised — no matter how carefully the source code was written or reviewed.
Compromised Toolchains in Practice
Thompson's exact attack, a backdoor that survives in the compiler binary after it's removed from the compiler's source, hasn't been publicly documented in the wild. The closest real example is W32/Induc (2009), a virus that patched Delphi installations: it modified the compiler's library source (SysConst.pas) on the developer's machine, so every program built there carried the virus, and those programs infected any other Delphi installations they found. It spread through toolchains rather than source repositories. Other toolchain compromises have occurred too:
XcodeGhost (2015): Attackers distributed a modified version of Apple's Xcode development environment through Chinese file-sharing sites. Developers who used the trojanized Xcode unknowingly built iOS applications that contained malware. Dozens of apps in the official App Store were found to be compiled with the modified Xcode, including widely-used apps with millions of users.
The attack worked because Xcode is a large download, and Apple's servers were slow to access from China. Developers downloaded a faster copy from an unofficial source, not realising it had been modified. The modified Xcode injected a malicious library into every app it compiled.
CCleaner (2017): The build environment for CCleaner, a widely-used system utility, was compromised. The attacker inserted malicious code into the build process, and the trojanized version was signed with Avast's legitimate code signing certificate and distributed through the official update mechanism. Over 2 million users installed the compromised version.
These attacks targeted the development tools rather than the source code, making them invisible to source code review. The output of the build process was malicious even though the source code was clean.
Linker and Build System Attacks
The compiler is not the only tool in the build pipeline. The linker, which combines compiled object files into an executable, is another target. A compromised linker could:
- Inject additional code sections into the final binary
- Modify symbol resolution to redirect function calls to malicious implementations
- Add hidden initialisation functions that execute before main()
Build systems (Make, CMake, Bazel, Gradle) are also targets. A compromised build configuration could:
- Download and execute additional code during the build
- Modify compiler flags to disable security features (removing stack canaries, disabling ASLR)
- Include additional source files that are not part of the visible project
The build system is particularly vulnerable because build configurations are complex, often poorly understood, and rarely audited with the same rigor as application source code.
Reproducible Builds: The Defence
The primary defence against toolchain attacks is reproducible builds: ensuring that compiling the same source code with the same tools on different machines produces a bit-for-bit identical binary.
If a build is reproducible, anyone can independently compile the source and compare the result to the distributed binary. If they match, the binary was produced from the published source code. If they do not match, something was altered - either the source, the tools, or the build process.
Achieving reproducibility is harder than it sounds. Many factors can cause non-determinism in builds:
- Timestamps: Compilers embed compilation timestamps in the binary. Using a fixed timestamp or stripping them is necessary.
- File ordering: If the build system processes files in non-deterministic order (e.g., directory listing order varies between file systems), the output may differ.
- Absolute paths: Compilers may embed the full path to source files in debug information. Building in the same directory path on every machine addresses this.
- Randomisation: Some compiler optimisations use random seeds. These must be fixed or deterministic.
- Tool versions: Even minor version differences in the compiler, linker, or system libraries can produce different output. The exact tool versions must be specified and used.
Projects like Debian, Tor, and Bitcoin have invested significant effort in achieving reproducible builds for their distributed binaries. It requires discipline and tooling, but it provides a strong guarantee that the distributed binary matches the published source.
Diverse Double-Compilation
Computer scientist David A. Wheeler proposed a defence against Thompson's compiler attack called diverse double-compilation (DDC). The idea is:
- Take the source code that the suspect compiler binary (C) claims to be built from
- Compile that source with a different, independently developed compiler T, giving stage 1. It should behave like C but its bytes will differ, because T optimises differently
- Compile the same source again, this time with stage 1, giving stage 2
- Compare stage 2 bit-for-bit with the suspect binary C
If they're identical, the binary C faithfully corresponds to its source: a Thompson-style backdoor hidden only in C's binary would have produced different output. The comparison needs a deterministic, reproducible build, and it assumes T and C don't carry the same backdoor, which is extremely unlikely for independently developed compilers.
This technique breaks the infinite regression of trust by using an independent compiler as a reference point. It does not require trusting either compiler absolutely - it only requires that they are not both compromised in the same way.
The Trust Stack
Thompson's insight extends beyond compilers. The entire stack of tools you use forms a chain of trust:
Your application source code ↓ compiled byThe compiler ↓ compiled byA previous compiler version ↓ running onThe operating system ↓ running onThe CPU hardware and firmwareAt each level, a compromise can affect everything above it. The CPU could have hidden instructions (as discovered in various undocumented x86 instructions). The firmware could be backdoored. The OS could be compromised. The compiler could inject code.
This doesn't mean all software is untrustworthy. It means security depends on the entire pipeline from source to execution, and source code quality is only one part of it. Reproducible builds, diverse compilation, hardware verification, layered defences: each one adds a fragment of assurance that the software you're running is the software you intended to run.