Skip to content
Business logic Audit finding Advanced

Marketplace may call onERC721Received() and create a lien during buyNftFromMarket() , creating divergence

The contract's onERC721Received() check only blocks registered marketplaces, but when buying through Reservoir's router, the actual NFT transfer comes from an executor contract that is not whitelisted. This allows a new lien to be created during buyNftFromMarket(), so two liens track the same NFT.

minhquanym Follow

Originally disclosed on Code4rena

Submitted by minhquanym

The contract supports a “push-based” NFT supply, where the price and rate are embedded in the data bytes. This way, the lender doesn’t need to additionally approve the NFT, but can just transfer it directly to the contract. However, since the contract also interacts with the marketplace to buy/sell NFT, it has to prevent the issue where the marketplace also sends data bytes, which might tie 1 NFT with 2 different liens and create divergence.

function onERC721Received(
    address operator,
    address from,
    uint256 tokenId,
    bytes calldata data
) external returns (bytes4) {
    if (data.length == 64) {
        // @audit marketplace is router so the executor contract might not be whitelisted
        if (registeredMarketplaces[operator]) {
            /// @dev transfer coming from registeredMarketplaces will go through buyNftFromMarket, where the NFT
            /// is matched with an existing lien (realize PnL) already. If procceds here, this NFT will be tied
            /// with two liens, which creates divergence.
            revert Errors.Unauthorized();
        }
        /// @dev MAX_PRICE and MAX_RATE should each be way below bytes32
        (uint256 price, uint256 rate) = abi.decode(data, (uint256, uint256));
        /// @dev the msg sender is the NFT collection (called by safeTransferFrom's _checkOnERC721Received check)
        _supplyNft(from, msg.sender, tokenId, price, rate);
    }
    return this.onERC721Received.selector;
}

The contract prevents it by using the registeredMarketplaces[] mapping, where it records the address of the marketplace. This check is explicitly commented in the codebase.

However, this is not enough. The protocol plans to integrate with Reservoir’s Router contract, so only the Router address is whitelisted in registeredMarketplaces[]. But the problem is, the address that transfers the NFT is not the Router, but the specific Executor contract, which is not whitelisted.

As a result, the marketplace might bypass this check and create a new lien in onERC721Received() during the buyNftFromMarket() flow, thus making 2 liens track the same NFT.

Proof of Concept

Function _execBuyNftFromMarket() does a low-level call to the exchange.

// execute raw order on registered marketplace
bool success;
if (useToken == 0) {
    // use ETH
    // solhint-disable-next-line avoid-low-level-calls
    (success, ) = marketplace.call{value: amount}(tradeData);
} else if (useToken == 1) {
    // use WETH
    weth.deposit{value: amount}();
    weth.approve(marketplace, amount);
    // solhint-disable-next-line avoid-low-level-calls
    (success, ) = marketplace.call(tradeData);
}

The contract calls to Reservoir’s router contract, which then calls to a specific module to execute the buy.

https://github.com/reservoirprotocol/indexer/blob/6c89d546d3fb98d5eaa505b9943e89bd91f2e8ec/packages/contracts/contracts/router/ReservoirV6_0_1.sol#L50

function _executeInternal(ExecutionInfo calldata executionInfo) internal {
  address module = executionInfo.module;

  // Ensure the target is a contract
  if (!module.isContract()) {
    revert UnsuccessfulExecution();
  }

  (bool success, ) = module.call{value: executionInfo.value}(executionInfo.data);
  if (!success) {
    revert UnsuccessfulExecution();
  }
}

Recommended Mitigation Steps

Consider adding a flag that indicates the contract is in the buyNftFromMarket() flow and use it as a check in onERC721Received(). For example:

_marketBuyFlow = 1;
_execBuyNftFromMarket(lien.collection, tokenId, amount, useToken, marketplace, tradeData);
_marketBuyFlow = 0;

And in onERC721Receive():

if (data.length == 64) {
  if(_martketBuyFlow) {
    return this.onERC721Received.selector;
  }
}

Assessed type

Invalid Validation

wukong-particle (Particle) confirmed and commented:

We are considering adding ReentrancyGaurd around all functions that modify the lien (to prevent other issues like https://github.com/code-423n4/2023-05-particle-findings/issues/14). Here, we should be able to re-use the ReentrancyGaurd variable to prevent divergence.

So something like this:

buyNftFromMarket(...) external payable override validateLien(Lien, LienId) nonReentrant {
 ...
}

in onERC721Received:

if (data.length == 64) {
  if(_status === _ENTERED) {
    revert Errors.Unauthorized();
  }
}

We will need to modify _status to be internal instead of private from Openzeppelin’s original ReentrancyGaurd.sol.

wukong-particle (Particle) commented:

Fixed.


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

The marketplace could bypass the check and create a new lien during buyNftFromMarket(), so two liens track the same NFT, creating divergence.

What the attacker needed

Protocol integrates with Reservoir router; marketplace is registered; buyNftFromMarket() is called

The fix

The project fixed it by using a reentrancy guard flag to detect when the contract is in the buyNftFromMarket() flow and revert in onERC721Received().

How it was found

  1. The contract calls the registered marketplace (Reservoir router) with trade data.
  2. The router calls a specific module (executor) to execute the buy.
  3. The executor transfers the NFT, triggering onERC721Received() with data length 64.
  4. Because the executor is not in registeredMarketplaces, the check passes and a new lien is created.

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 lien is a claim on an NFT. The contract tries to stop a marketplace from creating a second lien while it is buying an NFT, but it only checks the marketplace's main address. The marketplace uses a helper contract to move the NFT, and that helper is not on the list, so the check is bypassed.

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 minhquanym

View all

28 more disclosed reports, best first.

More from Particle Leverage AMM Protocol Invitational

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.