getTargetExternalLendingAmount() when targetUtilization == 0 no check whether enough externalUnderlyingAvailableForWithdraw
The function getTargetExternalLendingAmount returns zero when targetUtilization is zero, without checking whether the external lending protocol has enough available balance to withdraw the full amount. This can cause the rebalancing transaction to revert, preventing the protocol from quickly exiting a lending position.
Source: https://github.com/sherlock-audit/2023-12-notional-update-5-judging/issues/51
Found by
bin2chen
Summary
in getTargetExternalLendingAmount()
When targetUtilization == 0, it directly returns targetAmount=0.
It lacks the judgment of whether there is enough externalUnderlyingAvailableForWithdraw.
This may cause _rebalanceCurrency() to revert due to insufficient balance for withdraw.
Vulnerability Detail
when setRebalancingTargets() , we can setting all the targets to zero to immediately exit money
it will call _rebalanceCurrency() -> _isExternalLendingUnhealthy() -> getTargetExternalLendingAmount()
function getTargetExternalLendingAmount(
Token memory underlyingToken,
PrimeCashFactors memory factors,
RebalancingTargetData memory rebalancingTargetData,
OracleData memory oracleData,
PrimeRate memory pr
) internal pure returns (uint256 targetAmount) {
// Short circuit a zero target
@> if (rebalancingTargetData.targetUtilization == 0) return 0;
....
if (targetAmount < oracleData.currentExternalUnderlyingLend) {
uint256 forRedemption = oracleData.currentExternalUnderlyingLend - targetAmount;
if (oracleData.externalUnderlyingAvailableForWithdraw < forRedemption) {
// increase target amount so that redemptions amount match externalUnderlyingAvailableForWithdraw
targetAmount = targetAmount.add(
// unchecked - is safe here, overflow is not possible due to above if conditional
forRedemption - oracleData.externalUnderlyingAvailableForWithdraw
);
}
}
When targetUtilization==0, it returns targetAmount ==0.
It lacks the other judgments of whether the current externalUnderlyingAvailableForWithdraw is sufficient.
Exceeding externalUnderlyingAvailableForWithdraw may cause _rebalanceCurrency() to revert.
For example:
currentExternalUnderlyingLend = 100
externalUnderlyingAvailableForWithdraw = 99
If targetUtilization is modified to 0,
then targetAmount should be 1, not 0.
0 will cause an error due to insufficient available balance for withdrawal.
So, it should still try to withdraw as much deposit as possible first, wait for replenishment, and then withdraw the remaining deposit until the deposit is cleared.
Impact
A too small targetAmount may cause AAVE withdraw to fail, thereby causing the inability to setRebalancingTargets() to fail.
Code Snippet
Tool used
Manual Review
Recommendation
Remove targetUtilization == 0 directly returning 0.
The subsequent logic of the method can handle targetUtilization == 0 normally and will not cause an error.
function getTargetExternalLendingAmount(
Token memory underlyingToken,
PrimeCashFactors memory factors,
RebalancingTargetData memory rebalancingTargetData,
OracleData memory oracleData,
PrimeRate memory pr
) internal pure returns (uint256 targetAmount) {
// Short circuit a zero target
- if (rebalancingTargetData.targetUtilization == 0) return 0;
Discussion
sherlock-admin2
1 comment(s) were left on this issue during the judging contest.
takarez commented:
valid because {valid medium and a duplicate of 052}
nevillehuang
@jeffywu Is the impact of this significant and can compound to cause a material enough loss due to inability to rebalance? Also who is responsible for maintaining a high enough externalUnderlyingAvailableForWithdraw?
jeffywu
Hmm, I guess this would be tripped if governance tries to set the target utilization to zero. Likely, this occurs if we want to pull funds very quickly from an external lending protocol. The check is important to ensure that we pull as much funds as we can without reverting.
This issue would significantly hamper our ability to get out of a lending protocol quickly. A higher severity could be justified.
sherlock-admin
The protocol team fixed this issue in PR/commit https://github.com/notional-finance/contracts-v3/pull/26.
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 early return for targetUtilization == 0 bypasses the logic that adjusts the target amount based on externalUnderlyingAvailableForWithdraw, leading to an attempt to withdraw more than is available.
Impact
A too small targetAmount may cause the AAVE withdrawal to fail, preventing setRebalancingTargets() from succeeding and significantly hampering the ability to quickly exit a lending protocol.
What the attacker needed
Governance sets target utilization to zero; externalUnderlyingAvailableForWithdraw is less than…
The fix
The protocol team fixed this issue by removing the early return for targetUtilization == 0, as confirmed in the referenced PR.
How it was found
- Set rebalancing targets to zero to exit the external lending position.
- The function getTargetExternalLendingAmount returns zero because targetUtilization is zero.
- The rebalancing logic attempts to withdraw the full currentExternalUnderlyingLend amount.
- If externalUnderlyingAvailableForWithdraw is less than that amount, the withdrawal reverts.
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.
This is a smart contract bug. The code calculates how much money to withdraw from an external lending pool. When a setting is zero, it skips a check that ensures the pool actually has enough money available to withdraw. If the pool doesn't have enough, the withdrawal fails and the whole transaction reverts, so the protocol cannot move its funds as 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.