# Sandwich attack to accruePremiumAndExpireProtections()

accruePremiumAndExpireProtections() raises totalSTokenUnderlying, letting an attacker front-run with deposit() and back-run with withdraw() to capture a share of accrued premium.

- Payout: Contest reward
- Severity: high
- Program: Carapace
- Credited: libratus
- Disclosed: 2023-02-01
- Write-up quality: 6/10
- Original: https://github.com/sherlock-audit/2023-02-carapace-judging/issues/26
- Library page: https://bugbountylibrary.com/r/sherlock%3A2023-02-carapace%3AH-12
- Tags: domain:web3 impact:funds-loss kind:contest platform:sherlock sev:high tech:evm tech:solidity technique:source-review vuln:business-logic vuln:oracle-manipulation

**Target:** ProtectionPool.sol accruePremiumAndExpireProtections() / deposit() / withdraw()
**Root cause:** accruePremiumAndExpireProtections() increases totalSTokenUnderlying in a single step, instantly raising the sToken exchange rate; deposits and withdrawals in the same block capture or dodge that step.
**Steps:**
1. Pool state: totalSTokenUnderlying=1,000,000, totalSupply=1,000,000 shares (rate 1/1); Bob holds 100,000 shares.
2. Pending accruePremiumAndExpireProtections() will add 100,000 to totalSTokenUnderlying (L346).
3. Bob front-runs: `deposit(100000)` -> 1,100,000 underlying / 1,100,000 shares, rate still 1/1; Bob now 200,000 shares.
4. accruePremiumAndExpireProtections() executes: 1,200,000 underlying / 1,100,000 shares, rate 12/11.
5. Bob back-runs: `withdraw(100000 shares)` (request made two cycles earlier) -> 100,000*12/11 = 109,090 underlying.
6. Profit ~9,090 underlying tokens.
**How found:** Reading accruePremiumAndExpireProtections() and noticing it mutates totalSTokenUnderlying in one step, then modeling deposit/withdraw around that step as a classic share-price sandwich.
**Exploitation:** Attacker pre-positions shares to satisfy the two-cycle withdrawal request, front-runs the premium accrual with deposit() in the same block, lets accruePremiumAndExpireProtections() bump the exchange rate, then withdraws to realize the inflated share value.
**Insight:** Any function that bumps a vault/pool's underlying balance in a single call (premium accrual, fee distribution, donation) is sandwichable via deposit/withdraw around it; check whether accrual is streamed or instantaneous.
