Skip to content
Crypto Audit finding Advanced

Value of asset token can be incorrect when usage of ETH/USD Chainlink oracle is needed

When a token lacks a direct token/USD Chainlink oracle, the protocol can route its value through token/ETH and ETH/USD oracles. The code then returns the value in ETH's 18 decimals instead of USD's 8 decimals, making the token appear about 10^10 times more valuable than it should.

rbserver Follow with SpicyMeatball

Originally disclosed on Code4rena

Submitted by rbserver, also found by SpicyMeatball

Impact

There are tokens that have token/ETH but no token/USD Chainlink oracles currently in which these tokens have the in scope token behaviors described in https://code4rena.com/audits/2024-04-noya and should be supported by this protocol. To support these tokens, the ETH/USD Chainlink oracle can be used as a part of the route set by the following NoyaValueOracle.updatePriceRoute function for converting the token amount to ETH first and then converting the converted ETH amount to USD.

https://github.com/code-423n4/2024-04-noya/blob/cc3854f634a72bd4a8b597021887088ca2d6d29f/contracts/helpers/valueOracle/NoyaValueOracle.sol#L61-L64

    function updatePriceRoute(address asset, address base, address[] calldata s) external onlyMaintainer {
        priceRoutes[asset][base] = s;
        emit UpdatedPriceRoute(asset, base, s);
    }

When converting the value of such token in ETH to USD, the following ChainlinkOracleConnector.getValue function would return getValueFromChainlinkFeed(AggregatorV3Interface(primarySource), amount, getTokenDecimals(decimalsSource), isPrimaryInverse); because amountIn is in ETH’s decimals that is 18 and uintprice and sourceTokenUnit are in USD’s decimals that is 8 in the getValueFromChainlinkFeed function below, the value returned by the ChainlinkOracleConnector.getValue function is in ETH’s decimals. Hence, when converting such token amount to ETH first and then to USD, the value of the corresponding token amount is in ETH’s decimals instead of USD’s decimals. In comparison, if the token/USD oracle could exist and be used for such token, the value of such token returned by ChainlinkOracleConnector.getValue function would be in USD’s decimals instead of ETH’s decimals. Thus, the value of such token is much higher when using the token/ETH and ETH/USD oracles indirectly comparing to using the token/USD oracle directly given if such token/USD oracle can become existent in the future. If such token/USD oracle does become existent and be used in the ChainlinkOracleConnector contract in the future, the value of such token amount calculated previously using the token/ETH and ETH/USD oracles indirectly would be incorrectly much higher than the value of the same token amount newly calculated using the token/USD oracle directly.

https://github.com/code-423n4/2024-04-noya/blob/cc3854f634a72bd4a8b597021887088ca2d6d29f/contracts/helpers/valueOracle/oracles/ChainlinkOracleConnector.sol#L89-L103

    function getValue(address asset, address baseToken, uint256 amount) public view returns (uint256) {
        if (asset == baseToken) {
            return amount;
        }

        (address primarySource, bool isPrimaryInverse) = getSourceOfAsset(asset, baseToken);
        if (primarySource == address(0)) {
            revert NoyaChainlinkOracle_PRICE_ORACLE_UNAVAILABLE(asset, baseToken, primarySource);
        }
        address decimalsSource = isPrimaryInverse ? baseToken : asset;
        decimalsSource = decimalsSource == ETH || decimalsSource == USD ? primarySource : decimalsSource;
        return getValueFromChainlinkFeed(
            AggregatorV3Interface(primarySource), amount, getTokenDecimals(decimalsSource), isPrimaryInverse
        );
    }

https://github.com/code-423n4/2024-04-noya/blob/cc3854f634a72bd4a8b597021887088ca2d6d29f/contracts/helpers/valueOracle/oracles/ChainlinkOracleConnector.sol#L138-L141

    function getTokenDecimals(address token) public view returns (uint256) {
        uint256 decimals = IERC20Metadata(token).decimals();
        return 10 ** decimals;
    }

https://github.com/code-423n4/2024-04-noya/blob/cc3854f634a72bd4a8b597021887088ca2d6d29f/contracts/helpers/valueOracle/oracles/ChainlinkOracleConnector.sol#L115-L135

    function getValueFromChainlinkFeed(
        AggregatorV3Interface source,
        uint256 amountIn,
        uint256 sourceTokenUnit,
        bool isInverse
    ) public view returns (uint256) {
        int256 price;
        uint256 updatedAt;
        (, price,, updatedAt,) = source.latestRoundData();
        uint256 uintprice = uint256(price);
        if (block.timestamp - updatedAt > chainlinkPriceAgeThreshold) {
            revert NoyaChainlinkOracle_DATA_OUT_OF_DATE();
        }
        if (price <= 0) {
            revert NoyaChainlinkOracle_PRICE_ORACLE_UNAVAILABLE(address(source), address(0), address(0));
        }
        if (isInverse) {
            return (amountIn * sourceTokenUnit) / uintprice;
        }
        return (amountIn * uintprice) / (sourceTokenUnit);
    }

