Fund loss or griefing in all order matching functions [ matchOneToOneOrders() , matchOneToManyOrders() , matchOrders() , takeMultipleOneOrders() , takeOrders() ] because condition (seller != buyer ) is not checked in any of them
The order matching functions do not check that the seller and buyer are different addresses, so a user's own buy and sell orders can be matched against each other. An attacker could exploit this to cause fund loss or griefing, for example by having a user's buy order filled with the user's own sell order, leaving the user without the expected NFT.
Originally disclosed on Code4rena
Submitted by unforgiven, also found by GreyArt
Functions matchOneToOneOrders(), matchOneToManyOrders(), matchOrders(), takeMultipleOneOrders(), takeOrders() are for order matching and order execution and they validate different things about orders but there is no check for that seller != buyer, which can cause wrong order matching resulting in fund lose or fund theft or griefing. (it can be combined with other vulns to perform more damaging attacks)
Proof of Concept
We only give proof of concept for matchOneToManyOrders() and other order execution/matching functions has similar bugs which root cause is not checking seller != buyer. This is matchOneToManyOrders() code:
/**
@notice Matches one order to many orders. Example: A buy order with 5 specific NFTs with 5 sell orders with those specific NFTs.
@dev Can only be called by the match executor. Refunds gas cost incurred by the
match executor to this contract. Checks whether the given complication can execute the match.
@param makerOrder The one order to match
@param manyMakerOrders Array of multiple orders to match the one order against
*/
function matchOneToManyOrders(
OrderTypes.MakerOrder calldata makerOrder,
OrderTypes.MakerOrder[] calldata manyMakerOrders
) external {
uint256 startGas = gasleft();
require(msg.sender == MATCH_EXECUTOR, 'OME');
require(_complications.contains(makerOrder.execParams[0]), 'invalid complication');
require(
IComplication(makerOrder.execParams[0]).canExecMatchOneToMany(makerOrder, manyMakerOrders),
'cannot execute'
);
bytes32 makerOrderHash = _hash(makerOrder);
require(isOrderValid(makerOrder, makerOrderHash), 'invalid maker order');
uint256 ordersLength = manyMakerOrders.length;
// the below 3 variables are copied to memory once to save on gas
// an SLOAD costs minimum 100 gas where an MLOAD only costs minimum 3 gas
// since these values won't change during function execution, we can save on gas by copying them to memory once
// instead of SLOADing once for each loop iteration
uint16 protocolFeeBps = PROTOCOL_FEE_BPS;
uint32 wethTransferGasUnits = WETH_TRANSFER_GAS_UNITS;
address weth = WETH;
if (makerOrder.isSellOrder) {
for (uint256 i = 0; i < ordersLength; ) {
// 20000 for the SSTORE op that updates maker nonce status from zero to a non zero status
uint256 startGasPerOrder = gasleft() + ((startGas + 20000 - gasleft()) / ordersLength);
_matchOneMakerSellToManyMakerBuys(
makerOrderHash,
makerOrder,
manyMakerOrders[i],
startGasPerOrder,
protocolFeeBps,
wethTransferGasUnits,
weth
);
unchecked {
++i;
}
}
isUserOrderNonceExecutedOrCancelled[makerOrder.signer][makerOrder.constraints[5]] = true;
} else {
uint256 protocolFee;
for (uint256 i = 0; i < ordersLength; ) {
protocolFee += _matchOneMakerBuyToManyMakerSells(
makerOrderHash,
manyMakerOrders[i],
makerOrder,
protocolFeeBps
);
unchecked {
++i;
}
}
isUserOrderNonceExecutedOrCancelled[makerOrder.signer][makerOrder.constraints[5]] = true;
uint256 gasCost = (startGas - gasleft() + WETH_TRANSFER_GAS_UNITS) * tx.gasprice;
// if the execution currency is weth, we can send the protocol fee and gas cost in one transfer to save gas
// else we need to send the protocol fee separately in the execution currency
// since the buyer is common across many sell orders, this part can be executed outside the above for loop
// in contrast to the case where if the one order is a sell order, we need to do this in each for loop
if (makerOrder.execParams[1] == weth) {
IERC20(weth).safeTransferFrom(makerOrder.signer, address(this), protocolFee + gasCost);
} else {
IERC20(makerOrder.execParams[1]).safeTransferFrom(makerOrder.signer, address(this), protocolFee);
IERC20(weth).safeTransferFrom(makerOrder.signer, address(this), gasCost);
}
}
}
in its executions it calls InfinityOrderBookComplication.canExecMatchOneToMany(), verifyMatchOneToManyOrders(), isOrderValid() to see that if orders are valid and one order matched to all other orders but there is no check for seller != buyer in any of those functions. and also ERC721 and ERC20 allows funds and assets to be transferred from address to itself. So it’s possible for matchOneToManyOrders() to match one user sell orders to its buy orders which can cause fund theft or griefing. This is the scenario for fund lose in matchOneToManyOrders():
- Let’s assume orders
NFTids are for one collection for simplicity. NFT ID[1]fair price is8 ETHandNFT ID[2]fair price is2 ETH.user1wants to buyNFT IDs[1,2]at10 ETH(both of them) so he create one buy order and signs it.user1wants to sellNFT ID[1]at2.5 ETHand sellNFT ID[2]at8.5 ETH. and he wants to sell them immediately after buying them so he create this two sell orders and sign them.attackerwho hasNFT ID[1]creates an sell order for it at7.5 ETHand signs it.- Off-chain machining engine sends this orders to
matchOneToManyOrders(): many orders = [(attacker sell ID[1] at 7.5 ETH),(user1 sell ID[1] at 2.5 ETH)] , one order =(user1 buy IDs[1,2] at 10ETH) - Function
matchOneToManyOrders()logic will check orders and their matching and all the checks would be passed for matching one order to many order(becase tokens lists intersects and numTokens are valids too (1+1=2)) - Function
matchOneToManyOrders()would execute order and transfer funds and tokens which would result in: (transferring7.5 ETHfromuser 1toattacker) (transferring2.5 ETHfromuser1touser1) (transferringNFT ID[1]fromattackertouser1) (transferringNFT ID[1]fromuser1touser1) - So in the end contract executed
user1buy order(user1 buy IDs[1,2] at 10ETH)butuseronly receivedNFT ID[1]and didn’t receivedNFT ID[2]so contract code perform operation contradiction to whatuser1has been signed.
Of course for this attack to work for matchOneToManyOrders() off-chain matching engine need to send wrong data but checks on the contract are not enough.
There are other scenarios for other functions that can cause griefing, for example for function matchOrders(): a user can have multiple order to buy some tokens in list of ids. it’s possible to match these old orders:
user1has this order: A:(user1 BUY 1 of IDs[1,2,3])and B:(user1 BUY 1 of IDs[1,4,5])- then the order B get executed for ID[1] and
user1become the owner ofID[1] user1wants to sell some of his tokens so he signs this order: C::(user1 SELL 1 of IDs[1,6,7])- matching engine would send order A and C with
constructedNfts=ID[1]tomatchOrders(). matchOrders()would check conditions and would see that conditions are met and perform the transaction.user1would pay some unnecessary order fee and it would become like griefing and DOS attack for him.
There may be other scenarios for this vulnerability to be harmful for users.
Tools Used
VIM
Recommended Mitigation Steps
Add some checks to ensure that seller != buyer
nneverlander (Infinity) confirmed and resolved:
Agree with assessment. Fixed in: https://github.com/infinitydotxyz/exchange-contracts-v2/commit/c3c0684ac02e0cf1c03cdbee7e68c5a37fa334a8
HardlyDifficult (judge) decreased severity to Medium and commented:
This is an interesting scenario where a buyer looking to flip immediately could have their order under filled.
Given the specifics of this scenario where the user needs to sign both a buy and a sell with the same NFTs, I’m inclined to rate this a Medium risk issue.
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 order matching functions lack a check that the buyer and seller addresses are different, allowing self-matching.
Impact
A user could lose funds or receive fewer NFTs than their signed order specified, or be griefed with unnecessary fees; the report notes the off-chain matching engine must send incorrect data for the fund-loss scenario.
What the attacker needed
User signs both a buy and a sell order; off-chain matching engine sends crafted order data; attacker has an NFT to sell
The fix
The issue was fixed by adding checks to ensure that the seller and buyer are not the same address.
How it was found
- User1 creates a buy order for NFT IDs 1 and 2 at 10 ETH and signs it.
- User1 creates sell orders for NFT ID 1 at 2.5 ETH and NFT ID 2 at 8.5 ETH and signs them.
- Attacker creates a sell order for NFT ID 1 at 7.5 ETH and signs it.
- Off-chain matching engine sends the attacker's sell order and user1's sell order for ID 1 as many orders, and user1's buy order as the one order, to matchOneToManyOrders().
- The function checks pass and executes the match, transferring 7.5 ETH from user1 to the attacker, 2.5 ETH from user1 to user1, NFT ID 1 from attacker to user1, and NFT ID 1 from user1 to user1.
- User1's buy order is executed but user1 only receives NFT ID 1 and not NFT ID 2, contradicting the signed order.
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.
In an NFT marketplace, orders to buy and sell are matched by a contract. If the same person's buy and sell orders are matched together, they end up trading with themselves, which can lose them money or leave them without the item they wanted. This report shows that the contract never checked that the buyer and seller are different people.
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.