SECURITY ENGINEERING · PART 1 · LINUX AND WINDOWS, FROM THE INSIDE · INTERNALS · 15 OF 229

Buffer Overflows, ASLR, DEP and Stack Canaries

How classic memory-corruption attacks work at a high level, and the layered defenses that make them hard: what a buffer overflow is, why unsafe languages allow them, the defenses built into modern systems (non-executable memory, ASLR, stack canaries, RELRO, control-flow integrity), why they are defense in depth, and what an engineer does without writing exploits.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 9
  9. 10
  10. 11
  11. 12
  12. 13
  13. 14
  14. 15
  15. 16
  16. 17
  17. 18
  18. 19
  19. 20
  20. 21
  21. 22
  22. 23
  23. 24
  24. 25
  25. 26
  26. 27
  27. 28
  28. 29
  29. 30
  30. 31
  31. 32
  32. 33
  33. 34
  34. 35
  35. 36
  36. 37
  37. 38
  38. 39
  39. 40
  40. 41
  41. 42
  42. 43
  43. 44
  44. 45
  45. 46
  46. 47
  47. 48
  48. 49
  49. 50
  50. 51
  51. 52
  52. 53
  53. 54
  54. 55
  55. 56
  56. 57
  57. 58
  58. 59
  59. 60
  60. 61
  61. 62
  62. 63
  63. 64
  64. 106
  65. 107
  66. 108
  67. 109
  68. 110
  69. 111
  70. 112
  71. 113
  72. 114
  73. 115
  74. 116
  75. 117
  76. 118
  77. 119
  78. 120
  79. 121
  80. 122
  81. 123
  82. 124
  83. 125
  84. 126
  85. 127
  86. 128
  87. 129
  88. 130
  89. 131
  90. 132
  91. 133
  92. 134
  93. 135
  94. 136
  95. 137
  96. 138
  97. 139
  98. 140
  99. 141
  100. 142
  101. 143
  102. 144
1 / 11

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Overflow corrupts control data → code execution
  • unsafe languages allow it
  • DEP, ASLR, canaries
  • layers, patch, fuzz

Notes

The idea

The classic example is a buffer overflow. A buffer is a fixed-size piece of memory for some input. If the program copies input into it without checking the length, extra bytes spill past the end and overwrite whatever is next to it in memory. In the worst case that neighbouring memory includes the return address, the place the program jumps to when the current function finishes. Overwrite that, and you can redirect the program to code of your choosing. That is the heart of memory corruption.

A buffer overflow
  • a buffer is fixed-size memory for input
  • the code copies input without checking the length
  • extra bytes spill past the end, overwriting nearby memory
  • that memory can include a return address: where the program jumps next
  • control it, and you redirect the program

Why it happens

Why does this happen at all? In languages like C and C plus plus, the programmer manages memory by hand and there are no automatic bounds checks, so an off-by-one or a missing length check becomes an overflow, a use-after-free or a double-free. Memory-safe languages, Rust, Go, Java, Python, C sharp, check bounds and manage memory for you, so this entire class of bug largely disappears. That is why, for new code handling untrusted input, choosing a memory-safe language is itself a security decision.

The language matters
  • C and C++: manual memory, no automatic bounds checks → overflows, use-after-free, double-free are possible
  • Rust, Go, Java, Python, C#: memory-safe, bounds checked → this whole class mostly goes away
  • most big memory CVEs are in C/C++ code

What attackers want

The attacker's goal is to turn corrupted memory into running their own logic. Historically they injected code and jumped to it. Modern defenses stopped that, so attackers now reuse pieces of the program's own code, chained in a new order, called return-oriented programming. Either way the prize is remote code execution, often the most severe outcome there is. Sometimes an overflow only crashes the program, still a denial-of-service bug. The details are deep; the point is the stakes.

The goal
  • overwrite the return address or a function pointer
  • redirect execution to:
  • injected code (old style), or
  • existing code reused in a new order (ROP, modern)
  • result: remote code execution, often the worst outcome
  • sometimes just a crash (DoS)

The defenses

Modern systems do not rely on programmers never erring; they stack defenses. DEP, also called NX, marks memory as writable or executable but never both, so injected code will not run. ASLR randomises where things sit in memory each run, so the attacker cannot predict the addresses they need. A stack canary puts a secret value just before the return address; if an overflow changes it, the program aborts instead of returning into attacker control. RELRO and control-flow integrity add more layers.

Built-in protections
  • DEP/NX: memory is writable OR executable, not both → injected code will not run
  • ASLR: randomise addresses each run → attacker cannot predict where things are stack canary: a secret value before the return address; changed = abort
  • RELRO, CFI, FORTIFY, safe libs

Defense in depth

No single one of these is perfect. ASLR can be defeated if another bug leaks an address. A canary can sometimes be guessed or bypassed. Return-oriented programming was invented specifically to get around DEP. But that is the point of defense in depth: defeating all of them at once is much harder and usually needs more than one vulnerability, for example an information leak plus a memory-corruption bug. Each layer raises the cost, and raising the cost is how you win against all but the most determined attackers.

