LibSwap: Excess funds from swaps are not returned
The swap function did not return excess input tokens when the actual amount used was less than the specified fromAmount, leaving funds stranded in the contract. An attacker could not directly steal others' funds, but users could lose their own excess tokens.
Submitted by hickuphh3, also found by 0xDjango, cccz, JMukesh, and Ruhum
It is probable for _swapData.fromAmount to be greater than the actual amount used (eg. when swapping for an exact output, or when performing another swap after swapping with an exact input). However, these funds aren’t returned back to the user and are left in the lifi contract.
Proof of Concept
AnyswapFacet.test.ts#L153-L194
The test referenced above swaps for MATIC for 1000 USDT exactly. Logging the matic amounts before and after the swap and bridge call, one will find 18.01 MATIC is unused and left in the contract when it should be returned to the user.
Recommended Mitigation Steps
Store the contract’s from balance before and after the swap. Refund any excess back to the user.
uint256 actualFromAmount = LibAsset.getOwnBalance(fromAssetId);
(bool success, bytes memory res) = _swapData.callTo.call{ value: msg.value }(_swapData.callData);
if (!success) {
string memory reason = LibUtil.getRevertMsg(res);
revert(reason);
}
actualFromAmount -= LibAsset.getOwnBalance(fromAssetId);
require(fromAmount >= actualFromAmount, 'actual amount used more than specified');
// transfer excess back to user
if (actualFromAmount != fromAmount) {
// transfer excess to user
// difference calculation be unchecked since fromAmount > actualFromAmount
}
This comes with the requirement that the funds for every swap should be pulled from the user.
H3xept (Li.Fi) resolved and commented:
Fixed in lifinance/lifi-contracts@4d66e5ad5f9a897d9f8a66eb7a4e765e0b6ff97c
maxklenk (Li.Fi) disagreed with High severity and commented:
We “disagree with severity” as this issue would not allow to access other users funds and only happens if the user passes these kind of swaps himself. The multi-swap feature is mainly used to allow swapping multiple different tokens into one, which is then fully swapped.
gzeon (judge) decreased severity to Medium and commented:
Agree with sponsor and adjusting this to 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
The contract did not track the actual amount of input tokens consumed during a swap and failed to refund any excess to the user.
Impact
Users could lose excess tokens that remain stuck in the contract, but the issue does not allow access to other users' funds and only occurs with certain swap types.
What the attacker needed
User performs a swap with excess input; swap uses less than fromAmount
The fix
The contract was fixed to store the balance before and after the swap and refund any excess to the user.
How it was found
- The test swaps for MATIC using exactly 1000 USDT.
- After the swap and bridge call, 18.01 MATIC remains unused in the contract.
- The excess is not returned 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.
When you swap tokens, you might send more than needed. The contract kept the extra instead of giving it back. This means users could lose money, but only their own, not others'.
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.