Mixed Token Price Will Be Inflated or Deflated
The MixOracle::getThePrice() function uses the wrong token decimals when scaling the price, causing inflated or deflated prices for token pairs with different decimal scales. An attacker could exploit this to cause loss of funds for users or systems relying on the oracle.
Source: https://github.com/sherlock-audit/2024-10-debita-judging/issues/362
Found by
dimulski, jsmi
Summary
The MixOracle::getThePrice() function contains a logical error, causing the calculated price of a token to be incorrectly inflated or deflated. This issue arises when token pairs with differing decimal scales are used, leading to inaccurate pricing data.
Root Cause
- The problem lies in the following function:
function getThePrice(address tokenAddress) public returns (int) {
// get tarotOracle address
address _priceFeed = AttachedTarotOracle[tokenAddress];
require(_priceFeed != address(0), "Price feed not set");
require(!isPaused, "Contract is paused");
ITarotOracle priceFeed = ITarotOracle(_priceFeed);
address uniswapPair = AttachedUniswapPair[tokenAddress];
require(isFeedAvailable[uniswapPair], "Price feed not available");
// get twap price from token1 in token0
(uint224 twapPrice112x112, ) = priceFeed.getResult(uniswapPair);
address attached = AttachedPricedToken[tokenAddress];
// Get the price from the pyth contract, no older than 20 minutes
// get usd price of token0
int attachedTokenPrice = IPyth(debitaPythOracle).getThePrice(attached);
uint decimalsToken1 = ERC20(attached).decimals();
57: uint decimalsToken0 = ERC20(tokenAddress).decimals();
// calculate the amount of attached token that is needed to get 1 token1
int amountOfAttached = int(
61: (((2 ** 112)) * (10 ** decimalsToken1)) / twapPrice112x112
);
// calculate the price of 1 token1 in usd based on the attached token
uint price = (uint(amountOfAttached) * uint(attachedTokenPrice)) /
66: (10 ** decimalsToken1);
require(price > 0, "Invalid price");
return int(uint(price));
}
Here, decimalsToken1 is mistakenly used for scaling instead of decimalsToken0 in line 66. This discrepancy is critical when the token pair has different decimals. Furthermore, the variable decimalsToken0 is defined but not utilized anywhere else in the function, highlighting a clear logical oversight.
Internal pre-conditions
The admin sets token pairs in the MixOracle where the tokens have differing decimals (e.g., USDC with 6 decimals and DAI with 18 decimals).
External pre-conditions
No response
Attack Path
- Assume
MixOracle::getThePrice()is called withtokenAddress = DAI. - In the contract:
decimalsToken0 = 1e18(DAIhas18decimals).decimalsToken1 = 1e6(USDChas6decimals).
- Due to the logical error in line
66, the token price will be inflated by1e12(or deflated in other cases), depending on the tokens in the pair. - This incorrect price propagation may result in:
- Incorrect exchange rates.
- Loss of funds for users or systems relying on this data.
Impact
Mix oracle get the inflated/deflated price, leading to the loss of funds.
PoC
No response
Mitigation
In line 61, replace decimalsToken1 with decimalsToken0.
uint price = (uint(amountOfAttached) * uint(attachedTokenPrice)) /
- (10 ** decimalsToken1);
+ (10 ** decimalsToken0);
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/DebitaFinance/Debita-V3-Contracts/commit/c5573af2b866686390a82eef67817454624ed9c7
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 function uses `decimalsToken1` instead of `decimalsToken0` when scaling the price calculation, leading to incorrect pricing when the two tokens have different decimal places.
Impact
The oracle returns inflated or deflated prices, which could lead to loss of funds for users or systems relying on this data.
What the attacker needed
Admin sets token pairs with different decimals
The fix
The protocol team fixed this issue by replacing `decimalsToken1` with `decimalsToken0` in the price calculation.
How it was found
- Assume MixOracle::getThePrice() is called with tokenAddress = DAI.
- In the contract, decimalsToken0 = 1e18 (DAI has 18 decimals) and decimalsToken1 = 1e6 (USDC has 6 decimals).
- Due to the logical error in line 66, the token price will be inflated by 1e12 (or deflated in other cases), depending on the tokens in the pair.
- This incorrect price propagation may result in incorrect exchange rates and loss of funds for users or systems relying on this data.
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 price oracle is a service that tells a smart contract how much a token is worth. This oracle made a mistake in its maths: it used the number of decimal places of one token when it should have used the other's, so the price it reported was wrong. If other contracts trust this wrong price, people could lose money.