Liquidations miss delegate call to swapper
The LiquidationRow contract calls the async swapper with a normal external call, so the tokens to be swapped are never transferred to the swapper and the swap reverts. As a result, liquidation cannot be performed and rewards collected through claimsVaultRewards become stuck in the contract.
0x007 Follow with 0x3b, 0x73696d616f and 24 others
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-06-tokemak-judging/issues/205
Found by
0x007, 0x3b, 0x73696d616f, 0xDjango, 0xJuda, 0xSurena, 0xTheC0der, 0xbepresent, 0xvj, ADM, Angry_Mustache_Man, Ch_301, Kalyan-Singh, MrjoryStewartBaxter, berndartmueller, bin2chen, duc, lil.eth, lodelux, nobody2018, p0wd3r, pengun, rvierdiiev, saidam017, shaka, talfao, xiaoming90
LiquidationRow acts as an orchestrator of claiming process. It liquidates tokens across vaults using the liquidateVaultsForToken function. This function has a flaw and will revert. Swapper contract is called during the function call, but tokens are not transferred to it nor tokens are transferred back from the swapper to the calling contract. Based on other parts of the codebase the problem is that swapper should be invoked with a low-level delegatecall instead of a normal call.
Vulnerability Detail
The LiquidationRow contract is an orchestrator for the claiming process. It is primarily used to collect rewards for vaults. It has a method called liquidateVaultsForToken. Based on docs this method is for: Conducts the liquidation process for a specific token across a list of vaults, performing the necessary balance adjustments, initiating the swap process via the asyncSwapper, taking a fee from the received amount, and queues the remaining swapped tokens in the MainRewarder associated with each vault.
function liquidateVaultsForToken(
address fromToken,
address asyncSwapper,
IDestinationVault[] memory vaultsToLiquidate,
SwapParams memory params
) external nonReentrant hasRole(Roles.LIQUIDATOR_ROLE) onlyWhitelistedSwapper(asyncSwapper) {
uint256 gasBefore = gasleft();
(uint256 totalBalanceToLiquidate, uint256[] memory vaultsBalances) =
_prepareForLiquidation(fromToken, vaultsToLiquidate);
_performLiquidation(
gasBefore, fromToken, asyncSwapper, vaultsToLiquidate, params, totalBalanceToLiquidate, vaultsBalances
);
}
The second part of the function is performing the liquidation by calling _performLiquidation. A problem is at the beginning of it. IAsyncSwapper is called to swap tokens.
function _performLiquidation(
uint256 gasBefore,
address fromToken,
address asyncSwapper,
IDestinationVault[] memory vaultsToLiquidate,
SwapParams memory params,
uint256 totalBalanceToLiquidate,
uint256[] memory vaultsBalances
) private {
uint256 length = vaultsToLiquidate.length;
// the swapper checks that the amount received is greater or equal than the params.buyAmount
uint256 amountReceived = IAsyncSwapper(asyncSwapper).swap(params);
// ...
}
As you can see the LiquidationRow doesn't transfer the tokens to swapper and swapper doesn't pul them either (swap function here). Because of this the function reverses.
I noticed that there is no transfer back to LiquidationRow from Swapper either. Tokens can't get in or out.
When I searched the codebase, I found that Swapper is being called on another place using the delegatecall method. This way it can operate with the tokens of the caller. The call can be found here - LMPVaultRouter.sol:swapAndDepositToVault. So I think that instead of missing transfer, the problem is actually in the way how swapper is called.
Impact
Rewards collected through LiquidationRow claimsVaultRewards get stuck in the contract. Liquidation can't be called because it reverts when Swapper tries to work with tokens it doesn't possess.
Code Snippet
Tool used
Manual Review
Recommendation
Change the async swapper call from the normal function call to the low-level delegatecall function the same way it is done in LMPVaultRouter.sol:swapAndDepositToVault.
I would like to address that AsyncSwapperMock in LiquidationRow.t.sol is a poorly written mock and should be updated to represent how the AsyncSwapper work. It would be nice to update the test suite for LiquidationRow because its current state won't catch this. If you check the LiquidationRow.t.sol tests, the mock swap function only mints tokens, no need to use delegatecall. This is why tests missed this vulnerability.
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 async swapper is invoked with a regular external call instead of a low-level delegatecall, so it cannot operate on the caller's tokens.
Impact
Liquidation reverts, so rewards collected through claimsVaultRewards get stuck in the contract.
What the attacker needed
LIQUIDATOR_ROLE holder calls the function; whitelisted swapper address
The fix
The async swapper call should be changed from a normal function call to a low-level delegatecall, the same way it is done in LMPVaultRouter.sol:swapAndDepositToVault.
How it was found
- Call liquidateVaultsForToken with a token, an async swapper and a list of vaults.
- The function prepares balances and then calls the swapper's swap function.
- The swapper has no tokens and no transfer happens, so the swap reverts.
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 can call another contract in two main ways: a normal call, where the other contract works with its own funds, or a delegatecall, where the other contract runs inside the caller's context and can use the caller's funds. Here the code used a normal call, so the swapper had no tokens to trade and the whole liquidation failed. The fix is to use delegatecall, as another part of the same codebase already does.
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.