Section 1.18

Defense in depth

Definition

Defense in depth is a security design strategy that places several protective layers between an attacker and an asset, so that an attack succeeds only if it defeats every layer. The layers should fail for different reasons, so that what defeats one layer is stopped by another. Two layers that share a weakness, such as the same parser (the code that splits raw input into fields) or the same key, fail together, and against attacks that use that weakness they count as one layer.

The idea

A device accepts program updates over a radio link. Its protection could rest on one check: the update must carry a valid digital signature §5.14, a value that only the holder of a private key can produce. If that key is stolen, or the signature code has a bug, a malicious update then runs with nothing else in its way. A layered design asks a different question at each step.

A stolen key gets an update past the first question but not the second. A bug in validation gets an update past the second question, but an outsider without the key is still stopped at the first.

Accident investigators use the Swiss cheese model, introduced by the psychologist James Reason. Each layer of protection is a slice of cheese with holes in it, and harm passes only where holes in every slice line up. The picture is right about the goal, which is slices whose holes do not line up. It stops in two places. Holes in software are not scattered at random: an attacker searches for a path through all the slices and then uses it every time. And two slices cut from the same piece of code have their holes in the same places.

How it works

Building and judging layers takes six steps.

  1. Name the asset and the attacks that the threat model §1.2, the written account of what an attacker can do, includes.

  2. For each layer, write down three things: what it stops, what assumption it rests on, and what is left if that assumption fails.

  3. Check that every attack path passes every layer. A route around a layer removes that layer for every attack that takes the route.

  4. Find shared dependencies. List what each layer relies on: the parser, the key, configuration data, libraries, the compiler, the hardware and the designers’ assumptions. A flaw in anything that two layers share is a common-mode failure §19.2.13: one cause that defeats both at once.

  5. Diversify. Prefer layers of different kinds, such as a cryptographic check, a check of the content’s structure and hardware isolation, and give each layer its own view of the input instead of a value that another layer computed.

  6. Make each layer fail closed §1.13, so that an error inside a layer refuses the input instead of waving it through, and make each refusal visible, because a layer that stops an attack is evidence that an earlier layer failed.

Layers also differ in when they act. Some prevent an attack, such as a signature check. Some detect it, such as an alarm on repeated failed checks. Some contain it, such as memory isolation that confines a compromised program to its own region. Some recover from it, such as returning to the last-known-good state §8.25. Covering more than one of these stages means that an attack that prevention misses can still be noticed, confined or undone.

Worked example

Independence changes the numbers. Suppose, purely to show the arithmetic, that two layers each miss 10% of some class of random faults. If the misses are independent, both layers miss the same fault with probability 0.1×0.1=0.010.1 \times 0.1 = 0.01, which is 1%. If the two layers miss exactly the same faults, both miss with probability 0.1, which is 10%, and the second layer adds nothing. Shared code moves a pair of layers toward the second case.

Here is how a shared parser does that. An update message starts with a 2-byte length field, stored with its low byte first, followed by the program. A program of 300 bytes has length 300=1×256+44300 = 1 \times 256 + 44, which is 0x012C in hexadecimal, so the field holds the bytes 0x2C and 0x01 in that order. One parsing library, used both by the tool that creates updates and by the device, has a bug: it reads only the first byte of the field and reports a length of 0x2C, which is 44. Two layers on the device use that library.

The loader, written separately, copies all 300 bytes that the radio delivered. The last 300−44=256300 - 44 = 256 bytes are covered by neither layer. An attacker who has captured one genuine update changes bytes in that range, for example by placing a forbidden instruction at byte 200. The tag still matches, the validator still finds nothing, and the loader installs the altered program. Two layers passed it, and both were blind to the same 256 bytes.

The repair makes the layers independent of the parser. The MAC is checked over the exact bytes that the radio delivered, all of them, before any parsing. The validator inspects the same buffer and the same length that the loader will use. And the device refuses any message whose length field disagrees with the number of bytes received, here 44 against 300. A parser bug can now make the device refuse an update, but it cannot hide bytes from either layer.

Stated precisely

Let AA be the set of attacks that the threat model includes. For each layer ii, numbered 1 to nn, let DiD_i be the part of AA that defeats layer ii. If every attack must pass every layer, the attacks that succeed are exactly those that lie in every DiD_i: successful attacks=D1∩D2∩…∩Dn,\text{successful attacks} = D_1 \cap D_2 \cap \dots \cap D_n, where ∩\cap gives the elements common to the sets on either side. Adding a layer helps exactly as much as it shrinks this intersection. A flaw shared by two layers puts every attack that uses the flaw into both sets, so those attacks stay in the intersection.

For random faults, write pip_i for the probability that a fault lies in DiD_i. Both of two layers fail with probability Pr[fault in D1∩D2]\Pr[\text{fault in } D_1 \cap D_2], which can be as large as the smaller of p1p_1 and p2p_2. It equals p1p2p_1 p_2 only when the two failures are independent events, and independence must be argued from how the layers are built, not assumed. An attacker does not draw attacks at random: the attacker picks one from the intersection whenever the intersection is not empty. Against an attacker, the question is whether the intersection is empty, not how probable it is.

The argument rests on two assumptions besides the sets themselves. No route bypasses a layer. And the failure of one layer does not switch off another: if a crash in the first layer makes the second skip its check, the two layers are in series only on paper.

Limits and common mistakes

Two copies of the same check are not two layers. Running the same code twice, or running two pieces of code that share a parser, a key or a stored value, lets one flaw defeat both.

A later layer that skips its work because an earlier layer “already checked” is no layer at all. Each layer must establish its own property from its own view of the input.

Multiplying failure probabilities is valid only for independent random faults. Against an attacker, a product such as 0.1×0.10.1 \times 0.1 has no meaning; what matters is whether one input defeats both layers.

Each layer adds code, and new code adds attack surface §1.5, the set of places where an attacker can supply input. A parser added for a second layer is itself something an attacker can target. Layers also cost time and memory, and each one is another reason to refuse a legitimate request.

Defense in depth does not excuse a known hole in one layer. Each layer must be argued on its own, and several weak layers with holes in the same places add up to one weak layer.

Questions answered

Are two checks independent, or do both trust the same parser?

Two checks are independent only when no single flaw, input or stolen secret can defeat both; if both read their input through the same parser, the code that splits raw bytes into fields, they are not independent against any bug in that parser. A parser that misreads a field gives both checks the same wrong view of the input, so an attack that exploits the misreading passes both checks, and against that attack the two count as one.

To decide, trace every value that each check uses back to its origin: which parser read it, which stored metadata, key or configuration it depends on, and which library and compiler built the code. Any item that the two checks share can cause a common-mode failure, one cause that defeats both. For each shared item, ask whether one flaw in it defeats both checks. If it does, give one check its own view of the input: verify an authentication tag over the exact bytes received, before any parsing; have the validator inspect the same bytes that the loader will use; and refuse input whose declared length disagrees with the number of bytes received. Do not multiply the two checks’ failure rates unless independence has been shown, and never against an attacker who chooses the input.

Related entries: Trusted computing base (TCB) (§1.4), Bytecode authenticity versus safety (§2.43), Authentication coverage (§5.41), Fail closed (§1.13), Attack surface (§1.5).