Intermediate Topic 27 of 51 Security & Cryptography
Buffer Overflow
Writing past a buffer's end to corrupt memory.
How it works
A buffer overflow occurs when a program writes more data into a fixed-size memory buffer than it can hold, overwriting adjacent memory such as variables, saved registers and return addresses. In unsafe languages like C and C++, functions such as strcpy and gets do not check bounds. Attackers can crash programs or redirect execution to injected or existing code. Stack-based and heap-based overflows differ in what they corrupt.
Step-by-step walkthrough
- A function reserves a four byte buffer on the stack.
- Normal input fills the buffer.
- Longer input keeps writing past the end.
- It overwrites the saved return address.
- When the function returns, execution jumps to attacker-chosen code, so check bounds.
Where it is used
It is a classic memory-safety vulnerability behind many historic worms and remote exploits, and it still appears in firmware, drivers and network services.
Common pitfall
Defenses include bounds-checked functions and safe languages such as Rust, Go and Java, stack canaries, non-executable memory, address space layout randomization and fuzzing during development. Off-by-one errors, integer overflows leading to small allocations and format-string flaws cause related bugs. Use compiler sanitizers, static analysis and code review, and treat all external input lengths as untrusted.
Where it fits in the course
Buffer Overflow is topic 27 of 51 in Security & Cryptography, rated Intermediate. It follows CSRF. The next topic is Denial of Service.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Security & Cryptography quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Security & Cryptography quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Buffer Overflow?
Where do people use Buffer Overflow?
What is the most common mistake with Buffer Overflow?
What should I learn before this topic?
How can I practice it?
Is Buffer Overflow hard to learn?
How long does it take to learn Buffer Overflow?
Which topics are related to Buffer Overflow?
What should I study after Buffer Overflow?
Related topics
- CSRF (Security & Cryptography)
- Denial of Service (Security & Cryptography)
- Cross-Site Scripting (Security & Cryptography)
- Man-in-the-Middle Attacks (Security & Cryptography)
- Z-Buffer (Graphics, Vision & Media)
- Circular Buffer (Data Structures)