Incorrect handling of pricefeed.decimals()
The oracle contract incorrectly scales price feed values based on the feed's decimals, assuming 8 decimals. For feeds with other decimal counts, the returned price is wrong by orders of magnitude or zero, which could break depeg detection and market creation.
Originally disclosed on Code4rena
Submitted by carrotsmuggler, also found by 0x52, 0xDecorativePineapple, 0xPanas, auditor0517, Bahurum, durianSausage, hyh, Jeiwan, ladboy233, Lambda, pauliax, PwnPatrol, R2, Respx, scaraven, teawaterwire, and zzzitron
Impact
Wrong math for handling pricefeed decimals. This code will only work for pricefeeds of 8 decimals, any others give wrong/incorrect data. The maths used can be shown in three lines:
nowPrice = (price1 * 10000) / price2;
nowPrice = nowPrice * int256(10**(18 - priceFeed1.decimals()));
return nowPrice / 1000000;
Line1: adds 4 decimals Line2: adds (18 - d) decimals, (where d = pricefeed.decimals()) Line3: removes 6 decimals
Total: adds (16 - d) decimals
when d=8, the contract correctly returns an 8 decimal number. However, when d = 6, the function will return a 10 decimal number. This is further raised by (18-d = 12) decimals when checking for depeg event, leading to a 22 decimal number which is 4 orders of magnitude incorrect.
if d=18, (like usd-eth pricefeeds) contract fails / returns 0.
All chainlink contracts which give price in eth, operate with 18 decimals. So this can cripple the system if added later.
Proof of Concept
Running the test AssertTest.t.sol:testPegOracleMarketCreation and changing the line on
to
PegOracle pegOracle3 = new PegOracle(
0xB1552C5e96B312d0Bf8b554186F846C40614a540, //usd-eth contract address
btcEthOracle
);
gives this output
oracle3price1: 1085903802394919427
oracle3price2: 13753840915281064000
oracle3price1 / oracle3price2: 0
returning an oracle value of 0. Simulating with a mock price feed of 6 decimals gives results 4 orders of magnitude off.
Tools Used
Foundry, VS-Code
Recommended Mitigation Steps
Since only the price ratio is calculated, there is no point in increasing the decimal by (18-d) in the second line. Proposed solution:
nowPrice = (price1 * 10000) / price2;
nowPrice = nowPrice * int256(10**(priceFeed1.decimals())) * 100;
return nowPrice / 1000000;
This returns results in d decimals, no matter the value of d.
MiguelBits (Y2K Finance) confirmed
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 scaling formula hardcodes an assumption of 8 decimals instead of using the feed's actual decimals to normalise the result.
Impact
For feeds with 6 decimals the result is four orders of magnitude off, and for 18-decimal feeds the contract returns zero, which could cripple the system if such feeds are added.
What the attacker needed
A price feed with decimals other than 8
The fix
The scaling should use the feed's actual decimals to normalise the result, for example by multiplying by 10^decimals and 100 before dividing by 1,000,000, so the output is always in the feed's decimals.
How it was found
- Open the test file AssertTest.t.sol and locate the line that creates a PegOracle instance.
- Replace the first price feed address with the usd-eth Chainlink feed address.
- Run the test testPegOracleMarketCreation.
- Observe that the oracle returns a price of 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.
Price feeds report prices as whole numbers with a fixed number of decimal places. The code assumed every feed uses 8 decimals, so when a feed used a different number, the calculated price was off by a huge factor or became zero. This could make the system think a stablecoin had depegged when it had not, or stop markets from being created.