Skip to content
Business logic Audit finding Advanced

Broken TWAP validation allows spot-price manipulation and renders slippage checks ineffective

The validatePrice() function in UniswapPriceOracle does not compute a real time-weighted average price (TWAP) and instead always returns the current trade price, making the slippage check ineffective. An attacker could manipulate the Uniswap V2 pool price within a single block and have buyback or liquidity removal transactions execute at the manipulated price, profiting at the protocol's expense.

BenRai Follow with 0x_DyDx, 0xastronatey and 48 others

Originally disclosed on Code4rena

Submitted by BenRai, also found by 0x_DyDx, 0xastronatey, 0xbrett8571, 0xDemon, 0xFBI, 0xkujen, 0xshdax, 0xVrka, 4nibhal, 7Tecra7, Aamir, ACai, AcsonBarbosa, AgentPulse, aman234, AnonOpcode, Auditor3, ayazwx, bhatmuneeb, Biqua, BlueSheep, bun, catower917, ChainSentry, chuvak, codecoffeeguy, coinsspor, count-sum, Cryptor, DARKNAVY, dexter, Dillon, dman, DQH1, Egbe, emerald7017, Funen, fx, Gakarot, gaving101, golem0, grearlake, happykilling, harry, hodlturk, Huang, humaira45, ibrahimatix0x01, ix2b100, jayx, jerry0422, jkk812812, jo13, JoshuaM33, JuggerNaut63, justingoro, K42, kind0dev, KKKKK, kmkm, konstantinos193, Konzy, KuwaTakushi, Lamsy, Maro0o0o, maxcrate, Morraez, mrMorningstar, MRSHEEP, Nakamoto, Ni9htNi9ht, nisedo, Noahdgoat, Nyx, odeili, OuttakeClaw, PlayboiEvokid, prajithnair98, recr4sh, Reey, richa, Rifter, rzizah, saeedfesal10, sahuang, sanbir, santipu_, shalevhvs, Silvermist, sruibm, suhyeon, taronsung, tedre, thedream, tradingview, uwezkhan06, UzeyirCh, valarislife, Valves, Valves, Varun_05, Viridis, vitaliishuba, white-fox02, Wojack, wonderofme, y4y, Zbugs, zeroDayHunter, zpan, and zzebra83

autonolas-tokenomics/../contracts/oracles/UniswapPriceOracle.sol #L55-L91

The validatePrice() function does not compute a real Uniswap V2 TWAP and instead collapses to the current spot price, making the slippage check ineffective and trivially manipulable within a single block.

Finding Description

The validatePrice() function is meant to validate the trade price against a time-weighted average price (TWAP) from a Uniswap V2 pair, enforcing a maximum allowed slippage. Correct TWAP-based validation is a core security mechanism against flash loans, sandwich attacks, and short-term price manipulation by averaging prices over time.

Uniswap V2 exposes cumulative price variables (price0CumulativeLast and price1CumulativeLast) that grow monotonically over time, encoding the reserve ratio in UQ112x112 format multiplied by elapsed time. To compute a TWAP, a consumer must:

  1. Snapshot cumulative prices and timestamps at time t0
  2. Read cumulative prices again at time t1
  3. Compute the average price as the difference divided by elapsed time

validatePrice() deviates from this design:

  • It reads the latest cumulative price from the pair
  • Reads blockTimestampLast from getReserves()
  • Builds an artificial “cumulative price” using the current tradePrice
  • Derives a “TWAP” from these values
  • Compares that “TWAP” to the current trade price to enforce slippage

The core bug is in the construction and use of cumulative prices:

uint256 cumulativePrice = cumulativePriceLast + (tradePrice * elapsedTime);

uint256 timeWeightedAverage = (cumulativePrice - cumulativePriceLast) / elapsedTime;

This simplifies to

timeWeightedAverage =
(cumulativePriceLast + (tradePrice * elapsedTime) - cumulativePriceLast) / elapsedTime =
(tradePrice * elapsedTime)/ elapsedTime =
tradePrice

The computed TWAP is always equal to the current tradePrice, regardless of history, elapsed time, or pool liquidity. The slippage check is effectively nullified because the deviation is always zero (aside from rounding).

Further issues:

  • tradePrice comes from getPrice() and is not guaranteed to reflect Uniswap reserves.
  • The Uniswap V2 cumulative prices are UQ112x112, but the function uses raw uint256 math without proper fixed-point handling.
  • blockTimestampLast reflects the last reserve update, not a consumer-controlled oracle observation.
  • The function is view and uses no persisted state, so it is fully exploitable via flash loans or same-block reserve manipulation.

