Skip to content
Business logic Audit finding Intermediate

Layerzero fee refund address is not handled correctly

The LayerZero fee refund address is set to the intermediate contract instead of the original user, so overpaid fees are sent to the contract and the user loses the refund. An attacker could not directly exploit this, but users lose funds.

ladboy233 Follow

Originally disclosed on Code4rena

Submitted by ladboy233

The protocol aims to integrate with layerzero v2. To leverage layerzero infrastructure to send out cross-chain message, the user has to pay the native fee. If the user underpays the message fee, transaction reverts. If the user overpays the message fee, the excessive fee is refunded back.

However, in the current implementation, the refund address is not compose correctly.

In MagnetarBaseModule.sol, there is a function:

   function _lzCustomWithdraw(
        address _asset,
        LZSendParam memory _lzSendParam,
        uint128 _lzSendGas,
        uint128 _lzSendVal,
        uint128 _lzComposeGas,
        uint128 _lzComposeVal,
        uint16 _lzComposeMsgType
    ) private {
        PrepareLzCallReturn memory prepareLzCallReturn = _prepareLzSend(_asset, _lzSendParam, _lzSendGas, _lzSendVal);

        TapiocaOmnichainEngineHelper _toeHelper = new TapiocaOmnichainEngineHelper();
        PrepareLzCallReturn memory prepareLzCallReturn2 = _toeHelper.prepareLzCall(
            ITapiocaOmnichainEngine(_asset),
            PrepareLzCallData({
                dstEid: _lzSendParam.sendParam.dstEid,
                recipient: _lzSendParam.sendParam.to,
                amountToSendLD: 0,
                minAmountToCreditLD: 0,
                msgType: _lzComposeMsgType,
                composeMsgData: ComposeMsgData({
                    index: 0,
                    gas: _lzComposeGas,
                    value: prepareLzCallReturn.msgFee.nativeFee.toUint128(),
                    data: _lzSendParam.sendParam.composeMsg,
                    prevData: bytes(""),
                    prevOptionsData: bytes("")
                }),
                lzReceiveGas: _lzSendGas + _lzComposeGas,
                lzReceiveValue: _lzComposeVal
            })
        );

        if (msg.value < prepareLzCallReturn2.msgFee.nativeFee) {
            revert Magnetar_GasMismatch(prepareLzCallReturn2.msgFee.nativeFee, msg.value);
        }

        IOftSender(_asset).sendPacket{value: prepareLzCallReturn2.msgFee.nativeFee}(
            prepareLzCallReturn2.lzSendParam, prepareLzCallReturn2.composeMsg
        );
    }

First, we are creating a temp TapiocaOmnichainEngineHelper contract, then calling prepareLzCall to compose the data type. The function is long, but the important thing is that the refundAddress is set to address(msg.sender).

lzSendParam_ = LZSendParam({
    sendParam: sendParam_,
    fee: msgFee_,
    extraOptions: oftMsgOptions_,
    refundAddress: address(msg.sender)
});

prepareLzCallReturn_ = PrepareLzCallReturn({
    composeMsg: composeMsg_,
    composeOptions: composeOptions_,
    sendParam: sendParam_,
    msgFee: msgFee_,
    lzSendParam: lzSendParam_,
    oftMsgOptions: oftMsgOptions_
});

Who is msg.sender?

In this case:

  • User calls contract A,
  • Contract A creates contract B,
  • Contract A calls contract B prepareLzCall method.
  • Inside the prepareLzCall function call, msg.sender will be address contract A.

However, that is not what we want. The refunded fee should go to original msg.sender who triggered the withdraw and paid the native fee.

A simple POC can be ran:

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import "forge-std/Test.sol";
import "forge-std/console.sol";

contract Helper {
    function prepare() public returns (address) {
        return msg.sender;
    }
}

contract Magnetar {

    function compose() public returns (address) {
        Helper help = new Helper();
        return help.prepare();
    }
}

contract CounterTest is Test {

    using stdStorage for StdStorage;
    StdStorage stdlib;

    function setUp() public {

    }

    function testWhoSend() public {

        address user = vm.addr(1234);

        vm.startPrank(user);

        Magnetar mag = new Magnetar();

        address refundAddress = mag.compose();

        console.log("refund address is user", refundAddress == user);
        console.log("refund address is contract", refundAddress == address(mag));
    }

}

If we run the code with:

forge test -vv

We are getting:

Running 1 test for test/Counter.t.sol:CounterTest
[PASS] testWhoSend() (gas: 196196)
Logs:
  refund address is user false
  refund address is contract true

Then, because the refund address is incorrectly set to contract, the original user lose refund layerzero fee. Consider that users always need to overpay the fee to send messsage. The cumulative loss of refund fee will be high and make the user lose fees.

Recommended Mitigation Steps

Set the layerzero refund address to a user input address:

lzSendParam_ = LZSendParam({
    sendParam: sendParam_,
    fee: msgFee_,
    extraOptions: oftMsgOptions_,
    refundAddress: refundAddress // change here
});

prepareLzCallReturn_ = PrepareLzCallReturn({
    composeMsg: composeMsg_,
    composeOptions: composeOptions_,
    sendParam: sendParam_,
    msgFee: msgFee_,
    lzSendParam: lzSendParam_,
    oftMsgOptions: oftMsgOptions_
});

Assessed type

Token-Transfer

0xRektora (Tapioca) confirmed

cryptotechmaker (Tapioca) commented:

Main PR here.

Secondary PRs here, here and here.


The original report as published on Code4rena, by ladboy233. 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 refund address is set to msg.sender inside a helper contract, which is the calling contract, not the original user.

Impact

Users lose the overpaid LayerZero fee because the refund is sent to the contract instead of them; the cumulative loss could be high.

What the attacker needed

User overpays the LayerZero fee

The fix

The refund address should be set to a user-supplied address instead of msg.sender.

How it was found

  1. User calls contract A (Magnetar).
  2. Contract A creates contract B (TapiocaOmnichainEngineHelper).
  3. Contract A calls contract B's prepareLzCall method.
  4. Inside prepareLzCall, msg.sender is contract A, so refundAddress is set to contract A.
  5. The refund goes to contract A instead of the user.

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 send a cross-chain message, you pay a fee upfront. If you pay too much, the extra should be returned to you. Here, the extra is sent to a temporary contract instead of you, so you lose that money.

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 ladboy233

View all

47 more disclosed reports, best first.

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