Auction can potentially sell more contracts than it has collateral for
The auction contract's withdrawal function incorrectly calculates the number of contracts sold because it resets the count for each withdrawal, allowing more contracts to be sold than the total collateral available. An attacker could withdraw more than their fair share, leaving the protocol with insufficient collateral and creating naked options.
yixxas Follow with hansfriese
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2022-09-knox-judging/issues/80
Found by
yixxas, hansfriese
Summary
auction.totalContracts is determined by the amount of collateral the protocol has received. After an auction has ended, users are allowed to withdraw and what they receive depends on whether their orders have filled, or they receive a refund, or a mixture of both. However, wrong accounting in _previewWithdraw() can lead to the fill and refund value to be calculated wrongly.
Vulnerability Detail
Each time a withdrawal is made, the order is removed from the order book as seen in L339 in _previewWithdraw(). Now, the issue here is in the line totalContractsSold += data.size. If a user with an order that is higher priced, that is with a lower index in the order book( since order book is in decreasing order based on price ), chooses to withdraw first, their order is removed from the order book. Now, the next user who does a withdraw will call this same function, but totalContractsSold is calculated from 0 again. This leads to problems and I illustrate with a simple example below.
Assume,
auction.totalContracts = 10
Alice first addLimitOrder() with price = 10, size = 10.
Bob then addLimitOrder() with price = 10, 'size = 1'.
Now order book have 2 orders.
processOrder() is then called, since utilisation reaches a 100%, clearing price is set to 10 and all contracts are sold to Alice.
Now, Alice does a withdraw first and withdraws successfully with fill = 10, refund = 0. Now Bob tries to withdraw, the previous order is removed from the order book, so when i = 1, it enters the if (data.price64x64 >= lastPrice64x64) check. This time, if (totalContractsSold + data.size >= auction.totalContracts) check does not pass since totalContractsSold = 0, data.size = 1, auction.totalContracts = 10. It will then enter the else part where fill += data.size. This means that additional contracts are being sold to Bob even though it has been previously sold to Alice which exceeds the limit that the collateral allows.
Impact
Auction is selling more contracts than it has collateral for which creates plenty of liquidity problems / issues with risk as options are now "naked".
Code Snippet
function _previewWithdraw(
AuctionStorage.Layout storage l,
bool isPreview,
uint64 epoch,
address buyer
) private returns (uint256, uint256) {
...
uint256 totalContractsSold;
...
// traverse the order book and return orders placed by the buyer
for (uint256 i = 1; i <= length; i++) {
OrderBook.Data memory data = orderbook._getOrderById(next);
next = orderbook._getNextOrder(next);
if (data.buyer == buyer) {
if (
lastPrice64x64 < type(int128).max &&
data.price64x64 >= lastPrice64x64
) {
// if the auction has not been cancelled, and the order price is greater than or
// equal to the last price, fill the order and calculate the refund amount
uint256 paid = data.price64x64.mulu(data.size);
uint256 cost = lastPrice64x64.mulu(data.size);
if (
totalContractsSold + data.size >= auction.totalContracts
) {
// if part of the current order exceeds the total contracts available, partially
// fill the order, and refund the remainder
uint256 remainder =
auction.totalContracts - totalContractsSold;
cost = lastPrice64x64.mulu(remainder);
fill += remainder;
} else {
// otherwise, fill the entire order
fill += data.size;
}
// the refund takes the difference between the amount paid and the "true" cost of
// of the order. the "true" cost can be calculated when the clearing price has been
// set.
refund += paid - cost;
} else {
// if last price >= type(int128).max, auction has been cancelled, only send refund
// if price < last price, the bid is too low, only send refund
refund += data.price64x64.mulu(data.size);
}
if (!isPreview) {
// when a withdrawal is made, remove the order from the order book
orderbook._remove(data.id);
}
}
totalContractsSold += data.size;
}
return (refund, fill);
}
Tool used
Manual Review
Recommendation
This problem arises due to how orders that have the same price as the clearing price, yet should not be filled due to exceeding the limit is not accounted for. A check in _previewWithdraw() needs to be done to prevent this edge case.
I believe removing an order from the order book after withdrawal is done to prevent multiple withdrawals from the same user. If this is the case, we can use a mapping to check this instead, so that totalContractsSold remains accurate. We can then refund users once this exceeds auction.totalContracts.
Discussion
0xCourtney
https://github.com/KnoxFinance/knox-contracts/pull/80
rcstanciu
Reply from @arbitrary-CodeBeholder
looks good
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 tracking total contracts sold is reset to zero for each withdrawal instead of being persisted across withdrawals, so the limit check fails after the first withdrawal.
What the attacker needed
Auction with multiple orders; users can withdraw after auction ends
The fix
The withdrawal logic should be changed to track total contracts sold across all withdrawals, for example by using a mapping to prevent multiple withdrawals instead of removing orders, and refunding users once the total exceeds the available collateral.
How it was found
- Alice places a limit order for 10 contracts at price 10.
- Bob places a limit order for 1 contract at price 10.
- The auction processes orders and sets the clearing price to 10, selling all 10 contracts to Alice.
- Alice withdraws and receives 10 filled contracts and 0 refund.
- Bob withdraws; because Alice's order was removed, the totalContractsSold resets to 0, so Bob's order is fully filled (1 contract) even though all 10 were already sold.
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 auction, the contract promises to sell a limited number of items (contracts) based on how much collateral it holds. When users withdraw, the code is supposed to track how many have been sold so far, but it resets that count for each withdrawal. This means later users can get more items than they should, leaving the auction short of collateral.
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.