Skip to content
Business logic Audit finding Beginner

Incorrect Freshness Logic Validation in PythOracle breaking the entire mechanism for triggering orders

The PythOracle contract's freshness check uses a reversed comparison, so it always treats price data as stale. As a result, valid orders that rely on this oracle fail to execute.

0x486776 Follow with 0xCNX, 0xMitev and 47 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-11-oku-judging/issues/115

Found by

0x486776, 0xCNX, 0xMitev, 0xNirix, 0xRiO, 0xShoonya, 0xaxaxa, 0xc0ffEE, 0xgremlincat, 0xmujahid002, 10ap17, 4gontuk, BijanF, Boy2000, BugPull, ChaosSR, Contest-Squad, DharkArtz, ExtraCaterpillar, Icon0x, John44, LonWof-Demon, LordAdhaar, MoonShadow, NickAuditor2, PoeAudits, Pro_King, Smacaud, Sparrow_Jac, TxGuard, Uddercover, Waydou, Weed0607, X0sauce, Xcrypt, ZanyBonzy, curly, durov, jovemjeune, lukris02, mladenov, mxteem, nikhilx0111, oxwhite, pkabhi01, s0x0mtee, safdie, sakibcy, silver_eth, skid0016, t0x1c, tmotfl, tnevler, udo, vinica_boy, xiaoming90, yovchev_yoan, zhenyazhd, zhoo

Summary

The PythOracle contract incorrectly validates the freshness of price data using the getPriceUnsafe() function. The current logic ensures that prices are always considered stale, which results in valid orders failing to execute.

Root Cause

In PythOracle.sol:29, the logic : https://github.com/sherlock-audit/2024-11-oku/blob/main/oku-custom-order-types/contracts/oracle/External/PythOracle.sol#L28-L31

is incorrect. The condition ensures that the price is always considered stale, regardless of whether the price is recent or valid. The comparison fails to verify if the price.publishTime is newer than the defined threshold.

Internal pre-conditions

1- The noOlderThan parameter is set during the function call, defining the allowed freshness window for price data. 2- A valid price.publishTime is provided by the oracle, but due to incorrect logic, it fails validation.

External pre-conditions

1- The price feed from the Pyth Oracle contains a valid and fresh publishTime that is newer than block.timestamp - noOlderThan. 2- No tampering or delays in oracle updates occur externally.

Attack Path

1- Alice places a stop-limit order for a token pair using the PythOracle as the price feed. 2- The oracle updates its price feed, providing a fresh price with a publishTime newer than block.timestamp - noOlderThan. 3- When checkInRange() is called, the require condition in PythOracle.sol:29 evaluates the price as stale, despite it being valid. 4- The order fails to execute as the system misinterprets the freshness of the price data.

Impact

The protocol and its users face the following consequences:

User Losses:

  • Orders fail to execute at the right price, leading to missed opportunities for profit or failure to exit losing positions.
  • This affects users placing stop-loss or take-profit orders reliant on timely price updates.

Protocol Reputation:

  • Continuous failures in executing valid orders due to perceived stale data undermine user trust in the system.

PoC

Example Scenario

1- Alice places a stop-limit order to sell Token A for Token B if the price of Token A falls below 50. 2- The oracle updates its price feed with a publishTime of block.timestamp - 5 seconds. 3- The noOlderThan parameter is set to 30 seconds.

Execution: The condition in PythOracle.sol:29 evaluates:

Solidity

require(price.publishTime < block.timestamp - 30, "Stale Price");
  • With price.publishTime = block.timestamp - 5, the condition becomes:
Solidity

block.timestamp - 5 < block.timestamp - 30
  • This condition is always false, causing the price to be deemed stale.

Result:

  • Alice's stop-limit order does not execute, resulting in financial losses as she misses the opportunity to sell her tokens before the price drops further.

Mitigation

Correct the logic to ensure the freshness validation verifies that the publishTime is newer than the threshold:

Solidity

require(price.publishTime >= block.timestamp - noOlderThan, "Stale Price");

The following test demonstrates the issue and verifies the fix:

Solidity

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "forge-std/Test.sol";

contract PythOracleTest is Test {
    uint256 public noOlderThan = 30;

    function testIncorrectFreshnessCheck() public {
        uint256 currentTime = block.timestamp;
        uint256 validPublishTime = currentTime - 5;

        // Incorrect logic
        bool stale = (validPublishTime < currentTime - noOlderThan);
        assertTrue(stale); // This fails even though the price is valid.

        // Correct logic
        bool fresh = (validPublishTime >= currentTime - noOlderThan);
        assertTrue(fresh); // This passes as the price is valid.
    }
}

Discussion

sherlock-admin2

The protocol team fixed this issue in the following PRs/commits: https://github.com/gfx-labs/oku-custom-order-types/pull/1

The original report as published on Sherlock, by 0x486776. 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 freshness condition uses a less-than comparison instead of greater-than-or-equal, so it always evaluates to true for any recent price.

Impact

Valid orders fail to execute, causing users to miss profit opportunities or fail to exit losing positions, which could undermine trust in the protocol.

What the attacker needed

PythOracle used as price feed; valid fresh price data available

The fix

The logic should be corrected to require that the publishTime is greater than or equal to block.timestamp minus noOlderThan.

How it was found

  1. Alice places a stop-limit order using PythOracle as the price feed.
  2. The oracle updates with a fresh publishTime newer than block.timestamp - noOlderThan.
  3. When checkInRange() is called, the require condition evaluates the price as stale.
  4. The order fails to execute.

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 smart contract checks whether price data is recent enough before using it. The check was written backwards, so it always says the price is too old, even when it is fresh. This stops orders from going through.

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 0x486776

View all

7 more disclosed reports, best first.

More from Oku's New Order Types Contract Contest

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.