Vault accounts with excess cash can avoid being settled
Vault accounts with excess cash in one currency and debt in another could become unsettled because settlement transfers out the excess cash, failing the collateral ratio check. This deadlock prevents liquidation, allowing sophisticated users to avoid liquidation and potentially causing bad debt.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-03-notional-judging/issues/207
Found by
xiaoming90 + 0xleastwood
Summary
If excess cash was transferred out from an account during account settlement, then the protocol will check the account's collateral ratio and revert if the position is unhealthy. Because it may not be possible to settle a vault account, liquidators cannot reduce account debt by purchasing vault shares because _authenticateDeleverage() will check to see if a vault has matured.
Vulnerability Detail
Considering an account's health is determined by a combination of its outstanding debt, cash holdings and the total underlying value of its vault shares, transferring out excess cash may actually put an account in an unhealthy position.
File: VaultAccountAction.sol
41: function settleVaultAccount(address account, address vault) external override nonReentrant {
42: requireValidAccount(account);
43: require(account != vault);
44:
45: VaultConfig memory vaultConfig = VaultConfiguration.getVaultConfigStateful(vault);
46: VaultAccount memory vaultAccount = VaultAccountLib.getVaultAccount(account, vaultConfig);
47:
48: // Require that the account settled, otherwise we may leave the account in an unintended
49: // state in this method because we allow it to skip the min borrow check in the next line.
50: (bool didSettle, bool didTransfer) = vaultAccount.settleVaultAccount(vaultConfig);
51: require(didSettle, "No Settle");
52:
53: vaultAccount.accruePrimeCashFeesToDebt(vaultConfig);
54:
55: // Skip Min Borrow Check so that accounts can always be settled
56: vaultAccount.setVaultAccount({vaultConfig: vaultConfig, checkMinBorrow: false});
57:
58: if (didTransfer) {
59: // If the vault did a transfer (i.e. withdrew cash) we have to check their collateral ratio. There
60: // is an edge condition where a vault with secondary borrows has an emergency exit. During that process
61: // an account will be left some cash balance in both currencies. It may have excess cash in one and
62: // insufficient cash in the other. A withdraw of the excess in one side will cause the vault account to
63: // be insolvent if we do not run this check. If this scenario indeed does occur, the vault itself must
64: // be upgraded in order to facilitate orderly exits for all of the accounts since they will be prevented
65: // from settling.
66: IVaultAccountHealth(address(this)).checkVaultAccountCollateralRatio(vault, account);
67: }
68: }
It is important to note that all vault liquidation actions require a vault to first be settled. Hence, through self-liquidation, sophisticated vault accounts can have excess cash in one currency and significant debt holdings in the vault's other currencies.
File: VaultLiquidationAction.sol
197: function _authenticateDeleverage(
198: address account,
199: address vault,
200: address liquidator
201: ) private returns (
202: VaultConfig memory vaultConfig,
203: VaultAccount memory vaultAccount,
204: VaultState memory vaultState
205: ) {
206: // Do not allow invalid accounts to liquidate
207: requireValidAccount(liquidator);
208: require(liquidator != vault);
209:
210: // Cannot liquidate self, if a vault needs to deleverage itself as a whole it has other methods
211: // in VaultAction to do so.
212: require(account != msg.sender);
213: require(account != liquidator);
214:
215: vaultConfig = VaultConfiguration.getVaultConfigStateful(vault);
216: require(vaultConfig.getFlag(VaultConfiguration.DISABLE_DELEVERAGE) == false);
217:
218: // Authorization rules for deleveraging
219: if (vaultConfig.getFlag(VaultConfiguration.ONLY_VAULT_DELEVERAGE)) {
220: require(msg.sender == vault);
221: } else {
222: require(msg.sender == liquidator);
223: }
224:
225: vaultAccount = VaultAccountLib.getVaultAccount(account, vaultConfig);
226:
227: // Vault accounts that are not settled must be settled first by calling settleVaultAccount
228: // before liquidation. settleVaultAccount is not permissioned so anyone may settle the account.
229: require(block.timestamp < vaultAccount.maturity, "Must Settle");
230:
231: if (vaultAccount.maturity == Constants.PRIME_CASH_VAULT_MATURITY) {
232: // Returns the updated prime vault state
233: vaultState = vaultAccount.accruePrimeCashFeesToDebtInLiquidation(vaultConfig);
234: } else {
235: vaultState = VaultStateLib.getVaultState(vaultConfig, vaultAccount.maturity);
236: }
237: }
Consider the following example:
Alice has a valid borrow position in the vault which is considered risky. She has a small bit of secondary cash but most of her debt is primary currency denominated. Generally speaking her vault is healthy. Upon settlement, the small bit of excess secondary cash is transferred out and her vault is undercollateralised and eligible for liquidation. However, we are deadlocked because it is not possible to settle the vault because checkVaultAccountCollateralRatio() will fail, and it's not possible to purchase the excess cash and offset the debt directly via liquidateVaultCashBalance() or deleverageAccount() because _authenticateDeleverage() will revert if a vault has not yet been settled.
Impact
Vault accounts can create positions which will never be eligible for liquidation and the protocol may accrue bad debt.
Code Snippet
Tool used
Manual Review
Recommendation
Consider adding a liquidation method which settles a vault account and allows for a liquidator to purchase vault shares, offsetting outstanding debt, before performing collateral ratio checks.
Discussion
T-Woodward
We're changing settlement to just leave excess cash in the account's cash balance and not transfer out. This allows us to drop the collateral check and remove this issue.
0xleastwood
Escalate for 10 USDC
This issue identifies a deadlock situation where it is not possible to liquidate a vault unless it has first been settled. But it is also not possible to purchase vault cash and offset debt. Hence, a vault can be considered healthy prior to settlement, but because some excess cash is transferred out and the protocol performs collateral checks via checkVaultAccountCollateralRatio(), vault settlement may fail.
I believe the severity of this situation to also be high risk because sophisticated users are able to avoid liquidation.
sherlock-admin
Escalate for 10 USDC
This issue identifies a deadlock situation where it is not possible to liquidate a vault unless it has first been settled. But it is also not possible to purchase vault cash and offset debt. Hence, a vault can be considered healthy prior to settlement, but because some excess cash is transferred out and the protocol performs collateral checks via
checkVaultAccountCollateralRatio(), vault settlement may fail.I believe the severity of this situation to also be
highrisk because sophisticated users are able to avoid liquidation.
You've created a valid escalation for 10 USDC!
To remove the escalation from consideration: Delete your comment.
You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.
Trumpero
Agree with the escalation, this issue should be a high.
hrishibhat
Result: High Unique Considering this issue as a valid high based on the escalation that certain users can avoid liquidations
sherlock-admin
Escalations have been resolved successfully!
Escalation status:
- 0xleastwood: accepted
jeffywu
Fixed in: https://github.com/notional-finance/contracts-v2/pull/135
0xleastwood
@0xleastwood + @xiaoming9090: PR 135 attempted to fix the issue by removing the transfer of excess assets from the settlement. It will store the excess assets to be refunded in s.primaryCash so that it can be read to vaultAccount.tempCashBalance later when exiting the vault (VaultAccountAction.exitVault). The transfer of excess assets will be performed during the exit vault.
However, in an edge case where a liquidator deleverages another account and happens to have primary cash in their vault account, their s.primaryCash (excess cash) will be wiped.
Following is the function call flow for reference:
deleverageAccount -> _transferVaultSharesToLiquidator -> liquidator.setVaultAccount -> Effect: liquidator's s.primaryCash = 0
jeffywu
I'm not sure this is the case:
function _transferVaultSharesToLiquidator(
address receiver,
VaultConfig memory vaultConfig,
uint256 vaultSharesToLiquidator,
uint256 maturity
) private {
// Liquidator will receive vault shares that they can redeem by calling exitVault. If the liquidator has a
// leveraged position on then their collateral ratio will increase
VaultAccount memory liquidator = VaultAccountLib.getVaultAccount(receiver, vaultConfig);
// The liquidator must be able to receive the vault shares (i.e. not be in the vault at all or be in the
// vault at the same maturity). If the liquidator has fCash in the current maturity then their collateral
// ratio will increase as a result of the liquidation, no need to check their collateral position.
require(liquidator.maturity == 0 || liquidator.maturity == maturity, "Maturity Mismatch"); // dev: has vault shares
liquidator.maturity = maturity;
liquidator.vaultShares = liquidator.vaultShares.add(vaultSharesToLiquidator);
liquidator.setVaultAccount({vaultConfig: vaultConfig, checkMinBorrow: true, emitEvents: false});
}
- In
getVaultAccountthe vault cash balance will be loaded intotempCashBalance - In
setVaultAccountwe call_setVaultAccount - The first line of
_setVaultAccountrequires that the tempCashBalance is equal to zero. This will revert the deleverage transaction before the cash balance is cleared.
0xleastwood
You're right! I've checked this and agree that the flow of execution would look like:
VaultLiquidationAction.deleverageAccount
└── VaultAccount.getVaultAccount
└── liquidator.tempCashBalance = s.primaryCash // Set to non-zero value
└── VaultLiquidationAction._transferVaultSharesToLiquidator
└── liquidator.setVaultAccount
└── VaultAccount._setVaultAccount
└── require(vaultAccount.tempCashBalance == 0) // Revert
@0xleastwood + @xiaoming9090: Verified. Fixed in https://github.com/notional-finance/contracts-v2/pull/135.
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 settlement function transfers out excess cash and then checks the collateral ratio, which can fail and revert, while liquidation functions require the account to be settled first, creating a deadlock.
Impact
Vault accounts could create positions that are never eligible for liquidation, potentially causing the protocol to accrue bad debt.
What the attacker needed
A vault account with excess cash in one currency; debt in another currency; self-liquidation to create the position
The fix
The issue was fixed by changing settlement to leave excess cash in the account's cash balance instead of transferring it out, removing the collateral 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 DeFi protocol, users can borrow against collateral held in a vault. If a user's position becomes risky, liquidators should be able to step in and repay the debt in exchange for a reward. But a flaw meant that some users could make their position impossible to liquidate, leaving the protocol with unpaid debt.
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.