Section 3.21

Reachability

Definition

Reachability is the question whether a system, started in one of its allowed initial states and moving only by allowed steps, can arrive at a chosen target: a state, a set of states or a program point. A state is a complete description of the system at one moment, such as the current instruction and the values of all variables. The target is reachable when some finite sequence of allowed steps leads from an initial state to it. The answer depends on which initial states and steps the model allows.

The idea

A program copies len bytes into a 64-byte buffer, and the copy runs past the end of the buffer when len is above 64. Whether this is a security defect turns on one question: can any execution reach that copy with len above 64? If the only route passes through a debug command that the product build removes, the copy is unreachable in the product. If a network packet can set len, it is reachable, and such a packet is an attack. Asked about an error state, the same question checks a safety property §1.11: “the lock never opens without a valid code” holds exactly when no state in which the lock is open without a valid code is reachable.

Picture a building: rooms are states, doors are steps, and some rooms are entrances. The question is whether someone who comes in through an entrance can get to one particular room. Two differences from a real building matter. A door in a program can depend on values: the door from the test x < 1 into its true branch opens only for some values of x, so a route that looks connected on the floor plan can be impossible to walk. And the analyst draws the floor plan: which entrances exist, which doors the environment can open (a library function that may fail, a device that may return an unexpected value), and which outside events, such as an interrupt §6.11 or an incoming packet, can happen between two steps. A different plan can give a different answer.

How it works

Reachability is asked of a model with three parts, together called a transition system.

Four methods answer the question.

  1. Explicit search. Start from the initial states, and in each round add every successor of every state found so far, as in a breadth-first search §14.18. If the target appears, the sequence of steps that led to it is a witness, called a counterexample when the target is an error. If a round adds no new state, the set found contains all reachable states, and the target is unreachable if it is not in that set. On a model with finitely many states, the search always ends.

  2. Symbolic search. When there are too many states to list one by one, the analysis represents sets of states by formulas, such as 0≤x≤1000 \le x \le 100, and computes the successors of a whole set at once. Symbolic model checking §3.30 tools work this way. The number of states can grow exponentially with the number of variables, which is called state explosion §3.26.

  3. Overapproximation §19.2.5. An analysis can compute a superset of the reachable states, for example by keeping only a range of values for each variable. If the target lies outside the superset, the target is unreachable. If the target lies inside, it may be reachable, or the superset may be too coarse, so the path that the analysis offers must be checked. A path through the control-flow graph §3.13 (the diagram of which statement can follow which) is feasible when some input satisfies every branch condition along it. An SMT solver §3.28 (satisfiability modulo theories solver, a program that decides whether a set of such conditions can hold together) can check this. If the path is infeasible, the analysis is made more precise and run again; counterexample-guided abstraction refinement §15.13 automates this loop.

  4. Underapproximation. Testing, fuzzing §10.9 and bounded model checking §3.29 explore only part of the behavior, for example the executions of at most kk steps. A target that they reach is reachable, and the run is the witness. A target that they do not reach may still be reachable.

Worked example

Exploring a small model. A controller has four states: A (idle), B (receiving), C (processing) and D (error). The allowed steps are A to B, B to C, and C back to A. The only initial state is A. Write RkR_k for the set of states found after kk rounds, and Post(X)\mathrm{Post}(X) for the set of states one step after some state in the set XX.

State C is reachable, with the witness A, B, C of two steps. State D is unreachable, because no step leads into it.

Now the model of the environment changes. The processing in state C calls a library function, and the model now allows that call to fail, which adds a step from C to D. The rounds are the same up to R2={A,B,C}R_2 = \{A, B, C\}, and then R3={A,B,C}∪{B,C,A,D}={A,B,C,D}R_3 = \{A, B, C\} \cup \{B, C, A, D\} = \{A, B, C, D\}. The error state D is now reachable, with the witness A, B, C, D of three steps. The program did not change; the assumption about the library function did.

An input assumption. The code x = sensor(); if (x == 2) error(); reaches error only if the sensor returns 2. If the model says that the sensor returns only 0 or 1, error is unreachable. If the real sensor can return any byte value from 0 to 255, error is reachable with x equal to 2.

A path in the graph that no execution takes. In if (x < 1) { if (x > 3) error(); }, the control-flow graph contains a path to error: the true edge of x < 1, then the true edge of x > 3. Along that path x does not change, so an execution would need an integer xx with x<1x < 1, that is x≤0x \le 0, and also x>3x > 3, that is x≥4x \ge 4. No integer is both, so the node error is reachable in the graph, but no state at error is reachable in the program.

Stated precisely

