Thunder Loan

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

[C-01] Storage Layout Collision in Upgrade — Flash Loan Fee Becomes 100%

[C-01] Storage Layout Collision in Upgrade — Flash Loan Fee Becomes 100%

Description

In an upgradeable proxy pattern, storage slots must remain consistent between implementation versions. ThunderLoan.sol declares s_feePrecision as a storage variable occupying slot N, followed by s_flashLoanFee at slot N+1. ThunderLoanUpgraded.sol changes s_feePrecision to a compile-time constant (FEE_PRECISION), which does not occupy storage. This shifts s_flashLoanFee up to slot N, where it reads 1e18 (the old s_feePrecision value) instead of 3e15.

// ThunderLoan.sol — storage layout written by proxy
@> uint256 private s_feePrecision; // Slot N → value 1e18
@> uint256 private s_flashLoanFee; // Slot N+1 → value 3e15
// ThunderLoanUpgraded.sol — storage layout read by proxy after upgrade
@> uint256 public constant FEE_PRECISION = 1e18; // NOT in storage (constant)
@> uint256 private s_flashLoanFee; // Now reads Slot N → gets 1e18!

Risk

Likelihood: High

  • Occurs deterministically on any upgrade from ThunderLoan to ThunderLoanUpgraded via the UUPS proxy. No attacker action required — the storage corruption is inherent to the upgrade.

Impact: High

  • Flash loan fee jumps from 0.3% to 100% of borrowed value, making all flash loans economically impossible.

  • Existing liquidity providers cannot earn yield because no new flash loans will be taken.

Severity: Critical

Proof of Concept

When the proxy upgrades from ThunderLoan to ThunderLoanUpgraded, the proxy's storage is not migrated. The upgraded contract reads s_flashLoanFee from slot N (which was s_feePrecision = 1e18), not slot N+1 (which was s_flashLoanFee = 3e15). Any subsequent call to getCalculatedFee() uses 1e18 as the fee multiplier:

  • Before upgrade: fee = value * 3e15 / 1e18 = value * 0.003 (0.3%)

  • After upgrade: fee = value * 1e18 / 1e18 = value * 1.0 (100%)

// Verify by comparing storage slot reads before and after upgrade:
// Before: slot N = 1e18 (s_feePrecision), slot N+1 = 3e15 (s_flashLoanFee)
// After: slot N = 1e18 (now read as s_flashLoanFee!), constant FEE_PRECISION = 1e18
function test_storage_collision_after_upgrade() public {
// Deploy proxy with ThunderLoan
ThunderLoan impl = new ThunderLoan();
ERC1967Proxy proxy = new ERC1967Proxy(address(impl), "");
ThunderLoan thunder = ThunderLoan(address(proxy));
thunder.initialize(address(mockPoolFactory));
// Fee is 0.3% before upgrade
uint256 feeBefore = thunder.getFee();
assertEq(feeBefore, 3e15); // 0.3%
// Upgrade to ThunderLoanUpgraded
ThunderLoanUpgraded implV2 = new ThunderLoanUpgraded();
thunder.upgradeTo(address(implV2));
ThunderLoanUpgraded thunderV2 = ThunderLoanUpgraded(address(proxy));
// Fee is now 100% — storage collision!
uint256 feeAfter = thunderV2.getFee();
assertEq(feeAfter, 1e18); // 100% — BROKEN
}

Recommended Mitigation

Preserve the storage layout by keeping s_feePrecision as a storage variable in the upgraded contract, maintaining slot alignment:

// ThunderLoanUpgraded.sol
mapping(IERC20 => AssetToken) public s_tokenToAssetToken;
- uint256 public constant FEE_PRECISION = 1e18;
+ uint256 private s_feePrecision; // Keep as storage to preserve slot N
uint256 private s_flashLoanFee;
+ uint256 public constant FEE_PRECISION = 1e18; // Use constant for logic, but don't remove the storage slot
Updates

Lead Judging Commences

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

[H-01] Storage Collision during upgrade

