Low precision is used when checking spot price deviation
The spot price deviation check uses low precision, causing rounding down that can allow a deviation close to 2% to pass when the limit is 1%. An attacker could potentially manipulate the spot price or exploit a valuation discrepancy for MEV.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-12-notional-update-5-judging/issues/32
Found by
xiaoming90
Summary
Low precision is used when checking spot price deviation, which might lead to potential manipulation or create the potential for an MEV opportunity due to valuation discrepancy.
Vulnerability Detail
Assume the following:
- The max deviation is set to 1%
nTokenOracleValueis 1,000,000,000nTokenSpotValueis 980,000,001
File: Constants.sol
46: // Basis for percentages
47: int256 internal constant PERCENTAGE_DECIMALS = 100;
File: nTokenCalculations.sol
61: int256 maxValueDeviationPercent = int256(
62: uint256(uint8(nToken.parameters[Constants.MAX_MINT_DEVIATION_LIMIT]))
63: );
64: // Check deviation limit here
65: int256 deviationInPercentage = nTokenOracleValue.sub(nTokenSpotValue).abs()
66: .mul(Constants.PERCENTAGE_DECIMALS).div(nTokenOracleValue);
67: require(deviationInPercentage <= maxValueDeviationPercent, "Over Deviation Limit");
Based on the above formula:
nTokenOracleValue.sub(nTokenSpotValue).abs().mul(Constants.PERCENTAGE_DECIMALS).div(nTokenOracleValue);
((nTokenOracleValue - nTokenSpotValue) * Constants.PERCENTAGE_DECIMALS) / nTokenOracleValue
((1,000,000,000 - 980,000,001) * 100) / 1,000,000,000
(19,999,999 * 100) / 1,000,000,000
1,999,999,900 / 1,000,000,000 = 1.9999999 = 1
The above shows that the oracle and spot values have deviated by 1.99999%, which is close to 2%. However, due to a rounding error, it is rounded down to 1%, and the TX will not revert.
Impact
The purpose of the deviation check is to ensure that the spot market value is not manipulated. If the deviation check is not accurate, it might lead to potential manipulation or create the potential for an MEV opportunity due to valuation discrepancy.
Code Snippet
Tool used
Manual Review
Recommendation
Consider increasing the precision.
For instance, increasing the precision from Constants.PERCENTAGE_DECIMALS (100) to 1e8 would have caught the issue mentioned earlier in the report even after the rounding down.
nTokenOracleValue.sub(nTokenSpotValue).abs().mul(1e8).div(nTokenOracleValue);
((nTokenOracleValue - nTokenSpotValue) * 1e8) / nTokenOracleValue
((1,000,000,000 - 980,000,001) * 1e8) / 1,000,000,000
(19,999,999 * 1e8) / 1,000,000,000 = 1999999.9 = 1999999
1% of 1e8 = 1000000
require(deviationInPercentage <= maxValueDeviationPercent, "Over Deviation Limit")
require(1999999 <= 1000000, "Over Deviation Limit") => Revert
Discussion
sherlock-admin2
1 comment(s) were left on this issue during the judging contest.
takarez commented:
valid because { This is valid}
sherlock-admin
The protocol team fixed this issue in PR/commit https://github.com/notional-finance/contracts-v3/pull/34.
sherlock-admin
The Lead Senior Watson signed-off on the fix.
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 deviation percentage is calculated with only two decimal places of precision, so integer division truncates the result and understates the actual deviation.
Impact
The inaccurate deviation check might allow spot price manipulation or create an MEV opportunity due to valuation discrepancy.
What the attacker needed
Spot price deviates by nearly 2%; max deviation set to 1%
The fix
The precision was increased from 100 to 1e8, and the protocol team fixed this issue.
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 compares two prices to make sure they are not too different. It calculates the difference as a percentage but rounds down, so a difference of almost 2% can be treated as 1% and pass the check. This could let someone take advantage of the price difference.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.