Skip to content
Smart contracts Audit finding Intermediate

Refund mechanism for failed cross-chain transactions does not work

The refund mechanism for failed cross-chain transactions is ineffective because the revert that forwards the error also reverses the refund transfer. An attacker could permanently lock bridged funds if a cross-chain execution fails with a permanent error.

peakbolt Follow with Kaysoft, windhustler and 2 others

Originally disclosed on Code4rena

Submitted by peakbolt, also found by Kaysoft, windhustler, carrotsmuggler (1, 2), xuwinnie, and cergyk

https://github.com/Tapioca-DAO/tapioca-bar-audit/blob/2286f80f928f41c8bc189d0657d74ba83286c668/contracts/usd0/modules/USDOLeverageModule.sol#L180-L185

https://github.com/Tapioca-DAO/tapioca-bar-audit/blob/2286f80f928f41c8bc189d0657d74ba83286c668/contracts/usd0/modules/USDOMarketModule.sol#L178-L186

https://github.com/Tapioca-DAO/tapioca-bar-audit/blob/2286f80f928f41c8bc189d0657d74ba83286c668/contracts/usd0/modules/USDOOptionsModule.sol#L187-L197

https://github.com/Tapioca-DAO/tapiocaz-audit/blob/bcf61f79464cfdc0484aa272f9f6e28d5de36a8f/contracts/tOFT/modules/BaseTOFTLeverageModule.sol#L195-L200

https://github.com/Tapioca-DAO/tapiocaz-audit/blob/bcf61f79464cfdc0484aa272f9f6e28d5de36a8f/contracts/tOFT/modules/BaseTOFTMarketModule.sol#L170-L175

https://github.com/Tapioca-DAO/tapiocaz-audit/blob/bcf61f79464cfdc0484aa272f9f6e28d5de36a8f/contracts/tOFT/modules/BaseTOFTOptionsModule.sol#L202-L212

https://github.com/Tapioca-DAO/tapiocaz-audit/blob/bcf61f79464cfdc0484aa272f9f6e28d5de36a8f/contracts/tOFT/modules/BaseTOFTStrategyModule.sol#L163-L168

There is a refund mechanism in USDO and TOFT modules that will return funds when the execution on the destination chain fails.

It happens when module.delegatecall() fails, where the following code (see below) will trigger a refund of the bridged fund to the user. After that a revert is then ‘forwarded’ to the main executor contract (BaseUSDO or BaseTOFT).

However, the issue is that the revert will also reverse the refund even when the revert is forwarded.

https://github.com/Tapioca-DAO/tapioca-bar-audit/blob/2286f80f928f41c8bc189d0657d74ba83286c668/contracts/usd0/modules/USDOLeverageModule.sol#L180-L185

        if (!success) {
            if (balanceAfter - balanceBefore >= amount) {
                IERC20(address(this)).safeTransfer(leverageFor, amount);
            }

            //@audit - this revert will actually reverse the refund before this
            revert(_getRevertMsg(reason)); //forward revert because it's handled by the main executor
        }

Although the main executor contract will _storeFailedMessage() to allow users to retryMessage() and re-execute the failed transaction, it will not go through if the error is permanent. That means the retryMessage() will also revert and there is no way to recover the funds.

Impact

User will lose their bridged fund if the cross chain execution encounters a permanent error, which will permanently lock up the bridged funds in the contract as there is no way to recover it.

Proof of Concept

  1. Add a revert() in leverageUpInternal() within USDOLeverageModule.sol#L197 as follows, to simulate a permanent failure for the remote execution at destination chain.

https://github.com/Tapioca-DAO/tapioca-bar-audit/blob/2286f80f928f41c8bc189d0657d74ba83286c668/contracts/usd0/modules/USDOLeverageModule.sol#L197.

function leverageUpInternal(
        uint256 amount,
        IUSDOBase.ILeverageSwapData memory swapData,
        IUSDOBase.ILeverageExternalContractsData memory externalData,
        IUSDOBase.ILeverageLZData memory lzData,
        address leverageFor
    ) public payable {

        //@audit - to simulate a permanent failure for this remote execution (e.g. issue with swap)
        revert();

        ...
    }
  1. Add the following console.log to singularity.test.ts#L4113
            console.log("USDO_10 balance for deployer.address (expected to be equal to 10000000000000000000) : ", await USDO_10.balanceOf(deployer.address));
  1. Run the test case 'should bounce between 2 chains' under 'multiHopBuyCollateral()' tests in singularity.test.ts. It will show that the deployer.address fails to receive the refund amount.

Recommended Mitigation Steps

Implement a ‘pull’ mechanism for users to withdraw the refund instead of ‘pushing’ to the user.

That can be done by using a a new state variable within USDO and TOFT to store the refund amount for the transaction with the corresponding payloadHash for failedMessages mapping.

Checks must be implemented to ensure that if user withdraws the refund, the corresponding failedMessages entry is cleared so that the user cannot retry the transaction again.

Similarly, if retryMessage() is used to re-execute the transaction successfully, the refund amount in the new state variable should be cleared.

0xRektora (Tapioca) confirmed via duplicate issue #1410


The original report as published on Code4rena, by peakbolt. 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 refund transfer is executed before a revert that is forwarded to the main executor, causing the refund to be rolled back along with the revert.

Impact

User will lose their bridged fund if the cross chain execution encounters a permanent error, which will permanently lock up the bridged funds in the contract as there is no way to recover it.

What the attacker needed

Cross-chain execution fails permanently

The fix

Implement a 'pull' mechanism for users to withdraw the refund instead of 'pushing' to the user, with state variables to track refunds and clear failed messages upon withdrawal or successful retry.

How it was found

  1. Add a revert in leverageUpInternal to simulate a permanent failure.
  2. Add a console.log to check the deployer's balance.
  3. Run the test case 'should bounce between 2 chains' under 'multiHopBuyCollateral()' tests.
  4. Observe that the deployer fails to receive the refund amount.

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 a cross-chain transaction fails, the system tries to refund the user's funds. But the code also reverts the transaction, which undoes the refund. So the user loses their funds permanently.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by peakbolt

View all

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

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.