Skip to content
Business logic Audit finding Intermediate

ETHOracle.getLatestPrice needs to convert to 18 decimals

The ETHOracle contract returns the Chainlink ETH/USD price with 8 decimals, but its getPrecision() function claims 18 decimals. This mismatch causes the StrategyLeverage contract to calculate totalCollateralInEth incorrectly, potentially leading to overborrowing.

bin2chen Follow

Originally disclosed on Code4rena

Submitted by bin2chen

In ETHOracle.sol, getPrecision() is defined as 10 ** 18, but the actual oracle used is 8-decimals.

https://data.chain.link/feeds/arbitrum/mainnet/eth-usd

This data feeds’ decimals is 8.

/**
 *  ETH/USD Oracle using chainlink data feeds
 *
 *  For more information about the feed go to
@> *  https://data.chain.link/feeds/arbitrum/mainnet/eth-usd
 *
 **/
contract ETHOracle is IOracle {
    IChainlinkAggregator private immutable _ethPriceFeed;
@>  uint256 private constant _PRECISION = 10 ** 18;
....
    function getLatestPrice() public view override returns (IOracle.Price memory price) {
        (, int256 answer, uint256 startedAt, uint256 updatedAt,) = _ethPriceFeed.latestRoundData();
        if ( answer<= 0 ) revert InvalidPriceFromOracle();
        if ( startedAt ==0 || updatedAt == 0 ) revert InvalidPriceUpdatedAt();

@>      price.price = uint256(answer);  //@audit 8 decimals
        price.lastUpdate = updatedAt;
    }

PythOracle is used to get prices for other tokens, also getPrecision() == 18.

contract PythOracle is IOracle {
...
    uint256 private constant _PRECISION = 18;

    function _getPriceInternal(uint256 age) private view returns (IOracle.Price memory outPrice) {
        PythStructs.Price memory price = age == 0 ?
            _pyth.getPriceUnsafe(_priceID):
            _pyth.getPriceNoOlderThan(_priceID, age);

        if (price.expo >= 0) {
            outPrice.price =
                uint64(price.price) *
                uint256(10 ** (_PRECISION + uint32(price.expo)));
        } else {
            outPrice.price =
                uint64(price.price) *
                uint256(10 ** (_PRECISION - uint32(-price.expo)));
        }
        outPrice.lastUpdate = price.publishTime;
    }

Since a different precision is used, then the calculation of totalCollateralInEth at StrategyLeverage will be wrong.

    function _getPosition(
        uint256 priceMaxAge
    ) internal view returns (uint256 totalCollateralInEth, uint256 totalDebtInEth) {
...
        totalCollateralInEth = 0;
        totalDebtInEth = 0;

        (uint256 collateralBalance,  uint256 debtBalance ) = _getMMPosition();

        if (collateralBalance != 0) {
            IOracle.Price memory ethPrice = priceMaxAge == 0 ?
                _ethUSDOracle.getLatestPrice():
                _ethUSDOracle.getSafeLatestPrice(priceMaxAge);
            IOracle.Price memory collateralPrice = priceMaxAge == 0 ?
                _collateralOracle.getLatestPrice():
                _collateralOracle.getSafeLatestPrice(priceMaxAge);
            if (
                !(priceMaxAge == 0 ||
                    (priceMaxAge > 0 && (ethPrice.lastUpdate >= (block.timestamp - priceMaxAge))) ||
                    (priceMaxAge > 0 &&
                        (collateralPrice.lastUpdate >= (block.timestamp - priceMaxAge))))
            ) {
                revert PriceOutdated();
            }
@>          totalCollateralInEth = (collateralBalance * collateralPrice.price) / ethPrice.price;
        }
        if (debtBalance != 0) {
            totalDebtInEth = debtBalance;
        }
    }

Impact

Incorrect calculation of totalCollateralInEth, affecting borrowing judgment, e.g. overvaluation overborrowing, etc.

Recommended Mitigation

Convert to 18 decimals.

hvasconcelos (BakerFi) confirmed

ickas (BakerFi) commented:

Fixed → https://github.com/baker-fi/bakerfi-contracts/pull/40


The original report as published on Code4rena, by bin2chen. 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

The ETHOracle contract does not convert the 8-decimal Chainlink price to the 18-decimal precision it advertises.

Impact

Incorrect calculation of totalCollateralInEth could lead to overvaluation and overborrowing.

What the attacker needed

None

The fix

The issue was fixed by converting the price to 18 decimals.

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.

Smart contracts often use fixed-point numbers with a set number of decimal places. Here, one price source uses 8 decimals but the code assumes 18, so when it divides by that price, the result is off by a huge factor. This can make the system think a user has more collateral than they really do, letting them borrow too much.

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.

More by bin2chen

View all

139 more disclosed reports, best first.

More from BakerFi Invitational

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.