A reentrancy attack exploits a flaw in smart contract logic where an external call to an untrusted contract allows the attacker to re-enter the original function before it finishes executing. This lets the attacker drain funds repeatedly because the contract has not yet updated the user's balance.
How the Attack Works
The Vulnerability
The core issue arises when a smart contract sends Ether or calls an external contract before updating its internal state. The attacker's fallback function is triggered during the external call, allowing re-entry into the vulnerable function.
Vulnerable contract flow:
1. Attacker calls withdraw() with 1 ETH balance
2. Contract sends 1 ETH to attacker via external call
3. Attacker's fallback/receive function triggers
4. Fallback calls withdraw() AGAIN
5. Balance not yet updated - contract thinks attacker still has 1 ETH
6. Contract sends another 1 ETH
7. Steps 3-6 repeat until contract is empty
The most famous reentrancy attack was the DAO hack, which resulted in the theft of approximately 60 million dollars worth of Ether and ultimately led to the Ethereumhard fork that created Ethereum Classic.
Detail
Value
Attacker
Unknown (white hat hacker debated)
Amount stolen
~3.6M ETH (60M USD at the time)
Vulnerability
Reentrancy in splitDAO function
Consequence
Ethereum hard fork (ETH vs ETC)
Date
June 17, 2016
Prevention Techniques
Checks-Effects-Interactions Pattern
The most important defense is to reorder the function logic so that state changes happen before external calls.
1. CHECKS - Validate all conditions (require statements)
2. EFFECTS - Update all state variables (balances, etc.)
3. INTERACTIONS - Make external calls last (transfers, etc.)
Copy link
Additional Defenses
ReentrancyGuard modifier - OpenZeppelin provides a nonReentrant modifier that prevents re-entry
Pull over push payments - Let users withdraw funds instead of sending to them automatically
Transfer vs call - Use transfer() (limited to 2300 gas) instead of call.value()
Mutex locks - Implement a locking mechanism during critical operations
Reentrancy vulnerabilities are consistently ranked among the most critical smart contract risks. Professional audits from firms like Trail of Bits, OpenZeppelin, and ConsenSys Diligence specifically test for reentrancy patterns. Any DeFi protocol handling user funds should undergo multiple audits before mainnet deployment.