Skip to content
DoS Audit finding Intermediate

DOS of market operations with malicious offers

The Rubicon market contract allows offers with the zero address as owner or recipient, which cannot be filled because standard ERC20 tokens revert on transfers to the zero address. An attacker can create such an offer at the best price, causing all calls to buyAllAmount and sellAllAmount to revert and blocking the Position contract and new order creation.

carrotsmuggler Follow with joestakey, Udsen and 41 others

Originally disclosed on Code4rena

Submitted by carrotsmuggler, also found by joestakey, Udsen, brgltd, alexzoid, zhuXKET, 0xBeirao, nirlin, halden, McToady, LaScaloneta, immeas, Evo, favelanky, 0Kage, Nyx, Nyx, Udsen, Neon2835, 141345, bin2chen, whoismatthewmc1, ast3ros, whoismatthewmc1, 0xfusion, CryptoCraze, CryptoCraze, Juntao, SaeedAlipoor01988, Lilyjjo, 0xPiercer, MalfurionWhitehat, CRYP70, 3agle, SaeedAlipoor01988, ktg, ladboy233, ladboy233, sces60107, sces60107, popular00, anodaram, mrpathfindr, and mrpathfindr.

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

The Rubicon market has two functions that are interesting: buyAllAmount and sellAllAmount. These functions are responsible for using up offers until the entire amount is bought or sold. The contract stores offers in a sorted linked list, each with its unique id, and sorted according to the price. This ensures that the head of the linked list has the best price, or the best offer for either sell or buy. This head is referenced in the code by the mapping _best[address][address] and further worse, offers can be navigated to by calling best.next, similar to a linked list navigation.

When buyAllAmount is called, a while loop is ran, and the best offer is taken out in each iteration using the function getBestOffer() similar to the snippet below:

