Skip to content
Business logic Audit finding Intermediate

Tokens with decimals larger than 18 are not supported

The code assumes all tokens have 18 decimals, so tokens with more than 18 decimals cause underflow and reverts in price and swap functions. An attacker could not directly exploit this, but it breaks functionality for such tokens.

WatchPug Follow

Originally disclosed on Code4rena

Submitted by WatchPug, also found by 0x1f8b

For tokens with decimals larger than 18, many functions across the codebase will revert due to underflow.

ConnextPriceOracle.sol#L99-L115

function getPriceFromDex(address _tokenAddress) public view returns (uint256) {
    PriceInfo storage priceInfo = priceRecords[_tokenAddress];
    if (priceInfo.active) {
      uint256 rawTokenAmount = IERC20Extended(priceInfo.token).balanceOf(priceInfo.lpToken);
      uint256 tokenDecimalDelta = 18 - uint256(IERC20Extended(priceInfo.token).decimals());
      uint256 tokenAmount = rawTokenAmount.mul(10**tokenDecimalDelta);
      uint256 rawBaseTokenAmount = IERC20Extended(priceInfo.baseToken).balanceOf(priceInfo.lpToken);
      uint256 baseTokenDecimalDelta = 18 - uint256(IERC20Extended(priceInfo.baseToken).decimals());
      uint256 baseTokenAmount = rawBaseTokenAmount.mul(10**baseTokenDecimalDelta);
      uint256 baseTokenPrice = getTokenPrice(priceInfo.baseToken);
      uint256 tokenPrice = baseTokenPrice.mul(baseTokenAmount).div(tokenAmount);

      return tokenPrice;
    } else {
      return 0;
    }
  }

StableSwapFacet.sol#L426

precisionMultipliers[i] = 10**uint256(SwapUtils.POOL_PRECISION_DECIMALS - decimals[i]);

Chainlink feeds’ with decimals > 18 are not supported neither:

ConnextPriceOracle.sol#L122-L140

function getPriceFromChainlink(address _tokenAddress) public view returns (uint256) {
    AggregatorV3Interface aggregator = aggregators[_tokenAddress];
    if (address(aggregator) != address(0)) {
      (, int256 answer, , , ) = aggregator.latestRoundData();

      // It's fine for price to be 0. We have two price feeds.
      if (answer == 0) {
        return 0;
      }

      // Extend the decimals to 1e18.
      uint256 retVal = uint256(answer);
      uint256 price = retVal.mul(10**(18 - uint256(aggregator.decimals())));

      return price;
    }

    return 0;
  }

Recommended Mitigation Steps

Consider checking if decimals > 18 and normalize the value by div the decimals difference.

ecmendenhall (Connext) commented:

I gave this a along with issue #61 because these findings both identified an additional location in the StableSwap contract where the 18 decimal assumption is hardcoded.

jakekidd (Connext) confirmed and commented:

I gave this a ❤️ along with #61 because these findings both identified an additional location in the StableSwap contract where the 18 decimal assumption is hardcoded.

Marking as confirmed (and leaving issue open) for this reason. Would be great to merge both findings into 1 issue in the finalized audit.

jakekidd (Connext) commented:

Assortment of findings across these three issues: https://github.com/code-423n4/2022-06-connext-findings/issues/39 https://github.com/code-423n4/2022-06-connext-findings/issues/61 https://github.com/code-423n4/2022-06-connext-findings/issues/204

jakekidd (Connext) resolved:

Fixed by connext/nxtp@f2e5b66

0xleastwood (judge) commented:

Marking this as the primary issue because it highlights an active part of the codebase while other issues do not. initializeSwap will not be compatible with any token with decimals greater than 18.


The original report as published on Code4rena, by WatchPug. 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 code hardcodes 18 as the expected number of decimals and subtracts the token's actual decimals from it without checking if the token has more than 18 decimals.

Impact

Functions like getPriceFromDex, getPriceFromChainlink, and initializeSwap will revert for tokens with more than 18 decimals, making them unusable in those parts of the protocol.

What the attacker needed

Token with >18 decimals

The fix

The code was fixed by checking if decimals > 18 and normalising the value by dividing the decimals difference.

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.

Some tokens have more than 18 decimal places. The code subtracts the token's decimals from 18, which becomes negative and causes an error (underflow) that stops the function. This means those tokens cannot be used in the affected functions.

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 WatchPug

View all

197 more disclosed reports, best first.

More from Connext Amarok contest

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.