Reentrancy from matchOneToManyOrders
The matchOneToManyOrders function updates the maker order nonce only after transferring tokens, allowing a malicious contract to re-enter and execute the same order again via takeOrders. This lets an attacker cause a user's order to be fulfilled twice, resulting in the user buying more NFTs than intended and losing funds.
Submitted by kenzo, also found by 0xDjango
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityExchange.sol#L178
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityExchange.sol#L216
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityExchange.sol#L230
Vulnerability Details
matchOneToManyOrders doesn’t conform to Checks-Effects-Interactions pattern, and updates the maker order nonce only after the NFTs and payment have been sent. Using this, a malicious user can re-enter the contract and re-fulfill the order using takeOrders.
Impact
Orders can be executed twice. User funds would be lost.
Proof of Concept
matchOneToManyOrders will set the order nonce as used only after the tokens are being sent:
function matchOneToManyOrders(OrderTypes.MakerOrder calldata makerOrder, OrderTypes.MakerOrder[] calldata manyMakerOrders) external {
...
if (makerOrder.isSellOrder) {
for (uint256 i = 0; i < ordersLength; ) {
...
_matchOneMakerSellToManyMakerBuys(...); // @audit will transfer tokens in here
...
}
//@audit setting nonce to be used only here
isUserOrderNonceExecutedOrCancelled[makerOrder.signer][makerOrder.constraints[5]] = true;
} else {
for (uint256 i = 0; i < ordersLength; ) {
protocolFee += _matchOneMakerBuyToManyMakerSells(...); // @audit will transfer tokens in here
...
}
//@audit setting nonce to be used only here
isUserOrderNonceExecutedOrCancelled[makerOrder.signer][makerOrder.constraints[5]] = true;
...
}
So we can see that tokens are being transferred before nonce is being set to executed.
Therefore, POC for an attack - Alice wants to buy 2 unspecified WolfNFT, and she will pay via AMP, an ERC-777 token. Malicious user Bob will set up an offer to sell 2 WolfNFT. The MATCH_EXECUTOR will match the offers. Bob will set up a contract such that upon receiving of AMP, it will call takeOrders with Alice’s order, and 2 other WolfNFTs. (Note that although takeOrders is nonReentrant, matchOneToManyOrders is not, and so the reentrancy will succeed.)
So in takeOrders, the contract will match Alice’s order with Bob’s NFTs, and then set Alice’s order’s nonce to true, then matchOneToManyOrders execution will resume, and again will set Alice’s order’s nonce to true.
Alice ended up buying 4 WolfNFTs although she only signed an order for 2. Tough luck, Alice.
(Note: a similar attack can be constructed via ERC721’s onERC721Received.)
Recommended Mitigation Steps
Conform to CEI and set the nonce to true before executing external calls.
nneverlander (Infinity) confirmed and resolved:
HardlyDifficult (judge) commented:
Great catch! Agree with the assessment.
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
Impact
Orders can be executed twice, causing user funds to be lost.
What the attacker needed
Victim signs an order; attacker deploys a malicious contract; payment token supports callbacks (ERC-777 or ERC-721)
The fix
The fix is to follow the checks-effects-interactions pattern by setting the nonce to true before making any external calls.
How it was found
- Alice signs an order to buy 2 WolfNFTs paying with AMP, an ERC-777 token.
- Bob sets up an offer to sell 2 WolfNFTs and deploys a contract that, upon receiving AMP, calls takeOrders with Alice's order and 2 other WolfNFTs.
- The MATCH_EXECUTOR calls matchOneToManyOrders, which transfers tokens to Bob's contract before setting Alice's order nonce.
- Bob's contract re-enters via takeOrders, matching Alice's order with Bob's NFTs and setting the nonce.
- matchOneToManyOrders resumes and sets the nonce again, but Alice has now bought 4 WolfNFTs instead of 2.
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.
A smart contract function updates a record of whether an order has been used only after it sends tokens. A malicious contract can call back into the exchange before that update happens and use the same order again. This means the victim ends up buying more items than they agreed to.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.