The topic
A buffer overflow writes past the end of a fixed buffer. On the AT89C51 that write can change adjacent internal RAM, including a saved return address.
Where it belongs
Course: Embedded systems. At KU this was EECS 388, after binary reverse engineering.
Teach this after: the stack frame and the calling convention. Count the homework as required work, not extra credit.
Files and how to use them
Use the two slide decks as the class lectures. Then assign the homework. Students build the C file in Keil.
- Lecture, part 1. Use in class after the calling-convention lecture. The stack, the saved return address, and how an overrun reaches them.
- Lecture, part 2. Use in class before the homework. The 8051 memory map, and how the lab is scored.
- Homework handout. The graded assignment. Spring 2024 scored it at two points.
- vulnerable_program.c. The starter students compile. The handout is written against this file.
Video lectures
These are links, not files stored with the module. Play one in class, or send the link to students.
Further reading
Optional papers for the instructor, or for a student who wants to read past the assignment.
- Smashing the Stack for Fun and Profit, Aleph One, Phrack, vol. 7, no. 49, 1996. The classic account of a stack overrun on a desktop. This lab is the same idea on a machine with 256 bytes of internal data memory.
- Cowan, Pu, Maier, and others, “StackGuard: Automatic Adaptive Detection and Prevention of Buffer-Overflow Attacks,” USENIX Security Symposium, pp. 63–78, 1998.
- Szekeres, Payer, Wei, and Song, “SoK: Eternal War in Memory,” IEEE Symposium on Security and Privacy, pp. 48–62, 2013.
The exercise
A lookup routine returns a stored secret when a supplied date matches a record. The copy into the input buffer does not check the buffer length.
On a desktop, a saved frame pointer and a saved return address sit in separate words. On the AT89C51, internal data memory is 256 bytes. The register banks occupy the low addresses, and constants are copied from code memory at startup. This chip has no stack canary and no address-space layout randomization.
Outcome
The student leaks successive secrets through R7, then rewrites the copy so it cannot leave the buffer, and explains why that rewrite holds on this processor.
What the student does
- Read the provided C program and find the unbounded copy and the records it is meant to protect.
- Build an input long enough to disturb neighboring memory, and watch the debugger. The handout scores the length of that input, then whether the first secret appears in R7, then the first and second, then more than two.
- Repair the program so data cannot be written past the buffer, and explain in writing how the change prevents the overflow.
The lecture names stack canaries and address-space layout randomization, then the lab removes both. The repair has to be a bounds check in this program. Partial credit follows how far the leak gets, so a student who can read the memory map but does not recover every secret still has a score.
In Spring 2023 this assignment was a bonus. From Spring 2024 it is required. Treat it as required if you adopt it. Students who skip a bonus still meet the calling convention on the exam.



