A reentrancy attack (reentrancy, calling a function again before the previous invocation has finished) exploits a weak point in smart contract design: a malicious contract manages to call a vulnerable contract multiple times while that contract has not yet finished its first execution. This lets the attacker drain other people's funds or corrupt the contract's state. Such attacks have already cost DeFi enormous sums. The DAO hack in 2016 and the KyberSwap incident in 2023 are enough to illustrate the scale of the damage.
Contracts that handle token transfers or withdrawals without solid state locking are the typical target. The idea is simple: execute the withdrawal function several times inside a single transaction and bypass the limits the developer put in place. This class of vulnerability never goes away and keeps migrating across blockchain ecosystems, so developers and auditors have to keep it in mind constantly.
Understanding the mechanics of reentrancy matters to everyone who writes on-chain applications or simply interacts with them. Recognizing the characteristic patterns and putting defenses in place early raises a contract's security level noticeably. Below is a breakdown of how the attack works and how to prevent it.
MECHANICS
It all starts with what looks like an ordinary step: a user's contract calls a function on the vulnerable contract, for example a withdrawal function. The vulnerable contract initiates a token transfer back to the user, and this is where the gap opens. Before the vulnerable contract updates its state (that is, reduces the balance), control passes to the user's contract and its fallback function fires (the fallback is a handler called automatically when funds are received) or a specially prepared function triggers.
That function immediately calls the vulnerable contract's withdrawal function again. Because the balance has not yet been reduced after the first withdrawal, the contract considers the second call legitimate and processes another withdrawal. The cycle repeats many times within a single transaction, and the attacker walks away with more than they were owed.
The key condition for the attack lies in the order of operations inside the vulnerable contract. It makes an external call (sends funds) before updating its internal state (reducing the sender's balance). That gap is precisely what opens the window: while the state is formally still unchanged, the reentrant call goes through as if nothing happened.
To make this concrete, imagine an ATM that dispenses cash and only then deducts the amount from your account. If you can request another dispensation in the moment when the money is already coming out but the deduction has not yet been recorded, the ATM will honestly hand over a second batch from the same balance. A reentrancy attack does exactly that to a contract, except in fractions of a second and dozens of times in a row.
RISK INDICATORS
When evaluating a contract, be on your guard if you see any of the following signs:
- The contract sends Ether or tokens before updating its internal balances.
- The
call()function is used for external transfers without reentrancy protection. - Functions that change state do not follow the Checks-Effects-Interactions pattern.
- External contracts are allowed to trigger state changes without proper validation.
- Sensitive functions have no mutexes (locks that prevent the function from being entered again).
- The audit does not separately check for reentrancy at every external interaction point.
PROTECTION
The foundational measure: strictly apply the Checks-Effects-Interactions pattern in every function that changes state. Internal checks come first, then state is updated, and only after that do external calls happen. With this order in place, a reentrant call sees an already-modified balance and fails.
On top of that, add protection in the form of mutexes or locks so that a function cannot be called again before the previous execution has completed. OpenZeppelin's library provides the battle-tested ReentrancyGuard modifier, which can be attached to sensitive functions in literally one line.
External calls deserve special attention, especially token transfers and Ether sends. Use the low-level call() only when absolutely necessary, and always cover it with reentrancy protection. Where possible, rely on established ERC-20 wrapper contracts that handle transfers safely.
Finally, run security audits with a targeted focus on reentrancy. Bring in reputable third-party auditors with a proven track record in this area. Formal verification (a mathematical proof that the code behaves strictly according to its specification) provides an additional guarantee.
Comparing the two main approaches helps you decide what to use:
| Checks-Effects-Interactions | ReentrancyGuard (mutex) |
|---|---|
| Reorders operations inside the function | Sets a lock flag on entry and clears it on exit |
| Requires no extra variables or gas | Spends a small amount of gas to write the flag |
| Protects only that function | Covers all functions that carry the modifier |
In practice the two are used together: the pattern as the primary code hygiene measure, the mutex as a backup layer.
AFTERMATH
If a reentrancy attack is suspected, damage containment takes center stage. The basic step is to pause all functions that involve fund transfers and state changes. This is normally done through an emergency stop (circuit breaker) mechanism or a pause, if that capability was built into the contract in advance.
After that, notify incident response teams and blockchain explorers as quickly as possible. Platforms like PeckShield and community initiatives like SEAL 911 help track illicit funds and coordinate the response. Also report to law enforcement, such as the FBI or the relevant national cybercrime unit, to kick off an investigation.
Fund recovery depends on the circumstances. Sometimes it is possible to negotiate with exchanges to freeze stolen assets; sometimes multisignature wallets (requiring several signatures for a transfer) are used for controlled movement of remaining funds; and in rare cases, with community consent, a hard fork is carried out. The specific path is determined by the blockchain itself, the nature of the exploit, and the governance mechanisms available.