Anyone can get swaps for free given certain conditions in swap .
The swap function only transfers tokens from the user if the contract's own balance is less than the requested amount, so an attacker can swap using tokens already held by the contract. This allows the attacker to take any leftover ERC20 tokens that have accumulated in the contract.
Submitted by hake, also found by csanuragjain, hickuphh3, hyh, Kenshin, kirk-baird, obront, pmerkleplant, rayn, Ruhum, shw, tintin, VAD37, WatchPug, and wuwe1
LibSwap.swap GenericSwapFacet.sol
Remaining or unaccounted ERC20 balance could be freely taken through swapTokensGenerics and swap.
Proof of Concept
function swap(bytes32 transactionId, SwapData calldata _swapData) internal {
uint256 fromAmount = _swapData.fromAmount;
uint256 toAmount = LibAsset.getOwnBalance(_swapData.receivingAssetId);
address fromAssetId = _swapData.sendingAssetId;
if (!LibAsset.isNativeAsset(fromAssetId) && LibAsset.getOwnBalance(fromAssetId) < fromAmount) {
LibAsset.transferFromERC20(fromAssetId, msg.sender, address(this), fromAmount);
}
if (!LibAsset.isNativeAsset(fromAssetId)) {
LibAsset.approveERC20(IERC20(fromAssetId), _swapData.approveTo, fromAmount);
}
// solhint-disable-next-line avoid-low-level-calls
(bool success, bytes memory res) = _swapData.callTo.call{ value: msg.value }(_swapData.callData);
if (!success) {
string memory reason = LibUtil.getRevertMsg(res);
revert(reason);
}
toAmount = LibAsset.getOwnBalance(_swapData.receivingAssetId) - toAmount;
Given:
- There has been a deposit to LiFi of a non-native ERC20 that makes
LibAsset.getOwnBalance(fromAssetId)a desirable amount. - Attacker calls
swapTokensGenericwith a_swapData.fromAmountvalue just belowLibAsset.getOwnBalance(fromAssetId). - First
ifstatement inswapis skipped (no funds are tranferred to LiFis contract).
if (!LibAsset.isNativeAsset(fromAssetId) && LibAsset.getOwnBalance(fromAssetId) < fromAmount) {
LibAsset.transferFromERC20(fromAssetId, msg.sender, address(this), fromAmount);
}
- Swap happens and increases
LibAsset.getOwnBalance(_lifiData.receivingAssetId) - Difference of LiFis balance of the receiving token before and after swap is calculated using
postSwapBalanceand transfered to attacker.
Recommended Mitigation Steps
Ensure funds are always subtracted from users account in swap, even if LiFi has enough balance to do the swap.
H3xept (Li.Fi) acknowledged and commented:
We are aware that the contract allows users to use latent funds, although we disagree on it being an issue as no funds (ERC20 or native) should ever lay in the contract. To make sure that no value is ever kept by the diamond, we now provide refunds for outstanding user value (after bridges/swaps).
Keeping this as Med Risk. There can be fund leftover in the contract under normal operation, for example this tx. In fact, ~$300 worth of token is left in the LI.Fi smart contract on ETH mainnet 0x5a9fd7c39a6c488e715437d7b1f3c823d5596ed1 as of block 14597316. I don’t think this is High Risk because the max amount lost is no more than allowed slippage, which can be loss to MEV too.
ezynda3 (Li.Fi) resolved and commented:
This has been fixed in the most recent version of
src/Helpers/Swapper.solwhich sweeps any latent funds back to the user’s wallet.
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 swap function only pulls tokens from the user when the contract's own balance is insufficient, allowing the use of latent funds without deducting from the user's account.
Impact
An attacker could take any ERC20 tokens left in the contract, though the amount is limited to the contract's latent balance and the judge noted the maximum loss is no more than allowed slippage.
What the attacker needed
Contract holds leftover ERC20 tokens; attacker can call swapTokensGeneric
The fix
The swap function should always deduct the swap amount from the user's account, even if the contract already holds enough tokens; the project later fixed this by sweeping latent funds back to the user.
How it was found
- Ensure the contract holds a non-native ERC20 token balance.
- Call swapTokensGeneric with a fromAmount just below the contract's balance of that token.
- The transfer from the user is skipped because the contract already has enough tokens.
- The swap executes and increases the contract's balance of the receiving token.
- The difference in the receiving token's balance is transferred to the attacker.
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 holds some tokens that belong to other users. The swap function checks if the contract has enough tokens to cover the swap, and if so, it does not take tokens from the person making the swap. An attacker can therefore make a swap without paying, effectively stealing the leftover 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.