Malicious actor can steal any actively rented NFT and freeze the rental payments (of the affected rentals) in the escrow contract
The stopRent function does not validate that a rental order exists before transferring the rented NFT from the renter's safe back to the lender, and the order hash omits the rentalWallet field. An attacker can supply a fake order with the victim's rentalWallet and their own lender address, then create and fulfill a matching order during the NFT transfer callback, stealing the NFT and freezing the rental payment in escrow.
JCN Follow with trachev, serial-coder and 19 others
Originally disclosed on Code4rena
Submitted by JCN, also found by trachev, serial-coder, zzzitron (1, 2), hash, rbserver, fnanni, juancito, Qkite (1, 2, 3), KupiaSec, krikolkk, zach, 0xpiken, kaden (1, 2), ravikiranweb3, J4X, rvierdiiev, Krace, Audinarey, Aymen0909, 0xDING99YA, Ward, DanielArmstrong (1, 2), evmboi32, AkshaySrivastav, and ABAIKUNANBAEV
The main exploit presented in this report takes advantage of multiple bugs. Some of these bugs on their own can be viewed as having their own “lesser” impacts. However, this report can be considered as my demonstration of the greatest impact for all the bugs described below:
- The
stopRent/stopRentBatchfunctions do not validate the existence of the suppliedRentalOrderuntil after the lender is sent the specified NFT. Therefore, a non-existentRentalOrdercan be supplied and then created during the callback to the lender (onERC721Received/onERC1155Received). (Successful exploitation of this bug is dependent on the other bugs below) - The signature digest signed by the protocol signer is predictable and not necessarily unique to a specific order. Therefore, a generic signature can be retrieved from theoretically any order (via the front-end) and can then be used arbitrarily (for multiple fulfillments) until
metadata.expiration > block.timestamp. This enables the above bug to be utilized, as a required signature is able to be retrieved beforehand so that the order creation and fulfillment process can be executed atomically for multiple orders. (Successful exploitation of this bug is not dependent on the other bugs mentioned) - Several core functions in
Signer.solare not compliant with EIP-712. This can be leveraged with the above bugs to obtain a valid signature and use this signature with multiple orders that have differentmetadata.orderTypevalues. This also allows arbitrarysafe walletaddresses to be included in theRentalOrderstruct supplied to thestopfunctions. (Successful exploitation of this bug is not dependent on the other bugs mentioned)
Combined, the above vulnerabilities allow the theft and freezing of any and/or all NFTs currently being rented out (active or expired, as long as the rental has not been stopped). The rental payments for the affected rentals will also be frozen in the escrow contract. The outline of this Bug Description section will be as follows:
- The main exploit path will be discussed
- Prerequisites for the main exploit will be discussed
- Lesser, independently achievable, impacts of the independent vulnerabilities (2 & 3) will be briefly explained
Main exploit path
In order to thoroughly detail this exploit path I will provide my explanation as a means to prove that the following actions are possible:
- A non existent
RentalOrdercan be passed into astopfunction and pass validation. The only requirement for this exploit is that theRentalOrder.orderTypespecifies aPAYorder andmsg.sender == RentalOrder.lender. - The only fields in this
RentalOrderthat pertain to the rental order (rented NFT) being exploited are theRentalOrder.itemsandRentalOrder.rentalWalletfields. Theitemsfield will specify the rented NFT that therentalWallet(victim) curently owns. All other fields can differ. - This non existent
RentalOrdercan be created and fulfilled during thereclamationprocess in which the rented NFT gets transferred from therentalWallet(victim) to theRentalOrder.lender(specified by exploiter). This can be done during theonERC721Received/onERC1155Receivedcallback to theRentalOrder.lender. Note that during the creation and fulfillment process we will specify ourattacker safeas theRentPayload.fulfillmentso that ourattacker safewill receive the stolen NFT specified in theitemsfield. - Once the
RentalOrderhas been created in storage, the next state-modifyingsettlePaymentandremoveRentalsfunction calls will succeed. The first call will result in theRentalOrder.lenderreceiving back the ERC20 token payment specified in thisRentalOrder, and the second call will remove this newly createdRentalOrderfrom storage. - During the
removeRentalscall the computedorderHash(this is used to identify a specific rental order) does not take into account theRentalOrder.rentalWallet. Therefore, this allowed us to supply the victim’ssafe walletinstead of ourattacker safein thestopfunction calls and ultimately produce the necessaryorderHashthat corresponds to our newly createdRentalOrder.
When a rental is stopped the stopRent function is invoked (the stopRentBatch function can also be used and has similar functionality). A RentalOrder struct is supplied to this function and the only validation for this struct is performed in the _validateRentalCanBeStoped internal function on line 267 of Stop::stopRent:
265: function stopRent(RentalOrder calldata order) external {
266: // Check that the rental can be stopped.
267: _validateRentalCanBeStoped(order.orderType, order.endTimestamp, order.lender);
Stop::_validateRentalCanBeStoped#L126-L154
126: function _validateRentalCanBeStoped(
127: OrderType orderType,
128: uint256 endTimestamp,
129: address expectedLender
130: ) internal view {
131: // Determine if the order has expired.
132: bool hasExpired = endTimestamp <= block.timestamp;
133:
134: // Determine if the fulfiller is the lender of the order.
135: bool isLender = expectedLender == msg.sender;
136:
137: // BASE orders processing.
138: if (orderType.isBaseOrder()) {
139: // check that the period for the rental order has expired.
140: if (!hasExpired) {
141: revert Errors.StopPolicy_CannotStopOrder(block.timestamp, msg.sender);
142: }
143: }
144: // PAY order processing.
145: else if (orderType.isPayOrder()) {
146: // If the stopper is the lender, then it doesnt matter whether the rental
147: // has expired. But if the stopper is not the lender, then the rental must have expired.
148: if (!isLender && (!hasExpired)) {
149: revert Errors.StopPolicy_CannotStopOrder(block.timestamp, msg.sender);
150: }
151: }
152: // Revert if given an invalid order type.
153: else {
154: revert Errors.Shared_OrderTypeNotSupported(uint8(orderType));
As we can see above, only the orderType, endTimestamp, and lender fields of the RentalOrder struct are used in order to validate whether or not a rental order can be stoppped. If a non-existent RentalOrder is supplied to the stopRent function, this check will pass if the orderType is a PAY order and the msg.sender for this call is the lender.
Next, a rentalAssetUpdates bytes array is constructed with respect to the NFTs specified in the RentalOrder.items field and the safe wallet specified in the RentalOrder.rentalWallet field. This rentalAssetsUpdate array contains information that ties together the rentalWallet and items fields supplied and is used to update storage when the RentalOrder is finally stopped (removed from storage).
272: bytes memory rentalAssetUpdates = new bytes(0);
273:
274: // Check if each item in the order is a rental. If so, then generate the rental asset update.
275: // Memory will become safe again after this block.
276: for (uint256 i; i < order.items.length; ++i) {
277: if (order.items[i].isRental()) { // @audit: `items[i]` has information regarding the NFT we wish to steal
278: // Insert the rental asset update into the dynamic array.
279: _insert(
280: rentalAssetUpdates,
281: order.items[i].toRentalId(order.rentalWallet), // @audit: `rentalWallet` is the victim (holds NFT)
282: order.items[i].amount
Note that the information in the rentalAssetsUpdates pertains to the rental order (rented NFT) that we are exploiting. No validation or state changes occur during this construction and therefore the execution continues to line 288:
288: if (order.hooks.length > 0) {
289: _removeHooks(order.hooks, order.items, order.rentalWallet);
290: }
The above lines of code are simple to bypass. If our supplied RentalOrder does not specify hooks, then the code on lines 288 - 290 will be skipped.
Next, the reclamation process is initiated:
292: // Interaction: Transfer rentals from the renter back to lender.
293: _reclaimRentedItems(order);
Stop::_reclaimRentedItem#L166-L177
166: function _reclaimRentedItems(RentalOrder memory order) internal {
167: // Transfer ERC721s from the renter back to lender.
168: bool success = ISafe(order.rentalWallet).execTransactionFromModule( // audit: perform call to victim
169: // Stop policy inherits the reclaimer package.
170: address(this),
171: // value.
172: 0,
173: // The encoded call to the `reclaimRentalOrder` function.
174: abi.encodeWithSelector(this.reclaimRentalOrder.selector, order),
175: // Safe must delegate call to the stop policy so that it is the msg.sender.
176: Enum.Operation.DelegateCall
177: );
The above code will initiate a delegate call via the RentalOrder.rentalWallet (victim). This call will invoke the following code:
Reclaimer::reclaimRentalOrder#L89-L99
89: // Transfer each item if it is a rented asset.
90: for (uint256 i = 0; i < itemCount; ++i) {
91: Item memory item = rentalOrder.items[i];
92:
93: // Check if the item is an ERC721.
94: if (item.itemType == ItemType.ERC721) // @audit: `item` points to targetted NFT
95: _transferERC721(item, rentalOrder.lender); // @audit: `lender` is defined by exploiter
96:
97: // check if the item is an ERC1155.
98: if (item.itemType == ItemType.ERC1155)
99: _transferERC1155(item, rentalOrder.lender);
32: function _transferERC721(Item memory item, address recipient) private {
33: IERC721(item.token).safeTransferFrom(address(this), recipient, item.identifier); // @audit: transferring NFT from victim safe to `lender`
34: }
35:
36: /**
37: * @dev Helper function to transfer an ERC1155 token.
38: *
39: * @param item Item which will be transferred.
40: * @param recipient Address which will receive the token.
41: */
42: function _transferERC1155(Item memory item, address recipient) private {
43: IERC1155(item.token).safeTransferFrom(
44: address(this), // @audit: delegate call, so address(this) == victim safe
45: recipient, // @audit: recipient is `lender`
46: item.identifier, // @audit: tokenId of target NFT
47: item.amount, // @audit: amount of target NFT
48: ""
49: );
As we can see above, the reclaimRentalOrder function will be invoked by the RentalOrder.rentalWallet via a delegate call and this will result in the target NFT being transferred out of the victim safe and to our specified lender address.
For this exploit, the lender address supplied will be a contract (attackerContract) and therefore a onERC721Received/onERC1155Received callback will be performed via this contract. During this callback, the order that will correspond to the supplied RentalOrder (currently non-existent) will be created and fulfilled. Here is a brief description of the steps that occur during this callback:
- The
attackerContractcreates an on-chainPAYorder (acting as lender). This order specifies the stolen NFT as the NFT to rent out. - The
attackerContractcreates a complimentary on-chainPAYEEorder (acting as renter). This order mirrors thePAYorder previously created. - The
attackerContractfulfills the orders viaSeaport::matchAdvancedOrders(acting as renter). Note that this will require a server-side signature to be retrieved beforehand. I will explain this further, along with other prerequisites for this exploit, in a later section. - The end result is that the stolen NFT has been sent to our specified
attacker safeand ourattackerContracthas sentxamount of ERC20 tokens to theescrowcontract for this rental order. (xcan be as little as1 wei). Our newly createdRentalOrderis then hashed and recorded in storage.
During our callback the Create::validateOrder function will be called by Seaport for our specified PAY order (this will also occur for our complimentary PAYEE order, but the PAYEE call does not result in any state changes). During this call the RentalOrder for our newly created order will be constructed and then the hash of that RentalOrder will be stored in storage, indicating an active rental:
Create::_rentFromZone#L579-L595
579: RentalOrder memory order = RentalOrder({
580: seaportOrderHash: seaportPayload.orderHash,
581: items: items,
582: hooks: payload.metadata.hooks,
583: orderType: payload.metadata.orderType,
584: lender: seaportPayload.offerer,
585: renter: payload.intendedFulfiller,
586: rentalWallet: payload.fulfillment.recipient,
587: startTimestamp: block.timestamp,
588: endTimestamp: block.timestamp + payload.metadata.rentDuration
589: });
590:
591: // Compute the order hash.
592: bytes32 orderHash = _deriveRentalOrderHash(order);
593:
594: // Interaction: Update storage only if the order is a Base Order or Pay order.
595: STORE.addRentals(orderHash, _convertToStatic(rentalAssetUpdates));
The above code shows how the RentalOrder is constructed. Notice that the RentalOrder struct contains a rentalWallet field. This struct is then passed into the _deriveRentalOrderHash function in order to derive the orderHash. The orderHash is then stored in storage via the Storage::addRentals call on line 595. Lets observe how this orderHash is computed:
Signer::_deriveRentalOrderHash#L181-L193
181: return
182: keccak256(
183: abi.encode(
184: _RENTAL_ORDER_TYPEHASH,
185: order.seaportOrderHash,
186: keccak256(abi.encodePacked(itemHashes)),
187: keccak256(abi.encodePacked(hookHashes)),
188: order.orderType,
189: order.lender,
190: order.renter, // @audit: rentalWallet field should be below this line
191: order.startTimestamp,
192: order.endTimestamp
193: )
According to the above code, the RentalOrder.rentalWallet field is not considered when creating the EIP-712 hash for the RentalOrder struct. Therefore, the RentalOrder.rentalWallet can be any address and the _deriveRentalOrderHash will produce a “correct” orderHash as long as all other fields pertain to an actual active order.
After the actions in the callback are performed, execution in the stopRent function continues to line 296:
295: // Interaction: Transfer ERC20 payments from the escrow contract to the respective recipients.
296: ESCRW.settlePayment(order);
PaymentEscrow::settlePayment#L320-L329
320: function settlePayment(RentalOrder calldata order) external onlyByProxy permissioned {
321: // Settle all payments for the order.
322: _settlePayment( // @audit: all order fields pertain to our newly created rental order, no issues here
323: order.items,
324: order.orderType,
325: order.lender,
326: order.renter,
327: order.startTimestamp,
328: order.endTimestamp
329: );
As we can see above, all the fields of the RentalOrder supplied, that are used in the _settlePayment function, pertain to our newly created RentalOrder. Remember, the only field that does not pertain to this new order is the RentalOrder.rentalWallet field, which points to the victim safe that was the previous owner of the stolen NFT. Therefore, execution in this function call will continue as expected for any PAY order: the lender and renter willl receive their pro-rata share of the rental payment that was sent to this escrow contract during fulfillment. Reminder: the attackerContract is both the lender and renter for this new order.
Finally, the RentalOrder supplied will be removed from storage:
299: STORE.removeRentals(
300: _deriveRentalOrderHash(order), // @audit: returns "correct" orderHash for our newly created RentalOrder
301: _convertToStatic(rentalAssetUpdates) // @audit: pertains to the victim's safe wallet + NFT
302: );
First, the orderHash for the supplied RentalOrder is retrieved via the _deriveRentalOrderHash internal function. As mentioned previously, the RentalOrder.rentalWallet is ignored when computing the orderHash and therefore the computed orderHash will correctly pertain to our newly created order (this is despite the fact that the supplied RentalOrder.rentalWallet points to the victim safe and not our attacker safe).
The _convertToStatic internal function on line 301 of Stop::stopRent will simply create a RentalAssetUpdate[] array which contains a rentalId that pertains the the victim safe and details of the target NFT.
The Storage::removeRentals function will then be called:
Storage::removeRentals#L216-L233
216: function removeRentals(
217: bytes32 orderHash, // @audit: orderHash for our newly created order
218: RentalAssetUpdate[] calldata rentalAssetUpdates
219: ) external onlyByProxy permissioned {
220: // The order must exist to be deleted.
221: if (!orders[orderHash]) { // @audit: orderHash was stored during callback
222: revert Errors.StorageModule_OrderDoesNotExist(orderHash);
223: } else {
224: // Delete the order from storage.
225: delete orders[orderHash]; // @audit: our order is removed from storage (stopped)
226: }
227:
228: // Process each rental asset.
229: for (uint256 i = 0; i < rentalAssetUpdates.length; ++i) {
230: RentalAssetUpdate memory asset = rentalAssetUpdates[i];
231:
232: // Reduce the amount of tokens for the particular rental ID.
233: rentedAssets[asset.rentalId] -= asset.amount; // @audit: storage update for victim's `rentalId`
As we can see above, our orderHash is removed from storage on line 225. And the state update on line 233 only pertains to the victim’s rental order. Note: When the victim’s rental order was created the rentedAssets[asset.rentalId] was incremented by asset.amount and here the mapping is simply being decremented by asset.amount. The attacker safe is now the owner of the target NFT. Since the orderHash corresponding to the RentalOrder, that the attackerContract created, was removed from storage during this call to stopRent, the stolen NFT will remain frozen in the attacker safe (only rentals with an orderHash stored in storage can be stopped). In addition, the rental payment for the exploited RentalOrder will remain frozen in the escrow contract since that rental order can also not be stopped. This is due to the fact that the affected order’s safe wallet no longer owns the NFT. Thus, the reclamation process will fail when the NFT is attempted to be transferred out of the safe wallet (the attacker safe is now the owner of these NFT). A malicious actor is able to perform this exploit to steal and affect multiple NFTs (and their respective rental orders) by utilizing the stopRentBatch function.
Prerequisites for the main exploit
Now I will discuss the prerequisites for this exploit. The first being that our attackerContract will require a valid server-side signature in order to fulfill the PAY and PAYEE orders created during the callback. Note: PAY orders require the fulfiller to also create a complimentary PAYEE order. The fulfiller is then expected to receive signatures for both of these orders and supply them together to the Seaport::matchAdvancedOrders function in order to fulfill both the PAY and PAYEE orders. Let us observe the digest that the server-side signer signs in order to create a valid signature:
Create::validateOrder#L759-L763
759: // Recover the signer from the payload.
760: address signer = _recoverSignerFromPayload(
761: _deriveRentPayloadHash(payload),
762: signature
763: );
Line 761 will return the digest and then the _recoverSignerFromPayload internal function will recover the signing address via the digest and the supplied signature. Zooming in on the _deriveRentPayloadHash function we will observe that this should return the EIP-712 hash of the RentPayload struct.
Signer::_deriveRentPayloadHash#L248-L260
248: function _deriveRentPayloadHash(
249: RentPayload memory payload
250: ) internal view returns (bytes32) {
251: // Derive and return the rent payload hash as specified by EIP-712.
252: return
253: keccak256(
254: abi.encode(
255: _RENT_PAYLOAD_TYPEHASH,
256: _deriveOrderFulfillmentHash(payload.fulfillment),
257: _deriveOrderMetadataHash(payload.metadata),
258: payload.expiration,
259: payload.intendedFulfiller
260: )
Below is the RentPayload struct:
154: struct RentPayload {
155: OrderFulfillment fulfillment; // @audit: safe wallet specified by fulfiller during signing
156: OrderMetadata metadata; // @audit: generic information pertaining to a rental
157: uint256 expiration; // @audit: defined during signing
158: address intendedFulfiller; // @audit: renter/fulfiller specified by fulfiller during signing
159: }
However, we will notice that the derived hash of the OrderMetadata struct computed in the _deriveOrderMetadataHash internal function is not EIP-712 compliant:
Signer::_deriveOrderMetadataHash#L218-L237
218: function _deriveOrderMetadataHash(
219: OrderMetadata memory metadata
220: ) internal view returns (bytes32) {
221: // Create array for hooks.
222: bytes32[] memory hookHashes = new bytes32[](metadata.hooks.length);
223:
224: // Iterate over each hook.
225: for (uint256 i = 0; i < metadata.hooks.length; ++i) {
226: // Hash the hook
227: hookHashes[i] = _deriveHookHash(metadata.hooks[i]);
228: }
229:
230: // Derive and return the metadata hash as specified by EIP-712.
231: return
232: keccak256(
233: abi.encode(
234: _ORDER_METADATA_TYPEHASH,
235: metadata.rentDuration,
236: keccak256(abi.encodePacked(hookHashes))
237: )
Below is the OrderMetadata struct:
50: struct OrderMetadata {
51: // Type of order being created.
52: OrderType orderType; // @audit: not included in derived nEIP-721 hash
53: // Duration of the rental in seconds.
54: uint256 rentDuration;
55: // Hooks that will act as middleware for the items in the order.
56: Hook[] hooks;
57: // Any extra data to be emitted upon order fulfillment.
58: bytes emittedExtraData; // @audit: not included in derived EIP-721 hash
59: }
As we can see above, the orderType and emittedExtraData fields are not considered when deriving the EIP-712 hash for the OrderMetadata struct. Since this OrderMetadata struct is present in the RentPayload struct, which is used as the signature digest, then the orderType can be changed and the derivation of the RentPayload EIP-712 hash will be “correct” (as long as the rentDuration and hooks fields are the correct). In addition, we will also notice that the OrderMetadata struct contains the only information in this digest that pertains to the order that the fulfiller is signing. Specifically, only the rentDuration and the hooks will be considered. The other fields in the RentPayload struct are supplied and therefore defined via the front-end whe
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 stopRent function validates the rental order only after transferring the NFT, and the order hash used for storage does not include the rentalWallet field, allowing a fake order to be created during the transfer callback and then matched to the victim's rental.
Impact
An attacker could steal any actively rented NFT and freeze the rental payments in the escrow contract.
What the attacker needed
Victim has an active rental; attacker can call stopRent as lender; attacker contract implements onERC721Received
How it was found
- Attacker calls stopRent with a non-existent PAY order where msg.sender is the lender and rentalWallet is the victim's safe.
- The function transfers the NFT from the victim's safe to the attacker's contract (lender).
- During the NFT transfer callback, the attacker creates and fulfills a matching rental order, sending a tiny payment to escrow.
- The function then settles the payment, returning the tiny payment to the attacker, and removes the newly created order from storage.
- The attacker ends up with the stolen NFT, and the victim's rental payment remains frozen in escrow.
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.
This is a bug in a smart contract that handles NFT rentals. When a rental is stopped, the contract sends the NFT back to the lender without first checking that the rental actually exists. An attacker can pretend to be the lender of someone else's rental, provide a fake order, and trick the contract into sending them the NFT. They can also create a real order during the transfer to make the system think everything is valid, and then take the rental payment too.
What is Access control?
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.