Skip to content
Access control Audit finding Advanced

No entry checks in crossSwap[Exact]TokensFor[Exact]Tokens

Two swap functions on the MarginRouter contract lack caller authorisation and do not validate the supplied pair and token arrays. An attacker could trade on behalf of the protocol, supply self-constructed pair contracts to run arbitrary code, and reenter the protocol, potentially losing funds.

Marginswap

Originally disclosed on Code4rena

The functions crossSwapTokensForExactTokens and crossSwapExactTokensForTokens of MarginRouter.sol do not check who is calling the function. They also do not check the contents of pairs and tokens nor do they check if the size of pairs and tokens is the same.

registerTradeAndBorrow within registerTrade does seem to do an entry check (require(isMarginTrader(msg.sender)…) however as this is an external function msg.sender is the address of MarginRouter.sol, which will verify ok.

Calling these functions allow the caller to trade on behalf of marginswap, which could result in losing funds. It’s possible to construct all parameters to circumvent the checks. Also the “pairs” can be fully specified; they are contract addresses that are called from getAmountsIn / getAmountsOut and from pair.swap. This way you can call arbitrary (self constructed) code, which can reentrantly call the marginswap code.

Recommend limiting who can call the functions. Perhaps whitelist contents of pairs and tokens. Check the size of pairs and tokens is the same.

werg (Marginswap) confirmed:

This has merit: particularly the part about self-constructed pairs. We either need much more rigorous checks or a a process for vetting & approving pairs. The latter is likely more gas efficient.

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 swap functions rely on an internal check that sees the router itself as the caller, and they never validate the caller or the pair and token arrays.

Impact

The caller could trade on behalf of the protocol and, by supplying self-constructed pairs, run arbitrary code that reenters the protocol, which could result in lost funds.

What the attacker needed

Ability to call the router contract directly

The fix

The functions should be restricted to authorised callers, with the pair and token contents whitelisted or vetted and their array lengths checked to match.

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 router contract is meant to pass a user's trade request to the right trading pairs. Here anyone could call the swap functions directly, and the contract trusted whatever pair addresses were handed to it. Because those addresses are called as code, an attacker could point them at their own contract and run their own logic inside the protocol.

What is Access control?

A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.

More from Marginswap

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Broken access control

The lesson behind this report.

A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.