Skip to content
Business logic Audit finding Intermediate

Wrong usage of transaction originator address instead of caller address

The Starknet handler emitted the transaction originator's address instead of the actual caller's address in cross-chain message events. This could mislead third parties reading the events, but the judge deemed it an incorrect implementation rather than a material vulnerability.

fyamf Follow with AllTooWell, haxatron and 3 others

Originally disclosed on Code4rena

Submitted by fyamf, also found by AllTooWell, haxatron, ABAIKUNANBAEV, 0xb0k0, calc1f4r (1, 2), and Draiakoo

Impact

The from_address parameter in the events emitted when sending a cross-chain message from Starknet is set to the transaction origin (tx.origin) instead of the actual caller (msg.sender). This causes incorrect data to be emitted in the event.

Proof of Concept

When a cross-chain message is sent from Starknet using the function handler_erc20::cross_chain_erc20_settlement, the function settlement::send_cross_chain_msg is called:

Within this function, the CrossChainMsg event is emitted for validators to pick up:

            self
                .emit(
                    CrossChainMsg {
                        cross_chain_settlement_id: cross_chain_settlement_id,
                        from_address: get_tx_info().unbox().account_contract_address,
                        from_chain: from_chain,
                        to_chain: to_chain,
                        from_handler: from_handler,
                        to_handler: to_handler,
                        payload_type: payload_type,
                        payload: payload
                    }
                );

https://github.com/code-423n4/2024-08-chakra/blob/main/cairo/handler/src/settlement.cairo#L299

The issue is that from_address is set to get_tx_info().unbox().account_contract_address, which represents “The account contract from which this transaction originates.” This is equivalent to tx.origin in EVM-based blockchains: https://github.com/starkware-libs/cairo/blob/main/corelib/src/starknet/info.cairo#L46

This is incorrect because the address of the original transaction sender is irrelevant. Instead, from_address should represent the caller of the handler_erc20::cross_chain_erc20_settlement function.

In contrast, the EVM-based code correctly sets from_address to the parameter forwarded by the ChakraSettlementHandler::cross_chain_erc20_settlement function, which is equivalent to msg.sender:

            // Send the cross-chain message
            settlement.send_cross_chain_msg(
                to_chain,
                msg.sender,
                to_handler,
                PayloadType.ERC20,
                cross_chain_msg_bytes
            );

https://github.com/code-423n4/2024-08-chakra/blob/main/solidity/handler/contracts/ChakraSettlementHandler.sol#L205

    function send_cross_chain_msg(
        string memory to_chain,
        address from_address,
        uint256 to_handler,
        PayloadType payload_type,
        bytes calldata payload
    ) external {
        //.....
        emit CrossChainMsg(
            txid,
            from_address,
            contract_chain_name,
            to_chain,
            from_handler,
            to_handler,
            payload_type,
            payload
        );
    }

https://github.com/code-423n4/2024-08-chakra/blob/main/solidity/settlement/contracts/ChakraSettlement.sol#L113

By using the transaction originator’s address (tx.origin) as from_address, the event may include an address that is irrelevant to the cross-chain transaction. This can result in misleading information for any third parties reading the events. Here are two scenarios where an incorrect from_address might be used:

Here is the simplified rephrased version of the report:

  • First Scenario: Imagine Alice sends an NFT to Bob’s contract using the safe_transfer function. This function makes an external call to Bob’s contract. During this call, if Bob’s contract calls handler_erc20::cross_chain_erc20_settlement to send a cross-chain message from Starknet to another chain, the CrossChainMsg event will show Alice’s address as from_address because Alice was the original sender of the transaction. This is incorrect because it is actually Bob’s contract sending the cross-chain message, so from_address should be Bob’s contract address, not Alice’s.

https://github.com/OpenZeppelin/cairo-contracts/blob/main/packages/token/src/erc721/erc721.cairo#L571

   // This is Bob's contract
   // When Alice transfers NFT to Bob's contract, this function is called.
       fn on_erc721_received(
           self: @ComponentState<TContractState>,
           operator: ContractAddress,
           from: ContractAddress,
           token_id: u256,
           data: Span<felt252>
       ) -> felt252 {
           // Calling `handler_erc20::cross_chain_erc20_settlement`
           //.....
       }
  • Second Scenario: Consider a scenario where a message is bridged from Ethereum (L1) to Starknet (L2) with Starknet validators managing the bridging process. When the message arrives on Starknet, the function marked with #[l1_handler] in the target contract is called by StarknetOS. In this situation, calling get_caller_address() in that function returns zero. If this function then calls handler_erc20::cross_chain_erc20_settlement, the CrossChainMsg event will show from_address as zero, since the original sender was StarknetOS. This is incorrect because from_address should be the target contract’s address, not zero.

https://book.cairo-lang.org/ch16-04-L1-L2-messaging.html?highlight=%23%5Bl1_handler%5D#sending-messages-from-ethereum-to-starknet

https://docs.cairo-lang.org/hello_starknet/user_auth.html#getting-the-caller-address

https://cairopractice.com/posts/get_caller_address_zero/

    // This is the target contract
    // This function is called by StarknetOS
    #[l1_handler]
    fn msg_handler_felt(ref self: ContractState, from_address: felt252, my_felt: felt252) {
        // Calling get_caller_address() here returns zero
        // Calling `handler_erc20::cross_chain_erc20_settlement`
        //.....
    }

