Skip to content
Business logic Audit finding Intermediate

StrategySupplyBase.undeploy does not return the amount of assets actually undeployed, which can cause a withdrawal to fail

The undeploy function returns the requested amount rather than the actual withdrawn amount, so a later transfer can exceed the balance and revert. This prevents users from withdrawing their tokens when the actual withdrawn amount is less than requested.

klau5 Follow

Originally disclosed on Code4rena

Submitted by klau5

https://github.com/code-423n4/2024-12-bakerfi/blob/0daf8a0547b6245faed5b6cd3f5daf44d2ea7c9a/contracts/core/strategies/StrategySupplyBase.sol#L128

Finding description and impact

The StrategySupplyBase.undeploy function returns the user’s requested amount instead of the actual withdrawn asset amount. When attempting to transfer more tokens than what was actually withdrawn, the transaction will revert, preventing users from withdrawing their tokens.

Proof of Concept

The StrategySupplyBase.undeploy function should return the actual amount of asset tokens withdrawn. However, it just returns the amount parameter requested by the user.

function undeploy(uint256 amount) external nonReentrant onlyOwner returns (uint256 undeployedAmount) {
    if (amount == 0) revert ZeroAmount();

    // Get Balance
    uint256 balance = getBalance();
    if (amount > balance) revert InsufficientBalance();

    // Transfer assets back to caller
@>  uint256 withdrawalValue = _undeploy(amount);

    // Check withdrawal value matches the initial amount
    // Transfer assets to user
@>  ERC20(_asset).safeTransfer(msg.sender, withdrawalValue);

    balance -= amount;
    emit StrategyUndeploy(msg.sender, withdrawalValue);
    emit StrategyAmountUpdate(balance);

@>  return amount;
}

The actual withdrawn asset amount may not match the user’s requested amount. For example, in StrategySupplyMorpho._undeploy, if the user requests more assets than what can be withdrawn from Morpho, it will only withdraw the maximum available amount and return that value. However, StrategySupplyBase.undeploy ignores this and returns the user’s requested amount.

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;
}

StrategySupplyBase._redeemInternal assumes that the amount returned by StrategySupplyBase.undeploy is the actual withdrawn token amount and attempts to transfer this to the user. However, this amount could be larger than what was actually withdrawn. Attempting to transfer more tokens will cause the transaction to revert, preventing users from withdrawing their tokens.

function _redeemInternal(
    uint256 shares,
    address receiver,
    address holder,
    bool shouldRedeemETH
) private returns (uint256 retAmount) {
    ...

@>  uint256 amount = _undeploy(withdrawAmount);
    ...

    // Calculate and handle withdrawal fees
    if (getWithdrawalFee() != 0 && getFeeReceiver() != address(0)) {
        fee = amount.mulDivUp(getWithdrawalFee(), PERCENTAGE_PRECISION);

        if (shouldRedeemETH && _asset() == wETHA()) {
            unwrapETH(amount);
@>          payable(receiver).sendValue(amount - fee);
@>          payable(getFeeReceiver()).sendValue(fee);
        } else {
@>          IERC20Upgradeable(_asset()).transfer(receiver, amount - fee);
@>          IERC20Upgradeable(_asset()).transfer(getFeeReceiver(), fee);
        }
    } else {
        if (shouldRedeemETH) {
            unwrapETH(amount);
@>          payable(receiver).sendValue(amount);
        } else {
@>          IERC20Upgradeable(_asset()).transfer(receiver, amount);
        }
    }

    emit Withdraw(msg.sender, receiver, holder, amount - fee, shares);
    retAmount = amount - fee;
}

Recommended Mitigation Steps

Use the actual amount of asset tokens withdrawn.

function undeploy(uint256 amount) external nonReentrant onlyOwner returns (uint256 undeployedAmount) {
    if (amount == 0) revert ZeroAmount();

    // Get Balance
    uint256 balance = getBalance();
    if (amount > balance) revert InsufficientBalance();

    // Transfer assets back to caller
    uint256 withdrawalValue = _undeploy(amount);

    // Check withdrawal value matches the initial amount
    // Transfer assets to user
    ERC20(_asset).safeTransfer(msg.sender, withdrawalValue);

-   balance -= amount;
+   balance -= withdrawalValue;
    emit StrategyUndeploy(msg.sender, withdrawalValue);
    emit StrategyAmountUpdate(balance);

-   return amount;
+   return withdrawalValue;
}

chefkenji (BakerFi) confirmed

BakerFi mitigated:

PR-28

Status: Unmitigated. Full details in the report from shaflow2, and also included in the Mitigation Review section below.


The original report as published on Code4rena, by klau5. 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

The undeploy function returns the input amount instead of the actual withdrawn amount returned by _undeploy.

Impact

Users may be unable to withdraw their tokens because the transaction reverts when the contract attempts to transfer more than was actually withdrawn.

What the attacker needed

User requests more than available in external protocol

The fix

The undeploy function should return the actual withdrawn amount (withdrawalValue) and update the balance accordingly.

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 function that handles withdrawals was returning the amount the user asked for, not the amount it actually got back from an external protocol. When the external protocol could only give less, the contract still tried to send the larger requested amount, which failed because there were not enough tokens. This meant users could not withdraw at all in that situation.

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 klau5

View all

21 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.