handle_l1_message may unfairly revert l2 tx with sufficient l1 sender balance, due to vulnerable fee charge implementation
The handle_l1_message function hardcodes a very high gas limit and gas price, so it deducts a large max_fee from the L1 sender's cached balance before checking if the actual transfer can succeed. This can cause a valid L2 transaction to revert with an exceptional halt and charge the full max_fee, even when the sender had enough balance for the transfer and actual fee.
oakcobalt Follow with muellerberndt and RadiantLabs
Originally disclosed on Code4rena
Submitted by oakcobalt, also found by muellerberndt and RadiantLabs
handle_l1_message can only be invoked by starknet os(@l1_handler) and is not a regular user invoked transaction from eth_send_raw_unsigned_tx flow.
kakarot::handle_l1_message -> library:[handle_l1_message -> Interpreter::execute)
handle_l1_message hardcodes EVM gaslimit(2100000000) and gasprice(1) for every L1->L2 message regardless of the complexity of the actual l2 tx. In interpreter::execute, L1sender’s cached balance will be subtracted with the max_fee (2100000000 x 1) first before performing ETH transfer or running EVM.
Case: L1 sender performs minimal operations on L2:
For an L1 sender who only transfers some ETH to a L2 address or perform simple opcodes, the actual gas cost (required_gas) may be very close to the intrinsic gas cost(21000). This means the actual_fee l1sender is required to pay is around 21000 x 1, which is far less than the calculated max_fee 2100000000 x 1.
In this case, interpreter::execute will first subtract max_fee(2100000000) from L1sender’s cached balance (Account.set_balance(sender, &new_balance)). Note that this cached balance subtraction is done before ETH value transfer and run(evm), which means any subsequent logic will be using L1sender’s new_balance (e.g., X - 2100000000).
//src/kakarot/interpreter.cairo
func execute{
...
}(
env: model.Environment*,
address: model.Address*,
is_deploy_tx: felt,
bytecode_len: felt,
bytecode: felt*,
calldata_len: felt,
calldata: felt*,
value: Uint256*,
gas_limit: felt,
access_list_len: felt,
access_list: felt*,
) -> (model.EVM*, model.Stack*, model.Memory*, model.State*, felt, felt) {
...
|> let max_fee = gas_limit * env.gas_price;
let (fee_high, fee_low) = split_felt(max_fee);
let max_fee_u256 = Uint256(low=fee_low, high=fee_high);
with state {
let sender = State.get_account(env.origin);
//@audit L1->L2 flow: L1 sender's cached is first subtracted with max_fee based on hardcoded values, regardless of the actual fee required based on L2tx's complexity
|> let (local new_balance) = uint256_sub([sender.balance], max_fee_u256);
let sender = Account.set_balance(sender, &new_balance);
...
let transfer = model.Transfer(sender.address, address, [value]);
let success = State.add_transfer(transfer);
...
if (success == 0) {
let (revert_reason_len, revert_reason) = Errors.balanceError();
tempvar evm = EVM.stop(evm, revert_reason_len, revert_reason, Errors.EXCEPTIONAL_HALT);
} else {
tempvar evm = evm;
}
with stack, memory, state {
let evm = run(evm);
}
let required_gas = gas_limit - evm.gas_left;
...
let actual_fee = total_gas_used * env.gas_price;
let (fee_high, fee_low) = split_felt(actual_fee);
let actual_fee_u256 = Uint256(low=fee_low, high=fee_high);
let transfer = model.Transfer(sender.address, coinbase.address, actual_fee_u256);
with state {
State.add_transfer(transfer);
State.finalize();
}
return (evm, stack, memory, state, total_gas_used, required_gas);
POC
Suppose L1sender’s current balance is (2100000000 + 21000). And L1sender wants to transfer 2100000000 to an L2 address. Suppose the required_gas is only 21000 so that L1sender’s current balance is sufficient.
- In
interpreter::execute,max_fee(2100000000) is first subtracted fromL1sender’s cached balance.new_balance = 2100. - ETH
value(2100000000) transfer logic is performed based on the cached new balance. (let success = State.add_transfer(transfer)) Because in the cached state2100 - 2100000000 < 0,success -> 0. success == 0logic is executed, EVM tx reverts with anEXCEPTIONAL_HALT.- Due to EVM reverted with
exceptional_halt, total gas is consumed,max fee 2100000000is charged.
The above shows that despite L1sender having enough balance to perform the L2 tx, L1sender’s tx is reverted with max fee 2100000000 charged. L1sender only has 21000 left after the tx and no transfer is done.
Impact
L1sender’s L2 tx can be unfairly reverted when they have enough balance to cover transfer value and the actual fee.
Because handle_l1_message is not invoked by RPC endpoint users, L1sender doesn’t have a chance to specify max_fee value. In such cases, L1sender might only reasonably expect to have the EVM gas cost sufficient based on the complexity of the L2 tx, which can be much lower than max_fee. Although max_fee is marginal in ETH value, L1sender’s L2 tx can still fail due to the sequence of fee deduction in the cached state. The failure is not the L1sender’s fault.
Recommended Mitigation Steps
Because handle_l1_message is different from eth_send_raw_unsigned_tx in its invocation and max_fee setting, consider allowing L1sender to input gas_limit from L1, and decode the user input gas_limit in handle_l1_message to compute max_fee. Or, refactor the control flow to skip max fee deduction when called from handle_l1_message.
ClementWalter (Kakarot) confirmed and commented:
Severity: Medium Ok, the gas price for
handle_l1_messageshould be0.
Kakarot mitigated:
PR 1584 fix:
handle_l1_messagegas price is set to0; superseded by PR 1623 fix: remove messaging and PR 12 fix: remove messaging
Status: Mitigation error. Full details in reports from RadiantLabs included in the Mitigation Review section below.
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 hardcoded gas limit and gas price in handle_l1_message cause an excessive max_fee to be subtracted from the sender's cached balance before the transfer is validated, leading to a revert when the remaining balance is insufficient for the transfer value.
What the attacker needed
L1 sender sends a message to L2; L1 sender has enough balance for transfer and actual fee
The fix
The gas price for handle_l1_message should be set to 0, as confirmed by the project, and a mitigation was attempted but later superseded.
How it was found
- The L1 sender has a balance of 2100000000 + 21000 and wants to transfer 2100000000 to an L2 address.
- In interpreter::execute, max_fee (2100000000) is subtracted from the L1 sender's cached balance, leaving 2100.
- The ETH value transfer of 2100000000 is attempted using the cached new balance, which is insufficient, so success becomes 0.
- The EVM transaction reverts with EXCEPTIONAL_HALT, consuming all gas and charging the max fee of 2100000000.
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 you send a message from one blockchain to another, the receiving side needs to pay for the work it does. This report found that the code always reserved a huge amount of money for fees before checking if the actual transfer could go through. That made some valid transfers fail and still charged the large fee, even though the real cost was much smaller.
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.