Pool's kickWithDeposit misses liquidation debt check
The kickWithDeposit function in the Ajna protocol's Pool contract lacked the _revertIfAuctionDebtLocked check that prevents removal of quote token deposits locked by liquidation debt. An attacker could use kickWithDeposit to withdraw funds from high-priced buckets (HPBs) that should have been reserved for covering bad debt, potentially shifting losses to lower-bucket depositors.
Source: https://github.com/sherlock-audit/2023-04-ajna-judging/issues/82
Found by
hyh
Summary
_revertIfAuctionDebtLocked() is missed in kickWithDeposit() function, that can actually remove deposit from anywhere, including HPB that are frozen by liquidation debt accumulator.
Vulnerability Detail
The inability to remove quote token deposit that is placed high enough to be covered by liquidation debt accumulator is a part of system design (see 7.5 Liquidation Debt of Ajna protocol white paper).
The corresponding check is performed by _revertIfAuctionDebtLocked() in moveQuoteToken() and removeQuoteToken(), but is missed in kickWithDeposit() that allows for quote funds retrieval from HPB as well.
Impact
HPB depositors can use kickWithDeposit() -> withdrawBonds() for quote funds removal, effectively avoiding liquidation debt controls, which can lead to deposit shortage for the matters of eventual bad debt coverage. I.e. in some situations when depositor knows that his funds are about to be used to cover bad debt it might be reasonably for them to use kickWithDeposit() even knowing that there most probably will be a kicker penalty imposed.
This not only can create a number of bad faith auctions, but can move a burden of debt write offs to lower bucket depositors, who are unaware of such possibility and do not actively monitor pool state. This will allow HPB depositors to obtain stable yield, but off load a part of the corresponding risks, profiting off the lower buckets depositors (who, in general, pocketed a somewhat lower yield, but receive more risk this way).
As there is no low-probability prerequisites and the impact is a violation of system design allowing one group of users to profit off another, setting the severity to be high.
Code Snippet
kickWithDeposit() can effectively remove quote tokens from any bucket to cover kick bond, but is not controlled for liquidation debt buffer:
https://github.com/sherlock-audit/2023-04-ajna/blob/main/ajna-core/src/base/Pool.sol#L321-L336
function kickWithDeposit(
uint256 index_,
uint256 npLimitIndex_
) external override nonReentrant {
PoolState memory poolState = _accruePoolInterest();
// kick auctions
KickResult memory result = KickerActions.kickWithDeposit(
auctions,
deposits,
buckets,
loans,
poolState,
index_,
npLimitIndex_
);
function kickWithDeposit(
...
) external returns (
KickResult memory kickResult_
) {
...
// kick top borrower
kickResult_ = _kick(
...
);
// amount to remove from deposit covers entire bond amount
if (vars.amountToDebitFromDeposit > kickResult_.amountToCoverBond) {
// cap amount to remove from deposit at amount to cover bond
vars.amountToDebitFromDeposit = kickResult_.amountToCoverBond;
// recalculate the LUP with the amount to cover bond
kickResult_.lup = Deposits.getLup(deposits_, poolState_.debt + vars.amountToDebitFromDeposit);
// entire bond is covered from deposit, no additional amount to be send by lender
kickResult_.amountToCoverBond = 0;
} else {
// lender should send additional amount to cover bond
kickResult_.amountToCoverBond -= vars.amountToDebitFromDeposit;
}
// revert if the bucket price used to kick and remove is below new LUP
if (vars.bucketPrice < kickResult_.lup) revert PriceBelowLUP();
// remove amount from deposits
if (vars.amountToDebitFromDeposit == vars.bucketDeposit && vars.bucketCollateral == 0) {
// In this case we are redeeming the entire bucket exactly, and need to ensure bucket LP are set to 0
vars.redeemedLP = vars.bucketLP;
>> Deposits.unscaledRemove(deposits_, index_, vars.bucketUnscaledDeposit);
vars.bucketUnscaledDeposit = 0;
} else {
...
>> Deposits.unscaledRemove(deposits_, index_, unscaledAmountToRemove);
vars.bucketUnscaledDeposit -= unscaledAmountToRemove;
}
_revertIfAuctionDebtLocked() is guarding direct quote funds removal via moveQuoteToken():
https://github.com/sherlock-audit/2023-04-ajna/blob/main/ajna-core/src/base/Pool.sol#L176-L185
function moveQuoteToken(
uint256 maxAmount_,
uint256 fromIndex_,
uint256 toIndex_,
uint256 expiry_
) external override nonReentrant returns (uint256 fromBucketLP_, uint256 toBucketLP_, uint256 movedAmount_) {
_revertAfterExpiry(expiry_);
PoolState memory poolState = _accruePoolInterest();
>> _revertIfAuctionDebtLocked(deposits, poolState.t0DebtInAuction, fromIndex_, poolState.inflator);
And removeQuoteToken():
https://github.com/sherlock-audit/2023-04-ajna/blob/main/ajna-core/src/base/Pool.sol#L210-L218
function removeQuoteToken(
uint256 maxAmount_,
uint256 index_
) external override nonReentrant returns (uint256 removedAmount_, uint256 redeemedLP_) {
_revertIfAuctionClearable(auctions, loans);
PoolState memory poolState = _accruePoolInterest();
>> _revertIfAuctionDebtLocked(deposits, poolState.t0DebtInAuction, index_, poolState.inflator);
Tool used
Manual Review
Recommendation
Consider adding the check:
https://github.com/sherlock-audit/2023-04-ajna/blob/main/ajna-core/src/base/Pool.sol#L321-L336
function kickWithDeposit(
uint256 index_,
uint256 npLimitIndex_
) external override nonReentrant {
PoolState memory poolState = _accruePoolInterest();
+ _revertIfAuctionDebtLocked(deposits, poolState.t0DebtInAuction, index_, poolState.inflator);
// kick auctions
KickResult memory result = KickerActions.kickWithDeposit(
auctions,
deposits,
buckets,
loans,
poolState,
index_,
npLimitIndex_
);
Discussion
grandizzy
https://github.com/ajna-finance/contracts/pull/894
dmitriia
Looks ok, lenderKick() (renamed kickWithDeposit()) no longer removes deposit, obtaining bond funds directly from the sender, so no liquidation debt check is now needed.
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 kickWithDeposit function omitted the _revertIfAuctionDebtLocked check that is present in moveQuoteToken and removeQuoteToken, allowing deposit removal from buckets that should be locked by liquidation debt.
Impact
HPB depositors could avoid liquidation debt controls and withdraw funds, potentially causing a deposit shortage for bad debt coverage and shifting the burden to lower-bucket depositors.
What the attacker needed
Depositor has funds in a high-priced bucket; depositor is aware of impending bad debt
The fix
The fix was to modify kickWithDeposit so that it no longer removes deposit, instead obtaining bond funds directly from the sender, eliminating the need for the liquidation debt check.
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 this lending protocol, some deposits are supposed to be locked to cover bad debt from failed loans. The function that lets users start auctions using their deposit didn't check if that deposit was locked, so users could pull out funds that were meant to stay put. This could leave the system short of money to cover losses, pushing the risk onto other users.
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.