Rebalance might be skipped even if the external lending is unhealthy
The off-target percentage calculation in the external lending health check uses the sum of current and target amounts as the denominator, which underestimates the deviation. This could cause a rebalance to be skipped even when external lending is unhealthy, potentially leading to liquidity issues.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-12-notional-update-5-judging/issues/46
Found by
xiaoming90
Summary
The deviation between the target and current lending amount (offTargetPercentage) will be underestimated due to incorrect calculation. As a result, a rebalancing might be skipped even if the existing external lending is unhealthy.
Vulnerability Detail
The formula used within the _isExternalLendingUnhealthy function below calculating the offTargetPercentage can be simplified as follows for the readability of this issue.
$$ offTargetPercentage = \frac{\mid currentExternalUnderlyingLend - targetAmount \mid}{currentExternalUnderlyingLend + targetAmount} \times 100% $$
Assume that the targetAmount is 100 and currentExternalUnderlyingLend is 90. The off-target percentage will be 5.26%, which is incorrect.
offTargetPercentage = abs(90 - 100) / (100 + 90) = 10 / 190 = 0.0526 = 5.26%
The correct approach is to calculate the off-target percentages as a ratio of the difference to the target:
$$ offTargetPercentage = \frac{\mid currentExternalUnderlyingLend - targetAmount \mid}{targetAmount} \times 100% $$
offTargetPercentage = abs(90 - 100) / (100) = 10 / 100 = 0.0526 = 10%
File: TreasuryAction.sol
405: function _isExternalLendingUnhealthy(
406: uint16 currencyId,
407: IPrimeCashHoldingsOracle oracle,
408: PrimeRate memory pr
409: ) internal view returns (bool isExternalLendingUnhealthy, OracleData memory oracleData, uint256 targetAmount) {
410: oracleData = oracle.getOracleData();
411:
412: RebalancingTargetData memory rebalancingTargetData =
413: LibStorage.getRebalancingTargets()[currencyId][oracleData.holding];
414: PrimeCashFactors memory factors = PrimeCashExchangeRate.getPrimeCashFactors(currencyId);
415: Token memory underlyingToken = TokenHandler.getUnderlyingToken(currencyId);
416:
417: targetAmount = ExternalLending.getTargetExternalLendingAmount(
418: underlyingToken, factors, rebalancingTargetData, oracleData, pr
419: );
420:
421: if (oracleData.currentExternalUnderlyingLend == 0) {
422: // If this is zero then there is no outstanding lending.
423: isExternalLendingUnhealthy = false;
424: } else {
425: uint256 offTargetPercentage = oracleData.currentExternalUnderlyingLend.toInt()
426: .sub(targetAmount.toInt()).abs()
427: .toUint()
428: .mul(uint256(Constants.PERCENTAGE_DECIMALS))
429: .div(targetAmount.add(oracleData.currentExternalUnderlyingLend));
430:
431: // prevent rebalance if change is not greater than 1%, important for health check and avoiding triggering
432: // rebalance shortly after rebalance on minimum change
433: isExternalLendingUnhealthy =
434: (targetAmount < oracleData.currentExternalUnderlyingLend) && (offTargetPercentage > 0);
435: }
436: }
Impact
The deviation between the target and current lending amount (offTargetPercentage) will be underestimated by approximately half the majority of the time. As a result, a rebalance intended to remediate the unhealthy external lending might be skipped since the code incorrectly assumes that it has not hit the off-target percentage. External lending beyond the target will affect the liquidity of the protocol, potentially resulting in withdrawal or liquidation, having issues executed due to insufficient liquidity.
Code Snippet
Tool used
Manual Review
Recommendation
Consider calculating the off-target percentages as a ratio of the difference to the target:
$$ offTargetPercentage = \frac{\mid currentExternalUnderlyingLend - targetAmount \mid}{targetAmount} \times 100% $$
Discussion
sherlock-admin2
1 comment(s) were left on this issue during the judging contest.
takarez commented:
valid because { valid }
T-Woodward
Would classify this as low severity because it just depends on what you take offTargetPercentage to mean. You can still accomplish the same thing by setting your governance parameters according to what offTargetPercentage means with the current code, so I wouldn't really say this is an issue.
However, I think it should be classified as low severity rather than disputed because the watson's suggestion for how to define offTargetPercentage is more intuitive.
sherlock-admin
The protocol team fixed this issue in PR/commit https://github.com/notional-finance/contracts-v3/pull/29.
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 off-target percentage was calculated as the absolute difference divided by the sum of current and target amounts, instead of dividing by the target amount alone.
Impact
The deviation is underestimated by about half most of the time, so a rebalance might be skipped, potentially causing withdrawal or liquidation issues due to insufficient liquidity.
What the attacker needed
None
The fix
The off-target percentage should be calculated as the absolute difference divided by the target amount.
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 if the amount lent externally is too far from a target. It calculates a percentage difference, but uses the wrong formula, making the difference look smaller than it is. So the contract might think everything is fine and not rebalance, even when it should.
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.