CompositeMultiOracle returns wrong decimals for prices?
The CompositeMultiOracle contract's price calculation functions may return incorrect prices due to a decimal scaling error. An attacker could potentially exploit this to inflate the value of a token when swapping, leading to incorrect amounts.
Submitted by cmichel
The CompositeMultiOracle.peek/get functions seem to return wrong prices. It’s unclear what decimals source.decimals refers to in this case. Does it refer to source.source token decimals?
It chains the price arguments through _peek function calls and a single price is computed as:
(priceOut, updateTimeOut) = IOracle(source.source).peek(base, quote, 10 ** source.decimals); // Get price for one unit
// @audit shouldn't this divide by 10 ** IOracle(source.source).decimals() instead?
priceOut = priceIn * priceOut / (10 ** source.decimals);
Assume all oracles use 18 decimals (oracle.decimals() returns 18) and source.decimals refers to the token decimals of source.source.
Then going from USDC -> DAI -> USDT (path = [DAI]) starts with a price of 1e18 in peek:
_peek(USDC, DAI, 1e18): Gets the price of1e6 USDC(as USDC has 6 decimals) in DAI with 18 decimals precision (because all oracle precision is set to 18):priceOut = priceIn * 1e18 / 1e6 = 1e18 * 1e18 / 1e6 = 1e30_peek(DAI, USDT, 1e30): Gets the price of1e18 DAI(DAI has 18 decimals) with 18 decimals precision:priceOut = priceIn * 1e18 / 1e18 = priceIn = 1e30
It then uses 1e30 as the price to go from USDC to USDT: value = price * amount / 1e18 = 1e30 * (1.0 USDC) / 1e18 = 1e30 * 1e6 / 1e18 = 1e18 = 1e12 * 1e6 = 1_000_000_000_000.0 USDT. Inflating the actual USDT amount.
The issue is that peek assumes that the final price is in 18 decimals in the value = price * amount / 1e18 division by 1e18. But _peek (and _get) don’t enforce this.
Recommend that _peek should scale the prices to 1e18 by doing:
(priceOut, updateTimeOut) = IOracle(source.source).get(base, quote, 10 ** source.decimals);
// priceOut will have same decimals as priceIn if we divide by oracle decimals
priceOut = priceIn * priceOut / (10 ** IOracle(source.source).decimals());
It does not need to divide by the source.source token precision (source.decimals), but by the oracle precision (IOracle(source.source).decimals()).
It’s confusing to deal with all these decimals, I should at least comment the code better, and try to make it easier to understand.
It’s unclear what decimals source.decimals refers to in this case. Does it refer to source.source token decimals?
CompositeMultiOracle takes IOracle contracts as sources, so
source.decimalsrefers to the token decimals of the oracle, not of the data source one level below.It does not need to divide by the source.source token precision (source.decimals), but by the oracle precision (IOracle(source.source).decimals()).
The source.source token precision would be
IChainlinkAggregatorV3(source.source()).decimals(), the source oracle precision issource.decimals(). CompositeMultiOracle cannot make an assumption on any fields present onsource.source, and must work only with the underlyingsourceIOracles.I’m still not disputing this finding. I need to dig further to make sure the decimals are right when different IOracle sources have different decimals, and I’ve hardcoded a few
1e18in there. Those are code smells.
Sent me into a wild goose chase to support IOracle of multiple decimals as sources to CompositeMultiOracle, only to realize that we create all IOracles and we always create them with 18 decimals, converting from the underlying data source if needed.
Ended up making CompositeMultiOracle require that underlying oracles have 18 decimals. Done.
alcueca (Yield) further patched:
Further refactored all oracles so that decimals are handled properly, and work on taking an amount of base as input, and returning an amount of quote as output. Our oracles don’t have decimals themselves anymore as a state variable, since the return values are in the decimals of quote. This means that CompositeMultiOracle is agnostic with regards to decimals, and doesn’t even need to know about them.
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 code divides by the token's decimal precision instead of the oracle's decimal precision when chaining price feeds, leading to incorrect scaling.
Impact
This could allow an attacker to receive an inflated amount of tokens when swapping, potentially draining funds from the protocol.
What the attacker needed
The protocol uses CompositeMultiOracle with oracles of…; Attacker can call the swap function
The fix
The code was fixed by requiring underlying oracles to have 18 decimals and later refactored to handle decimals properly.
How it was found
- Call peek with base USDC, quote USDT, and path containing DAI.
- The function calls _peek(USDC, DAI, 1e18) which returns a price scaled incorrectly.
- Then calls _peek(DAI, USDT, priceIn) which further scales the price.
- The final price is used in value = price * amount / 1e18, resulting in an inflated USDT amount.
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.
This report is about a smart contract that calculates prices for swapping tokens. The code might use the wrong number of decimal places when combining prices from different sources, which could make the final price wrong. If exploited, someone could get more tokens than they should when swapping.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.