Oracle assumes token and feed decimals will be limited to 18 decimals
The Oracle contract's price normalisation assumes that the sum of feed and token decimals will not exceed 36, so if it does the calculation underflows and reverts, making the oracle unusable for such tokens.
adriro Follow with pashov, sorrynotsorry and 12 others
Originally disclosed on Code4rena
Submitted by adriro, also found by pashov, sorrynotsorry, neumo, Chom, CertoraInc, Ruhum, eierina, Lambda, RaoulSchaffranek, cryptphi, codexploder, BClabs, 8olidity, and joestakey
https://github.com/code-423n4/2022-10-inverse/blob/main/src/Oracle.sol#L87 https://github.com/code-423n4/2022-10-inverse/blob/main/src/Oracle.sol#L121
The Oracle contract normalizes prices in both viewPrices and getPrices functions to adjust for potential decimal differences between feed and token decimals and the expected return value.
However these functions assume that feedDecimals and tokenDecimals won’t exceed 18 since the normalization calculation is 36 - feedDecimals - tokenDecimals, or that at worst case the sum of both won’t exceed 36.
This assumption should be safe for certain cases, for example WETH is 18 decimals and the ETH/USD chainlink is 8 decimals, but may cause an overflow (and a revert) for the general case, rendering the Oracle useless in these cases.
Proof of Concept
If feedDecimals + tokenDecimals > 36 then the expression 36 - feedDecimals - tokenDecimals will be negative and (due to Solidity 0.8 default checked math) will cause a revert.
Recommended Mitigation Steps
In case feedDecimals + tokenDecimals exceeds 36, then the proper normalization procedure would be to divide the price by 10 ** decimals. Something like this:
uint normalizedPrice;
if (feedDecimals + tokenDecimals > 36) {
uint decimals = feedDecimals + tokenDecimals - 36;
normalizedPrice = price / (10 ** decimals)
} else {
uint8 decimals = 36 - feedDecimals - tokenDecimals;
normalizedPrice = price * (10 ** decimals);
}
08xmt (Inverse) confirmed and commented:
Fixed in https://github.com/InverseFinance/FrontierV2/pull/25 Also pretty sure this is a dupe
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 normalisation formula assumes the sum of feed and token decimals is at most 36, without handling the case where it is larger.
Impact
For tokens where the sum of feed and token decimals exceeds 36, the oracle reverts and becomes useless, potentially blocking any functionality that relies on it.
What the attacker needed
Token and feed decimals sum over 36
The fix
The code was fixed to divide the price when the sum of decimals exceeds 36, as confirmed by the project.
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.
Smart contracts often need to adjust numbers to a common scale, called decimals. This code subtracts two decimal counts from 36, but if those counts add up to more than 36 the result goes negative, and the programming language stops with an error. That means the price feed cannot be read at all for tokens with many decimals.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.