Proof of Concept

Please add the following test in testFoundry\testOracle.sol. This test will pass to demonstrate the described scenario.

    function test_assetTokenValueIsIncorrectWhenETHUSDChainlinkOracleIsNeeded() public {
        // Some tokens do not have token/USD oracle so token/ETH and ETH/USD oracles need to be used.
        // Following code compares method using token/ETH and ETH/USD oracles indirectly to method using token/USD oracle directly.

        address ETH_USD_FEED = 0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419;

        vm.startPrank(owner);

        addTokenToChainlinkOracle(address(USDC), address(840), address(USDC_USD_FEED));
        addTokenToChainlinkOracle(address(USDC), address(0), address(USDC_ETH_FEED));
        addTokenToChainlinkOracle(address(0), address(840), address(ETH_USD_FEED));

        addTokenToNoyaOracle(address(USDC), address(chainlinkOracle));
        addTokenToNoyaOracle(address(0), address(chainlinkOracle));
        addTokenToNoyaOracle(address(840), address(chainlinkOracle));

        uint256 valueDirect = noyaOracle.getValue(address(USDC), address(840), 1e6);

        // when using USDC/USD oracle directly, 1e6 wei USDC is equivalent to 99998495 wei USD, which is in USD's decimals that is 8
        assertEq(valueDirect, 99998495);

        address[] memory assets = new address[](1);
        assets[0] = address(0);
        noyaOracle.updatePriceRoute(address(USDC), address(840), assets);

        uint256 valueIndirect = noyaOracle.getValue(address(USDC), address(840), 1e6);

        // when using USDC/ETH and ETH/USD oracles indirectly, 1e6 wei USDC is equivalent to 998152930103816659 wei USD, which is in ETH's decimals that is 18
        assertEq(valueIndirect, 998152930103816659);

        // value of 1e6 wei USDC is incorrectly much higher when using USDC/ETH and ETH/USD oracles indirectly comparing to using USDC/USD oracle directly
        assertEq(valueIndirect / valueDirect, 9981679525);

        vm.stopPrank();
    }

Recommended Mitigation Steps

getValueFromChainlinkFeed(AggregatorV3Interface(primarySource), amount, getTokenDecimals(decimalsSource), isPrimaryInverse) returned by the ChainlinkOracleConnector.getValue function can be further divided by 10 ** 10 when asset is ETH, baseToken is USD, and isPrimaryInverse is false. This makes such return value be in USD’s decimals that is 8.

gzeon (judge) increased severity to High and commented:

I think I see the problem, when decimalsSource is ETH/USD it treats the decimal as source.decimal() because they are not real ERC20. It is usually fine because ETH oracles are 18 decimals and USD oracles are 8 decimals which is same as the expected value. However, when ETH/USD oracle is used the logic would always consider it as 8 decimal (because ETH/USD oracle as 8 decimal) but in fact it should be treated as 18 decimal if we want the ETH decimal, hence the result is off by 10 decimals.

HadiEsna (NOYA) confirmed


The original report as published on Code4rena, by rbserver. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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 decimal scaling logic treats the ETH/USD oracle's own 8 decimals as the source token's decimals, so the intermediate ETH amount is not rescaled to USD's 8 decimals.

Impact

The value of a token amount calculated through the indirect route is incorrectly much higher than the value from a direct token/USD oracle, which could misstate asset values used by the protocol.

What the attacker needed

A token with token/ETH but no token/USD oracle; a price route using ETH/USD oracle; maintainer sets the price route

The fix

The return value should be further divided by 10^10 when the asset is ETH, the base token is USD, and the oracle is not inverted, so the result is in USD's 8 decimals.

How it was found

  1. Configure the oracle with a direct token/USD feed and record the value for a token amount.
  2. Configure a price route that converts the token to ETH and then ETH to USD.
  3. Read the value for the same token amount through the indirect route.
  4. Compare the two values and observe the indirect result is about 10^10 times larger.

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 protocol can chain two oracles together, first converting a token to ETH and then ETH to USD. The bug is that the final number keeps the wrong number of decimal places, so the USD value comes out far too large.

What is Crypto?

Encryption, signatures, randomness or certificate checks that do not protect what they claim to.

More by rbserver

View all

51 more disclosed reports, best first.

More from NOYA

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Cryptographic weaknesses

The lesson behind this report.

Encryption, signatures, randomness or certificate checks that do not protect what they claim to.