A transition system is a triple (S,I,→)(S, I, \to): SS is the set of states, I⊆SI \subseteq S is the set of initial states, and s→s′s \to s' means that state s′s' can follow state ss in one step. For a set XX of states, Post(X)={s′∣s→s′ for some s∈X}\mathrm{Post}(X) = \{\, s' \mid s \to s' \text{ for some } s \in X \,\}. Define R0=IR_0 = I and Rk+1=Rk∪Post(Rk)R_{k+1} = R_k \cup \mathrm{Post}(R_k). The set of reachable states is Reach=⋃k≥0Rk,\mathrm{Reach} = \bigcup_{k \ge 0} R_k, which is the set of states at the end of some finite sequence s0→s1→…→sms_0 \to s_1 \to \dots \to s_m with s0∈Is_0 \in I. A target set B⊆SB \subseteq S is reachable when Reach∩B≠∅\mathrm{Reach} \cap B \neq \emptyset. The index kk counts rounds of exploration, not elapsed time.

Reach\mathrm{Reach} is the least fixed point §3.22 of F(X)=I∪Post(X)F(X) = I \cup \mathrm{Post}(X): the smallest set of states that contains the initial states and every successor of its members. If SS is finite, each round either adds at least one state or changes nothing, and once a round changes nothing no later round does, so the iteration stops after at most |S||S| rounds, where |S||S| is the number of states. A safety property that forbids the states in BB holds exactly when BB is unreachable.

For programs with unbounded memory, reachability is undecidable §19.2.1: deciding whether the end of a program is reachable is the halting problem §19.2.2. For finite-state models it is decidable, although the number of states can be exponential in the size of the model’s description.

Each kind of result proves one thing. An overapproximation is a computed set AA with A⊇ReachA \supseteq \mathrm{Reach}; finding A∩B=∅A \cap B = \emptyset proves that BB is unreachable. An underapproximation is a computed set UU with U⊆ReachU \subseteq \mathrm{Reach}; finding U∩B≠∅U \cap B \neq \emptyset proves that BB is reachable. The other two outcomes, A∩B≠∅A \cap B \neq \emptyset and U∩B=∅U \cap B = \emptyset, prove nothing. Both proofs are about the model. An unreachability proof carries over to the real system only if the initial states and steps of the model include every behavior of the real system that the claim covers. A reachability proof carries over only if every step of the witness can really happen.

Limits and common mistakes

Not observed is not unreachable: tests and fuzzing that never reach a state show only that the inputs tried did not reach it. Inside an overapproximation is not reachable: a target inside the superset is a possible problem until a feasible witness is found. Reachable in the control-flow graph is not reachable in the program: a path in the graph is a real execution only if some input satisfies all of its branch conditions together.

An assumption that removes behavior must be enforced by something real. A model in which a library function never fails, an input never exceeds 64 bytes, or no interrupt arrives during a section of code proves unreachability only for a system with that property. The property must come from a check earlier in the code, from the guaranteed contract of the function, or from the hardware. Otherwise the proof is about a system that does not exist.

Faults outside the fault model §1.30 break unreachability claims. A bit flipped in memory, a voltage glitch that makes the processor skip an instruction, or a reset in the middle of a write can lead to states that no model of correct execution contains.

Reachable is not exploitable. Reaching a faulty instruction is necessary for an attack, but whether an attacker controls the values there is a further question, which taint analysis §3.19 addresses.

A bounded search proves a bounded claim. “Unreachable within 20 steps” says nothing about step 21.

Reachability answers whether something bad can happen. Whether something good eventually happens, such as every request eventually getting an answer, is a liveness property §1.12 and needs different methods.

Questions answered

Reachable under which input and environmental assumptions?

A reachability result holds only for the model it was computed in, so every claim must state the assumptions of that model:

Then read the result by its direction. “Reachable” means that the model contains a witness path; it is a real finding when every step of that path can happen in the real system. “Unreachable”, proved with an overapproximation of the behavior of the model, is a proof about the model. It carries over to the real system only if each assumption that excluded behavior is enforced there, by a check in the code, by the hardware or by the guaranteed contract of a function. A target that testing or a bounded search did not reach has not been shown unreachable. For example, if a program reaches its error branch only when a sensor reads 2, the branch is unreachable under the assumption that the sensor returns only 0 or 1, and reachable if the sensor can return any byte value.

Related entries: Control-flow graph (CFG) (§3.13), Fixed point (§3.22), Bounded model checking (§3.29), Symbolic execution (§3.27), Model checking (§3.30), Safety property (§1.11), Overapproximation versus underapproximation (§19.2.5), Counterexample-guided abstraction refinement (CEGAR) (§15.13).