InfinityExchange computes gas refunds in a way where the first order’s buyer pays less than the later ones
The gas refund calculation in the InfinityExchange contract incorrectly computes the starting gas per order inside a loop, causing later orders in a batch to pay more gas than earlier ones. This results in a loss of funds for buyers whose orders come later in the batch.
Submitted by Ruhum, also found by 0xf15ers, 0xsanson, antonttc, kenzo, and WatchPug
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityExchange.sol#L149
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityExchange.sol#L202
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityExchange.sol#L273
Impact
The way the gas refunds are computed in the InfinityExchange contract, the first orders pay less than the latter ones. This causes a loss of funds for the buyers whose orders came last in the batch.
Proof of Concept
The issue is that the startGasPerOrder variable is computed within the for-loop. That causes the first iterations to be lower than later ones.
Here’s an example for the following line: https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityExchange.sol#L202 To make the math easy we use the following values:
startGas = 1,000,000
gasPerOrder = 100,000 (so fulfilling an order costs us 100,000 gas)
ordersLength = 10
For the 2nd order we then get:
startGasPerOrder = 900,000 + ((1,000,000 + 20,000 - 900,000) / 10)
startGasPerOrder = 912,000
For the 9th order we get:
startGasPerOrder = 200,000 + ((1,000,000 + 20,000 - 200,000) / 10)
startGasPerOrder = 282,000
The startGasPerOrder variable is passed through a couple of functions without any modification until it reaches a line like this: https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityExchange.sol#L231
uint256 gasCost = (startGasPerOrder - gasleft() + wethTransferGasUnits) * tx.gasprice;
There, the actual gas costs for the user are computed.
In our case, that would be:
# 2nd order
# gasleft() is 800,00 because we said that executing the order costs ~100,000 gas. At the beginning of the order, it was 900,000 so now it's 800,000. This makes the computation a little more straightforward although it's not 100% correct.
gasCost = (912,000 - 800,000 + 50,000) * 1
gasCost = 162,000
# 9th order
gasCost = (282,000 - 100,000 + 50,000) * 1
gasCost = 232,000
So the 2nd order’s buyer pays 162,000 while the 9th order’s buyer pays 232,000.
As I said the math was dumbed down a bit to make it easier. The actual difference might not be as big as shown here. But, there is a difference.
Recommended Mitigation Steps
The startGasPerOrder variable should be initialized outside the for-loop.
nneverlander (Infinity) confirmed and resolved:
Agree with risk assessment.
HardlyDifficult (judge) decreased severity to Medium and commented:
When multiple orders are processed in batch, some users pay more than their expected share of gas costs.
Although the impact may be relatively small values, this appears to be a common path and would result in taking more value than expected from many users during normal usage. Rating this a Medium risk issue as it leaks value impacting users who are not first in a batch transaction.
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
The variable `startGasPerOrder` is computed inside the for-loop, so its value changes with each iteration, leading to inconsistent gas cost calculations.
Impact
Buyers whose orders come later in a batch pay more gas than expected, resulting in a loss of funds; the impact may be relatively small per order but affects many users in normal usage.
What the attacker needed
Batch of multiple orders
The fix
The `startGasPerOrder` variable should be initialized outside the for-loop, and the fix was confirmed by the project.
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 make a purchase on a blockchain, you pay a fee called gas. This contract tries to refund some gas to buyers, but it calculates the refund incorrectly for batches of orders. The first buyer gets a bigger refund than later buyers, so later buyers lose money.
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.