Settlement can be called when auction period isn't concluded, allowing HPB depositors to game bad debt settlements
The settlePoolDebt function allowed settlement at the exact end of the 72-hour auction period, while other operations were still blocked, enabling a depositor to atomically remove and re-add liquidity to settle bad debt with others' funds. This could let a depositor profit risk-free by gaming the settlement.
Source: https://github.com/sherlock-audit/2023-04-ajna-judging/issues/106
Found by
hyh
Summary
The end of auction period is included to it across the logic, but settlePoolDebt() treats the last moment as if it is beyond the period.
Vulnerability Detail
In settlePoolDebt() SettlerActions.sol#L113 the end of period control do not revert at block.timestamp == kickTime + 72 hours, allowing to run the settlement at the very last moment of the period.
Impact
Pool manipulations become possible at this point of time as both quote and collateral removal operations (guarded by _revertIfAuctionClearable) and settlePoolDebt() are available at this point of time.
As an example, HPB depositor can monitor pool state and upon the calculation that their bucket can be used for bad debt settlement, atomically run removeQuoteToken() -> settlePoolDebt() -> addQuoteToken() at block.timestamp == kickTime + 72 hours, retaining yield generating HPB position, while settling bad debt with funds of other depositors in nearby buckets.
While the probability looks to be medium, catching the exact moment is cumbersome, but achievable operation, the impact is one depositors profiting off others in a risk-free manner, so placing the overall severity to be medium.
Code Snippet
settlePoolDebt() can be run at block.timestamp == kickTime + 72 hours:
function settlePoolDebt(
AuctionsState storage auctions_,
mapping(uint256 => Bucket) storage buckets_,
DepositsState storage deposits_,
LoansState storage loans_,
ReserveAuctionState storage reserveAuction_,
PoolState calldata poolState_,
SettleParams memory params_
) external returns (SettleResult memory result_) {
uint256 kickTime = auctions_.liquidations[params_.borrower].kickTime;
if (kickTime == 0) revert NoAuction();
Borrower memory borrower = loans_.borrowers[params_.borrower];
>> if ((block.timestamp - kickTime < 72 hours) && (borrower.collateral != 0)) revert AuctionNotClearable();
While AuctionNotCleared() is block.timestamp - kickTime > 72 hours, i.e. clearable auction is [0, 72 hours] period:
function _revertIfAuctionClearable(
AuctionsState storage auctions_,
LoansState storage loans_
) view {
address head = auctions_.head;
uint256 kickTime = auctions_.liquidations[head].kickTime;
if (kickTime != 0) {
>> if (block.timestamp - kickTime > 72 hours) revert AuctionNotCleared();
Reserves take also includes the last timestamp to the period, proceeding with take:
function takeReserves(
ReserveAuctionState storage reserveAuction_,
uint256 maxAmount_
) external returns (uint256 amount_, uint256 ajnaRequired_) {
// revert if no amount to be taken
if (maxAmount_ == 0) revert InvalidAmount();
uint256 kicked = reserveAuction_.kicked;
>> if (kicked != 0 && block.timestamp - kicked <= 72 hours) {
Tool used
Manual Review
Recommendation
Consider having settlePoolDebt() wait for the whole period to pass:
function settlePoolDebt(
...
) external returns (SettleResult memory result_) {
...
- if ((block.timestamp - kickTime < 72 hours) && (borrower.collateral != 0)) revert AuctionNotClearable();
+ if ((block.timestamp - kickTime <= 72 hours) && (borrower.collateral != 0)) revert AuctionNotClearable();
Discussion
grandizzy
https://github.com/ajna-finance/contracts/pull/902
dmitriia
Looks ok
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 an off-by-one error in the time comparison: settlePoolDebt used a strict less-than check for the 72-hour period, while other functions used a less-than-or-equal check, creating a one-second window where settlement was allowed but other operations were not.
Impact
A depositor could profit risk-free by settling bad debt with other depositors' funds while retaining their own yield-generating position; the probability is medium but the impact is medium.
What the attacker needed
A depositor with funds in the pool; ability to monitor pool state; ability to submit atomic transactions
The fix
The comparison in settlePoolDebt should be changed to less-than-or-equal to include the last moment in the auction period, and this was fixed.
How it was found
- Monitor the pool state to find a bucket that can be used for bad debt settlement.
- At the exact timestamp when the auction period ends, atomically remove quote tokens, call settlePoolDebt, and add quote tokens back.
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's debt becomes bad, the pool can settle it using depositors' funds. The code had a timing flaw: at the very last second of a 72-hour auction period, a depositor could quickly move their money out, let the settlement use other people's money, and then put their money back. This let them keep earning interest while others bore the loss.
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.