Skip to content
Smart contracts Audit finding Intermediate

Incorrect UPPER_118BITS_MASK mask in OraclePackLibrary causes unexpected clearing of EMAs and lockMode in OraclePack

The UPPER_118BITS_MASK constant in OraclePackLibrary is too short, so when rebaseOraclePack applies it, the mask clears the EMAs and lockMode fields in addition to the intended referenceTick and residuals. This corrupts the OraclePack data, compromising its integrity.

joicygiore Follow with 0xnija, 0xspryon and 12 others

Originally disclosed on Code4rena

Submitted by joicygiore, also found by 0xnija, 0xspryon, 41rR4z0r, Agontuk, anonymousjoe, aster, axelot, Race, random1106, slvDev, SoarinSkySagar, theboiledcorn, Tupaia, and Vinay

OraclePack.sol L64-L65

Finding description and impact

When the OraclePackLibrary::rebaseOraclePack() function is called, the UPPER_118BITS_MASK mask is intended to clear the lower 118 bits of the referenceTick and residuals in OraclePack. However, the design of the mask is incorrect. The mask should only clear specific portions of the referenceTick and residuals, but due to the incorrect mask length, it inadvertently clears the EMAs and lockMode data as well.

This error results in the accidental overwriting of the EMAs and lockMode fields, thereby compromising the integrity of the OraclePack data.

    uint256 internal constant UPPER_118BITS_MASK =
        0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC0000000000000000000000000000000;

    function rebaseOraclePack(
        OraclePack oraclePack
    ) internal pure returns (int24 _newReferenceTick, OraclePack rebasedOraclePack) {
        unchecked {
            int24 _referenceTick = oraclePack.referenceTick();

            _newReferenceTick = getMedianTick(oraclePack);
            int24 deltaOffset = _newReferenceTick - _referenceTick;

            uint256 _newResiduals;
            for (uint8 i; i < 8; ++i) {
                int24 _residual = oraclePack.residualTick(i);
                int24 newEntry = _residual - deltaOffset;
                _newResiduals += (uint256(uint16(uint24(newEntry) & 0x0FFF)) & 0x0FFF) << (i * 12);
            }

@>            rebasedOraclePack = OraclePack.wrap(
                (OraclePack.unwrap(oraclePack) & UPPER_118BITS_MASK) +
                    (uint256(uint24(_newReferenceTick) & BITMASK_UINT22) << 96) +
                    uint96(_newResiduals)
            );
        }
    }

The portion F is only 128 bits long, and when combined with C (2 bits), it results in a total of 130 bits. During the & calculation, this leads to the unintended clearing of the lower bits of EMAs and lockMode in the original data.

0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC0000000000000000000000000000000

epoch(24) | orderMap(24) | EMAs(88) | lockMode(2)  = 138 bits

Recommended mitigation steps

Increase the length of the UPPER_118BITS_MASK to 138 bits, ensuring that the EMAs and lockMode fields are not inadvertently cleared.

Proof of Concept

Please add the following test content to a new file under test/foundry/core/ (e.g., test/foundry/core/poc_rebaseOraclePack.t.sol) and execute it:

View detailed Proof of Concept

Output:

Ran 1 test for test/foundry/core/poc_rebaseOraclePack.t.sol:Poc
[PASS] test_poc_rebaseOraclePack() (gas: 2713130)
Suite result: ok. 1 passed; 0 failed; 0 skipped; finished in 34.70s (420.68ms CPU time)

Panoptic mitigated:

Correct UPPER_138BITS_MASK to preserve lockMode and EMAs.

Status: Mitigation confirmed. Full details in reports from Valves, edoscoba, and Nyx.


The original report as published on Code4rena, by joicygiore. 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 UPPER_118BITS_MASK constant is only 128 bits long instead of the required 138 bits, so the bitwise AND clears bits belonging to the EMAs and lockMode fields.

Impact

The EMAs and lockMode fields in OraclePack are accidentally overwritten, compromising the integrity of the OraclePack data.

What the attacker needed

RebaseOraclePack is called

The fix

The mask was corrected to a 138-bit mask so that lockMode and EMAs are preserved.

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 mask is a pattern of bits used to keep or clear parts of a number. Here the mask was meant to clear only the lower parts of a packed data structure, but it was too short and also wiped out two other stored values, the EMAs and lockMode, so the contract's saved data became wrong.

What is Smart contracts?

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

More by joicygiore

View all

11 more disclosed reports, best first.

More from Panoptic: Next Core

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.