Section 2.42

Proof-carrying code

Definition

Proof-carrying code (PCC) is a technique in which untrusted code arrives with a formal proof that it obeys a safety policy chosen by the receiver. The receiver, called the code consumer, computes from the code itself the logical statement that must hold for the code to be safe, checks the supplied proof of that statement with a small proof checker, and runs the code only if the check succeeds. Finding the proof is the supplier’s work; the consumer only checks it. George Necula and Peter Lee introduced PCC in 1996 [286].

The idea

An operating system kernel wants to run a packet filter that an ordinary user wrote, at full native speed and without trusting the user. Interpreting the filter with run-time checks is safe but slow. Trusting a signature on the filter says who sent it, not what it does. Analyzing the machine code inside the kernel puts a large and difficult analyzer into the most trusted part of the system.

Proof-carrying code rests on an asymmetry: checking a proof is far easier than finding one. A filled-in Sudoku grid takes a minute to check and may take an hour to solve, and checking it requires no trust in the solver. So the hard part, finding the proof, moves to the untrusted supplier, and the trusted consumer keeps only a simple checker. The picture stops being accurate in one place: the consumer must also make sure that the proof is about this code. It does so by deriving the statement to be proved from the code it received, never from the supplier’s description of it. Necula and Lee’s 1996 paper, Safe Kernel Extensions Without Run-Time Checking, applied this to packet filters in machine code, which then ran without run-time checks.

How it works

  1. The consumer publishes a safety policy. It has three parts: the meaning of every instruction, written in logic; the safety rules, for example that a load from address pp is allowed only if pp lies inside a readable buffer; and the precondition, the facts that hold when the code is entered, for example that register r1 holds the start of the packet and r2 its length.

  2. The producer compiles the program and annotates every loop with a loop invariant §3.24, a condition that holds each time the loop is entered. The annotations need not be trusted: if one is false, no proof will check.

  3. Both sides compute the verification condition. A verification-condition generator (VCGen) walks the code and produces one logical formula, the verification condition (VC), which is true only if every run that starts in a state meeting the precondition obeys the safety rules.

  4. The producer proves the VC with any theorem prover it likes, however large, and encodes the proof as a proof object. Necula wrote proofs in the Edinburgh Logical Framework (LF), in which checking a proof amounts to type checking it.

  5. The consumer checks. It recomputes the VC from the code it received and checks that the proof object proves exactly that formula, using only the inference rules of the agreed logic.

  6. The code runs natively, with no run-time checks for the properties proved.

Because the consumer derives the VC from the code it received, tampering cannot produce unsafe accepted code, and no cryptography is needed for that. If the code or the proof is changed in transit, either the check fails, or the proof still checks, and then the changed code is still safe, though it may not be the code the producer sent.

Worked example

The precondition says that register r1 holds the start address bb of a packet, r2 holds its length nn, the bytes at addresses bb to b+n−1b + n - 1 are readable, and b+n≤264b + n \le 2^{64}, so the buffer does not wrap around the top of the address space. The safety rule allows a 1-byte load from address pp only if b≤p<b+nb \le p < b + n. The program reads byte number r3 of the packet, and the comparison in line 0 is unsigned:

0: if r3 >= r2 goto 3
1: r0 = load byte [r1 + r3]
2: exit
3: r0 = 0; exit

Write ii for the value of r3. Line 1 runs only when the jump in line 0 is not taken, that is when i<ni < n. The VC for the load is therefore ∀b,n,i:(b+n≤264∧i<n)⇒(b≤b+i∧b+i<b+n).\forall b, n, i:\ (b + n \le 2^{64} \ \wedge\ i < n) \Rightarrow (b \le b + i \ \wedge\ b + i < b + n). Read as ordinary integers, i<ni < n gives b+i<b+n≤264b + i < b + n \le 2^{64}, so the machine sum b+ib + i, computed modulo 2642^{64}, does not wrap and equals the ordinary sum. Then i≥0i \ge 0 gives b≤b+ib \le b + i, and i<ni < n gives b+i<b+nb + i < b + n. That short argument, written in the agreed logic, is the proof the producer sends. With n=16n = 16: for i=15i = 15, 15<1615 < 16, and the load reads b+15b + 15, the last byte; for i=16i = 16, the jump is taken and nothing is read.

