Skip to content
Business logic Audit finding Advanced

Inadequate slippage control

The slippage check compared the user's rate limit against the post-trade interest rate rather than the rate used during the trade, so a trade could execute at a rate exceeding the user's limit if the post-trade rate settled back within it. An attacker could exploit this to make users borrow at higher costs or lend at lower returns than intended.

xiaoming90 Follow

Originally disclosed on Sherlock

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

Found by

xiaoming90 + 0xleastwood

Summary

The current slippage control mechanism checks a user's acceptable interest rate limit against the post-trade rate, which could result in trades proceeding at rates exceeding the user's defined limit.

Vulnerability Detail

https://github.com/sherlock-audit/2023-03-notional/blob/main/contracts-v2/contracts/internal/markets/InterestRateCurve.sol#L421

Solidity
File: InterestRateCurve.sol
421:     function _getNetCashAmountsUnderlying(
422:         InterestRateParameters memory irParams,
423:         MarketParameters memory market,
424:         CashGroupParameters memory cashGroup,
425:         int256 totalCashUnderlying,
426:         int256 fCashToAccount,
427:         uint256 timeToMaturity
428:     ) private pure returns (int256 postFeeCashToAccount, int256 netUnderlyingToMarket, int256 cashToReserve) {
429:         uint256 utilization = getfCashUtilization(fCashToAccount, market.totalfCash, totalCashUnderlying);
430:         // Do not allow utilization to go above 100 on trading
431:         if (utilization > uint256(Constants.RATE_PRECISION)) return (0, 0, 0);
432:         uint256 preFeeInterestRate = getInterestRate(irParams, utilization);
433: 
434:         int256 preFeeCashToAccount = fCashToAccount.divInRatePrecision(
435:             getfCashExchangeRate(preFeeInterestRate, timeToMaturity)
436:         ).neg();
437: 
438:         uint256 postFeeInterestRate = getPostFeeInterestRate(irParams, preFeeInterestRate, fCashToAccount < 0);
439:         postFeeCashToAccount = fCashToAccount.divInRatePrecision(
440:             getfCashExchangeRate(postFeeInterestRate, timeToMaturity)
441:         ).neg();

When executing a fCash trade, the interest rate is computed based on the utilization of the current market (Refer to Line 432). The postFeeInterestRate is then computed based on the preFeeCashToAccount and trading fee, and this rate will be used to derive the exchange rate needed to convert fCashToAccount to the net prime cash (postFeeCashToAccount).

Note that the interest rate used for the trade is postFeeInterestRate, and postFeeCashToAccount is the amount of cash credit or debit to an account.

If there is any slippage control in place, the slippage should be checked against the postFeeInterestRate or postFeeCashToAccount. As such, there are two approaches to implementing slippage controls:

  • 1st Approach - The current interest rate is 2%. User sets their acceptable interest rate limit at 3% when the user submits the trade transaction. The user's tolerance is 1%. From the time the trade is initiated to when it's executed, the rate (postFeeInterestRate) rises to 5%, the transaction should revert due to the increased slippage beyond the user's tolerance.
  • 2nd Approach - If a user sets the minimum trade return of 1000 cash, but the return is only 900 cash (postFeeCashToAccount) when the trade is executed, the transaction should revert as it exceeded the user's slippage tolerance

Note: When users submit a trade transaction, the transaction is held in the mempool for a period of time before executing, and thus the market condition and interest rate might change during this period, and slippage control is used to protect users from these fluctuations.

However, within the codebase, it was observed that the slippage was not checked against the postFeeInterestRate or postFeeCashToAccount.

https://github.com/sherlock-audit/2023-03-notional/blob/main/contracts-v2/contracts/internal/markets/InterestRateCurve.sol#L405

Solidity
File: InterestRateCurve.sol
371:         // returns the net cash amounts to apply to each of the three relevant balances.
372:         (
373:             int256 netUnderlyingToAccount,
374:             int256 netUnderlyingToMarket,
375:             int256 netUnderlyingToReserve
376:         ) = _getNetCashAmountsUnderlying(
377:             irParams,
378:             market,
379:             cashGroup,
380:             totalCashUnderlying,
381:             fCashToAccount,
382:             timeToMaturity
383:         );
..SNIP..
388:         {
389:             // Do not allow utilization to go above 100 on trading, calculate the utilization after
390:             // the trade has taken effect, meaning that fCash changes and cash changes are applied to
391:             // the market totals.
392:             market.totalfCash = market.totalfCash.subNoNeg(fCashToAccount);
393:             totalCashUnderlying = totalCashUnderlying.add(netUnderlyingToMarket);
394: 
395:             uint256 utilization = getfCashUtilization(0, market.totalfCash, totalCashUnderlying);
396:             if (utilization > uint256(Constants.RATE_PRECISION)) return (0, 0);
397: 
398:             uint256 newPreFeeImpliedRate = getInterestRate(irParams, utilization);
..SNIP..
404:             // Saves the preFeeInterestRate and fCash
405:             market.lastImpliedRate = newPreFeeImpliedRate;
406:         }

After computing the net prime cash (postFeeCashToAccount == netUnderlyingToAccount) at Line 373 above, it updates the market.totalfCash and totalCashUnderlying. Line 395 computes the utilization after the trade happens, and uses the latest utilization to compute the new interest rate after the trade and save it within the market.lastImpliedRate

https://github.com/sherlock-audit/2023-03-notional/blob/main/contracts-v2/contracts/external/actions/TradingAction.sol#L268

Solidity
File: TradingAction.sol
234:     function _executeLendBorrowTrade(
..SNIP..
256:         cashAmount = market.executeTrade(
257:             account,
258:             cashGroup,
259:             fCashAmount,
260:             market.maturity.sub(blockTime),
261:             marketIndex
262:         );
263: 
264:         uint256 rateLimit = uint256(uint32(bytes4(trade << 104)));
265:         if (rateLimit != 0) {
266:             if (tradeType == TradeActionType.Borrow) {
267:                 // Do not allow borrows over the rate limit
268:                 require(market.lastImpliedRate <= rateLimit, "Trade failed, slippage");
269:             } else {
270:                 // Do not allow lends under the rate limit
271:                 require(market.lastImpliedRate >= rateLimit, "Trade failed, slippage");
272:             }
273:         }
274:     }

The trade is executed at Line 256 above. After the trade is executed, it will check for the slippage at Line 264-273 above.

Let $IR_1$ be the interest rate used during the trade (postFeeInterestRate), $IR_2$ be the interest rate after the trade (market.lastImpliedRate), and $IR_U$ be the user's acceptable interest rate limit (rateLimit).

Based on the current slippage control implementation, $IR_U$ is checked against $IR_2$. Since the purpose of having slippage control in DeFi trade is to protect users from unexpected and unfavorable price changes during the execution of a trade, $IR_1$ should be used instead.

Assume that at the time of executing a trade (TradeActionType.Borrow), $IR_1$ spikes up and exceeds $IR_U$. However, since the slippage control checks $IR_U$ against $IR_2$, which may have resettled to $IR_U$ or lower, the transaction proceeds despite exceeding the user's acceptable rate limit. So, the transaction succeeds without a revert.

This issue will exacerbate when executing large trades relative to pool liquidity.

Impact

The existing slippage control does not provide the desired protection against unexpected interest rate fluctuations during the transaction. As a result, users might be borrowing at a higher cost or lending at a lower return than they intended, leading to losses.

Code Snippet

https://github.com/sherlock-audit/2023-03-notional/blob/main/contracts-v2/contracts/internal/markets/InterestRateCurve.sol#L421

https://github.com/sherlock-audit/2023-03-notional/blob/main/contracts-v2/contracts/internal/markets/InterestRateCurve.sol#L405

https://github.com/sherlock-audit/2023-03-notional/blob/main/contracts-v2/contracts/external/actions/TradingAction.sol#L268

Tool used

Manual Review

Recommendation

Consider updating the slippage control to compare the user's acceptable interest rate limit (rateLimit) against the interest rate used during the trade execution (postFeeInterestRate).

Discussion

jeffywu

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

xiaoming9090

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

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

Likely the slippage check compares the user's rate limit against the post-trade implied rate (market.lastImpliedRate) instead of the interest rate actually used for the trade (postFeeInterestRate).

Impact

Users might be borrowing at a higher cost or lending at a lower return than they intended, leading to losses.

What the attacker needed

User submits a trade with a rate limit; market rate spikes during execution

The fix

The slippage control was fixed to compare the user's acceptable interest rate limit against the interest rate used during trade execution.

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.

In DeFi trading, slippage control lets a user say 'don't execute my trade if the price moves beyond this limit'. Here, the check used the interest rate after the trade finished, not the rate actually used during the trade. So if the rate spiked during execution but then settled back, the trade would still go through even though the user got a worse rate than they allowed.

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.