# Re-entrancy in settleAuction allow stealing all funds

settleAuction accepts attacker-controlled input tokens and lacks a reentrancy guard, letting a bonder re-enter to lower ibRatio and drain the Basket.

- Payout: Audit contest
- Severity: high
- Program: Kuiper
- Found by: cmichel
- Disclosed: 2022-01-26
- Write-up quality: 8/10
- Original: https://github.com/code-423n4/2021-09-defiprotocol-findings/issues/223
- Library page: https://bugbountylibrary.com/r/c4%3A2021-09-defiprotocol%3AH-01
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:code4rena sev:high tech:evm tech:solidity technique:chained technique:source-review vuln:business-logic vuln:reentrancy

**Target:** Auction.settleAuction / Basket settlement logic
**Root cause:** settleAuction performs external token transfers (safeTransferFrom on arbitrary inputTokens) before state updates and without a reentrancy guard; the post-transfer balance check uses newRatio which shrinks on each reentrant call, lowering tokensNeeded.
**Steps:**
1. Publisher calls basket.publishNewIndex to start auction.
2. Attacker waits until bondTimestamp-auctionStart makes newRatio < basket.ibRatio (e.g. ~11000s: newRatio=2e18-1.1e18=0.9e18).
3. Attacker calls bondForRebalance to become bonder.
4. Deploy attackerToken whose transferFrom re-enters Auction.settleAuction with crafted inputTokens/outputTokens.
5. Call settleAuction with inputTokens=[attackerToken]; inner call sets ibRatio=0.9e18, then outer resumes with lower tokensNeeded and extracts more outputTokens.
6. Repeat reentrancy until basket balance check fails; drain all basket tokens.
**How found:** Noticed Basket approves Auction for all tokens and settleAuction lets bonder transfer arbitrary amounts, gated only by a post-hoc balance check; observed inputTokens are attacker-controlled and state (ibRatio) updates after transfers, so reentering lowers newRatio and thus tokensNeeded.
**Exploitation:** Bonder supplies a malicious ERC20 as inputToken; its transferFrom re-enters settleAuction. Each nested call completes settlement, lowering basket.ibRatio (0.9e18 -> 0.7e18 -> 0.3e18), which reduces tokensNeeded in the balance require, allowing progressively larger outputToken withdrawals. Attacker returns bondAmount each iteration and hollows out the basket.
**Escalation:** Recursive reentrancy compounds the ibRatio reduction, scaling from a single over-withdrawal to full basket drain.
**Insight:** When a settlement function's safety check depends on a ratio/state variable that is updated after external calls, check whether attacker-supplied tokens can reenter to shrink that variable and relax the invariant between iterations.

