Skip to content
Business logic Audit finding Advanced

Wrong use of nftID to check if a PowerFarm position is an Aave position

The contract uses the wrong identifier (the WiseLending NFT ID instead of the PowerFarm key ID) to check whether a position is an Aave position, causing incorrect borrow shares to be fetched. This prevents users from manually paying back Aave positions and can block liquidation of such positions.

nonseodion Follow with 0xStalin

Originally disclosed on Code4rena

Submitted by nonseodion, also found by 0xStalin

When a PowerFarm position is created its keyId is used as a key in the isAave mapping to indicate if it is an Aave position or not. The keyId is the index of the Power Farm NFT linked with the position.

PendlePowerManager.sol#L129-L130

        isAave[keyId] = _isAave;

The keyId is also linked with another nftId. This other nftId is used to hold the keyIds Power Farm position in the WiseLending contract. They are linked together in the farmingKeys mapping of the MinterReserver contract.

MinterReserver.sol#L88C1-L91C46

        uint256 keyId = _getNextReserveKey();

        reservedKeys[_userAddress] = keyId;
        farmingKeys[keyId] = _wiseLendingNFT;

The issue is the check if a position is an Aave position is done using the WiseLending nftId instead of the Power Farm’s keyId. This occurs five times in the code:

It is used in getLiveDebtRatio() to know the pool token borrowed so the borrowShares can be retrieved.

PendlePowerFarm.sol#L64-L70

        uint256 borrowShares = isAave[_nftId]
            ? _getPositionBorrowSharesAave(
                _nftId
            )
            : _getPositionBorrowShares(
                _nftId
        );

It is used in _manuallyPaybackShares() to know the pool token to pay back.

PendlePowerFarm.sol#L127-L129

        if (isAave[_nftId] == true) {
            poolAddress = AAVE_WETH_ADDRESS;
        }

It is used in checkDebtRatio() to know the pool token borrowed so the borrowShares can be retrieved.

PendlePowerFarmMathLogic.sol#L396-L402

   ❌    uint256 borrowShares = isAave[_nftId]
            ? _getPositionBorrowSharesAave(
                _nftId
            )
            : _getPositionBorrowShares(
                _nftId
        );

It is used in _coreLiquidation() to select a token to payback and to know the pool token borrowed so the borrowShares can be retrieved.

PendlePowerFarmLeverageLogic.sol#L575-L590

   ❌    address paybackToken = isAave[_nftId] == true
            ? AAVE_WETH_ADDRESS
            : WETH_ADDRESS;

        paybackAmount = WISE_LENDING.paybackAmount(
            paybackToken,
            _shareAmountToPay
        );

   ❌    uint256 cutoffShares = isAave[_nftId] == true
            ? _getPositionBorrowSharesAave(_nftId)
                * FIVTY_PERCENT
                / PRECISION_FACTOR_E18
            : _getPositionBorrowShares(_nftId)
                * FIVTY_PERCENT
                / PRECISION_FACTOR_E18;

These have the following effects:

  • For getLiveDebtRatio(), users would get zero when they try to retrieve their debt ratio.

  • For _manuallyPaybackShares() users won’t be able to pay back their shares manually from the PowerFarm contract since it’ll fetch zero shares as borrow shares.

  • The last two instances are used in liquidation and allow a malicious user to have a position that can’t be liquidated even though it is eligible for liquidation. The malicious user can:

    1. Create an Aave PowerFarm position.
    2. The position becomes eligible for liquidation after some price changes.
    3. Liquidators cannot liquidate the position because the call to _coreLiquidation first calls checkDebtRatio() which uses the wrong borrowShares to calculate the debt ratio and returns true. Thus, causing a revert.

PendlePowerFarmLeverageLogic.sol#L571C1-L574C1

        if (_checkDebtRatio(_nftId) == true) {
            revert DebtRatioTooLow();
        }

Impact

  1. Malicious users can open positions that can’t get liquidated.
  2. Users can’t pay back manually when it is an Aave position.
  3. getLiveDebtRatio() returns zero always when it is an Aave position.

Proof of Concept

