Smart contract bugs / In what order does it run?
Reentrancy
The contract sends ETH or calls another contract before it updates its own state. The receiver calls back in and withdraws again, with the old balance.
OWASP SC08:2026 · 55 real exploits · $379.5M lost
How the attack works
- A function checks a balance, makes an external call (sends ETH, transfers an ERC-777/721/1155 token, calls a hook), and only then updates the balance.
- The receiver is a contract. Its
receive()or hook function calls the same function, or another function of the protocol. - The state still shows the old balance, so the second call passes the check. The loop repeats until the pool is empty.
- Variants: cross-function (call back into a different function), cross-contract (shared state), read-only (another protocol reads a view function in the middle of the update).
Vulnerable and fixed code
function withdraw() external {
uint256 amt = balance[msg.sender];
(bool ok,) = msg.sender.call{value: amt}(""); // attacker re-enters here
require(ok);
balance[msg.sender] = 0; // too late
}import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
function withdraw() external nonReentrant {
uint256 amt = balance[msg.sender];
balance[msg.sender] = 0; // effects first
(bool ok,) = msg.sender.call{value: amt}(""); // interaction last
require(ok, "send failed");
}Review checklist
- Follow checks-effects-interactions: check, update state, then make the external call.
- Add
nonReentrantto every function that makes an external call, and to all functions that share state with it. - Remember the hidden calls:
safeTransferFromof ERC-721/1155, ERC-777 hooks, flash-loan callbacks,onERC721Received. - If other protocols read your view functions (price, share rate), protect them against read-only reentrancy.
- If you fork Compound V2 or another old codebase, check for tokens with hooks: many forks were drained this way.
How to test for it
- Write an attacker contract that re-enters from
receive()and from a token hook. The second call must revert. - Slither detectors
reentrancy-eth,reentrancy-no-ethandreentrancy-benign.
Real exploits (55)
-
Fei Rari (Rari Capital Fuse pools): Rari's Fuse pools (Fei Rari) lost about $80M to a reentrancy attack through CEther's call.value and the unprotected Comptroller exitMarket() in a Compound fork.
Root cause and fix
Vulnerable code:
Comptroller / CEther (fUSDC-127 Fuse pool) · exitMarket()Rari's Compound fork does not follow checks-effects-interactions. borrow() sends ETH to the borrower via call.value before it records the debt. The attacker re-entered from the fallback function and called exitMarket(). The March reentrancy fix did not cover that function, so the debt was not yet recorded and the collateral looked free to withdraw. The attacker flash-loaned USDC, deposited it as collateral, borrowed ETH, re-entered to take the collateral back, and kept the borrowed ETH. A commenter says an earlier Compound fix for this had been reverted by the developers.
What would have stopped it: Update the borrow record before any external ETH transfer, and put a nonReentrant guard on every state-changing function, including Comptroller's exitMarket(). Add tests that re-enter from a receiver contract, and review any change that reverts upstream security fixes.
-
Rari Capital / Fei Protocol (Fuse pool 127): Rari Capital/Fei Fuse pool lost about $80 million because the ETH borrow function sent ETH before updating borrow state, so the attacker re-entered and withdrew collateral.
Root cause and fix
Vulnerable code:
CEtherDelegate (fETH_127) / Fuse Comptroller · borrow()The Fuse cEther borrow() sent ETH to the borrower before recording the borrow, which broke the checks-effects-interactions order. The Comptroller's collateral check therefore did not see the new debt. The attacker flash-loaned USDC from Balancer, minted fUSDC as collateral, entered the market, and borrowed ETH. In the receive() callback they called exitMarket on the USDC collateral, which passed because the debt was not yet recorded. They then redeemed the USDC and repaid the flash loan, keeping the borrowed ETH without collateral.
What would have stopped it: Update borrow state before making the external ETH transfer, and add a nonReentrant guard to all cToken and Comptroller entry points, including exitMarket. Use transfer-style or other non-callback sends, and test for reentrancy through the ETH receive hook.
-
GMX V1: GMX V1 lost about $42M on Arbitrum to a cross-contract reentrancy that manipulated global short average prices and inflated GLP price; the funds were later returned in exchange for a $5M bounty.
Root cause and fix
Vulnerable code:
Vault / OrderBook / PositionManager (GLP Manager) · executeDecreaseOrder()executeDecreaseOrder() accepted an arbitrary contract address as the order account. When the order-keeper ran it, the refund step called back into the attacker's contract through gmxPositionCallback(). The attacker reentered the vault and reward router mid-transaction, while global short size and globalShortAveragePrices were not updated atomically. This left the BTC average short price far too low ($109,505 down to $1,913), which inflated AUM and the GLP price (from $1.45 to over $27). The attacker then minted and redeemed GLP at the inflated price to drain the pool.
What would have stopped it: Put nonReentrant guards on all entry points of the vault, GLP manager and reward router, so a callback cannot reenter them. Update global short size and average price atomically, and do not let a keeper-executed order call user-supplied contract addresses. Re-audit every post-audit change, especially patches for earlier bugs.
-
Grim Finance: Grim Finance, a Beefy Finance fork on Fantom, lost about $30M because depositFor() in its vault could be re-entered through an attacker-chosen deposit token.
Root cause and fix
Vulnerable code:
GrimBoostVault · depositFor()depositFor() in GrimBoostVault had no reentrancy guard. It also let the caller choose the deposit token, so the attacker passed their own contract as the token. During token.safeTransferFrom, that contract re-entered depositFor() several times. The last nested call deposited real SPIRIT-LP, and the share minting counted the balance wrongly at each level, so the attacker received far more GB vault tokens than they deserved. They then withdrew those tokens for extra SPIRIT-LP, removed the liquidity, and repaid the flash loan.
What would have stopped it: Add a nonReentrant modifier to depositFor() and the other deposit and withdraw functions. Do not let callers choose the deposit token: use only the vault's own want token, or check the token against a whitelist. Compute shares from balance changes only after the transfer has finished.
-
Penpie: Penpie, a yield protocol built on Pendle, lost about $27M when an attacker used a fake Pendle market to re-enter batchHarvestMarketRewards() and inflate rewards.
Root cause and fix
Vulnerable code:
PendleStaking / MasterPenpie (via PendleMarketDepositHelper) · batchHarvestMarketRewards()Penpie's batchHarvestMarketRewards() did not guard against re-entry and accepted a market the attacker created, with no check that it was a real Pendle market. The attacker deployed a fake Pendle market. When the harvest function (_harvestBatchMarketRewards) called redeemRewards(), the attacker's contract re-entered and inflated its staking balance. It then claimed rewards it had not earned.
What would have stopped it: Add a nonReentrant guard to the harvest and reward functions. Only accept markets that are registered or whitelisted by the protocol, and check reward accounting before and after external calls. Audit every upgrade that adds new market integrations.
-
Lendf.Me: Lendf.Me lost about $25M because its money market could be re-entered through the ERC777 tokensToSend hook of imBTC during supply.
Root cause and fix
Vulnerable code:
MoneyMarket · supply() / withdraw()The lending pool's supply() transferred imBTC (an ERC777 token) in before it updated the user's balance. ERC777 calls the sender's tokensToSend hook during the transfer. The attacker registered a hook through ERC1820. They supplied a large amount, then supplied 1 unit more. In the hook for the 1-unit supply, they called withdraw(max). The withdraw paid out the full balance, and then supply() finished and wrote the stale, higher balance. The attacker kept their deposit credit and could withdraw again, which drained the pool.
What would have stopped it: Follow checks-effects-interactions: update balances before any token transfer. Add a nonReentrant guard to supply, withdraw and other state-changing functions. Be careful when listing ERC777 or other hook-enabled tokens as collateral or deposits.
-
Cream Finance: Cream Finance lost $18.8M when the AMP token's ERC777-style transfer hooks let an attacker re-enter borrow() before the first borrow was recorded.
Root cause and fix
Vulnerable code:
AMP token contract · borrow()Cream listed the AMP token as collateral. AMP is an ERC777-style token (ERC1820) that calls hooks (_callPreTransferHooks) during transfer. The attacker took a flash loan, deposited WETH as collateral and borrowed AMP. During the AMP transfer, the hook re-entered borrow() and took more ETH before the first borrow was recorded. A second contract then liquidated part of the loan, and the attacker repaid the flash loan and kept the surplus. This was repeated over 17 transactions for about 6k ETH.
What would have stopped it: Add reentrancy guards (nonReentrant) to borrow and the other lending functions, and update state before making external token transfers (checks-effects-interactions). Review each new collateral token for transfer hooks before listing it, since the audit did not cover the AMP addition.
-
Agave DAO and Hundred Finance: Agave DAO (Aave fork, $5.5M) and Hundred Finance (Compound fork, $6.2M) lost a combined $11.7M on Gnosis Chain to a reentrancy attack via the xDAI token's transfer callback.
Root cause and fix
Vulnerable code:
Agave lending pool v1 (LendingPool) · callAfterTransfer()The xDAI token on Gnosis Chain has a callAfterTransfer() hook that lets the receiver run code during a transfer. The lending protocols accepted it as collateral and updated debt only after the transfer. The attacker used flash loans as initial collateral and nested borrow calls inside the callback, borrowing again before the debt balance was updated. This let them borrow assets worth far more than the collateral supplied.
What would have stopped it: Follow checks-effects-interactions, or add reentrancy guards on borrow, supply and repay paths. Vet tokens used as collateral and reject tokens with transfer hooks, as the original Aave and Compound do. Re-check forked code against the features of the new chain's tokens.
-
Rari Capital (Alpha Homora / Cream-style Bank): Rari Capital's Alpha Homora-style Bank vault was drained of roughly $11M in ETH through cross-contract reentrancy during work(), using a fake token contract.
Root cause and fix
Vulnerable code:
Bank (Alpha Homora v1 vault) 0x67B66C99D3Eb37Fa76Aa3Ed1ff33E8e39F0b9c7A · work()The Bank's work() function reaches external contracts (the goblin and a token address in the calldata) without a reentrancy guard. It also does not check that the token address in the calldata is a trusted one. The attacker used a contract at 0x2f75... as the fake token. It has a donate() function, and the attacker sent about 1,031 ETH to it. While work() was running, that contract called back into the protocol before the vault's accounting was updated. This let the attacker pull out ETH that was not theirs. The PoC only shows the entry calls, not the exact re-entered path, so the details of the callback are inferred from the title.
What would have stopped it: Add a nonReentrant guard to work() and to every function that touches the same shared state, so a callback cannot re-enter across contracts. Follow checks-effects-interactions, and check any token or goblin address from calldata against a whitelist.
-
XSURGE (SurgeBNB): XSURGE lost about $5 million on BNB Chain when an attacker used a flash loan and re-entered the Surge token's sell() through the BNB payout callback, which let them buy and sell at a stale price.
Root cause and fix
Vulnerable code:
Surge · sell()The Surge token's sell() sends BNB to the seller before it finishes updating its state. The price is derived from the contract's BNB balance and the token supply, so it was stale during that external call. The attacker flash-borrowed 10,000 WBNB from PancakeSwap, unwrapped it and bought Surge. They then called sell() repeatedly. In the receive() callback during each payout they bought more Surge at the not-yet-updated low price, and sold it all at the higher price. After repaying the flash loan they kept the profit.
What would have stopped it: Add a reentrancy guard (nonReentrant) to buy and sell. Follow checks-effects-interactions: burn the tokens and update the balances and price state before sending BNB. Test that the price calculation is consistent in the middle of a sell.
45 more examples in the full guide.