Protocol fee resides in the diamond contract can be wrongly sent to users if the underlying vault temporarily disables withdrawal
When a user collects earnings, the contract attempts to withdraw from an underlying vault that has temporarily disabled withdrawals, causing the withdrawal to be skipped. The contract then deposits its own tokens (which include protocol fees) into the vault and later transfers those tokens to the user, resulting in loss of protocol fees.
Source: https://github.com/sherlock-audit/2025-04-burve-judging/issues/247
Found by
h2134
Summary
Protocol fee resides in diamond contract can be wrongly sent to users if the underlying vault temporarily disables withdrawal.
Root Cause
When user collects earnings, trimAllBalances() is called to catch up on rehypothecation gains.
ValueFacet::collectEarnings():
// Catch up on rehypothecation gains before we claim fees.
Store.closure(cid).trimAllBalances();
The earnings are to be withdrawn from and then deposited back into the vault.
uint256 residualReal = realBalance - targetReal;
@> vProxy.withdraw(cid, residualReal);
bgtResidual = FullMath.mulDiv(residualReal, bgtValue, value);
@> reserveSharesEarned = ReserveLib.deposit(
vProxy,
self.vid,
residualReal - bgtResidual
);
vProxy.commit();
Normally, the withdrawn amount and deposited amount are expected to have a perfect net so there would not be an actual deposit or withdrawal.
if (assetsToDeposit > 0 && assetsToWithdraw > 0) {
// We can net out and save ourselves some fees.
if (assetsToDeposit > assetsToWithdraw) {
assetsToDeposit -= assetsToWithdraw;
assetsToWithdraw = 0;
} else if (assetsToWithdraw > assetsToDeposit) {
assetsToDeposit = 0;
assetsToWithdraw -= assetsToDeposit;
} else {
// Perfect net!
@> return;
}
}
However, things would go wrong if the underlying vault temporarily disables withdrawal. As can be seen, VaultProxy queries the withdrawable amount from the underlying vault, this is done by calling maxWithdraw() of the underlying vault.
function withdraw(
VaultProxy memory self,
ClosureId cid,
uint256 amount
) internal {
// We effectively don't allow withdraws beyond uint128 due to the capping in balance.
uint128 available = self.active.balance(cid, false);
@> uint256 maxWithdrawable = self.active.withdrawable();
if (maxWithdrawable < available) available = uint128(maxWithdrawable);
if (amount > available) {
self.active.withdraw(cid, available);
self.backup.withdraw(cid, amount - available);
} else {
self.active.withdraw(cid, amount);
}
}
VaultE4626Impl::withdrawable():
/// Return the most we can withdraw right now.
function withdrawable(
VaultE4626 storage self
) internal view returns (uint128) {
@> return min128(self.vault.maxWithdraw(address(this)));
}
When the underlying vault disables withdrawal, maxWithdraw() returns 0. Hence the VaultProxy withdrawal would return early without queueing withdrawal and updating VaultTemp.vars[2] (assets to withdraw).
/// Queue up a withdrawal for a given cid.
function withdraw(
VaultPointer memory self,
ClosureId cid,
uint256 amount
) internal {
@> if (isNull(self) || amount == 0) return;
if (self.vType == VaultType.E4626) {
getE4626(self).withdraw(self.temp, cid, amount);
} else {
revert VaultTypeUnrecognized(self.vType);
}
}
As a result, when the queued deposit/withdrawal is committed, there is not prefect net between assetsToDeposit and assetsToWithdraw, hence the tokens in the diamond contract will be deposited to the underlying vault and the transferred amount is assetsToDeposit.
By then trimBalance() is done and the process flow back into collectEarnings(), the withdraw() in ReserveLib is called.
ValueFacet::collectEarnings():
collectedBalances[i] = ReserveLib.withdraw(
vid,
collectedShares[i]
);
function withdraw(
VertexId vid,
uint256 shares
) internal returns (uint256 amount) {
Reserve storage reserve = Store.reserve();
uint8 idx = vid.idx();
if (reserve.shares[idx] == 0) return 0;
VaultProxy memory vProxy = VaultLib.getProxy(vid);
uint128 balance = vProxy.balance(RESERVEID, true);
amount = (shares * balance) / reserve.shares[idx];
@> vProxy.withdraw(RESERVEID, amount);
@> vProxy.commit();
reserve.shares[idx] -= shares;
}
Likewise, VaultProxy returns earlier when the underlying vault's maxWithdraw() returns 0, and there is no actual withdrawal in commit() as assetsToWithdraw is 0.
Then back in collectEarnings(), it sends tokens to the user by transferring the tokens in the diamond contract (again).
ValueFacet::collectEarnings():
TransferHelper.safeTransfer(
tokenReg.tokens[i],
recipient,
collectedBalances[i]
);
The tokens reside in diamond contract are the protocol fee, but they are wrongly sent to users.
Internal Pre-conditions
There are protocol fee in the diamond contract.
External Pre-conditions
The underlying vault disable withdrawal temporarily.
Attack Path
None
Impact
The protocol fee is lost.
PoC
Please run forge test --mt testAudit_CollectingEarningsEatsUpProtocolFee.
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.27;
import "forge-std/Test.sol";
import "./mocks/MockERC20.sol";
import "openzeppelin-contracts/mocks/docs/ERC4626Fees.sol";
import {VaultType} from "../src/multi/vertex/VaultPointer.sol";
import "../src/multi/Diamond.sol";
import "../src/integrations/adjustor/MixedAdjustor.sol";
import "../src/integrations/adjustor/DecimalAdjustor.sol";
contract AuditTest is Test {
uint8 constant MAX_TOKENS = 16;
address owner = makeAddr("OWNER");
address diamond;
MockERC20 tokenA = new MockERC20("Token A", "A", 18);
MockERC20 tokenB = new MockERC20("Token B", "B", 6);
MockERC4626WithFees vaultA = new MockERC4626WithFees(tokenA, "Vault A", "VA");
MockERC4626WithFees vaultB = new MockERC4626WithFees(tokenB, "Vault B", "VB");
function setUp() public {
vm.startPrank(owner);
BurveFacets memory facets;
{
address vauleFacet = address(new ValueFacet());
address valueTokenFacet = address(new ValueTokenFacet());
address simplexFacet = address(new SimplexFacet());
address swapFacet = address(new SwapFacet());
address vaultFacet = address(new VaultFacet());
MixedAdjustor mixedAdjustor = new MixedAdjustor();
// Configure adjustor
{
DecimalAdjustor decimalAdjustor = new DecimalAdjustor();
mixedAdjustor.setAdjustor(address(tokenB), address(decimalAdjustor));
}
address adjustor = address(mixedAdjustor);
facets = BurveFacets({
valueFacet: vauleFacet,
valueTokenFacet: valueTokenFacet,
simplexFacet: simplexFacet,
swapFacet: swapFacet,
vaultFacet: vaultFacet,
adjustor: adjustor
});
}
diamond = address(new SimplexDiamond(facets, "ValueToken", "BVT"));
vm.label(diamond, "Diamond");
vm.label(address(tokenA), "Token A");
vm.label(address(tokenB), "Token B");
vm.label(address(vaultA), "Vault A");
vm.label(address(vaultB), "Vault B");
// Add Vertex
{
// TokenA
SimplexFacet(diamond).addVertex(address(tokenA), address(vaultA), VaultType.E4626);
// TokenB
SimplexFacet(diamond).addVertex(address(tokenB), address(vaultB), VaultType.E4626);
}
// Add Closure
{
bool success;
uint16 cid = 3; // 0b11
uint128 startingTarget = 1e12;
uint128 baseFeeX128 = 0;
uint128 protocolTakeX128 = 0;
// uint128 baseFeeX128 = 34028236692093846346337460743176821145; // 10%
// uint128 protocolTakeX128 = 34028236692093846346337460743176821145; //10%
uint256 amountA = startingTarget / 10 ** (18 - tokenA.decimals());
uint256 amountB = startingTarget / 10 ** (18 - tokenB.decimals());
tokenA.mint(owner, amountA);
tokenA.approve(diamond, amountA);
tokenB.mint(owner, amountB);
tokenB.approve(diamond, amountB);
SimplexFacet(diamond).addClosure(cid, startingTarget, baseFeeX128, protocolTakeX128);
}
vm.stopPrank();
}
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);
}
}
contract MockERC4626WithFees is ERC4626Fees {
uint256 entryFeeBasisPoints;
uint256 exitFeeBasisPoints;
address feeRecipient;
constructor(
IERC20 asset_,
string memory name_,
string memory symbol_
) ERC4626(asset_) ERC20(name_, symbol_) {
feeRecipient = msg.sender;
}
function setEntryFee(uint256 entryFeeBasisPoints_) public {
entryFeeBasisPoints = entryFeeBasisPoints_;
}
function setExitFee(uint256 exitFeeBasisPoints_) public {
exitFeeBasisPoints = exitFeeBasisPoints_;
}
function _entryFeeBasisPoints() internal view override returns (uint256) {
return entryFeeBasisPoints;
}
function _exitFeeBasisPoints() internal view override returns (uint256) {
return exitFeeBasisPoints;
}
function _entryFeeRecipient() internal view override returns (address) {
return feeRecipient;
}
function _exitFeeRecipient() internal view override returns (address) {
return feeRecipient;
}
}
Mitigation
It is recommended to revert withdrawal transaction if the underlying vault is temporarily disabled.
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/itos-finance/Burve/pull/72
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the withdrawal function returns early when the vault's maxWithdraw() is zero, so the expected netting of deposit and withdrawal does not occur, causing the contract to deposit its own tokens and later send them to the user.
Impact
The protocol fee is lost.
What the attacker needed
Protocol fee in diamond contract; Underlying vault disables withdrawal
The fix
The withdrawal transaction should revert if the underlying vault is temporarily disabled.
How it was found
- Alice adds value to the protocol.
- Mock vault earnings and disable vault withdrawal by making maxWithdraw return 0.
- Mock protocol fees in the diamond contract.
- Alice calls collectEarnings.
- The protocol fee is lost as tokens are transferred to Alice.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A smart contract holds fees that belong to the protocol. When a user collects their earnings, the contract tries to move funds in and out of a vault, but if the vault temporarily blocks withdrawals, the contract mistakenly uses its own fee tokens to pay the user. This means the protocol loses money.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.