Possible to liquidate past the debt outstanding above the min borrow without liquidating the entire debt outstanding
A validation check in the liquidation logic used the wrong sign, so a liquidator could repay part of a vault's debt and leave the account with a remaining debt below the minimum borrow size. This could leave small, unprofitable-to-liquidate positions that might become bad debt for the protocol.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-03-notional-judging/issues/194
Found by
xiaoming90 + 0xleastwood
Summary
It is possible to liquidate past the debt outstanding above the min borrow without liquidating the entire debt outstanding. Thus, leaving accounts with small debt that are not profitable to unwind if it needs to liquidate.
Vulnerability Detail
File: VaultValuation.sol
250: // NOTE: deposit amount is always positive in this method
251: if (depositUnderlyingInternal < maxLiquidatorDepositLocal) {
252: // If liquidating past the debt outstanding above the min borrow, then the entire
253: // debt outstanding must be liquidated.
254:
255: // (debtOutstanding - depositAmountUnderlying) is the post liquidation debt. As an
256: // edge condition, when debt outstanding is discounted to present value, the account
257: // may be liquidated to zero while their debt outstanding is still greater than the
258: // min borrow size (which is normally enforced in notional terms -- i.e. non present
259: // value). Resolving this would require additional complexity for not much gain. An
260: // account within 20% of the minBorrowSize in a vault that has fCash discounting enabled
261: // may experience a full liquidation as a result.
262: require(
263: h.debtOutstanding[currencyIndex].sub(depositUnderlyingInternal) < minBorrowSize,
264: "Must Liquidate All Debt"
265: );
depositUnderlyingInternalis always a positive value (Refer to comment on Line 250) that represents the amount of underlying deposited by the liquidatorh.debtOutstanding[currencyIndex]is always a negative value representing debt outstanding of a specific currency in a vault accountminBorrowSizeis always a positive value that represents the minimal borrow size of a specific currency (It is stored as uint32 in storage)
If liquidating past the debt outstanding above the min borrow, then the entire debt outstanding must be liquidated.
Assume the following scenario:
depositUnderlyingInternal=70 USDCh.debtOutstanding[currencyIndex]=-100 USDCminBorrowSize=50 USDC
If the liquidation is successful, the vault account should be left with -30 USDC debt outstanding because 70 USDC has been paid off by the liquidator. However, this should not happen under normal circumstances because the debt outstanding (-30) does not meet the minimal borrow size of 50 USDC and the liquidation should revert/fail.
The following piece of validation logic attempts to ensure that all outstanding debt is liquidated if post-liquidation debt does not meet the minimal borrowing size.
require(
h.debtOutstanding[currencyIndex].sub(depositUnderlyingInternal) < minBorrowSize,
"Must Liquidate All Debt"
);
Plugging in the values from our scenario to verify if the code will revert if the debt outstanding does not meet the minimal borrow size.
require(
(-100 USDC - 70 USDC) < 50 USDC
);
===>
require(
(-170 USDC) < 50 USDC
);
===>
require(true) // no revert
The above shows that it is possible for someone to liquidate past the debt outstanding above the min borrow without liquidating the entire debt outstanding. This shows that the math formula in the code is incorrect and not working as intended.
Impact
A liquidation can bring an account below the minimum debt. Accounts smaller than the minimum debt are not profitable to unwind if it needs to liquidate (Reference)
As a result, liquidators are not incentivized to liquidate those undercollateralized positions. This might leave the protocol with bad debts, potentially leading to insolvency if the bad debts accumulate.
Code Snippet
Tool used
Manual Review
Recommendation
Update the formula to as follows:
require(
- h.debtOutstanding[currencyIndex].sub(depositUnderlyingInternal) < minBorrowSize,
+ h.debtOutstanding[currencyIndex].neg().sub(depositUnderlyingInternal) > minBorrowSize,
"Must Liquidate All Debt"
);
Plugging in the values from our scenario again to verify if the code will revert if the debt outstanding does not meet the minimal borrow size.
require(
((-100 USDC).neg() - 70 USDC) > 50 USDC
);
===>
require(
(100 USDC - 70 USDC) > 50 USDC
);
===>
require(
(30 USDC) > 50 USDC
);
===>
require(false) // revert
The above will trigger a revert as expected when the debt outstanding does not meet the minimal borrow size.
Discussion
jeffywu
Fixed: https://github.com/notional-finance/contracts-v2/pull/132
xiaoming9090
@0xleastwood + @xiaoming9090 : Verified. Fixed in PR https://github.com/notional-finance/contracts-v2/pull/132
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 validation subtracted a positive deposit from a negative debt value without first negating the debt, so the comparison was always true and never enforced the minimum borrow size.
Impact
A liquidation can bring an account below the minimum debt, and such small accounts are not profitable to unwind, which might leave the protocol with bad debts and potentially lead to insolvency if they accumulate.
What the attacker needed
A vault account with debt; liquidator able to repay part of the debt
The fix
The formula was fixed by negating the debt outstanding before subtracting the deposit, so the check correctly reverts when the remaining debt is below the minimum borrow size.
How it was found
- Identify a vault account with debt outstanding of -100 USDC and a minimum borrow size of 50 USDC.
- A liquidator deposits 70 USDC to repay part of the debt.
- The validation computes (-100 - 70) < 50, which is true, so the liquidation succeeds.
- The account is left with -30 USDC debt, below the minimum borrow size.
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.
When someone borrows money in a vault, the protocol requires that any remaining debt after a partial repayment is either zero or at least a minimum size. The code meant to enforce this compared a negative debt number directly, so the check always passed. As a result, a liquidator could leave a tiny debt behind that nobody is motivated to clean up.
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.