Skip to content
Business logic Audit finding Intermediate

External lending can exceed the threshold

The maximum external lending amount was calculated incorrectly, allowing Notional to lend more than the external withdrawal threshold. This could leave insufficient liquidity on external markets, causing users to be unable to withdraw their prime cash or preventing liquidations.

xiaoming90 Follow

Originally disclosed on Sherlock

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

Found by

xiaoming90

Summary

Due to an incorrect calculation of the max lending amount, external lending can exceed the external withdrawal threshold. If this restriction/threshold is not adhered to, users or various core functionalities within the protocol will have issues redeeming or withdrawing their prime cash.

Vulnerability Detail

The following is the extract from the Audit Scope Documentation provided by the protocol team on the contest page that describes the external withdraw threshold:

● External Withdraw Threshold: ensures that Notional has sufficient liquidity to withdraw from an external lending market. If Notional has 1000 units of underlying lent out on Aave, it requires 1000 * externalWithdrawThreshold units of underlying to be available on Aave for withdraw. This ensures there is sufficient buffer to process the redemption of Notional funds. If available liquidity on Aave begins to drop due to increased utilization, Notional will automatically begin to withdraw its funds from Aave to ensure that they are available for withdrawal on Notional itself.

To ensure the redeemability of Notional’s funds on external lending markets, Notional requires there to be redeemable funds on the external lending market that are a multiple of the funds that Notional has lent on that market itself.

Assume that the externalWithdrawThreshold is 200% and the underlying is USDC. Therefore, PERCENTAGE_DECIMALS/externalWithdrawThreshold = 100/200 = 0.5 (Line 83-84 below). This means that the number of USDC to be available on AAVE for withdrawal must be two (2) times the number of USDC Notional lent out on AAVE (A multiple of 2).

The externalUnderlyingAvailableForWithdraw stores the number of liquidity in USDC on the AAVE pool available to be withdrawn.

If externalUnderlyingAvailableForWithdraw is 1000 USDC and currentExternalUnderlyingLend is 400 USDC, this means that the remaining 600 USDC liquidity on the AAVE pool is not owned by Notional.

The maxExternalUnderlyingLend will be 600 * 0.5 = 300. Thus, the maximum amount that Notional can lend externally at this point is 300 USDC.

Assume that after Notional has lent 300 USDC externally to the AAVE pool.

The currentExternalUnderlyingLend will become 400+300=700, and the externalUnderlyingAvailableForWithdraw will become 1000+300=1300

Following is the percentage of USDC in AAVE that belong to Notional

Solidity
700/1300 = 0.5384615385 (53%).

At this point, the invariant is broken as the number of USDC to be available on AAVE for withdrawal is less than two (2) times the number of USDC lent out on AAVE after the lending.

https://github.com/sherlock-audit/2023-12-notional-update-5/blob/main/contracts-v3/contracts/internal/balances/ExternalLending.sol#L81

Solidity
File: ExternalLending.sol
36:     function getTargetExternalLendingAmount(
..SNIP..
79:         uint256 maxExternalUnderlyingLend;
80:         if (oracleData.currentExternalUnderlyingLend < oracleData.externalUnderlyingAvailableForWithdraw) {
81:             maxExternalUnderlyingLend =
82:                 (oracleData.externalUnderlyingAvailableForWithdraw - oracleData.currentExternalUnderlyingLend)
83:                 .mul(uint256(Constants.PERCENTAGE_DECIMALS))
84:                 .div(rebalancingTargetData.externalWithdrawThreshold);
85:         } else {
86:             maxExternalUnderlyingLend = 0;
87:         }

The root cause is that when USDC is deposited to AAVE to get aUSDC, the total USDC in the pool increases. Therefore, using the current amount of USDC in the pool to determine the maximum deposit amount is not an accurate measure of liquidity risk.

Impact

To ensure the redeemability of Notional’s funds on external lending markets, Notional requires there to be redeemable funds on the external lending market that are a multiple of the funds that Notional has lent on that market itself.

If this restriction is not adhered to, users or various core functionalities within the protocol will have issues redeeming or withdrawing their prime cash. For instance, users might not be able to withdraw their assets from the protocol due to insufficient liquidity, or liquidation cannot be carried out due to lack of liquidity, resulting in bad debt accumulating within the protocol and negatively affecting the protocol's solvency.

Code Snippet

https://github.com/sherlock-audit/2023-12-notional-update-5/blob/main/contracts-v3/contracts/internal/balances/ExternalLending.sol#L81

Tool used

Manual Review

Recommendation

To ensure that a deposit does not exceed the threshold, the following formula should be used to determine the maximum deposit amount:

Let's denote:

$T$ as the externalWithdrawThreshold $L$ as the currentExternalUnderlyingLend $W$ as the externalUnderlyingAvailableForWithdraw $D$ as the Deposit (the variable we want to solve for)

$$ T = \frac{L + D}{W + D} $$

Solving $D$, the formula for calculating the maximum deposit ($D$) is

$$ D = \frac{TW-L}{1-T} $$

Using back the same example in the "Vulnerability Detail" section.

The maximum deposit amount is as follows:

Solidity
D = (TW - L) / (1 - T)
D = (0.5 * 1000 - 400) / (1 - 0.5)
D = (500 - 400) / 0.5 = 200

If 200 USDC is lent out, it will still not exceed the threshold of 200%, which demonstrates that the formula is working as intended in keeping the multiple of two (200%) constant before and after the deposit.

Solidity
(400 + 200) / (1000 + 200) = 0.5

Discussion

sherlock-admin2

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

takarez commented:

valid because { valid and a duplicate of 035 and even seem to be by same user}

sherlock-admin

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

sherlock-admin

The Lead Senior Watson signed-off on the fix.

The original report as published on Sherlock, by xiaoming90. 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

Likely the maximum lend calculation used the current available liquidity without accounting for the fact that depositing increases both the lent amount and the available liquidity, so the threshold ratio is not preserved.

Impact

Users might not be able to withdraw their assets due to insufficient liquidity, or liquidations could fail, leading to bad debt and affecting solvency.

What the attacker needed

External lending enabled; externalWithdrawThreshold set

The fix

The issue was fixed by using a corrected formula that solves for the maximum deposit while keeping the threshold ratio constant.

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.

Notional lends funds to external markets like Aave. To ensure it can always get its money back, it requires that the external market holds a multiple of what Notional lent. The code that calculates how much more Notional can lend used the wrong formula, so it could lend too much and break that safety buffer.

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

122 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.