Skip to content
Business logic Audit finding Intermediate

NestedFactory : User can utilise accidentally sent ETH funds via processOutputOrders() / processInputAndOutputOrders()

The NestedFactory contract accepts plain ETH transfers from any sender and its order processing functions do not verify that ETH input comes from the reserve, allowing anyone to create an output order using the contract's accidentally sent ETH balance. An attacker could thereby convert stray ETH into tokens for their own benefit.

GreyArt Follow

Originally disclosed on Code4rena

Submitted by GreyArt

NestedFactory.sol#L71 NestedFactory.sol#L286-L296 NestedFactory.sol#L370-L375 NestedFactory.sol#L482-L492

Should a user accidentally send ETH to the NestedFactory, anyone can utilise it to their own benefit by calling processOutputOrders() / processInputAndOutputOrders(). This is possible because:

  1. receive() has no restriction on the sender
  2. processOutputOrders() does not check msg.value, and rightly so, because funds are expected to come from reserve.
  3. transferInputTokens() does not handle the case where ETH could be specified as an address by the user for an output order.
if (address(_inputToken) == ETH) {
  require(address(this).balance >= _inputTokenAmount, "NF: INVALID_AMOUNT_IN");
  weth.deposit{ value: _inputTokenAmount }();
  return (IERC20(address(weth)), _inputTokenAmount);
}

Hence, the attack vector is simple. Should a user accidentally send ETH to the contract, create an output Order with token being ETH and amount corresponding to the NestedFactory’s ETH balance.

Recommended Mitigation Steps

  1. Since plain / directETH transfers are only expected to solely come from weth (excluding payable functions), we recommend restricting the sender to be weth, like how it is done in [FeeSplitter](https://github.com/code-423n4/2022-02-nested/blob/main/contracts/FeeSplitter.sol#L101-L104).

    We are aware that this was raised previously here: code-423n4/2021-11-nested-findings#188 and would like to add that restricting the sender in the receive() function will not affect payable functions. From from what we see, plain ETH transfers are also not expected to come from other sources like NestedReserve or operators.

receive() external payable {
  require(msg.sender == address(weth), "NF: ETH_SENDER_NOT_WETH");
}
  1. Check that _fromReserve is false in the scenario address(_inputToken) == ETH.
if (address(_inputToken) == ETH) {
  require(!_fromReserve, "NF: INVALID_INPUT_TOKEN");
  require(address(this).balance >= _inputTokenAmount, "NF: INVALID_AMOUNT_IN");
  weth.deposit{ value: _inputTokenAmount }();
  return (IERC20(address(weth)), _inputTokenAmount);
}

maximebrugel (Nested Finance) confirmed and resolved:

PR: Med/High Risk Fixes


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

Likely the contract's receive function accepts ETH from any address and the order processing logic does not ensure that ETH used as input originates from the reserve, so any ETH balance held by the contract can be spent by an attacker.

Impact

An attacker could take any ETH accidentally sent to the contract and use it for their own orders, causing a loss to the original sender or the protocol.

What the attacker needed

ETH accidentally sent to the contract; attacker can call order processing functions

The fix

The receive function should be restricted to accept ETH only from the WETH contract, and the ETH input path should require that the funds are not coming from the reserve.

How it was found

  1. A user accidentally sends ETH to the NestedFactory contract.
  2. An attacker calls processOutputOrders() or processInputAndOutputOrders() and creates an output order with token set to ETH and an amount equal to the contract's ETH balance.
  3. The contract uses its ETH balance to deposit into WETH and fulfil the order, benefiting the attacker.

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 smart contract can hold Ether (ETH) that people send to it by mistake. This report shows that the contract did not check who sent the ETH or where it came from when processing orders, so anyone could use that stray ETH to buy tokens for themselves. The fix is to only accept ETH from the approved wrapper contract and to reject ETH that did not come from the reserve.

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 GreyArt

View all

12 more disclosed reports, best first.

More from Nested Finance contest

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.