When code defaults on remaining liability, it does not delete remaining auction, which is problematic if the user has called fill with a % less than 100
When a bad debt auction is partially filled and the backstop threshold falls below 5%, the code defaults the remaining liability but leaves the remaining auction in storage. This allows a later filler to obtain the remaining backstop tokens without taking on any debt, causing a loss to backstop stakers.
rscodes Follow with 0xtheauditor, aldarion and 1 other
Originally disclosed on Code4rena
Submitted by rscodes, also found by 0xtheauditor, aldarion, and klau5
Finding description
pub fn fill_bad_debt_auction(...) {
....
// bid only contains d_token asset amounts
backstop_state.rm_positions(e, pool, map![e], auction_data.bid.clone());
filler_state.add_positions(e, pool, map![e], auction_data.bid.clone());
....
// If the backstop still has liabilities and less than 5% of the backstop threshold burn bad debt
if !backstop_state.positions.liabilities.is_empty() {
let pool_backstop_data = backstop_client.pool_data(&e.current_contract_address());
let threshold = calc_pool_backstop_threshold(&pool_backstop_data);
if threshold < 0_0000003 {
// ~5% of threshold
let reserve_list = storage::get_res_list(e);
for (reserve_index, liability_balance) in backstop_state.positions.liabilities.iter() {
let res_asset_address = reserve_list.get_unchecked(reserve_index);
let mut reserve = pool.load_reserve(e, &res_asset_address, true);
backstop_state.default_liabilities(e, &mut reserve, liability_balance);
pool.cache_reserve(reserve);
PoolEvents::defaulted_debt(e, res_asset_address, liability_balance);
}
}
}
backstop_state.store(e);
}
We can see that in the bottom of the code, if the backstop threshold goes below the certain % then it deletes the remaining liability by calling backstop_state.default_liabilities.
However if we look at the function fill in auction.rs where users can pass in a variable percent_filled.
pub fn fill(
...
--> percent_filled: u64,
) -> AuctionData {
...
let auction_data = storage::get_auction(e, &auction_type, user);
--> let (to_fill_auction, remaining_auction) = scale_auction(e, &auction_data, percent_filled);
match AuctionType::from_u32(e, auction_type) {
AuctionType::UserLiquidation => {
fill_user_liq_auction(e, pool, &to_fill_auction, user, filler_state)
}
AuctionType::BadDebtAuction => {
fill_bad_debt_auction(e, pool, &to_fill_auction, filler_state)
}
AuctionType::InterestAuction => {
fill_interest_auction(e, pool, &to_fill_auction, &filler_state.address)
}
};
if let Some(auction_to_store) = remaining_auction {
storage::set_auction(e, &auction_type, user, &auction_to_store);
} else {
storage::del_auction(e, &auction_type, user);
}
to_fill_auction
}
So, if the user fills with like 50%, for example, scale_auction only passes along like 50% of the auction data to fill_bad_debt_auction, the remaining 50% is stored in remaining_auction and stored back into storage::set_auction.
Hence, right now the bug is that when the threshold goes below 5%, the code deletes the liability by defaulting on it, but does not delete any remaining auction, which allows users to steal backstop tokens.
Sequence
Let’s explore what happens during such a case.
Bid token - the dtokens (debt) the auction winner will take on
- From block 0 to 200, remains the same at the peak value. Then from block 200 to 400 begins to decrease. After block 400, its just zero
Lot token - The backstop token the pool will transfer to the auction winner.
- From block 0 to 200, the amount of lot tokens given to winner scales upwards and peaks at block 200. Then from then on stays the same at that peaked value.
- Suppose there is a bad debt auction.
Alicefills the bad debt auction, callingfillwithpercent_filled = 50%.- It goes below the threshold and the code deletes backstop remaining liability.
- However, the code does not delete the 50%
remaining_auctionwhich is now set in the bad debt auction storage key (also prevents new bad debt from being created btw). - From now, all the way till block 400, no auctioner can fill the auction. This is because
<block 400, the bid token is non-zero, and since the code defaulted the liability under backstop, the linebackstop_state.rm_positionswill panic.
Hence, the auction can only be filled after block 400. But, now the issue is, when the auction is filled at block 400 onwards, the user who fills the auction does not take on any debt in place of the backstop, but still gets the lot amount of backstop tokens for FREE (at the expense of backstop stakers).
Impact
This breaks the normal flow of the bad debt auction and causes serious loss for backstop stakers. If you think about, of course every auction can be like this, where everyone waits until block 400 to get the backstop token for free, at the loss of the backstop stakers.
However, that does not happen due to the free market the code facilitates. Once the exchange becomes worth it, auctioners will rush to do the exchange, taking on debt in exchange for the backstop token because they know that if they don’t, then others will anyway.
But in this case, the fact that the code removes liability from backstop but not the remaining_auction means that no one can fill the auction before block 400 because it will always panic due to the rm_position, which now breaks the free market flow of regular bad debt auctions.
- This loophole allows the first user to call
fillat block 400 to steal all the backstop tokens in remaining lot amount without taking on any debt. (Which is also despite the backstop already dropping below threshold)
PoC
PoC is written here.
Recommendation
Delete remaining_auction if the code is going to default liabilities. So that backstop stakers don’t lose tokens for nothing (since there isn’t any liability anymore) when the threshold is below 5% already.
markus_pl10 (Script3) confirmed
Validated this is an issue.
The 5% threshold was chosen as a “dust” limit where the expectations of the pool was that it could get up to that amount of backstop funds to cover its defaulted debt, otherwise, it was on it’s own. The pool only expects that much coverage, and the backstop assumes all it’s tokens can be used to cover bad debt.
This does cause some unnecessary loss for suppliers, as generally fillers would have used the rest of the funds in the auction to cover additional bad debt.
However, the worst case loss for suppliers and/or backstop depositors does not change, it just opens a scenario where both parties can have a worst case experience, at the gain of a filler.
Good finding, but it requires a series of conditions to line up just right, so I can’t see it exceeding medium.
Fixed to:
- Only process post auction actions like bad debt and defaulting if the auction was fully filled, and no remaining auction exists.
- Re-work
bad_debtto handle any edge case where auction leaves an account in a state where debt can be either marked as bad debt or defaulted.
Commit 59acbc9; Improvements to assign bad debt after user liquidations and clean up post liquidation actions.
Status: Mitigation confirmed. Full details in reports from 0xAlix2, Testerbot, 0x007, oakcobalt and rscodes.
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
Likely, the fill function defaults the remaining liability when the backstop threshold is low but fails to delete the corresponding remaining auction data, leaving an auction with no debt to take on.
Impact
This could cause a loss for backstop stakers because a filler can obtain the remaining backstop tokens without taking on any debt, though the judge noted it requires a series of conditions to line up.
What the attacker needed
A bad debt auction exists; backstop threshold below 5%; attacker can call fill
The fix
The code was fixed to only process post-auction actions like bad debt and defaulting if the auction was fully filled and no remaining auction exists.
How it was found
- A bad debt auction exists and the backstop threshold is below 5%.
- Alice calls fill with percent_filled = 50%, which triggers defaulting of the remaining liability.
- The remaining 50% of the auction is stored back into storage.
- No one can fill the remaining auction before block 400 because the bid token is non-zero and the code panics when trying to remove positions.
- After block 400, a user fills the remaining auction and receives the lot tokens without taking on any debt.
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 lending pool, when a borrower cannot repay, their debt is auctioned off. The winner takes on the debt and receives some backstop tokens as a reward. This report shows that if someone only buys part of the auction and the pool's safety fund (backstop) is almost empty, the system cancels the remaining debt but forgets to cancel the rest of the auction. Later, someone can claim the remaining reward tokens without taking any debt, which harms the people who deposited into the safety fund.
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.