Skip to content
Business logic Audit finding Intermediate

User will loose funds

A swap function accepts a route array containing only one token, so the internal swap loop never executes and returns zero. A user who also sets a minimum buy amount of zero can have their deposited funds taken while receiving nothing back.

Anurag Jain Follow

Originally disclosed on Code4rena

Submitted by csanuragjain, also found by 0x1f8b

User will lose funds if user accidentally pass route with only 1 value which is route[0]=X WETH while calling swapForETH or swapWithETH/swapEntireBalance/swap function.

Proof of Concept

  1. User calls swapForETH function with below params:
pay_amt=500
buy_amt_min=0
route[0]=WETH
expectedMarketFeeBPS=1
  1. User will transfer 500+fees amount to the contract
require(
            ERC20(route[0]).transferFrom(
                msg.sender,
                address(this),
                pay_amt.add(pay_amt.mul(expectedMarketFeeBPS).div(10000))
            ),
            "initial ERC20 transfer failed"
        );
  1. Now _swap function is called. This function will do nothing and loop will not run due to condition failure
for (uint256 i = 0; i < route.length - 1; i++)

// here since route.length - 1 is 1-1=0 so loop will not run as i=0 and 0<0 is false
  1. Since currentAmount will be 0 and buyamtmin is also 0 so require(currentAmount >= buyamtmin, “didnt clear buyamtmin”); will pass and 0 will be returned back
  2. Swap is complete and user will not receive anything

Note:

The same need to be fixed for swapWithETH,swapEntireBalance,swap function as well

Recommended Mitigation Steps

Add below check

require(route.length>1, "Invalid route param");

bghughes (Rubicon) disagreed with severity and commented:

Medium risk because it’s user error.

pauliax (warden) commented:

I think the problem is created by the warden by specifying buy_amt_min=0. If a slippage of 0 is specified then you basically anticipate that you may receive nothing. While enforcing min route length is a good suggestion, I do not think it should be that severe.

HickupHH3 (judge) decreased severity to Medium and commented:

2 user errors have to be made for this to happen:

  • User specifies only 1 token in the route: WETH
  • Zero slippage: buy_amt_min = 0

Hence, because of these prerequisites, I will downgrade the issue to medium severity.


The original report as published on Code4rena, by csanuragjain. 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 swap loop iterates only while the index is less than route length minus one, so a single-entry route skips the loop entirely and the function never validates that the route has at least two tokens.

Impact

A user could lose the deposited tokens and fees while receiving nothing, but only if they both supply a single-token route and set zero slippage; the judge rated this medium because two user errors are required.

What the attacker needed

User supplies a one-token route; user sets minimum buy amount to zero

The fix

The swap functions should be fixed by rejecting routes that contain fewer than two tokens.

How it was found

  1. Call the swap function with a route containing only one token, a pay amount, a minimum buy amount of zero and a market fee value.
  2. The contract pulls the pay amount plus fees from the caller into itself.
  3. The internal swap loop does not run because the route has only one entry.
  4. The minimum-output check passes since both the current amount and the minimum are zero, and zero is returned.
  5. The swap finishes and the user receives nothing.

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 swap contract moves one token into another by following a list of steps called a route. If that list has only one entry, the contract has nothing to swap through, so it does nothing but still takes the user's tokens. Because the user also set the minimum acceptable output to zero, the contract's safety check passes and the user ends up with nothing.

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 Anurag Jain

View all

63 more disclosed reports, best first.

More from Rubicon v2

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.