Skip to content
Business logic Audit finding Intermediate

EthRouter can’t perform multiple changes

The EthRouter contract's change function sends the entire msg.value to each pool in a loop, but after the first iteration the router's balance is reduced, causing subsequent iterations to fail with an out-of-funds error. This prevents users from performing multiple changes in a single transaction.

minhtrng Follow with adriro, Voyvoda and 11 others

Originally disclosed on Code4rena

Submitted by minhtrng, also found by adriro, Voyvoda, ych18, 0xRobocop, bin2chen, chaduke, Ruhum, ladboy233, 0x4db5362c, Kek, BradMoon, ChrisTina, and Rappie

EthRouter is meant to support multiple changes in one tx, but that would fail.

Proof of Concept

The function EthRouter.change sends msg.value to pool in a for loop:

for (uint256 i = 0; i < changes.length; i++) {
    Change memory _change = changes[i];

    ...

    // execute change
    PrivatePool(_change.pool).change{value: msg.value}(
        _change.inputTokenIds,
        _change.inputTokenWeights,
        _change.inputProof,
        _change.stolenNftProofs,
        _change.outputTokenIds,
        _change.outputTokenWeights,
        _change.outputProof
    );

The pool subtracts the fee, and sends the rest back to the router. After the first iteration the router contains less ETH than msg.value and will revert

Add to Change.t.sol and run with forge test --match test_twoChanges -vvvv

    function test_twoChangesOneCall() public {
    uint256[] memory inputTokenIds = new uint256[](1);
    uint256[] memory inputTokenWeights = new uint256[](0);
    uint256[] memory outputTokenIds = new uint256[](1);
    uint256[] memory outputTokenWeights = new uint256[](0);

    uint256[] memory inputTokenIds2 = new uint256[](1);
    uint256[] memory inputTokenWeights2 = new uint256[](0);
    uint256[] memory outputTokenIds2 = new uint256[](1);
    uint256[] memory outputTokenWeights2 = new uint256[](0);

    inputTokenIds[0] = 5;
    outputTokenIds[0] = 0;

    inputTokenIds2[0] = 6;
    outputTokenIds2[0] = 1;

    EthRouter.Change[] memory changes = new EthRouter.Change[](2);
    changes[0] = EthRouter.Change({
        pool: payable(address(privatePool)),
        nft: address(milady),
        inputTokenIds: inputTokenIds,
        inputTokenWeights: inputTokenWeights,
        inputProof: PrivatePool.MerkleMultiProof(new bytes32[](0), new bool[](0)),
        stolenNftProofs: new IStolenNftOracle.Message[](0),
        outputTokenIds: outputTokenIds,
        outputTokenWeights: outputTokenWeights,
        outputProof: PrivatePool.MerkleMultiProof(new bytes32[](0), new bool[](0))
    });

    changes[1] = EthRouter.Change({
        pool: payable(address(privatePool)),
        nft: address(milady),
        inputTokenIds: inputTokenIds2,
        inputTokenWeights: inputTokenWeights2,
        inputProof: PrivatePool.MerkleMultiProof(new bytes32[](0), new bool[](0)),
        stolenNftProofs: new IStolenNftOracle.Message[](0),
        outputTokenIds: outputTokenIds2,
        outputTokenWeights: outputTokenWeights2,
        outputProof: PrivatePool.MerkleMultiProof(new bytes32[](0), new bool[](0))
    });

    (uint256 changeFee,) = privatePool.changeFeeQuote(inputTokenIds.length * 1e18);

    //WARDEN: multiply with 10 just to make sure there really is enough
    ethRouter.change{value: changeFee*10}(changes, 0);

}

Output:

...
    │   ├─ [0] PrivatePool::change{value: 50000000000000000000}([6], [], ([], []), [], [1], [], ([], []))
    │   │   └─ ← "EvmError: OutOfFund"
    │   └─ ← "EvmError: Revert"
    └─ ← "EvmError: Revert"

Recommended Mitigation Steps

Only send the required change fee and not msg.value.

outdoteth (Caviar) confirmed and mitigated:

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

The proposed fix is to add a baseTokenAmount field in the Change struct and to use this field instead of msg.value when making the change operation.

PrivatePool(_change.pool).change{value: _change.baseTokenAmount}(
  // -- snip --
);

Alex the Entreprenerd (judge) commented:

The Warden has shown how, because msg.value is passed in a loop, to functions that could reduce this.balance, the tx can revert, breaking the functionality for those use cases.

Because that doesn’t cause a loss of principal, but shows a broken functionality for some cases, I agree with Medium Severity.

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


The original report as published on Code4rena, by minhtrng. 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 router forwards the full msg.value to each pool in a loop, but the pool returns only the fee-subtracted amount, leaving the router with insufficient balance for subsequent iterations.

Impact

Users cannot perform multiple changes in one transaction; the transaction reverts, but no funds are lost.

What the attacker needed

User attempts multiple changes in one transaction

The fix

The fix was to add a baseTokenAmount field to the Change struct and use that amount instead of msg.value when calling the pool.

How it was found

  1. Set up two changes with different input and output token IDs.
  2. Call ethRouter.change with a value equal to ten times the change fee.
  3. The transaction reverts with an out-of-funds error on the second change.

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.

The router is a smart contract that helps users interact with pools. When a user wants to make multiple changes at once, the router tries to send the same amount of money to each pool, but after the first payment it doesn't have enough left for the next, so the whole transaction 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 minhtrng

View all

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