Following the stack-frame thread, here is the next layer of the story, in defender language.
The problem the frame diagram showed: the return address sits on the same stack as the data, so a bug that writes too far can change where ret goes. Mitigations attack that from different sides:
- Canaries detect that something overwrote the area between buffer and return address.
- NX says data pages are not executable, so injected bytes cannot simply run.
- ASLR / PIE randomize addresses so the attacker needs to know where things are.
- Shadow stack: the CPU keeps a second copy of every return address in a separate protected region, and on
retcompares the two. A mismatch stops the program. The overwritten copy on the normal stack stops mattering. - CFI (control-flow integrity): before an indirect jump or call, check that the target is a valid destination, for example the start of a function with a matching type. A jump into the middle of a function is refused.
None of these is perfect on its own. They stack. Each one removes a class of easy wins and raises the cost of the next one, which is why CTF boxes are usually built with some of them off, so the exercise isolates one idea at a time.
When you read a challenge, the first question should be: which of these are on? It tells you what kind of challenge it is.