Skip to content
This repository has been archived by the owner on May 26, 2023. It is now read-only.

ak1 - repay business logic should handle the case if the repaid amount is greater than loan amount. #327

Closed
github-actions bot opened this issue Jan 27, 2023 · 0 comments
Labels
Duplicate A valid issue that is a duplicate of an issue with `Has Duplicates` label Medium A valid Medium severity issue Reward A payout will be made for this issue

Comments

@github-actions
Copy link

github-actions bot commented Jan 27, 2023

ak1

medium

repay business logic should handle the case if the repaid amount is greater than loan amount.

Summary

In cooler contract, there is a business logic implemented where the borrower or anyone can repay the loan either partically or fully.

but in one scenario, the repaid amount can be greater then the loan amount. in this case, the contract reverts which would be a loss to imo. The borrower can change the mind and come back to repay again which could not be sure if they change their mindset.

Vulnerability Detail

function repay (uint256 loanID, uint256 repaid) external {
    Loan storage loan = loans[loanID];


    if (block.timestamp > loan.expiry) 
        revert Default();
    
    uint256 decollateralized = loan.collateral * repaid / loan.amount;


    if (repaid == loan.amount) delete loans[loanID];
    else {
        loan.amount -= repaid;
        loan.collateral -= decollateralized;
    }


    debt.transferFrom(msg.sender, loan.lender, repaid);
    collateral.transfer(owner, decollateralized);
}

in above if statement if (repaid == loan.amount) if the repaid > loan.amount, then the else part is executed where the transaction will revert due to underflow.

Impact

Repayment could be postponed or skipped if the borrower change their mindset.

Code Snippet

https://github.com/sherlock-audit/2023-01-cooler/blob/main/src/Cooler.sol#L108-L124

Tool used

Manual Review

Recommendation

Update the logic as shown below.

    function repay (uint256 loanID, uint256 repaid) external {
    Loan storage loan = loans[loanID];


    if (block.timestamp > loan.expiry) 
        revert Default();

  + if (repaid >=  loan.amount)--------------updated
  +    repaid = loan.amount;------------------updated
    
    uint256 decollateralized = loan.collateral * repaid / loan.amount;


    if (repaid == loan.amount) delete loans[loanID];
    else {
        loan.amount -= repaid;
        loan.collateral -= decollateralized;
    }


    debt.transferFrom(msg.sender, loan.lender, repaid);
    collateral.transfer(owner, decollateralized);
}

Duplicate of #218

@github-actions github-actions bot added Duplicate A valid issue that is a duplicate of an issue with `Has Duplicates` label Medium A valid Medium severity issue labels Jan 27, 2023
@sherlock-admin sherlock-admin added the Reward A payout will be made for this issue label Feb 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate A valid issue that is a duplicate of an issue with `Has Duplicates` label Medium A valid Medium severity issue Reward A payout will be made for this issue
Projects
None yet
Development

No branches or pull requests

1 participant