Skip to content
Business logic Audit finding Intermediate

`KeeperOracle.request` adds only the first pair of market+account addresses per oracle version to callback list, ignoring all the subsequent ones

The KeeperOracle.request function returns early if the oracle version was already requested, so only the first market and account are added to the callback list for that version. As a result, subsequent markets and accounts that requested the same oracle version are never called back when the oracle settles.

bin2chen Follow with panprog and rvierdiiev

Originally disclosed on Sherlock

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

Found by

bin2chen, panprog, rvierdiiev

Summary

The new feature introduced in 2.1 is the callback called for all markets and market+account pairs which requested the oracle version. These callbacks are called once the corresponding oracle settles. For this reason, KeeperOracle keeps a list of markets and market+account pairs per oracle version to call market.update on them:

Solidity
/// @dev Mapping from version to a set of registered markets for settlement callback
mapping(uint256 => EnumerableSet.AddressSet) private _globalCallbacks;

/// @dev Mapping from version and market to a set of registered accounts for settlement callback
mapping(uint256 => mapping(IMarket => EnumerableSet.AddressSet)) private _localCallbacks;

However, currently KeeperOracle stores only the market+account from the first request call per oracle version, because if the request was already made, it returns from the function before adding to the list:

Solidity
function request(IMarket market, address account) external onlyAuthorized {
    uint256 currentTimestamp = current();
@@@ if (versions[_global.currentIndex] == currentTimestamp) return;

    versions[++_global.currentIndex] = currentTimestamp;
    emit OracleProviderVersionRequested(currentTimestamp);

    // @audit only the first request per version reaches these lines to add market+account to callback list
    _globalCallbacks[currentTimestamp].add(address(market));
    _localCallbacks[currentTimestamp][market].add(account);
    emit CallbackRequested(SettlementCallback(market, account, currentTimestamp));
}

Vulnerability Detail

According to docs, the same KeeperOracle can be used by multiple markets. And every account requesting in the same oracle version is supposed to be called back (settled) once the oracle version settles.

Impact

The new core function of the protocol doesn't work as expected and KeeperOracle will fail to call back markets and accounts if there is more than 1 request in the same oracle version (which is very likely).

Code Snippet

KeeperOracle.request will return early if the request for this oracle version was already made: https://github.com/sherlock-audit/2023-10-perennial/blob/main/perennial-v2/packages/perennial-oracle/contracts/keeper/KeeperOracle.sol#L77

The lines to add market+account to callback list will only be reached once per oracle version: https://github.com/sherlock-audit/2023-10-perennial/blob/main/perennial-v2/packages/perennial-oracle/contracts/keeper/KeeperOracle.sol#L82-L83

Tool used

Manual Review

Recommendation

Move addition to callback list to just before the condition to exit function early:

Solidity
function request(IMarket market, address account) external onlyAuthorized {
    uint256 currentTimestamp = current();
    _globalCallbacks[currentTimestamp].add(address(market));
    _localCallbacks[currentTimestamp][market].add(account);
    emit CallbackRequested(SettlementCallback(market, account, currentTimestamp));
    if (versions[_global.currentIndex] == currentTimestamp) return;

    versions[++_global.currentIndex] = currentTimestamp;
    emit OracleProviderVersionRequested(currentTimestamp);
}

Discussion

panprog

Mitigation Review:

Fixed

MLON33

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

The original report as published on Sherlock, by bin2chen. 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

The early return statement is placed before the code that adds the market and account to the callback lists, so only the first request per oracle version is recorded.

Impact

The protocol's new callback feature fails to notify markets and accounts when there is more than one request in the same oracle version, which is very likely.

What the attacker needed

Multiple markets or accounts request same oracle version

The fix

The addition to the callback list should be moved before the early return condition, so every request is recorded.

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.

This is a smart contract bug where a function is supposed to keep a list of everyone who asked for a price update. But because the function exits early on repeat calls, only the first person gets added to the list. Later, when the price is ready, only that first person is notified, and everyone else is ignored.

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 bin2chen

View all

139 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.