SwapType.CloseExactOut balance check too strict can be DOSed
A swap function required the received output token balance to exactly equal the expected amount, so any small change in the pool ratio caused the transaction to revert. An attacker could front-run a swap with a tiny trade to shift the ratio and block the victim's swap.
ladboy233 Follow with rvierdiiev and said
Originally disclosed on Code4rena
Submitted by ladboy233, also found by rvierdiiev and said
SwapType.CloseExactOut balance check too strict can be DOSed.
Proof of Concept
In CoreCallback function, we have the logic below:
uint amtOut = IERC20(swapInfo.tokenOut).balanceOf(address(this));
if (swapInfo.swapType == SwapType.OpenExactIn) {
// transfer to coll pool to mint
IERC20(swapInfo.tokenOut).safeTransfer(marginPos.collPool, amtOut);
emit SwapToIncreasePos(swapInfo.initPosId, swapInfo.tokenIn, swapInfo.tokenOut, amtIn, amtOut);
} else {
// transfer to borr pool to repay
uint amtSwapped = amtIn;
if (swapInfo.swapType == SwapType.CloseExactOut) {
_require(IERC20(swapInfo.tokenOut).balanceOf(address(this)) == swapInfo.amtOut, Errors.SLIPPAGE_CONTROL);
amtSwapped -= IERC20(swapInfo.tokenIn).balanceOf(address(this));
}
emit SwapToReducePos(swapInfo.initPosId, swapInfo.tokenIn, swapInfo.tokenOut, amtSwapped, amtOut);
}
Note the logic below:
if (swapInfo.swapType == SwapType.CloseExactOut) {
_require(IERC20(swapInfo.tokenOut).balanceOf(address(this)) == swapInfo.amtOut, Errors.SLIPPAGE_CONTROL);
amtSwapped -= IERC20(swapInfo.tokenIn).balanceOf(address(this));
}
This check is too strict:
_require(IERC20(swapInfo.tokenOut).balanceOf(address(this)) == swapInfo.amtOut, Errors.SLIPPAGE_CONTROL);
Malicious user can DOS the swap by simply perform a tiny swap by frontruning the swap to change the token ratio in the LP pool.
For example, the swapInfo.amtOut is 1000 USDC, and swapInfo.tokenOut is USDC.
It is difficult to get exactly 1000 USDC, so any swap that executes before the swap can make the received USDC amount to 1000.1 USDC or 999. USDC, then swap transaction reverts.
Recommended Mitigation Steps
Do not use ==, use >=
hansfriese (Judge) decreased severity to Medium and commented:
Downgrade to Medium as there is no direct fund loss.
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 used an exact equality check on the received token balance instead of allowing a minimum amount, so normal slippage caused reverts.
Impact
The swap could be repeatedly blocked, causing a denial of service, but the judge noted there is no direct fund loss.
What the attacker needed
Ability to front-run a pending swap
The fix
The check should use a greater-than-or-equal comparison rather than exact equality.
How it was found
- Attacker watches for a pending CloseExactOut swap.
- Attacker front-runs it with a tiny swap that shifts the pool ratio.
- The victim's swap receives slightly more or less than the expected amount.
- The equality check fails and the victim's transaction 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 swap expected to receive exactly a set amount of a token. Because other trades can slightly change how much a swap returns, the exact-match check often fails and the transaction is cancelled. An attacker can deliberately make a tiny trade first so the victim's swap always fails, which is a denial of service.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.