There's a pool with 1000 ETH in balance, offering flash loans. It has a fixed fee of 1 ETH.
A user has deployed a contract with 10 ETH in balance. It’s capable of interacting with the pool and receiving flash loans of ETH.
Take all ETH out of the user's contract. If possible, in a single transaction.
TL;DR
The flashloan pool charges 1 ETH service fee for each flashloan request. The borrower parameter can be anyone, therefore we can force some other address to borrow flashloan repetitively and drain the fund of that address.
Code Audit
The first thing we see is the unusual flash loan fee:
uint256privateconstant FIXED_FEE =1ether;// not the cheapest flash loan
If we can charge the user 10 times, then user's fund will be drained.
The NaiveReceiverLenderPool.flashLoan() function has some issues:
functionflashLoan(addressborrower,uint256borrowAmount)externalnonReentrant{uint256 balanceBefore =address(this).balance;if(balanceBefore < borrowAmount)revertNotEnoughETHInPool();if(!borrower.isContract())revertBorrowerMustBeADeployedContract();// Transfer ETH and handle control to receiver borrower.functionCallWithValue(abi.encodeWithSignature("receiveEther(uint256)", FIXED_FEE), borrowAmount);if(address(this).balance < balanceBefore + FIXED_FEE){revertFlashLoanHasNotBeenPaidBack();}}
This function does not verify if borrower == msg.sender, so anyone can call this function on behalf of another user. Recall that the flash loan is extremely high, so we have a good chance here to drain user's fund.
Moreover, note that there is nothing stops us from calling flashLoan() multiple times. How is the flash loan fee handled in such case? Take a look at FlashLoanReceiver.receiveEther():
We see that the fee is added linearly. That means 1 flash loan costs 1 ETH, 2 flash loans cost 2 ETH, and so on. To drain user's fund, we can run 10 flash loans consecutively.
Building PoC
Borrow flashloan on behalf of flashLoanReceiver to drain its fund via the fee: