Skip to content
Business logic Audit finding Intermediate

All swapping functions lack checks for returned tokens

The swapping functions in GenericSwapFacet and LibSwap do not verify that tokens were actually received after a swap. An attacker could supply calldata that calls a non-swapping function on a whitelisted router, causing the transaction to succeed without returning tokens, while the contract has already taken the user's input tokens.

0xDjango Follow

Originally disclosed on Code4rena

Submitted by 0xDjango, also found by GeekyLumberjack and pmerkleplant

GenericSwapFacet.sol#L23-L30 LibSwap.sol#L48

Every function that stems from the GenericSwapFacet lacks checks to ensure that some tokens have been returned via the swaps. In LibSwap.sol in the swap() function, the swap call is sent to the target DEX. A return of success is required, otherwise the operation will revert.

Each “inner” swap via LibSwap.sol lacks output checks and also the “outer” swapTokensGeneric() via GenericSwapFacet.sol lacks a final check as well.

There is a possibility that the calldata is accidently populated with a function in the target router that is not actually performing any swapping functionality, getAmountsOut() for example. The function will return true, but no new returned tokens will be present in the contract. Meanwhile, the contract has already received the user’s fromTokens directly.

Recommended Mitigation Steps

This would be a potential use case of using function signature whitelists as opposed to contract address whitelists, as noted as a possibility by the LiFi team.

Otherwise, the following require statement in swapTokensGeneric() would ensure that at least a single token was received:

require(LibAsset.getOwnBalance(_swapData.receivingAssetId) - toAmount) > 0, "No tokens received")

H3xept (Li.Fi) resolved and commented:

Fixed in lifinance/lifi-contracts@3a42484dda8bafcfd122c8aa3b61d3766d545bf9

gzeon (judge) commented:

Sponsor confirmed with fix.


The original report as published on Code4rena, by 0xDjango. 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 functions only check that the external call succeeded, not that the expected tokens were received, and they allow any function on a whitelisted router to be called.

Impact

An attacker could cause a swap to succeed without receiving any tokens, while the contract has already received the user's input tokens, leading to loss of funds.

What the attacker needed

Whitelisted router with non-swap function; user initiates a swap

The fix

The issue was fixed by adding a check that at least one token was received, or by using function signature whitelists instead of contract address whitelists.

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 expect to get new tokens back. This code only checks that the swap call didn't fail, not that you actually received anything. So someone could trick the system into thinking a swap happened when it didn't, and you'd lose your tokens.

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 0xDjango

View all

23 more disclosed reports, best first.

More from LI.FI contest

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.