Thunder Loan

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

LP deposits create unbacked share liability by incorrectly increasing the exchange rate

Root + Impact

Description

Liquidity providers deposit underlying tokens into ThunderLoan and receive AssetToken shares. Those shares are redeemable for underlying according to the AssetToken exchange rate, and the README states that AssetTokens gain interest over time depending on how often users take out flash loans.

ThunderLoan.deposit() mints shares for the deposited amount and then immediately increases the AssetToken exchange rate using getCalculatedFee(token, amount), even though no flash loan occurred and no fee was paid into the pool. Any deposit where the calculated fee is nonzero increases liabilities as if the protocol received both the deposit and a flash-loan fee, while the AssetToken receives only the deposited amount. This creates unbacked share liability and can make LP withdrawals fail once redeemable claims exceed live reserves.

// src/protocol/ThunderLoan.sol
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);
}
// src/protocol/AssetToken.sol
function updateExchangeRate(uint256 fee) external onlyThunderLoan {
// newExchangeRate = oldExchangeRate * (totalSupply + fee) / totalSupply
@> uint256 newExchangeRate = s_exchangeRate * (totalSupply() + fee) / totalSupply();
if (newExchangeRate <= s_exchangeRate) {
revert AssetToken__ExhangeRateCanOnlyIncrease(s_exchangeRate, newExchangeRate);
}
@> s_exchangeRate = newExchangeRate;
emit ExchangeRateUpdated(s_exchangeRate);
}

Risk

Likelihood:

  • This occurs on ordinary deposits into an allowed token whenever getCalculatedFee(token, amount) is nonzero and large enough to increase the exchange rate.

  • The default configuration and mock oracle price demonstrate the issue deterministically: a 1000e18 deposit causes a calculated fee of 3e18, increasing the exchange rate from 1e18 to 1.003e18 without adding that fee to reserves.

Impact:

  • Every affected deposit increases redeemable liabilities by more than the underlying actually received. The PoC uses one 1000e18 deposit as the minimal example: it creates 1003e18 of redeemable liability while the AssetToken holds only 1000e18 underlying.

  • Liquidity providers can be unable to withdraw their deposited funds after the protocol accumulates unbacked liabilities. The single-LP empty-market case is the shortest trace showing this impact, not a limitation of the bug.

Proof of Concept

Add the following test file:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
import { AssetToken } from "../src/protocol/AssetToken.sol";
import { BaseTest } from "./unit/BaseTest.t.sol";
contract I02DepositDoesNotMintUnbackedSharesTest is BaseTest {
uint256 private constant DEPOSIT_AMOUNT = 1_000e18;
address private liquidityProvider = address(0xA11CE);
function test_I02_01_singleDepositDoesNotCreateUnbackedShareLiability() public {
thunderLoan.setAllowedToken(tokenA, true);
AssetToken assetToken = thunderLoan.getAssetFromToken(tokenA);
uint256 startingExchangeRate = assetToken.getExchangeRate();
uint256 calculatedDepositFee = thunderLoan.getCalculatedFee(tokenA, DEPOSIT_AMOUNT);
tokenA.mint(liquidityProvider, DEPOSIT_AMOUNT);
vm.startPrank(liquidityProvider);
tokenA.approve(address(thunderLoan), DEPOSIT_AMOUNT);
thunderLoan.deposit(tokenA, DEPOSIT_AMOUNT);
vm.stopPrank();
uint256 liveUnderlying = tokenA.balanceOf(address(assetToken));
uint256 exchangeRate = assetToken.getExchangeRate();
uint256 shareLiability =
(assetToken.totalSupply() * exchangeRate) / assetToken.EXCHANGE_RATE_PRECISION();
assertEq(startingExchangeRate, assetToken.EXCHANGE_RATE_PRECISION(), "setup: unexpected starting rate");
assertGt(calculatedDepositFee, 0, "setup: deposit fee path not reached");
assertGt(exchangeRate, startingExchangeRate, "setup: deposit did not increase exchange rate");
assertEq(liveUnderlying, DEPOSIT_AMOUNT, "setup: live underlying should equal received deposit");
assertLe(shareLiability, liveUnderlying, "deposit created unbacked share liability");
}
function test_I02_02_singleLpRedeemRevertsAfterOnlyDeposit() public {
thunderLoan.setAllowedToken(tokenA, true);
tokenA.mint(liquidityProvider, DEPOSIT_AMOUNT);
vm.startPrank(liquidityProvider);
tokenA.approve(address(thunderLoan), DEPOSIT_AMOUNT);
thunderLoan.deposit(tokenA, DEPOSIT_AMOUNT);
vm.expectRevert();
thunderLoan.redeem(tokenA, type(uint256).max);
vm.stopPrank();
}
}

Run:

forge test --match-contract I02DepositDoesNotMintUnbackedSharesTest -vv

Result:

[FAIL: deposit created unbacked share liability: 1003000000000000000000 > 1000000000000000000000]
test_I02_01_singleDepositDoesNotCreateUnbackedShareLiability()
[PASS]
test_I02_02_singleLpRedeemRevertsAfterOnlyDeposit()

The first test shows that the deposit leaves the AssetToken with 1000e18 underlying but 1003e18 of share liability. The second test shows the user-facing impact: the only LP cannot redeem after only depositing.

Recommended Mitigation

Do not update the exchange rate during deposits. Exchange-rate increases should be backed by actual fees paid into the AssetToken, such as after successful flash-loan repayment.

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

Consider transferring the underlying before minting shares, or using a balance-delta check, so share issuance is based on the amount actually received by the AssetToken.

Updates

Lead Judging Commences

ai-first-flight-judge Lead Judge 1 day 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!