Section 7.37

Protocol state machine

Definition

A protocol state machine describes a communication protocol as a finite set of states, the inputs allowed in each state, and the transition that each allowed input causes. A state records how far a conversation has progressed, such as “receiving data” or “data verified”. An input is a message or an event such as a disconnection or a timeout §9.34. The receiver accepts an input only if its current state allows it. Parsing decides what a message is; the state machine §8.33 decides whether it is allowed now.

The idea

A device receives an update in pieces. It has received half of the object when a perfectly formed message arrives: “commit”, meaning install what you received. The parser, the code that checks the message’s format, finds nothing wrong. Only the device’s record of the conversation knows that half of the object is missing and that the object has not been checked for authenticity. If the device obeys, it installs half an object. The same bytes are right after a verified transfer and dangerous in the middle of one.

A state machine makes that record explicit. A paper form with numbered steps, processed in order by a clerk, is a fair picture: the clerk will not stamp step 5 while step 3 is blank, however neat the page is. The picture fails in one respect. A conversation over a radio link can stop at any moment, because the connection drops, and the protocol state machine must say what happens then, in every state. A clerk’s form rarely says what to do when the applicant walks out halfway.

How it works

Designing a protocol state machine takes four steps. The running example is a transfer that sends an object in fragments, pieces small enough for one packet, into a staging area, storage reserved for incoming objects that are not yet installed.

  1. Name the states. Idle: no transfer. Receiving: fragments are arriving. Complete: all bytes are present but not checked. Verified: the whole object passed its authenticity check. Committed: the object is installed.

  2. Name the inputs. Messages from the sender: BEGIN, which starts a transfer and gives its identifier and total length; FRAGMENT, which carries an offset, the position of the bytes in the object, and the bytes; and COMMIT. Events from lower layers or from timers: DISCONNECT and TIMEOUT.

  3. Write the table. For every pair of a state and an input, give the next state, the effect, and the transition guard §8.34, a condition that must hold for the transition to happen. For example:

    • Idle and BEGIN, with a total length no larger than the staging area: go to Receiving, record the transfer identifier and the total length, and set the count of received bytes to 0.

    • Receiving and FRAGMENT, with an offset equal to the count and an end that does not pass the total length: store the bytes and add their number to the count. When the count reaches the total length, go to Complete.

    • On entering Complete, the receiver checks the authenticity of the whole object. On success go to Verified; on failure discard the bytes and go to Idle.

    • Verified and COMMIT: install the object and go to Committed.

    • Any pair not in the table: reject the input, with no effect and no change of state.

  4. Define failure in every state. DISCONNECT and TIMEOUT can arrive in any state, so the table needs an entry for each of them in each state. Every resource that a state holds, such as the staging area, must be released on every path that leaves the state.

What happens to a staged transfer, one whose bytes sit in the staging area, when the connection drops has two safe answers.

Under both answers, bytes from an unfinished transfer are never installed, run or treated as verified. The authenticity check runs over the complete object, retained bytes included, before Verified is reached.

The transfer identifier must not be a value that lower layers reuse. In BLE §7.1 (Bluetooth Low Energy), the chip gives each connection a number called the connection handle, and a later connection, possibly from another device, can receive the same number. A transfer identifier chosen by the sender and covered by the message authentication §1.6 stays the same across connections; a connection handle does not.

Worked example

A 12-byte object is sent as two 6-byte fragments. The receiver uses the retain-and-resume design with an expiry of 60 s.

  1. The state is Idle. BEGIN arrives with identifier 42 and total length 12. The guard holds, so the state becomes Receiving with count 0.

  2. FRAGMENT arrives with offset 0 and 6 bytes. The offset equals the count, and 0+6=6≤120 + 6 = 6 \le 12. The count becomes 6.

  3. COMMIT arrives. The table has no entry for Receiving and COMMIT, so the receiver rejects it: only 6 of the 12 bytes are present, and nothing has been checked. The message itself was well formed.

  4. The connection drops at time tt. The state becomes Suspended with identifier 42, count 6 and expiry t+60t + 60 s.

  5. The sender reconnects at t+5t + 5 s and sends an authenticated RESUME for identifier 42. Since t+5<t+60t + 5 < t + 60, the receiver returns to Receiving and answers “count 6”.

  6. FRAGMENT arrives with offset 6 and 6 bytes. Now 6+6=126 + 6 = 12, the count reaches the total length, and the state becomes Complete. The authenticity check over all 12 bytes succeeds, and the state becomes Verified. Now COMMIT is accepted, and the state becomes Committed.

Two variations show the other transitions. If the sender reconnects at t+70t + 70 s instead, then t+70>t+60t + 70 > t + 60: the receiver has already released the 6 bytes and returned to Idle, so it rejects RESUME, and the sender starts again with BEGIN. If an attacker connects during the suspension and sends FRAGMENT with offset 6, the receiver is in Suspended, where FRAGMENT has no entry, and rejects it. An unauthenticated RESUME fails its guard and is rejected as well.