while (buy_amt > 0) {
            //Meanwhile there is amount to buy
            offerId = getBestOffer(buy_gem, pay_gem); //Get the best offer for the token pair
            require(offerId != 0, "offerId == 0");

The offers are then filled one after the other. However, if an offer is unfillable, or reverts, then the whole transaction reverts. This breaks the functionality of the buyAllAmount function and creates a DOS attack vector.

The standard openzeppelin implementation of the ERC20 token does not support transfers to the zero address. If a transaction tries to send tokens to 0 address, it reverts with ‘ERC20: transfer to the zero address’. Thus, if an offer is created where the receiver of the payment is zero address, that offer can be unfillable. If this offer is created such that it is the best possible price, then this offer will exist at the head of the linked list in the _best variable and thus will be the first offer looked into. Since this offer is unfillable, the entire transaction will revert.

When creating an offer, the user can specify two addresses, the owner address and the recipient address. The contract does not sanitize these addresses and even accepts ‘0 address’ in these fields. These are also the addresses where the payment token is sent when the order is filled.

Thus, a malicious user can create a buy/sell order of a small volume but at a very good price. They can set both the owner and the recipient to ‘0’ when calling offer(). This will create a lucrative unfillable order which will always be called on first. This will lead to all transactions reverting due to this single unfillable offer.

The impact is even more pronounced for the Position.sol contract. Unlike normal users, this contract does not have the ability to select offers by id, and is only filled using buyAllAmount or sellAllAmount. A DOS attack, as described above, will break the functionality of the two mentioned functions and break the whole Position contract, as a result. This can lead to large losses where users are unable to unwind positions before liquidations.

This also has a secondary impact of blocking the creation of future orders. As can be seen here, in the _matcho function, when new offers are created they are first matched with existing offers in the same range. If there is still a balance remaining, a new offer is created. Now if the existing offers are such unfillable orders, it would also block the creation of further new orders, causing another functionality DOS.

Proof of Concept

The POC shows that if the recipient and owner addresses are both set to ‘0’, the order cannot be filed. This order will exist in the linked list sorted by price and thus will be the first order to be filled.

First, change the code in deployRubiconProtocolFixture() where the orders are created here to the following order:

await rubiconMarket.functions[
            "offer(uint256,address,uint256,address,address,address)"
        ](
            parseUnits("100"),
            testCoin.address,
            parseUnits("110", 6),
            testStableCoin.address,
            ethers.constants.AddressZero,
            ethers.constants.AddressZero,
            { from: owner.address }
        )

This creates an unfillable order. Now try running the provided test in “can take an offer with 0 makerFee” by using the command: hardhat test --grep "0 makerFee".

This test fails with ‘ERC20: transfer to the zero address’ error, since the order is unfillable. The test body is also copied here as reference:

it("can take an offer with 0 makerFee", async function () {
                const { rubiconMarket, testCoin, owner } = await loadFixture(
                    deployRubiconProtocolFixture
                )
                const balanceBefore = await testCoin.balanceOf(owner.address)
                // in TEST
                const quantity = parseUnits("14")

                // amount that should be received with fee deducted (only Protocol fee!)
                const quantityAfterFee = await rubiconMarket.calcAmountAfterFee(
                    quantity
                )

                // buying TEST with USDCT
                await rubiconMarket.buy(2, quantity)
                // saving new balance state
                const balanceAfter = await testCoin.balanceOf(owner.address)

                // validating that owner received proper amount with ONLY protocol fee charged
                expect(balanceAfter.sub(balanceBefore)).to.be.equal(
                    quantityAfterFee
                )
            })

This shows unfillable orders can be created. If this order exists at the top, it will revert calls from the Position.sol contract.

Tools Used

Hardhat

Recommended Mitigation Steps

This requires multiple mitigation steps:

  1. Do not allow ‘0 address’ as owner. This can be enforced with a require statement in the offer() function in SimpleMarket contract.
  2. Unfillable orders can still be created by setting recipient to blacklisted addresses, for tokens like USDT. To navigate this, in the while loop in buyAllAmount, include error handling with try-catch blocks to use the next offer, as the current offer is unfillable.

daoio (Rubicon) confirmed

HickupHH3 (judge) commented:

The impact is even more pronounced for the Position.sol contract. Unlike normal users, this contract does not have the ability to select offers by id, and is only filled using buyAllAmount or sellAllAmount. A DOS attack as described above will break the functionality of the two mentioned functions, and thus break the whole Position contract as a result. This can lead to large losses where users are unable to unwind positions before liquidations.

This also has a secondary impact of blocking the creation of future orders. As can be seen here, in the _matcho function, when new offers are created, they are first matched with existing offers in the same range, and if there is still a balance remaining, a new offer is created. Now if the existing offers are such unfillable orders, it would also block the creation of further new orders, causing another functionality DOS.

Impact was well described and explained. I consider this to be medium severity though, because it’s on L2s where the mainly supported tokens do not revert on null address (unless proven otherwise).

Satisfactory: has to be similar to best report in impact description (key word I’m looking for is DoSing the Position via buyAllAmount()/sellAllAmount()).

50%: Mentions DoS in some capacity / loss of user funds.

25%: Only mention zero address check with no / little impact description.

After a brief consultation, I’ve come to the following conclusion over non-standard ERC20 implementations:

  • No return of bool/returning false: Excluded because it’s been caught by the 4naly3er.
  • FoT/Rebasing: Isolated impact, doesn’t affect trades of other tokens.
  • Revert on zero value: Low, isolated impact, doesn’t affect trades of other tokens.
  • Revert on null address: Medium, as a substantial number of ERC20 tokens inherit OZ’s implementation that has this check, allowing for DoS.

HickupHH3 (judge) decreased severity to Medium

carrotsmuggler (warden) commented:

Impact was well described and explained. I consider this to be medium severity though, because it’s on L2s where the mainly supported tokens do not revert on null address (unless proven otherwise).

According to uniswap statistics, the top 8 tokens by volume are listed below, and their contracts have been evaluated. For all these tokens, transfers to ‘0 address’ results in a revert.

Token | Address |

USDT | 0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9 |

USDC | 0xaf88d065e77c8cC2239327C5EDb3A432268e5831 |

weth | 0x82aF49447D8a07e3bd95BD0d56f35241523fBab1 |

wbtc | 0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f |

arb | 0x912CE59144191C1204E64559FE8253a0e49E6548 |

dai | 0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1 |

bridged usdc | 0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8 |

GMX | 0xfc5A1A6EB076a2C7aD06eD22C90d7E710E35ad0a |

Every token (except DAI) follows the open-zeppelin standard and has a require block in their _transfer function.

require(_sender != address(0), "BaseToken: transfer from the zero address");
require(_recipient != address(0), "BaseToken: transfer to the zero address");

DAI doesn’t follow the standard, but has a statement which behaves in a similar manner.

Since the vast majority (97%+) of the uniswap transactions involve at least one of these tokens, it is safe to assume that most transactions would be affected by this bug.

Since the severity is high (complete breaking of Position contract), and the likelihood is high (97%+ affected volume), the severity of this bug should be upgraded to High.

HickupHH3 (judge) increased severity to High and commented:

Justified.


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

The offer creation function does not reject the zero address as owner or recipient, allowing an unfillable offer that reverts the entire buyAllAmount or sellAllAmount loop when it is the best offer.

Impact

All calls to buyAllAmount and sellAllAmount revert, breaking the Position contract and preventing users from unwinding positions before liquidations; new order creation is also blocked. The judge noted that on L2s some tokens may not revert on the zero address, but the warden showed that most major tokens do, so the impact is high.

What the attacker needed

Ability to create an offer on the market; the market uses a token that reverts on zero-address…

The fix

The offer function should reject the zero address as owner, and the buyAllAmount loop should use try-catch to skip unfillable offers.

How it was found

  1. Create an offer with a very good price and set both owner and recipient to the zero address.
  2. The offer is stored at the head of the sorted linked list because of its good price.
  3. Call buyAllAmount or sellAllAmount, which loops over offers starting from the best one.
  4. The loop tries to fill the malicious offer, but the token transfer to the zero address reverts, causing the whole transaction to revert.

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 market lets people trade tokens by placing offers. The code that fills all offers at once stops completely if it meets an offer that cannot be completed. Here, someone can place an offer that can never be completed because it tries to send tokens to the zero address, which many tokens forbid. Because this offer is the best price, it is always tried first, so every trade fails.

What is DoS?

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.

More by carrotsmuggler

View all

42 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.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.