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.
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);
}
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.
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.
Generating the AI write-up for this report.
This usually takes about a minute. 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 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
Set up two changes with different input and output token IDs.
Call ethRouter.change with a value equal to ten times the change fee.
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.
Generating the AI write-up for this report.
This usually takes about a minute. 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
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.