## Description The thunderloanupgrade.sol storage layout is not compatible with the storage layout of thunderloan.sol which will cause storage collision and mismatch of variable to different data. ## Vulnerability Details Thunderloan.sol at slot 1,2 and 3 holds s_feePrecision, s_flashLoanFee and s_currentlyFlashLoaning, respectively, but the ThunderLoanUpgraded at slot 1 and 2 holds s_flashLoanFee, s_currentlyFlashLoaning respectively. the s_feePrecision from the thunderloan.sol was changed to a constant variable which will no longer be assessed from the state variable. This will cause the location at which the upgraded version will be pointing to for some significant state variables like s_flashLoanFee to be wrong because s_flashLoanFee is now pointing to the slot of the s_feePrecision in the thunderloan.sol and when this fee is used to compute the fee for flashloan it will return a fee amount greater than the intention of the developer. s_currentlyFlashLoaning might not really be affected as it is back to default when a flashloan is completed but still to be noted that the value at that slot can be cleared to be on a safer side. ## Impact 1. Fee is miscalculated for flashloan 1. users pay same amount of what they borrowed as fee ## POC 2 ``` function testFlashLoanAfterUpgrade() public setAllowedToken hasDeposits { //upgrade thunderloan upgradeThunderloan(); uint256 amountToBorrow = AMOUNT * 10; console.log("amount flashloaned", amountToBorrow); uint256 calculatedFee = thunderLoan.getCalculatedFee( tokenA, amountToBorrow ); AssetToken assetToken = thunderLoan.getAssetFromToken(tokenA); vm.startPrank(user); tokenA.mint(address(mockFlashLoanReceiver), amountToBorrow); thunderLoan.flashloan( address(mockFlashLoanReceiver), tokenA, amountToBorrow, "" ); vm.stopPrank(); console.log("feepaid", calculatedFee); assertEq(amountToBorrow, calculatedFee); } ``` Add the code above to thunderloantest.t.sol and run `forge test --mt testFlashLoanAfterUpgrade -vv` to test for the second poc ## Recommendations The team should should make sure the the fee is pointing to the correct location as intended by the developer: a suggestion recommendation is for the team to get the feeValue from the previous implementation, clear the values that will not be needed again and after upgrade reset the fee back to its previous value from the implementation. ##POC for recommendation ``` // function upgradeThunderloanFixed() internal { thunderLoanUpgraded = new ThunderLoanUpgraded(); //getting the current fee; uint fee = thunderLoan.getFee(); // clear the fee as thunderLoan.updateFlashLoanFee(0); // upgrade to the new implementation thunderLoan.upgradeTo(address(thunderLoanUpgraded)); //wrapped the abi thunderLoanUpgraded = ThunderLoanUpgraded(address(proxy)); // set the fee back to the correct value thunderLoanUpgraded.updateFlashLoanFee(fee); } function testSlotValuesFixedfterUpgrade() public setAllowedToken { AssetToken asset = thunderLoan.getAssetFromToken(tokenA); uint precision = thunderLoan.getFeePrecision(); uint fee = thunderLoan.getFee(); bool isflanshloaning = thunderLoan.isCurrentlyFlashLoaning(tokenA); /// 4 slots before upgrade console.log("????SLOTS VALUE BEFORE UPGRADE????"); console.log("slot 0 for s_tokenToAssetToken =>", address(asset)); console.log("slot 1 for s_feePrecision =>", precision); console.log("slot 2 for s_flashLoanFee =>", fee); console.log("slot 3 for s_currentlyFlashLoaning =>", isflanshloaning); //upgrade function upgradeThunderloanFixed(); //// after upgrade they are only 3 valid slot left because precision is now set to constant AssetToken assetUpgrade = thunderLoan.getAssetFromToken(tokenA); uint feeUpgrade = thunderLoan.getFee(); bool isflanshloaningUpgrade = thunderLoan.isCurrentlyFlashLoaning( tokenA ); console.log("????SLOTS VALUE After UPGRADE????"); console.log("slot 0 for s_tokenToAssetToken =>", address(assetUpgrade)); console.log("slot 1 for s_flashLoanFee =>", feeUpgrade); console.log( "slot 2 for s_currentlyFlashLoaning =>", isflanshloaningUpgrade ); assertEq(address(asset), address(assetUpgrade)); //asserting precision value before upgrade to be what fee takes after upgrades assertEq(fee, feeUpgrade); // #POC assertEq(isflanshloaning, isflanshloaningUpgrade); } ``` Add the code above to thunderloantest.t.sol and run with `forge test --mt testSlotValuesFixedfterUpgrade -vv`. it can also be tested with `testFlashLoanAfterUpgrade function` and see the fee properly calculated for flashloan

Support

FAQs

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

Give us feedback!