Truncation in OrderValidator can lead to resetting the fill and selling more tokens
A partial order's fill fraction could be truncated to zero due to a uint120 overflow, allowing an attacker to reset the fill state and repeatedly fill the order, selling more tokens than intended.
Submitted by Spearbit, also found by 0xsanson, broccoli, cmichel, hyh, ming, OriDabush, Saw-mon_and_Natalie, and Yarpo
OrderValidator.sol#L228 OrderValidator.sol#L231 OrderValidator.sol#L237 OrderValidator.sol#L238
A partial order’s fractions (numerator and denominator) can be reset to 0 due to a truncation. This can be used to craft malicious orders:
-
Consider user Alice, who has 100 ERC1155 tokens, who approved all of their tokens to the
marketplaceContract. -
Alice places a
PARTIAL_OPENorder with 10 ERC1155 tokens and consideration of ETH. -
Malory tries to fill the order in the following way:
- Malory tries to fill 50% of the order, but instead of providing the fraction
1 / 2, Bob provides2**118 / 2**119. This sets thetotalFilledto2**118andtotalSizeto2**119. - Malory tries to fill 10% of the order, by providing
1 / 10. The computation2**118 / 2**119 + 1 / 10is done by “cross multiplying” the denominators, leading to the acutal fraction beingnumerator = (2**118 * 10 + 2**119)anddenominator = 2**119 * 10. - Because of the
uint120truncation in OrderValidator.sol#L228-L248, thenumeratoranddenominatorare truncated to0and0respectively. - Bob can now continue filling the order and draining any approved (1000 tokens in total) of the above ERC1155 tokens, for the same consideration amount!
- Malory tries to fill 50% of the order, but instead of providing the fraction
Proof of Concept
View full POC.
The following change would make the above POC fail:
modified contracts/lib/OrderValidator.sol
@@ -225,6 +225,8 @@ contract OrderValidator is Executor, ZoneInteraction {
// Update order status and fill amount, packing struct values.
_orderStatus[orderHash].isValidated = true;
_orderStatus[orderHash].isCancelled = false;
+ require(filledNumerator + numerator <= type(uint120).max, "overflow");
+ require(denominator <= type(uint120).max, "overflow");
_orderStatus[orderHash].numerator = uint120(
filledNumerator + numerator
);
@@ -234,6 +236,8 @@ contract OrderValidator is Executor, ZoneInteraction {
// Update order status and fill amount, packing struct values.
_orderStatus[orderHash].isValidated = true;
_orderStatus[orderHash].isCancelled = false;
+ require(numerator <= type(uint120).max, "overflow");
+ require(denominator <= type(uint120).max, "overflow");
_orderStatus[orderHash].numerator = uint120(numerator);
_orderStatus[orderHash].denominator = uint120(denominator);
}
Recommended Mitigation Steps
A basic fix for this would involve adding the above checks for overflow / truncation and reverting in that case. However, we think the mechanism is still flawed in some respects and requires more changes to fully fix it. See a related issue: “A malicious filler can fill a partial order in such a way that the rest cannot be filled by anyone” that points out a related but a more fundamental issue with the mechanism.
0xleastwood (judge) commented:
I’ve identified that this issue and all of its duplicates clearly outline how an attacker might overflow an order to continually fulfill an order at the same market price.
An instance where this issue might cause issues is during a restricted token sale. A relevant scenario is detailed as follows:
- A new token is created and the owner wishes to sell 50% of the token supply to the public.
- Because of an edge case in
OrderValidator, the order fulfillment can be reset to allow the public to more than 50% of the total token supply.- As a result, allocations intended to be distributed to investors and the team, will no longer be available.
- It is important to note, that additional tokens will be sold at the intended market price listed by the original order.
For these reasons, I believe this issue to be of high severity because it breaks certain trust assumptions made by the protocol and its userbase. By intentionally forcing a user to sell additional tokens, you are effectively altering the allocation of their wallet holdings, potentially leading to further funds loss as they may incur slippage when they have to sell these tokens back.
A great finding from all involved!
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 contract stores the fill fraction in a uint120 without checking that the numerator and denominator fit, so large values are truncated to zero.
Impact
An attacker could reset the fill state and repeatedly fill a partial order, causing the seller to sell more tokens than intended at the same price; the judge noted this could break trust assumptions in restricted token sales.
What the attacker needed
Victim has approved tokens to the marketplace; victim has placed a partial order; attacker can fill orders
The fix
The issue was fixed by adding overflow checks before storing the numerator and denominator, reverting if they exceed uint120.
How it was found
- Alice approves 100 ERC1155 tokens to the marketplace contract.
- Alice places a PARTIAL_OPEN order for 10 ERC1155 tokens with ETH consideration.
- Malory fills 50% of the order using a fraction with large numerator and denominator (2**118 / 2**119).
- Malory fills 10% of the order using 1/10, causing the combined fraction to overflow uint120 and truncate to 0/0.
- Malory continues filling the order, draining all 1000 approved ERC1155 tokens for the same consideration.
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.
When you partially fill an order, the contract records how much you've filled as a fraction. If the numbers in that fraction get too large, they are cut off and become zero, so the contract forgets that any filling happened. An attacker can then fill the order again and again, getting more tokens than the seller intended to sell.
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.