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
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.
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
- Add a
revert()inleverageUpInternal()withinUSDOLeverageModule.sol#L197as follows, to simulate a permanent failure for the remote execution at destination chain.
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();
...
}
- Add the following
console.logto singularity.test.ts#L4113
console.log("USDO_10 balance for deployer.address (expected to be equal to 10000000000000000000) : ", await USDO_10.balanceOf(deployer.address));
- Run the test case
'should bounce between 2 chains'under'multiHopBuyCollateral()'tests insingularity.test.ts. It will show that thedeployer.addressfails 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
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
- Add a revert in leverageUpInternal to simulate a permanent failure.
- Add a console.log to check the deployer's balance.
- Run the test case 'should bounce between 2 chains' under 'multiHopBuyCollateral()' tests.
- 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.