PrivatePool.flashLoan() takes fee from the wrong address
The flashLoan function takes the fee from the caller (msg.sender) instead of the receiver of the callback, contrary to the EIP-3156 specification. This can cause an unexpected loss of funds for a caller who has pre-approved the pool to spend their tokens and is not the owner of the flashloan contract.
Submitted by Ruhum
Instead of taking the fee from the receiver of the flashloan callback, it pulls it from msg.sender.
As specified in EIP-3156:
“After the callback, the flashLoan function MUST take the amount + fee token from the receiver, or revert if this is not successful.”
This will be an unexpected loss of funds for the caller if they have the pool pre-approved to spend funds (e.g. they previously bought NFTs) and are not the owner of the flashloan contract they use for the callback.
Additionally, for ETH pools, it expects the caller to pay the fee upfront. But, the fee is generally paid with the profits made using the flashloaned tokens.
Proof of Concept
If baseToken is ETH, it expects the fee to already be sent with the call to flashLoan(). If it’s an ERC20 token, it will pull it from msg.sender instead of receiver:
function flashLoan(IERC3156FlashBorrower receiver, address token, uint256 tokenId, bytes calldata data)
external
payable
returns (bool)
{
// ...
// calculate the fee
uint256 fee = flashFee(token, tokenId);
// if base token is ETH then check that caller sent enough for the fee
if (baseToken == address(0) && msg.value < fee) revert InvalidEthAmount();
// ...
// transfer the fee from the borrower
if (baseToken != address(0)) ERC20(baseToken).transferFrom(msg.sender, address(this), fee);
return success;
}
Recommended Mitigation Steps
Change to:
uint initialBalance = address(this).balance;
// ...
if (baseToken != address(0)) ERC20(baseToken).transferFrom(receiver, address(this), fee);
else require(address(this).balance - initialBalance == fee);
outdoteth (Caviar) acknowledged
Alex the Entreprenerd (judge) commented:
I have considered downgrading to QA for the ETH aspect as technically there is no EIP for ETH flashloans (FL EIP is only for ERC20s).
That said, the way payment is pulled in ERC20s is breaking the spec, and for this reason am awarding Medium Severity.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The code uses msg.sender instead of the receiver parameter when transferring the flash loan fee.
Impact
A caller who has pre-approved the pool to spend their tokens and is not the owner of the flashloan contract could lose the fee amount unexpectedly; for ETH pools, the caller must pay the fee upfront, which is not how flash loans usually work.
What the attacker needed
Caller has pre-approved the pool to spend tokens; caller is not the owner of the flashloan contract
The fix
The fee should be taken from the receiver instead of msg.sender, and for ETH pools the contract should check that the fee was received rather than requiring it upfront.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A flash loan lets someone borrow tokens for a very short time, as long as they pay a small fee. The code that handles the loan takes that fee from the person who started the loan, not from the contract that actually receives the borrowed tokens. If the starter has already given the pool permission to take their tokens, they can lose money without expecting it.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.