Skip to content
Business logic Audit finding Advanced

User can bypass entryFee by sending arbitrary calldata to ParaSwap operator

The NestedFactory contract allowed users to bypass entry fees by supplying arbitrary calldata to the ParaSwap operator, which could be manipulated to swap a tiny amount of input token for a large amount of output token via a malicious ERC777 token. An attacker could thus deposit a large amount into the vault without paying the intended entry fee.

0xDjango Follow

Originally disclosed on Code4rena

Submitted by 0xDjango

NestedFactory.sol#L466 ParaswapOperator.sol#L34

Any user is able to bypass the entryFee collection when using NestedFactory.create() by passing in arbitrary calldata when using the ParaSwap router. High level, a user can pass in calldata to swap from a miniscule amount of input token to an ERC777 with themselves as the recipient and will gain control of execution, at which time they can send a large amount of output token back to the Nested Factory.

If the user sends 1 wei of input token, the Nested Factory will return an entryFee of 0 due to precision loss. The amount of output token returned to the contract via the direct transfer from the user will then be deposited in the vault.

Proof of Concept

Steps

  • User calls NestedFactory.create() with a single input order. This input order will define the parameters of the call to Paraswap.

  • The single order defines the following in pseudocode:

    1. inputToken: Any token, but we’ll use address(0) ETH
    2. amount: 1 wei
    3. Order(operator=Paraswap, token=USDC, calldata=calldata)

The calldata used in the call to paraswap would swap from ETH to any ERC777 (NOT USDC), with an attack contract address set as the beneficiary. Upon transferring the swapped ERC777 to the user’s attack contract, the contract would immediately send e.g. 1,000,000 USDC directly back to the Nested Factory contract.

  • The Paraswap operator checks the balances of the buy and sell tokens. Note that the buy token is defined in the Order token parameter, not the calldata passed to Paraswap. Since the operator will check the balance of USDC, it looks like we’ve swapped 1 wei ETH for 1,000,000 USDC.
  • The Paraswap operator returns the swap amounts back to Nested Factory.
  • Nested Factory deposits the 1,000,000 USDC to the vault for the user without charging any entryFee.

NOTE: I use 1 wei as an extreme example. You would have to ensure that you’re swapping at least enough to receive 1 wei of the ERC777 to transfer to the attack contract.

Recommended Mitigation Steps

Allowing a user to pass arbitrary call data to a router is risky because routers allow several paths for an attacker to gain control of execution. Originally, I believed this exploit to be possible simply by swapping to ETH, which would perform an external call to the beneficiary, but Paraswap actually only forwards 10,000 gas when performing ETH transfers. If Nested plans to include a vanilla Uniswap router operator, this would be an issue. Here is the Paraswap transfer logic:

function transferTokens(
        address token,
        address payable destination,
        uint256 amount
    )
    internal
    {
        if (amount > 0) {
            if (token == ETH_ADDRESS) {
                (bool result, ) = destination.call{value: amount, gas: 10000}("");
                require(result, "Failed to transfer Ether");
            }
            else {
                IERC20(token).safeTransfer(destination, amount);
            }
        }

    }

Therefore, it might be worth exploring the option of allowing the user to only choose from a list of predefined function signatures when making calls to Paraswap. The final Order param that is passed to the operator would be built within the contract by concatenating the function, input, and output tokens. Even then, if the output token truly is an ERC777, the user would be able to intercept control and directly transfer more of the ERC777.

A large-scale fix would be to charge the entry fee on the amount of output tokens after performing the swap. I’m not sure if this falls in line with Nested’s plans though.

maximebrugel (Nested) acknowledged

Jack the Pug (judge) commented:

I find this to be a valid Medium issue. I have a few things to add:

  1. It applies not only to the ParaSwap operator but also the other operators, ie, 0x;
  2. Not just the entryFee, the exitFees can also be bypassed in a similar way;
  3. Not necessarily using a ERC777, the attacker can also use a malicious ERC20 they deployed on their own;

The root cause is that:

entryFee and exitFees should be charged on the token that gets in and out the Reserve, not the inputToken/outputToken of the swap.


The original report as published on Code4rena, by 0xDjango. 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 entry fee is calculated on the input token amount before the swap, and the operator trusts the user-supplied calldata and token balances, allowing manipulation via a malicious token.

Impact

An attacker could bypass the entry fee and deposit a large amount into the vault, potentially causing financial loss to the platform; the judge noted that exit fees could also be bypassed and other operators are affected.

What the attacker needed

A user account; ability to deploy a malicious contract; use of ParaSwap operator

The fix

The entry and exit fees should be charged on the tokens that enter and leave the Reserve, not on the swap's input or output tokens, and users should be restricted to predefined function signatures.

How it was found

  1. User calls NestedFactory.create() with a single input order specifying a tiny amount of input token and a calldata for ParaSwap that swaps to an ERC777 token with the attacker's contract as beneficiary.
  2. The attacker's contract receives the ERC777 tokens and sends a large amount of the desired output token directly to the Nested Factory contract.
  3. The ParaSwap operator checks the balance of the buy token (USDC) and sees a large amount, believing the swap produced it.
  4. The Nested Factory deposits the large amount of USDC into the vault for the user without charging any entry fee.

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 deposit funds into a vault, the platform charges a small fee. This report shows that by tricking the platform's swap mechanism, you could avoid paying that fee and still deposit a large amount. The trick involved using a special token that lets the attacker run code during the swap, which they used to send extra funds directly to the vault.

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 0xDjango

View all

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