There are 3 tests below and they can all be run in PendlePowerFarmControllerBase.t.sol.

  • testAaveGetLiveDebtRatio() shows that getLiveDebtRatio() returns zero.
  • testAaveManuallyPayback() shows that borrowed tokens can’t be paid back using manuallyPaybackShares().
  • testCannotLiquidate() shows that Aave positions cannot be liquidated.
    function testAaveGetLiveDebtRatio() public cheatSetup(true){
        _prepareAave();
        uint256 keyID = powerFarmManagerInstance.enterFarm(
            true,
            1 ether,
            15 ether,
            entrySpread
        );

        uint nftId = powerFarmManagerInstance.farmingKeys(keyID);
        // gets borrow shares of weth instead of aeth
        uint ratio = powerFarmManagerInstance.getLiveDebtRatio(nftId);
        assertEq(ratio, 0);
    }

    function testAaveManuallyPayback() public cheatSetup(true){
        _prepareAave();
        uint256 keyID = powerFarmManagerInstance.enterFarm(
            true,
            1 ether,
            15 ether,
            entrySpread
        );

        uint nftId = powerFarmManagerInstance.farmingKeys(keyID);
        uint borrowShares = wiseLendingInstance.getPositionBorrowShares(nftId, AWETH);
        // tries to payback weth instead of aweth and reverts with an arithmetic underflow
        // since the position has 0 weth borrow shares
        vm.expectRevert();
        powerFarmManagerInstance.manuallyPaybackShares(keyID, borrowShares);
    }

    error DebtRatioTooLow();
    function testCannotLiquidate() public cheatSetup(true){
        _prepareAave();
        uint256 keyID = powerFarmManagerInstance.enterFarm(
            true,
            1 ether,
            15 ether,
            entrySpread
        );

        // increase collateral factors to make position eligible for liquidation
        wiseLendingInstance.setPoolParameters(AWETH, 99e16, type(uint256).max); // increasw Wiselending coll factor
        vm.store(address(powerFarmManagerInstance), bytes32(uint(2)), bytes32(uint(99e16))); //increasw PowerFarm coll factor
        assertEq(powerFarmManagerInstance.collateralFactor(), 99e16);

        uint nftId = powerFarmManagerInstance.farmingKeys(keyID);
        uint borrowShares = wiseLendingInstance.getPositionBorrowShares(nftId, AWETH);
        // will revert if it can't be liquidated
        wiseSecurityInstance.checksLiquidation(nftId, AWETH, borrowShares);

        uint nftIdLiquidator = positionNftsInstance.mintPosition();

        vm.expectRevert(DebtRatioTooLow.selector);
        powerFarmManagerInstance.liquidatePartiallyFromToken(nftId, nftIdLiquidator, borrowShares );
    }

Recommended Mitigation Steps

Consider checking if a position is an Aave position using the keyId of the position.

PendlePowerFarm.sol#L64-L70

-       uint256 borrowShares = isAave[_nftId]
+       uint256 borrowShares = isAave[keyId]
            ? _getPositionBorrowSharesAave(
                _nftId
            )
            : _getPositionBorrowShares(
                _nftId
        );

PendlePowerFarm.sol#L127-L129

-       if (isAave[_nftId] == true) {
+       if (isAave[keyId] == true) {
            poolAddress = AAVE_WETH_ADDRESS;
        }

PendlePowerFarmMathLogic.sol#L396-L402

-        uint256 borrowShares = isAave[_nftId]
+        uint256 borrowShares = isAave[keyId]
            ? _getPositionBorrowSharesAave(
                _nftId
            )
            : _getPositionBorrowShares(
                _nftId
        );

PendlePowerFarmLeverageLogic.sol#L575-L590

-        address paybackToken = isAave[_nftId] == true
+        address paybackToken = isAave[keyId] == true
            ? AAVE_WETH_ADDRESS
            : WETH_ADDRESS;

        paybackAmount = WISE_LENDING.paybackAmount(
            paybackToken,
            _shareAmountToPay
        );

-       uint256 cutoffShares = isAave[_nftId] == true
+       uint256 cutoffShares = isAave[keyId] == true
            ? _getPositionBorrowSharesAave(_nftId)
                * FIVTY_PERCENT
                / PRECISION_FACTOR_E18
            : _getPositionBorrowShares(_nftId)
                * FIVTY_PERCENT
                / PRECISION_FACTOR_E18;

vm06007 (Wise Lending) commented:

While this is marked as High, we would like to bring this to a Medium, as there is still a way to liquidate such nft/user should that even occur (depending if there’s AavePool present etc).

@vonMangoldt can provide details on how this can be mitigated and user can still be liquidated even if they would have a chance to enter with the wrong isAave flag.

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

Impact

Malicious users could open Aave positions that cannot be liquidated, users cannot manually pay back Aave positions, and getLiveDebtRatio returns zero for Aave positions; however, the sponsor noted there is still a way to liquidate such positions, so the impact may be limited.

What the attacker needed

Aave position created; Price changes make position eligible

The fix

The code was fixed to use the keyId instead of the nftId when checking the isAave mapping.

How it was found

  1. Create an Aave PowerFarm position.
  2. Wait for price changes to make the position eligible for liquidation.
  3. Attempt to liquidate the position; the call reverts because the debt ratio check uses the wrong borrow shares.

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 this system, each farming position has two identifiers: a key ID for the PowerFarm and an NFT ID for the lending contract. The code mistakenly uses the NFT ID to look up whether the position is an Aave position, but that lookup table is keyed by the PowerFarm key ID. As a result, the contract thinks Aave positions are not Aave, so it fetches the wrong data and some actions fail.

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.