Section 1.23

Security invariant

Definition

A security invariant is a condition on a system’s state that must be true in every state the system can reach, chosen so that it implies a security property. “The image in the active firmware slot carries a valid vendor signature” is an example. An invariant is proved by induction: it holds in every initial state, and every possible transition from a state where it holds leads to a state where it holds. Every transition counts, including errors, resets, recovery code, concurrent tasks and the attacker’s own actions.

The idea

An update system promises that only vendor-signed firmware ever runs. Testing the normal sequence, which downloads an image, checks its signature, installs it and restarts, shows the promise holding in the states that this sequence visits. An attacker does not follow that sequence. The attacker writes to the download area after the check, cuts the power in the middle of installation, or sends the install command twice. The promise is only as good as the worst state that any such sequence can reach.

An invariant turns “every sequence” into a finite amount of work. Sequences are endless, so the proof checks transitions one at a time instead: if the condition holds before a step, it holds after the step. Then no sequence can contain a first step that breaks it, so no sequence breaks it at all. A row of dominoes gives the picture: show that the first one falls and that each falling domino knocks over the next, and every domino falls, however long the row. The picture stops at one point. Dominoes stand in a single line, while a system can branch: from one state, many different transitions may be possible, and the step argument must cover all of them.

How it works

A proof that a condition II is a security invariant has four parts.

  1. State. List the variables that make up the state, both those kept in storage that survives a reset, such as flash memory §6.6, and those kept in RAM, which a reset clears.

  2. Initial states. Say which states the system can start in, for example the state in which it leaves the factory.

  3. Transitions. List every way the state can change: each operation of the normal flow, each error branch, a reset or power loss at every point where one can occur, the code that runs at boot or during recovery, each interleaving of concurrent tasks, and every action that the threat model §1.2 gives the attacker. A transition that the list misses is a transition about which the proof says nothing.

  4. Obligations. The base case: II holds in every initial state. The inductive step: for each transition, if II holds before it, then II holds after it.

Often the inductive step fails for a condition that is nevertheless true in every reachable state §3.21. The step for one transition needs a fact that II does not mention, for example that a stored fingerprint describes data that was checked. The repair is to strengthen II by adding that fact and to prove the stronger condition, which implies II. A condition whose inductive step succeeds on its own is called an inductive invariant §19.2.4; the inductive invariant entry states its obligations formally. A loop invariant §3.24, a condition preserved by each pass of one loop, is the same reasoning applied to one loop of a program; a security invariant applies it to the whole system, with the attacker’s actions among the transitions.

Worked example

A device has two firmware slots, two regions of flash memory that each hold one complete firmware image. Its state has three parts.

The invariant to prove is II: the image in slot σ\sigma carries a valid digital signature §5.14 made with the vendor’s private key, a value that only the holder of that key can produce and that the device can check with the matching public key. II alone is not inductive, because the step for activation needs a fact about VV. The proof therefore uses II together with JJ: if VV is not none, then VV is the digest of some image that carries a valid vendor signature.

Base case. The device leaves the factory with the vendor-signed image A in slot 0, slot 1 erased, σ=0\sigma = 0 and V=noneV = \text{none}. II holds because A is signed, and JJ holds because V=noneV = \text{none}.

Transitions. Each of the five transitions keeps both II and JJ true.

  1. Write. Anyone, the attacker included, writes any bytes at any offset of the inactive slot, slot 1−σ1 - \sigma, at any time except while Activate runs. The active slot, σ\sigma and VV do not change, so II and JJ still hold. The design does not depend on a write clearing VV.

  2. Verify. The device computes the digest dd of the inactive slot once and checks the signature against dd, not against a second reading of the slot, so a write that arrives meanwhile cannot make a valid signature vouch for other bytes. If the signature is valid, it sets VV to dd; otherwise it sets VV to none. The active slot is untouched, so II holds. VV receives only the digest of a signed image, so JJ holds.

  3. Activate. With writes to the inactive slot blocked until it finishes, the device computes the digest of the inactive slot again. If VV is not none and the digest equals VV, it sets σ\sigma to 1−σ1 - \sigma with one write, then sets VV to none. The new active image has digest VV, and by JJ, VV is the digest of a signed image. Collision resistance §5.5, the property that no feasible computation finds two different inputs with the same digest, then implies that the new active image is that signed image, so II holds. Setting VV to none keeps JJ.

  4. Reset. Power fails or the device restarts at any instant. RAM is lost, so VV becomes none and JJ holds. Flash keeps both slots and σ\sigma. Writes only ever touch the inactive slot, and σ\sigma is assumed to hold either its old or its new value after an interrupted write, so the active slot holds a complete signed image and II holds.

  5. Boot. The boot code runs the image in slot σ\sigma and changes nothing, so II and JJ hold.

