PublicVault.processEpoch updates YIntercept incorrectly when totalAssets() <= expected
The processEpoch function in PublicVault updates the yIntercept variable even when totalAssets() is less than or equal to the expected amount, in which case no funds are sent to the withdraw proxy. This causes the vault's accounting to be incorrect, potentially leading to loss of funds or incorrect share pricing.
Originally disclosed on Code4rena
Submitted by rvierdiiev
When processEpoch is called it calculates amount of withdrawReserve that will be sent to the withdraw proxy. Later it updates yIntercept variable.
https://github.com/code-423n4/2023-01-astaria/blob/main/src/PublicVault.sol#L275-L337
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);
}
The part that we need to investigate is this.
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)
);
In case if totalAssets() > expected then withdrawReserve is totalAssets() - expected multiplied by liquidationWithdrawRatio.
That means that withdrawReserve amount will be sent of public vault to the withdraw proxy, so total assets should decrease by this amount.
In this case call of _setYIntercept below is correct.
However in case when totalAssets() <= expected then withdrawReserve is set to 0, that means that nothing will be sent to the withdraw proxy. But _setYIntercept is still called in this case and total assets is decreased, but should not.
Tools Used
VsCode
Recommended Mitigation Steps
In case when totalAssets() <= expected do not call _setYIntercept.
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
The code unconditionally calls _setYIntercept after the if-else block, even when the else branch sets withdrawReserve to zero and no assets are transferred.
Impact
The vault's yIntercept is incorrectly reduced, leading to inaccurate accounting that could affect share prices and potentially cause loss of funds for users.
What the attacker needed
TotalAssets() <= expected during processEpoch
The fix
Only call _setYIntercept when totalAssets() > expected, i.e., when withdrawReserve is non-zero.
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 contract calculates how much money to send to a withdrawal contract. If there isn't enough money, it sends nothing but still reduces an internal number that tracks the vault's value. This makes the vault think it has less money than it actually does, which can cause problems for users.
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.