withdraw() does: read balance (10), send 10, set balance = 0. Sending ether runs the receiver's code, and the attacker's receive() calls withdraw() again from inside the send. It re-enters twice in total, nested. How much does the attacker get?
balance = 0 is the last line of withdraw(), and the first call cannot reach it while it is still inside send(). Every nested call reads 10 and sends 10. The three balance = 0 writes all run afterwards, during unwinding, and all write the same 0.
The vault’s withdraw() reads your balance, sends it to you, then sets your balance to zero. Sending ether runs code at the receiving address. That code calls withdraw() again. The new call reads your balance, which is still the old number because the first call has not reached its last line. It sends again. Each call is a stack frame waiting to execute balance = 0, and none of them get there until the attacker stops.
▶ SIMULATION
0 / 13
VAULT CONTRACT
ETH held: 100
call depth: 0
no active call
idle
LEDGER (recorded balances)
balance[attacker] = 10
balance[others] = 90
sum = 100 vs vault 100
ATTACKER
wallet: 0 (owed 10)
re-entries rejected: 0
✓ vault ETH == sum of recorded balances whenever control is outside the vault✓ the attacker receives at most its recorded balance
SCHEDULE
01[ATK]ATK: call withdraw()
02[VAULT]VAULT: require(balance[atk] > 0) → 10, ok
03[VAULT]VAULT: send 10 to atk (external call)
04[ATK]ATK: receive(): withdraw() again
05[VAULT]VAULT: require(balance[atk] > 0) → still 10, ok
06[VAULT]VAULT: send 10 to atk (external call)
07[ATK]ATK: receive(): withdraw() again
08[VAULT]VAULT: require(balance[atk] > 0) → still 10, ok
function withdraw() {
uint amt = balance[msg.sender];
require(amt > 0);
msg.sender.call{value: amt}(""); // ← callee's receive() re-enters here
balance[msg.sender] = 0; // ← runs only after every re-entry
}
function withdraw() {
uint amt = balance[msg.sender]; // CHECKS
require(amt > 0);
balance[msg.sender] = 0; // EFFECTS first
msg.sender.call{value: amt}(""); // INTERACTIONS last: a re-entry sees 0
}
// Belt and braces: a nonReentrant mutex on top.
Watch the LEDGER panel and the invariant strip. In DRAIN, the vault’s ETH drops below the ledger at the first external call, and that gap never closes. The first invariant only checks it while control is outside the vault, so it flickers back to green whenever a vault frame is running. In FIX: CHECKS-EFFECTS-INTERACTIONS the same re-entry attempt hits require(balance > 0) with balance already 0. In FIX: REENTRANCY GUARD the attack is blocked but the first invariant still flashes red during the call-out: the ledger is stale while control is outside, which is why the guard is the weaker fix.
INJECT A FAULTNOT YET
This vault pays before it records. With an honest caller it works. Inject a second call to withdraw() at a position where it drains the vault.
GOAL: the attacker holds more than the 10 it was owed
FIX ITNOT YET
Rewrite withdraw so the attacker gets exactly what it is owed, however many times its receive() calls back in. The grader re-enters up to three deep and checks the vault still covers the ledger.
IN PRODUCTION
Smart contracts first, but the same shape lives in every callback system: a model that fires an observer in the middle of an update, an async handler that awaits a network call between checking a quota and decrementing it, a signal handler that re-enters malloc. Symptom: an invariant that holds before and after each call, and not during.
IN INTERVIEWS
“What was the DAO bug and how do you prevent it?” Walk the three lines, show the stack of frames, and give the fix as an order of operations, not a library. Then say why a mutex guard alone leaves stale state visible, and name a non-blockchain example to show you see the pattern, not the incident.
RECALL CARDSTap to flip. They join your review deck when you mark the lesson complete.