Oracle may be attacked if an attacker can pump the tokens for the entire block
The price oracle's current function can be manipulated because its time-weighted average price (TWAP) window can be as short as one block. An attacker with enough funds could pump a token's price for a single block and cause the oracle to report an inflated price.
Submitted by Chom
https://github.com/Plex-Engineer/stableswap/blob/489d010eb99a0885139b2d5ed5a2d826838cc5f9/contracts/BaseV1-core.sol#L190-L201 https://github.com/Plex-Engineer/stableswap/blob/489d010eb99a0885139b2d5ed5a2d826838cc5f9/contracts/BaseV1-core.sol#L154-L171
Attacker may use huge amount of their fund to pump the token in a liquidity pair for one entire block. The oracle will capture the manipulated price as current TWAP implementation may only cover 1 block if timed correctly. (First block on every periodSize = 1800 minutes)
This is possible and similar to Inverse finance April attack: https://www.coindesk.com/tech/2022/04/02/defi-lender-inverse-finance-exploited-for-156-million/
Proof of Concept
Assume currently is the first block after periodSize = 1800 minutes
function _update(uint balance0, uint balance1, uint _reserve0, uint _reserve1) internal {
uint blockTimestamp = block.timestamp;
uint timeElapsed = blockTimestamp - blockTimestampLast; // overflow is desired
if (timeElapsed > 0 && _reserve0 != 0 && _reserve1 != 0) {
reserve0CumulativeLast += _reserve0 * timeElapsed;
reserve1CumulativeLast += _reserve1 * timeElapsed;
}
Observation memory _point = lastObservation();
timeElapsed = blockTimestamp - _point.timestamp; // compare the last observation with current timestamp, if greater than 30 minutes, record a new event
if (timeElapsed > periodSize) {
observations.push(Observation(blockTimestamp, reserve0CumulativeLast, reserve1CumulativeLast));
}
reserve0 = balance0;
reserve1 = balance1;
blockTimestampLast = blockTimestamp;
emit Sync(reserve0, reserve1);
}
timeElapsed > periodSize (Just greater as periodSize is just passed) -> add current cumulative reserve to observations list
Now, let pump the token. And use some technique that inverse finance hacker have used to hold that price for 1 block.
function current(address tokenIn, uint amountIn) external view returns (uint amountOut) {
Observation memory _observation = lastObservation();
(uint reserve0Cumulative, uint reserve1Cumulative,) = currentCumulativePrices();
if (block.timestamp == _observation.timestamp) {
_observation = observations[observations.length-2];
}
uint timeElapsed = block.timestamp - _observation.timestamp;
uint _reserve0 = (reserve0Cumulative - _observation.reserve0Cumulative) / timeElapsed;
uint _reserve1 = (reserve1Cumulative - _observation.reserve1Cumulative) / timeElapsed;
amountOut = _getAmountOut(amountIn, tokenIn, _reserve0, _reserve1);
}
1 Block passed
reserve0Cumulative or reserve1Cumulative may now be skyrocketed due to current token pumping.
timeElapsed = block.timestamp - _observation.timestamp is less than 20 seconds (= 1 block) since _observation.timestamp has just stamped in the previous block.
As timeElapsed is less than 20 seconds (= 1 block) this mean _reserve0 and _reserve1 are just a TWAP of less than 20 seconds (= 1 block) which is easily manipulated by Inverse finance pumping attack technique.
As reserve0Cumulative or reserve1Cumulative is skyrocketed in the 1 block timeframe that is being used in TWAP, _reserve0 or _reserve1 also skyrocketed.
As a conclusion, price oracle may be attacked if attacker can pump price for 1 block since TWAP just cover 1 block.
Recommended Mitigation Steps
You should calculate TWAP average of _reserve0 and _reserve1 in the _update function by using cumulative reserve difference from last update to now which has a duration of periodSize = 1800 minutes.
And when querying for current price you can just return _reserve0 and _reserve1.
Refer to official uniswap v2 TWAP oracle example: https://github.com/Uniswap/v2-periphery/blob/master/contracts/examples/ExampleOracleSimple.sol
Alex the Entreprenerd (judge) decreased severity to Medium and commented:
The warden has shown how, the
currentpricing mechanism can be easily manipulated due to an excessively small observation window.Because the finding shows a property of the system, I believe it to be valid.
However, the loss of funds is contingent on someone foolish enough to use that code for their pricing.
Because of that, I believe Medium Severity to be more appropriate.
To confirm: Do not use
currentfor pricing an asset, you will get rekt.
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 TWAP observation window can be as short as one block because the current function uses the time since the last observation, which may be less than 20 seconds, instead of a fixed longer period.
Impact
The oracle could report a manipulated price, potentially leading to loss of funds for any contract that uses it for pricing, though the judge noted this requires someone to actually use the vulnerable function.
What the attacker needed
Attacker has large funds to pump token; timing at first block after periodSize; contract uses current for pricing
The fix
The TWAP should be calculated over the full periodSize (1800 minutes) using cumulative reserve differences, and the current price should simply return those averaged reserves.
How it was found
- Wait until the first block after the periodSize (1800 minutes) has passed, so a new observation is recorded.
- Pump the token price in the liquidity pair and use a technique to hold that price for one block.
- Call the current function, which computes a TWAP over the short time since the last observation, now reflecting the pumped price.
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 oracle is a service that tells a smart contract what an asset is worth. This oracle calculates an average price over a very short time window, sometimes just one block. If someone can temporarily push the price up for that one block, the oracle will believe the fake high price and other contracts may act on it.