Skip to content
Business logic Audit finding Intermediate

findNewOwner edgecase

The findNewOwner function in RCOrderbook uses a loop counter to decide whether processing is complete, but when the loop ends exactly at the deletion limit, it incorrectly treats a valid new owner as invalid. This can cause a legitimate owner to be skipped and later deleted without receiving ownership, impacting availability.

gpersoon Follow

Originally disclosed on Code4rena

Submitted by gpersoon

In the function findNewOwner of RCOrderbook, as loop is done which included the check _loopCounter < maxDeletions Afterwards, a check is done for “(*loopCounter != maxDeletions)” to determine if the processing is finished. If *loopCounter == maxDeletions then the conclusion is that it isn’t finished yet.

However, there is the edgecase that the processing might just be finished at the same time as _loopCounter == maxDeletions.

You can see this the best if you assume maxDeletions==1, in that case it will never draw the conclusion it is finished. Of course having maxDeletions==1 is very unlikely in practice.

 function findNewOwner(uint256 _card, uint256 _timeOwnershipChanged)  external  override  onlyMarkets  {
...
    // delete current owner
    do {
        _newPrice = _removeBidFromOrderbookIgnoreOwner( _head.next, _market, _card );
        _loopCounter++;             // delete next bid if foreclosed
    } while (    treasury.foreclosureTimeUser( _head.next, _newPrice,  _timeOwnershipChanged ) <  minimumTimeToOwnTo &&
            _loopCounter < maxDeletions );

    if (_loopCounter != maxDeletions) {   // the old owner is dead, long live the new owner
        _newOwner = ....
        ...
    } else {
        // we hit the limit, save the old owner, we'll try again next time
        ...
    }
}

Recommend using a different way to determine that the processing is done. This could save some gas. Note: the additional check also costs gas, so you have to verify the end result.

Perhaps in setDeletionLimit, doublecheck that _deletionLimit > 1.

Splidge (Reality Cards) confirmed and disagreed with severity:

oh wow, this is actually a really big problem. It’s easier to see it if maxDeletions is 1 but it exists with any size of maxDeletions. Whenever we find a valid owner on the final iteration of the loop the if statement will simply check if it was the final loop. That valid owner is then assumed to be invalid and saved for the next transaction to try and find a new owner. When that next transaction happens the valid owner is immediately deleted and not given any ownership of the card at all. I think this just falls short of 3 (High risk) because I don’t think it’d be possible for an attacker to engineer the situation to have a particular user deleted without ownership. But I believe this would count as 2 (Med risk) because the protocol “availability could be impacted” for the user that is deleted.

Splidge (Reality Cards) commented:

I have since thought of an attack that could have used this and might raise it to 3 (High risk).

Due to the difficultly of monitoring which cards you own all the time a valid strategy which some users employ is to bid high enough to scare off other users (usually bidding significantly beyond the 10% minimum increase). Suppose Alice employs this strategy by bidding $100 on a card that was previously only $10. Mal (our attacker) wishes to rent the card but wants to pay less than $100. Mal could use Sybil accounts to place maxDeletions - 1 bids all for the minimum rental duration (only funding the accounts for the minimum duration). Mal would then need to wait for the minimum duration of all these bids to expire, (maxDeletions - 1 ) * minimumRentalDuration Once this has completed Mal can place a bid at $11, this will trigger a rent collection which will attempt to findNewOwner, Alice being the user that was found on the last iteration of the loop would be considered as invalid. There will not be a change of ownership or any events emitted about this until the next rent collection is triggered. This means that the UI would still consider Alice to be the owner of card (Mals’ Sybil bids having had LogRemoveFromOrderbook and LogUserForeclosed events emitted) and other users might not consider trying to outbid this, whereas actually Mal is accruing time at a significantly cheaper rate.

Thinking about it, this doesn’t really even need Alice at all, Mal could have placed all the higher bids to simultaneously scare off other users while renting at a lower price.

I think the fix is relatively simple, by checking if we found a valid user OR hit the deletion limit we can make it so that we don’t skip any bids. This would then leave Alice (or Mal in the other version) correctly having to pay for the time at the higher price.

0xean (judge) commented:

upgrading based on sponsors analysis

Splidge (Reality Cards) patched:

Fixed here

The original report as published on Code4rena, by gpersoon. 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 code uses the loop counter reaching the maximum as the sole indicator that processing is incomplete, without checking whether a valid new owner was actually found.

What the attacker needed

MaxDeletions set to a value ≥1; multiple bids in the orderbook; attacker can place many bids (e.g., via Sybil accounts)

The fix

The fix was to check whether a valid user was found or the deletion limit was hit, so that no bids are skipped.

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 function tries to find a new owner by removing old bids in a loop. It uses a counter to know when to stop, but if the loop stops exactly at the limit, it mistakenly thinks it hasn't finished and skips a valid owner. This means a rightful owner can lose their card without warning.

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 gpersoon

View all

47 more disclosed reports, best first.

More from Reality Cards (round 2)

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.