Skip to content
Business logic Audit finding Intermediate

Rebalance process reverts due to zero amount deposit and redemption

The rebalancing strategy always attempts to deposit or redeem zero amounts for holdings that are only being redeemed or deposited, because the deposit and redeem amounts are mutually exclusive. When integrating with external money markets like AAVE that revert on zero-amount operations, the entire rebalancing process reverts, preventing Notional from moving funds.

xiaoming90 Follow

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-03-notional-judging/issues/195

Found by

xiaoming90 + 0xleastwood

Summary

Depositing or redeeming zero amount against certain external money markets will cause the rebalancing process to revert.

Vulnerability Detail

For a specific holding (e.g. cToken), the redeemAmounts and depositAmounts are mutually exclusive. So if the redeemAmounts for a specific holding is non-zero, the depositAmounts will be zero and vice-versa. This is because of the if-else block at Lines 48-56 below. Only redeemAmounts or depositAmounts of a specific holding can be initialized, but not both.

https://github.com/sherlock-audit/2023-03-notional/blob/main/contracts-v2/contracts/external/pCash/ProportionalRebalancingStrategy.sol#L48

Solidity
File: ProportionalRebalancingStrategy.sol
40:         for (uint256 i; i < holdings.length;) {
41:             address holding = holdings[i];
42:             uint256 targetAmount = totalValue * rebalancingTargets[i] / uint256(Constants.PERCENTAGE_DECIMALS);
43:             uint256 currentAmount = values[i];
44: 
45:             redeemHoldings[i] = holding;
46:             depositHoldings[i] = holding;
47: 
48:             if (targetAmount < currentAmount) {
49:                 unchecked {
50:                     redeemAmounts[i] = currentAmount - targetAmount;
51:                 }
52:             } else if (currentAmount < targetAmount) {
53:                 unchecked {
54:                     depositAmounts[i] = targetAmount - currentAmount;
55:                 }
56:             }
57: 
58:             unchecked {
59:                 ++i;
60:             }
61:         }
62:

For each holding, the following codes always deposit or redeem a zero value. For example, cETH holding, if the redeemAmounts is 100 ETH, the depositAmounts will be zero. (because of the if-else block). Therefore, getDepositCalldataForRebalancing function will be executed and attempt to deposit zero amount to Compound.

https://github.com/sherlock-audit/2023-03-notional/blob/main/contracts-v2/contracts/external/pCash/ProportionalRebalancingStrategy.sol#L63

Solidity
File: ProportionalRebalancingStrategy.sol
63:         rebalancingData.redeemData = oracle.getRedemptionCalldataForRebalancing(redeemHoldings, redeemAmounts);
64:         rebalancingData.depositData = oracle.getDepositCalldataForRebalancing(depositHoldings, depositAmounts);

The problem is that the deposit/mint or redeem/burn function of certain external money markets will revert if the amount is zero. Notional is considering integrating with a few external monkey markets and one of them is AAVE.

In this case, when Notional deposit zero amount to AAVE or redeem zero amount from AAVE, it causes the rebalancing process to revert because of the onlyAmountGreaterThanZero modifier on the AAVE's deposit and redeem function.

https://github.com/aave/aave-protocol/blob/4b4545fb583fd4f400507b10f3c3114f45b8a037/contracts/lendingpool/LendingPool.sol#L305

