PublicVault.processEpoch calculates withdrawReserve incorrectly; Users can lose funds
The processEpoch function in PublicVault sets the withdraw reserve to zero whenever the vault's total assets are less than or equal to the amount expected from an auction, even when the withdraw ratio is 100%. As a result, withdrawing liquidity providers can receive less than they are owed, and in the example given they could lose all of the vault's funds.
Originally disclosed on Code4rena
Submitted by rvierdiiev
PublicVault.processEpoch calculates withdrawReserve incorrectly. As result user can receive less funds when totalAssets() <= expected from auction.
Proof of Concept
When users wants to withdraw from PublicVault then WithdrawProxy is deployed and PublicVault.processEpoch function is responsible to calculate s.withdrawReserve. This amount depends on how many shares should be redeemed and if there is auction for the epoch.
https://github.com/code-423n4/2023-01-astaria/blob/main/src/PublicVault.sol#L275-L343
solidity
function processEpoch() public {
// check to make sure epoch is over
if (timeToEpochEnd() > 0) {
revert InvalidState(InvalidStates.EPOCH_NOT_OVER);
}
VaultData storage s = _loadStorageSlot();
if (s.withdrawReserve > 0) {
revert InvalidState(InvalidStates.WITHDRAW_RESERVE_NOT_ZERO);
}
WithdrawProxy currentWithdrawProxy = WithdrawProxy(
s.epochData[s.currentEpoch].withdrawProxy
);
// split funds from previous WithdrawProxy with PublicVault if hasn't been already
if (s.currentEpoch != 0) {
WithdrawProxy previousWithdrawProxy = WithdrawProxy(
s.epochData[s.currentEpoch - 1].withdrawProxy
);
if (
address(previousWithdrawProxy) != address(0) &&
previousWithdrawProxy.getFinalAuctionEnd() != 0
) {
previousWithdrawProxy.claim();
}
}
if (s.epochData[s.currentEpoch].liensOpenForEpoch > 0) {
revert InvalidState(InvalidStates.LIENS_OPEN_FOR_EPOCH_NOT_ZERO);
}
// reset liquidationWithdrawRatio to prepare for re calcualtion
s.liquidationWithdrawRatio = 0;
// check if there are LPs withdrawing this epoch
if ((address(currentWithdrawProxy) != address(0))) {
uint256 proxySupply = currentWithdrawProxy.totalSupply();
s.liquidationWithdrawRatio = proxySupply
.mulDivDown(1e18, totalSupply())
.safeCastTo88();
currentWithdrawProxy.setWithdrawRatio(s.liquidationWithdrawRatio);
uint256 expected = currentWithdrawProxy.getExpected();
unchecked {
if (totalAssets() > expected) {
s.withdrawReserve = (totalAssets() - expected)
.mulWadDown(s.liquidationWithdrawRatio)
.safeCastTo88();
} else {
s.withdrawReserve = 0;
}
}
_setYIntercept(
s,
s.yIntercept -
totalAssets().mulDivDown(s.liquidationWithdrawRatio, 1e18)
);
// burn the tokens of the LPs withdrawing
_burn(address(this), proxySupply);
}
// increment epoch
unchecked {
s.currentEpoch++;
}
}
s.liquidationWithdrawRatio depends on how many shares exists inside WithdrawProxy. In case if amount of shares inside WithdrawProxy equal to amount of shares inside PublicVault that means that withdraw ratio is 100% and all funds from Vault should be sent to WithdrawProxy.
In case if auction is in progress then WithdrawProxy.getExpected is not 0 and some amount of funds is expected from auction.
unchecked {
if (totalAssets() > expected) {
s.withdrawReserve = (totalAssets() - expected)
.mulWadDown(s.liquidationWithdrawRatio)
.safeCastTo88();
} else {
s.withdrawReserve = 0;
}
}
This is s.withdrawReserve calculation. As you can see in case if totalAssets() <= expected then s.withdrawReserve is set to 0 and no funds will be sent to proxy. This is incorrect though.
For example in the case when withdraw ratio is 100% all funds should be sent to the withdraw proxy, but because of that check, some part of funds will be still inside the vault and depositors will lose their funds. If for example totalAssets is 5eth and expected is 5 eth, then depositors will lose all 5 eth.
This check is done in such way, because of calculations inside WithdrawProxy. But it’s not correct.
Tools Used
VsCode
Recommended Mitigation Steps
You need to check this logic again. Maybe you need to always send s.withdrawReserve = totalAssets().mulWadDown(s.liquidationWithdrawRatio).safeCastTo88() amount to the withdraw proxy. But then you need to rethink, how WithdrawProxy will handle yIntercept increase/decrease.
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 withdraw reserve is only computed from the surplus above the auction's expected amount, so when total assets do not exceed that expectation the reserve is forced to zero instead of being scaled by the withdraw ratio.
Impact
Withdrawing users could receive less than their share, and in the example where total assets equal the expected auction amount they could lose the entire balance.
What the attacker needed
An epoch with LPs withdrawing; an auction in progress for the epoch
The fix
The logic should be reworked so the reserve is always the total assets scaled by the withdraw ratio, with the interaction with the withdraw proxy's y-intercept handling reconsidered.
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 vault holds money pooled from many users, and when someone wants to take their share out, the contract works out how much of the vault's balance should be moved to a separate withdrawal contract. The bug is that this calculation gives zero whenever the vault's current balance is not larger than the amount still expected from an auction, so the money stays stuck in the vault instead of reaching the person withdrawing. In the worst case described, a user withdrawing everything could get nothing back.
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.