Skip to content
Business logic Audit finding Intermediate

Incorrect slippage handling in swap_internal()

The swap slippage check in swap_internal() used a logical OR, so a swap was accepted when only one of the two output amounts was above zero. An attacker or unlucky user could complete a swap that returns nothing of the token they wanted while still passing validation.

Tigerfrake Follow

Originally disclosed on Code4rena

Submitted by Tigerfrake

In the swap_internal() function, the slippage check uses the || operator to validate the swap results. This can lead to a scenario where one of the amounts (amount_0_abs or amount_1_abs) is allowed to be zero, potentially resulting in unwanted slippage.

    assert_or!(
        amount_0_abs > U256::zero() || amount_1_abs > U256::zero(), // Problematic operator
        Error::SwapResultTooLow
    );

Using the || operator allows the swap to proceed even if one of the amounts is zero, which could lead to unacceptable slippage.

Scenario:

  • Consider a user swapping 100 units of token A (amount_0) for token B (amount_1).
  • Due to slippage, token B’s output (amount_1_abs) becomes zero, while token A’s output (amount_0_abs) remains positive.
  • With the current || operator, the swap would still be considered valid since one amount is greater than zero, even though the user receives no token B (amount_1_abs = 0), resulting in a poor outcome for the user.

Impact

Using the || operator means that one token amount can be zero while the other passes the check, leading to an imbalanced swap that might not meet user expectations.

Recommended Mitigation Steps

Replace the || operator with the && operator to ensure both token amounts are greater than zero.

    assert_or!(
-       amount_0_abs > U256::zero() || amount_1_abs > U256::zero(),
+       amount_0_abs > U256::zero() && amount_1_abs > U256::zero(),
        Error::SwapResultTooLow
    );

Assessed type

Invalid Validation

af-afk (Superposition) confirmed

0xsomeone (judge) commented:

The Warden has identified an incorrect conditional clause that would permit either zero-input non-zero output swaps or non-zero input zero output ones, the latter of which may occur in a realistic scenario and would be unacceptable for the user.

I consider a medium-risk severity rating to be acceptable for this behavior as it would solely manifest in low transaction amounts and high value discrepancy AMM pairs.

af-afk (Superposition) commented:

Fixed: https://github.com/fluidity-money/long.so/commit/b0d39cf8d1be2096cba9c845e424b17c958847c5


The original report as published on Code4rena, by Tigerfrake. 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 validation combined the two amount checks with a logical OR instead of a logical AND, so a single non-zero amount satisfied the condition.

Impact

A swap could complete with one output amount at zero, giving the user a poor result; the judge noted this would mainly appear with low transaction amounts and high value discrepancy AMM pairs.

What the attacker needed

A swap on an AMM pair with high value discrepancy; low transaction amounts

The fix

The check was changed to require both amounts to be greater than zero, and the project confirmed it was fixed.

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 lets you trade one token for another. Before accepting the trade, the code checks that you actually received something back. The check was written so that getting a positive amount of just one token was enough, meaning you could hand over tokens and receive nothing of the one you asked for. The fix requires both amounts to be above zero.

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 Tigerfrake

View all

4 more disclosed reports, best first.

More from Superposition

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.