Skip to content
Business logic Audit finding Intermediate

Fully repaying a loan will result in debt payment being lost

When a loan is fully repaid, the loan storage is deleted before the debt transfer, causing the lender address to become zero. The debt tokens are then sent to the zero address, resulting in lost funds for the lender.

0x52 Follow with wagmi, serial-coder and 7 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-01-cooler-judging/issues/33

Found by

0x52, wagmi, serial-coder, HonorLt, stent, Avci, libratus, Bahurum, ElKu, berndartmueller

Summary

When a loan is fully repaid the loan storage is deleted. Since loan is a storage reference to the loan, loan.lender will return address(0) after the loan has been deleted. This will result in the debt being transferred to address(0) instead of the lender. Some ERC20 tokens will revert when being sent to address(0) but a large number will simply be sent there and lost forever.

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 Cooler#repay the loan storage associated with the loanID being repaid is deleted. loan is a storage reference so when loans[loanID] is deleted so is loan. The result is that loan.lender is now address(0) and the loan payment will be sent there instead.

Impact

Lender's funds are sent to address(0)

Code Snippet

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

Tool used

Manual Review

Recommendation

Send collateral/debt then delete:

-   if (repaid == loan.amount) delete loans[loanID];
+   if (repaid == loan.amount) {
+       debt.transferFrom(msg.sender, loan.lender, loan.amount);
+       collateral.transfer(owner, loan.collateral);
+       delete loans[loanID];
+       return;
+   }

Discussion

hrishibhat

Sponsor comment:

Great spot, embarassing oversight.

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 loan storage is deleted before the debt transfer, so the lender address is zero when the transfer occurs.

Impact

The lender's funds are sent to the zero address and lost, though some ERC20 tokens may revert and prevent the loss.

What the attacker needed

A loan exists and is fully repaid

The fix

The debt and collateral transfers should be performed before deleting the loan storage.

How it was found

  1. Call the repay function with the full loan amount.
  2. The contract deletes the loan storage, setting the lender address to zero.
  3. The contract transfers the debt tokens to the zero address, losing them.

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 smart contract, data can be stored permanently on the blockchain. When a loan is fully repaid, the contract deletes the loan record. But because the code uses a reference to that record, after deletion the lender's address becomes zero. The repayment is then sent to the zero address, which is like a black hole, so the lender never gets their money back.

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 Cooler Update

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.