In reimburseLiquidityFees() of SponserVault contract swaps tokens without slippage limit so its possible to perform sandwich attack and it create MEV
The reimburseLiquidityFees function in the SponsorVault contract calls tokenExchange.swapExactIn without specifying a minimum output amount (slippage limit). An attacker could sandwich the swap to manipulate the exchange rate and cause the contract to receive fewer tokens, profiting at the contract's expense.
Originally disclosed on Code4rena
Submitted by unforgiven
When code swaps tokens it should specify slippage but in reimburseLiquidityFees() code contract calls tokenExchange.swapExactIn() without slippage and it’s possible to perform sandwich attack and make contract to swap on bad exchange rates and there is MEV.
Proof of Concept
This is reimburseLiquidityFees() code in SponserVault:
/**
* @notice Performs liquidity fee reimbursement.
* @dev Uses the token exchange or liquidity deposited in this contract.
* The `_receiver` address is only used for emitting in the event.
* @param _token The address of the token
* @param _liquidityFee The liquidity fee amount
* @param _receiver The address of the receiver
* @return Sponsored liquidity fee amount
*/
function reimburseLiquidityFees(
address _token,
uint256 _liquidityFee,
address _receiver
) external override onlyConnext returns (uint256) {
uint256 sponsoredFee;
if (address(tokenExchanges[_token]) != address(0)) {
uint256 currentBalance = address(this).balance;
ITokenExchange tokenExchange = tokenExchanges[_token];
uint256 amountIn = tokenExchange.getInGivenExpectedOut(_token, _liquidityFee);
amountIn = currentBalance >= amountIn ? amountIn : currentBalance;
// sponsored fee may end being less than _liquidityFee due to slippage
sponsoredFee = tokenExchange.swapExactIn{value: amountIn}(_token, msg.sender);
} else {
uint256 balance = IERC20(_token).balanceOf(address(this));
sponsoredFee = balance < _liquidityFee ? balance : _liquidityFee;
// some ERC20 do not allow to transfer 0 amount
if (sponsoredFee > 0) {
IERC20(_token).safeTransfer(msg.sender, sponsoredFee);
}
}
emit ReimburseLiquidityFees(_token, sponsoredFee, _receiver);
return sponsoredFee;
}
As you can see there is no slippage defined when calling swapExactIn() can that swap could happen in any exchange rate. it’s possible to perform sandwich attack and do large swap before and after the transaction and make users lose funds. and it’s also MEV opportunity.
Tools Used
VIM
Recommended Mitigation Steps
Specify slippage when calling swap tokens.
jakekidd (Connext) acknowledged and commented:
This is absolutely correct, but unfortunately there is no apparent viable on-chain solution. If we revert because slippage is too high, behavior would be inconsistent and UX would be bad, to say the least. If we instead choose to not sponsor because the slippage is too high, then sponsorship could be griefed severely (i.e. ‘if we can’t get at least X, then give the user nothing’ is not a valid sponsorship policy).
Some things that make a sandwich attack less feasible however:
- Using exchanges with deep liquidity provision (sandwich attacks require principle liquidity, and cannot leverage flashloans).
- The fact that the liquidity fee here is only .05% of the transfer means that it would take a very heavy-handed attack and at least 1 very large transfer to make it profitable (for a 1M transfer, max profit is 500 dollars).
This is a good entrypoint for a future feature that gives sponsors better options, such as only sponsoring transfers for specific assets that they supply (i.e. so no swap is required).
cc @LayneHaber for thoughts. Because this essentially boils down to a feature request on behalf of sponsors - not a bug.
0xleastwood (judge) commented:
Seeing as I can’t verify if
tokenExchange.swapExactInis indeed vulnerable to slippage, I will just side with the sponsor/warden on this one.
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 is called without a minimum output amount, so the contract accepts any exchange rate.
Impact
An attacker could sandwich the swap to make the contract receive fewer tokens, though the sponsor notes that deep liquidity and the small fee size make this less feasible.
What the attacker needed
Contract holds tokens for swap; attacker can trade on same exchange
The fix
Specify a slippage limit when calling the swap function.
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 a contract swaps tokens, it should say the least it will accept. Here it doesn't, so someone can trade just before and after to push the price and make the contract get a bad deal. This is called a sandwich attack.
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.