Skip to content
Smart contracts Audit finding Intermediate

CTokenOracle.sol#getCErc20Price contains critical math error

The CTokenOracle contract's price calculation for cTokens incorrectly divides by the underlying token's decimals instead of 10 raised to that power, causing cToken prices to be overvalued by many orders of magnitude. An attacker could deposit a single cToken and drain all liquidity pools.

0x52 Follow

Originally disclosed on Sherlock

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

Found by

0x52

Summary

CTokenOracle.sol#getCErc20Price contains a math error that immensely overvalues CTokens

Vulnerability Detail

CTokenOracle.sol#L66-L76

function getCErc20Price(ICToken cToken, address underlying) internal view returns (uint) {
    /*
        cToken Exchange rates are scaled by 10^(18 - 8 + underlying token decimals) so to scale
        the exchange rate to 18 decimals we must multiply it by 1e8 and then divide it by the
        number of decimals in the underlying token. Finally to find the price of the cToken we
        must multiply this value with the current price of the underlying token
    */
    return cToken.exchangeRateStored()
    .mulDivDown(1e8 , IERC20(underlying).decimals())
    .mulWadDown(oracle.getPrice(underlying));
}

In L74, IERC20(underlying).decimals() is not raised to the power of 10. The results in the price of the LP being overvalued by many order of magnitudes. A user could deposit one CToken and drain the reserves of every liquidity pool.

Impact

All lenders could be drained of all their funds due to excessive over valuation of CTokens cause by this error

Code Snippet

CTokenOracle.sol#L66-L76

Tool used

Manual Review

Recommendation

Fix the math error by changing L74:

return cToken.exchangeRateStored()
.mulDivDown(1e8 , 10 ** IERC20(underlying).decimals())
.mulWadDown(oracle.getPrice(underlying));

Sentiment Team

Fixed as recommended. PR here.

Lead Senior Watson

Confirmed fix.

The original report as published on Sherlock, by 0x52. 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

The code divides by the number of decimals instead of 10 raised to that number, causing a massive overvaluation.

Impact

All lenders could be drained of all their funds due to excessive overvaluation of CTokens caused by this error.

What the attacker needed

Ability to deposit cTokens

The fix

The math error was fixed by changing the divisor to 10 raised to the power of the underlying token's 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.

A smart contract used a wrong number in a price calculation, making a token appear far more valuable than it is. An attacker could then use that overvalued token to take more than their fair share from pools, potentially emptying them.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by 0x52

View all

211 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.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.