Skip to content
Business logic Audit finding Intermediate

Updating fee factor may create issues for the protocol

When the protocol fee factor is updated, the new fee is applied to interest accrued before the change, and the supply rate still reflects the old fee factor. This can cause inconsistencies in fee accounting for the protocol.

SpicyMeatball Follow with oakcobalt

Originally disclosed on Code4rena

Submitted by SpicyMeatball, also found by oakcobalt

https://github.com/code-423n4/2024-07-benddao/blob/main/src/libraries/logic/ConfigureLogic.sol#L509-L521 https://github.com/code-423n4/2024-07-benddao/blob/117ef61967d4b318fc65170061c9577e674fffa1/src/libraries/logic/InterestLogic.sol#L248 https://github.com/code-423n4/2024-07-benddao/blob/117ef61967d4b318fc65170061c9577e674fffa1/src/libraries/logic/InterestLogic.sol#L201-L202

Impact

When the protocol fee is modified in the Configurator module:

  • The new fee percentage will be applied to the interest collected before the fee change.
  • The protocol will use the supply rate that was calculated using the old fee factor.

Proof of Concept

The feeFactor parameter is used to calculate amount of interest that goes to the treasury:

  function _accrueFeeToTreasury(
    DataTypes.AssetData storage assetData,
    DataTypes.GroupData storage groupData,
    uint256 prevGroupBorrowIndex
  ) internal {
    AccrueToTreasuryLocalVars memory vars;

    if (assetData.feeFactor == 0) {
      return;
    }

    vars.totalScaledBorrow = groupData.totalScaledCrossBorrow + groupData.totalScaledIsolateBorrow;

    //calculate the total debt at moment of the last interaction
    vars.prevTotalBorrow = vars.totalScaledBorrow.rayMul(prevGroupBorrowIndex);

    //calculate the new total debt after accumulation of the interest on the index
    vars.currTotalBorrow = vars.totalScaledBorrow.rayMul(groupData.borrowIndex);

    //debt accrued is the sum of the current debt minus the sum of the debt at the last update
    vars.totalDebtAccrued = vars.currTotalBorrow - vars.prevTotalBorrow;

>>  vars.amountToMint = vars.totalDebtAccrued.percentMul(assetData.feeFactor);

    if (vars.amountToMint != 0) {
      assetData.accruedFee += vars.amountToMint.rayDiv(assetData.supplyIndex).toUint128();
    }

To adjust the asset’s supply rate taking into account fee collected by the treasury:

  function updateInterestRates(
    DataTypes.PoolData storage poolData,
    DataTypes.AssetData storage assetData,
    uint256 liquidityAdded,
    uint256 liquidityTaken
  ) internal {
    ---SNIP---
    // calculate the asset supply rate
    vars.nextAssetSupplyRate = vars.nextAssetBorrowRate.rayMul(vars.assetUtilizationRate);
    vars.nextAssetSupplyRate = vars.nextAssetSupplyRate.percentMul(
      PercentageMath.PERCENTAGE_FACTOR - assetData.feeFactor
    );
>>  assetData.supplyRate = vars.nextAssetSupplyRate.toUint128();

A discrepancy may occur when the pool admin changes the feeFactor. The new feeFactor will be applied to interest collected before the feeFactor was updated:

function updateInterestIndexs(
    DataTypes.PoolData storage /*poolData*/,
    DataTypes.AssetData storage assetData
  ) internal {
    ---SNIP---
    for (vars.i = 0; vars.i < vars.assetGroupIds.length; vars.i++) {
      vars.loopGroupId = uint8(vars.assetGroupIds[vars.i]);
      DataTypes.GroupData storage loopGroupData = assetData.groupLookup[vars.loopGroupId];
      vars.prevGroupBorrowIndex = loopGroupData.borrowIndex;
      _updateBorrowIndex(assetData, loopGroupData);
>>    _accrueFeeToTreasury(assetData, loopGroupData, vars.prevGroupBorrowIndex);
    }

    // save updating time
    assetData.lastUpdateTimestamp = uint40(block.timestamp);
  }

But the protocol will still use the old supply rate that was calculated using the previous feeFactor. This may cause inconsistencies in the fee accounting for the protocol.

Recommended Mitigation Steps

  function executeSetAssetProtocolFee(address msgSender, uint32 poolId, address asset, uint16 feeFactor) internal {

+   InterestLogic.updateInterestIndexs(poolData, assetData);
    assetData.feeFactor = feeFactor;
+   InterestLogic.updateInterestRates(poolData, assetData, 0, 0);

    emit Events.SetAssetProtocolFee(poolId, asset, feeFactor);
  }

thorseldon (BendDAO) confirmed and commented:

Fixed here.

We suggest adjust the severity level to Low Risk or Information, because there’s many TX action will frequently trigger to update the interest index, e.g., borrow/repay. We check the same logic exist in Aave V2 & V3.

0xTheC0der (judge) commented:

Thanks for that additional clarification about the likelihood of this being an issue.

Due to the possibility of fixing this and avoiding even temporary impacts and in consistency with other parameter change relates issues (see Issue #8 and Issue #26), I’d like to maintain Medium severity.


The original report as published on Code4rena, by SpicyMeatball. 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 fee factor is updated without first accruing interest and updating the supply rate, so the new fee applies to past interest and the old supply rate remains in use.

Impact

This may cause inconsistencies in the fee accounting for the protocol, though frequent interest updates from other actions reduce the likelihood of lasting impact.

What the attacker needed

Pool admin changes fee factor

The fix

The fee update function should first accrue interest and then update the interest rates after changing the fee factor; the project confirmed it was fixed.

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 lets people lend and borrow digital tokens. The protocol takes a small cut of the interest, called a fee. If the fee percentage is changed, the new percentage is used for interest that was earned before the change, and the interest rate paid to lenders is not updated immediately. This can make the accounting wrong for a short time.

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 SpicyMeatball

View all

18 more disclosed reports, best first.

More from BendDAO Invitational

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.