Skip to content
Business logic Audit finding Intermediate

A user can lose more value than he specifies in the spread when he enters a PowerFarm

A user entering a PowerFarm on Arbitrum can lose more value than the allowed spread because the deposit amount is reduced by a Uniswap swap before the final value check, causing the check to compare against a lower baseline. The user may lose up to approximately $45.24 more than intended on a $1000 deposit with a 105% spread.

nonseodion Follow

Originally disclosed on Code4rena

Submitted by nonseodion

When a user enters or exits a PowerFarm, he specifies an allowed spread. The spread specifies the minimum value of the position allowed at the end of the transaction.

E.g. a spread of 105% on a position with a value of $1000 ensures the value does not fall below $950.

1000 * (200-105) / 100 = 950

When a user opens a position on Arbitrum, the ENTRY_ASSET is converted to WETH on UniswapV3. The second argument in line 434 specifies the minimum value for the swap. The spread is applied to this argument. Hence, the _depositAmount in line 432 cannot go less than what the spread allows.

PendlePowerFarmLeverageLogic.sol#L423-L440

426:         uint256 reverseAllowedSpread = 2
427:             * PRECISION_FACTOR_E18
428:             - _allowedSpread;
429:
430:         if (block.chainid == ARB_CHAIN_ID) {
431:
432:             _depositAmount = _getTokensUniV3(
433:                 _depositAmount,
434:                 _getEthInTokens(
435:                         ENTRY_ASSET,
436:                         _depositAmount
437:                     )
438:                 * reverseAllowedSpread
439:                 / PRECISION_FACTOR_E18,
440:                 WETH_ADDRESS,
441:                 ENTRY_ASSET
442:             );
443:         }

The value at the end of the transaction is checked in line 508 below to ensure it does not go below the allowed spread. The ethValueBefore is calculated from _depositAmount in line 489. Note that if the swap on Uniswap occurred the _depositAmount may already be the minimum value. The ethValueAfter is scaled with the allowed spread in line 501.

PendlePowerFarmLeverageLogic.sol#L485-L505

489:         uint256 ethValueBefore = _getTokensInETH(
490:             ENTRY_ASSET,
491:             _depositAmount
492:         );
493:
494:         (
495:             uint256 receivedShares
496:             ,
497:         ) = IPendleChild(PENDLE_CHILD).depositExactAmount(
498:             netLpOut
499:         );
500:         // @audit-issue the calculation for ethValueAfter below is incorrect
501:         uint256 ethValueAfter = _getTokensInETH(
502:             PENDLE_CHILD,
503:             receivedShares
504:         )
505:             * _allowedSpread
506:             / PRECISION_FACTOR_E18;
507:         // @audit-issue ethValueBefore on Arbitrum uses the depositAmount that allowedSpread has already been applied
508:         if (ethValueAfter < ethValueBefore) {
509:             revert TooMuchValueLost();
510:         }

Thus the comparison in line 508 may compare ethValueAfter with the minimum value of the spread instead of the value of the initial deposit. With this implementation, if the spread for a $1000 transaction is 105%, the minimum value after the transaction becomes $902.5.

(1000 * 95 / 100) * (95/100) = 902.5. The user can lose $47.5 (950-902.5).

Note: In the implementation, the value at the end is scaled up instead of the value at the beginning being scaled down like the examples show. Hence, the actual minimum value is lesser i.e. ~$904.76 (950/1.05). This is a different bug.

Proof of Concept

  1. A user enters the market with $1000 of WBTC and specifies a spread of 105%.
  2. At the end of the transaction the comparison is done against $950. So the actual value he gets can be between ~$904.76 and $950 and the transaction would pass.
  3. The user may lose between $0 and $45.24 (950 - 904.76).

Recommended Mitigation Steps

Consider storing the actual deposit and using it to calculate ethValueBefore.

PendlePowerFarmLeverageLogic.sol#L423-L488

        uint256 reverseAllowedSpread = 2
            * PRECISION_FACTOR_E18
            - _allowedSpread;
+       unit256 actualDeposit = _depositAmount;
        if (block.chainid == ARB_CHAIN_ID) {

            _depositAmount = _getTokensUniV3(
                _depositAmount,
                _getEthInTokens(
                        ENTRY_ASSET,
                        _depositAmount
                    )
                * reverseAllowedSpread
                / PRECISION_FACTOR_E18,
                WETH_ADDRESS,
                ENTRY_ASSET
            );
        }

    ...

        uint256 ethValueBefore = _getTokensInETH(
            ENTRY_ASSET,
-            _depositAmount
+.           actualDeposit
        );

Assessed type

Uniswap

Wise Lending commented:

Mitigated here.


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

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 deposit amount is modified by a Uniswap swap before being used to calculate the baseline value for the final spread check, so the check compares against an already-reduced amount.

Impact

The user could lose up to approximately $45.24 more than the allowed spread on a $1000 deposit with a 105% spread.

What the attacker needed

User enters a PowerFarm on Arbitrum; User specifies an allowed spread

The fix

The actual deposit amount should be stored before the swap and used to calculate the baseline value for the final check.

How it was found

  1. A user enters the market with $1000 of WBTC and specifies a spread of 105%.
  2. At the end of the transaction the comparison is done against $950.
  3. The actual value he gets can be between ~$904.76 and $950 and the transaction would pass.
  4. The user may lose between $0 and $45.24.

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.

When you deposit money into a system, you set a limit on how much you are willing to lose. The system is supposed to stop the transaction if the final value falls below that limit. Here, a swap happens first that reduces the deposit amount, and then the check uses that reduced amount as the starting point, so the actual loss can be larger than you 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 nonseodion

View all

8 more disclosed reports, best first.

More from Wise Lending

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.