UniswapV2PriceOracle.sol currentCumulativePrices() will revert when priceCumulative addition overflow
The oracle contract calls a Uniswap library function that relies on arithmetic overflow, but the project compiles it with Solidity 0.8 or later, where overflow reverts. When the cumulative price values grow large enough, the call reverts and breaks the oracle and anything depending on it.
Submitted by WatchPug
(uint price0Cumulative, uint price1Cumulative, uint32 blockTimestamp) = address(pair).currentCumulativePrices();
Because the Solidity version used by the current implementation of UniswapV2OracleLibrary.sol is >=0.8.7, and there are some breaking changes in Solidity v0.8.0:
Arithmetic operations revert on underflow and overflow.
Ref: https://docs.soliditylang.org/en/v0.8.13/080-breaking-changes.html#silent-changes-of-the-semantics
While in UniswapV2OracleLibrary.sol, subtraction overflow is desired at blockTimestamp - blockTimestampLast in currentCumulativePrices():
if (blockTimestampLast != blockTimestamp) {
// subtraction overflow is desired
uint32 timeElapsed = blockTimestamp - blockTimestampLast;
// addition overflow is desired
// counterfactual
price0Cumulative += uint(FixedPoint.fraction(reserve1, reserve0)._x) * timeElapsed;
// counterfactual
price1Cumulative += uint(FixedPoint.fraction(reserve0, reserve1)._x) * timeElapsed;
}
In another word, Uniswap/v2-periphery/contracts/libraries/UniswapV2OracleLibrary only works at solidity < 0.8.0.
As a result, when price0Cumulative or price1Cumulative is big enough, currentCumulativePrices will revert due to overflow.
Impact
Since the overflow is desired in the original version, and it’s broken because of using Solidity version >0.8. The UniswapV2PriceOracle contract will break when the desired overflow happens, and further breaks other parts of the system that relies on UniswapV2PriceOracle.
Recommended Mitigation Steps
Note: this recommended fix requires a fork of the library contract provided by Uniswap.
Change to:
if (blockTimestampLast != blockTimestamp) {
unchecked {
// subtraction overflow is desired
uint32 timeElapsed = blockTimestamp - blockTimestampLast;
// addition overflow is desired
// counterfactual
price0Cumulative += uint(FixedPoint.fraction(reserve1, reserve0)._x) * timeElapsed;
// counterfactual
price1Cumulative += uint(FixedPoint.fraction(reserve0, reserve1)._x) * timeElapsed;
}
}
jn-lp (Phuture Finance) confirmed and resolved
Context
Phuture's UniswapV2PriceOracle.sol is a contract that reads cumulative price data from a Uniswap V2 pair. The oracle calls the pair's currentCumulativePrices() function, which is implemented in Uniswap's UniswapV2OracleLibrary.sol, to obtain price0Cumulative, price1Cumulative and blockTimestamp. Those cumulative values are the basis of a time-weighted average price (TWAP): the difference between two cumulative readings divided by the elapsed time gives the average price over that window.
The bug is not in Phuture's own arithmetic. It is in the interaction between Uniswap's library and the Solidity compiler version Phuture compiled with. The library was written for Solidity versions before 0.8.0, where integer arithmetic wraps silently. Phuture's build uses a compiler version at or above 0.8.7, where arithmetic reverts on overflow and underflow. The overflow that the library relies on therefore becomes a revert.
Setup
The relevant code is at UniswapV2PriceOracle.sol line 62:
(uint price0Cumulative, uint price1Cumulative, uint32 blockTimestamp) = address(pair).currentCumulativePrices();
This is a call into the Uniswap V2 pair contract, which in turn uses UniswapV2OracleLibrary.sol. The report states that the Solidity version used by the current implementation of UniswapV2OracleLibrary.sol is >=0.8.7. No other deployment details, network, or tooling are given.
Finding it
The researcher started from the Solidity 0.8.0 breaking change documented in the compiler release notes:
Arithmetic operations revert on underflow and overflow.
That change is the opposite of what Uniswap's oracle library expects. In UniswapV2OracleLibrary.sol, the subtraction and the additions in currentCumulativePrices() are deliberately allowed to wrap. The library source, as quoted in the report, is:
if (blockTimestampLast != blockTimestamp) {
// subtraction overflow is desired
uint32 timeElapsed = blockTimestamp - blockTimestampLast;
// addition overflow is desired
// counterfactual
price0Cumulative += uint(FixedPoint.fraction(reserve1, reserve0)._x) * timeElapsed;
// counterfactual
price1Cumulative += uint(FixedPoint.fraction(reserve0, reserve1)._x) * timeElapsed;
}
The comments in the code say it plainly: subtraction overflow is desired, and addition overflow is desired. The cumulative prices are designed to wrap around, and consumers are expected to compute differences that also wrap. Under a compiler that reverts on overflow, that design no longer works. The researcher's conclusion is direct: Uniswap/v2-periphery/contracts/libraries/UniswapV2OracleLibrary only works at Solidity below 0.8.0.
Exploitation
There is no attacker-supplied request or payload in this report. The failure is triggered by the normal operation of the oracle over time, so the steps below describe the conditions rather than an attack sequence.
Step 1: the oracle calls the pair
UniswapV2PriceOracle.sol calls currentCumulativePrices() on the Uniswap V2 pair, as shown in the code block above. The call returns the two cumulative prices and the current block timestamp.
Step 2: the library updates the cumulative values
Inside currentCumulativePrices(), if the block timestamp differs from the last stored timestamp, the library computes timeElapsed and adds the current price, scaled by elapsed time, to each cumulative value. Both the subtraction and the additions are expected to overflow and wrap.
Step 3: the cumulative value grows past the type's range
Each call adds a positive amount to price0Cumulative and price1Cumulative. Over enough time and activity, one of these values becomes large enough that the addition would exceed the range of uint256.
Step 4: the addition reverts
Because the contract is compiled with Solidity >=0.8.7, the addition does not wrap. It reverts. The report states the outcome plainly: when price0Cumulative or price1Cumulative is big enough, currentCumulativePrices will revert due to overflow.
Root cause
Uniswap's UniswapV2OracleLibrary.sol was written for compilers before 0.8.0, where overflow and underflow wrap silently. Its cumulative price scheme depends on that behaviour: the comments mark both the subtraction and the additions as intentionally overflowing. Phuture compiled against a compiler at or above 0.8.7, where the same operations revert. Nothing in the code adapts the library to the new semantics, so the intended wrap becomes a hard failure. The existing defence, the compiler's checked arithmetic, is exactly what breaks the design.
Impact
The report describes the impact in terms of availability. The UniswapV2PriceOracle contract will break when the desired overflow happens, and that break propagates to other parts of the system that rely on UniswapV2PriceOracle. The condition is that a cumulative price has grown large enough for the addition to overflow. The report does not quantify how long that takes or which downstream components are affected.
Fix
The recommended mitigation is to wrap the arithmetic in an unchecked block, and the report notes that this requires a fork of the library contract provided by Uniswap. The proposed code is:
if (blockTimestampLast != blockTimestamp) {
unchecked {
// subtraction overflow is desired
uint32 timeElapsed = blockTimestamp - blockTimestampLast;
// addition overflow is desired
// counterfactual
price0Cumulative += uint(FixedPoint.fraction(reserve1, reserve0)._x) * timeElapsed;
// counterfactual
price1Cumulative += uint(FixedPoint.fraction(reserve0, reserve1)._x) * timeElapsed;
}
}
The unchecked block restores the wrapping behaviour the library was designed around, so the intended overflow no longer reverts. The finding was confirmed and resolved by jn-lp (Phuture Finance).
Variants to test
- Audit every vendored Uniswap library for comments such as "overflow is desired" or "underflow is desired", then check the pragma of the contract that imports it. A positive result is a library written for pre-0.8.0 semantics compiled into a contract using 0.8.x without
unchecked. - Look for cumulative or accumulator patterns (TWAP oracles, reward-per-token accounting, fee growth counters) that are meant to wrap. A positive result is an addition or subtraction on a value that grows monotonically and is expected to wrap, placed outside an
uncheckedblock. - Check timestamp arithmetic of the form
block.timestamp - lastTimestampwherelastTimestampcan exceed the current timestamp, for example after a manipulation or a reset. A positive result is a revert path reachable by an external caller. - Compare the pragma range of each dependency against the pragma of the importing contract. A positive result is a dependency whose stated range excludes the compiler version actually used in the build.
Hunting notes
This class of bug hides in copied or vendored code: libraries lifted from Uniswap, Compound or similar projects that were written before Solidity 0.8.0 and then dropped into a newer codebase. During recon, look at the repository's dependency tree and at any in-tree copies of well-known libraries, and compare their pragmas with the project's own. Comments that explicitly call out desired overflow are the strongest signal.
What happened, in plain words
Phuture is a project that runs smart contracts on a blockchain. A smart contract is a small program that lives on the blockchain and runs automatically when someone calls it. One of Phuture's contracts, UniswapV2PriceOracle.sol, is a price oracle: a piece of software whose job is to report the price of something. It does not work out the price from scratch. Instead it borrows price data from Uniswap, a well-known decentralised exchange, and turns that data into an average price over a period of time.
To get the raw data, the Phuture contract calls a function called currentCumulativePrices() that lives in a Uniswap helper library, UniswapV2OracleLibrary.sol. A library is reusable code that other programs can call, a bit like a shared toolbox. The function returns two running totals, price0Cumulative and price1Cumulative, plus the current block timestamp. A timestamp is just the time, recorded as a number. The word "cumulative" means "added up so far": each call adds the current price, multiplied by the time that has passed, onto the running total. To get an average price over a window, you take two of these running totals at different moments and subtract one from the other, then divide by the time between them. That average is called a time-weighted average price, or TWAP.
The bug is not in Phuture's own arithmetic. It is a clash between two things: the way Uniswap's library was written, and the version of the Solidity compiler that Phuture used to build its contract. Solidity is the programming language smart contracts are written in, and the compiler is the program that turns that source code into something the blockchain can run. Older Solidity compilers let numbers silently wrap around when they got too big, the way an old car odometer rolls from 999999 back to 000000. Uniswap's library was written for those older compilers and actually depends on that wrapping. Phuture compiled with a newer compiler, version 0.8.7 or above, where the same situation does not wrap: it stops with an error. So a moment that the library was designed to handle quietly becomes a hard failure.
The setup
The report points at one line of Phuture's contract, UniswapV2PriceOracle.sol line 62:
(uint price0Cumulative, uint price1Cumulative, uint32 blockTimestamp) = address(pair).currentCumulativePrices();
Line by line, in plain words:
(uint price0Cumulative, uint price1Cumulative, uint32 blockTimestamp)declares three variables to hold the answer.uintmeans an unsigned integer, a whole number that cannot be negative.uint32is a smaller integer that takes up 32 bits, which is enough to hold a timestamp.=means "store the result of the right-hand side in these three variables".address(pair)treats the variablepairas a blockchain address, the unique identifier of another contract.pairhere is a Uniswap V2 pair contract, the contract that holds two tokens and tracks their exchange rate..currentCumulativePrices()calls that function on the pair contract. The pair contract in turn usesUniswapV2OracleLibrary.solto do the work.
The report states that the Solidity version used by the current implementation of UniswapV2OracleLibrary.sol is >=0.8.7. That is, the library is being compiled with a compiler at or above version 0.8.7. No other deployment details, network or tooling are given.
Finding it
The researcher started from a documented change in the Solidity compiler. When Solidity reached version 0.8.0, it changed how arithmetic behaves, and the release notes say:
Arithmetic operations revert on underflow and overflow.
Two words need unpacking. Overflow is what happens when a calculation produces a number too large for the space reserved for it, so the value spills over. Underflow is the same idea at the other end, when a subtraction would go below zero for a number that cannot be negative. Revert means the transaction is cancelled and any changes it made are undone, as if it never happened. Before 0.8.0, these situations wrapped around silently. From 0.8.0 onwards, they stop the transaction.
That change is the opposite of what Uniswap's oracle library expects. In UniswapV2OracleLibrary.sol, the subtraction and the additions inside currentCumulativePrices() are deliberately allowed to wrap. The library source, as quoted in the report, is:
if (blockTimestampLast != blockTimestamp) {
// subtraction overflow is desired
uint32 timeElapsed = blockTimestamp - blockTimestampLast;
// addition overflow is desired
// counterfactual
price0Cumulative += uint(FixedPoint.fraction(reserve1, reserve0)._x) * timeElapsed;
// counterfactual
price1Cumulative += uint(FixedPoint.fraction(reserve0, reserve1)._x) * timeElapsed;
}
Line by line:
if (blockTimestampLast != blockTimestamp) {checks whether the current block timestamp is different from the last one the library stored. If nothing has changed since the last call, there is nothing to add, so the block is skipped.// subtraction overflow is desiredis a comment, ignored by the compiler, telling the reader that the next line is meant to wrap around if it needs to.uint32 timeElapsed = blockTimestamp - blockTimestampLast;works out how much time has passed since the last update and stores it intimeElapsed.// addition overflow is desiredis another comment, this time about the two lines that follow.// counterfactualis a comment explaining that the value being added is a hypothetical price, the price that would apply if the two reserves were in the ratio they are now.price0Cumulative += uint(FixedPoint.fraction(reserve1, reserve0)._x) * timeElapsed;adds the current price of token 0, scaled by the elapsed time, onto the running total for token 0.+=means "add to the existing value".FixedPoint.fractioncomputes a fraction from the two reserves, and._xtakes the top part of that fraction.price1Cumulative += uint(FixedPoint.fraction(reserve0, reserve1)._x) * timeElapsed;does the same for token 1, with the reserves swapped.
The comments say it plainly: subtraction overflow is desired, and addition overflow is desired. The cumulative prices are designed to wrap around, and anyone reading them is expected to compute differences that also wrap. Under a compiler that reverts on overflow, that design no longer works. The researcher's conclusion is direct: Uniswap/v2-periphery/contracts/libraries/UniswapV2OracleLibrary only works at Solidity below 0.8.0.
How the failure happens
There is no attacker-supplied request or payload in this report. Nobody has to send anything malicious. The failure is triggered by the normal operation of the oracle over time, so the steps below describe the conditions rather than an attack sequence.
Step 1: the oracle calls the pair
UniswapV2PriceOracle.sol calls currentCumulativePrices() on the Uniswap V2 pair, as shown in the code block above. The call returns the two cumulative prices and the current block timestamp. This is the normal, everyday operation of the oracle: it asks the pair for the latest running totals so it can work out an average price.
Step 2: the library updates the cumulative values
Inside currentCumulativePrices(), if the block timestamp differs from the last stored timestamp, the library computes timeElapsed and adds the current price, scaled by elapsed time, to each cumulative value. Both the subtraction and the additions are expected to overflow and wrap. In the old compiler this was harmless: the numbers rolled over like an odometer and the arithmetic carried on.
Step 3: the cumulative value grows past the type's range
Each call adds a positive amount to price0Cumulative and price1Cumulative. The totals only ever go up. Over enough time and activity, one of these values becomes large enough that the addition would exceed the range of uint256, the 256-bit unsigned integer that holds it. Think of a counter with a fixed number of digits: eventually the next increment would need one more digit than there is room for.
Step 4: the addition reverts
Because the contract is compiled with Solidity >=0.8.7, the addition does not wrap. It reverts. The report states the outcome plainly: when price0Cumulative or price1Cumulative is big enough, currentCumulativePrices will revert due to overflow. The report states that when price0Cumulative or price1Cumulative is big enough, currentCumulativePrices will revert due to overflow.
Root cause
Uniswap's UniswapV2OracleLibrary.sol was written for compilers before 0.8.0, where overflow and underflow wrap silently. Its cumulative price scheme depends on that behaviour: the comments mark both the subtraction and the additions as intentionally overflowing. Phuture compiled against a compiler at or above 0.8.7, where the same operations revert. Nothing in the code adapts the library to the new rules, so the intended wrap becomes a hard failure. The existing defence, the compiler's checked arithmetic, is exactly what breaks the design. It is a bit like taking a recipe written for a fan oven and following it in a microwave: each step is correct on its own, but the appliance behaves differently and the result fails.
Impact
The report describes the impact in terms of availability, meaning whether the system keeps working. The UniswapV2PriceOracle contract will break when the desired overflow happens, and that break propagates to other parts of the system that rely on UniswapV2PriceOracle. The condition is that a cumulative price has grown large enough for the addition to overflow. The report does not quantify how long that takes or which downstream components are affected.
Fix
The recommended mitigation is to wrap the arithmetic in an unchecked block, and the report notes that this requires a fork of the library contract provided by Uniswap. A fork is a copy of someone else's code that you maintain yourself, so you can change it. An unchecked block tells the compiler: do not check this arithmetic, let it wrap the old way. The proposed code is:
if (blockTimestampLast != blockTimestamp) {
unchecked {
// subtraction overflow is desired
uint32 timeElapsed = blockTimestamp - blockTimestampLast;
// addition overflow is desired
// counterfactual
price0Cumulative += uint(FixedPoint.fraction(reserve1, reserve0)._x) * timeElapsed;
// counterfactual
price1Cumulative += uint(FixedPoint.fraction(reserve0, reserve1)._x) * timeElapsed;
}
}
Line by line:
if (blockTimestampLast != blockTimestamp) {is unchanged: only update when time has actually moved on.unchecked {opens a block in which the compiler skips its overflow and underflow checks. Everything inside is allowed to wrap.- The four lines inside are the same as before: compute
timeElapsed, then add the scaled price to each cumulative total. The comments still explain that the wrapping is intentional. }closes theuncheckedblock, and the final}closes theif.
The unchecked block restores the wrapping behaviour the library was designed around, so the intended overflow no longer reverts. The finding was confirmed and resolved by jn-lp (Phuture Finance).
Variants to test
These are the general lessons to carry to other codebases.
- Audit every vendored Uniswap library for comments such as "overflow is desired" or "underflow is desired", then check the pragma of the contract that imports it. A pragma is the line at the top of a Solidity file that states which compiler versions are acceptable. A positive result is a library written for pre-0.8.0 semantics compiled into a contract using 0.8.x without
unchecked. - Look for cumulative or accumulator patterns (TWAP oracles, reward-per-token accounting, fee growth counters) that are meant to wrap. A positive result is an addition or subtraction on a value that grows monotonically and is expected to wrap, placed outside an
uncheckedblock. - Check timestamp arithmetic of the form
block.timestamp - lastTimestampwherelastTimestampcan exceed the current timestamp, for example after a manipulation or a reset. A positive result is a revert path reachable by an external caller. - Compare the pragma range of each dependency against the pragma of the importing contract. A positive result is a dependency whose stated range excludes the compiler version actually used in the build.
Hunting notes
This class of bug hides in copied or vendored code: libraries lifted from Uniswap, Compound or similar projects that were written before Solidity 0.8.0 and then dropped into a newer codebase. During recon, meaning the early stage where you map out what you are looking at, look at the repository's dependency tree and at any in-tree copies of well-known libraries, and compare their pragmas with the project's own. Comments that explicitly call out desired overflow are the strongest signal.
Glossary
- Smart contract: a program stored on a blockchain that runs automatically when called.
- Blockchain: a shared, append-only record of transactions maintained by many computers.
- Oracle: software that supplies outside data, such as a price, to a smart contract.
- Uniswap: a decentralised exchange where tokens are swapped directly by smart contracts.
- Uniswap V2 pair: the contract that holds two tokens and tracks their exchange rate.
- Library: reusable code that other programs call, like a shared toolbox.
- Solidity: the programming language used to write smart contracts.
- Compiler: the program that turns source code into something the blockchain can run.
- Pragma: the line at the top of a Solidity file stating which compiler versions are acceptable.
- Cumulative price: a running total of price multiplied by elapsed time, added up since the start.
- TWAP (time-weighted average price): an average price over a window, computed from two cumulative readings.
- Timestamp: the time of an event, recorded as a number.
- Block timestamp: the timestamp of the block in which a transaction is included.
- uint / uint32 / uint256: unsigned integers, whole numbers that cannot be negative; the number is the bit width.
- Overflow: a calculation producing a number too large for its storage, so it spills over.
- Underflow: a calculation going below the smallest value a type can hold.
- Wrap around: rolling over from the maximum value back to the minimum, like an odometer.
- Revert: cancelling a transaction and undoing its changes.
- Unchecked block: a Solidity block in which overflow and underflow checks are skipped.
- Fork: a copy of someone else's code that you maintain and change yourself.
- Vendored code: third-party code copied into your own repository rather than installed as a dependency.
- Dependency tree: the list of external packages a project relies on, and their own dependencies.
- Recon: the early mapping stage of a security review.
- Availability: whether a system keeps working and responding.
- Payload: the data sent in a request; this report contains none.
- Reserves: the amounts of each token held by a Uniswap pair.
- Fixed point: a way of representing fractions using whole numbers.