A run shows the guards at work. The attacker writes an unsigned image X into slot 1; Verify finds the signature invalid and sets VV to none, so Activate refuses. The vendor’s signed image B is then written into slot 1, and Verify sets VV to the digest of B. The attacker changes the byte at offset 100 of slot 1. VV still holds the digest of B, so JJ still holds, but Activate computes the digest of the changed slot, finds that it differs from VV, and refuses. After B is written again, Activate finds equal digests and sets σ\sigma to 1. In every state of the run, the active slot holds A or B.

Now replace VV by a one-bit flag that Verify sets to 1 after a valid signature and that only Activate clears. The same run ends badly: Verify sets the flag after B is written, the attacker changes byte 100, and Activate, seeing the flag set, makes the changed image active. II fails. The proof shows where. The step for Activate needs the fact “flag set implies that the inactive slot is signed”, and the Write transition does not preserve that fact. The first transition that makes II false is Activate, but the defect lies in what Write does to the fact that Activate relies on.

Stated precisely

Let SS be the set of states, let S0S_0 be the set of initial states, and write s→ts \to t when the system can move from state ss to state tt in one transition of any of the kinds listed above. A state is reachable if some finite sequence of transitions leads to it from an initial state. A predicate II, a condition that is true or false of each state, is an invariant if I(s)I(s) holds in every reachable state ss. It suffices to prove the two obligations I(s) for every s∈S0,andI(s) and s→t imply I(t) for all s,t∈S.I(s) \text{ for every } s \in S_0, \qquad\text{and}\qquad I(s) \text{ and } s \to t \text{ imply } I(t) \text{ for all } s, t \in S. By induction on the length of the sequence, II then holds in every reachable state. The second obligation must hold for every state that satisfies II, reachable or not. When it fails only in states that cannot be reached, find a stronger predicate I*I^{*} that implies II and satisfies both obligations, and prove I*I^{*} instead; in the example, I*I^{*} is “II and JJ”.

The conclusion rests on four assumptions.

  1. Complete transition list: the transitions include every way the state can change under the threat model and the fault model §1.30.

  2. Atomic steps: each listed transition happens as one indivisible step, or each point where it can be interrupted is listed as a transition of its own.

  3. Cryptographic assumptions: properties used in a step, such as collision resistance of the hash function or unforgeability of signatures, hold; the conclusion then holds except with negligible probability.

  4. Faithful code: the code carries out each transition as the model describes it.

An invariant expresses a safety property §1.11, a property saying that a bad state is never reached.

Limits and common mistakes

An invariant can be true and too weak. “The selector holds 0 or 1” is an invariant of the example, yet it says nothing about who wrote the active image. Check separately that the invariant implies the security property that is claimed.

The proof covers only the transitions it lists. A hardware unit that copies data into flash without the processor, a debug port, a recovery mode in the bootloader §6.30, or a second task that writes the inactive slot while Activate runs each changes the state, and each needs its own step in the proof.

Atomicity §8.3 is an assumption to check. The example assumes that an interrupted write leaves σ\sigma with its old or its new value. Flash hardware does not always guarantee this, and designs then keep the selector in a record protected by a checksum §4.32 and fall back to the previous record when the new one is damaged.

Some security properties concern history rather than the current state, for example “never run a version older than one already run”. Such a property becomes an invariant only once the state records the history it needs, here the highest version installed so far, kept in a monotonic counter §8.28: a stored value that the code only ever increases.

Checking the condition at run time is a different defense from proving it. For example, a bootloader configured to require a signature checks each image before starting it and rejects one that fails verification. A proof shows that no violation is reachable under the listed transitions and assumptions.

A fault outside the fault model, such as a bit that flips by itself in flash, can break II with no listed transition. Either add such faults to the transitions or state that they lie outside the claim. An invariant also cannot say that something good eventually happens, such as an update eventually being installed; that is a liveness property §1.12, which needs a different kind of proof.

Questions answered

Show the base case and every transition that preserves the invariant.

For a firmware update device with two slots, one base case and five transitions prove the invariant II: the image in the active slot carries a valid vendor signature. The device has two flash slots, a selector σ\sigma in flash that names the active slot, and a variable VV in RAM that holds either none or the digest (a cryptographic fingerprint) of an image whose signature was verified. The step for activation needs a fact about VV, so the proof carries a second condition JJ: if VV is not none, it is the digest of a signed image.

By induction on the number of transitions, II holds in every reachable state. The proof is complete only if this list contains every way the state can change. A recovery mode, a second writer during Activate or a debug port would each need its own step. A proof that checks only Verify and Activate omits the Write and Reset transitions, which are where real designs fail: a design that records “verified” as a flag, which a later write to the slot does not clear, passes the Verify and Activate steps and still activates altered bytes.

Related entries: Loop invariant (§3.24), Fault model (§1.30), Failure atomicity (§8.5), Inductive invariant and its three obligations (§19.2.4), Safety property (§1.11), Monotonic counter (§8.28).