CompoundStrategy attempts to transfer out a greater amount of ETH than will actually be withdrawn, leading to DoS
The CompoundStrategy contract calculates the amount of cETH to redeem using integer division, which truncates and yields slightly less ETH than requested. When the strategy then checks its wrapped native balance against the requested amount, the shortfall causes the withdrawal to revert, blocking withdrawals.
Submitted by kaden
Withdrawals will revert whenever there is not sufficient wrapped native tokens to cover loss from integer truncation.
Proof of Concept
CompoundStrategy._withdraw calculates the amount of cETH to redeem with cToken.exchangeRateStored based on a provided amount of ETH to receive from the withdrawal.
function _withdraw(
address to,
uint256 amount
) internal override nonReentrant {
uint256 available = _currentBalance();
require(available >= amount, "CompoundStrategy: amount not valid");
uint256 queued = wrappedNative.balanceOf(address(this));
if (amount > queued) {
uint256 pricePerShare = cToken.exchangeRateStored();
uint256 toWithdraw = (((amount - queued) * (10 ** 18)) /
pricePerShare);
cToken.redeem(toWithdraw);
To understand the vulnerability, we look at the CEther contract which we are attempting to withdraw from.
/**
* @notice Sender redeems cTokens in exchange for the underlying asset
* @dev Accrues interest whether or not the operation succeeds, unless reverted
* @param redeemTokens The number of cTokens to redeem into underlying
* @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
*/
function redeem(uint redeemTokens) external returns (uint) {
return redeemInternal(redeemTokens);
}
redeem returns the result from redeemInternal
/**
* @notice Sender redeems cTokens in exchange for the underlying asset
* @dev Accrues interest whether or not the operation succeeds, unless reverted
* @param redeemTokens The number of cTokens to redeem into underlying
* @return uint 0=success, otherwise a failure (see ErrorReporter.sol for details)
*/
function redeemInternal(uint redeemTokens) internal nonReentrant returns (uint) {
uint error = accrueInterest();
if (error != uint(Error.NO_ERROR)) {
// accrueInterest emits logs on errors, but we still want to log the fact that an attempted redeem failed
return fail(Error(error), FailureInfo.REDEEM_ACCRUE_INTEREST_FAILED);
}
// redeemFresh emits redeem-specific logs on errors, so we don't need to
return redeemFresh(msg.sender, redeemTokens, 0);
}
After accruing interest and checking for errors, redeemInternal returns the result from redeemFresh with the amount of tokens to redeem passed as the second param.
function redeemFresh(address payable redeemer, uint redeemTokensIn, uint redeemAmountIn) internal returns (uint) {
...
/* exchangeRate = invoke Exchange Rate Stored() */
(vars.mathErr, vars.exchangeRateMantissa) = exchangeRateStoredInternal();
...
/* 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
*/
vars.redeemTokens = redeemTokensIn;
(vars.mathErr, vars.redeemAmount) = mulScalarTruncate(Exp({mantissa: vars.exchangeRateMantissa}), redeemTokensIn);
if (vars.mathErr != MathError.NO_ERROR) {
return failOpaque(Error.MATH_ERROR, FailureInfo.REDEEM_EXCHANGE_TOKENS_CALCULATION_FAILED, uint(vars.mathErr));
}
...
vars.err = doTransferOut(redeemer, vars.redeemAmount);
...
}
redeemFresh retrieves the exchange rate (the same one that CompoundStrategy._withdraw uses to calculate the amount to redeem), and uses it to calculate vars.redeemAmount which is later transferred to the redeemer. This is the amount of underlying ETH that we are redeeming the CEther tokens for.
We can carefully copy over the logic used in CEther to calculate the amount of underlying ETH to receive, as well as the logic used in CompoundStrategy._withdraw to determine how many CEther to redeem for the desired output amount of underlying ETH, creating the following test contract in Remix.
pragma solidity 0.8.19;
contract MatchCalcs {
uint constant expScale = 1e18;
struct Exp {
uint mantissa;
}
function mulScalarTruncate(Exp memory a, uint scalar) pure external returns (uint) {
return truncate(mulScalar(a, scalar));
}
function mulScalar(Exp memory a, uint scalar) pure internal returns (Exp memory) {
uint256 scaledMantissa = mulUInt(a.mantissa, scalar);
return Exp({mantissa: scaledMantissa});
}
function mulUInt(uint a, uint b) internal pure returns (uint) {
if (a == 0) {
return 0;
}
uint c = a * b;
if (c / a != b) {
return 0;
} else {
return c;
}
}
function truncate(Exp memory exp) pure internal returns (uint) {
// Note: We are not using careful math here as we're performing a division that cannot fail
return exp.mantissa / expScale;
}
function getToWithdraw(uint256 amount, uint256 exchangeRate) pure external returns (uint) {
return amount * (10 ** 18) / exchangeRate;
}
}
We run the following example with the current exchangeRateStored (at the time of writing) of 200877136531571418792530957 and an output amount of underlying ETH to receive of 1e18 on the function getToWithdraw, receiving an output of 4978167337 CEther. This is the amount of CEther that would be passed to CEther.redeem.
Next we can see the output amount of underlying ETH according to CEthers logic. We pass the same exchangeRateStored value and the amount of CEther to redeem: 4978167337, receiving an output amount of 999999999831558306, less than the intended amount of 1e18.
Since we receive less than intended to receive from CEther.redeem, the _withdraw call likely fails at the following check:
require(
wrappedNative.balanceOf(address(this)) >= amount,
"CompoundStrategy: not enough"
);
Recommended Mitigation Steps
Rather than computing an amount of CEther to redeem, we can instead use the CEther.redeemUnderlying function to receive our intended amount of underlying ETH.
cryptotechmaker (Tapioca) confirmed
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 strategy converts the desired ETH amount to cETH using integer division that truncates downwards, so redeeming that many cETH returns slightly less than the requested amount, which then fails the balance check.
Impact
Withdrawals could revert whenever the strategy needs to redeem cETH and the truncated redemption leaves it short of the requested amount, causing a denial of service for users.
What the attacker needed
Strategy holds cETH but insufficient wrapped native; withdrawal amount exceeds queued balance
The fix
The strategy should use redeemUnderlying to request the exact underlying ETH amount instead of computing a cETH amount to redeem.
How it was found
- Call withdraw for an amount greater than the wrapped native balance held by the strategy.
- The strategy computes cETH to redeem by dividing the shortfall by the exchange rate, truncating the result.
- It calls cToken.redeem with that truncated cETH amount.
- CEther redeems and transfers out slightly less ETH than the requested amount.
- The strategy's balance check fails and the withdrawal reverts.
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.
When you withdraw, the contract first works out how many 'shares' to cash in. Because of how whole-number maths works in code, it cashes in slightly too few shares and gets back a tiny bit less money than you asked for. It then checks it has enough money to pay you the full amount, sees it is short by a fraction, and cancels the whole withdrawal.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.