Skip to content
Business logic Audit finding Advanced

`MultiInvoker._latest` will return `latestPrice = 0` when latest oracle version is invalid causing liquidation to send 0 fee to liquidator or incorrect order execution

The MultiInvoker contract's _latest function returned a price of zero when the latest oracle version was invalid, because it did not check validity. This caused liquidations to charge no fee and some trigger orders to execute at incorrect prices.

panprog Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-10-perennial-judging/issues/31

Found by

panprog

Summary

There was a slight change of oracle versions handling in 2.1: now each requested oracle version must be commited, either as valid or invalid. This means that now the latest version can be invalid (price = 0). This is handled correctly in Market, which only uses timestamp from the latest oracle version, but the price comes either from latest version (if valid) or global.latestPrice (if invalid).

However, MultiInvoker always uses price from oracle.latest without verifying if it's valid, meaning it will return latestPrice = 0 if the latest oracle version is invalid. This is returned from the _latest function.

Such latest price = 0 leads to 2 main problems:

  • Liquidations orders in MultiInvoker will send 0 liquidation fee to liquidator (will liquidate for free)
  • Some TriggerOrders will trigger incorrectly (canExecuteOrder will return true when the real price didn't reach the trigger price, or false even if the real prices reached the trigger price)

Vulnerability Detail

MultiInvoker._latest has the following code for latest price assignment:

Solidity
OracleVersion memory latestOracleVersion = market.oracle().latest();
latestPrice = latestOracleVersion.price;
IPayoffProvider payoff = market.payoff();
if (address(payoff) != address(0)) latestPrice = payoff.payoff(latestPrice);

This latestPrice is what's returned from the _latest, it isn't changed anywhere else. Notice that there is no check for latest oracle version validity.

And this is the code for KeeperOracle._commitRequested:

Solidity
function _commitRequested(OracleVersion memory version) private returns (bool) {
    if (block.timestamp <= (next() + timeout)) {
        if (!version.valid) revert KeeperOracleInvalidPriceError();
        _prices[version.timestamp] = version.price;
    }
    _global.latestIndex++;
    return true;
}

Notice that commits made outside the timeout window simply increase _global.latestIndex without assigning _prices, meaning it remains 0 (invalid). This means that latest oracle version will return price=0 and will be invalid if commited after the timeout from request time has passed.

Price returned by _latest is used when calculating liquidationFee:

Solidity
function _liquidationFee(IMarket market, address account) internal view returns (Position memory, UFixed6, UFixed6) {
    // load information about liquidation
    RiskParameter memory riskParameter = market.riskParameter();
@@@ (Position memory latestPosition, Fixed6 latestPrice, UFixed6 closableAmount) = _latest(market, account);

    // create placeholder order for liquidation fee calculation (fee is charged the same on all sides)
    Order memory placeholderOrder;
    placeholderOrder.maker = Fixed6Lib.from(closableAmount);

    return (
        latestPosition,
        placeholderOrder
@@@         .liquidationFee(OracleVersion(latestPosition.timestamp, latestPrice, true), riskParameter)
            .min(UFixed6Lib.from(market.token().balanceOf(address(market)))),
        closableAmount
    );
}

liquidationFee calculation in order multiplies order size by latestPrice, meaning it will be 0 when price = 0. This liquidation fee is then used in market.update for liquidation fee to receive by liquidator:

Solidity
    function _liquidate(IMarket market, address account, bool revertOnFailure) internal isMarketInstance(market) {
@@@     (Position memory latestPosition, UFixed6 liquidationFee, UFixed6 closable) = _liquidationFee(market, account);
        Position memory currentPosition = market.pendingPositions(account, market.locals(account).currentId);
        currentPosition.adjust(latestPosition);

        try market.update(
                account,
                currentPosition.maker.isZero() ? UFixed6Lib.ZERO : currentPosition.maker.sub(closable),
                currentPosition.long.isZero() ? UFixed6Lib.ZERO : currentPosition.long.sub(closable),
                currentPosition.short.isZero() ? UFixed6Lib.ZERO : currentPosition.short.sub(closable),
@@@             Fixed6Lib.from(-1, liquidationFee),
                true

This means liquidator will receive 0 fee for the liquidation.

It is also used in canExecuteOrder:

Solidity
    function _executeOrder(address account, IMarket market, uint256 nonce) internal {
        if (!canExecuteOrder(account, market, nonce)) revert MultiInvokerCantExecuteError();
...
    function canExecuteOrder(address account, IMarket market, uint256 nonce) public view returns (bool) {
        TriggerOrder memory order = orders(account, market, nonce);
        if (order.fee.isZero()) return false;
@@@     (, Fixed6 latestPrice, ) = _latest(market, account);
@@@     return order.fillable(latestPrice);
    }

Meaning canExecuteOrder will do comparision with price = 0 instead of real latest price. For example: limit buy order to buy when price <= 1000 (when current price = 1100) will trigger and execute buy at the price = 1100 instead of 1000 or lower.

Impact

  • liquidation done after invalid oracle version via MultiInvoker LIQUIDATE action will charge and send 0 liquidation fee from the liquidating account, thus liquidator loses these funds.
  • some orders with comparison of type -1 (<= price) will incorrectly trigger and will be executed when price is far from reaching the trigger price. This loses user funds due to unexpected execution price of the pending order.

Code Snippet

_latest simply takes oracle.latest price, which can be 0, without any check for oracle version validity: https://github.com/sherlock-audit/2023-10-perennial/blob/main/perennial-v2/packages/perennial-extensions/contracts/MultiInvoker.sol#L394-L402

Tool used

Manual Review

Recommendation

_latest should replicate the process for the latest price from Market instead of using price from the oracle's latest version:

  • if the latest oracle version is valid, then use its price
  • if the latest oracle version is invalid, then iterate all global pending positions backwards and use price of any valid oracle version at the position.
  • if all pending positions are at invalid oracles, use market's global.latestPrice

Discussion

panprog

Mitigation Review:

Fixed.

  • Liquidation now removed from MultiInvoker
  • Order can not be executed for invalid version. This is still not optimal, because it should be possible to directly execute the order even when latest oracle version is invalid (using previous valid oracle price), but MultiInvoker will revert. However, this is low/info.

MLON33

Fix: https://github.com/equilibria-xyz/perennial-v2/pull/166

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

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 _latest function did not verify whether the latest oracle version was valid before using its price.

Impact

Liquidations via MultiInvoker could charge zero fee, and some trigger orders could execute at prices far from the intended trigger, causing loss of funds.

What the attacker needed

Latest oracle version is invalid; use MultiInvoker for liquidation or trigger orders

The fix

The issue was fixed by removing liquidation from MultiInvoker and preventing order execution for invalid oracle versions.

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.

An oracle is a service that provides price data to a smart contract. If the latest price update is marked invalid, its price is zero. The MultiInvoker contract used this zero price without checking, so liquidators received no fee and some orders triggered wrongly.

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 panprog

View all

66 more disclosed reports, best first.

More from Perennial V2 Update #1

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.