Skip to content
Business logic Audit finding Advanced

Missing fromToken != toToken check

The contract fails to check that the input and output tokens are different, allowing an attacker to call a swap function with the same token and a fake pair contract that returns zero output. This lets the attacker withdraw funds from the contract without proper accounting, bypassing a time-lock.

Marginswap

Originally disclosed on Code4rena

Attacker calls MarginRouter.crossSwapExactTokensForTokens with a fake pair and the same token[0] == token[1]. crossSwapExactTokensForTokens(1000 WETH, 0, [ATTACKER_CONTRACT], [WETH, WETH]). When the amounts are computed by the amounts = UniswapStyleLib.getAmountsOut(amountIn - fees, pairs, tokens); call, the attacker contract returns fake reserves that yield 0 output. When _swapExactT4T is called, the funds are sent to the fake contract and doing nothing passes all checks in _swap call that follows because the startingBalance is stored after the initial Fund withdraw to the pair.

function _swapExactT4T() {
  // withdraw happens here
    Fund(fund()).withdraw(tokens[0], pairs[0], amounts[0]);
    _swap(amounts, pairs, tokens, fund());
}

function _swap() {
  uint256 startingBalance = IERC20(outToken).balanceOf(_to);
  uint256 endingBalance = IERC20(outToken).balanceOf(_to);
  // passes as startingBalance == endingBalance + 0
  require(
      endingBalance >= startingBalance + amounts[amounts.length - 1],
      "Defective AMM route; balances don't match"
  );
}

The full impact is not yet known as registerTrade could still fail when subtracting the inAmount and adding 0 outAmount. At least, this attack is similar to a withdrawal which is supposed to only occur after a certain coolingOffPeriod has passed, but this time-lock is circumvented with this attack.

Recommend moving the fund withdrawal to the first pair after the startingBalance assignment. Check fromToken != toToken as cyclical trades (arbitrages) are likely not what margin traders are after. Consider if the same check is required for registerTradeAndBorrow / adjustAmounts functions.

The original report as published on Code4rena. 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 does not verify that the input and output tokens are distinct, and it records the starting balance after withdrawing funds, so a zero-output swap passes the balance check.

Impact

The attacker could withdraw funds without waiting for the cooling-off period, though the full impact is not yet known because registerTrade might still fail.

What the attacker needed

Ability to deploy a fake pair contract; ability to call the swap function

The fix

The fund withdrawal should be moved to after the starting balance is recorded, and a check should be added to ensure the input and output tokens are different.

How it was found

  1. Attacker deploys a fake pair contract that returns reserves yielding zero output.
  2. Attacker calls crossSwapExactTokensForTokens with the same token as input and output, and the fake pair.
  3. The contract withdraws funds to the fake pair and then checks balances, which pass because the starting balance is recorded after the withdrawal.

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 lets users swap tokens, but it doesn't check that the two tokens are different. An attacker can pretend to swap a token for itself using a fake exchange that gives back nothing, and the contract still sends out real funds. This bypasses a waiting period meant to prevent quick withdrawals.

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 from Marginswap

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.