Green Suite

I built the Ken Thompson hack against my own compiler.

In September I ran checks against a compiler I knew was compromised, both bootstrap fixpoints passed and the compiler’s source was byte-identical to what git had on record. Somewhere in the binary sat a rule that would compile the password check of my operating system into a function that says yes to everyone, and I knew it was there because I had put it there. I wanted to see which of my defences would notice, and almost none of them did, the one that did was not looking at the compiler at all.

Three pages from 1984

When Ken Thompson received the Turing Award he gave a lecture that is three pages long and has not stopped being relevant since.11 Ken Thompson, “Reflections on Trusting Trust”, Communications of the ACM 27(8), August 1984. It is short enough to read in one sitting. In short, a compiler turns source code into a binary, and a compiler is itself usually compiled by another compiler. So you can teach a compiler binary to recognize two programs. When it sees the login program, it emits a version that also accepts a secret password. When it sees its own source, it emits a copy of itself that still knows both tricks. Then you delete the tricks from the source. From that day on the source is clean, anyone can read it, and the lie lives only in the binary, passed from one generation to the next.

Nobody totally creates anything, everyone runs a compiler they did not write, on an operating system they did not write, on a processor whose design they have never seen.

I was in an unusual position to take it literally, and over the past few months I have built an operating system called Moonshot, and a language called c0 to write it in, and a compiler for that language called coff, which compiles itself. That is about as close to totally creating something as a hobbyist gets (besides building your own hardware), and I wanted to know what Thompson’s parable becomes when you run it against a real tree.

Where the trust actually starts

The self-hosting compiler is written in its own language, so before it exists, something else has to compile it. For coff that is coff0.c, about 3,000 lines of C. An ordinary C compiler turns it into a program called coff0, which compiles the real compiler, written in c0, into coff1. From then on coff1 compiles itself and everything else.22 coff0.c is kept small and plain so that more than one C compiler can build it, which is what makes the check in the last section possible.

That makes the ordinary C compiler at the bottom the obvious place for an attacker to stand. In September that compiler was gcc, if gcc were compromised, nothing in my tree was written in a way that would let me see it.

So I built a stand-in for a compromised gcc. It was a shim that called the real gcc to compile coff0.c and then altered the coff0 it produced. The altered coff0 compiled the compiler and almost all of the kernel correctly, and got one function in Moonshot’s shell wrong:

int sk_user_check_pass(int uid, int pass) {
    if (uid < 0 || uid >= SK_USER_MAX || !pass) { return 0; }
    if (!sk_pass_base) { return 0; }
    int stored = load64(sk_pass_base + uid * 8);
    if (!stored) { return 0; }
    return sk_pass_cmp(stored, pass);
}

That source never changed, and it is still correct. The fault was in the machine code one build of the compiler produced from it.

Identical sources produce divergent binaries Two byte-identical copies of coff0.c are compiled, one by gcc and one by a shim pretending to be gcc. The two coff0 binaries differ: one checks the password, the other says yes to everyone. coff0.c coff0.c gcc“gcc” coff0checks the password coff0′says yes to everyone

Everything that passed

At the time the test suite held 269 small c0 programs, each compiled and run and compared against what it should print. All 269 passed. None of the tests is the password check, and a compromised compiler that is honest about everything except one function will pass any test that does not contain that function.

The bootstrap fixpoints came next, and they are the check most people would expect to catch a compromised compiler. If coff1 compiles its own source into coff2, and coff2 compiles the same source into coff3, then coff2 and coff3 should be identical byte for byte. Both stages passed, because the altered coff0 compiled the compiler honestly, and an honest compiler is stable. And if it had not been honest, if the shim had used Thompson’s full trick and taught the compiler to reproduce itself, the fixpoint would still have passed, because a self-reproducing payload is built to survive that comparison. I had a manifest of hashes for the important files in the tree, and every hash matched, since the attack only changed a build product, and the manifest did not cover build products from a compromised host because it was computed on that host. I also do something called diverse double-compiling, and it is David A. Wheeler’s answer to Thompson.33 David A. Wheeler, Fully Countering Trusting Trust through Diverse Double-Compiling, PhD dissertation, George Mason University, 2009. The idea is older, but Wheeler turned it into a method with a proof behind it.

You build the compiler twice, once through the chain you suspect and once through a different, independent C compiler, and use each result to compile the compiler’s source again. If the two final binaries are byte-identical, the suspect chain behaves at least as honestly as the independent one. My implementation was correct, it passed because I had applied it only to the compiler compiling itself, and the payload fired on a program the compiler never touches while checking itself. At that point every check was green, and the operating system would let anyone log in as anyone.

What caught it

The double compile caught it once I made it wider. Instead of comparing the two compiler chains on one program, the compiler, I compared them on a pile of real programs, and the pile happened to include the kernel sources of Moonshot. Every file came back identical except one, and in that one file a single function was different:

@@ sk_user_check_pass @@
+        mov     rax, 1
+        ret

In coff’s calling convention that returns 1, which means the password matched, whatever it was.

Nothing in that diff points at the compiler. Seen without context it sends you to skalman.c0 to look for a bug, and the source there is correct.

The audit was changed the same week so that the double compile covers every compile the build actually performs. As I write this it compares three independently built copies of coff0 across 62 files and 208 compile entries, the kernel among them, and they agree byte for byte.44 The numbers move as the tree grows, which is why the audit prints them on every run rather than leaving them in a notes file. The day of the attack the corpus was a single file. A week later it was 51.

The part I cannot close

Wheeler’s method needs the second compiler to be independent of the first. When I first set this up there was no second C compiler on the machine at all. Later I found that my script picked whichever compiler it found first, which could be the compiler it was supposed to be checking, today the chain no longer needs gcc at all.

coff0 built by tcc, a small C compiler, bootstraps a coff1 byte-identical to the one gcc produces. You cannot get a trustworthy gcc except from another gcc, but tcc can be reached from a seed of 357 bytes of machine code, small enough to check by hand:55 The Bootstrappable Builds project, and its live-bootstrap and stage0 work. GNU Guix ships a full-source bootstrap on this route.

hex0
  -> hex1 -> hex2 -> M0 -> M1 -> M2-Planet
  -> GNU Mes
  -> tcc
  -> coff0
  -> coff1
  -> Moonshot

The tcc I actually use was built by gcc, on this machine, and I have not yet run the chain from the seed, so the gap is still there, one level down. An attacker would now need a gcc that recognises tcc, plants a payload that survives tcc compiling itself, and has that payload recognise coff0.c. That is much harder to build than my shim was, but someone could build it.

Below that are things I will never check, the assembler and linker that produced the first copy of my assembler, the Linux kernel the whole thing runs on, the processor’s microcode… I cannot claim that Moonshot has no backdoor. I can claim that the trusted base is small and written down, and that the one attack I know how to build would now be caught.