Section 1.11

Safety property

Definition

A safety property is a property of a system’s executions that says something bad never happens: a forbidden event never occurs, or the system never enters a forbidden state. “No write lands outside its buffer” and “no unauthenticated code ever runs” are safety properties. Every violation has a finite witness, called a bad prefix: a finite beginning of an execution after which the property is broken whatever the system does next. The contrasting kind is a liveness property §1.12, which says that something good eventually happens.

The idea

A program owns a buffer of 16 bytes, at offsets 0 to 15, and writes one byte at offset 16. At that moment the rule “never write outside the buffer” is broken, and nothing the program does afterwards repairs the history. A later reset can restore the memory, but the forbidden write still happened. To show the violation, it is enough to exhibit the steps up to that write; nobody needs to see how the execution continues.

A rule such as “never step on the lawn” works the same way. One photograph of a foot on the grass proves a violation, and no amount of good behavior afterwards undoes it. Compare the promise “the bus will eventually come”: however long one has waited, the bus may still come, so no finite wait proves the promise false. The first rule is a safety property and the second a liveness property. Leslie Lamport introduced the two names in 1977 [234], and Bowen Alpern and Fred Schneider gave the exact definitions used today in 1985 [235]. The lawn picture stops being accurate in one respect: in a program, the forbidden event must be defined precisely, down to the instruction, before anyone can say whether it happened.

How it works

A safety property is established in four steps.

  1. Define the bad thing exactly. Write it as an event or a state that can be recognized at a single step: a store instruction whose target bytes are not all inside a region the program may write; a jump to code whose signature check has not succeeded; a read from an object after the object was freed.

  2. Describe the executions. An execution is the sequence of states the system passes through, or the sequence of events it performs, one step at a time. A record of one execution is called a trace. The property holds for the system when no possible execution, for any input and any interleaving of threads (instruction sequences that take turns on the processor) and interrupt handlers §6.12 (code the processor runs when hardware signals an event), contains the bad thing.

  3. Enforce it at run time, or prove it in advance. A runtime check, such as a runtime bounds check §2.27, inspects each step just before it happens and stops the execution before a bad step. Fred Schneider showed in 2000 [236] that a monitor of this kind, which sees only the execution so far, can enforce only safety properties, because it must decide from a finite prefix. A proof instead shows in advance that no execution reaches a bad state.

  4. Prove it with an invariant. An invariant is a condition that holds in the initial state and is preserved by every step: if it holds before a step, it holds after the step. If the invariant also excludes every bad state, induction over the steps shows that no reachable state §3.21 is bad. Model checking §3.30, which explores the reachable states by machine, either confirms such a condition or returns a counterexample trace, which is a finite bad prefix.

Worked example

A buffer has length n=16n = 16 bytes, so its valid offsets are 0 to 15. A write of ww bytes at offset oo touches the offsets oo to o+w−1o + w - 1, and it stays inside the buffer exactly when o≥0o \ge 0 and o+w≤no + w \le n.

The starting offset alone does not decide the question; the width of the write matters too.

A finite witness for the second case is a trace of three events:

  1. reserve a buffer of 16 bytes at address 0x20000000;

  2. compute the offset 16 from the input;

  3. store 1 byte at address 0x20000010, which is 0x20000000 plus 16.

Event 3 writes the first byte past the end of the buffer. The three events form a bad prefix: every continuation of them still contains the forbidden store. Event 2 alone does not violate a rule about memory accesses, because computing an out-of-range offset touches no memory. It violates the rule only if the rule also forbids computing such an offset, so the rule must say which it means.

An invariant shows the opposite for a correct loop. The loop sets i=0i = 0 and, while i<16i < 16, stores 1 byte at offset ii and then adds 1 to ii. The invariant is 0≤i≤160 \le i \le 16. It holds initially, because i=0i = 0. A store happens only when i<16i < 16, so i+1≤16i + 1 \le 16 and the store stays inside the buffer. After the store, ii grows by 1 to at most 16, so the invariant still holds. Therefore no execution of this loop contains a store outside the buffer.

Stated precisely

Let an execution be a sequence of states σ=s0s1s2…\sigma = s_0 s_1 s_2 \dots, and let a property PP be a set of executions: those that satisfy it. Write σ≤i\sigma_{\le i} for the finite prefix s0s1…sis_0 s_1 \dots s_i. PP is a safety property when every execution that violates PP has a finite prefix that no continuation repairs: σ∉P⟹there is an i such that no execution that begins with σ≤i is in P.\sigma \notin P \implies \text{there is an } i \text{ such that no execution that begins with } \sigma_{\le i} \text{ is in } P. Such a prefix σ≤i\sigma_{\le i} is a bad prefix. The most common special case is an invariance property, “every reachable state satisfies the condition QQ”; its bad prefixes end in a state that violates QQ.

A safety claim about a real system rests on two assumptions. First, the model of executions includes every step the real system can take: every code path, every interrupt handler, every other thread, and every device that writes memory directly. Second, the bad event is defined where the harm happens. A rule about stores says nothing about reads.

A deadline turns a promise about the future into a safety property. “Every request gets a response within 100 milliseconds” is violated by a finite prefix: one in which 100 milliseconds pass after some request without a response. “Every request eventually gets a response” has no such prefix and is a liveness property. In the same way, partial correctness of a program, which says that if the program stops then its result is right, is a safety property, and termination is a liveness property.

Limits and common mistakes

A safety property does not promise that anything happens. A system that does nothing, or rejects every request, satisfies every safety property whose bad event it never performs. Usefulness needs liveness and availability §1.10 as well.

Passing tests is not proof. Tests run finitely many executions, and a safety property is about all of them. One failing execution refutes the property; no number of passing ones establishes it.

A crash is neither necessary nor sufficient as a witness. A bounds check that halts a program before a bad store is the property being enforced, not violated. A bad store that silently corrupts memory without any crash is a violation.

Not every security property is a property of single executions. Noninterference §1.42, the rule that secret inputs must not affect public outputs, compares pairs of executions. Such properties are called hyperproperties, and a monitor that watches one execution cannot enforce them in general.

Statements about averages are not safety properties. One forbidden event refutes the claim “never”; it does not refute a claim such as “an error rate below 1%”.

Questions answered

Give a finite witness that a memory-safety property failed.

A finite witness is a concrete, finite sequence of steps from an allowed starting state to one memory access that breaks the stated rule, with enough detail that anyone can confirm the breach by arithmetic. For example:

  1. Starting state: the program owns a buffer of 16 bytes at address 0x20000000, so its valid offsets are 0 to 15.

  2. Input: a message whose index field holds 15.

  3. Step: the program writes a 2-byte value at offset 15.

The write covers offsets 15 and 16, and 15+2=17>1615 + 2 = 17 > 16, so the second byte lands one byte past the end of the buffer. This three-step trace is a bad prefix: whatever the program does next, the out-of-range write has already happened, so the property “every write stays inside its buffer” is false for this program.

A good witness names the object and its length, the offset and the width of the access, and the input that leads there. A log line that says only that the program crashed is not a witness, because a crash may be a correct check stopping the program, and memory can be corrupted without any crash.

Related entries: Liveness property (§1.12), Security invariant (§1.23), Overflow-safe range check (§4.17), Runtime bounds check (§2.27), Model checking (§3.30), Buffer overflow (§4.4), Noninterference (§1.42). Further reading: [234, 235, 236].