deposit() instead treats every deposit as though it generated a flash-loan fee. It increases the exchange rate using a calculated fee that was never transferred into the pool. As a result, newly minted AssetTokens become worth more than the deposited assets, allowing a depositor to redeem more tokens than they supplied. The surplus is taken from existing LP liquidity.// Root cause in the codebase with @> marks to highlight the relevant section.
Likelihood:
Every sufficiently large deposit into a pool with existing LP liquidity triggers an exchange-rate increase despite no fee entering the pool.
An attacker only needs the underlying token and approval to call the public deposit() and redeem() functions.
Impact:
A depositor can immediately redeem more underlying tokens than they deposited.
Existing LPs bear the loss, and repeated deposits/redemptions can progressively drain their liquidity.
Assume a supported 18-decimal token priced at 1 WETH and a 0.3% protocol fee.
An LP deposits 1,000 tokens:
Pool balance: 1,000
AssetToken supply: 1,000
An attacker deposits 1,000 tokens:
The attacker receives 1,000 AssetTokens at the pre-deposit 1:1 exchange rate.
getCalculatedFee(1,000) returns 3 tokens.
updateExchangeRate(3) changes the rate to:
The pool only received the attacker’s 1,000 deposited tokens:
Pool balance: 2,000
Total AssetToken claims: 2,000 * 1.0015 = 2,003
The attacker immediately redeems 1,000 AssetTokens:
The attacker profits 1.5 tokens while the original LP’s remaining claim is undercollateralized by the same amount.
poc with code Below
Do not update the exchange rate during deposits. Update it only after a flash loan has successfully repaid an actual fee.
A safer design is to calculate the exchange rate from actual pool assets:
Alternatively, after confirming flash-loan repayment, increase the exchange rate based on the actual received fee—not a quoted fee—and never call updateExchangeRate() from deposit().
# Summary Exchange rate for asset token is updated on deposit. This means users can deposit (which will increase exchange rate), and then immediately withdraw more underlying tokens than they deposited. # Details Per documentation: > Liquidity providers can deposit assets into ThunderLoan and be given AssetTokens in return. **These AssetTokens gain interest over time depending on how often people take out flash loans!** Asset tokens gain interest when people take out flash loans with the underlying tokens. In current version of ThunderLoan, exchange rate is also updated when user deposits underlying tokens. This does not match with documentation and will end up causing exchange rate to increase on deposit. This will allow anyone who deposits to immediately withdraw and get more tokens back than they deposited. Underlying of any asset token can be completely drained in this manner. # Filename `src/protocol/ThunderLoan.sol` # Permalinks https://github.com/Cyfrin/2023-11-Thunder-Loan/blob/8539c83865eb0d6149e4d70f37a35d9e72ac7404/src/protocol/ThunderLoan.sol#L153-L154 # Impact Users can deposit and immediately withdraw more funds. Since exchange rate is increased on deposit, they will withdraw more funds then they deposited without any flash loans being taken at all. # Recommendations It is recommended to not update exchange rate on deposits and updated it only when flash loans are taken, as per documentation. ```diff function deposit(IERC20 token, uint256 amount) external revertIfZero(amount) revertIfNotAllowedToken(token) { AssetToken assetToken = s_tokenToAssetToken[token]; uint256 exchangeRate = assetToken.getExchangeRate(); uint256 mintAmount = (amount * assetToken.EXCHANGE_RATE_PRECISION()) / exchangeRate; emit Deposit(msg.sender, token, amount); assetToken.mint(msg.sender, mintAmount); - uint256 calculatedFee = getCalculatedFee(token, amount); - assetToken.updateExchangeRate(calculatedFee); token.safeTransferFrom(msg.sender, address(assetToken), amount); } ``` # POC ```solidity function testExchangeRateUpdatedOnDeposit() public setAllowedToken { tokenA.mint(liquidityProvider, AMOUNT); tokenA.mint(user, AMOUNT); // deposit some tokenA into ThunderLoan vm.startPrank(liquidityProvider); tokenA.approve(address(thunderLoan), AMOUNT); thunderLoan.deposit(tokenA, AMOUNT); vm.stopPrank(); // another user also makes a deposit vm.startPrank(user); tokenA.approve(address(thunderLoan), AMOUNT); thunderLoan.deposit(tokenA, AMOUNT); vm.stopPrank(); AssetToken assetToken = thunderLoan.getAssetFromToken(tokenA); // after a deposit, asset token's exchange rate has aleady increased // this is only supposed to happen when users take flash loans with underlying assertGt(assetToken.getExchangeRate(), 1 * assetToken.EXCHANGE_RATE_PRECISION()); // now liquidityProvider withdraws and gets more back because exchange // rate is increased but no flash loans were taken out yet // repeatedly doing this could drain all underlying for any asset token vm.startPrank(liquidityProvider); thunderLoan.redeem(tokenA, assetToken.balanceOf(liquidityProvider)); vm.stopPrank(); assertGt(tokenA.balanceOf(liquidityProvider), AMOUNT); } ```
The contest is live. Earn rewards by submitting a finding.
Submissions are being reviewed by our AI judge. Results will be available in a few minutes.
View all submissionsThe contest is complete and the rewards are being distributed.