Skip to content
Business logic Audit finding Intermediate

Wrong liquidation logic

The liquidation check in a cross-margin lending contract used an inverted comparison, so the condition for being liquidatable was reversed. As a result, healthy traders could be liquidated while under-collateralised traders could not be.

Marginswap

Originally disclosed on Code4rena

The belowMaintenanceThreshold function decides if a trader can be liquidated:

function belowMaintenanceThreshold(CrossMarginAccount storage account)
    internal
    returns (bool)
{
    uint256 loan = loanInPeg(account, true);
    uint256 holdings = holdingsInPeg(account, true);
    // The following should hold:
    // holdings / loan >= 1.1
    // =>
    return 100 * holdings >= liquidationThresholdPercent * loan;
}

The inequality in the last equation is wrong because it says the higher the holdings (margin + loan) compared to the loan, the higher the chance of being liquidated. The inverse equality was probably intended return 100 * holdings <= liquidationThresholdPercent * loan;. Users that shouldn’t be liquidated can be liquidated, and users that should be liquidated cannot get liquidated.

The original report as published on Code4rena. 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 comparison operator in the liquidation threshold check is inverted, using greater-than-or-equal where less-than-or-equal was intended.

What the attacker needed

A cross-margin account with a loan

The fix

The comparison should be flipped so that liquidation triggers only when holdings are at or below the threshold relative to the loan.

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 lending contract lets people borrow against the value of what they hold, and if their holdings fall too low compared with the loan, someone can close the position (liquidate it). This code compared the two numbers the wrong way round, so the safety check fired for the wrong people. That means safe borrowers could be closed out and risky ones left open.

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 from Marginswap

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.