Swap functions are Reenterable
Several swap functions in the LiFi contracts record a token balance before and after executing external swap calls, then transfer the difference to the caller. Because the swap calls can re-enter the contract, an attacker could manipulate the post-swap balance calculation by calling functions like withdraw during the swap, potentially inflating the amount transferred.
Originally disclosed on Code4rena
Submitted by kirk-baird, also found by ACai, hake, and rayn
DiamondCutFacet.sol#L14-L22 CBridgeFacet.sol#L92-L121 AnyswapFacet.sol#L74-L110 NXTPFacet.sol#L85-L102 NXTPFacet.sol#L150-L171
There is a reenterancy vulnerability in functions which call Swapper._executeSwap() which would allow the attacker to change their postSwapBalance.
The functions following similar logic to that seen in GenericSwapFacet.swapTokensGeneric().
uint256 receivingAssetIdBalance = LibAsset.getOwnBalance(_lifiData.receivingAssetId);
// Swap
_executeSwaps(_lifiData, _swapData);
uint256 postSwapBalance = LibAsset.getOwnBalance(_lifiData.receivingAssetId) - receivingAssetIdBalance;
LibAsset.transferAsset(_lifiData.receivingAssetId, payable(msg.sender), postSwapBalance);
This logic records the balance before and after the _executeSwaps() function. The difference is then transferred to the msg.sender.
The issue occurs since it is possible for an attacker to reenter this function during _executeSwaps(), that is because execute swap makes numerous external calls, such as to the AMM, or to untrusted ERC20 token addresses.
If a function is called such as WithdrawFacet.withdraw() this will impact the calculations of postSwapBalance which will account for the funds transferred out during withdrawal. Furthermore, any functions which transfers funds into the contract will also be counted in the postSwapBalance calculations.
Vulnerable Functions:
GenericSwapFacet.swapTokensGeneric()CBridgeFacet.swapAndStartBridgeTokensViaCBridge()AnyswapFacet.swapAndStartBridgeTokensViaAnyswap()HopFacet.swapAndStartBridgeTokensViaHop()NXTPFacet.swapAndStartBridgeTokensViaNXTP()NXTPFacet.swapAndCompleteBridgeTokensViaNXTP()
Proof of Concept
GenericSwapFacet.swapTokensGeneric()
function swapTokensGeneric(LiFiData memory _lifiData, LibSwap.SwapData[] calldata _swapData) public payable {
uint256 receivingAssetIdBalance = LibAsset.getOwnBalance(_lifiData.receivingAssetId);
// Swap
_executeSwaps(_lifiData, _swapData);
uint256 postSwapBalance = LibAsset.getOwnBalance(_lifiData.receivingAssetId) - receivingAssetIdBalance;
LibAsset.transferAsset(_lifiData.receivingAssetId, payable(msg.sender), postSwapBalance);
CBridgeFacet.swapAndStartBridgeTokensViaCBridge()
function swapAndStartBridgeTokensViaCBridge(
LiFiData memory _lifiData,
LibSwap.SwapData[] calldata _swapData,
CBridgeData memory _cBridgeData
) public payable {
if (_cBridgeData.token != address(0)) {
uint256 _fromTokenBalance = LibAsset.getOwnBalance(_cBridgeData.token);
// Swap
_executeSwaps(_lifiData, _swapData);
uint256 _postSwapBalance = LibAsset.getOwnBalance(_cBridgeData.token) - _fromTokenBalance;
require(_postSwapBalance > 0, "ERR_INVALID_AMOUNT");
_cBridgeData.amount = _postSwapBalance;
} else {
uint256 _fromBalance = address(this).balance;
// Swap
_executeSwaps(_lifiData, _swapData);
uint256 _postSwapBalance = address(this).balance - _fromBalance;
require(_postSwapBalance > 0, "ERR_INVALID_AMOUNT");
_cBridgeData.amount = _postSwapBalance;
}
_startBridge(_cBridgeData);
AnyswapFacet.swapAndStartBridgeTokensViaAnyswap()
function swapAndStartBridgeTokensViaAnyswap(
LiFiData memory _lifiData,
LibSwap.SwapData[] calldata _swapData,
AnyswapData memory _anyswapData
) public payable {
address underlyingToken = IAnyswapToken(_anyswapData.token).underlying();
if (_anyswapData.token != address(0) && underlyingToken != IAnyswapRouter(_anyswapData.router).wNATIVE()) {
if (underlyingToken == address(0)) {
underlyingToken = _anyswapData.token;
}
uint256 _fromTokenBalance = LibAsset.getOwnBalance(underlyingToken);
// Swap
_executeSwaps(_lifiData, _swapData);
uint256 _postSwapBalance = LibAsset.getOwnBalance(underlyingToken) - _fromTokenBalance;
require(_postSwapBalance > 0, "ERR_INVALID_AMOUNT");
_anyswapData.amount = _postSwapBalance;
} else {
uint256 _fromBalance = address(this).balance;
// Swap
_executeSwaps(_lifiData, _swapData);
require(address(this).balance - _fromBalance >= _anyswapData.amount, "ERR_INVALID_AMOUNT");
uint256 _postSwapBalance = address(this).balance - _fromBalance;
require(_postSwapBalance > 0, "ERR_INVALID_AMOUNT");
_anyswapData.amount = _postSwapBalance;
}
_startBridge(_anyswapData);
HopFacet.swapAndStartBridgeTokensViaHop()
function swapAndStartBridgeTokensViaHop(
LiFiData memory _lifiData,
LibSwap.SwapData[] calldata _swapData,
HopData memory _hopData
) public payable {
address sendingAssetId = _bridge(_hopData.asset).token;
uint256 _sendingAssetIdBalance = LibAsset.getOwnBalance(sendingAssetId);
// Swap
_executeSwaps(_lifiData, _swapData);
uint256 _postSwapBalance = LibAsset.getOwnBalance(sendingAssetId) - _sendingAssetIdBalance;
require(_postSwapBalance > 0, "ERR_INVALID_AMOUNT");
_hopData.amount = _postSwapBalance;
_startBridge(_hopData);
NXTPFacet.swapAndStartBridgeTokensViaNXTP()
function swapAndStartBridgeTokensViaNXTP(
LiFiData memory _lifiData,
LibSwap.SwapData[] calldata _swapData,
ITransactionManager.PrepareArgs memory _nxtpData
) public payable {
address sendingAssetId = _nxtpData.invariantData.sendingAssetId;
uint256 _sendingAssetIdBalance = LibAsset.getOwnBalance(sendingAssetId);
// Swap
_executeSwaps(_lifiData, _swapData);
uint256 _postSwapBalance = LibAsset.getOwnBalance(sendingAssetId) - _sendingAssetIdBalance;
require(_postSwapBalance > 0, "ERR_INVALID_AMOUNT");
_nxtpData.amount = _postSwapBalance;
_startBridge(_lifiData.transactionId, _nxtpData);
NXTPFacet.swapAndCompleteBridgeTokensViaNXTP()
function swapAndCompleteBridgeTokensViaNXTP(
LiFiData memory _lifiData,
LibSwap.SwapData[] calldata _swapData,
address finalAssetId,
address receiver
) public payable {
uint256 startingBalance = LibAsset.getOwnBalance(finalAssetId);
// Swap
_executeSwaps(_lifiData, _swapData);
uint256 postSwapBalance = LibAsset.getOwnBalance(finalAssetId);
uint256 finalBalance;
if (postSwapBalance > startingBalance) {
finalBalance = postSwapBalance - startingBalance;
LibAsset.transferAsset(finalAssetId, payable(receiver), finalBalance);
}
emit LiFiTransferCompleted(_lifiData.transactionId, finalAssetId, receiver, finalBalance, block.timestamp);
}
Recommended Mitigation Steps
Consider adding a reentrancy guard over every function which may send or receive tokens. It may be easiest too add this guard over the fallback() function however that could prevent view functions from being called (since it would perform storage operations).
Ensure the same slot is used to store the reentrancy guard so all required functions are covered by a single guard.
Bridge functions are vulnerable as well.
H3xept (Li.Fi) resolved and commented:
Fixed in lifinance/lifi-contracts@703919f74d8b750e3bcf7a84bdcb4d742bc8d45a
Sponsor confirmed with fix. While the reentrancy is valid there is no exploit, keeping this as Medium Risk.
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 perform external calls (to AMMs or tokens) before completing their balance-based accounting, without a reentrancy guard, allowing state changes during the swap to affect the post-swap balance calculation.
Impact
An attacker could potentially manipulate the post-swap balance to receive more funds than intended, though the judge noted no exploit was demonstrated and kept the severity at Medium.
What the attacker needed
Attacker can trigger a callback during swap; victim contract holds funds
The fix
The issue was fixed by adding a reentrancy guard to all functions that may send or receive tokens, using a shared storage slot.
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 reentrancy attack happens when a contract calls another contract, and that other contract calls back into the first one before the first call finishes. Here, the swap functions check the contract's balance before and after making external calls, and then send the difference to the user. If an attacker can make the contract call back into itself during those external calls, they might change the balance in a way that tricks the contract into sending them more funds than they should get.