Adversary can abuse a quirk of compound redemption to manipulate the underlying exchange rate and maliciously disable cToken collaterals
A quirk in Compound's cToken redemption rounding lets an attacker redeem a tiny amount of underlying while burning fewer cTokens than the exact exchange rate implies, slightly lowering the cToken exchange rate. Because Reserve's collateral plugin disables a cToken whenever its reference price drops, the attacker can force the protocol to liquidate that collateral into a backup asset, causing losses.
Submitted by 0x52
Adversary can maliciously disable cToken collateral to cause loss to rToken during restructuring.
Proof of Concept
if (referencePrice < prevReferencePrice) {
markStatus(CollateralStatus.DISABLED);
}
CTokenNonFiatCollateral and CTokenFiatCollateral both use the default refresh behavior presented in FiatCollateral which has the above lines which automatically disables the collateral if the reference price ever decreases. This makes the assumption that cToken exchange rates never decrease but this is an incorrect assumption and can be exploited by an attacker to maliciously disable a cToken being used as collateral.
uint redeemTokens;
uint redeemAmount;
/* If redeemTokensIn > 0: */
if (redeemTokensIn > 0) {
/*
* We calculate the exchange rate and the amount of underlying to be redeemed:
* redeemTokens = redeemTokensIn
* redeemAmount = redeemTokensIn x exchangeRateCurrent
*/
redeemTokens = redeemTokensIn;
redeemAmount = mul_ScalarTruncate(exchangeRate, redeemTokensIn);
} else {
/*
* We get the current exchange rate and calculate the amount to be redeemed:
* redeemTokens = redeemAmountIn / exchangeRate
* redeemAmount = redeemAmountIn
*/
// @audit redeemTokens rounds in favor of the user
redeemTokens = div_(redeemAmountIn, exchangeRate);
redeemAmount = redeemAmountIn;
}
The exchange rate can be manipulated by a tiny amount during the redeem process. The focus above is the scenario where the user requests a specific amount of underlying. When calculating the number of cTokens to redeem for a specific amount of underlying it rounds IN FAVOR of the user. This allows the user to redeem more underlying than the exchange rate would otherwise imply. Because the user can redeem slightly more than intended they can create a scenario in which the exchange rate actually drops after they redeem. This is because compound calculates the exchange rate dynamically using the current supply of cTokens and the assets under management.
function exchangeRateStoredInternal() virtual internal view returns (uint) {
uint _totalSupply = totalSupply;
if (_totalSupply == 0) {
/*
* If there are no tokens minted:
* exchangeRate = initialExchangeRate
*/
return initialExchangeRateMantissa;
} else {
/*
* Otherwise:
* exchangeRate = (totalCash + totalBorrows - totalReserves) / totalSupply
*/
uint totalCash = getCashPrior();
uint cashPlusBorrowsMinusReserves = totalCash + totalBorrows - totalReserves;
uint exchangeRate = cashPlusBorrowsMinusReserves * expScale / _totalSupply;
return exchangeRate;
}
}
The exchangeRate when _totalSupply != 0 is basically:
exchangeRate = netAssets * 1e18 / totalSupply
Using this formula for we can now walk through an example of how this can be exploited
Example:
cTokens always start at a whole token ratio of 50:1 so let’s assume this ratio to begin with. Let’s use values similar to the current supply of cETH which is ~15M cETH and ~300k ETH. We’ll start by calculating the current ratio:
exchangeRate = 300_000 * 1e18 * 1e18 / 15_000_000 * 1e8 = 2e26
Now to exploit the ratio we request to redeem 99e8 redeemAmount which we can use to calculate the amount of tokens we need to burn:
redeemAmount = 99e8 * 1e18 / 2e26 = 1.98 -> 1
After truncation the amount burned is only 1. Now we can recalculate our ratio:
exchangeRate = ((300_000 * 1e18 * 1e18) - 99e8) / ((15_000_000 * 1e8) - 1) = 199999999999999933333333333
The ratio has now been slightly decreased. In CTokenFiatCollateral the exchange rate is truncated to 18 dp so:
(referencePrice < prevReferencePrice) -> (19999999999999993 < 2e18) == true
This results in that the collateral is now disabled. This forces the vault to liquidate their holdings to convert to a backup asset. This will almost certainly incur losses to the protocol that were maliciously inflicted.
The path to exploit is relatively straightforward:
refresh() cToken collateral to store current rate -> Manipulate compound rate via redemption -> refresh() cToken collateral to disable
Recommended Mitigation Steps
Since the issue is with the underlying compound contracts, nothing can make the attack impossible but it can be made sufficiently difficult. The simplest deterrent would be to implement a rate error value (i.e. 100) so that the exchange rate has to drop more than that before the token is disabled. The recommended value for this is a bit more complicated to unpack. The amount that the exchange rate changes heavily depends on the number of cTokens minted. The larger the amount the less it changes. Additionally a malicious user can make consecutive redemptions to lower the rate even further. Using an error rate of 1e12 would make it nearly impossible for this to be exploited while still being very sensitive to real (and concerning) changes in exchange rate.
- if (referencePrice < prevReferencePrice) {
+ if (referencePrice < prevReferencePrice - rateError) {
markStatus(CollateralStatus.DISABLED);
}
I do see in the cToken code base that the warden is correct with regard to the round down mechanism when redeeming cTokens using a redeemAmountIn.
The question I think comes down to is this dust amount enough to counteract the interest that would be accrued to the cToken which is added during the refresh call in
CTokenFiatCollateralWill leave open for sponsor review.
tmattimore (Reserve) confirmed
Issue confirmed.
Many defi protocols may have similar issues. We may choose to mitigate by building in revenue hiding to something like 1 part in 1 million to all collateral plugins.
This PR adds universal revenue hiding to all appreciating collateral: reserve-protocol/protocol#620
Status: Mitigation confirmed with comments. Full details in reports from HollaDieWaldfee, 0xA5DF, and AkshaySrivastav.
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 collateral plugin assumes the cToken exchange rate never decreases, but Compound's redeemAmountIn path rounds the cToken burn down in the user's favour, so a redemption can lower the rate by a dust amount.
Impact
The collateral is disabled, forcing the vault to liquidate its holdings into a backup asset, which will almost certainly cause losses to the protocol; the text does not quantify the loss or show a live exploit.
What the attacker needed
Attacker can redeem from the underlying Compound market; attacker can call refresh() on the collateral; the cToken market has a large supply
The fix
The project mitigated by adding universal revenue hiding (a small error tolerance) to all appreciating collateral plugins so a dust-sized rate drop does not disable them.
How it was found
- Call refresh() on the cToken collateral to store the current exchange rate as the previous reference price.
- Redeem a small specific amount of underlying from Compound so the cToken burn rounds down and the exchange rate drops slightly.
- Call refresh() again so the new lower reference price is compared with the stored one and the collateral is disabled.
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.
Compound cTokens represent a share of a pool; their exchange rate is the pool's assets divided by the number of cTokens. When you redeem a specific amount of the underlying asset, the code rounds the number of cTokens you must burn in your favour, so the pool can end up with a marginally lower exchange rate. Reserve's plugin treats any drop in that rate as a sign the collateral is failing and disables it, so an attacker can trigger that disable on purpose.
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.