Submitted by Trust, also found by V_B, cryptonue, PwnedNoMore, KIntern_NA, fs0c, cryptphi, bin2chen, JTJabba, HE1M, Picodes, hansfriese, KingNFT, R2, M4TZ1P, and 8olidity
HIGH: Attacker can steal any funds in the contract by state confusion (no preconditions).
LOC:
https://github.com/code-423n4/2022-11-size/blob/706a77e585d0852eae6ba0dca73dc73eb37f8fb6/src/SizeSealed.sol#L33
https://github.com/code-423n4/2022-11-size/blob/706a77e585d0852eae6ba0dca73dc73eb37f8fb6/src/SizeSealed.sol#L238
Auctions in SIZE can be in one of several states, as checked in the atState() modifier:
Its important to note that if current block timestamp is greater than endTimestamp, a.data.lowestQuote is used to determine if finalize() was called.
The value is set to max at createAuction.
In finalize, it is set again, using user-controlled input:
The issue is that it is possible to break the state machine by calling finalize() and setting lowestQuote to type(uint128).max. If the other parameters are crafted correctly, finalize() will succeed and perform transfers of unsold base amount and traded quote amount:
Critically, attacker will later be able to call cancelAuction() and cancelBid(), as they are allowed as long as the auction has not finalized:
The attack will look as follows:
Note that the values of minimumBidQuote, reserveQuotePerbase must be carefully chosen to satisfy all the inequality requirements in createAuction(), bid() and finalize(). This is why merely spotting that lowestQuote may be set to max in finalize is not enough and in my opinion, POC-ing the entire flow is necessary for a valid finding.
This was the main constraint to bypass:
Since clearingQuote must equal UINT128_MAX, we must satisfy:
(2**128-1) * (2**128-1) / clearingBase = quoteAmount * (2**128-1) / baseAmount. The solution I found was setting clearingBase to (2**128-1) and quoteAmount = baseAmount.
We also have constraints on reserveQuotePerBase. In createAuction:
While in finalize():
And an important constraint on quoteAmount and minimumBidQuote:
Merging them gives us two equations to substitute variables in:
In the POC Ive crafted parameters to steal 2**30 quote tokens, around 1000 in USDC denomination. With the above equations, increasing or decreasing the stolen amount is simple.
An attacker can steal all tokens held in the SIZE auction contract.
Copy the following code in SizeSealed.t.sol
Manual audit, foundry tests
Do not trust the value of lowestQuote when determining the finalize state, use a dedicated state variable for it.
RagePit (SIZE) confirmed
