Skip to content
Business logic Audit finding Intermediate

The tradeEnd in BackingManager isn’t updating correctly

The tradeEnd value for Dutch auctions is set to the auction's end time, which can be up to a week, and is not updated when the auction settles early. This can prevent new Dutch auctions from being created for up to a week, even after the basket becomes ready.

ether_sky Follow with RadiantLabs and stuart_the_minion

Originally disclosed on Code4rena

Submitted by ether_sky, also found by RadiantLabs and stuart_the_minion

Lines of code

https://github.com/code-423n4/2024-07-reserve/blob/3f133997e186465f4904553b0f8e86ecb7bbacbf/contracts/p1/BackingManager.sol#L114 https://github.com/code-423n4/2024-07-reserve/blob/3f133997e186465f4904553b0f8e86ecb7bbacbf/contracts/p1/BackingManager.sol#L166

Impact

In the BackingManager, we use the tradeEnd value for each type of trade to prevent next auction from occurring within the same block. We can find a comment in the code explaining this in line 114.

function rebalance(TradeKind kind) external nonReentrant {
    // DoS prevention:
114:    // unless caller is self, require that the next auction is not in same block
    require(
        _msgSender() == address(this) || tradeEnd[kind] < block.timestamp,
        "already rebalancing"
    );

This approach works correctly for Batch auctions. However, with Dutch auctions, the tradeEnd value can inadvertently block the next auction from starting for a certain period.

Proof of Concept

The maximum auction length can be up to 1 week.

uint48 public constant MAX_AUCTION_LENGTH = 60 * 60 * 24 * 7; // {s} max valid duration, 1 week

And the minimum warm-up period in the BasketHandler is 1 minute.

uint48 public constant MIN_WARMUP_PERIOD = 60; // {s} 1 minute

Suppose a Dutch auction is created in the BackingManager with a length of 1 week (7 days), starting at timestamp T. The tradeEnd for this auction type is set to T + 7 days in line 166.

function rebalance(TradeKind kind) external nonReentrant {
155:    if (doTrade) {

165:	    ITrade trade = tryTrade(kind, req, prices);
166:	    tradeEnd[kind] = trade.endTime(); // {s}

168:	}
}

Now, let’s consider a scenario where one of the assets in the basket temporarily falls into an IFFY status. At this point, the current status of the basket is also IFFY (line 323~325).

function status() public view returns (CollateralStatus status_) {
	uint256 size = basket.erc20s.length;
    if (disabled || size == 0) return CollateralStatus.DISABLED;
   
    for (uint256 i = 0; i < size; ++i) {
        CollateralStatus s = assetRegistry.toColl(basket.erc20s[i]).status();
323:        if (s.worseThan(status_)) {
324:            if (s == CollateralStatus.DISABLED) return CollateralStatus.DISABLED;
325:            status_ = s;
        }
    }
}

The auction is settled one day later, triggering the rebalance function (line 92).

function settleTrade(IERC20 sell) public override(ITrading, TradingP1) returns (ITrade trade) {
    delete tokensOut[sell];
    trade = super.settleTrade(sell); // nonReentrant

    if (_msgSender() == address(trade)) {       
92:        try this.rebalance(trade.KIND()) {} catch (bytes memory errData) {
        }
    }
}

The check at line 116 passes because the caller is the BackingManager itself. However, the BasketHandler status is still IFFY, meaning it isn’t ready for a new auction. As a result, the check at line 121 prevents the next auction from being created.

function rebalance(TradeKind kind) external nonReentrant {
    requireNotTradingPausedOrFrozen();
    assetRegistry.refresh();

    require(
116:        _msgSender() == address(this) || tradeEnd[kind] < block.timestamp,
        "already rebalancing"
    );

    require(tradesOpen == 0, "trade open");
   
121:    require(basketHandler.isReady(), "basket not ready");
   
    require(block.timestamp >= basketHandler.timestamp() + tradingDelay, "trading delayed");
}

The tradeEnd is still set to T + 7 days and the current time is now T + 1 day.

After another day, the asset’s status recovers to SOUND, and the BasketHandler status updates to SOUND as well. After the warm-up period (1 minute in our scenario), the BasketHandler is ready, and the time is now T + 2 days + 1 minute. At this point, we want to create a new Dutch auction for rebalancing. However, we can’t create the next Dutch auction until T + 7 days due to the existing tradeEnd.

This delay clearly poses an issue.

Recommended Mitigation Steps

    function settleTrade(IERC20 sell) public override(ITrading, TradingP1) returns (ITrade trade) {
            delete tokensOut[sell];
            trade = super.settleTrade(sell); // nonReentrant

            if (_msgSender() == address(trade)) {
                try this.rebalance(trade.KIND()) {} catch (bytes memory errData) {
                    if (errData.length == 0) revert(); // solhint-disable-line reason-string
                }

    +			tradeEnd[kind] = uint48(block.timestamp);

            }
        }
    }

