LayerZeroModule miscalculates gas, risking loss of assets
The LayerZeroModule contract used global baseGas and gasPerByte values from the source chain to calculate the gas limit for destination-chain messages, instead of using destination-specific values. When these values differ between chains, the delivery could be underpriced, causing lzReceive to revert and the NFT to become stuck permanently.
Holograph gets its cross chain messaging primitives through Layer Zero. To get pricing estimate, it uses the DstConfig price struct exposed in LZ’s RelayerV2.
The issue is that the important baseGas and gasPerByte configuration parameters, which are used to calculate a custom amount of gas for the destination LZ message, use the values that come from the source chain. This is in contrast to LZ which handles DstConfigs in a mapping keyed by chainID. The encoded gas amount is described here.
Impact
The impact is that when those fields are different between chains, one of two things may happen:
Less severe - we waste excess gas, which is refunded to the lzReceive() caller (Layer Zero)
More severe - we underprice the delivery cost, causing lzReceive() to revert and the NFT stuck in limbo forever.
The code does not handle a failed lzReceive (differently to a failed executeJob). Therefore, no failure event is emitted and the NFT is screwed.
Recommended Mitigation Steps
Firstly, make sure to use the target gas costs.
Secondly, re-engineer lzReceive to be fault-proof, i.e. save some gas to emit result event.
I respectfully disagree that this is even a valid issue.
@Trust - please re-review the affected code. You’ll notice that we are in fact extracting destination chain gas data. And if you review the 100s of cross-chain testnet transactions that we have already made with that version of code, you will notice that the math is exact.
Maybe I am misunderstanding something, so some clarification would be great if you think I’m wrong on this.
Please take a look at LayerZeroModule.sol’s send function:
function send(
uint256, /* gasLimit*/
uint256, /* gasPrice*/
uint32 toChain,
address msgSender,
uint256 msgValue,
bytes calldata crossChainPayload
) external payable {
require(msg.sender == address(_operator()), "HOLOGRAPH: operator only call");
LayerZeroOverrides lZEndpoint;
assembly {
lZEndpoint := sload(_lZEndpointSlot)
}
// need to recalculate the gas amounts for LZ to deliver message
lZEndpoint.send{value: msgValue}(
uint16(_interfaces().getChainId(ChainIdType.HOLOGRAPH, uint256(toChain), ChainIdType.LAYERZERO)),
abi.encodePacked(address(this), address(this)),
crossChainPayload,
payable(msgSender),
address(this),
abi.encodePacked(uint16(1), uint256(_baseGas() + (crossChainPayload.length * _gasPerByte())))
);
}
The function uses _baseGas() and _gasPerByte() as the relayer adapter parameters as described in the submission description’s link. These two getters are global for all chains.
I agree that the getMessage() function takes into account the correct fees for the destination chain.
@Trust - Ya but these refer to destination gas limits. BaseGas and GasPerByte is the amount of gas that is used by the crossChainMessage function that LayerZero triggers on cross-chain call HolographOperator.sol#L484
Discussed this in more detail with @Trust, definitely a critical issue.
Need to add destination chain-specific _baseGas and _gasPerByte to mitigate EVM differences in opcode costs.
The original report as published on Code4rena, by Trust. 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
Likely the send function used global _baseGas and _gasPerByte values instead of destination-chain-specific values when encoding the LayerZero relayer adapter parameters.
Impact
If the gas parameters differ between chains, the delivery cost may be underpriced, causing lzReceive to revert and the NFT to be stuck in limbo forever; no failure event is emitted.
What the attacker needed
Cross-chain message via LayerZero; gas parameters differ between chains
The fix
The project added destination chain-specific _baseGas and _gasPerByte values and re-engineered lzReceive to be fault-proof, and the issue was resolved.
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
When a message is sent from one blockchain to another, the sender must pay for enough gas (computational effort) for the destination chain to process it. This report found that the contract used gas settings from the source chain rather than the destination chain, so the payment could be too low. If that happens, the destination transaction fails and the NFT can be stuck forever.
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.