# `LidoVault::vaultEndedWithdraw` doesn't take into consideration income withdrawals before slashing, blocking variable users from withdrwing their income

LidoVault computes post-slash totalEarnings using pre-slash withdrawnStakingEarningsInStakes, inflating variable users' earnings share beyond balance and reverting withdrawals.

- Payout: Audit contest
- Severity: medium
- Program: Saffron Finance
- Found by: 0x73696d616f
- Disclosed: 2024-08-05
- Write-up quality: 7/10
- Original: https://github.com/sherlock-audit/2024-08-saffron-finance-judging/issues/73
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-08-saffron-finance%3AM-1
- Tags: area:web3 domain:web3 impact:dos kind:contest platform:sherlock sev:medium tech:solidity technique:chained technique:source-review vuln:accounting-error vuln:dos

**Target:** LidoVault.sol vaultEndedWithdraw / finalizeVaultEndedWithdrawals
**Root cause:** vaultEndedWithdraw multiplies post-slash vaultEndingETHBalance/vaultEndingStakesAmount by withdrawnStakingEarningsInStakes recorded pre-slash, overstating totalEarnings and stakingEarningsShare.
**Steps:**
1. Deposit FIXED 1000 ETH and VARIABLE 15+15 ETH; claim fixed premium
2. Mid-duration add staking earnings so vault earns 100 ETH
3. VARIABLE user calls `withdraw(SIDE.VARIABLE)`; withdrawnStakingEarningsInStakes becomes >0
4. Advance past endTime; vault ended
5. Mock Lido slashing via `subtractStakingEarnings(50 ETH)`
6. FIXED user withdraws and finalizes
7. VARIABLE user calls `finalizeVaultEndedWithdrawals(SIDE.VARIABLE)` -> reverts with 'ETF'
**How found:** Reasoned about README's slashing tolerance: noticed withdrawnStakingEarningsInStakes is denominated in pre-slash shares while vaultEndingETHBalance/vaultEndingStakesAmount are post-slash, then built a test combining mid-vault variable withdrawal with post-end slashing.
**Exploitation:** No attacker profit; griefing/DoS condition where slashing after a variable income withdrawal makes stakingEarningsShare exceed contract balance, so transferWithdrawnFunds reverts and variable users cannot withdraw income.
**Insight:** When a contract mixes share-denominated snapshots taken at different times (pre-slash vs post-slash), check every multiplication of a stored share count by a current exchange rate; slashing/rebasing between the two corrupts the product.

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

**Test reproducing slashing DOS after income withdrawal**

```
it("BUG - DOS, can't withdraw after Slashing", async () => {
  const { lidoVault, addr1, addr2, addr3 } = await loadFixture(deployLidoVaultFixture)
  const { lidoMock } = await setupMockLidoContracts(lidoVault)

  // Users deposit FIXED and VARIABLE
  await lidoVault.connect(addr1).deposit(SIDE.FIXED, { value: parseEther('1000') })
  await lidoVault.connect(addr2).deposit(SIDE.VARIABLE, { value: parseEther('15') })
  await lidoVault.connect(addr3).deposit(SIDE.VARIABLE, { value: parseEther('15') })

  // Vault has started
  expect(await lidoVault.isStarted()).to.equal(true)

  // User 1 claims FIXED premium
  await lidoVault.connect(addr1).claimFixedPremium()

  // Half time passes
  const { duration, endTime } = await getTimeState(lidoVault)
  await time.increaseTo(endTime - duration / BigInt(2))

  // Lido rebasing, vault earns 100 ETH
  await lidoMock.addStakingEarningsForTargetETH(
    parseEther('1100'),
    await lidoVault.getAddress()
  )

  // User 2 withdraws their income (part of the above rebasing)
  await lidoVault.connect(addr2).withdraw(SIDE.VARIABLE)

  // Withdrawal was sent to Lido
  expect(
    (await lidoVault.getVariableToVaultOngoingWithdrawalRequestIds(addr2.address)).length
  ).to.equal(1)
  // `withdrawnStakingEarningsInStakes` is now > 0
  expect(await lidoVault.withdrawnStakingEarningsInStakes()).to.be.greaterThan(0)

  // End time passes
  await time.increaseTo(endTime + BIG_INT_ONE)

  // Vault is ended
  expect(await lidoVault.isEnded()).to.equal(true)

  // Lido slashes the vault
  await lidoMock.subtractStakingEarnings(parseEther('50'))

  // User 1 withdraws their FIXED deposit
  await lidoVault.connect(addr1).withdraw(SIDE.FIXED)
  await lidoVault.connect(addr1).finalizeVaultEndedWithdrawals(SIDE.FIXED)

  // User 3 can't withdraw his income
  await expect(
    lidoVault.connect(addr3).finalizeVaultEndedWithdrawals(SIDE.VARIABLE)
  ).to.be.revertedWith('ETF')
})
```

