Protection & Security

defense in depth

A medieval castle did not rely on a single wall. It had a moat, then an outer wall, then an inner wall, then a fortified keep at the center. An attacker who got past one barrier still faced the next, and each layer bought the defenders time. Defense in depth is that same wisdom applied to computers: never bet everything on one protection, but stack several independent defenses so that the failure of any single one does not mean total compromise.

Concretely, a well-defended system layers controls that do not depend on each other. A program might be written in a memory-safe way (layer 1); compiled with stack canaries and the no-execute bit so that even if a buffer overflows the attack is harder (layer 2); run inside a sandbox with least privilege so a hijack is contained (layer 3); behind a firewall that limits what can even reach it (layer 4); and watched by auditing and logging so an intrusion is detected and can be cleaned up (layer 5). The whole point is that these are independent: an attacker must defeat all of them in sequence, and each layer assumes the ones outside it might have already failed.

Why bother, if each layer is supposedly secure? Because no single defense is ever perfect — bugs exist, configurations drift, and clever attackers find paths the designers did not imagine. Defense in depth accepts this honestly and plans for partial failure, so that one mistake is not fatal. The caveat is that depth is not the same as security theater: ten weak, correlated locks that all open with the same flaw add little, while a few genuinely independent layers add a lot. The value comes from independence, not from the sheer count.

An attacker finds a buffer overflow in a photo app. The stack canary trips and the program crashes instead of being hijacked — but suppose they bypass it; ASLR scrambles addresses so their payload jumps to the wrong place; suppose they beat that too; the app runs in a sandbox, so even arbitrary code cannot read other apps' data. Three independent layers, three chances to stop the attack.

Independent layers each assume the others might fail.

More layers is not automatically more secure. Layers that all share the same weakness fail together; the benefit comes from layers being genuinely independent, so that one breach does not unlock the rest.

Also called
layered defensedefence in depth多層防禦層層防禦