Underlying delta is calculated on internal token balance
The rebalance function calculates the underlying delta using internal token precision (1e8) and compares it to a fixed threshold, which is inconsistent for tokens with different decimals. This could cause over-sensitive reverts for tokens with fewer decimals and under-sensitive checks for tokens with more decimals.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-03-notional-judging/issues/205
Found by
xiaoming90 + 0xleastwood
Summary
The underlying delta is calculated on the internal token balance, which might cause inconsistency with tokens of varying decimals.
Vulnerability Detail
File: TreasuryAction.sol
284: function _executeRebalance(uint16 currencyId) private {
285: IPrimeCashHoldingsOracle oracle = PrimeCashExchangeRate.getPrimeCashHoldingsOracle(currencyId);
286: uint8[] memory rebalancingTargets = _getRebalancingTargets(currencyId, oracle.holdings());
287: (RebalancingData memory data) = REBALANCING_STRATEGY.calculateRebalance(oracle, rebalancingTargets);
288:
289: (/* */, uint256 totalUnderlyingValueBefore) = oracle.getTotalUnderlyingValueStateful();
290:
291: // Process redemptions first
292: Token memory underlyingToken = TokenHandler.getUnderlyingToken(currencyId);
293: TokenHandler.executeMoneyMarketRedemptions(underlyingToken, data.redeemData);
294:
295: // Process deposits
296: _executeDeposits(underlyingToken, data.depositData);
297:
298: (/* */, uint256 totalUnderlyingValueAfter) = oracle.getTotalUnderlyingValueStateful();
299:
300: int256 underlyingDelta = totalUnderlyingValueBefore.toInt().sub(totalUnderlyingValueAfter.toInt());
301: require(underlyingDelta.abs() < Constants.REBALANCING_UNDERLYING_DELTA);
302: }
The underlyingDelta is denominated in internal token precision (1e8) and is computed by taking the difference between totalUnderlyingValueBefore and totalUnderlyingValueAfter in Line 300 above.
Next, the underlyingDelta is compared against the Constants.REBALANCING_UNDERLYING_DELTA (10_000=0.0001) to ensure that the rebalance did not exceed the acceptable delta threshold.
However, the same Constants.REBALANCING_UNDERLYING_DELTA is used across all tokens such as ETH, DAI, and USDC. As a result, the delta will not be consistent with tokens of varying decimals.
Impact
Using the internal token precision (1e8) might result in an over-sensitive trigger for tokens with fewer decimals (e.g. 1e6) as they are scaled up and an under-sensitive one for tokens with more decimals (e.g. 1e18) as they are scaled down, leading to inconsistency across different tokens when checking against the Constants.REBALANCING_UNDERLYING_DELTA.
This also means that the over-sensitive one will trigger a revert more easily and vice versa.
Code Snippet
Tool used
Manual Review
Recommendation
Consider using the external token balance and scale Constants.REBALANCING_UNDERLYING_DELTA to the token's decimals.
Discussion
jeffywu
Valid suggestion
jeffywu
Fixed: https://github.com/notional-finance/contracts-v2/pull/118
0xleastwood
@0xleastwood + @xiaoming9090: Verified. Fixed in PR https://github.com/notional-finance/contracts-v2/pull/118. The issue of unnecessary scaling in PR 118 has been resolved in this commit (https://github.com/notional-finance/contracts-v2/commit/a200877c2b1d7c9f4a13b1fd84c8ecd3dae005a9).
Additional Suggestion: Small improvement could be made where the delta is only checked if the change moves in the negative direction. This would protect the protocol a bit more from DoS-style of attacks via donation.
jeffywu
@0xleastwood, the getTotalUnderlyingValueStateful method reads from an internally tracked contract balance so it should not be affected by any donations to the protocol. However, agree that only checking to the downside would be slightly more efficient here.
0xleastwood
Good point, I forgot that was the case.
0xleastwood
@0xleastwood + @xiaoming9090: Verified. Fixed in https://github.com/notional-finance/contracts-v2/pull/118
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 code uses a single constant threshold for the delta check without scaling it to the token's decimals, while the delta itself is denominated in internal precision.
Impact
The inconsistency could cause rebalances to revert more easily for tokens with fewer decimals and less easily for tokens with more decimals, potentially leading to failed operations or unnoticed large deviations.
What the attacker needed
None
The fix
The issue was fixed by using the external token balance and scaling the threshold to the token's decimals.
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.
Smart contracts often store token amounts in a fixed internal precision, like 1e8, regardless of the token's actual decimals. The code checks if the change in value during a rebalance is small enough, but uses the same threshold for all tokens. This means the check is too strict for some tokens and too loose for others, which could cause problems.
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.