DexManagerFacet: batchRemoveDex() removes first dex only
The batchRemoveDex function in DexManagerFacet only removes the first DEX from the approved list because it uses a return statement instead of break inside a loop. An attacker or user could believe multiple DEXes were removed when only one was, leaving others still approved for swaps.
Submitted by hickuphh3, also found by 0xDjango, catchup, csanuragjain, hyh, shw, WatchPug, and ych18
The function intends to allow the removal of multiple dexes approved for swaps. However, the function will only remove the first DEX because return is used instead of break in the inner for loop.
if (s.dexs[j] == _dexs[i]) {
_removeDex(j);
// should be replaced with break;
return;
}
This error is likely to have gone unnoticed because no event is emitted when a DEX is added or removed.
Proof of Concept
Add the following lines below L44 of [AnyswapFacet.test.ts](https://github.com/code-423n4/2022-03-lifinance/blob/main/test/facets/AnyswapFacet.test.ts#L44)
await dexMgr.addDex(ANYSWAP_ROUTER)
await dexMgr.batchRemoveDex([ANYSWAP_ROUTER, UNISWAP_ADDRESS])
// UNISWAP_ADDRESS remains as approved dex when it should have been removed
console.log(await dexMgr.approvedDexs())
Recommended Mitigation Steps
Replace return with break.
if (s.dexs[j] == _dexs[i]) {
_removeDex(j);
break;
}
In addition, it is recommend to emit an event whenever a DEX is added or removed.
H3xept (Li.Fi) resolved and commented:
Fixed in lifinance/lifi-contracts@0a078bbbdf8ec92bd72efc4257900af416d537d4
Valid POC and sponsor confirmed with fix.
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 developer used a return statement instead of a break statement inside a nested loop, causing the function to exit after removing only the first matching DEX.
Impact
The function fails to remove all requested DEXes, leaving unauthorised DEXes approved for swaps, which could allow unintended token swaps.
What the attacker needed
Access to batchRemoveDex function; at least two approved DEXes
The fix
The fix was to replace return with break and to emit events when a DEX is added or removed.
How it was found
- Add a DEX to the approved list using addDex.
- Call batchRemoveDex with an array containing that DEX and another approved DEX.
- Observe that only the first DEX is removed and the second remains approved.
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.
This is a smart contract bug where a function meant to remove several items from a list only removes the first one. The code uses 'return' to exit the whole function early instead of 'break' to just stop the inner loop, so the remaining items are never processed. As a result, some DEXes (decentralised exchanges) stay approved even though the user asked to remove them.
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.