UniswapPriceOracle is used on chains with Uniswap V2 pools (e.g. Ethereum mainnet). validatePrice() is the gate for all OLAS swap and liquidity-exit flows that use V2 oracles.

1. BuyBackBurner._buyOLAS() (V2 path)

  • Called from buyBack(secondToken, secondTokenAmount) when using V2 pools.

  • Flow: require(IOracle(poolOracle).validatePrice(maxSlippage), “Before swap slippage limit is breached”) → getPrice() → _performSwap() → getPrice() again → post-swap bounds check.

  • Anyone can call buyBack() after secondTokens (e.g. WETH) are deposited into the contract. The protocol swaps treasury/user deposits for OLAS and sends it to bridge2Burner for burning or bridging.

  • Because validatePrice() always passes (TWAP ≈ spot), the pre-swap check is ineffective. An attacker can flash-loan, manipulate the pool, and have the buyback execute at the manipulated price. The protocol receives less OLAS per unit of ETH, and the attacker can unwind to capture arbitrage profit.

    2. LiquidityManagerETH.*checkTokensAndRemoveLiquidityV2() and LiquidityManagerOptimism.*checkTokensAndRemoveLiquidityV2()

  • Called from convertToV3() when migrating V2 LP to V3.

  • Flow: if (!IOracle(oracleV2).validatePrice(maxSlippage / 100)) revert SlippageLimitBreached() → remove V2 liquidity → mint V3.

  • Only the owner can call convertToV3(). The oracle is used to ensure the V2 exit happens at a fair price before migrating to V3.

  • Because validatePrice() always passes, the protocol can exit V2 liquidity at a manipulated price. The resulting token amounts (OLAS and quote) are worse than the true TWAP would imply, and the attacker can profit from the price deviation when the protocol swaps or migrates.

Attacker scenario

  1. Observe a buyBack() or convertToV3() transaction in the mempool.
  2. Swap to move the Uniswap V2 pool price (e.g. large swap in either direction).
  3. Execute the victim transaction in the same block.
  4. validatePrice() passes because TWAP ≈ manipulated spot.
  5. Swap or liquidity removal executes at the manipulated price.
  6. Attacker reverses the pool manipulation in the same block.
  7. Attacker pockets the arbitrage; the protocol or users take the loss.

The attack is cheap (flash loan + gas), permissionless, and repeatable. The same logic applies to any chain where UniswapPriceOracle is used for OLAS swaps or liquidity exits.

Recommendation

Implement a proper Uniswap V2 TWAP oracle pattern by persisting cumulative price snapshots across blocks and computing averages from differences between observations:

  • Store price{0,1}CumulativeLast and timestampLast in contract storage
  • Update them in a state-changing function (e.g. updateOracle())
  • Compute TWAPs using cumulative price deltas
  • Correctly handle UQ112x112 fixed-point math

Slippage should be evaluated only after a correct TWAP is computed. This restores the intended oracle guarantees and protects against same-block and flash-loan-based price manipulation.


The original report as published on Code4rena, by BenRai. 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

Likely the function builds an artificial cumulative price using the current trade price and then subtracts the same cumulative price, so the TWAP simplifies to the spot price.

Impact

An attacker could flash-loan, manipulate the pool, and have buybacks or V2 liquidity exits execute at the manipulated price, causing the protocol to receive less OLAS per unit of ETH or worse token amounts, while the attacker captures arbitrage profit.

What the attacker needed

Uniswap V2 pool with OLAS; Attacker can flash-loan; Victim transaction in mempool

The fix

The oracle should persist cumulative price snapshots across blocks and compute TWAPs from differences between observations, with correct UQ112x112 fixed-point handling.

How it was found

  1. Observe a buyBack() or convertToV3() transaction in the mempool.
  2. Swap to move the Uniswap V2 pool price.
  3. Execute the victim transaction in the same block.
  4. validatePrice() passes because TWAP ≈ manipulated spot.
  5. Swap or liquidity removal executes at the manipulated price.
  6. Attacker reverses the pool manipulation in the same block.

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 TWAP is a way to average a price over time so that a single large trade cannot easily distort it. This code tried to check the trade price against a TWAP, but a maths mistake meant the 'average' it calculated was always just the current price. So the check did nothing, and someone could move the price with a flash loan and trade against the protocol at that fake price.

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 BenRai

View all

13 more disclosed reports, best first.

More from Olas

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.