Skip to content
DoS Audit finding Intermediate

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.

fez-init (INIT) confirmed


The original report as published on Code4rena, by ladboy233. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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

  1. Attacker watches for a pending CloseExactOut swap.
  2. Attacker front-runs it with a tiny swap that shifts the pool ratio.
  3. The victim's swap receives slightly more or less than the expected amount.
  4. 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.

More by ladboy233

View all

47 more disclosed reports, best first.

More from INIT Capital Invitational

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.