Skip to content
Business logic Audit finding Advanced

HPB may be incorrectly bankrupt due to use of unscaled value in `_forgiveBadDebt`

The bankruptcy check in `_forgiveBadDebt` uses an unscaled deposit value instead of a scaled one, so the exchange rate calculation is off by the bucket's scale factor. This can cause a bucket to be incorrectly marked bankrupt, wiping out user LP tokens and losing their funds.

0xkaden Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-09-ajna-judging/issues/25

Found by

0xkaden

An unscaled value is used in place of where a scaled value should be used in the bankruptcy check in _forgiveBadDebt. This may cause the bucket to be incorrectly marked as bankrupt, losing user funds.

Vulnerability Detail

At the end of _forgiveBadDebt, we do a usual bankruptcy check in which we check whether the remaining deposit and collateral will be little enough that the exchange rate will round to 0, in which case we mark the bucket as bankrupt, setting the bucket lps and effectively all user lps as 0.

The problem lies in the fact that we use depositRemaining as part of this check, which represents an unscaled value. As a result, when computing whether the exchange rate rounds to 0 our logic is off by a factor of the bucket's scale. The bucket may be incorrectly marked as bankrupt if the unscaled depositRemaining would result in an exchange rate of 0 when the scaled depositRemaining would not.

Impact

Loss of user funds.

Code Snippet

https://github.com/sherlock-audit/2023-09-ajna/blob/main/ajna-core/src/libraries/external/SettlerActions.sol#L485

Solidity
// If the remaining deposit and resulting bucket collateral is so small that the exchange rate
// rounds to 0, then bankrupt the bucket.  Note that lhs are WADs, so the
// quantity is naturally 1e18 times larger than the actual product
// @audit depositRemaining should be a scaled value
if (depositRemaining * Maths.WAD + hpbBucket.collateral * _priceAt(index) <= bucketLP) {
    // existing LP for the bucket shall become unclaimable
    hpbBucket.lps            = 0;
    hpbBucket.bankruptcyTime = block.timestamp;

    emit BucketBankruptcy(
        index,
        bucketLP
    );
}

Tool used

Manual Review

Recommendation

Simply scale depositRemaining before doing the bankruptcy check, e.g. something like:

Solidity
depositRemaining = Maths.wmul(depositRemaining, scale);
if (depositRemaining * Maths.WAD + hpbBucket.collateral * _priceAt(index) <= bucketLP) {
    // existing LP for the bucket shall become unclaimable
    hpbBucket.lps            = 0;
    hpbBucket.bankruptcyTime = block.timestamp;

    emit BucketBankruptcy(
        index,
        bucketLP
    );
}

The original report as published on Sherlock, by 0xkaden. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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 bankruptcy check uses `depositRemaining` without multiplying it by the bucket's scale factor, so the comparison is off by that factor.

Impact

User funds could be lost because the bucket may be incorrectly marked bankrupt, setting all user LP tokens to zero.

What the attacker needed

Bucket with small remaining deposit; forgive bad debt called

The fix

The code should scale `depositRemaining` before performing the bankruptcy 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 system, a 'bucket' holds deposits and collateral, and its exchange rate decides how much each LP token is worth. The code checks if the remaining deposit is so tiny that the exchange rate rounds to zero, and if so, it declares bankruptcy and sets all LP tokens to zero. The bug is that the check uses a raw deposit amount instead of the properly scaled version, so it can wrongly think the bucket is bankrupt and erase user tokens.

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.

More by 0xkaden

View all

11 more disclosed reports, best first.

More from Ajna #2

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.