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
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);
}
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.
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.
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
- Call repay with a very small amount of debt tokens so the collateral reduction rounds to zero.
- Repeat the small repayments to reduce the loan amount without reducing the collateral.
- 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.