Additionally, when a callback message is received, the CrossChainResult event will show from_address as get_tx_info().unbox().account_contract_address, which is also incorrect and misleading.

Recommended Mitigation Steps

Following modifications are recommended:

        fn cross_chain_erc20_settlement(ref self: ContractState, to_chain: felt252, to_handler: u256, to_token: u256, to: u256, amount: u256) -> felt252{
            assert(self.support_handler.read((to_chain, to_handler)), 'not support handler');
            let settlement = IChakraSettlementDispatcher {contract_address: self.settlement_address.read()};
            let from_chain = settlement.chain_name();
            let token = IERC20Dispatcher{contract_address: self.token_address.read()};
            let token_burnable = IERC20MintDispatcher{contract_address: self.token_address.read()};
            if self.mode.read() == SettlementMode::MintBurn{
                token.transfer_from(get_caller_address(), get_contract_address(), amount);
            }else if self.mode.read() == SettlementMode::LockMint{
                token.transfer_from(get_caller_address(), get_contract_address(), amount);
            }else if self.mode.read() == SettlementMode::BurnUnlock{
                token_burnable.burn_from(get_caller_address(), amount);
            }else if self.mode.read() == SettlementMode::LockUnlock{
                token.transfer_from(get_caller_address(), get_contract_address(), amount);
            }

            let tx_id = LegacyHash::hash(get_tx_info().unbox().transaction_hash, self.msg_count.read());
            let tx: CreatedCrossChainTx = CreatedCrossChainTx{
                    tx_id: tx_id,
                    from_chain: from_chain,
                    to_chain: to_chain,
                    from: get_contract_address(),
                    to: to,
                    from_token: self.token_address.read(),
                    to_token: to_token,
                    amount: amount,
                    tx_status: CrossChainTxStatus::PENDING
                };
            self.created_tx.write(tx_id, tx);
            self.msg_count.write(self.msg_count.read()+1);
            // message id = message count
            let message_id = tx_id;
            let transfer = ERC20Transfer{
                method_id: 1,
                from: contract_address_to_u256(get_caller_address()),
                to: to,
                from_token: contract_address_to_u256(self.token_address.read()),
                to_token: to_token,
                amount: amount
            };

            let message = Message{
                version: 1,
                message_id: message_id.into(),
                payload_type: PayloadType::ERC20,
                payload: encode_transfer(transfer),
            };

            // send cross chain msg
-           settlement.send_cross_chain_msg(to_chain, to_handler, PayloadType::ERC20, encode_message(message));
+           settlement.send_cross_chain_msg(to_chain, to_handler, PayloadType::ERC20, encode_message(message),
+           get_caller_address());
            // emit CrossChainLocked

            self.emit(
                    CrossChainLocked{
                        tx_id: tx_id,
                        from: get_caller_address(),
                        to: to,
                        from_chain: get_tx_info().unbox().chain_id,
                        to_chain: to_chain,
                        from_token: self.token_address.read(),
                        to_token: to_token,
                        amount: amount
                    }
                );
            return tx_id;
        }
        fn send_cross_chain_msg(
            ref self: ContractState, to_chain: felt252, to_handler: u256, payload_type :u8,payload: Array<u8>,
+           from_address: felt252,
        ) -> felt252 {
            let from_handler = get_caller_address();
            let from_chain = self.chain_name.read();
            let cross_chain_settlement_id = LegacyHash::hash(get_tx_info().unbox().transaction_hash, self.tx_count.read());
            self.created_tx.write(cross_chain_settlement_id, CreatedTx{
                tx_id:cross_chain_settlement_id,
                tx_status: CrossChainMsgStatus::PENDING,
                from_chain: from_chain,
                to_chain: to_chain,
                from_handler: from_handler,
                to_handler: to_handler
            });

            self
                .emit(
                    CrossChainMsg {
                        cross_chain_settlement_id: cross_chain_settlement_id,
-                       from_address: get_tx_info().unbox().account_contract_address,
+                       from_address: from_address,
                        from_chain: from_chain,
                        to_chain: to_chain,
                        from_handler: from_handler,
                        to_handler: to_handler,
                        payload_type: payload_type,
                        payload: payload
                    }
                );
            self.tx_count.write(self.tx_count.read()+1);
            return cross_chain_settlement_id;
        }

https://github.com/code-423n4/2024-08-chakra/blob/main/cairo/handler/src/settlement.cairo#L284

zvlwwj (Chakra) confirmed

0xsomeone (judge) commented:

The Warden has outlined how the system will utilize the tx.origin when processing cross-chain transaction creations which is incorrect. I believe that this does not result in a material vulnerability and is instead an incorrect system implementation meriting a medium-severity rating.


The original report as published on Code4rena, by fyamf. 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 code used the transaction's origin address (tx.origin equivalent) instead of the immediate caller's address (msg.sender equivalent) when emitting the event.

Impact

The event could contain an irrelevant or zero address, misleading third parties; the judge stated this does not result in a material vulnerability.

What the attacker needed

A contract calls the settlement function; or a message arrives via l1_handler

The fix

The code should be changed to pass the caller's address (get_caller_address()) as a parameter to send_cross_chain_msg and use that for from_address.

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 smart contract sends a message, it records who sent it. The code recorded the address that started the whole transaction (like the person who initiated a chain of calls) rather than the contract that actually made the call. This means the recorded sender could be wrong, confusing anyone who relies on that record.

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.

More by fyamf

View all

7 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.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.