Stated precisely

A deterministic protocol state machine is a tuple (S,I,s0,δ)(S, I, s_0, \delta). Here SS is a finite set of states, II is the set of inputs (messages and events), s0s_0 is the initial state, an element of SS, and δ\delta is the transition function. For a state ss and an input mm, δ(s,m)\delta(s, m) is either a next state together with an effect, or the outcome reject\mathrm{reject}, which leaves the state unchanged and performs no effect. A guard is a condition on the input’s content and on the stored data; when the guard of a transition fails, the outcome is reject\mathrm{reject}.

The specification is complete when δ(s,m)\delta(s, m) is defined for every state ss and every input mm, including DISCONNECT and TIMEOUT. For the transfer above, the safety property §1.11, a statement that something bad never happens, reads: for every sequence of inputs, the receiver enters Committed only if, earlier in the same transfer, the count reached the total length and the authenticity check succeeded over exactly the bytes that are installed. Because δ\delta depends on the state, the same input can be accepted in one state and rejected in another: δ(𝚁𝚎𝚌𝚎𝚒𝚟𝚒𝚗𝚐,𝙲𝙾𝙼𝙼𝙸𝚃)=reject\delta(\texttt{Receiving}, \texttt{COMMIT}) = \mathrm{reject}, while δ(𝚅𝚎𝚛𝚒𝚏𝚒𝚎𝚍,𝙲𝙾𝙼𝙼𝙸𝚃)\delta(\texttt{Verified}, \texttt{COMMIT}) installs the object. Testing each message parser on its own cannot establish the safety property, because the property is about sequences of inputs.

Limits and common mistakes

Messages that are each authentic can still form a forbidden sequence. A message authentication code §5.7 (a short value that only a holder of the secret key can compute) shows who made a message; it does not show that the message belongs at this point or to this transfer. An attacker who recorded authentic messages from an earlier transfer can try to insert one into the current transfer. Bind the transfer identifier, and a sequence number where order matters, into the authenticated bytes, so that the checks of the state machine rest on values an attacker cannot change. This is a question of authentication coverage §5.41, the set of bytes the code protects.

Code without an explicit state machine still has one, spread over flags and variables. The combinations that nobody wrote down are the ones nobody checked.

A disconnection is an ordinary input, not an exception. A design that handles it only in the common state leaks the staging area, or leaves stale bytes that a later transfer mistakes for its own.

Retained state has a cost. Each suspended transfer holds memory until it expires, and a peer that starts many transfers and abandons them can use up the staging area, much as idle peers can cause connection-slot exhaustion §7.35.

The sender and the receiver each run a state machine, and a lost message can leave them out of step: the sender may believe the object is committed while the receiver still waits for COMMIT. A status request that asks the receiver for its state, together with the duplicate handling of the Retransmission and duplicate delivery §7.36 entry, brings them back into step.

Real implementations have failed in exactly this way. TLS (Transport Layer Security) is the protocol that protects web traffic, and OpenSSL is a widely used software library that implements it. In 2014 OpenSSL was found to accept the TLS handshake message ChangeCipherSpec earlier than the protocol allows. When both ends ran affected versions, an attacker on the network path could make them derive their keys from a zero-length master key, keys the attacker could compute as well. The flaw is CVE-2014-0224 in the Common Vulnerabilities and Exposures (CVE) list. Each message was well formed; only the order was wrong.

Testing must exercise sequences of inputs. Fuzzing §10.9, which feeds a program large numbers of generated inputs, finds an order error only if it sends a sequence that reaches the state where the error lies. Model checking §3.30, which explores every reachable state §3.21 of a model, can establish the safety property for the model, and only for code that matches the model.

Questions answered

What happens to a staged transfer after disconnect and reconnect?

What happens to a staged transfer, an object whose received bytes wait in reserved storage and are not yet installed, is decided by the rule that the receiver’s protocol state machine, its table of states and allowed inputs, gives for a disconnection in the transfer’s current state, and a correct design gives such a rule for every state. Two choices are safe. Discard: the disconnection releases the staged bytes and the staging area and returns the receiver to its idle state, and after reconnecting the sender starts the transfer again. Retain and resume: the receiver keeps the staged bytes, the transfer identifier and the count of received bytes until an expiry time; after reconnecting, the sender proves with an authenticated resume message that it continues the same transfer, and continues from the byte count that the receiver reports. A transfer identifier chosen by the sender identifies the transfer across connections. A connection handle, which a later connection can reuse, does not.

Under both choices the disconnection must not leak memory, and the partly received bytes must never be installed, run or treated as verified. The authenticity check runs over the complete object, retained bytes included, before any commit is accepted.

Related entries: Finite-state machine (§8.33), State transition guard (§8.34), Length-prefix framing (§4.30), Authentication coverage (§5.41), Retransmission and duplicate delivery (§7.36), Connection-slot exhaustion (§7.35).