Skip to content
Business logic Audit finding Intermediate

Incorrect unwrapNativeTokenInWallet receiver address

The protocol's unwrapNativeTokenInWallet function attempts to pull wrapped native tokens from msg.sender instead of the specified receiver when a user requests native tokens as output. This causes withdrawals or borrows to fail when the caller and receiver differ, as the caller may not hold the wrapped tokens.

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/modules/BVault.sol#L74 https://github.com/code-423n4/2024-07-benddao/blob/main/src/modules/CrossLending.sol#L50 https://github.com/code-423n4/2024-07-benddao/blob/main/src/modules/IsolateLending.sol#L51

Impact

If the user specifies native tokens as output assets, the protocol will mistakenly try to unwrap wrapped tokens from the msg.sender instead of the receiver address that was specified in the function call.

Proof of Concept

This issue occurs in the following modules:

  • BVault
  • CrossLending
  • IsolateLending

Users are allowed to specify an output asset when borrowing/withdrawing funds from the protocol. If the output asset is a native token, a wrapped version will be sent to the receiver via the erc20TransferOutLiquidity function, and then the receiver of these tokens must send them to the protocol, which will unwrap and send them back:

  function unwrapNativeTokenInWallet(address wrappedNativeToken, address user, uint256 amount) internal {
    require(amount > 0, Errors.INVALID_AMOUNT);

    bool success = IWETH(wrappedNativeToken).transferFrom(user, address(this), amount);
    require(success, Errors.TOKEN_TRANSFER_FAILED);

    IWETH(wrappedNativeToken).withdraw(amount);

    safeTransferNativeToken(user, amount);
  }

The problem here is that the wrapped tokens will be sent to the params.receiver address, but the unwrapNativeTokenInWallet function will try to get them from msg.sender, who may not have them in the wallet. As a result withdraw/borrow functionality may fail if msg.sender != receiver:

  function withdrawERC20(
    uint32 poolId,
    address asset,
    uint256 amount,
    address onBehalf,
    address receiver
  ) public whenNotPaused nonReentrant {
    address msgSender = unpackTrailingParamMsgSender();
    DataTypes.PoolStorage storage ps = StorageSlot.getPoolStorage();
    bool isNative;
    if (asset == Constants.NATIVE_TOKEN_ADDRESS) {
      isNative = true;
      asset = ps.wrappedNativeToken;
    }

    SupplyLogic.executeWithdrawERC20(
      InputTypes.ExecuteWithdrawERC20Params({
        msgSender: msgSender,
        poolId: poolId,
        asset: asset,
        amount: amount,
        onBehalf: onBehalf,
>>      receiver: receiver
      })
    );

    if (isNative) {
>>    VaultLogic.unwrapNativeTokenInWallet(asset, msgSender, amount);
    }
  }

Same in crossBorrowERC20 and isolateBorrow functions.

Recommended Mitigation Steps

    if (isNative) {
-     VaultLogic.unwrapNativeTokenInWallet(asset, msgSender, amount);
+     VaultLogic.unwrapNativeTokenInWallet(asset, receiver, amount);
    }

Assessed type

Token-Transfer

thorseldon (BendDAO) confirmed and commented:

Fixed here.


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

The code passes msg.sender instead of the receiver address to unwrapNativeTokenInWallet, so it tries to transfer wrapped tokens from the caller rather than the intended recipient.

Impact

Withdraw or borrow functionality may fail when the caller and receiver are different addresses, preventing users from receiving native tokens.

What the attacker needed

Caller and receiver are different addresses; output asset is a native token

How it was found

  1. A user calls withdrawERC20 (or crossBorrowERC20/isolateBorrow) specifying a native token as the output asset and a receiver different from msg.sender.
  2. The protocol sends the wrapped native token to the receiver via erc20TransferOutLiquidity.
  3. The protocol then calls unwrapNativeTokenInWallet with msg.sender as the user parameter.
  4. unwrapNativeTokenInWallet attempts to transfer the wrapped tokens from msg.sender, who may not have them, causing the transaction to fail.

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 withdraw or borrow from a DeFi protocol, you can ask for the native token (like ETH). The protocol first sends you a wrapped version (like WETH) and then asks you to send it back so it can unwrap it. The bug is that it asks the wrong person (the caller, not the receiver) to send the wrapped tokens back, so if you're withdrawing on behalf of someone else, the transaction fails.

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.