Incorrect calculations for Surplus Auction creation cause massive surplus imbalances
The auctionSurplus function used the wrong constant (ONE_HUNDRED_WAD instead of WAD) in a comparison and a multiplication, so a surplus auction could be created with an amount to sell that was about 100 times too large, and even when the transfer percentage was 100% an auction would still be created while the full surplus was also transferred. An attacker could exploit this to buy large amounts of system coins cheaply, causing massive price imbalances and destabilising the protocol.
falconhoof Follow with tnquanghuy0512, 0x6d6164616e and 7 others
Originally disclosed on Code4rena
Submitted by falconhoof, also found by pep7siup (1, 2), tnquanghuy0512, 0x6d6164616e, twicek, immeas, T1MOH, Krace, Baki, bitsurfer, and Beosin
There are two big issues in AccountingEngine.sol::auctionSurplus() when calculating values for the creating a Surplus Auction; specifically in Lines 213 - 217.
- The first issue is the
ifstatement at line 213 which incorrectly checks if_params.surplusTransferPercentageis less thanONE_HUNDRED_WADwhen it should check if it is less thanWAD.
if (_params.surplusTransferPercentage < ONE_HUNDRED_WAD) {
The maximum value for _params.surplusTransferPercentage, as checked by the function at line 199, is 1e18 so the check at line 213 will always return TRUE.
However, the check should return FALSE when _params.surplusTransferPercentage is 1e18 or 100% because in that case an auction shouldn’t be created; rather the full surplusAmount should be transferred to extraSurplusReceiver in the next code block.
- The second issue is the use of
ONE_HUNDRED_WADto calculate_amountToSellat Line 215 which results in a hugely inflated figure in the newly created surplus auction.
The use of ONE_HUNDRED_WAD causes the calculated figure to be 100 times greater than it should be.
_amountToSell: _params.surplusAmount.wmul(ONE_HUNDRED_WAD - _params.surplusTransferPercentage)
Impact
Issue 1. For the first issue; when _params.surplusTransferPercentage is 100%, a ghost surplus Auction will be created and the entire surplusAmount amount will also be sent to the extraSurplusReceiver essentially double-counting a large amount of the surplus. This double accounting can destabilise the system and lead to underflows elsewhere.
Issue 2. Surplus auctions are created with massively inflated figures for _amountToSell. This has the potential to cause massive price imbalances and crash the protocol. There is potential here for a malicious actor to leverage the vulnerability to their advantage by creating lots of false surplus in system coins which they purchase cheaply.
Proof of Concept
The following combines both issues into one PoC to show the worst case scenario.
Given the following values and assuming the initial all checks pass in function before reaching Line 213 AccountingEngine.sol::auctionSurplus():
WAD = 1e18
surplusTransferPercentage = 1e18 (representative of 100%)
surplusAmount = 3e18
ONE_HUNDRED_WAD = 100e18
The following condition at Line 213 will always return TRUE:
if (_params.surplusTransferPercentage < ONE_HUNDRED_WAD)
So an auction will be created with _amountToSell 100 times higher than it should be:
_amountToSell: _params.surplusAmount.wmul(ONE_HUNDRED_WAD - _params.surplusTransferPercentage),
Returns 297e18; where coin surplusAmount is only 3e18:
_amountToSell = 3e18 * (100e18 - 1e18) / 1e18
= 3e18 * 99e18 / 1e18
= 297e18
= 297 WAD
Following this, a coin amount of surplusAmount is also sent to the extraSurplusReceiver calculated in Lines 224-231 which is actually the intended behaviour.
Tools Used
Foundry
Recommended Mitigation Steps
Update the function on the two affected lines to use WAD instead of ONE_HUNDRED_WAD as:
function auctionSurplus() external returns (uint256 _id) {
if(_params.surplusTransferPercentage > WAD) revert AccEng_surplusTransferPercentOverLimit();
if (_params.surplusAmount == 0) revert AccEng_NullAmount();
if (extraSurplusReceiver == address(0)) revert AccEng_NullSurplusReceiver();
if (block.timestamp < lastSurplusTime + _params.surplusDelay) revert AccEng_SurplusCooldown();
uint256 _coinBalance = safeEngine.coinBalance(address(this));
uint256 _debtBalance = safeEngine.debtBalance(address(this));
(_coinBalance, _debtBalance) = _settleDebt(_coinBalance, _debtBalance, _unqueuedUnauctionedDebt(_debtBalance));
if (_coinBalance < _debtBalance + _params.surplusAmount + _params.surplusBuffer) {
revert AccEng_InsufficientSurplus();
}
// auction surplus percentage
- if (_params.surplusTransferPercentage < ONE_HUNDRED_WAD) {
+ if (_params.surplusTransferPercentage < WAD) {
_id = surplusAuctionHouse.startAuction({
- _amountToSell: _params.surplusAmount.wmul(ONE_HUNDRED_WAD - _params.surplusTransferPercentage),
+ _amountToSell: _params.surplusAmount.wmul(WAD - _params.surplusTransferPercentage),
_initialBid: 0
});
lastSurplusTime = block.timestamp;
emit AuctionSurplus(_id, 0, _params.surplusAmount.wmul(ONE_HUNDRED_WAD - _params.surplusTransferPercentage));
}
// transfer surplus percentage
if (_params.surplusTransferPercentage > 0) {
if (extraSurplusReceiver == address(0)) revert AccEng_NullSurplusReceiver();
safeEngine.transferInternalCoins({
_source: address(this),
_destination: extraSurplusReceiver,
_rad: _params.surplusAmount.wmul(_params.surplusTransferPercentage)
});
lastSurplusTime = block.timestamp;
emit TransferSurplus(extraSurplusReceiver, _params.surplusAmount.wmul(_params.surplusTransferPercentage));
}
}
RaymondFam (Lookout) commented:
_amountToSellis indeed very close to 100 times (99% plus).
pi0neerpat (OpenDollar) confirmed
The warden has demonstrated how due to the incorrect use of
ONE_HUNDRED_WADinstead ofWADwhen calculating percentages, surplus auctions will be created with massively inflated values, breaking the accounting of the protocol. As such, I agree with high severity.Selected this report for best as it outlines the issues, impacts and recommended mitigation really well despite the lack of a coded PoC.
pi0neerpat (OpenDollar) commented
Resolved here.
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 code used ONE_HUNDRED_WAD (100e18) instead of WAD (1e18) in both a comparison and a multiplication when handling a percentage value that is scaled so that 1e18 represents 100%.
Impact
Surplus auctions could be created with massively inflated amounts, causing price imbalances and potentially crashing the protocol; a malicious actor could exploit this to buy large amounts of system coins cheaply.
What the attacker needed
SurplusTransferPercentage set to 100%; surplusAmount greater than zero; extraSurplusReceiver set; surplus cooldown passed
The fix
The function was fixed by replacing ONE_HUNDRED_WAD with WAD in both the comparison and the multiplication.
How it was found
- Set surplusTransferPercentage to 1e18 (100%) and surplusAmount to 3e18.
- Call auctionSurplus; the check at line 213 incorrectly passes because 1e18 < 100e18.
- An auction is created with _amountToSell calculated as 3e18 * (100e18 - 1e18) / 1e18 = 297e18, which is 99 times the actual surplus.
- The full surplusAmount is also transferred to extraSurplusReceiver, double-counting the surplus.
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.
This is a smart contract bug in a system that creates auctions to sell off extra coins. The code used the number 100 instead of 1 when calculating percentages, so the auction offered about 100 times more coins than it should have. Someone could then buy those coins very cheaply, which could break the system's balance.
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.