What Is a Reentrancy Attack on Ethereum?
A reentrancy attack exploits a vulnerability in smart contract code where an external contract calls back into the original contract before the first invocation finishes. This allows the attacker to repeatedly drain funds, often emptying the contract in a single transaction. It is one of the oldest and most infamous attack patterns on Ethereum, responsible for the 2016 DAO hack that led to the Ethereum Classic fork.
How Reentrancy Works
Smart contracts on Ethereum can call other contracts. When Contract A calls Contract B, Contract B can call back into Contract A before Contract A has completed its own logic. If Contract A updates its internal state (like a user's balance) after making the external call, the attacker can exploit this ordering.
The core problem is a violation of the "checks-effects-interactions" pattern: the contract checks conditions, then updates its own state, then interacts with external contracts. In a vulnerable contract, the interaction happens before the state update.
A Simplified Example
Imagine a contract that lets users withdraw their deposited ETH:
- The contract checks that the user has enough balance.
- The contract sends ETH to the user's address.
- The contract updates the user's balance to zero.
The attacker deploys a malicious contract that, when it receives ETH, calls the withdraw function again. Since step 3 hasn't happened yet, the contract still shows the attacker's balance as the original amount. The attacker can repeat this loop until the contract is drained or the gas runs out.
Why reentrancy attacks are dangerous
The attack can happen within a single transaction, making it invisible to normal monitoring. The attacker does not need to front-run or manipulate the mempool. The damage is immediate and total - the vulnerable contract can lose its entire balance.
Because Ethereum transactions are atomic (all or nothing), a successful reentrancy attack either completes entirely or reverts. If the attacker runs out of gas mid-attack, the entire transaction reverts, but the attacker can simply try again with more gas.
How to prevent reentrancy attacks
Use the checks-effects-interactions pattern
This is the simplest and most reliable defense. Always structure functions in this order:
- Checks: Validate conditions (e.g., does the user have enough balance?).
- Effects: Update the contract's state (e.g., reduce the user's balance).
- Interactions: Make external calls (e.g., send ETH).
If step 3 triggers a callback, the state is already updated, so the callback sees the correct (reduced) balance.
Use a reentrancy guard
A reentrancy guard is a modifier that locks a function while it is executing. If the function is called again before the first call finishes, the guard rejects the reentrant call. OpenZeppelin's ReentrancyGuard contract provides this pattern and is widely used.
Limit External Calls
Avoid sending ETH or calling unknown contracts inside functions that manage user balances. Where possible, use a withdrawal pattern where users pull funds themselves rather than having the contract push funds.
Use Transfer Instead of Call (Legacy)
On older Ethereum versions, transfer() sent a fixed 2300 gas - enough to log an event but not enough to execute a reentrancy attack. However, this is no longer reliable after the gas cost changes in EIP-2929 and EIP-1884. The recommended approach today is to use call() with a reentrancy guard.
Notable Reentrancy Attacks
The DAO hack in 2016 was the first major reentrancy attack. An attacker drained approximately 3.6 million ETH from The DAO contract by exploiting a reentrancy vulnerability in its split function. This event led to a contentious hard fork that created Ethereum and Ethereum Classic.
Since then, many DeFi protocols have been exploited using variations of reentrancy. Flash loan attacks often combine reentrancy with other vulnerabilities. The attack surface has expanded as smart contracts interact more complexly.
How to Check if a Contract Is Vulnerable
If you are interacting with a contract, you can check Etherscan for the source code. Look for functions that make external calls before updating state. Common red flags include:
- Using
call{value: amount}("")before updating a balance mapping. - Functions that send ETH and then subtract from a user's balance afterward.
- Absence of a reentrancy guard modifier on functions that transfer value.
For your own contracts, always use static analysis tools like Slither or Mythril to detect reentrancy vulnerabilities before deploying.
Summary
A reentrancy attack exploits the ordering of external calls and state updates in a smart contract. It allows an attacker to drain funds by recursively calling back into the vulnerable function. The standard defenses are the checks-effects-interactions pattern and reentrancy guard modifiers. Understanding this attack is essential for anyone writing or auditing Ethereum smart contracts.
Not financial advice. bidenerc.site publishes market data and general information about Biden Coin. Crypto assets are volatile and you can lose everything you put in. Nothing here is a recommendation to buy, sell or hold, and we make no price predictions.
Prices are sourced from third parties and may be delayed or wrong. Verify anything you intend to act on against a primary source.