Smart Contract Security Part 1: Reentrancy, the Checks-Effects-Interactions Pattern and Safe ETH Withdrawals
Learning only Study these examples on a local or test network. Never attack contracts that you do not own.
Why Security Matters So Much
- Deployed code is public and usually cannot be changed
- Contracts hold real money
- Attackers read your code and test it all day
- One small mistake can empty a contract
A Vulnerable Bank
solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract VulnerableBank {
mapping(address => uint256) public balances;
function deposit() external payable {
balances[msg.sender] += msg.value;
}
function withdraw() external {
uint256 amount = balances[msg.sender];
require(amount > 0, "No balance");
(bool ok, ) = msg.sender.call{value: amount}(""); // 1. sends ETH first
require(ok, "Send failed");
balances[msg.sender] = 0; // 2. updates balance too late
}
}
What Goes Wrong
When the bank sends ETH to a contract, that contract's receive function runs. It can call withdraw() again before the bank has set the balance to zero.
text
withdraw() -> send ETH -> attacker's receive() -> withdraw() again
-> balance still not zero -> send ETH again -> ... repeat until bank is empty
The Attacker (Illustration)
solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
interface IBank {
function deposit() external payable;
function withdraw() external;
}
contract Attacker {
IBank public bank;
constructor(address bankAddress) {
bank = IBank(bankAddress);
}
function attack() external payable {
bank.deposit{value: msg.value}();
bank.withdraw();
}
receive() external payable {
if (address(bank).balance >= msg.value) {
bank.withdraw();
}
}
}
Fix 1: Checks-Effects-Interactions
Always follow this order inside a function:
- Checks: validate inputs and permissions
- Effects: update your own state (balances)
- Interactions: only then call other addresses or send ETH
solidity
function withdraw() external {
uint256 amount = balances[msg.sender];
require(amount > 0, "No balance");
balances[msg.sender] = 0; // effect first
(bool ok, ) = msg.sender.call{value: amount}(""); // interaction last
require(ok, "Send failed");
}
Fix 2: ReentrancyGuard
solidity
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
contract SafeBank is ReentrancyGuard {
mapping(address => uint256) public balances;
function deposit() external payable {
balances[msg.sender] += msg.value;
}
function withdraw() external nonReentrant {
uint256 amount = balances[msg.sender];
require(amount > 0, "No balance");
balances[msg.sender] = 0;
(bool ok, ) = msg.sender.call{value: amount}("");
require(ok, "Send failed");
}
}
nonReentrant blocks a second entry into the function while the first is still running. Use both the pattern and the guard for important functions.
History Lesson
In 2016, The DAO, an early investment contract, lost a very large amount of ETH to a reentrancy attack. The community then chose to fork Ethereum, which created Ethereum and Ethereum Classic. It remains the best-known example of why contract security matters.
Practice
- Explain reentrancy in two sentences
- Why does changing the order of two lines fix it?
- Test both banks in Remix with an attacker contract on the Remix VM
Conclusion
Reentrancy happens when a contract sends ETH before updating its state. Update state first, interact last and add a reentrancy guard.
Quick Summary: importance; vulnerable bank; what goes wrong; attacker; fixes; history.