# The incorrect accounting of protocol fee will cause double charging fee and wrong distribution of earnings for variable users

LidoVault counts protocol fee inside withdrawn staking shares, so variable users who withdraw mid-vault are double-charged fees and earnings are misdistributed at vault end.

- Payout: Audit contest
- Severity: high
- 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/129
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-08-saffron-finance%3AH-2
- Tags: area:web3 domain:web3 impact:funds-loss impact:integrity kind:contest platform:sherlock sev:high tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:fee-bypass

**Target:** LidoVault.sol variable-side withdrawal accounting
**Root cause:** When a variable user withdraws mid-vault, ethAmountOwed (which includes the protocol fee) is converted to shares and added to withdrawnStakingEarningsInStakes and variableToWithdrawnStakingEarningsInShares. At vault end, totalEarnings subtracts totalProtocolFee again while those fee-inclusive shares are still counted, so the fee is effectively charged twice and the user's share is understated.
**Steps:**
1. Deploy LidoVault with fixedCap=100 ether, variableCap=10 ether, protocolFeeBps=2000, mock Lido rate=1.
2. Alice deposits 10 ether on variable side; set Lido rate to 1.1.
3. Alice calls `withdraw(1)` then `finalizeVaultOngoingVariableWithdrawals()`; totalProtocolFee becomes 2 ether and fee-inclusive shares are recorded.
4. Skip past vault end; set rate to 1.21; Alice calls `withdraw(1)` and `finalizeVaultEndedWithdrawals(1)`.
5. Observe Alice receives only 6 ether instead of 8, and 2 ether remains stuck in the vault after feeReceiver and fixed depositor withdraw.
6. Second path: Alice and Bob each deposit 5 ether; Alice withdraws 4 ether mid-vault; at end Alice gets 2.95 instead of 4 and Bob gets 8.45, with 1 ether stuck.
**How found:** Code reading of the mid-vault withdraw path: noticed ethAmountOwed includes protocolFee but is converted wholesale to shares and stored in withdrawnStakingEarningsInStakes, then at vault end totalEarnings subtracts totalProtocolFee again while those shares are still counted, implying double counting.
**Exploitation:** Any variable-side depositor who withdraws while the vault is active (isStarted && !isEnded) triggers the mis-accounting; on final withdrawal their earnings are reduced by an extra protocol fee that is never credited to the fee receiver, leaving ETH stranded in the contract and shifting earnings between variable users.
**Insight:** When a fee is deducted from a payout, check whether the pre-fee amount is also stored in share/accounting state used later; fee-inclusive shares combined with a later fee subtraction double-charge. Trace the same value through both the interim and final settlement paths.

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

**Mock Lido contracts used by PoC**

```
contract MockLido {
    uint256 public rate;
    mapping(address => uint256) private _sharesOf;

    constructor() {
        rate = 1e27;
    }

    function submit(address _referral) external payable returns (uint256) {
        uint256 shares = msg.value * 1e27 / rate;
        _sharesOf[msg.sender] += shares;
        return shares;
    }

    function approve(address spender, uint256 amount) external returns (bool) {
        return true;
    }

    function getPooledEthByShares(uint256 _sharesAmount) external view returns (uint256) {
        return _sharesAmount * rate / 1e27;
    }

    function getSharesByPooledEth(uint256 _ethAmount) external view returns (uint256) {
        return _ethAmount * 1e27 / rate;
    }

    function balanceOf(address _account) external view returns (uint256) {
        return _sharesOf[_account] * rate / 1e27;
    }

    function sharesOf(address _account) external view returns (uint256) {
        return _sharesOf[_account];
    }

    function setRate(uint256 _rate) external {
        rate = _rate;
    }

    function burnShares(uint256 _amount, address _owner) external {
        uint256 shares = _amount * 1e27 / rate;
        _sharesOf[_owner] -= shares;
    }
}

contract MockLidoWithdrawalQueueERC721 {
    MockLido public lido;

    mapping(uint256 requestId => uint256 amount) public requestIdToAmount;
    uint256 public currentRequestId;
    
    constructor(address _lido) {
        lido = MockLido(_lido);
    }
    function claimWithdrawal(uint256 _requestId) external {
        payable(msg.sender).call{value: requestIdToAmount[_requestId]}("");
    }

    function requestWithdrawals(uint256[] calldata _amounts, address _owner) external returns (uint256[] memory requestIds) {
        lido.burnShares(_amounts[0], _owner);
        requestIdToAmount[currentRequestId] = _amounts[0];

        requestIds = new uint256[](1);
        requestIds[0] = currentRequestId++;
    }
    receive() external payable {
    }
}
```

**Forge test command to run PoC**

```
Run command: `forge test --match-path test/PoC.t.sol -vv`
```

