Section 8.21

Commit record and selector

Definition

A commit record is a small record in persistent storage whose valid presence declares that a larger change, written beforehand, is complete and in force. A selector is a commit record that chooses: its content names which of several stored versions is the current one, for example “version 8, stored in area B”. Both let one small write decide a large update, so a crash leaves the old outcome or the new one. The difference: a commit record says that a change happened; a selector says which version to use.

The idea

A device keeps two copies of its configuration, an old one and a new one, in two areas of flash memory §6.6. After a loss of power during an update, both copies are complete, and both pass their checksums §4.32, short values computed from the data that reveal accidental damage. The bytes alone cannot say which copy is current. Rules that look only at the copies, such as taking the copy at the higher address or the one whose content looks newer, decide by accident and not by what the update intended.

A commit record replaces the guess with a decision that the update makes on purpose, at one instant. The update first writes the large data where it changes nothing, then writes the small record, and takes effect when the record becomes durable, meaning certain to survive a loss of power. A railway switch is a fair picture: both tracks are laid in advance, and one movement of the switch decides which track the train takes. The picture stops in two places. A switch cannot be left half thrown, but a stored record can be torn by a loss of power, so the record needs its own check. And a switch does not inspect the track beyond it, while boot code, the code that runs first after a restart, must check the data that a selector names.

How it works

A selector that survives crashes and mistakes holds these fields:

If an attacker can write the storage, the record check must be a message authentication code §5.7 or a digital signature §5.14, a check value that only a holder of a secret key can produce, because an attacker can compute a correct checksum for any content.

Writing a new record. The update first writes the new data and waits until it is durable; the Write ordering §8.20 entry treats this step. It then writes the new record into a place that does not hold the newest valid record: the older of two fixed record areas, used in turn, or the next free position of an append-only log §8.13. Overwriting the only valid record in place would leave no valid record at all if power failed during that write.

Choosing at boot. Boot code follows a fixed procedure.

  1. Read every record area. Discard each record whose record check fails or whose fields are impossible, such as a length larger than its slot.

  2. Order the remaining records by sequence number, with a comparison that works across counter wraparound §6.21, the return of a fixed-width counter to zero after its largest value.

  3. Take the newest record. Check its target: the stored data must have the recorded length and digest.

  4. If the device refuses old versions, check that the record’s version is not below the stored version floor, the lowest version the device still accepts.

  5. If both checks pass, use the target. If either fails, return to step 3 with the next older record, if the design allows a fallback to it.

  6. If no record passes, follow the declared refusal rule, for example stay in a recovery mode that waits for a new update. Never guess, for example by taking whatever data looks newest.

Damaged, stale and contradictory records need different treatment.

Reclaiming space. The data a record names must stay intact while boot code may still choose that record; erasing it turns a safe fallback into a failed check at step 3. The garbage collection §8.16 that frees space must therefore follow the records: it may erase a version only after a newer record that does not need that version is durable and, when versions go through a trial, confirmed.

Worked example

A device stores versions in two slots, A and B, storage areas that each hold one version. Two record areas, R0R_0 and R1R_1, each hold one record. Before an update, R0R_0 holds sequence number 41, naming slot A with version 7, and R1R_1 holds sequence number 40, naming slot B with version 6. Version 7 is confirmed, so version 6 is no longer needed. The update writes version 8 into slot B and waits until it is durable. It then writes a record with sequence number 41+1=4241 + 1 = 42, naming slot B with version 8, into R1R_1, because R1R_1 holds the older record (40<4140 < 41).

Boot code decides as follows in four situations.

  1. Power fails before the new record is durable. R1R_1 holds either the old record with sequence number 40 or a torn record that fails its record check. Either way the newest intact record is R0R_0 with 41, slot A passes its check, and the device runs version 7: the update did not happen.

  2. The new record is durable. R1R_1 holds 42, newer than 41, and slot B matches the digest of version 8, so the device runs version 8.

  3. Later, a fault damages slot B. R1R_1 is intact, but its target fails the digest check. Boot code moves to R0R_0 with 41, finds slot A intact, and runs version 7, provided version 7 is not below the version floor.

  4. The next update writes sequence number 42+1=4342 + 1 = 43 into R0R_0, because R0R_0 now holds the older record (41<4241 < 42).

