Reentrancy Attack
- Category
- Security and Custody
- Level
- Expert
Simple definition
A contract bug of ordering: it sends funds out before recording that your balance fell, so the receiving contract can call back in and be paid again from stale bookkeeping.
Professional definition
A vulnerability class in which a contract makes an external call before updating its own state. The called contract can call back into the vulnerable function while the first invocation is unfinished, see the pre-update state, and repeat an action — classically a withdrawal — that should already have been recorded as taken.
Technical definition
The flaw is ordering, not arithmetic. A function that checks a condition, performs an external transfer and only then writes storage leaves a window in which its invariants are false while control sits with untrusted code. Variants include cross-function reentrancy, where the re-entering call hits a different function sharing the same state, and read-only forms, where a view function reports inconsistent state. The standard mitigation is the checks-effects-interactions order: validate, update state, then make external calls. A reentrancy guard adds a lock; both together are usual practice.
Academic definition
A concurrency-like vulnerability in which an external call transfers control before the caller's state invariants have been restored, permitting recursive invocation against stale state.
Mechanism
A call to another contract is a transfer of control, not merely a message. Until the calling function returns, its storage still holds whatever it last wrote, so any invariant it means to restore after the call does not yet exist. Making state updates the last step removes the window entirely, which is why ordering, not review effort, is the primary defence.
In practice
- Who uses it
- Contract developers, auditors, and reviewers reading withdrawal, settlement or reward-claim logic.
- Why it matters
- It is one of the oldest and most costly contract vulnerability classes, and the mitigation is cheap, mechanical and verifiable by reading.
- Limitations
- Guards and ordering stop classic reentrancy, not every state-inconsistency bug: callbacks, token transfer hooks and composed protocols create paths a review must trace deliberately.
Common mistakes
- Making an external transfer before writing the state change.
- Trusting a token or callback because the project is well known.
- Assuming a guard on one function protects state shared with others.
Examples
A pool holds 10 units and an account is entitled to 1. If the withdrawal function pays before it writes the new balance, the recorded entitlement never falls during the call, so the pool can be drained to zero within one transaction.
- Pool balance = 10 units
- Recorded entitlement of the calling account = 1 unit
- Order of operations: check, transfer out, then write state
- Each re-entry reads the entitlement as still 1, because the write has not happened
- Payments of 1 unit repeat while the pool has a balance: 10 payments
- Correct order writes the entitlement to 0 before the transfer, so the second check fails
Illustrative counting of a single vulnerable pattern. The figure is not a loss estimate for any real incident, and the fix is the ordering, not a limit on the number of calls.
A settlement function reduces a user's recorded collateral only after sending the payout. The receiving address is a contract, and its receive logic calls the same function again before the first call has returned.
Related terminology
- related
- Smart Contract Smart Contract Audit Bug Bounty Flash Loan Oracle Manipulation Decentralised Finance Lending Protocol Approval Exploit
Flash Loan
A flash loan supplies capital inside one transaction; reentrancy is a flaw in call ordering. Flash loans often magnify an exploit but are not themselves the bug.
Sources
IOSCO · recommendations addressing smart-contract vulnerabilities and operational resilience
https://www.iosco.org/library/pubdocs/pdf/ioscopd754.pdf URL verified
BIS Quarterly Review · 2021-12 · discussion of code risk and exploit losses in decentralised finance
https://www.bis.org/publ/qtrpdf/r_qt2112b.pdf URL verified
Editorial status
Flagged by the editor as uncertain on a linguistic, mathematical or regulatory point.
