Skip to content
Business logic Audit finding Advanced

Attacker can force the failure of transactions that use tryCatch

The tryCatch function in the AmbireAccount contract forwards all remaining gas to an external call, so a malicious relayer can choose a gas limit that makes the inner call run out of gas while the outer transaction still succeeds. This forces the failure of transactions that were meant to tolerate inner call failures, and the nonce is still incremented.

adriro Follow

Originally disclosed on Code4rena

Submitted by adriro

An attacker, or malicious relayer, can force the failure of transactions that rely on tryCatch() by carefully choosing the gas limit.

The tryCatch() function present in the AmbireAccount contract can be used to execute a call in the context of a wallet, which is eventually allowed to fail; i.e. the operation doesn’t revert if the call fails.

119: 	function tryCatch(address to, uint256 value, bytes calldata data) external payable {
120: 		require(msg.sender == address(this), 'ONLY_IDENTITY_CAN_CALL');
121: 		(bool success, bytes memory returnData) = to.call{ value: value, gas: gasleft() }(data);
122: 		if (!success) emit LogErr(to, value, data, returnData);
123: 	}

EIP-150 introduces the “rule of 1/64th” in which 1/64th of the available gas is reserved in the calling context and the rest of it is forward to the external call. This means that, potentially, the called function can run out of gas, while the calling context may have some gas to eventually continue and finish execution successfully.

A malicious relayer, or a malicious actor that front-runs the transaction, can carefully choose the gas limit to make the call to tryCatch() fail due out of gas, while still saving some gas in the main context to continue execution. Even if the underlying call in tryCatch() would succeed, an attacker can force its failure while the main call to the wallet is successfully executed.

Proof of Concept

The following test reproduces the attack. The user creates a transaction to execute a call using tryCatch() to a function of the TestTryCatch contract, which simulates some operations that consume gas. The attacker then executes the bundle by carefully choosing the gas limit (450,000 units of gas in this case) so that the call to TestTryCatch fails due to out of gas, but the main call to execute() in the wallet (here simplified by using executeBySender() to avoid signatures) gets correctly executed.

Note: the snippet shows only the relevant code for the test. Full test file can be found here.

contract TestTryCatch {
    uint256[20] public foo;

    function test() external {
        // simulate expensive operation
        for (uint256 index = 0; index < 20; index++) {
            foo[index] = index + 1;
        }
    }
}

function test_AmbireAccount_ForceFailTryCatch() public {
    address user = makeAddr("user");

    address[] memory addrs = new address[](1);
    addrs[0] = user;
    AmbireAccount account = new AmbireAccount(addrs);

    TestTryCatch testTryCatch = new TestTryCatch();

    AmbireAccount.Transaction[] memory txns = new AmbireAccount.Transaction[](1);
    txns[0].to = address(account);
    txns[0].value = 0;
    txns[0].data = abi.encodeWithSelector(
        AmbireAccount.tryCatch.selector,
        address(testTryCatch),
        uint256(0),
        abi.encodeWithSelector(TestTryCatch.test.selector)
    );

    // This should actually be a call to "execute", we simplify the case using "executeBySender"
    // to avoid the complexity of providing a signature. Core issue remains the same.
    vm.expectEmit(true, false, false, false);
    emit LogErr(address(testTryCatch), 0, "", "");
    vm.prank(user);
    account.executeBySender{gas: 450_000}(txns);

    // assert call to TestTryCatch failed
    assertEq(testTryCatch.foo(0), 0);
}

Recommendation

The context that calls in tryCatch(), can check the remaining gas after the call to determine if the remaining amount is greater than 1/64 of the available gas, before the external call.

  function tryCatch(address to, uint256 value, bytes calldata data) external payable {
      require(msg.sender == address(this), 'ONLY_IDENTITY_CAN_CALL');
+     uint256 gasBefore = gasleft();
      (bool success, bytes memory returnData) = to.call{ value: value, gas: gasleft() }(data);
+     require(gasleft() > gasBefore/64);
      if (!success) emit LogErr(to, value, data, returnData);
  }

Ivshti (Ambire) commented:

