Downcasting to uint96 can cause assets to be lost for some tokens
The total bid amount in an auction is downcast from uint256 to uint96 without checks, which can overflow for tokens with low unit value. This leads to a loss of funds for the auctioneer when the total exceeds the maximum uint96 value.
Use a higher type or warn the user's of the limitations on the auction sizes
Discussion
0xJem
Duplicate of #34
Oighty
Pretty similar to #209. Might be a duplicate.
nevillehuang
Agree both hinges on a high totalAmountIn
kosedogus
Escalate
Since there are minutes until the end of auction period, I might miss something, if that is the case sorry about that.
_settle calls _getLotMarginalPrice to get the totalAmountIn.
The loop which adds amountIn's to totalAmountIn does not add every individual bid, if the latest bid filled the capacity loop breaks.
capacity is taken from lotData as we can see:
Solidity
uint256 capacity = lotData[lotId_].capacity;
And the capacity in lotData is uint96:
Solidity
mapping(uint96 id => Lot lot) public lotData;
...
struct Lot {
uint48 start; // 6 +
uint48 conclusion; //
uint8 quoteTokenDecimals;
uint8 baseTokenDecimals;
bool capacityInQuote;
uint96 capacity;
uint96 sold;
uint96 purchased;
uint96 partialPayout;
}
Hence the capacity itself is below max value of uint96 inherently, and if we exceed capacity with the latest bid, then loop breaks.
So what happens to latest bid?
It's bidId is recorded and it is only partially filled, the excess is removed from totalAmountIn as we can see below:
if (result.partialFillBidId != 0) {
// Load routing and bid data
Bid storage bidData = bids[lotId_][result.partialFillBidId];
// Set the bidder on for the partially filled bid
settlement_.pfBidder = bidData.bidder;
settlement_.pfReferrer = bidData.referrer;
// Calculate the payout and refund amounts
uint256 fullFill =
Math.mulDivDown(uint256(bidData.amount), baseScale, result.marginalPrice);
uint256 excess = result.capacityExpended - capacity;
settlement_.pfPayout = uint96(fullFill - excess);
settlement_.pfRefund =
uint96(Math.mulDivDown(uint256(bidData.amount), excess, fullFill));
// Reduce the total amount in by the refund amount
result.totalAmountIn -= settlement_.pfRefund;
Hence it seems like totalAmountIn can not possibly pass capacity which is uint96. If it can not pass uint96, there can't be any overflow.
sherlock-admin2
Escalate
Since there are minutes until the end of auction period, I might miss something, if that is the case sorry about that.
_settle calls _getLotMarginalPrice to get the totalAmountIn.
The loop which adds amountIn's to totalAmountIn does not add every individual bid, if the latest bid filled the capacity loop breaks.
capacity is taken from lotData as we can see:
Solidity
uint256 capacity = lotData[lotId_].capacity;
And the capacity in lotData is uint96:
Solidity
mapping(uint96 id => Lot lot) public lotData;
...
struct Lot {
uint48 start; // 6 +
uint48 conclusion; //
uint8 quoteTokenDecimals;
uint8 baseTokenDecimals;
bool capacityInQuote;
uint96 capacity;
uint96 sold;
uint96 purchased;
uint96 partialPayout;
}
Hence the capacity itself is below max value of uint96 inherently, and if we exceed capacity with the latest bid, then loop breaks.
So what happens to latest bid?
It's bidId is recorded and it is only partially filled, the excess is removed from totalAmountIn as we can see below:
if (result.partialFillBidId != 0) {
// Load routing and bid data
Bid storage bidData = bids[lotId_][result.partialFillBidId];
// Set the bidder on for the partially filled bid
settlement_.pfBidder = bidData.bidder;
settlement_.pfReferrer = bidData.referrer;
// Calculate the payout and refund amounts
uint256 fullFill =
Math.mulDivDown(uint256(bidData.amount), baseScale, result.marginalPrice);
uint256 excess = result.capacityExpended - capacity;
settlement_.pfPayout = uint96(fullFill - excess);
settlement_.pfRefund =
uint96(Math.mulDivDown(uint256(bidData.amount), excess, fullFill));
// Reduce the total amount in by the refund amount
result.totalAmountIn -= settlement_.pfRefund;
Hence it seems like totalAmountIn can not possibly pass capacity which is uint96. If it can not pass uint96, there can't be any overflow.
You've created a valid escalation!
To remove the escalation from consideration: Delete your comment.
You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.
kosedogus
Since there are minutes until the end of auction period, I might miss something, if that is the case sorry about that.
@kosedogus I do not quite get your escalation point. Maybe a PoC could help me decipher it. I see a clear loss of funds here from downcasting.
Cc @10xhash @Oighty
Oighty
Adding a bunch of uint96 amounts together can exceed type(uint96).max so casting totalAmountIn from a uint256 to a uint96 can overflow.
kosedogus
What I was saying, capacity is itself uint96.During loop that adds amountIn's together, everything copied as a uint256 and calculations are done with uint256 so that overflow won't occur. If adding a bid to totalAmountIn made it pass capacity (which is normally uint96, but for the purpose of preventing overflow it is copied as uint256 before this check), then loop breaks. The amount that exceeds capacity removed from totalAmountIn before it is downcasted to uint96. So totalAmountIn can be at most same with capacity in the end, which is uint96. So there won't be overflow.
Evert0x
@nevillehuang any reply to the latest comment?
nevillehuang
@kosedogus @10xhash Could you guys verify the escalation comment? Based on comment here overflow is still possible no on the last bid added correct? I think a PoC could verify the claim and the issue.
10xhash
@kosedogus @10xhash Could you guys verify the escalation comment? Based on comment here overflow is still possible no on the last bid added correct? I think a PoC could verify the claim and the issue.
The capacity is the amount of base token the seller wants to sell while amountIn is the amount of quote tokens that are paid by buyers. So the uint96 constrain on capacity is not related with totalAmountIn
The original report as published on Sherlock, by FindEverythingX. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Generating the AI write-up for this report.
This usually takes about a minute. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
Likely the code casts a uint256 total to uint96 without checking if it exceeds the maximum value that uint96 can hold.
Impact
The auctioneer could suffer extreme loss when auctions bring in more than the maximum uint96 amount of tokens.
What the attacker needed
Auction total exceeds uint96 max
The fix
The issue was fixed by using uint256 instead of uint96 for the total amount, avoiding the unsafe casting.
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.
Generating the AI write-up for this report.
This usually takes about a minute. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
In smart contracts, numbers have fixed sizes. A uint96 can only hold values up to about 7.9e28. If you try to store a larger number in it, it wraps around to a small value. Here, the total amount of tokens bid in an auction is stored in a uint96, but for tokens like Shiba Inu, the total can easily exceed that limit, causing the recorded total to be wrong and the auctioneer to lose funds.
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.