Why layers
  • any single defense can be defeated with effort
  • ASLR can leak; canaries can be guessed or skipped; ROP beats DEP
  • but defeating ALL of them at once is much harder and needs more than one bug
  • so modern exploits often chain an info-leak + a corruption

What you actually do

So what does a security engineer do? Not write exploits. You make these bugs rare and hard to use. For new code handling untrusted input, prefer a memory-safe language. For C and C plus plus, compile with the protections on: stack protector, RELRO, fortified functions and position-independent executables so ASLR applies. Keep ASLR and DEP enabled system-wide. Patch promptly, since most memory bugs you meet are known CVEs with fixes. And fuzz your inputs, which Part eight covers.

The engineer’s job
  • prefer memory-safe languages for new, exposed code
  • compile with the protections on (stack-protector, RELRO, FORTIFY, PIE for ASLR)
  • keep ASLR/DEP enabled system- wide (CIS checks it)
  • patch promptly: most are known CVEs with fixes
  • fuzz inputs to find them early (Part 8)

Beyond the stack

Buffer overflows on the stack are the textbook case, but the family is larger. Heap overflows corrupt a different region of memory with the same consequences. Use-after-free bugs use memory that was already freed, which an attacker can reclaim and control. Integer overflows can trick a program into allocating too small a buffer, leading back to an overflow. You do not need to master each one. The unifying fact is that they are all memory-safety failures, and memory-safe languages prevent the great majority of them.

Related bugs
  • heap overflows: same idea, different memory region
  • use-after-free: use memory that was already freed
  • integer overflow → undersized buffer → overflow
  • format-string bugs
  • all are memory-safety issues; memory-safe languages prevent most of them

Common mistakes

Mistakes. Writing new, internet-exposed code in unsafe C when a memory-safe language would do. Compiling without the protective flags, so canaries and RELRO are missing. Disabling ASLR because it made debugging easier and then forgetting. Treating memory-corruption CVEs as low priority when they are often remote code execution. Not fuzzing the parts of your code that parse untrusted input. And the root of it all, trusting that input will be the length you expected.

Avoid these
  • new exposed code in unsafe C for no reason
  • compiling without protections
  • disabling ASLR for convenience
  • ignoring memory-corruption CVEs
  • no fuzzing of input parsers
  • trusting input lengths

Say it like this

In the interview: I defend against memory-corruption attacks by preferring memory-safe languages for new, exposed code, and for existing C or C plus plus, compiling with stack protector, RELRO, fortified functions and position-independent executables, with ASLR and DEP enabled system-wide. These are defense in depth, so a working exploit usually needs an information leak as well as a corruption bug. On top of that I patch memory CVEs quickly and fuzz the code that parses untrusted input.

In the interview
  • "Prefer memory-safe languages
  • for exposed code; for C/C++, compile with stack protector, RELRO, FORTIFY and PIE, and keep ASLR and DEP on. They are defense in depth, so an exploit
  • usually needs an info-leak too.
  • Then patch CVEs fast and fuzz
  • input parsers."

Interview questions

Q1What is a buffer overflow?

A bug where a program writes more data into a fixed-size buffer than it can hold, overwriting adjacent memory. If that memory includes control data such as a return address or function pointer, an attacker can redirect execution, potentially to their own code.

Q2Why are C and C++ prone to memory-corruption bugs?

They use manual memory management with no automatic bounds checking, so missing length checks cause overflows, use-after-free and double-free bugs. Memory-safe languages (Rust, Go, Java, Python, C#) check bounds and manage memory, removing most of this class.

Q3What does DEP/NX do?

Data Execution Prevention (the NX bit) marks memory regions as either writable or executable, never both, so code an attacker injects into a data buffer cannot be run. It forces attackers toward code-reuse techniques instead of injecting shellcode.

Q4What is ASLR?

Address Space Layout Randomization places the stack, heap, libraries and executable at randomized addresses on each run, so an attacker cannot reliably predict where the code or data they need lives, making memory exploits much harder to write.

Q5What is a stack canary?

A secret value placed just before the return address on the stack. A buffer overflow that reaches the return address also overwrites the canary; the program checks it before returning and aborts if it changed, stopping the hijack.

Q6Why are these memory defenses considered defense in depth?

Each can be bypassed individually (ASLR via an info leak, DEP via return-oriented programming, canaries via guessing or skipping), but defeating all of them at once is much harder and usually requires more than one vulnerability, so together they greatly raise the cost.

Q7What does a security engineer do about memory bugs if they do not write exploits?

Prefer memory-safe languages for new exposed code; compile C/C++ with stack protector, RELRO, FORTIFY and PIE; keep ASLR and DEP enabled; patch memory-corruption CVEs promptly; and fuzz code that parses untrusted input to find bugs early.

Q8What is use-after-free?

Using a pointer to memory that has already been freed. An attacker can cause the freed memory to be reallocated with data they control, so the stale pointer then operates on attacker-controlled data, potentially leading to code execution. It is prevented by memory-safe languages.