Skip to content
Business logic Audit finding Intermediate

Users are incorrectly refunded when liquidity is insufficient

The swap function takes the actual input amount from the user but then also refunds the difference between the original requested amount and the actual input, even though that difference was never taken. An attacker could repeatedly swap to drain the pool's funds through these unnecessary refunds.

ZanyBonzy Follow with Q7, Tigerfrake and 1 other

Originally disclosed on Code4rena

Submitted by ZanyBonzy, also found by Q7, Tigerfrake, and DadeKuma

In swap_2_internal, if the first pool doesn’t have enough liquidity, amount_in could be less than original_amount, and as expected, amount_in is taken from swapper. But the function still refunds original_amount - amount_in to the user if original_amount is more than amount_in.

From the function, we can see than amount_in is taken from swapper. Then the function checks if original_amount is more than amount_in, before which the difference is transferred back to the sender.

>>      erc20::take(from, amount_in, permit2)?;
        erc20::transfer_to_sender(to, amount_out)?;

>>      if original_amount > amount_in {
            erc20::transfer_to_sender(
                to,
                original_amount
>>                  .checked_sub(amount_in)
                    .ok_or(Error::TransferToSenderSub)?,
            )?;
        }

An unnecessary refund is processed leading to loss of funds for the protocol. Malicious users can take advantage of this to “rob” the protocol of funds through the refunds.

Recommended Mitigation Steps

No need to process refunds since amount_in is already taken.

        erc20::take(from, amount_in, permit2)?;
        erc20::transfer_to_sender(to, amount_out)?;

-       if original_amount > amount_in {
-           erc20::transfer_to_sender(
-               to,
-               original_amount
-                   .checked_sub(amount_in)
-                   .ok_or(Error::TransferToSenderSub)?,
-           )?;
        }

Assessed type

Context

af-afk (Superposition) confirmed

0xsomeone (judge) commented:

The submission and its duplicates have correctly identified that the refund process in the swap_2_internal_erc20 function is extraneous and thus results in excess funds being sent to the user.

I believe a high-risk severity rating is appropriate as the issue manifests itself in all cases and would result in direct fund loss for the AMM pair.

af-afk (Superposition) commented:

For Issue #12 @0xsomeone how does this compare to your findings here?

0xsomeone (judge) commented:

@af-afk - I am unsure what comparison is to be drawn here. None of the findings are mine as I am a judge, and I do not believe that the finding referenced has any relation to this one when it comes to impact.

af-afk (Superposition) commented:

Sorry, I should clarify, I mean your assessment that both are valid. It’s not possible for both of these to be correct, right? I’m of the opinion that this refund should not be implemented after consideration (and this submission) since the contract’s quoting functionality should indicate that this is taking place.

0xsomeone (judge) commented:

@af-afk - the original submission shared was submitted in a audit that relies on a different commit hash from this one. As we can observe in the highlighted code segment, the code originally transferred the original_amount from the from address.

In the remediated code that was part of this audit, the code was updated to simultaneously extract the amount_in from the user and perform a refund. The incorrect aspect is that two different solutions for the same problem were incorporated, rendering the refund to be extraneous. I hope this clears things up!

af-afk (Superposition) commented:

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


The original report as published on Code4rena, by ZanyBonzy. 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 code both takes the actual input amount and also refunds the difference from the original amount, duplicating the refund logic.

Impact

The protocol loses funds because excess refunds are sent to users, and an attacker could drain the pool; the judge rated it high risk as it affects all swaps.

What the attacker needed

Ability to call swap function; pool with insufficient liquidity for requested amount

The fix

The refund block should be removed because the input amount is already taken from the user.

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, the system should only take what it needs and give you back any unused amount. Here, it took the smaller amount but also sent you a refund as if it had taken the larger amount, so you get free tokens from the pool. Doing this repeatedly empties the pool.

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 ZanyBonzy

View all

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