Skip to content
Business logic Audit finding Intermediate

RubiconRouter.swapEntireBalance() doesn’t handle the slippage check properly

The swapEntireBalance function in RubiconRouter ignores the user-supplied minimum output amount (buy_amt_min) and instead computes a lower value by subtracting a fee, so the slippage check is weaker than intended. An attacker could exploit this to cause users to receive fewer tokens than they specified, potentially leading to loss of funds.

Ruhum Follow

Originally disclosed on Code4rena

Submitted by Ruhum, also found by IllIllI, berndartmueller, eccentricexit, blackscale, and hansfriese

The swapEntireBalance() function allows the user to pass a buy_amt_min value which is the minimum number of tokens they should receive from the swap. But, the function doesn’t pass the value to the underlying swap() function. Thus, the user’s min value will be ignored. Since that will result in unexpected outcomes where user funds might be lost, I rate this issue as HIGH.

Proof of Concept

swapEntireBalance():

    function swapEntireBalance(
        uint256 buy_amt_min,
        address[] calldata route, // First address is what is being payed, Last address is what is being bought
        uint256 expectedMarketFeeBPS
    ) external returns (uint256) {
        //swaps msg.sender entire balance in the trade
        uint256 maxAmount = ERC20(route[0]).balanceOf(msg.sender);
        ERC20(route[0]).transferFrom(
            msg.sender,
            address(this),
            maxAmount // Account for expected fee
        );
        return
            _swap(
                maxAmount,
                maxAmount.sub(buy_amt_min.mul(expectedMarketFeeBPS).div(10000)), //account for fee
                route,
                expectedMarketFeeBPS,
                msg.sender
            );
    }

The second parameter of the _swap() call should be the min out value. Instead maxAmount.sub(buy_amt_min.mul(expectedMarketFeeBPS).div(10000)) is used.

Example:

amount = 100
buy_amt_min = 99
expectedMarketFeeBPS = 500 // 5%

actual buy_amy_min = 100 - (99 * (500 / 10000)) = 95.05

So instead of using 99 the function uses 95.05 which could result in the user receiving fewer tokens than they expected.

Recommended Mitigation Steps

Pass buy_amt_min directly to _swap().

bghughes (Rubicon) marked as duplicate:

Duplicate of #104.

HickupHH3 (judge) commented:

Not a duplicate. This has to do with applying a fee on buy_amt_min instead of passing the actual value directly. Lower slippage tolerance means potential loss of funds, hence the high severity.


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

The function incorrectly calculates the minimum output by subtracting a fee from the user's minimum instead of passing the user's minimum directly to the swap.

Impact

Users could receive fewer tokens than their specified minimum, leading to potential loss of funds.

What the attacker needed

User calls swapEntireBalance; user sets a minimum output amount

The fix

Pass buy_amt_min directly to the _swap() function.

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 swap tokens, you can set a minimum amount you are willing to receive to protect against bad prices. This report says the function that swaps your entire balance ignores that minimum and uses a smaller number instead, so you might get less than you asked for.

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 Ruhum

View all

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