---
title: "Reentrancy"
url: https://dontgetrekt.today/dev/reentrancy
section: "In what order does it run?"
owasp: SC08:2026
---

# 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.

## How the attack works

1. 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.
2. The receiver is a contract. Its `receive()` or hook function calls the same function, or another function of the protocol.
3. The state still shows the old balance, so the second call passes the check. The loop repeats until the pool is empty.
4. 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 code

```solidity
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
}
```

## Fixed code

```solidity
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 `nonReentrant` to every function that makes an external call, and to all functions that share state with it.
- Remember the hidden calls: `safeTransferFrom` of 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-eth` and `reentrancy-benign`.

## Real exploits (55)

- 2022-04-30 Fei Rari (Rari Capital Fuse pools) ($80.0M): 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. [rekt.news](https://rekt.news/fei-rari-rekt)
- 2022-04-30 Rari Capital / Fei Protocol (Fuse pool 127) ($80.0M): 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. [PoC](https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2022-04/Rari_exp.sol)
- 2025-07-09 GMX V1 ($42.0M): 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. [rekt.news](https://rekt.news/gmx-rekt)
- 2021-12-18 Grim Finance ($30.0M): 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. [rekt.news](https://rekt.news/grim-finance-rekt) [PoC](https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2021-12/Grim_exp.sol)
- 2024-09-03 Penpie ($27.0M): 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. [rekt.news](https://rekt.news/penpie-rekt) [PoC](https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2024-09/Penpiexyzio_exp.sol)
- 2020-04-19 Lendf.Me ($25.0M): Lendf.Me lost about $25M because its money market could be re-entered through the ERC777 tokensToSend hook of imBTC during supply. [PoC](https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2020-04/LendfMe_exp.sol)
- 2021-08-30 Cream Finance ($18.8M): 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. [rekt.news](https://rekt.news/cream-rekt) [PoC](https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2021-08/Cream_exp.sol)
- 2022-03-15 Agave DAO and Hundred Finance ($11.7M): 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. [rekt.news](https://rekt.news/agave-hundred-rekt) [PoC](https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2022-03/Agave_exp.sol)
- 2021-05-09 Rari Capital (Alpha Homora / Cream-style Bank) ($11.0M): 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. [PoC](https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2021-05/RariCapital_exp.sol)
- 2021-08-17 XSURGE (SurgeBNB) ($5.0M): 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. [PoC](https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2021-08/XSURGE_exp.sol)

## References

- [OWASP SC08:2026](https://scs.owasp.org/sctop10/SC08-ReentrancyAttacks/)
- [SWC-107: Reentrancy](https://swcregistry.io/docs/SWC-107/)
- [Solidity docs: Security considerations](https://docs.soliditylang.org/en/latest/security-considerations.html#reentrancy)
