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 is a security invariant has four parts.
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.
Initial states. Say which states the system can start in, for example the state in which it leaves the factory.
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.
Obligations. The base case: holds in every initial state. The inductive step: for each transition, if holds before it, then 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 does not mention, for example that a stored fingerprint describes data that was checked. The repair is to strengthen by adding that fact and to prove the stronger condition, which implies . 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 contents of slot 0 and slot 1.
The selector , one byte in flash with the value 0 or 1, naming the active slot: the slot whose image the device runs at boot.
, a variable in RAM that holds either the value none or a digest of an image. The digest is computed with SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output), a cryptographic hash function §5.1 that maps any data to a 32-byte value.
The invariant to prove is : the image in slot 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. alone is not inductive, because the step for activation needs a fact about . The proof therefore uses together with : if is not none, then 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, and . holds because A is signed, and holds because .
Transitions. Each of the five transitions keeps both and true.
Write. Anyone, the attacker included, writes any bytes at any offset of the inactive slot, slot , at any time except while Activate runs. The active slot, and do not change, so and still hold. The design does not depend on a write clearing .
Verify. The device computes the digest of the inactive slot once and checks the signature against , 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 to ; otherwise it sets to none. The active slot is untouched, so holds. receives only the digest of a signed image, so holds.
Activate. With writes to the inactive slot blocked until it finishes, the device computes the digest of the inactive slot again. If is not none and the digest equals , it sets to with one write, then sets to none. The new active image has digest , and by , 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 holds. Setting to none keeps .
Reset. Power fails or the device restarts at any instant. RAM is lost, so becomes none and holds. Flash keeps both slots and . Writes only ever touch the inactive slot, and 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 holds.
Boot. The boot code runs the image in slot and changes nothing, so and 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 to none, so Activate refuses. The vendor’s signed image B is then written into slot 1, and Verify sets to the digest of B. The attacker changes the byte at offset 100 of slot 1. still holds the digest of B, so still holds, but Activate computes the digest of the changed slot, finds that it differs from , and refuses. After B is written again, Activate finds equal digests and sets to 1. In every state of the run, the active slot holds A or B.
Now replace 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. 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 false is Activate, but the defect lies in what Write does to the fact that Activate relies on.
Stated precisely
Let be the set of states, let be the set of initial states, and write when the system can move from state to state 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 , a condition that is true or false of each state, is an invariant if holds in every reachable state . It suffices to prove the two obligations By induction on the length of the sequence, then holds in every reachable state. The second obligation must hold for every state that satisfies , reachable or not. When it fails only in states that cannot be reached, find a stronger predicate that implies and satisfies both obligations, and prove instead; in the example, is “ and ”.
The conclusion rests on four assumptions.
Complete transition list: the transitions include every way the state can change under the threat model and the fault model §1.30.
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.
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.
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 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 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 : the image in the active slot carries a valid vendor signature. The device has two flash slots, a selector in flash that names the active slot, and a variable 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 , so the proof carries a second condition : if is not none, it is the digest of a signed image.
Base case: the factory state has the signed image A in slot 0, and , so and hold.
Write: anyone writes any bytes into the inactive slot only; the active slot, and are unchanged, so both conditions hold.
Verify: sets to the inactive slot’s digest only if its signature is valid, and to none otherwise; holds, and the active slot is untouched.
Activate: with writes blocked, switches only if is not none and the recomputed digest of the inactive slot equals ; by and the collision resistance of the hash, the new active image is signed; then becomes none.
Reset at any instant: RAM loss sets to none; flash keeps a complete active image and a selector that holds its old or its new value.
Boot: runs slot and changes nothing.
By induction on the number of transitions, 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).