Asymmetric calculation of price difference
The contract calculates the price difference between the current price and a verified price using two different formulas depending on whether the current price is above or below the verified price. This makes the allowed deviation asymmetric, so a price above the verified price can deviate more than one below it before being rejected.
t4sk Follow with Bauchibred, lanrebayode77 and 1 other
Originally disclosed on Code4rena
Submitted by t4sk, also found by Bauchibred, lanrebayode77, and hunter_w3b
Price difference is calculated in 2 ways depending on whether price > verified price or not.
If price > verified price, this is the equation:
(price - verified price) / price
Otherwise price is calculated with this equation:
(verified price - price) / verified price
When the 2 equations above are graphed with price = horizontal axis, we get 2 different curves, see here.
The first equation produces a asymptotic curve (shown in red). The second equation produces a linear curve (shown in green). Therefore, the rate at which the price difference changes is different depending on whether price > verified price or not.
Example
Price difference of +1 or -1 from verified price are not symmetric:
# p < v
v = 2
p = 1
d = (v - p) / v
print(d)
# output is 0.5
# p > v
v = 2
p = 3
d = (p - v) / p
print(d)
# output is 0.33333
Tools Used
Desmos graphing calculator and python
Recommended Mitigation Steps
Use a different equation to check price difference (shown in blue here):
|price - verified price| / verified price <= max difference
Assuming verifyPriceX96 > 0:
uint256 diff = priceX96 >= verifyPriceX96
? (priceX96 - verifyPriceX96) * 10000
: (verifyPriceX96 - priceX96) * 10000;
require(diff / verifyPriceX96 <= maxDifferenceX1000)
Assessed type
Math
PR here - fixed calculation.
Status: Mitigation Confirmed. Full details in reports from ktg and b0g0.
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 price difference is normalised by the current price in one branch and by the verified price in the other, instead of using a single symmetric formula.
Impact
The contract may accept prices that deviate more than the intended maximum when the price is above the verified price, potentially allowing manipulation within that wider band.
What the attacker needed
None
The fix
The calculation was fixed by using a single symmetric formula that divides the absolute difference by the verified 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 smart contract checks that a price is close to a trusted 'verified price' before accepting it. The check uses one formula when the price is higher and another when it is lower, so the same size of difference is treated differently. This means the contract is more lenient in one direction, which could let a price move further than intended.
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.