Skip to content
Business logic Audit finding Intermediate

The calculation of assetsMax is incorrect.

The _undeploy function calculates assetsMax using stale totalSupplyAssets and totalSupplyShares from Morpho storage, ignoring accrued interest. This underestimates assetsMax, so a user withdrawing an amount that includes interest may trigger the branch that withdraws all shares, taking other users' assets.

shaflow2 Follow

Originally disclosed on Code4rena

Submitted by shaflow2

https://github.com/code-423n4/2024-12-bakerfi/blob/3873b82ae8b321473f3afaf08727e97be0635be9/contracts/core/strategies/StrategySupplyMorpho.sol#L78

Finding description and impact

In the _undeploy function, assetsMax is incorrectly calculated because the contract directly retrieves totalSupplyAssets and totalSupplyShares from _morpho storage without accounting for the accrued interest over time. This leads to an underestimation of assetsMax, which may allow users to withdraw more assets than they should, causing losses to other users.

Proof of Concept

    /// @notice Retrieves the current balance of the managed asset in the Morpho protocol.
    /// @return The current balance of the managed asset.
    function _getBalance() internal view override returns (uint256) {
        return _morpho.expectedSupplyAssets(_marketParams, address(this));
    }

In the StrategySupplyMorpho contract, when retrieving assets, the expectedSupplyAssets function is used, which considers accrued interest and fees from the elapsed time since the last update. This ensures that the withdraw and redeem functions calculate assets, including unaccounted interest.

    function _undeploy(uint256 amount) internal override returns (uint256) {
        Id id = _marketParams.id();
        uint256 assetsWithdrawn = 0;
        uint256 totalSupplyAssets = _morpho.totalSupplyAssets(id);
        uint256 totalSupplyShares = _morpho.totalSupplyShares(id);

        uint256 shares = _morpho.supplyShares(id, address(this));
        uint256 assetsMax = shares.toAssetsDown(totalSupplyAssets, totalSupplyShares);

        if (amount >= assetsMax) {
            (assetsWithdrawn, ) = _morpho.withdraw(_marketParams, 0, shares, address(this), address(this));
        } else {
            (assetsWithdrawn, ) = _morpho.withdraw(_marketParams, amount, 0, address(this), address(this));
        }

        return assetsWithdrawn;
    }

However, in the _undeploy function, the calculation of assetsMax does not account for the accrued interest and fees over time. This may result in assetsMax being underestimated compared to its actual value. On the other hand, the amount parameter includes accrued interest and fees, which can lead to the function entering the wrong branch. If the function mistakenly enters the second branch, it may incorrectly convert all _morpho shares in the strategy to assets and send them to the withdrawer. In this case, the strategy’s assets will be 0, but the vault shares will still remain in the vault. The remaining shareholders in the vault will not be able to normally claim assets.

Example:

  1. Initial Deposits: user1 deposits 1e16 assets into the vault and receives 1e16 shares. user2 deposits 1e20 assets and receives 1e20 shares.

  2. Interest Accumulation: After some time, the _morpho strategy generates interest. The vault’s totalAssets() now returns 1e20 + 1e16 + 1e17 assets.

  3. Redeem by user2: user2 decides to redeem all their shares. When calculating withdrawAmount = (shares * totalAssets()) / totalSupply(), totalAssets() includes the interest. The calculated withdrawAmount is passed to the _undeploy function. In _undeploy, the maximum amount of assets that can be converted from the current _morpho shares (assetsMax) is calculated. However, since the calculation of assetsMax does not account for the interest, assetsMax < withdrawAmount.

    As a result, the strategy withdraws all _morpho shares, converts them into assets, and sends them to user2. This means user2 inadvertently receives both their own principal and interest as well as user1’s principal and interest.

  4. Abnormal State: Now, the strategy holds no _morpho shares, so totalAssets() returns 0. However, the vault still has user1’s 1e16 shares. This creates an abnormal state in the vault.

Recommended mitigation steps

When calculating assetsMax, consider the accrued interest and fees that have not been updated.

    function _undeploy(uint256 amount) internal override returns (uint256) {
        Id id = _marketParams.id();
        uint256 assetsWithdrawn = 0;
-       uint256 totalSupplyAssets = _morpho.totalSupplyAssets(id);
-       uint256 totalSupplyShares = _morpho.totalSupplyShares(id);

-       uint256 shares = _morpho.supplyShares(id, address(this));
-       uint256 assetsMax = shares.toAssetsDown(totalSupplyAssets, totalSupplyShares);
+       uint256 assetsMax = _morpho.expectedSupplyAssets(_marketParams, address(this));

        if (amount >= assetsMax) {
            (assetsWithdrawn, ) = _morpho.withdraw(_marketParams, 0, shares, address(this), address(this));
        } else {
            (assetsWithdrawn, ) = _morpho.withdraw(_marketParams, amount, 0, address(this), address(this));
        }

        return assetsWithdrawn;
    }

chefkenji (BakerFi) confirmed

BakerFi mitigated:

PR-22

Status: Mitigation confirmed. Full details in reports from 0xlemon and shaflow2.


The original report as published on Code4rena, by shaflow2. 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 contract uses raw totalSupplyAssets and totalSupplyShares from Morpho storage instead of the interest-adjusted expectedSupplyAssets when computing the maximum withdrawable assets.

Impact

A user could withdraw more assets than they should, causing losses to other users and leaving the vault in an abnormal state where remaining shareholders cannot normally claim assets.

What the attacker needed

Users have deposited into the vault; interest has accrued in Morpho; a user redeems all their shares

The fix

The calculation of assetsMax should use expectedSupplyAssets to include accrued interest and fees, and the project mitigated this via a pull request.

How it was found

  1. user1 deposits 1e16 assets and receives 1e16 shares; user2 deposits 1e20 assets and receives 1e20 shares.
  2. After some time, the Morpho strategy generates interest, so the vault's totalAssets() becomes 1e20 + 1e16 + 1e17.
  3. user2 redeems all shares; withdrawAmount is calculated using totalAssets() including interest.
  4. In _undeploy, assetsMax is computed without interest, so assetsMax < withdrawAmount.
  5. The strategy withdraws all Morpho shares, converts them to assets, and sends them to user2.
  6. The strategy holds no Morpho shares, totalAssets() returns 0, but user1's shares remain, creating an abnormal state.

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 deposit money and earn interest. When one person withdraws, the contract miscalculates how much their shares are worth because it forgets about interest earned over time. This can let that person take more than their fair share, leaving others unable to get their money back.

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 shaflow2

View all

12 more disclosed reports, best first.

More from BakerFi 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.