Pool tokens can be stolen via PrivatePool.flashLoan function from previous owner
The previous owner of a PrivatePool could set token approvals via the execute function before selling the pool, then use flashLoan to steal the pool's tokens after ownership transfer. An attacker could drain all ERC721 and ERC20 tokens from the pool.
PrivatePool.sol ERC721 and ERC20 tokens can be stolen by the previous owner via execute and flashLoan functions (or by malicious approval by the current owner via execute)
Proof of Concept
Let’s say that Bob is the attacker and Alice is a regular user.
Bob creates a PrivatePool.sol where he deposits 5 ERC721 tokens and 500 USDC.
Then Bob creates a malicious contract (let’s call it PrivatePoolExploit.sol) and this contract contains onFlashLoan (IERC3156FlashBorrower), transferFrom , ownerOf, onERC721Received functions (like ERC721 does) and an additional attack function.
Via PrivatePool.execute function Bob approves USDC spending (type(uint).max) and setApprovalForAll for ERC721 tokens
Since the ownership of PrivatePool is stored in Factory.sol as an ERC721 token, ownership can be sold on any ERC721 marketplace. Alice decides to buy Bob’s PrivatePool and ownership is transferred to Alice.
Right after the ownership is transferred, Bob runs PrivatePoolExploit.attack function, which calls PrivatePool.flashLoan where PrivatePoolExploit.transferFrom will be called since the flash loan can be called on any address.
All the funds are stolen by Bob and Alice’s PrivatePool is left with nothing.
The contract caller should not be able to choose the token address in the PrivatePool.flashLoan function because there is no way to know if the token contract is actually an ERC721 contract.
Suggest removing token from function input parameters and using nft token everywhere, where token was used.
function flashLoan(IERC3156FlashBorrower receiver, uint256 tokenId, bytes calldata data)
external
payable
returns (bool)
{
address nftAddress = nft;
// check that the NFT is available for a flash loan
if (!availableForFlashLoan(nftAddress, tokenId)) revert NotAvailableForFlashLoan();
// calculate the fee
uint256 fee = flashFee(nftAddress, 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 NFT to the borrower
ERC721(nftAddress).safeTransferFrom(address(this), address(receiver), tokenId);
// call the borrower
bool success =
receiver.onFlashLoan(msg.sender, nftAddress, tokenId, fee, data) == keccak256("ERC3156FlashBorrower.onFlashLoan");
// check that flashloan was successful
if (!success) revert FlashLoanFailed();
// transfer the NFT from the borrower
ERC721(nftAddress).safeTransferFrom(address(receiver), address(this), tokenId);
// transfer the fee from the borrower
if (baseToken != address(0)) ERC20(baseToken).transferFrom(msg.sender, address(this), fee);
return success;
}
I think a potential fix is to prevent execute() from being able to call the baseToken the nft that is associated with the contract, which would stop the malicious approvals.
The Warden has shown how, due to composability, it’s possible for the previous owner to set the PrivatePool to grant approvals for all it’s tokens, in a way that will allow the previous owner to steal them back.
There are many considerations to this:
Would a reasonable buyer check for previous approvals?
Would a more reasonable approach be to buy the NFTs and create their own Pool?
Nonetheless the Approval Farming can be performed and the attack can be done in that way, however the buyer would have to buy a Pool for which the approvals have been setup and they would have to do so without revoking them (they could buy and revoke in the same tx).
Because of this, I belive that the finding is valid but of Medium Severity.
The original report as published on Code4rena, by Brenzee. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Generating the AI write-up for this report.
This usually takes about 40 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
Likely the execute function allowed the owner to approve spending of the pool's own baseToken and NFT, and flashLoan accepted an arbitrary token address, enabling the previous owner to exploit those approvals.
Impact
The previous owner could steal all ERC721 and ERC20 tokens from the pool after selling it, leaving the new owner with nothing.
What the attacker needed
Attacker is previous owner; victim buys the pool without revoking approvals
The fix
The execute function was fixed to revert if the target contract is the baseToken or nft, preventing malicious approvals.
How it was found
Bob creates a PrivatePool and deposits ERC721 tokens and USDC.
Bob deploys a malicious contract with flash loan and ERC721 callback functions.
Bob uses PrivatePool.execute to approve USDC spending and setApprovalForAll for the ERC721 tokens.
Bob sells the PrivatePool ownership to Alice via an ERC721 marketplace.
Bob calls his malicious contract's attack function, which invokes PrivatePool.flashLoan.
The flash loan transfers tokens to Bob's contract, which does not return them, stealing all funds.
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.
Generating the AI write-up for this report.
This usually takes about 40 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
A private pool is a smart contract that holds tokens and can be sold like an NFT. The previous owner could secretly give themselves permission to move the pool's tokens before selling it, then use a flash loan to take them back after the sale.
What is Access control?
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.