@Picodes - we tend to disagree with the severity here. Gas attacks are possible in almost all cases of using Ambire accounts through a relayer. It’s an inherent design compromise of ERC-4337 as well, and the only way to counter it is with appropriate offchain checks/reputation systems and griefing protections.

Also, the solution seems too finicky. What if the tryCatch is called within execute (which it very likely will), which requires even more gas left to complete? Then 1) the solution won’t be reliable 2) the attacker can make the attack anyway through execute

Picodes (judge) commented:

The main issue here is, the nonce is incremented, despite the fact the transaction wasn’t executed as intended, which would force the user to resign the payload and would be a griefing attack against the user. I do break an important invariant, which if the nonce is incremented, the transaction signed by the user was included as he intended.

Also, I think this can be used within tryCatchLimit to pass a lower gasLimit: quoting EIP150: “If a call asks for more gas than the maximum allowed amount (i.e. the total amount of gas remaining in the parent after subtracting the gas cost of the call and memory expansion), do not return an OOG error; instead, if a call asks for more gas than all but one 64th of the maximum allowed amount, call with all but one 64th of the maximum allowed amount of gas (this is equivalent to a version of EIP-901 plus EIP-1142).”

Ivshti (Ambire) commented:

@Picodes - I’m not sure I understand. The whole point of signing something that calls into tryCatch is that you don’t care about the case where the nonce is incremented, but the transaction is failing. What am I missing?

Picodes (judge) commented:

The whole point of signing something that calls into tryCatch is that you don’t care about the case where the nonce is incremented but the transaction is failing

You don’t care if the transactions fail because the sub-call is invalid, but you do if it’s because the relayer manipulated the gas, right?

Ivshti (Ambire) commented:

@Picodes - Ok, I see the point here - probably repeating stuff that others said before, but trying to simplify. The relayer can rug users by taking their fee, regardless of the fact that the inner transactions fail, due to the relayer using a lower gasLimit. This would be possible if some of the sub-transactions use tryCatch, but the fee payment does not.

However, I’m not sure how the mitigation would work. Can the relayer still calculate a “right” gas limit for which the tryCatch will fail, but the rest will succeed?

Picodes (judge) commented:

My understanding is that using gasleft() > gasBefore/64, we know for sure than the inner call didn’t fail due to an out of gas, as it was called with 63*gasBefore/64. So the relayer has to give enough gas for every subcall to execute fully, whether it is successful or not.

Ivshti (Ambire) commented:

I see, this sounds reasonable. I need a bit more time to think about it and if it is, we’ll apply this mitigation.

Ambire mitigated:

Check gasleft to prevent this attack.

Status: Not fully mitigated. Full details in reports from adriro and carlitox477, and also included in the Mitigation Review section below.


The original report as published on Code4rena, by adriro. 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 tryCatch function forwards all available gas to the external call without checking afterwards whether the call failed because it ran out of gas, so the EIP-150 1/64th gas reservation lets the outer context survive an inner out-of-gas failure.

Impact

A malicious relayer could take the user's fee while forcing the inner tryCatch call to fail, and the nonce is still incremented, so the user must re-sign the payload; the report notes this is a griefing attack and the mitigation was not fully effective.

What the attacker needed

A smart contract wallet using tryCatch; a malicious relayer or front-runner; control over the transaction gas limit

The fix

The tryCatch function should record the gas before the call and require that the remaining gas after the call is greater than one sixty-fourth of that amount, so an inner out-of-gas failure cannot be hidden; the project applied this mitigation but it was not fully effective.

How it was found

  1. The user creates a transaction that calls tryCatch to execute an external function.
  2. The attacker or malicious relayer chooses a gas limit for the bundle.
  3. The gas limit is low enough that the inner call runs out of gas, but high enough that the main execute call still succeeds.
  4. The inner call fails and emits LogErr, while the wallet transaction completes and the nonce is incremented.

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 wallet lets a user bundle several actions into one transaction. One action type, tryCatch, is supposed to run an external call and continue even if that call fails. Because of an Ethereum rule that reserves a small portion of gas for the caller, a malicious relayer can pick a gas limit that starves only the inner call, making it fail while the rest of the transaction still completes.

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 adriro

View all

57 more disclosed reports, best first.

More from Ambire Wallet - 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.