Skip to content
Smart contracts Audit finding Advanced

Re-entrancy bug allows inflating balance

The MarginRouter contract trusted token amounts returned by a caller-supplied fake pair contract and credited them before completing the real swap, and the swap callback allowed re-entering the router. An attacker could therefore be credited several times for a single real trade and eventually drain tokens.

Marginswap

Originally disclosed on Code4rena

One can call the MarginRouter.crossSwapExactTokensForTokens function first with a fake contract disguised as a token pair: crossSwapExactTokensForTokens(0.0001 WETH, 0, [ATTACKER_CONTRACT], [WETH, WBTC]). When the amounts are computed by the amounts = UniswapStyleLib.getAmountsOut(amountIn - fees, pairs, tokens); call, the attacker contract returns fake reserves that yield 1 WBTC for the tiny input. The resulting amount is credited through registerTrade. Afterwards, _swapExactT4T([0.0001 WETH, 1 WBTC], 0, [ATTACKER_CONTRACT], [WETH, WBTC]) is called with the fake pair and token amounts. At some point _swap is called, the starting balance is stored in startingBalance, and the attacker contract call allows a re-entrancy:

pair.swap(0.0001 WETH, 1 WBTC, FUND, new bytes(0)); // can re-enter here

From the ATTACKER*CONTRACT we re-enter the MarginRouter.crossSwapExactTokensForTokens(30 WETH, 0, WETH*WBTC*PAIR, [WETH, WBTC])function with the actual WETH <> WBTC pair contract. All checks pass, the FUND receives the actual amount, the outer*swapcontinues execution after the re-entrancy and theendingBalance >= startingBalance + amounts[amounts.length - 1] check passes as well because the inner swap successfully deposited these funds. We end up doing 1 real trade but being credited twice the output amount.

This allows someone to be credited multiples of the actual swap result. This can be repeated many times and finally, all tokens can be stolen.

Recommend adding re-entrancy guards (from OpenZeppelin) to all external functions of MarginRouter. There might be several attack vectors of this function as the attacker controls many parameters. The idea of first doing an estimation with UniswapStyleLib.getAmountsOut(amountIn - fees, pairs, tokens) and updating the user with these estimated amounts, before doing the actual trade, feels quite vulnerable to me. Consider removing the estimation and only doing the actual trade first, then calling registerTrade with the actual trade amounts returned.

The original report as published on Code4rena. 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 router trusted reserves and amounts from an attacker-controlled pair contract and credited the trade before the real swap finished, with no re-entrancy guard on its external functions.

Impact

An attacker could be credited multiples of the real swap output, repeat this, and eventually steal all tokens.

What the attacker needed

Ability to deploy an attacker contract; router accepts caller-supplied pairs

The fix

Re-entrancy guards should be added to all external functions of MarginRouter, and the estimation step should be dropped so registerTrade is called only with the actual amounts returned by the real trade.

How it was found

  1. Call crossSwapExactTokensForTokens with a fake attacker contract posing as the token pair.
  2. The fake pair returns inflated reserves so a tiny input is valued at 1 WBTC.
  3. The inflated amount is credited through registerTrade.
  4. During the swap the attacker contract re-enters crossSwapExactTokensForTokens using the real WETH/WBTC pair.
  5. The inner swap deposits real funds, so the outer balance check passes and the credit is counted twice.

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 is a program on a blockchain that holds and moves funds. This router asked a contract supplied by the user how much a trade was worth and believed the answer, then paid out credit before finishing the real trade. Because the attacker's contract could call back into the router mid-trade, it could get credited more than once for the same money.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More from Marginswap

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.