Skip to content
Access control Audit finding Intermediate

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.

Brenzee Follow with ulqiorra and ladboy233

Originally disclosed on Code4rena

Submitted by Brenzee, also found by ulqiorra and ladboy233

https://github.com/code-423n4/2023-04-caviar/blob/main/src/PrivatePool.sol#L461

https://github.com/code-423n4/2023-04-caviar/blob/main/src/PrivatePool.sol#L623-L654

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.

  1. Bob creates a PrivatePool.sol where he deposits 5 ERC721 tokens and 500 USDC.
  2. 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.
  3. Via PrivatePool.execute function Bob approves USDC spending (type(uint).max) and setApprovalForAll for ERC721 tokens
  4. 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.
  5. 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.
  6. All the funds are stolen by Bob and Alice’s PrivatePool is left with nothing.

See warden’s original submission for full POC.

Tools Used

Foundry/VSCode

Recommended Mitigation Steps

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;
    }

outdoteth (Caviar) confirmed, but disagreed with severity and commented:

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.

outdoteth (Caviar) mitigated:

Fixed in: https://github.com/outdoteth/caviar-private-pools/pull/2

Proposed fix is to add a check in the execute() function that will revert if the target contract is the baseToken or nft.

if (target == address(baseToken) || target == address(nft)) revert InvalidTarget();

Alex the Entreprenerd (judge) decreased severity to Medium and commented:

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.

Status: Mitigation confirmed with comments. Full details in reports from rbserver, KrisApostolov, and rvierdiiev.


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.

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

  1. Bob creates a PrivatePool and deposits ERC721 tokens and USDC.
  2. Bob deploys a malicious contract with flash loan and ERC721 callback functions.
  3. Bob uses PrivatePool.execute to approve USDC spending and setApprovalForAll for the ERC721 tokens.
  4. Bob sells the PrivatePool ownership to Alice via an ERC721 marketplace.
  5. Bob calls his malicious contract's attack function, which invokes PrivatePool.flashLoan.
  6. 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.

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.

More by Brenzee

View all

1 more disclosed report, best first.

More from Caviar Private Pools

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Broken access control

The lesson behind this report.

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.