Solidity
function deposit(address _reserve, uint256 _amount, uint16 _referralCode)
    external
    payable
    nonReentrant
    onlyActiveReserve(_reserve)
    onlyUnfreezedReserve(_reserve)
    onlyAmountGreaterThanZero(_amount)
{

https://github.com/aave/aave-protocol/blob/4b4545fb583fd4f400507b10f3c3114f45b8a037/contracts/lendingpool/LendingPool.sol#LL331C1-L342C6

Solidity
function redeemUnderlying(
    address _reserve,
    address payable _user,
    uint256 _amount,
    uint256 _aTokenBalanceAfterRedeem
)
    external
    nonReentrant
    onlyOverlyingAToken(_reserve)
    onlyActiveReserve(_reserve)
    onlyAmountGreaterThanZero(_amount)
{

The above issue is not only limited to AAVE and might also happen in other external markets.

Even if the external money market does not revert when minting or burning zero amount, there is a small possibility that the supported underlying token might revert on zero value transfer (https://github.com/d-xo/weird-erc20#revert-on-zero-value-transfers). Because mint will do a transferOut and burn will do a transferIn against zero value

Impact

Notional would not be able to rebalance its underlying holding. The key feature of Notional V3 is to allow its Treasury Manager to rebalance underlying holdings into various other money market protocols.

This makes Notional more resilient to issues in external protocols and future-proofs the protocol. If rebalancing does not work, Notional will be unable to move its fund out of a vulnerable external market, potentially draining protocol funds if this is not mitigated.

Another purpose of rebalancing is to allow Notional to allocate Notional V3’s capital to new opportunities or protocols that provide a good return. If rebalancing does not work, the protocol and its users will lose out on the gain from the investment.

On the other hand, if an external monkey market that Notional invested in is consistently underperforming or yielding negative returns, Notional will perform a rebalance to reallocate its funds to a better market. However, if rebalancing does not work, they will be stuck with a suboptimal asset allocation, and the protocol and its users will incur losses.

Code Snippet

https://github.com/sherlock-audit/2023-03-notional/blob/main/contracts-v2/contracts/external/pCash/ProportionalRebalancingStrategy.sol#L48

https://github.com/sherlock-audit/2023-03-notional/blob/main/contracts-v2/contracts/external/pCash/ProportionalRebalancingStrategy.sol#L63

Tool used

Manual Review

Recommendation

Consider implementing validation to ensure the contract does not deposit zero amount to or redeem zero amount from the external market.

Following is the pseudocode for the potential fixes that could be implemented within the _getDepositCalldataForRebalancing of the holding contract to mitigate this issue. The same should be done for redemption.

Solidity
function _getDepositCalldataForRebalancing(
    address[] calldata holdings, 
    uint256[] calldata depositAmounts
) internal view virtual override returns (
    DepositData[] memory depositData
) {
    require(holdings.length == NUM_ASSET_TOKENS);
    for (int i = 0; i < holdings.length; i++) {
    	if (depositAmounts[i] > 0) {
    		// populate the depositData[i] with the deposit calldata to external money market>
    	}
    }
}

The above solution will return an empty calldata if the deposit amount is zero for a specific holding.

Within the _executeDeposits function, skip the depositData if it has not been initialized.

Solidity
function _executeDeposits(Token memory underlyingToken, DepositData[] memory deposits) private {
    uint256 totalUnderlyingDepositAmount;

    for (uint256 i; i < deposits.length; i++) {
        DepositData memory depositData = deposits[i];
        // if depositData is not initialized, skip to the next one

Discussion

jeffywu

Fixed in: https://github.com/notional-finance/contracts-v2/pull/120

xiaoming9090

@0xleastwood + @xiaoming9090 : Verified. Fixed in PR https://github.com/notional-finance/contracts-v2/pull/120

The original report as published on Sherlock, by xiaoming90 + 0xleastwood. 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 code unconditionally calls deposit and redeem functions for all holdings, even when the amount is zero, without checking if the amount is greater than zero.

Impact

Notional would be unable to rebalance its holdings, potentially leaving funds stuck in vulnerable or underperforming external markets and causing losses.

What the attacker needed

Integration with an external market that reverts on zero…; Rebalancing triggered with a holding that requires only…

The fix

The fix was to add validation to skip deposit or redeem calls when the amount is zero, as implemented in the referenced pull request.

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 that manages investments tries to move money between different lending platforms. For each type of asset, it either adds money or removes money, but it still sends a request to do the opposite action with zero amount. Some platforms reject zero-amount requests, so the whole process fails and the money cannot be moved.

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 xiaoming90

View all

17 more disclosed reports, best first.

More from Notional Update #4

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.