Skip to content
Business logic Audit finding Intermediate

`ChainlinkOracle.sol#getPrice()` The price will be wrong when the token's USD price feed's `decimals != 8`

The ChainlinkOracle contract assumed that all token USD price feeds have 8 decimals, but some feeds (like AMPL/USD) have 18 decimals. This caused getPrice() to return an incorrect price, allowing an attacker to deposit a small amount and drain funds.

Lambda Follow with csanuragjain, CRYP70 and 6 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2022-08-sentiment-judging/tree/main/019-H

Found by

Lambda, csanuragjain, CRYP70, WATCHPUG, berndartmueller, pashov, IllIllI, Bahurum, sorrynotsorry

Summary

ChainlinkOracle assumes and inexplicitly requires the token's USD feed's decimals to be 8. However, there are certain token's USD feed has a different decimals.

Vulnerability Detail

In the current implementation, it assumes $tokenFeedDecimals = ethFeedDecimals$ (feed[token].decimals() must equals ethUsdPriceFeed.decimals() = 8).

However, there are tokens with USD price feed's decimals != 8 (E.g: AMPL / USD feed decimals = 18)

When the token's USD feed's decimals != 8, ChainlinkOracle.sol#getPrice() will return an incorrect price in ETH.

The correct calculation formula should be:

$$ \frac{ answer_{token} \cdot 10^{ethFeedDecimals} }{ answer_{eth} \cdot 10^{tokenFeedDecimals} } \cdot 10^{18} $$

PoC

Given:

  • 1.0 AMPL worth 1.14 USD, feed[ampl].decimals() == 18, answer_ampl = 1140608758261546000 Source: feed[ampl]

  • 1.0 ETH worth 1588.11 USD, ethUsdPriceFeed.decimals() == 8, answer_eth = 158811562094 Source: ethUsdPriceFeed

chainlinkOracle.getPrice(AMPL) will return ~7.18m (eth):

$$ \frac{ answer_{token} \cdot 10^{18} }{ answer_{eth} } = \frac{ 1140608758261546000 \cdot 10^{18} }{ 158811562094 } = 7182151873718260663354494 $$

Impact

When the price feed with decimals != 18 is set, the attacker can deposit a small amount of the asset and drain all the funds from the protocol.

Code Snippet

https://github.com/sentimentxyz/protocol/blob/4e45871e4540df0f189f6c89deb8d34f24930120/src/core/RiskEngine.sol#L178-L188

Solidity
function _valueInWei(address token, uint amt)
    internal
    view
    returns (uint)
{
    return oracle.getPrice(token)
    .mulDivDown(
        amt,
        10 ** ((token == address(0)) ? 18 : IERC20(token).decimals())
    );
}

https://github.com/sentimentxyz/oracle/blob/59b26a3d8c295208437aad36c470386c9729a4bc/src/chainlink/ChainlinkOracle.sol#L47-L59

Solidity
/// @inheritdoc IOracle
/// @dev feed[token].latestRoundData should return price scaled by 8 decimals
function getPrice(address token) external view virtual returns (uint) {
    (, int answer,,,) =
        feed[token].latestRoundData();

    if (answer < 0)
        revert Errors.NegativePrice(token, address(feed[token]));

    return (
        (uint(answer)*1e18)/getEthPrice()
    );
}

https://github.com/sentimentxyz/oracle/blob/59b26a3d8c295208437aad36c470386c9729a4bc/src/chainlink/ChainlinkOracle.sol#L65-L73

Solidity
function getEthPrice() internal view returns (uint) {
    (, int answer,,,) =
        ethUsdPriceFeed.latestRoundData();

    if (answer < 0)
        revert Errors.NegativePrice(address(0), address(ethUsdPriceFeed));

    return uint(answer);
}

Tool used

Manual Review

Recommendation

Consider adding a check for feed.decimals() to make sure feed's decimals = 8:

Solidity
constructor(AggregatorV3Interface _ethUsdPriceFeed) Ownable(msg.sender) {
    require(_ethUsdPriceFeed.decimals() == 8, "...");
    ethUsdPriceFeed = _ethUsdPriceFeed;
}
Solidity
function setFeed(
    address token,
    AggregatorV3Interface _feed
) external adminOnly {
    require(_feed.decimals() == 8, "...");
    feed[token] = _feed;
    emit UpdateFeed(token, address(_feed));
}

Sentiment Team

Our chainlink oracles use token/usd feed for all the assets we support via chainlink, all the token/usd chainlink feeds have 8 decimals and hence will not lead to any issue with the final price calculation. PR here.

Lead Senior Watson

Confirmed fix.

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

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 contract did not normalise the token feed's decimals against the ETH feed's decimals, assuming both were 8.

Impact

When a feed with decimals other than 8 is used, an attacker could deposit a small amount and drain all protocol funds.

What the attacker needed

A token with a USD feed whose decimals != 8

The fix

The team added a check that the feed's decimals equal 8, and the fix was confirmed.

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 tells a smart contract how much a token is worth. This contract assumed every token's price feed used the same number of decimal places (8), but some use more. That made the calculated price wildly wrong, so an attacker could exploit it to steal funds.

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 Lambda

View all

64 more disclosed reports, best first.

More from Sentiment Update #3

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.