strcpy copies bytes from input into name without bounds checking. If input is longer than 16 bytes, the copy continues past the end of name, overwriting adjacent stack memory. If the saved return address is overwritten, the function may return to an attacker-controlled address.
When data written past the buffer overwrites neighboring memory, we call that a buffer overflow.

- Overwrite a saved return address to redirect execution.
- Inject shellcode into the overflowed buffer and point the return address at it (classic technique).
- Use ROP to chain together small existing code snippets when the stack is non-executable.
- If the new address is invalid, the program usually crashes — if valid and attacker-controlled, arbitrary code execution is possible.
-
Stack canaries (Stack Smashing Protector)
- Compilers can insert a small, unpredictable value (a canary) between the buffer and the saved return address. Before returning from a function, the runtime checks whether the canary was modified. If it was, the program aborts instead of returning to a potentially attacker-supplied address.
- Example compiler options:
-fstack-protector,-fstack-protector-strong. - Canaries are effective at detecting contiguous buffer overruns that would corrupt the return address, but they are not a silver bullet (non-contiguous corruptions or info leaks can bypass them).
-
Non-executable stack (DEP / NX)
- Operating systems can mark stack pages non-executable so injected shellcode on the stack cannot be executed.
- Limitations: attackers can still use return-oriented programming (ROP) or other control-flow hijacking techniques to reuse existing executable code.

Practical recommendations
- Avoid unsafe string and memory functions like
strcpy,strcat,gets, and unboundedmemcpywith untrusted sizes. - Use platform-provided secure APIs (
strlcpy,strncpy_s) or safe libraries that centralize bounds checks. - Compile with available hardening flags (
-fstack-protector,-D_FORTIFY_SOURCE=2) and enable OS mitigations (ASLR, NX). - Consider safer languages (Rust, Go, managed runtimes) for new development where appropriate.
- Combine mitigations: multiple layers (compiler, OS, safe code) significantly reduce risk.
- OWASP — Buffer Overflow: https://owasp.org
- Return-Oriented Programming (ROP): https://en.wikipedia.org/wiki/Return-oriented_programming
- Address Space Layout Randomization (ASLR): https://en.wikipedia.org/wiki/Address_space_layout_randomization
Defensive best practice: Write bounds-checked code, compile with stack protections and hardening flags, and enable OS mitigations like NX/DEP and ASLR. Layered defenses (safe APIs + compiler and OS protections) provide the best protection against buffer overflow exploits.