msg.value is Sent Multipletimes When Performing a Swap
The contract's swap function repeatedly sends the same msg.value to external calls for each swap in a batch, allowing an attacker to drain the contract's native token balance by making multiple swaps from the native token. The attacker receives multiple times their original msg.value in swapped tokens.
Originally disclosed on Code4rena
Submitted by kirk-baird, also found by hyh, rayn, TomFrenchBlockchain, VAD37, WatchPug, and wuwe1
LibSwap.sol#L42 Swapper.sol#L12-L23
msg.value is attached multiple times to external swap calls in LibSwap.swap().
If Swapper._executeSwaps() is called with the native token as the swapData.fromAssetId more than once and msg.value > 0 then more value will be transferred out of the contract than is received since msg.value will be transferred out _swapData.length times.
The impact is that the contract can have all the native token balance drained by an attacker who has makes repeated swap calls from the native token into any other ERC20 token. Each time the original msg.value of the sender will be swapped out of the contract. This attack essentially gives the attacker _swapData.length * msg.value worth of native tokens (swapped into another ERC20) when they should only get msg.value.
Proof of Concept
Swapper._executeSwaps() iterates over a list of SwapData calling LibSwap.swap() each time (note this is an internal call).
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]);
}
}
}
Inside LibSwap.swap() we make an external call to _swapData.callTo with value : msg.value. Due to the loop in Swapper._executeSwaps() this repeatedly sends the original msg.value in the external call.
(bool success, bytes memory res) = _swapData.callTo.call{ value: msg.value }(_swapData.callData);
Recommended Mitigation Steps
This issue may be mitigated by only allowing fromAssetId to be the native token once in _swapData in Swapper._executeSwaps(). If it occurs more than once the transaction should revert.
H3xept (Li.Fi) acknowledged, but disagreed with High severity 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).
gzeon (judge) decreased severity to Medium and commented:
Adjusting this as Medium 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. Not a duplicate of M-02 since
msg.valueis the vector here.
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 contract uses msg.value inside a loop, so the same native token amount is forwarded on each iteration instead of being consumed once.
Impact
An attacker could drain the contract's native token balance, but the maximum loss is limited to leftover funds and slippage, and the issue was fixed by sweeping latent funds back to the user.
What the attacker needed
Contract holds native token balance; attacker can call swap with native token
The fix
The issue was fixed by sweeping any latent funds back to the user's wallet in the most recent version of Swapper.sol.
How it was found
- Call Swapper._executeSwaps() with swapData where fromAssetId is the native token more than once and msg.value > 0.
- The function loops over each swapData and calls LibSwap.swap().
- Each LibSwap.swap() makes an external call with value: msg.value, sending the same amount multiple times.
- The attacker receives _swapData.length * msg.value worth of native tokens swapped into another ERC20.
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 send cryptocurrency to a smart contract, the amount is called msg.value. This contract's swap function used that same amount for every swap in a batch, so if you asked for several swaps, it sent your money multiple times. An attacker could exploit this to take more money from the contract than they put in.
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.