Skip to content
Business logic Audit finding Intermediate

User is unable to partially payback loan if they aren't able to post enough isoUSD to bring them back to minOpeningMargin

The closeLoan function required that after repaying debt and withdrawing collateral, the remaining debt still met the minimum opening margin. This prevented users from making partial repayments without also withdrawing collateral, potentially leading to unfair liquidations.

0x52 Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2022-11-isomorph-judging/issues/161

Found by

0x52

Summary

The only way for a user to reduce their debt is to call closeLoan. If the amount repaid does not bring the user back above minOpeningMargin then the transaction will revert. This is problematic for users that wish to repay their debt but don't have enough to get back to minOpeningMargin as it could lead to unfair liquidations.

Vulnerability Detail

    if(outstandingisoUSD >= TENTH_OF_CENT){ //ignore leftover debts less than $0.001
        uint256 collateralLeft = collateralPosted[_collateralAddress][msg.sender] - _collateralToUser;
        uint256 colInUSD = priceCollateralToUSD(currencyKey, collateralLeft); 
        uint256 borrowMargin = (outstandingisoUSD * minOpeningMargin) / LOAN_SCALE;
        require(colInUSD > borrowMargin , "Remaining debt fails to meet minimum margin!");
    }

The checks above are done when a user calls closeLoan. This ensures that the user's margin is back above minOpeningMargin before allowing them to remove any collateral. This is done as a safeguard to block loans users from effectively opening loans at lower than desired margin. This has the unintended consequence that as user cannot pay off any of their loan if they do not increase their loan back above minOpeningMargin. This could prevent users from being able to save a loan that is close to liquidation causing them to get liquidated when they otherwise would have paid off their loan.

Impact

User is unable to make partial repayments if their payment does not increase margin enough

Code Snippet

https://github.com/sherlock-audit/2022-11-isomorph/blob/main/contracts/Isomorph/contracts/Vault_Synths.sol#L197-L248

Tool used

Manual Review

Recommendation

I recommend adding a separate function that allows users to pay off their loan without removing any collateral:

function paybackLoan(
    address _collateralAddress,
    uint256 _USDToVault
    ) external override whenNotPaused 
    {
    _collateralExists(_collateralAddress);
    _closeLoanChecks(_collateralAddress, 0, _USDToVault);
    //make sure virtual price is related to current time before fetching collateral details
    //slither-disable-next-line reentrancy-vulnerabilities-1
    _updateVirtualPrice(block.timestamp, _collateralAddress);
    (   
        bytes32 currencyKey,
        uint256 minOpeningMargin,
        ,
        ,
        ,
        uint256 virtualPrice,

    ) = _getCollateral(_collateralAddress);
    //check for frozen or paused collateral
    _checkIfCollateralIsActive(currencyKey);

    uint256 isoUSDdebt = (isoUSDLoanAndInterest[_collateralAddress][msg.sender] * virtualPrice) / LOAN_SCALE;
    require( isoUSDdebt >= _USDToVault, "Trying to return more isoUSD than borrowed!");
    uint256 outstandingisoUSD = isoUSDdebt - _USDToVault;

    uint256 collateral = collateralPosted[_collateralAddress][msg.sender];
    uint256 colInUSD = priceCollateralToUSD(currencyKey, collateral); 
    uint256 borrowMargin = (outstandingisoUSD * liquidatableMargin) / LOAN_SCALE;
    require(colInUSD > borrowMargin , "Liquidation margin not met!");

    //record paying off loan principle before interest
    //slither-disable-next-line uninitialized-local-variables
    uint256 interestPaid;
    uint256 loanPrinciple = isoUSDLoaned[_collateralAddress][msg.sender];
    if( loanPrinciple >= _USDToVault){
        //pay off loan principle first
        isoUSDLoaned[_collateralAddress][msg.sender] = loanPrinciple - _USDToVault;
    }
    else{
        interestPaid = _USDToVault - loanPrinciple;
        //loan principle is fully repaid so record this.
        isoUSDLoaned[_collateralAddress][msg.sender] = 0;
    }
    //update mappings with reduced amounts
    isoUSDLoanAndInterest[_collateralAddress][msg.sender] = isoUSDLoanAndInterest[_collateralAddress][msg.sender] - ((_USDToVault * LOAN_SCALE) / virtualPrice);
    emit ClosedLoan(msg.sender, _USDToVault, currencyKey, 0);
    //Now all effects are handled, transfer the assets so we follow CEI pattern
    _decreaseLoan(_collateralAddress, 0, _USDToVault, interestPaid);
}

Discussion

kree-dotcom

Applied fix to Vault_Synth here https://github.com/kree-dotcom/isomorph/commit/6b0879e59a3e5156fde88f4c90d0029aa31b3786 Vault_Velo and Vault_Lyra fixed here https://github.com/kree-dotcom/isomorph/commit/7b113eaf19c126db796190f271d85bc5c2cba865

change the closeLoan() check to

if((outstandingisoUSD > 0) && (_collateralToUser > 0)){ //leftover debt must meet minOpeningMargin if requesting collateral back uint256 collateralLeft = ... ... }

This way checks are only triggered if the user is repaying some of their loan and requesting capital back. This allows us to prevent people from opening loans with a lower than minOpeningMargin but allows them to reduce their loan without needing to match the minOpeningMargin.

Note Vault_Velo has to use a slightly different method because we are handling NFTs instead of ERC20s. I have checked the _calculateProposedReturnedCapital() function we are relying on will also work fine if given an array of non-owned NFTs (i.e. the user is not receiving any collateral back just reducing their loan) by returning 0.

Fix typo in Vault_Velo https://github.com/kree-dotcom/isomorph/commit/67b2f981a7cbe3fb0b6a057dab82d69d6d61fdc9

0x00052

Fixes look good. Only values the collateral if there is debt and the user is removing collateral

The original report as published on Sherlock, by 0x52. 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

The closeLoan function applied the minimum opening margin check even when the user was only repaying debt and not withdrawing collateral, preventing partial repayments that would leave the loan below the margin.

Impact

Users could be unable to make partial repayments, potentially leading to unfair liquidations when they could have otherwise reduced their debt.

What the attacker needed

User has an existing loan; user wants to repay part of the loan; repayment does not bring margin above minOpeningMargin

The fix

The fix changed the check to only apply when the user is both repaying debt and withdrawing collateral, allowing partial repayments without collateral withdrawal.

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 a lending system, a user borrows money (isoUSD) by putting up collateral. The system requires that the borrowed amount stays below a certain fraction of the collateral value (the margin). The closeLoan function let users repay part of their loan and take back some collateral, but it checked that the remaining debt still met the margin requirement. This meant that if a user's debt was already close to the limit, they couldn't repay any amount without also adding more collateral, which could lead to them being liquidated even though they were trying to reduce their 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.

More by 0x52

View all

211 more disclosed reports, best first.

More from Isomorph

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.