Now someone changes line 0 to if r3 > r2 goto 3. Line 1 now runs whenever i≤ni \le n, and the consumer’s VCGen produces a VC with the hypothesis i≤ni \le n. At i=n=16i = n = 16 the conclusion b+16<b+16b + 16 < b + 16 is false, so no proof of the new VC exists. The old proof proves a different formula, and the checker rejects it. The off-by-one read is caught without any signature.

The no-wrap fact b+n≤264b + n \le 2^{64} belongs to the trusted policy. If the policy omitted it, the VC would be false for a buffer that ends at the top of the address space, no proof would exist, and a safe program would be rejected: a nuisance, not a hole. The opposite mistake is a hole. A logic rule that treated machine addition as ordinary addition would let a producer prove formulas that are false on the real machine, and the checker would accept them. No check can reveal an error in the policy or the logic that the checker itself relies on.

Stated precisely

Let PP be the received program, Pre\mathrm{Pre} the precondition and SS the safety rules, and let VC(P)=VCGen(P,Pre,S)\mathrm{VC}(P) = \mathrm{VCGen}(P, \mathrm{Pre}, S). The consumer accepts PP with proof π\pi exactly when Check(π,VC(P))\mathrm{Check}(\pi, \mathrm{VC}(P)) succeeds. The guarantee is Check(π,VC(P)) succeeds⇒every run of P from a state satisfying Pre obeys S.\mathrm{Check}(\pi, \mathrm{VC}(P)) \text{ succeeds} \Rightarrow \text{every run of } P \text{ from a state satisfying } \mathrm{Pre} \text{ obeys } S. It rests on six assumptions.

  1. The logic’s axioms and inference rules are sound: they prove only true formulas.

  2. VCGen is sound with respect to the instruction semantics: a true VC implies that every run is safe.

  3. The checker accepts only proofs that follow the rules.

  4. The real processor behaves as the semantics says.

  5. The host enters PP only in states that satisfy Pre\mathrm{Pre}.

  6. The code that runs is exactly the code that was checked.

The trusted computing base §1.4, the set of parts whose errors can break the guarantee, is therefore the policy, the logic, VCGen, the checker, the processor and the loader that connects the checked code to execution. The producer, its compiler, its theorem prover, the loop invariants and the proof itself are outside it. Foundational proof-carrying code (Appel, 2001) [287] removes VCGen from that list: it defines safety directly from a formal description of the machine, so the proof covers the whole argument.

Limits and common mistakes

A proof establishes only the policy that was written down. A proof of memory safety says nothing about termination, resource use or whether the code’s decisions are right, unless the policy states those properties.

PCC does not authenticate. Anyone can send safe code with a valid proof; whether that sender may install code is a separate authorization §1.7 decision.

Proofs cost. A proof can be larger than the code, and checking it takes time and memory on the consumer, so proof size and checking need their own limits. A prover may fail to find a proof for a program that is in fact safe, and that program is then rejected.

The checked code must be the code that runs. Translating it after the check, for example with a just-in-time compiler §2.14, needs its own correctness argument.

PCC is one of several ways to place the proof effort. The Linux eBPF §2.1 (extended Berkeley Packet Filter) verifier searches for the safety argument itself, inside the kernel, by abstract interpretation §3.4, and receives no certificate, so the whole analyzer is part of the trusted base. Typed assembly language (Morrisett and others, 1998) [288] is a close relative of PCC in which the evidence takes the form of type annotations §12.46 on machine code.

Questions answered

Which specification and checker remain trusted?

The specification and checker that remain trusted are the consumer’s own: its safety policy, its logic, its verification-condition generator and its proof checker. Proof-carrying code ships untrusted code together with a formal proof that the code obeys the receiver’s policy, and the receiver runs the code only if the proof checks. In detail, the receiver must trust:

The producer, its compiler, its theorem prover and the proof itself need not be trusted, because a false or mismatched proof fails the check.

Related entries: Loop invariant (§3.24), eBPF verifier (§3.3), Trusted computing base (TCB) (§1.4), Soundness (§3.7), Hoare triple (§3.31), Bytecode authenticity versus safety (§2.43). Further reading: [71, 165, 286, 287, 288].