Difficulty: Advanced
What is a reentrancy attack in Ethereum smart contracts, and how do you prevent it?
A reentrancy attack happens when a contract makes an external call to another contract before it has finished updating its own state, and that external contract calls back into the original function. Because the original function's state is still stale, the callback can re-run logic that should only run once, for example withdrawing funds repeatedly before the balance is ever reduced. This was the flaw behind the 2016 DAO hack, which drained around 3.6 million ETH and led to the Ethereum hard fork.
On Ethereum, sending ETH to an address can trigger that recipient's code. When a contract sends ETH using a low-level call, the recipient's receive or fallback function runs immediately, still inside the sender's transaction. If the sending contract updates its records only after the transfer, the attacker's fallback can call back into the withdraw function while the balance still shows the full amount. The function has no idea it is being re-entered, so it pays out again and again until gas or funds run out.
The classic buggy order is: check the balance, send the ETH, then update the balance. The send happens before the update, so during the send the attacker re-enters and the stale balance passes the check every time. The fix is to change the ordering and add guards.
nonReentrant modifier sets a lock on entry and releases it on exit, so any re-entrant call reverts. Useful for functions that cannot fully avoid external calls mid-logic.Reentrancy is not only about ETH transfers. ERC-777 token hooks and any callback-style token can re-enter, and read-only reentrancy can corrupt view functions that other protocols rely on for pricing. Modern audits treat any external call as a reentrancy risk, not just raw ETH sends.
call is risky; any external interaction that hands control to another contract can re-enter.transfer 2300-gas idea), which is unreliable since gas costs change across network upgrades.// VULNERABLE: sends ETH before updating state
function withdraw() external {
uint256 amount = balances[msg.sender];
require(amount > 0, "nothing to withdraw");
(bool ok, ) = msg.sender.call{value: amount}(""); // re-entry point
require(ok, "transfer failed");
balances[msg.sender] = 0; // too late: attacker already re-entered
}
// SAFE: Checks-Effects-Interactions + reentrancy guard
function withdrawSafe() external nonReentrant {
uint256 amount = balances[msg.sender];
require(amount > 0, "nothing to withdraw");
balances[msg.sender] = 0; // effect BEFORE the external call
(bool ok, ) = msg.sender.call{value: amount}("");
require(ok, "transfer failed");
}
Zeroing the balance before the external call means a re-entrant withdraw sees a zero balance and reverts. The nonReentrant modifier adds a lock as a second line of defence. The language shown is Solidity; cpp is used only for highlighting.
Smart Contracts, Reentrancy, Checks-Effects-Interactions, Reentrancy Guard, Solidity