# `totalEarnings` is incorrect when withdrawing after ending which will withdraw too many funds leaving the `Vault` insolvent

Protocol fee stored in ETH not shares in LidoVault causes totalEarnings overestimation, letting early withdrawers drain excess funds and insolvency.

- Payout: Audit contest
- Severity: high
- Program: Saffron Finance
- Found by: 0x73696d616f
- Disclosed: 2024-08-05
- Write-up quality: 8/10
- Original: https://github.com/sherlock-audit/2024-08-saffron-finance-judging/issues/85
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-08-saffron-finance%3AH-1
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:sherlock sev:high tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:business-logic

**Target:** LidoVault.sol smart contract
**Root cause:** totalProtocolFee is not scaled to the current stETH eth/shares ratio, so as the ratio increases it overestimates totalEarnings.
**Steps:**
1. Deploy LidoVault with 100 ETH and 100 stETH shares; two users each hold 50% variable bearer tokens.
2. stETH share price doubles to 2 ETH/share (200 ETH, 100 shares).
3. User A calls `withdraw()`: totalEarnings = 200*(100+0)/100 - 100 = 100; ethAmountOwed = 50; protocolFee = 2.5 ETH; stakesAmountOwed = 25; state: totalProtocolFee=2.5, withdrawnStakingEarningsInStakes=25, ETH=150, shares=75.
4. stETH share price doubles again to 4 ETH/share (300 ETH, 75 shares); vault ends; fixed users claim 100 ETH leaving 200 ETH and 50 shares.
5. User B calls `vaultEndedWithdraw()`: vaultEndingStakesAmount=50, vaultEndingETHBalance=200; `finalizeVaultEndedWithdrawals()` sets amountWithdrawn=200, vaultEndedStakingEarnings=200 - 200 * 0.05 = 190.
6. `vaultEndedWithdraw()` computes totalEarnings = 200*25/50 - 2.5 + 190 = 287.5; ethAmountOwed = 143.75, but only 190 ETH remains and user B should get 75% (142.5), while 143.75/190 ~= 0.757.
7. User B withdraws 143.75 ETH, leaving insufficient funds for user A's remaining claim, causing insolvency.
**How found:** Code review of LidoVault::vaultEndedWithdraw() revealed totalProtocolFee not scaled to current stETH eth/shares ratio, while other components were scaled; manual calculation with changing share price confirmed overestimation.
**Exploitation:** Early withdrawer calls withdraw() before vault end, then after vault end calls vaultEndedWithdraw() to receive an inflated ethAmountOwed due to unscaled protocol fee, draining more than their fair share and leaving later withdrawers unable to claim.
**Insight:** When mixing ETH and share-denominated values in DeFi accounting, ensure all fee/earnings components are converted to the same unit (shares) before arithmetic; track protocol fees in shares to automatically scale with exchange rate changes.

