getTargetExternalLendingAmount() targetAmount may far less than the correct value
The function getTargetExternalLendingAmount() incorrectly caps the target lending amount by the maximum external deposit, which already includes the protocol's current deposit. This can cause the protocol to withdraw funds unnecessarily, harming its interests.
Source: https://github.com/sherlock-audit/2023-12-notional-update-5-judging/issues/52
Found by
bin2chen
Summary
When calculating ExternalLending.getTargetExternalLendingAmount(),
it restricts targetAmount greater than oracleData.maxExternalDeposit.
However, it does not take into account that oracleData.maxExternalDeposit includes the protocol deposit currentExternalUnderlyingLend
This may result in the returned quantity being far less than the correct quantity.
Vulnerability Detail
in getTargetExternalLendingAmount()
It restricts targetAmount greater than oracleData.maxExternalDeposit.
function getTargetExternalLendingAmount(
Token memory underlyingToken,
PrimeCashFactors memory factors,
RebalancingTargetData memory rebalancingTargetData,
OracleData memory oracleData,
PrimeRate memory pr
) internal pure returns (uint256 targetAmount) {
...
targetAmount = SafeUint256.min(
// totalPrimeCashInUnderlying and totalPrimeDebtInUnderlying are in 8 decimals, convert it to native
// token precision here for accurate comparison. No underflow possible since targetExternalUnderlyingLend
// is floored at zero.
uint256(underlyingToken.convertToExternal(targetExternalUnderlyingLend)),
// maxExternalUnderlyingLend is limit enforced by setting externalWithdrawThreshold
// maxExternalDeposit is limit due to the supply cap on external pools
@> SafeUint256.min(maxExternalUnderlyingLend, oracleData.maxExternalDeposit)
);
this is : targetAmount = min(targetExternalUnderlyingLend, maxExternalUnderlyingLend, oracleData.maxExternalDeposit)
The problem is that when calculating oracleData.maxExternalDeposit, it does not exclude the existing deposit currentExternalUnderlyingLend of the current protocol.
For example:
currentExternalUnderlyingLend = 100
targetExternalUnderlyingLend = 100
maxExternalUnderlyingLend = 10000
oracleData.maxExternalDeposit = 0 (All AAVE deposits include the current deposit currentExternalUnderlyingLend)
If according to the current calculation result: targetAmount=0, this will result in needing to withdraw 100. (currentExternalUnderlyingLend - targetAmount)
In fact, only when the calculation result needs to increase the deposit (targetAmount > currentExternalUnderlyingLend), it needs to be restricted by maxExternalDeposit.
The correct one should be neither deposit nor withdraw, that is, targetAmount=currentExternalUnderlyingLend = 100.
Impact
A too small targetAmount will cause the withdrawal of deposits that should not be withdrawn, damaging the interests of the protocol.
Code Snippet
Tool used
Manual Review
Recommendation
Only when targetAmount > currentExternalUnderlyingLend is a deposit needed, it should be considered that it cannot exceed oracleData.maxExternalDeposit
function getTargetExternalLendingAmount(
Token memory underlyingToken,
PrimeCashFactors memory factors,
RebalancingTargetData memory rebalancingTargetData,
OracleData memory oracleData,
PrimeRate memory pr
) internal pure returns (uint256 targetAmount) {
...
- targetAmount = SafeUint256.min(
- // totalPrimeCashInUnderlying and totalPrimeDebtInUnderlying are in 8 decimals, convert it to native
- // token precision here for accurate comparison. No underflow possible since targetExternalUnderlyingLend
- // is floored at zero.
- uint256(underlyingToken.convertToExternal(targetExternalUnderlyingLend)),
- // maxExternalUnderlyingLend is limit enforced by setting externalWithdrawThreshold
- // maxExternalDeposit is limit due to the supply cap on external pools
- SafeUint256.min(maxExternalUnderlyingLend, oracleData.maxExternalDeposit)
- );
+ targetAmount = SafeUint256.min(uint256(underlyingToken.convertToExternal(targetExternalUnderlyingLend)),maxExternalUnderlyingLend);
+ if (targetAmount > oracleData.currentExternalUnderlyingLend) { //when deposit , must check maxExternalDeposit
+ uint256 forDeposit = targetAmount - oracleData.currentExternalUnderlyingLend;
+ if (forDeposit > oracleData.maxExternalDeposit) {
+ targetAmount = targetAmount.sub(
+ forDeposit - oracleData.maxExternalDeposit
+ );
+ }
+ }
Discussion
sherlock-admin2
1 comment(s) were left on this issue during the judging contest.
takarez commented:
valid because { This is valid medium ; watson explained how an incorrect value can be returned}
sherlock-admin
The protocol team fixed this issue in PR/commit https://github.com/notional-finance/contracts-v3/pull/25.
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
The code applies the maximum external deposit cap without subtracting the current external lending amount, so the cap is effectively too low when the protocol already has a deposit.
What the attacker needed
Protocol has an existing external deposit
The fix
The fix should only apply the maxExternalDeposit limit when the target amount exceeds the current external lending amount, as shown in the recommendation.
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 where a calculation for how much to lend externally uses a limit that already counts the protocol's existing deposit. As a result, the protocol might withdraw money it shouldn't, losing potential earnings.
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.