Skip to content
Business logic Audit finding Intermediate

Frontrunning in UniswapHandler calls to UniswapV2Router

The UniswapHandler contract calls UniswapV2Router methods for swapping and removing liquidity without setting a minimum output amount, passing zero instead. An attacker can watch the mempool and sandwich these transactions to profit from the guaranteed trade.

thank Follow

Originally disclosed on Code4rena

Submitted by thankyou, also found by 0x0x0x, cmichel, defsec, harleythedog, hyh, Koustre, leastwood, Meta0xNull, pauliax, pmerkleplant, tabish, WatchPug, and xYrYuYx_

UniswapHandler utilizes UniswapV2Router to swap, add liquidity, and remove liquidity with the UniswapV2Pair contract. In order to utilize these functionalities, UniswapHandler must call various UniswapV2Router methods.

  • addLiquidity
  • removeLiquidity
  • swapExactTokensForTokens (swaps for both DAI and Malt)

In all three methods, UniswapV2Router requires the callee to provide input arguments that define how much the amount out minimum UniswapHandler will allow for a trade. This argument is designed to prevent slippage and more importantly, sandwich attacks.

UniswapHandler correctly handles price slippage when calling addLiquidity. However, that is not the case for removeLiquidity and swapExactTokensForTokens here and here. For both methods, 0 is passed in as the amount out minimum allowed for a trade. This allows for anyone watching the mempool to sandwich attack UniswapHandler (or any contract that calls UniswapHandler) in such a way that allows the hacker to profit off of a guaranteed trade.

How does this work? Let’s assume UniswapHandler makes a call to UniswapV2Router#swapExactTokensForTokens to trade DAI for Malt. Any hacker who watches the mempool and sees this transaction can immediately buy as much Malt as they want. This raises the price of Malt. Since UniswapHandler is willing to accept any amount out minimum (the number is set to zero), then the UniswapHandler will always trade DAI for Malt. This second transaction raises the price of Malt even further. Finally, the hacker trades their Malt for DAI, receiving a profit due to the artificially inflated price of Malt from the sandwich attack.

It’s important to note that anyone has access to the UniswapV2Router contract. There are no known ACL controls on UniswapV2Router. This sandwich attack can impact even the buyMalt function.

The following functions when called are vulnerable to frontrunning attacks:

And by extension the following contract functions since they also call the UniswapHandler function calls:

Proof of Concept

Refer to the impact section for affected code and links to the appropriate LoC.

Recommended Mitigation Steps

The UniswapV2Router and UniswapV2Pair contract should allow only the UniswapHandler contract to call either contract. In addition, price slippage checks should be implemented whenever removing liquidity or swapping tokens. This ensures that a frontrunning attack can’t occur.

Anything Else We Should Know

I wish I had more time to work on this bug but unfortunately I have several current clients who require significant time from me. I’m happy to pursue this beyond the initial submission, in particular building a concrete PoC. I think the most important takeaway from this bug find is that anyone can purchase Malt at any time and anyone can manipulate the Malt reserve. This in turn impacts other functionalities that rely on the Malt reserve to make price/token calculations such as exiting an auction early or reinvesting rewards.

0xScotch (sponsor) confirmed

Alex the Entreprenerd (judge) commented:

Because transactions sit on the mempool (which is public, hence accessible by anyone), they can be frontrun, because of this swapping protocols (uniswap in this case) offer slippage checks. Setting the slippage checks allows a frontrunner to squeeze the maximum amount of value possible (sometimes the whole amount).

Because this applies to a leak of value, I believe medium severity to be correct

The original report as published on Code4rena, by thank. 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

Likely the contract passes zero as the minimum output amount for swaps and liquidity removal, disabling slippage protection.

Impact

An attacker could profit by sandwiching trades, causing a loss of value for the protocol or its users; the judge rated this medium severity.

What the attacker needed

Access to mempool; ability to trade on Uniswap

The fix

The contracts should restrict access to only the UniswapHandler and implement slippage checks when removing liquidity or swapping tokens.

How it was found

  1. Attacker watches the mempool for a transaction calling UniswapHandler to swap DAI for Malt.
  2. Attacker buys Malt before the victim's transaction, raising its price.
  3. Victim's transaction executes at the inflated price because the minimum output is zero.
  4. Attacker sells the Malt for DAI, profiting from the price difference.

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 trades tokens on a decentralised exchange, it can specify the minimum amount it will accept to protect against price changes. Here, the contract accepted any amount, so an attacker could buy the token first, let the contract trade at a worse price, then sell for a profit. 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.

More by thank

View all

1 more disclosed report, best first.

More from Malt Protocol - Versus contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.