Settlement of batch auction can exceed the gas limit
Settling a batch auction iterates over all bids, which can exceed the block gas limit if there are too many bids, making settlement impossible. This causes sellers and bidders to lose their funds.
0xR360 Follow with Kose, MrjoryStewartBaxter and 2 others
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-03-axis-finance-judging/issues/237
Found by
0xR360, Kose, MrjoryStewartBaxter, flacko, shaka
Summary
Settlement of batch auction can exceed the gas limit, making it impossible to settle the auction.
Vulnerability Detail
When a batch auction (EMPAM) is settled, to calculate the lot marginal price, the contract iterates over all bids until the capacity is reached or a bid below the minimum price is found.
As some of the operations performed in the loop are gas-intensive, the contract may run out of gas if the number of bids is too high.
Note that additionally, there is another loop in the _settle function that iterates over all the remaining bids to delete them from the queue. While this loop consumes much less gas per iteration and would require the number of bids to be much higher to run out of gas, it adds to the problem.
Impact
Settlement of batch auction will revert, causing sellers and bidders to lose their funds.
Code Snippet
Proof of concept
Change the minimum bid percent to 0.1% in the EmpaModuleTest contract in EMPAModuleTest.sol.
- uint24 internal constant _MIN_BID_PERCENT = 1000; // 1%
+ uint24 internal constant _MIN_BID_PERCENT = 100; // 0.1%
Add the following code to the contract EmpaModuleSettleTest in settle.t.sol and run forge test --mt test_settleOog.
modifier givenBidsCreated() {
uint96 amountOut = 0.01e18;
uint96 amountIn = 0.01e18;
uint256 numBids = 580;
for (uint256 i = 0; i < numBids; i++) {
_createBid(_BIDDER, amountIn, amountOut);
}
_;
}
function test_settleOog() external
givenLotIsCreated
givenLotHasStarted
givenBidsCreated
givenLotHasConcluded
givenPrivateKeyIsSubmitted
givenLotIsDecrypted
{
uint256 gasBefore = gasleft();
_settle();
assert(gasBefore - gasleft() > 30_000_000);
}
Tool used
Manual Review
Recommendation
An easy way to tackle the issue would be to change the _MIN_BID_PERCENT value from 10 (0.01%) to 1000 (1%) in the EMPAM.sol contract, which would limit the number of iterations to 100.
A more appropriate solution, if it is not acceptable to increase the min bid percent, would be to change the settlement logic so that can be handled in batches of bids to avoid running out of gas.
In both cases, it would also be recommended to limit the number of decrypted bids that can be deleted from the queue in a single transaction.
Discussion
Oighty
Acknowledge. This is valid. We had changed the queue implementation to be less gas intensive on inserts, but it ended up making removals (i.e. settle) more expensive. A priority for us is supporting as many bids on settlement as we can (which allows smaller bid sizes). We're likely going to switch to a linked list implementation to achieve this.
sherlock-admin4
The protocol team fixed this issue in the following PRs/commits: https://github.com/Axis-Fi/moonraker/pull/137
10xhash
The protocol team fixed this issue in the following PRs/commits: Axis-Fi/moonraker#137
Fixed The implementation is changed from heap to linked list to reduce the gas cost and the max bid count for settlement is reduced to 2500 making the max gas expenditure around 8million for settlement
sherlock-admin4
The Lead Senior Watson signed off on the fix.
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 settlement function iterates over all bids without a cap, and the gas cost per iteration is high enough that a large number of bids exceeds the block gas limit.
Impact
Settlement of the batch auction reverts, causing sellers and bidders to lose their funds.
What the attacker needed
A batch auction with many bids; Minimum bid percent set low enough to allow many bids
The fix
The protocol team fixed this issue by changing the queue implementation from a heap to a linked list to reduce gas cost and by capping the maximum number of bids for settlement at 2500.
How it was found
- Set the minimum bid percent to 0.1% in the test contract.
- Create 580 bids for the auction.
- Conclude the auction and decrypt the bids.
- Call the settle function and observe that it consumes more than 30 million gas.
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 a batch auction, all bids are processed together when the auction ends. The smart contract loops through every bid to calculate the final price and clean up, but if there are too many bids, the transaction runs out of gas and fails. This means the auction can never be settled, and the money put in by sellers and bidders gets stuck.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.