akshatmittal (Reserve) confirmed, but disagreed with severity and commented:

We’d like to bump this down to Low.

This is an issue, although does not impact protocol availability. The protocol can still function as expected using the other trading methods.

cccz (judge) commented:

After reconsidering the issue, I think this should be valid Medium. It indeed affects the availability of the protocol, which meets the Medium severity.

tbrent (Reserve) commented:

The issue can only impact DutchTrade because it is the only KIND that can have auctions that end before the endTime.

I only see the availability of the protocol being impacted if the GnosisTrade’ reportViolation() triggers, which would ultimately be the real cause of the loss of availability. GnosisTrade is supposed to be the fallback mechanism for trading.

Possibly relevant: the notions of disabling in the Broker are very different for dutch trade and batch trade:

  • in dutch trades it is intended to detect if the protocol is trading off bad pricing data, since a dutch trade necessarily involves an assumption about the highest possible price. This is possible to trigger intentionally by burning $.
  • in batch trades it is intended to detect if EasyAuction goes beyond the worst-case min buy amounts. Similar to if a Uniswap swap violated slippage constraints, it should not happen. This should not be possible to trigger intentionally by burning $.

ether_sky (warden) commented:

Thanks for your reveiw.

There are only two types of trades. Because of this issue, Dutch auctions cannot occur for up to one week. In some cases, Dutch auctions could be more efficient than Batch trades, but we are required to use Batch trades instead.

Additionally, as the sponsor described, Batch trades can be paused due to reportViolation(). In such cases, this issue could have a significant impact.

Therefore, I believe this issue deserves a Medium severity based on its impact and likelihood.

cccz (judge) commented:

Yes, as warden said, Dutch auctions being unavailable for a period of time is a concern.

akshatmittal (Reserve) commented:

@cccz - Let me clarify what @tbrent is trying to say.

Batch Auctions are always available, and the whole reason they exist is for scenarios like this. The key about reportViolation for Gnosis Trade is that it checks for an EasyAuction protocol invariant, and nothing else. That is why it’s the “backup” trading method.

Dutch Auctions and Batch Auctions have different characteristics from a what-happens-when perspective, but similar characteristics from a competition and pricing perspective. Yes, Dutch Auctions make it more efficient for the participants, which is why it exists, but it’s not something that the protocol entirely depends on.

Unavailability of Dutch Auction does not mean the protocol is at risk or is “unavailable”, which is why we disable Dutch Auctions in many more conditions than Batch Auctions. Keep in mind that early versions of the protocol did not even have Dutch Trade to begin with.

That said, we’ll leave it to you to decide here, we are internally considering it a low severity issue.

ether_sky (warden) commented:

Hi @akshatmittal - thanks for your comment.

I understood all your points. However, how can auditors know whether the Dutch Auctions is not important? The only thing which we could find is that there are only 2 types of auctions and one of them can be unavailable for some periods and each auctions can be paused due to reportViolation functionality.

Anyway, I will respect judge’s decision.

Thanks again.

Reserve mitigated

Status: Mitigation confirmed. Full details in reports from RadiantLabs, ether_sky and Bauchibred.


The original report as published on Code4rena, by ether_sky. 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 tradeEnd mapping is set to the auction's end time but is not reset when the auction settles early, so it blocks new auctions until the original end time.

Impact

Dutch auctions may be unavailable for up to a week, but batch auctions remain available as a fallback, so the protocol can still function.

What the attacker needed

A Dutch auction is created; the auction settles before its end time; the basket is not ready at settlement

The fix

The tradeEnd should be updated to the current block timestamp when a trade settles, so that new auctions are not blocked by the original end time.

How it was found

  1. A Dutch auction is created with a length of 1 week, setting tradeEnd to T + 7 days.
  2. The auction settles one day later, triggering rebalance, but the basket is not ready so no new auction is created.
  3. The basket becomes ready after another day, but tradeEnd still blocks new Dutch auctions until T + 7 days.

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.

In this smart contract, a Dutch auction is a way to sell assets where the price drops over time. The contract records when the auction will end, but if the auction finishes early, that record is not updated. Later, when the system wants to start a new Dutch auction, it sees the old end time and refuses to start until that time passes, which could be up to a week later.

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 ether_sky

View all

11 more disclosed reports, best first.

More from Reserve Core

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.