Skip to content
Audit finding Intermediate

CTokenV3Collateral._underlyingRefPerTok should use the decimals from underlying Comet

The function _underlyingRefPerTok used the decimals of the wrapper token instead of the underlying Comet token when converting the exchange rate, leading to an incorrect reference price. This could cause the collateral's value to be miscomputed, potentially affecting the protocol's accounting.

sces60107 Follow

Originally disclosed on Code4rena

Submitted by sces60107

https://github.com/reserve-protocol/protocol/blob/9ee60f142f9f5c1fe8bc50eef915cf33124a534f/contracts/plugins/assets/compoundv3/CTokenV3Collateral.sol#L56 https://github.com/reserve-protocol/protocol/blob/9ee60f142f9f5c1fe8bc50eef915cf33124a534f/contracts/plugins/assets/compoundv3/CusdcV3Wrapper.sol#L46 https://github.com/reserve-protocol/protocol/blob/9ee60f142f9f5c1fe8bc50eef915cf33124a534f/contracts/plugins/assets/compoundv3/CusdcV3Wrapper.sol#L236

CTokenV3Collateral._underlyingRefPerTok uses erc20Decimals which is the decimals of CusdcV3Wrapper. But it should use the decimals of the underlying Comet.

Proof of Concept

CTokenV3Collateral.\_underlyingRefPerTokcomputes the actual quantity of whole reference units per whole collateral tokens. And it passeserc20Decimalstoshiftl_toFix. https://github.com/reserve-protocol/protocol/blob/9ee60f142f9f5c1fe8bc50eef915cf33124a534f/contracts/plugins/assets/compoundv3/CTokenV3Collateral.sol#L56

    function _underlyingRefPerTok() internal view virtual override returns (uint192) {
        return shiftl_toFix(ICusdcV3Wrapper(address(erc20)).exchangeRate(), -int8(erc20Decimals));
    }

However, the correct decimals should be the decimals of underlying Comet since it is used in CusdcV3Wrapper.exchangeRate.

https://github.com/reserve-protocol/protocol/blob/9ee60f142f9f5c1fe8bc50eef915cf33124a534f/contracts/plugins/assets/compoundv3/CusdcV3Wrapper.sol#L236

    function exchangeRate() public view returns (uint256) {
        (uint64 baseSupplyIndex, ) = getUpdatedSupplyIndicies();
        return presentValueSupply(baseSupplyIndex, safe104(10**underlyingComet.decimals()));
    }

Recommended Mitigation Steps

    function _underlyingRefPerTok() internal view virtual override returns (uint192) {
-       return shiftl_toFix(ICusdcV3Wrapper(address(erc20)).exchangeRate(), -int8(erc20Decimals));
+       return shiftl_toFix(ICusdcV3Wrapper(address(erc20)).exchangeRate(), -int8(comet.decimals()));
    }

Assessed type

Decimal

tbrent (Reserve) confirmed

pmckelvy1 (Reserve) commented:

https://github.com/reserve-protocol/protocol/pull/889

Reserve Mitigated:

Use decimals from underlying Comet. PR: https://github.com/reserve-protocol/protocol/pull/889

Status: Mitigation confirmed. Full details in reports from RaymondFam, ronnyx2017 and bin2chen.


The original report as published on Code4rena, by sces60107. 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 used the wrapper token's decimals instead of the underlying Comet token's decimals when scaling the exchange rate.

Impact

The incorrect scaling could lead to an inaccurate reference price for the collateral, potentially affecting the protocol's valuation and accounting.

What the attacker needed

None

The fix

The code was fixed to use the decimals from the underlying Comet token instead of the wrapper'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.

This report is about a smart contract that calculates the value of a collateral token. It used the wrong number of decimal places when converting a rate, which could make the calculated value wrong. That might cause the protocol to misjudge how much the collateral is worth.

More by sces60107

View all

3 more disclosed reports, best first.

More from Reserve Core

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.