`ratiosX96Value` rounds in favor of user and not vault
The vault's `ratiosX96Value` is rounded down when calculating withdrawal amounts, which increases the coefficient used to determine user payouts. This causes users to receive slightly more than they should, gradually depleting the vault and potentially leading to insolvency.
IronsideSec Follow with X12, eeyore and 1 other
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-06-mellow-judging/issues/61
Found by
Ironsidesec, X12, eeyore, hash
Summary
ratiosX96Value is rounded down instead of up, causing withdrawals to favor users. This can slowly decrease the value in our vault, potentially leading to insolvency.
Vulnerability Detail
ratiosX96Value, calculated in calculateStack,
s.ratiosX96Value += FullMath.mulDiv(s.ratiosX96[i], priceX96, Q96);
is used as a denominator inside analyzeRequest to calculate coefficientX96 and the user's expectedAmounts.
uint256 value = FullMath.mulDiv(lpAmount, s.totalValue, s.totalSupply);
value = FullMath.mulDiv(value, D9 - s.feeD9, D9);
uint256 coefficientX96 = FullMath.mulDiv(value, Q96, s.ratiosX96Value);
...
expectedAmounts[i] = ratiosX96 == 0 ? 0 : FullMath.mulDiv(coefficientX96, ratiosX96, Q96);
However, calculateStack rounds the denominator down (thanks to mulDiv) , which increases coefficientX96 and thus increases what users withdraw.
This is unwanted behavior in vaults. Rounding towards users decreases the vault's value and can ultimately cause insolvency. The previous audit found a similar issue in the deposit function - M1.
Impact
The vault may become insolvent or lose small amounts of funds with each withdrawal.
Code Snippet
for (uint256 i = 0; i < tokens.length; i++) {
uint256 priceX96 = priceOracle.priceX96(address(this), tokens[i]);
s.totalValue += FullMath.mulDiv(amounts[i], priceX96, Q96);
s.ratiosX96Value += FullMath.mulDiv(s.ratiosX96[i], priceX96, Q96);
s.erc20Balances[i] = IERC20(tokens[i]).balanceOf(address(this));
}
Tool used
Manual Review
Recommendation
Round the value up instead of down, similar to how it's done inside deposit.
for (uint256 i = 0; i < tokens.length; i++) {
uint256 priceX96 = priceOracle.priceX96(address(this), tokens[i]);
s.totalValue += FullMath.mulDiv(amounts[i], priceX96, Q96);
- s.ratiosX96Value += FullMath.mulDiv(s.ratiosX96[i], priceX96, Q96);
+ s.ratiosX96Value += FullMath.mulDivRoundingUp(s.ratiosX96[i], priceX96, Q96);
s.erc20Balances[i] = IERC20(tokens[i]).balanceOf(address(this));
}
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/mellow-finance/mellow-lrt/pull/44
10xhash
Fixed Now rounded up
sherlock-admin2
The Lead Senior Watson signed off on the fix.
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
Impact
The vault may become insolvent or lose small amounts of funds with each withdrawal.
What the attacker needed
None
The fix
The issue was fixed by rounding the value up instead of down, as confirmed in the discussion.
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.
When you withdraw from a vault, the amount you get is based on a calculation that includes a division. If that division rounds down, the result is slightly larger for you, meaning you get a tiny bit more than you should. Over many withdrawals, this can drain the vault's funds and make it unable to pay everyone.