Skip to content
Smart contracts Audit finding Intermediate

Repaying loans with small amounts of debt tokens can lead to underflowing in the `roll` function

Repaying a loan with very small amounts of debt tokens can round the collateral reduction down to zero, so the loan amount shrinks while the collateral stays the same. This can later make the roll function revert because the required new collateral calculation underflows.

tsvetanovv Follow with rvierdiiev, ck and 7 others

Originally disclosed on Sherlock

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

Found by

tsvetanovv, rvierdiiev, ck, zaskoh, Allarious, Trumpero, Breeje, berndartmueller, jonatascm, Deivitto

Summary

Due to precision issues when repaying a loan with small amounts of debt tokens, the loan.amount can be reduced whereas the loan.collateral remains unchanged. This can lead to underflowing in the roll function.

Vulnerability Detail

The decollateralized calculation in the repay function rounds down to zero if the repaid amount is small enough. This allows iteratively repaying a loan with very small amounts of debt tokens without reducing the collateral.

The consequence is that the roll function can revert due to underflowing the newCollateral calculation once the loan.collateral is greater than collateralFor(loan.amount, req.loanToCollateral) (loan.amount is reduced by repaying the loan)

As any ERC-20 tokens with different decimals can be used, this precision issue is amplified if the decimals of the collateral and debt tokens differ greatly.

Impact

The roll function can revert due to underflowing the newCollateral calculation if the repay function is (iteratively) called with small amounts of debt tokens.

Code Snippet

Cooler.sol#L114

Solidity
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; // @audit-info (10e18 * 10) / 1_000e18 = 0 (rounds down due to imprecision)

    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);
}

Cooler.sol#L139

Calculating newCollateral in L139 can potentially revert due to underflowing if loan.collateral is greater than the required collateral (collateralFor(loan.amount, req.loanToCollateral) ).

A malicious user can use the imprecision issue in the repay function in L114 to repay small amounts of debt tokens (loan.collateral * repaid < loan.amount), which leads to no reduction of loan collateral, whereas the loan.amount is reduced.

This will prevent the roll function from being called.

Solidity
function roll (uint256 loanID) external {
    Loan storage loan = loans[loanID];
    Request memory req = loan.request;

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

    if (!loan.rollable)
        revert NotRollable();

    uint256 newCollateral = collateralFor(loan.amount, req.loanToCollateral) - loan.collateral;
    uint256 newDebt = interestFor(loan.amount, req.interest, req.duration);

    loan.amount += newDebt;
    loan.expiry += req.duration;
    loan.collateral += newCollateral;

    collateral.transferFrom(msg.sender, address(this), newCollateral);
}

Tool used

Manual Review

Recommendation

Consider preventing the loan from being repaid if the amount of returned collateral tokens is zero (i.e., decollateralized == 0).

Discussion

hrishibhat

Sponsor comment:

Good Spot.

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

Likely integer division in the repay function rounds the collateral reduction down to zero for small repayments, leaving the collateral unchanged while the debt decreases.

Impact

The roll function can revert, preventing the loan from being rolled over, when repay is called repeatedly with small amounts of debt tokens.

What the attacker needed

An active loan; ability to call repay with small amounts; debt and collateral tokens with differing decimals

The fix

The report recommends preventing repayment when the amount of collateral returned is zero.

How it was found

  1. Call repay with a very small amount of debt tokens so the collateral reduction rounds to zero.
  2. Repeat the small repayments to reduce the loan amount without reducing the collateral.
  3. Call roll, which calculates new collateral and reverts due to underflow.

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.

A smart contract lets people borrow tokens by locking other tokens as collateral. When you repay a tiny amount, the code calculates how much collateral to release, but that calculation rounds down to zero, so you get no collateral back even though your debt goes down. Later, a function that adjusts the loan tries to subtract a larger number from a smaller one, which crashes the transaction.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by tsvetanovv

View all

3 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.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.