Bad debt may persist even after complete liquidation in Velo Vault due to truncation
The vault calculates collateral value in two different ways: one sums pooled tokens before pricing, the other prices each NFT individually and truncates. This can cause the check for full liquidation to fail even when all collateral is taken, leaving bad debt uncleared.
Source: https://github.com/sherlock-audit/2022-11-isomorph-judging/issues/174
Found by
0x52
Summary
When liquidating a user, if all their collateral is taken but it is not valuable enough to repay the entire loan they would be left with remaining debt. This is what is known as bad debt because there is no collateral left to take and the user has no obligation to pay it back. When this occurs, the vault will forgive the user's debts, clearing the bad debt. The problem is that the valuations are calculated in two different ways which can lead to truncation issue that completely liquidates a user but doesn't clear their bad debt.
Vulnerability Detail
uint256 totalUserCollateral = totalCollateralValue(_collateralAddress, _loanHolder);
uint256 proposedLiquidationAmount;
{ //scope block for liquidationAmount due to stack too deep
uint256 liquidationAmount = viewLiquidatableAmount(totalUserCollateral, 1 ether, isoUSDBorrowed, liquidatableMargin);
require(liquidationAmount > 0 , "Loan not liquidatable");
proposedLiquidationAmount = _calculateProposedReturnedCapital(_collateralAddress, _loanNFTs, _partialPercentage);
require(proposedLiquidationAmount <= liquidationAmount, "excessive liquidation suggested");
}
uint256 isoUSDreturning = proposedLiquidationAmount*LIQUIDATION_RETURN/LOAN_SCALE;
if(proposedLiquidationAmount >= totalUserCollateral){
//@audit bad debt cleared here
}
The primary check before clearing bad debt is to check if proposedLiquidationAmount >= totalUserCollateral. The purpose of this check is to confirm that all of the user's collateral is being liquidated. The issue is that each value is calculated differently.
function totalCollateralValue(address _collateralAddress, address _owner) public view returns(uint256){
NFTids memory userNFTs = loanNFTids[_collateralAddress][_owner];
IDepositReceipt depositReceipt = IDepositReceipt(_collateralAddress);
//slither-disable-next-line uninitialized-local-variables
uint256 totalPooledTokens;
for(uint256 i =0; i < NFT_LIMIT; i++){
//check if each slot contains an NFT
if (userNFTs.ids[i] != 0){
totalPooledTokens += depositReceipt.pooledTokens(userNFTs.ids[i]);
}
}
return(depositReceipt.priceLiquidity(totalPooledTokens));
}
totalCollateralValue it used to calculate totalUserCollateral. In this method the pooled tokens are summed across all NFT's then they are priced. This means that the value of the liquidity is truncated exactly once.
function _calculateProposedReturnedCapital(
address _collateralAddress,
CollateralNFTs calldata _loanNFTs,
uint256 _partialPercentage
) internal view returns(uint256){
//slither-disable-next-line uninitialized-local-variables
uint256 proposedLiquidationAmount;
require(_partialPercentage <= LOAN_SCALE, "partialPercentage greater than 100%");
for(uint256 i = 0; i < NFT_LIMIT; i++){
if(_loanNFTs.slots[i] < NFT_LIMIT){
if((i == NFT_LIMIT -1) && (_partialPercentage > 0) && (_partialPercentage < LOAN_SCALE) ){
//final slot is NFT that will be split if necessary
proposedLiquidationAmount +=
(( _priceCollateral(IDepositReceipt(_collateralAddress), _loanNFTs.ids[i])
*_partialPercentage)/ LOAN_SCALE);
}
else {
proposedLiquidationAmount += _priceCollateral(IDepositReceipt(_collateralAddress), _loanNFTs.ids[i]);
}
}
}
return proposedLiquidationAmount;
}
_calculateProposedReturnedCapital is used to calculate proposedLiquidationAmount. The key difference is that each NFT is priced individually. The result is that the value is truncated up to NFT_LIMIT times. This can lead to proposedLiquidationAmount being less than totalUserCollateral even if all user collateral is being liquidated.
Example:
User A has 2 NFTs. They are valued as follows assuming no truncation: 10.6 and 10.7. When calculating via totalCollateralValue they will be summed before they are truncated while in _calculateProposedReturnedCapital they will be truncated before they are summed.
totalCollateralValue: 10.6 + 10.7 = 21.3 => 21 (truncated)
_calculateProposedReturnedCapital: 10.6 => 10 (truncated) 10.7 => 10 (truncated)
10 + 10 = 20
As shown above when using the exact same inputs into our two different functions the final answer is different. In a scenario like this, even though all collateral is taken from the user, their bad debt won't be cleared.
Impact
Bad debt will not be cleared in some liquidation scenarios
Code Snippet
Tool used
Manual Review
Recommendation
_calculateProposedReturnedCapital should be changed to be similar to totalCollateralValue, summing all pooled tokens before pricing:
function _calculateProposedReturnedCapital(
address _collateralAddress,
CollateralNFTs calldata _loanNFTs,
uint256 _partialPercentage
) internal view returns(uint256) {
+ IDepositReceipt depositReceipt = IDepositReceipt(_collateralAddress);
//slither-disable-next-line uninitialized-local-variables
+ uint256 totalPooledTokens
- uint256 proposedLiquidationAmount;
require(_partialPercentage <= LOAN_SCALE, "partialPercentage greater than 100%");
for(uint256 i = 0; i < NFT_LIMIT; i++){
if(_loanNFTs.slots[i] < NFT_LIMIT){
if((i == NFT_LIMIT -1) && (_partialPercentage > 0) && (_partialPercentage < LOAN_SCALE) ){
//final slot is NFT that will be split if necessary
+ totalPooledTokens += ((depositReceipt.pooledTokens(userNFTs.ids[i]) * _partialPercentage) / LOAN_SCALE);
- proposedLiquidationAmount +=
- (( _priceCollateral(IDepositReceipt(_collateralAddress), _loanNFTs.ids[i])
- *_partialPercentage)/ LOAN_SCALE);
}
else{
+ totalPooledTokens += depositReceipt.pooledTokens(userNFTs.ids[i]);
- proposedLiquidationAmount += _priceCollateral(IDepositReceipt(_collateralAddress), _loanNFTs.ids[i]);
}
}
}
+ return(depositReceipt.priceLiquidity(totalPooledTokens));
- return proposedLiquidationAmount;
}
Discussion
kree-dotcom
Fixed in the two following commits https://github.com/kree-dotcom/isomorph/commit/6c0bd26136ff4b33f23437551e22b1066156995b
Some errors from the above commit were fixed here https://github.com/kree-dotcom/isomorph/commit/4322e27fa3e61f1515ba8e88d275676c7b9ed915
0x00052
Fixes look good. Vault_Velo#_calculateProposedReturnedCapital now sums all tokens then value all at once to avoid truncation
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 inconsistency between summing pooled tokens before pricing (in totalCollateralValue) and pricing each NFT individually before summing (in _calculateProposedReturnedCapital), causing different truncation results.
Impact
Bad debt may not be cleared in some liquidation scenarios, leaving the protocol with unbacked debt.
What the attacker needed
User has multiple NFTs as collateral; liquidation occurs
The fix
The fix was to change _calculateProposedReturnedCapital to sum all pooled tokens before pricing, matching totalCollateralValue.
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 a borrower's collateral is worth less than their loan, the leftover debt is called bad debt. The vault tries to clear this debt when it takes all the collateral, but it uses two different maths methods that round down differently. So sometimes it thinks not all collateral was taken, and the bad debt stays on the books.
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.