Skip to content
Smart contracts Audit finding Advanced

Oracle Data Corruption via Storage Key Collision

The Oracle contract derives storage slots for token metadata and historical snapshots in a way that allows a collision between the Counts struct of one token and the Snapshot struct of another token with a specially crafted address. An attacker can corrupt the oracle's price and liquidity history for that token, causing downstream consumers to make incorrect pricing or risk decisions.

BlueSheep Follow with 0xauditagent, CoMMaNDO and 5 others

Originally disclosed on Code4rena

Submitted by BlueSheep, also found by 0xauditagent, CoMMaNDO, dantehrani, hirusha, ht111111, piki, and thimthor

https://github.com/code-423n4/2025-11-ekubo/blob/main/src/extensions/Oracle.sol#L97-L100

https://github.com/code-423n4/2025-11-ekubo/blob/main/src/extensions/Oracle.sol#L108-L111

https://github.com/code-423n4/2025-11-ekubo/blob/main/src/extensions/Oracle.sol#L139-L142

The Oracle extension uses a storage key derivation scheme that allows collisions between the Counts struct of one token and the Snapshot structs of another token. This permits an attacker to corrupt the oracle data (price and liquidity history) of a token that has specific address properties (e.g., 32 leading zeros), breaking the correctness and integrity of the Oracle’s historical data and any consumers that rely on it (e.g., ERC7726, PriceFetcher).

Vulnerability Details

The Oracle contract stores two types of data in its ExposedStorage:

  1. Counts struct (metadata) at the slot corresponding to the token address:

    // src/extensions/Oracle.sol
    Counts c;
    assembly ("memory-safe") {
       c := sload(token)
    }
    
  2. Snapshot structs (historical data) at a slot derived from the token address and index:

    // src/extensions/Oracle.sol
    assembly ("memory-safe") {
       last := sload(or(shl(32, token), index))
    }
    

The key for a snapshot is (token << 32) | index.

The key for counts is token.

A collision occurs if tokenA = (tokenB << 32) | index.

If tokenB is an address with 32 leading zeros (i.e., tokenB < 2^128), then tokenB << 32 fits within the 160 bits of an address (plus the index in the lower 32 bits).

Specifically, if tokenB has 32 leading zeros, let tokenA = address(tokenB << 32).

The storage slot for Counts(tokenA) is tokenA.

The storage slot for Snapshot(tokenB, 0) is (tokenB << 32) | 0, which equals tokenA.

Thus, initializing a pool for tokenA writes a Counts struct to the slot tokenA. This overwrites the Snapshot struct for tokenB at index 0.

The Counts struct has the following layout:

  • index (0-31 bits)
  • count (32-63 bits)
  • capacity (64-95 bits)
  • lastTimestamp (96-127 bits)

The Snapshot struct has the following layout:

  • timestamp (0-31 bits)
  • secondsPerLiquidityCumulative (32-191 bits)
  • tickCumulative (192-255 bits)

When Counts overwrites Snapshot:

  • Counts.index (usually 0 initially) overwrites Snapshot.timestamp.
  • Counts.count, capacity, lastTimestamp overwrite the lower bits of Snapshot.secondsPerLiquidityCumulative.
  • The high bits used for tickCumulative are also zeroed out.

As a result, the data stored at that snapshot index for tokenB is no longer a real historical observation, but a synthetic value derived from the Counts metadata of tokenA.

This has two concrete consequences:

  • Broken time ordering assumptions: Oracle.searchRangeForPrevious performs a binary search over snapshots assuming timestamps are monotonically increasing. Overwriting a snapshot’s timestamp with 0 can violate this monotonicity, causing the binary search to select an unexpected snapshot or, in edge cases, revert with NoPreviousSnapshotExists even when data exists.
  • Corrupted cumulative values used by consumers: Helpers like OracleLib.getEarliestSnapshotTimestamp and getMaximumObservationPeriod read snapshot 0 and are used by PriceFetcher to decide how far back it is safe to query. After corruption, these functions will return values derived from garbage data, and any TWAP / volatility / liquidity statistics that cross the corrupted snapshot will no longer reflect the true price history. ERC7726 and PriceFetcher both rely on extrapolateSnapshot and these cumulative values, so their on-chain price outputs for the affected token become untrustworthy.

Note that TWAPs in this system are generally computed from differences of cumulative values between two times (e.g., end - start divided by endTime - startTime). If both endpoints lie on the same linear segment (e.g., both extrapolated from the same corrupted snapshot), a pure timestamp shift alone will cancel out in the difference. The core issue here is not just “timestamp becomes 0”, but that we have injected an inconsistent, non-historical snapshot into the time series, which can change which snapshot is chosen and what cumulative values are used for TWAP and related calculations.

Impact

An attacker can corrupt the historical oracle data for any token whose address participates in such a collision, causing Oracle-based views (e.g., ERC7726 quotes, PriceFetcher TWAPs / volatilities / liquidity averages) to diverge from the real price path. This breaks the integrity guarantees expected from a manipulation-resistant oracle. While the current Ekubo core AMM does not directly use this oracle for safety-critical checks (so there is no immediate insolvency path inside this repo), downstream protocols that consume Ekubo’s oracle / ERC-7726 prices on-chain can make incorrect risk or pricing decisions based on corrupted data. The vulnerability requires a specially structured pair of token addresses, but such vanity addresses can be deliberately generated or may naturally exist.

