Submitted by Bauchibred, also found by grearlake (1, 2), Giorgio, and kodyvim
Take a look here.
This function is used to calculate the reference pool price. It uses either the latest slot price or TWAP based on twapSeconds.
Now note that unlike the original uniswap implementation, here the delta of the tick cumulative is being calculated in a different manner, i.e protocol implements (tickCumulatives[0] - tickCumulatives[1] instead of tickCumulatives[1] - (tickCumulatives[0] which is because here, secondsAgos[0] = 0; and  secondsAgos[1] = twapSeconds;; unlike in Uniswap OracleLibrary where secondsAgos[0] = secondsAgo; and secondsAgos[1] = 0;, so everything checks out and the tick deltas are calculated accurately, i.e in our case tickCumulativesDelta = tickCumulatives[0] - tickCumulatives[1].
The problem now is that in the case if our tickCumulativesDelta is negative, i.e int24(tickCumulatives[0] - tickCumulatives[1] < 0) , then the tick should be rounded down, as its done in the uniswap library.
But this is not being done and as a result, in the case if int24(tickCumulatives[0] - tickCumulatives[1]) is negative and (tickCumulatives[0] - tickCumulatives[1]) % secondsAgo != 0, then the returned tick will be bigger then it should be; which opens possibility for some price manipulations and arbitrage opportunities.
Note that for the second/third case, the call route to get to _getReferencePoolPriceX96() is: "_checkLoanIsHealthy() -> getValue() -> _getReferenceTokenPriceX96 -> _getTWAPPriceX96 -> _getReferencePoolPriceX96() " as can be seen here.
Add this line: if (tickCumulatives[0] - tickCumulatives[1] < 0 && (tickCumulatives[0] - tickCumulatives[1]) % secondsAgo != 0) timeWeightedTick --;.
Uniswap
kalinbas (Revert) confirmed
Revert mitigated:
Status: Mitigation confirmed. Full details in reports from thank_you, b0g0 and ktg.
