Skip to content
Business logic Audit finding Intermediate

_isExternalLendingUnhealthy() using stale factors

The view function checkRebalance() used stale prime cash factors when computing the target external lending amount, so the rebalancing bot could incorrectly decide that no rebalance was needed. This could delay or prevent necessary rebalancing, increasing risk to the protocol.

bin2chen Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-12-notional-update-5-judging/issues/18

Found by

bin2chen

Summary

In checkRebalance() -> _isExternalLendingUnhealthy() -> getTargetExternalLendingAmount(factors) using stale factors will lead to inaccurate targetAmount, which in turn will cause checkRebalance() that should have been rebalance to not execute.

Vulnerability Detail

rebalancingBot uses checkRebalance() to return the currencyIds [] that need to be rebalance.

call order : checkRebalance() -> _isExternalLendingUnhealthy() -> ExternalLending.getTargetExternalLendingAmount(factors)

Solidity
    function _isExternalLendingUnhealthy(
        uint16 currencyId,
        IPrimeCashHoldingsOracle oracle,
        PrimeRate memory pr
    ) internal view returns (bool isExternalLendingUnhealthy, OracleData memory oracleData, uint256 targetAmount) {
...

@>      PrimeCashFactors memory factors = PrimeCashExchangeRate.getPrimeCashFactors(currencyId);
        Token memory underlyingToken = TokenHandler.getUnderlyingToken(currencyId);

        targetAmount = ExternalLending.getTargetExternalLendingAmount(
            underlyingToken, factors, rebalancingTargetData, oracleData, pr
        );

A very important logic is to get targetAmount. The calculation of this value depends on factors. But currently used is PrimeCashFactors memory factors = PrimeCashExchangeRate.getPrimeCashFactors(currencyId);. This is not the latest. It has not been aggregated yet. The correct one should be ( /* */,factors) = PrimeCashExchangeRate.getPrimeCashRateView();.

Impact

Due to the incorrect targetAmount, it may cause the currencyId that should have been re-executed Rebalance to not execute rebalance, increasing the risk of the protocol.

Code Snippet

https://github.com/sherlock-audit/2023-12-notional-update-5/blob/main/contracts-v3/contracts/external/actions/TreasuryAction.sol#L414

Tool used

Manual Review

Recommendation

Diff
    function _isExternalLendingUnhealthy(
        uint16 currencyId,
        IPrimeCashHoldingsOracle oracle,
        PrimeRate memory pr
    ) internal view returns (bool isExternalLendingUnhealthy, OracleData memory oracleData, uint256 targetAmount) {
...

-       PrimeCashFactors memory factors = PrimeCashExchangeRate.getPrimeCashFactors(currencyId);
+       ( /* */,PrimeCashFactors memory factors) = PrimeCashExchangeRate.getPrimeCashRateView();
        Token memory underlyingToken = TokenHandler.getUnderlyingToken(currencyId);

        targetAmount = ExternalLending.getTargetExternalLendingAmount(
            underlyingToken, factors, rebalancingTargetData, oracleData, pr
        );

Discussion

sherlock-admin2

1 comment(s) were left on this issue during the judging contest.

takarez commented:

invalid because { not convince with the impact}

jeffywu

Prime factors are accrued immediately prior to the call: https://github.com/sherlock-audit/2023-12-notional-update-5/blob/main/contracts-v3/contracts/external/actions/TreasuryAction.sol#L319

bin2chen66

Escalate

This issue will not affect the rebalance() method, as @jeffywu mentioned, rebalance() uses the latest factors.

The problem I described affects checkRebalance(). According to the description

/// @notice View method used by Gelato to check if rebalancing can be executed and get the execution payload. function checkRebalance() external view override returns (bool canExec, bytes memory execPayload) {

This method is for the bot. If it uses stale factors, the bot will think that the requirements are not yet met, and there is no chance to execute rebalance().

thank you.

sherlock-admin2

Escalate

This issue will not affect the rebalance() method, as @jeffywu mentioned, rebalance() uses the latest factors.

The problem I described affects checkRebalance(). According to the description

/// @notice View method used by Gelato to check if rebalancing can be executed and get the execution payload. function checkRebalance() external view override returns (bool canExec, bytes memory execPayload) {

This method is for the bot. If it uses stale factors, the bot will think that the requirements are not yet met, and there is no chance to execute rebalance().

thank you.

You've created a valid escalation!

To remove the escalation from consideration: Delete your comment.

You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.

bin2chen66

different

rebalance call order: rebalance() -> _rebalanceCurrency() (will refresh factors first) -> _isExternalLendingUnhealthy() will get latest factors

checkRebalance call order: checkRebalance() -> _isExternalLendingUnhealthy() will get stale factors without refresh factors first

then If checkRebalance () is incorrect, rebalance () may not have a chance to execute

jeffywu

Understood, this is a valid escalation.

nevillehuang

Since this could directly undermine frequency of rebalancing, agree it can be medium severity.

Evert0x

@bin2chen66 do I understand correctly that this only impacts the bot Notional is planning to use?

This method is for the bot. If it uses stale factors, the bot will think that the requirements are not yet met, and there is no chance to execute rebalance().

bin2chen66

@Evert0x Hi, Yes, as far as I know, the logic of the bot is very simple, checkRebalance () is called periodically to get execPayload, such as execPayload=[1,2,3]. if execPayload is not empty then the bot calls rebalance (execPayload) . Whether there are any other uses, needs to be confirmed by the sponsor.

Evert0x

@bin2chen66 my judgment will be to keep the issue invalid

Incorrect values in View functions are by default considered low. Exception: In case any of these incorrect values returned by the view functions are used as a part of a larger function which would result in loss of funds then it would be a valid medium/high depending on the impact.

The part "used as a part of a larger function which would result in loss of funds" doesn't apply here as the bot is an off-chain component.

bin2chen66

@Evert0x I have an objection. This rebalance() depends entirely on checkRebalance (). Why is it not part of this function? How do you chew words? please respect the facts

Evert0x

@bin2chen66 it's not part of the function as an off-chain component (the bot) is expected to consume the output of the view function and initiate a transaction to call rebalance()

Rebalancing Bot: is an off chain bot that detects when rebalancing can occur via checkRebalance and will rebalance those currencies accordingly.

However, in the README it's clearly stated that the output of the view function is of importance to the functioning of the protocol.

Planning to accept escalation and make Medium.

Evert0x

Result: Medium Unique

sherlock-admin2

Escalations have been resolved successfully!

Escalation status:

sherlock-admin

The protocol team fixed this issue in PR/commit https://github.com/notional-finance/contracts-v3/pull/33.

sherlock-admin

The Lead Senior Watson signed-off on the fix.

The original report as published on Sherlock, by bin2chen. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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 view function checkRebalance() fetched prime cash factors without first accruing them, so it used outdated values instead of the latest accrued factors.

Impact

The bot may fail to trigger necessary rebalancing, increasing the risk to the protocol; the issue only affects the off-chain bot's decision, not the rebalance() function itself.

What the attacker needed

Rebalancing bot relies on checkRebalance(); stale factors not yet accrued

The fix

The issue was fixed by changing the code to use the latest accrued factors via getPrimeCashRateView() instead of getPrimeCashFactors().

How it was found

  1. The rebalancing bot periodically calls checkRebalance() to see if any currency needs rebalancing.
  2. checkRebalance() calls _isExternalLendingUnhealthy(), which retrieves prime cash factors using getPrimeCashFactors(currencyId).
  3. Those factors are stale because they have not been accrued, leading to an inaccurate targetAmount.
  4. The bot receives an empty or incorrect execPayload and does not call rebalance(), even though a rebalance was needed.

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 has a function that a bot calls to check whether it should rebalance funds. That function used old data (stale factors) instead of the latest, so the bot sometimes thought everything was fine when it was not. As a result, the bot might not trigger the rebalance, leaving the protocol in a riskier state.

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.

More by bin2chen

View all

139 more disclosed reports, best first.

More from Notional Update #5

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.