Wraparound: suppose the sequence number is a 16-bit field, which counts from 0 to 65,535 and then returns to 0. The rule “larger is newer” would rank 1 as older than 65,535. Serial-number comparison instead treats aa as newer than bb when (a−b)mod65,536(a - b) \bmod 65{,}536 lies between 1 and 32,767, where xmodnx \bmod n is the remainder of xx divided by nn, taken between 0 and n−1n - 1. For a=1a = 1 and b=65,535b = 65{,}535: 1−65,535=−65,5341 - 65{,}535 = -65{,}534, and −65,534+65,536=2-65{,}534 + 65{,}536 = 2, which lies between 1 and 32,767, so 1 is newer. The comparison is right whenever the two records are fewer than 32,768 updates apart, which always holds for two areas used in turn, since their sequence numbers differ by 1.

Stated precisely

Let RR be the set of records that boot code reads. For a record rr, let seq(r)\mathrm{seq}(r) be its sequence number and ver(r)\mathrm{ver}(r) its version. Say that rr is intact when its record check passes and its fields are in range. Say that rr is usable when the data it names has the recorded length and digest, and ver(r)≥F\mathrm{ver}(r) \ge F, where FF is the stored version floor (F=0F = 0 on a device that accepts old versions). Boot code chooses the record r*r^{*} that is newest by serial-number comparison of seq\mathrm{seq} among the records that are both intact and usable, and follows the refusal rule if no record qualifies.

The guarantee: after a crash at any instant, boot code chooses the version named by the newest record that became durable and intact; if that record’s data is no longer usable, it chooses the newest older record whose data is usable; and it never chooses incompletely written data. The guarantee holds under five assumptions.

  1. Write ordering. The data a record names is durable before the record.

  2. No overwrite of the newest record. A new record goes to an area that does not hold the newest intact record.

  3. Detection. The record check detects a torn record. A corruption that makes every content of the record, check value included, equally likely passes a 32-bit check with probability 2−322^{-32}. A torn record is not random in that sense: whether it passes is decided by the old and new contents and by which parts of the write completed, so this assumption must be established for the actual record format and write order.

  4. Preserved targets. The data named by the newest intact record, and by every older record that the design keeps as a fallback, is not erased or overwritten.

  5. Unambiguous order. No two intact records carry the same sequence number, and any two intact records are fewer than half the counter’s range apart.

The guarantee is about crashes, not attackers.

Limits and common mistakes

An intact record proves only that the record itself is undamaged. A selector whose own check passes can name data that was later erased or damaged, so its target needs a separate check.

A checksum or CRC detects accidents, not attacks: anyone who can write the storage can write a record with a correct checksum, or copy back an old one. For the same reason, “the highest sequence number wins” is not a security rule. Refusing old versions needs a floor kept where the attacker cannot rewrite it; the Version high-water mark §8.27 and Monotonic counter entries treat this.

Falling back to an older record undoes a change. After a torn record that is correct, because the change was never reported. After damage to a durable record or its data, a reported change is lost, and the device should report that it fell back.

Every update writes the record areas, so on flash memory these areas wear out first. Appending records to a larger area spreads the wear.

Questions answered

How is a stale or damaged selector handled at boot?

A damaged selector is ignored, and a stale selector loses to a newer intact one; in both cases boot code uses the newest selector that is intact and whose named data passes its checks. A selector is the small stored record that names which version of the software or data is current. Damaged means that it fails its own check value, such as a CRC (cyclic redundancy check) over its fields, or holds impossible values, usually because power failed during its write. A new selector is always written to a different area from the newest intact one, so the previous selector is still there, and using it means that the interrupted update did not take effect. Stale means intact but older, as shown by a lower sequence number, a counter that grows with each new selector; boot code uses it only when every newer selector, or the data it names, fails its checks.

Boot code checks the named data as well as the selector: the data must match the length and cryptographic hash recorded in the selector, and, on a device that refuses old versions, its version must not be below the stored version floor. If no selector passes all checks, boot code follows a fixed refusal rule, such as waiting in a recovery mode for a new update; it never guesses. An old selector that an attacker copied back is intact and passes its own check, so only a comparison with a version floor the attacker cannot lower detects it.

Related entries: Write ordering (§8.20), Rollback versus anti-rollback (§8.26), Version high-water mark (§8.27), Authentication coverage (§5.41), A/B update (§8.10), Counter wraparound (§6.21), Monotonic counter (§8.28), Redundant copies, sequence floors, and split-brain records (§19.3.6).