Skip to content
Business logic Audit finding Intermediate

PythOracle:if price.expo is less than 0, wrong prices will be recorded

The PythOracle contract's _recordPrice function mishandles prices with negative exponents, recording them as positive exponents instead. This causes a massive deviation in recorded prices, potentially leading to incorrect payoffs.

Emmanuel Follow with minhtrng and panprog

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-07-perennial-judging/issues/56

Found by

Emmanuel, minhtrng, panprog In PythOracle#_recordPrice function, prices with negative exponents are not handled correctly, leading to a massive deviation in prices.

Vulnerability Detail

Here is PythOracle#_recordPrice function:

Solidity
    function _recordPrice(uint256 oracleVersion, PythStructs.Price memory price) private {
        _prices[oracleVersion] = Fixed6Lib.from(price.price).mul(
            Fixed6Lib.from(SafeCast.toInt256(10 ** SafeCast.toUint256(price.expo > 0 ? price.expo : -price.expo)))
        );
        _publishTimes[oracleVersion] = price.publishTime;
    }

If price is 5e-5 for example, it will be recorded as 5e5 If price is 5e-6, it will be recorded as 5e6.

As we can see, there is a massive deviation in recorded price from actual price whenever price's exponent is negative

Impact

Wrong prices will be recorded. For example, If priceA is 5e-5, and priceB is 5e-6. But due to the wrong conversion,

  • There is a massive change in price(5e5 against 5e-5)
  • we know that priceA is ten times larger than priceB, but priceA will be recorded as ten times smaller than priceB. Unfortunately, current payoff functions may not be able to take care of these discrepancies

Code Snippet

https://github.com/sherlock-audit/2023-07-perennial/blob/main/perennial-v2/packages/perennial-oracle/contracts/pyth/PythOracle.sol#L203

Tool used

Manual Review

Recommendation

In PythOracle.sol, _prices mapping should not be mapping(uint256 => Fixed6) private _prices; Instead, it should be mapping(uint256 => Price) private _prices;, where Price is a struct that stores the price and expo:

Solidity
struct Price{
    Fixed6 price,
    int256 expo
}

This way, the price exponents will be preserved, and can be used to scale the prices correctly wherever it is used.

Discussion

sherlock-admin

1 comment(s) were left on this issue during the judging contest.

141345 commented:

h

arjun-io

Fixed: https://github.com/equilibria-xyz/perennial-v2/pull/53

The original report as published on Sherlock, by Emmanuel. 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 code takes the absolute value of the exponent and always multiplies by a positive power of ten, ignoring the sign of the exponent.

Impact

Wrong prices are recorded, which could lead to incorrect payoffs and financial discrepancies.

What the attacker needed

None

The fix

The _prices mapping should store both the price and its exponent, so that the exponent can be used to scale the price correctly.

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 price feed gives numbers like 5e-5, which means 0.00005. The code mistakenly treats the minus sign in the exponent as a plus, so it records 500000 instead. This makes the price completely wrong, which could cause financial losses.

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 Emmanuel

View all

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