REENTRANCY

The DAO, in miniature

L215 MINRACE CONDITIONS
PREDICT

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?

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
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
09[VAULT]VAULT: send 10 to atk (external call)
10[ATK]ATK: receive(): return (out of gas)
11[VAULT]VAULT: balance[atk] = 0; return (frame 3)
12[VAULT]VAULT: balance[atk] = 0; return (frame 2)
13[VAULT]VAULT: balance[atk] = 0; return (frame 1)
Start of drain scenario. 13 steps.
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
}

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 FAULT NOT 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 IT NOT 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.

Mental model:

Any call out of your code is a point where your own state can be read by someone else and your function can be re-entered before it finishes. Finish your bookkeeping before you call out: checks, effects, interactions, in that order. A guard that blocks re-entry is a seatbelt, not a fix; it leaves the stale state visible to everyone who is not blocked.

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 CARDS Tap to flip. They join your review deck when you mark the lesson complete.
NEXT: PUBLICATION →