Advanced3 min readNov 4, 2026

Reentrancy Attacks Explained

Reentrancy is a critical vulnerability where a contract's state is updated after an external call, allowing malicious actors to recursively drain funds.

Reentrancy occurs when a contract makes an external call to an untrusted address before it updates its own internal state, leaving it vulnerable to being called again before the original operation completes. This pattern was famously exploited in the 2016 DAO attack, where the attacker leveraged a recursive callback to withdraw funds repeatedly. By controlling the flow of execution, the malicious contract effectively pauses the victim's logic, executing subsequent operations while the victim still believes the previous withdrawal has not yet finalized.

The DAO and why it mattered

The DAO attack remains the definitive case study for reentrancy because it highlighted the catastrophic impact of failing to handle external state dependencies. The contract utilized a split mechanism, allowing members to withdraw their funds based on their ownership stake. However, the order of operations—invoking the receiver's fallback function before updating the user's recorded balance—allowed the attacker to re-enter the withdrawal function multiple times within the same transaction. This recursive drain bypassed balance checks entirely, leading to a significant loss of funds and eventually contributing to the Ethereum hard fork. The incident demonstrated that developers must treat every external contract as potentially malicious.

How the callback actually works

In a standard reentrancy scenario, the victim contract performs an interaction by sending Ether or calling a function on an untrusted contract address. When this external call is made, the control flow is transferred to the callee, which can then execute its own logic, including calling the victim contract again. Because the state variable representing the user's balance has not been decremented, the victim contract proceeds as if the original transaction has not been fully processed. This cycle repeats until the victim's gas is exhausted or the balance is fully depleted. Modern development avoids this by ensuring that the contract state accurately reflects the pending withdrawal before execution starts.

Checks, effects, interactions

The most effective defense against reentrancy is the strict application of the Checks-Effects-Interactions (CEI) pattern. This architectural standard mandates that developers perform all validation logic (Checks) first, update the contract state variables (Effects) second, and only perform external calls (Interactions) last. By updating the user's balance before initiating a transfer, the contract ensures that any nested call will see the updated, reduced balance. This renders recursive attempts to drain funds impossible, as the victim's state logic will reject subsequent calls due to insufficient balance.

| Stage | Action | Purpose | | :--- | :--- | :--- | | Checks | Validate inputs, requirements, permissions | Ensure conditions are met | | Effects | Modify local state variables | Reflect the transaction outcome | | Interactions | Perform external calls (e.g., call, send) | Finalize the logic outside the contract |

Reentrancy guards and their limits

Developers frequently implement reentrancy guards using a mutex pattern, often defined as a modifier in Solidity. This modifier sets a boolean flag in storage when a function begins and resets it upon completion, effectively locking the function against re-entry. Since EIP-1153, transient storage offers a more cost-effective alternative to permanent storage flags, as the data is automatically cleared after the transaction. However, these guards are not a panacea for all reentrancy vulnerabilities, as they only protect individual functions. If a contract contains multiple state-mutating functions that share the same underlying data, a user might still exploit a cross-function reentrancy path.

Variants and modern defences

Modern smart contract development must account for sophisticated reentrancy variants beyond simple balance drains. Read-only reentrancy occurs when a view function returns inconsistent values during a transaction, misleading third-party protocols that query this state. Token hooks, such as those found in ERC-777 or ERC-1155, also provide implicit callbacks that trigger whenever a transfer occurs, potentially executing code when the developer least expects it. Consequently, relying on the 2300 gas stipend provided by the transfer() function is no longer recommended. The best practice is to adopt a pull-payment pattern, where users must claim funds in a separate transaction, effectively breaking the atomic dependency between state changes and external interactions.

Code

Reentrancy Guard Implementation
modifier nonReentrant { require(!locked, 'Reentrancy detected'); locked = true; _; locked = false; }

References

  1. [1]SWC-107: Reentrancy— Smart Contract Weakness Registry
  2. [2]Smart contract security recommendations— ethereum.org

Something wrong or missing?

Editors review every submission. If a source is broken or a claim is wrong, tell us here.