Skip to content
Smart contracts Audit finding Advanced

Wrong items length assertion in basic order

An off-by-one error in a length assertion allowed a fulfiller to craft calldata with an empty additionalRecipients array while the order expected more, causing an out-of-bounds read that skipped transferring the last consideration item. This let the fulfiller pay less than the full consideration amount, though only for basic orders with multiple consideration items and a low-decimal ERC20 as the last item.

0xsanson Follow

Originally disclosed on Code4rena

Submitted by 0xsanson, also found by cmichel

BasicOrderFulfiller.sol#L346-L349

When fulfilling a basic order we need to assert that the parameter totalOriginalAdditionalRecipients is less or equal than the length of additionalRecipients written in calldata. However in _prepareBasicFulfillmentFromCalldata this assertion is incorrect (L346):

        // Ensure supplied consideration array length is not less than original.
        _assertConsiderationLengthIsNotLessThanOriginalConsiderationLength(
            parameters.additionalRecipients.length + 1,
            parameters.totalOriginalAdditionalRecipients
        );

The way the function is written (L75), it accepts also a length smaller than the original by 1 (basically there shouldn’t be a + 1 in the first argument).

Interestingly enough, in the case additionalRecipients.length < totalOriginalAdditionalRecipients, the inline-assembly for-loop at (L506) will read consideration items out-of-bounds. This can be a vector of exploits, as illustrated below.

Proof of Concept

Alice makes the following offer: a basic order, with two considerationItems. The second item has the following data:

consideration[1] = {
	itemType: ...,
	token: ...,
	identifierOrCriteria: ...,
	startAmount: X,
	endAmount: X,
	recipient: Y,
}

The only quantities we need to track are the amounts X and recipient Y.

When fulfilling the order normally, the fulfiller will spend X tokens sending them to Y. It’s possible however to exploit the previous bug in a way that the fulfiller won’t need to make this transfer.

To do this, the fulfiller needs to craft the following calldata:

calldata pointer | correct calldata | exploit calldata |

… | … | … |

0x204 | 1 (tot original) | 1 (tot original) |

0x224 | 0x240 (head addRec) | 0x240 (head addRec) |

0x244 | 0x2a0 (head sign) | 0x260 (head sign) |

0x264 | 1 (length addRec) | 0 (length addRec) |

0x284 | X (amount) | X (length sign) |

0x2a4 | Y (recipient) | Y (sign body) |

0x2c4 | 0x40 (length sign) | 0x00 (sign body) |

0x2e4 | [correct Alice sign] | … |

0x304 | [correct Alice sign] | … |

| | |

Basically writing additionalRecipients = [] and making the signature length = X, with Y being the first 32 bytes. Of course this signature will be invalid; however it doesn’t matter since the exploiter can call validate with the correct signature beforehand.

The transaction trace will look like this:

  • the assertion _assertConsiderationLengthIsNotLessThanOriginalConsiderationLength passes;
  • the orderHash calculated is the correct one, since the for-loop over original consideration items picks up calldata at pointers {0x284, 0x2a4} (L513);
  • the order was already validated beforehand, so the signature isn’t read;
  • at the end, during the tokens transfers, only offer and consideration[0] are transferred, since the code looks at additionalRecipients which is empty.

Conclusion:

Every Order that is “basic” and has two or more consideration items can be fulfilled in a way to not trade the last consideration item in the list. The fulfiller spends less then normally, and a recipient doesn’t get his due.

There’s also an extra requirement which is stricter: this last item’s startAmount (= endAmount) needs to be smallish (< 1e6). This is because this number becomes the signature bytes length, and we need to fill the calldata with extra zeroes to complete it. Realistically then the exploit will work only if the item is a ERC20 will low decimals.

I’ve made a hardhat test that exemplifies the exploit. (Link to gist)

Recommended Mitigation Steps

Remove the +1 at L347.

0age (OpenSea) confirmed, but disagreed with severity and commented:

Valid finding on the off-by-one error, this was already reported to us outside of c4 and we’re going to fix — will mention that it’s very difficult to find / craft exploitable payloads though, so severity should be lower.

HardlyDifficult (judge) decreased severity to Medium

0xleastwood (judge) commented:

While the issue outlines an exploit whereby an attacker can fulfill an order without paying the entire consideration amount, it does require a set of requirements, namely:

  • The item is an ERC20 with low decimals.
  • The order has considerationItems > 1.

Maximum extractable value for the most prevalent ERC20 token with low decimals, WBTC. This token uses 8 decimals and currently we know that calldata uses 16 gas for each byte used. Based on a block gas limit of 30,000,000, we can deduce that the calldata length has an upper bound of 1.875 MB. Based on this, the maximum extractable value would be (1,875,000 / 1e8) * $20,000 USD = $375 USD, assuming the price for each WBTC is $20,000 USD.

Relevant EIP detailing this is found at https://eips.ethereum.org/EIPS/eip-4488.

It is also important to note, that by utilising the entire available block space on Ethereum, it is very likely that the cost of the transaction will far exceed the amount received in the attack.

The attack does in fact leak value by allowing orders to be fulfilled at a slight discount. However, because this only affects very specific order types, I believe medium severity to be justified.

2 — Med: Assets not at direct risk, but the function of the protocol or its availability could be impacted, or leak value with a hypothetical attack path with stated assumptions, but external requirements.

This was one of the most interesting issues I’ve read, kudos to those who found it!

0age (OpenSea) resolved:

PR: ProjectOpenSea/seaport#317


The original report as published on Code4rena, by 0xsanson. 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 an off-by-one error: the assertion added 1 to the length of additionalRecipients before comparing, so it accepted a length one less than the original, and the subsequent loop read out of bounds.

Impact

A fulfiller could pay less than the full consideration amount by skipping the last consideration item, but only for basic orders with multiple consideration items where the last item is a low-decimal ERC20 with a smallish amount, and the transaction cost may exceed the gain.

What the attacker needed

Basic order with two or more consideration items; last item is a low-decimal ERC20; last item amount is smallish (< 1e6); order already validated

The fix

The off-by-one was fixed by removing the `+1` from the assertion, as confirmed by the resolved PR.

How it was found

  1. Craft calldata with additionalRecipients length set to 0 and signature length set to the amount X of the last consideration item.
  2. Call validate with the correct signature beforehand so the order is already validated.
  3. Call the basic fulfillment function with the crafted calldata; the assertion passes and the order hash is computed correctly from the out-of-bounds read.
  4. The signature is not read because the order was already validated, and only offer and consideration[0] are transferred.

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.

Smart contracts on Ethereum can be tricked by malformed input data. Here, a check that was supposed to ensure enough data was provided had a small counting mistake, so an attacker could supply less data than required. The contract then read past the end of the provided list and skipped a payment, letting the attacker pay less.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by 0xsanson

View all

8 more disclosed reports, best first.

More from OpenSea Seaport contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.