Skip to content
Business logic Audit finding Intermediate

Reliance on lifiData.receivingAssetId can cause loss of funds

The swap function used a user-supplied receiving asset identifier for the final transfer, which could differ from the asset actually received from the swap. If the two did not match, the swapped tokens remained stuck in the contract and the user lost them.

0xDjango Follow

Originally disclosed on Code4rena

Submitted by 0xDjango, also found by hake, kirk-baird, rayn, and shenwilly

GenericSwapFacet.sol#L23-L30

In the swapTokensGeneric() function, an arbitrary number of swaps can be performed from and to various tokens. However, the final balance that is sent to the user relies on _lifiData.receivingAssetId which has no use in the swapping functionality. LifiData is claimed to be used purely for analytical reasons per the comments to this function. If this value is input incorrectly, the swapped tokens will simply sit in the contract and be lost to the user.

Proof of Concept

Imagine a call to swapTokensGeneric() with the following parameters (excluding unnecessary parameters for this example):

  • LifiData.receivingAssetId = ‘0xUSDC_ADDRESS’

Single SwapData array:

  • LibSwap.SwapData.sendingAssetId = ‘0xWETH_ADDRESS’
  • LibSwap.SwapData.receivingAssetId = ‘0xDAI_ADDRESS’

Since the receivingAssetId from SwapData does not match the receivingAssetId from LifiData, the final funds will not be sent to the user after the swap is complete, based on the following lines of code:

uint256 receivingAssetIdBalance = LibAsset.getOwnBalance(_lifiData.receivingAssetId);

 _executeSwaps(_lifiData, _swapData);

 uint256 postSwapBalance = LibAsset.getOwnBalance(_lifiData.receivingAssetId) - receivingAssetIdBalance;

 LibAsset.transferAsset(_lifiData.receivingAssetId, payable(msg.sender), postSwapBalance);

Lines 1, 3, and 4 reference LifiData.receivingAssetId and handle the transfer of funds following the swaps. Line 2 performs the swap, referencing SwapData.receivingAssetId as can be seen in the executeSwaps() function definition:

function _executeSwaps(LiFiData memory _lifiData, LibSwap.SwapData[] calldata _swapData) internal {
        // Swap
        for (uint8 i; i < _swapData.length; i++) {
            require(
                ls.dexWhitelist[_swapData[i].approveTo] == true && ls.dexWhitelist[_swapData[i].callTo] == true,
                "Contract call not allowed!"
            );

            LibSwap.swap(_lifiData.transactionId, _swapData[i]);
        }
    }

Recommended Mitigation Steps

I recommend adding a check that _lifiData.receivingAssetId equals the receivingAssetId of the last index of the SwapData array, or simply use the receivingAssetId of the last index of the SwapData array for sending the final tokens to the user.

H3xept (Li.Fi) commented:

Fixed in lifinance/lifi-contracts@52aa2b8ea3bc51de3e60784c00201e29103fe250

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

The contract used the analytical `_lifiData.receivingAssetId` for the final balance check and transfer instead of the actual receiving asset from the last swap.

Impact

The user could lose the swapped funds because they remain in the contract instead of being transferred to the user.

What the attacker needed

User calls swapTokensGeneric; receivingAssetId mismatch

The fix

The contract was fixed by checking that the LifiData receiving asset matches the last SwapData receiving asset, or by using the last SwapData receiving asset for the final transfer.

How it was found

  1. Call swapTokensGeneric with a LifiData.receivingAssetId that differs from the SwapData.receivingAssetId.
  2. The contract records the balance of the LifiData asset before the swap.
  3. The swap executes and produces the SwapData asset.
  4. The contract computes the post-swap balance of the LifiData asset, which is unchanged, and transfers zero to 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.

A smart contract swap function lets users exchange one token for another. The contract decides which token to send back based on a value the user provides, but it does not check that this value matches the token actually produced by the swap. If the user gives the wrong value, the contract sends nothing and the swapped tokens stay trapped in the contract.

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.