Recommendations

Use keccak256 to derive the snapshot storage slot to avoid collisions.

// src/extensions/Oracle.sol

function getSnapshotSlot(address token, uint32 index) private pure returns (bytes32 slot) {
    assembly ("memory-safe") {
        mstore(0, token)
        mstore(32, index)
        slot := keccak256(0, 64)
    }
}

And replace the assembly sload/sstore calls to use this computed slot.

Proof of Concept

Note: The provided PoC requires replacing the assembly clz instruction with LibBit.clz (or equivalent) in src/math/ticks.sol, src/types/bitmap.sol, and src/math/time.sol if the testing environment’s compiler does not support the clz instruction. This modification is purely for compilation compatibility and does not affect the vulnerability logic.

The following test demonstrates the collision. Add this to test/OracleCollision.t.sol.

// SPDX-License-Identifier: ekubo-license-v1.eth
pragma solidity >=0.8.30;

import {PoolKey} from "../src/types/poolKey.sol";
import {createFullRangePoolConfig} from "../src/types/poolConfig.sol";
import {FullTest} from "./FullTest.sol";
import {oracleCallPoints} from "../src/extensions/Oracle.sol";
import {IOracle} from "../src/interfaces/extensions/IOracle.sol";
import {CoreLib} from "../src/libraries/CoreLib.sol";
import {OracleLib} from "../src/libraries/OracleLib.sol";
import {Snapshot} from "../src/types/snapshot.sol";
import {Counts} from "../src/types/counts.sol";
import {NATIVE_TOKEN_ADDRESS} from "../src/math/constants.sol";

contract OracleCollisionTest is FullTest {
    using CoreLib for *;
    using OracleLib for *;

    IOracle internal oracle;

    function setUp() public virtual override {
        FullTest.setUp();
        address deployAddress = address(uint160(oracleCallPoints().toUint8()) << 152);
        deployCodeTo("Oracle.sol", abi.encode(core), deployAddress);
        oracle = IOracle(deployAddress);
    }

    function createOraclePool(address quoteToken, int32 tick) internal returns (PoolKey memory poolKey) {
        poolKey = createPool(NATIVE_TOKEN_ADDRESS, quoteToken, tick, createFullRangePoolConfig(0, address(oracle)));
    }

    function test_collision_between_token_counts_and_snapshot() public {
        // 1. Choose tokenB as a small address (mimicking an address with leading zeros)
        // Avoid precompiles (1-9)
        address tokenB = address(100);

        // 2. Calculate colliding tokenA
        // Slot for snapshot index 0 of tokenB is: (tokenB << 32) | 0
        address tokenA = address(uint160(uint256(uint160(tokenB)) << 32));

        // 3. Initialize Pool with tokenB
        // This initializes Counts at slot `tokenB`
        createOraclePool(tokenB, 0);
        uint32 initTime = oracle.snapshots(tokenB, 0).timestamp();

        // 4. Verify initial state of Snapshot 0 (it should be empty/zero or init state)
        // Actually createOraclePool calls beforeInitializePool which initializes Snapshot 0.
        Snapshot s0 = oracle.snapshots(tokenB, 0);
        uint32 timestamp0 = s0.timestamp();
        assertEq(timestamp0, initTime);

        // 5. Initialize Pool with tokenA
        // This writes Counts to slot `tokenA`, which IS the slot for Snapshot 0 of tokenB
        vm.warp(block.timestamp + 100);
        createOraclePool(tokenA, 0);

        // 6. Verify Corruption
        Snapshot s0_corrupted = oracle.snapshots(tokenB, 0);

        // Counts.index (0) overwrites Snapshot.timestamp
        uint32 corruptedTimestamp = s0_corrupted.timestamp();

        assertEq(corruptedTimestamp, 0);
        assert(corruptedTimestamp != timestamp0);

        // Counts data overwrites secondsPerLiquidityCumulative
        uint160 corruptedSPC = s0_corrupted.secondsPerLiquidityCumulative();
        assert(corruptedSPC != 0);
    }
}

The original report as published on Code4rena, by BlueSheep. 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 storage slot for a snapshot is computed as `(token << 32) | index`, which can equal the slot for another token's Counts struct when that other token's address has 32 leading zeros.

Impact

An attacker could corrupt the historical oracle data for a token, causing Oracle-based views such as ERC7726 quotes and PriceFetcher TWAPs to diverge from the real price path, though the current Ekubo core AMM does not directly use this oracle for safety-critical checks.

What the attacker needed

A token address with 32 leading zeros; ability to initialize a pool for the colliding token

The fix

Use keccak256 to derive the snapshot storage slot to avoid collisions.

How it was found

  1. Choose a token address with 32 leading zeros (tokenB).
  2. Compute the colliding token address tokenA as tokenB shifted left by 32 bits.
  3. Initialize a pool for tokenB, which writes its Counts struct to slot tokenB.
  4. Initialize a pool for tokenA, which writes its Counts struct to slot tokenA, overwriting the Snapshot at index 0 for tokenB.
  5. Observe that the snapshot timestamp for tokenB becomes zero and the cumulative values are corrupted.

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 stores data in numbered slots. This report shows that two different pieces of data can end up in the same slot if one token's address is chosen in a special way. An attacker can then overwrite the price history of another token, making the oracle return wrong values.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More from Ekubo

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.