Precision differences when calculating userCollateralRatioMantissa causes major issues for some token pairs
The userCollateralRatioMantissa calculation divides raw debt and collateral values without normalising for token decimals, so for token pairs with very different precisions the ratio can round to zero. This can make some positions impossible to liquidate, breaking a key protocol invariant.
Source: https://github.com/sherlock-audit/2023-02-surge-judging/issues/122
Found by
0x52, Bauer, GimelSec, TrungOre, __141345__, ast3ros, bin2chen, ctf_sec, gogo, joestakey, peanuts, usmannk
Summary
When calculating userCollateralRatioMantissa in borrow and liquidate. It divides the raw debt value (in loan token precision) by the raw collateral balance (in collateral precision). This skew is fine for a majority of tokens but will cause issues with specific token pairs, including being unable to liquidate a subset of positions no matter what.
Vulnerability Detail
https://github.com/sherlock-audit/2023-02-surge/blob/main/surge-protocol-v1/src/Pool.sol#L474
When calculating userCollateralRatioMantissa, both debt value and collateral values are left in the native precision. As a result of this certain token pairs will be completely broken because of this. Other pairs will only be partially broken and can enter state in which it's impossible to liquidate positions.
Imagine a token pair like USDC and SHIB. USDC has a token precision of 6 and SHIB has 18. If the user has a collateral balance of 100,001 SHIB (100,001e18) and a loan borrow of 1 USDC (1e6) then their userCollateralRatioMantissa will actually calculate as zero:
1e6 * 1e18 / 100,001e18 = 0
There are two issues with this. First is that a majority of these tokens simply won't work. The other issue is that because userCollateralRatioMantissa returns 0 there are states in which some debt is impossible to liquidate breaking a key invariant of the protocol.
Any token with very high or very low precision will suffer from this.
Impact
Some token pairs will always be/will become broken
Code Snippet
https://github.com/sherlock-audit/2023-02-surge/blob/main/surge-protocol-v1/src/Pool.sol#L455-L498
Tool used
Recommendation
userCollateralRatioMantissa should be calculated using debt and collateral values normalized to 18 decimal points
Discussion
xeious
Fixed https://github.com/Surge-fi/surge-protocol-v1/commit/294aa4756fa32c66669e40902ec5c15aa05726e9
We need double checking on this.
IAm0x52
Fix looks good. All occurrences of this precision issue have been addressed.
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 calculation used raw token amounts with different decimal precisions instead of normalising both values to a common precision before dividing.
Impact
Some token pairs could be completely broken, and for others certain positions could become impossible to liquidate.
What the attacker needed
A token pair with very different decimals; a position with a small debt relative to collateral
The fix
The calculation was fixed by normalising debt and collateral values to 18 decimal points before computing the ratio.
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.
Different tokens can have different numbers of decimal places. If you divide one token's raw amount by another's without adjusting for that difference, the result can be zero even when the real ratio is not zero. Here that zero ratio meant the system could not tell that a loan was unsafe, so it could not be liquidated.