Large decimal of referenceToken causes overflow at oracle price calculation
The price calculation in V3Oracle.sol overflows when the reference token has 18 decimals and the Chainlink price exceeds a threshold, causing getValue() to revert. This makes the Chainlink price feed unusable, forcing reliance on the TWAP source and reducing pricing security.
JecikPo Follow with linmiaomiao, kfx and 4 others
Originally disclosed on Code4rena
Submitted by JecikPo, also found by linmiaomiao, kfx, KupiaSec, SpicyMeatball, kennedy1030, and t4sk
The price calculation at the V3Oracle.sol will revert upon reaching certain level when referenceToken is used with high decimal value (e.g. 18). The revert (specifically happening when calling getValue()) would make the Chainlink price feed useless; yet the TWAP price source would still be available. The protocol team would have to disable Chainlink and rely exclusively on the TWAP source reducing security of the pricing. The issue could manifest itself after certain amount of time once the project is already live and only when price returned by the feed reaches certain point.
Proof of Concept
The following code line has an issue:
chainlinkPriceX96 = (10 ** referenceTokenDecimals) * chainlinkPriceX96 * Q96 / chainlinkReferencePriceX96
/ (10 ** feedConfig.tokenDecimals);
When referenceTokenDecimals is 18, chainlinkPriceX96 is higher than some threshold between 18 and 19 (in Q96 notation), which will cause arithmetic overflow.
Recommended Mitigation Steps
Instead of calculating the price this way:
chainlinkPriceX96 = (10 ** referenceTokenDecimals) * chainlinkPriceX96 * Q96 / chainlinkReferencePriceX96
/ (10 ** feedConfig.tokenDecimals);
It could be done the following way as per Chainlink’s recommendation:
if (referenceTokenDecimals > feedConfig.tokenDecimals)
chainlinkPriceX96 = (10 ** referenceTokenDecimals - feedConfig.tokenDecimals) * chainlinkPriceX96 * Q96
/ chainlinkReferencePriceX96;
else if (referenceTokenDecimals < feedConfig.tokenDecimals)
chainlinkPriceX96 = chainlinkPriceX96 * Q96 / chainlinkReferencePriceX96
/ (10 ** feedConfig.tokenDecimals - referenceTokenDecimals);
else
chainlinkPriceX96 = chainlinkPriceX96 * Q96 / chainlinkReferencePriceX96;
Reference here.
Assessed type
Decimal
Fixed here.
Status: Mitigation confirmed. Full details in reports from ktg, thank_you and b0g0.
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 multiplication order in the price scaling formula causes an intermediate value to exceed the maximum integer size before division, leading to an arithmetic overflow.
Impact
The Chainlink price feed becomes unusable, forcing the protocol to rely solely on TWAP, which reduces pricing security; the issue may appear only after the project is live and when the feed price reaches a certain level.
What the attacker needed
ReferenceToken has 18 decimals; Chainlink price reaches a threshold
The fix
The calculation should be restructured to scale by the decimal difference before multiplying, as recommended by Chainlink, and the text states it was fixed.
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 smart contract calculates a price by multiplying large numbers. If the numbers get too big, the calculation fails and the function stops working. This means one of the two price sources becomes unusable, leaving only the other source, which is less secure.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.