Skip to content
Race condition Audit finding Advanced

The receive_cross_chain_msg function has potential replay attack risks

The receive_cross_chain_msg function does not check whether a cross-chain message has already been executed, relying entirely on the settlement contract. If the settlement contract address is updated without changing validators, previously executed messages can be replayed, potentially causing financial discrepancies.

shaflow2 Follow

Originally disclosed on Code4rena

Submitted by shaflow2

Impact

The receive_cross_chain_msg function does not check the execution status of cross-chain messages itself and relies entirely on the checks performed by the settlement contract. If the settlement contract is updated without changes to the validators, this could lead to a risk of previously executed transactions being replayed.

Proof of Concept

The settlement_address contract can be updated by the owner.

        fn upgrade_settlement(ref self:ContractState, new_settlement: ContractAddress){
            self.ownable.assert_only_owner();
            self.settlement_address.write(new_settlement);
        }

If the settlement_address contract is updated without changes to the validators, the cross-chain messages that were executed in the original settlement contract will no longer exist in the new settlement contract.

This allows an attacker to replay previously executed transactions from the old settlement contract, potentially leading to financial discrepancies or chaos because the receive_cross_chain_msg function does not check the execution status of cross-chain messages itself.

        fn receive_cross_chain_msg(ref self: ContractState, cross_chain_msg_id: u256, from_chain: felt252, to_chain: felt252,
        from_handler: u256, to_handler: ContractAddress, payload: Array<u8>) -> bool{
            assert(to_handler == get_contract_address(),'error to_handler');

            assert(self.settlement_address.read() == get_caller_address(), 'not settlement');

            assert(self.support_handler.read((from_chain, from_handler)) &&
                    self.support_handler.read((to_chain, contract_address_to_u256(to_handler))), 'not support handler');

            ...

            return true;
        }

Recommended Mitigation Steps

  1. Make the settlement_address unchange.
-       fn upgrade_settlement(ref self:ContractState, new_settlement: ContractAddress){
-           self.ownable.assert_only_owner();
-           self.settlement_address.write(new_settlement);
-       }
  1. The handler stores and verifies the state of cross-chain messages.
    #[storage]
    struct Storage {
        #[substorage(v0)]
        ownable: OwnableComponent::Storage,
        #[substorage(v0)]
        upgradeable: UpgradeableComponent::Storage,
        settlement_address: ContractAddress,
        token_address: ContractAddress,
        created_tx: LegacyMap<felt252, CreatedCrossChainTx>,
+       receive_tx: LegacyMap<felt252, CrossChainMsgStatus>,
        msg_count: u256,
        support_handler: LegacyMap<(felt252, u256), bool>,
        mode: u8
    }
        fn receive_cross_chain_msg(ref self: ContractState, cross_chain_msg_id: u256, from_chain: felt252, to_chain: felt252,
        from_handler: u256, to_handler: ContractAddress, payload: Array<u8>) -> bool{
            assert(to_handler == get_contract_address(),'error to_handler');

            assert(self.settlement_address.read() == get_caller_address(), 'not settlement');

            assert(self.support_handler.read((from_chain, from_handler)) &&
                    self.support_handler.read((to_chain, contract_address_to_u256(to_handler))), 'not support handler');
+           assert(self.receive_tx.read(cross_chain_msg_id) == CrossChainMsgStatus::UNKNOW, 'tx status error');
            ...

+           self.receive_tx.wirte(cross_chain_msg_id, CrossChainMsgStatus::SUCCESS);
            return true;
        }

zvlwwj (Chakra) confirmed

0xsomeone (judge) commented:

The submission outlines a potential issue that might arise in an upgrade flow of the system if the validators are not changed. I believe such a scenario is likely, and thus consider this to be an acceptable medium-risk vulnerability as transaction replays would be possible in the edge case described.

0xsomeone (judge) commented:

Just to clarify, the upgrade outlined in this submission is an address replacement, not an actual code upgrade. As such, storage will be affected (i.e. the settlement address will have a fresh storage set) and thus this vulnerability is applicable. Alleviating an issue in the settlement implementation will not affect the handler itself as the handler would be pointing to a new settlement implementation if the function outlined by the exhibit is invoked.


The original report as published on Code4rena, by shaflow2. 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.

How it worked

Root cause

The handler contract does not store or verify the execution status of cross-chain messages, so it cannot prevent replay if the settlement contract is replaced.

Impact

An attacker could replay previously executed cross-chain messages, potentially leading to financial discrepancies or chaos.

What the attacker needed

Owner updates settlement address; validators unchanged; attacker can submit messages

The fix

The handler should store and verify the status of cross-chain messages to prevent replay, and the settlement address should be made unchangeable.

How it was found

  1. The owner updates the settlement contract address to a new contract.
  2. The new settlement contract has no record of previously executed cross-chain messages.
  3. An attacker replays a previously executed cross-chain message through the new settlement contract.
  4. The handler processes the message again because it does not check if it was already executed.

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.

A cross-chain message is a way for one blockchain to tell another to do something, like transfer tokens. This contract receives such messages but does not remember which ones it has already processed. If the contract that sends these messages is replaced, old messages can be sent again and executed twice, which could cause financial problems.

What is Race condition?

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.

More by shaflow2

View all

12 more disclosed reports, best first.

More from Chakra

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Race conditions

The lesson behind this report.

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.