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.
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
// 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:
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
);
}
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.