Skip to content
Smart contracts Audit finding Intermediate

SettlementSignatureVerifier is missing check for duplicate validator signatures

The signature verification functions in both Solidity and Cairo settlement contracts do not check for duplicate validator signatures, allowing a single validator to meet the required threshold by repeating their signature. This lets a malicious validator drain the entire balance of a handler by submitting a forged cross-chain message.

SBSecurity Follow with Abdessamed, 0xHelium and 32 others

Originally disclosed on Code4rena

Submitted by SBSecurity, also found by Abdessamed, joicygiore (1, 2), 0xHelium, aldarion (1, 2), Sabit, 0x37 (1, 2), Tigerfrake, kutugu, Trooper, BY_DLIFE, Brene, jasonxiale, Coinymous (1, 2, 3), Cybrid, DanielArmstrong, IzuMan, bhilare_, 0x18a6 (1, 2), kind_killerwhale, 0xNirix, Kaysoft, calc1f4r (1, 2, 3), DimaKush, Shubham, Respx (1, 2), agent3bood, b0g0 (1, 2), Taiger (1, 2, 3), 10ap17 (1, 2), Omik, tekkac, jesjupyter, Bauchibred, Subroutine (1, 2), eierina, minato7namikazi, LuarSec (1, 2), JanuaryPersimmon2024 (1, 2), haxatron, said (1, 2), shaflow2 (1, 2), mojito_auditor, fyamf (1, 2), gesha17, klau5 (1, 2), 4rdiii, ZdravkoHr, yudan, NexusAudits, Sparrow, and biakia

Impact

SettlementSignatureVerifier::verifyECDSA and settlement::check_chakra_signatures lack checks for duplicate validators and allow passing the threshold with a single valid signature.

Proof of Concept

From the README, we see that validators aren’t trusted roles, they simply have to perform their duties and in case they are misbehaving MANAGER can remove them. It’s not expected they all to decide to perform a sybil attack.

But for this issue single validator can harm the entire protocol because signature verifiers don’t check for duplicated validators and one valid signature is enough to pass the check:

Solidity

SettlementSignatureVerifier.sol

function verifyECDSA(
    bytes32 msgHash,
    bytes calldata signatures
) internal view returns (bool) {
    require(
        signatures.length % 65 == 0,
        "Signature length must be a multiple of 65"
    );

    uint256 len = signatures.length;
    uint256 m = 0;
    for (uint256 i = 0; i < len; i += 65) {
        bytes memory sig = signatures[i:i + 65];
        if (
            validators[msgHash.recover(sig)] && ++m >= required_validators
        ) {
            return true;
        }
    }

    return false;
}

As we can see nowhere do we check whether this sig is contained twice in the signatures argument.

Knowing that a single validator can call ChakraSettlement::receive_cross_chain_msg with arbitrary arguments and drain all the ChakraSettlementHandler balances where he is an active validator.

The caller of the function should only give valid chains and handlers, the payload will also be valid so it can be decoded but the amount will be the entire balance (this is the max impact, but even 1 wei is considered steal because the caller has not burned or locked any tokens by first calling the send_cross_chain_msg function) of the handler.

Furthermore, the entire handler can be blocked because of the missing duplicate signature check:

  1. Single validator can sign message_hash that has ERC20TransferPayload like this, without locking any tokens in the handler on the source chain:

    ERC20TransferPayload memory payload = ERC20TransferPayload(
        ERC20Method.Transfer,
        AddressCast.to_uint256(msg.sender),
        to,//his own address on the destination chain
        AddressCast.to_uint256(token),
        to_token,
        //if SettlementMode: LockMint or MintBurn
        (type(uint256).max - IERC20(token).totalSupply())
        //if SettlementMode: LockUnlock or BurnUnlock
        IERC20(token).balanceOf(address(from_handler))
    );
    
  2. Then he will call the ChakraSettlement::receive_cross_chain_msg on the destination and signature validation will pass because he passed his signature to the signatures argument enough times so the signature_verifier.verify returns true (again this happens due to the missing duplicate signatures check):

    For example if the required_validators = 3, his signature bytes will look like this:

    [0xf86c0a8502540be400825208944….0xf86c0a8502540be400825208944….0xf86c0a8502540be400825208944….], where dots are the remaining bytes but I think you got it ;)

    function verifyECDSA(
        bytes32 msgHash,
        bytes calldata signatures
    ) internal view returns (bool) {
        require(
            signatures.length % 65 == 0,
            "Signature length must be a multiple of 65"
        );
    
        uint256 len = signatures.length;
        uint256 m = 0;
        for (uint256 i = 0; i < len; i += 65) {
            bytes memory sig = signatures[i:i + 65];
            if (
                validators[msgHash.recover(sig)] && ++m >= required_validators
            ) {
                return true;
            }
        }
    
        return false;
    }
    
  3. ChakraSettlementHandler::receive_cross_chain_msg will be executed:

    bool result = ISettlementHandler(to_handler).receive_cross_chain_msg(
        txid,
        from_chain,
        from_address,
        from_handler,
        payload_type,
        payload,
        sign_type,
        signatures
    );
    
  4. Malicious ERC20TransferPayload will be encoded and the handler will either mint to the attacker the amount which will make the totalSupply of the token = uint256.max if the mode is mint or the entire balance of the ChakraSettlementHandler stealing the locked tokens of the honest users.

Cairo

The same issue applies to the Cairo code as well:

settlement.cairo

fn check_chakra_signatures(
    self: @ContractState, message_hash: felt252, signatures: Array<(felt252, felt252, bool)>
){
    let mut pass_count = 0;
    let mut i = 0;
    loop {
        if i > signatures.len()-1{
            break;
        }
        let (r,s,y) = * signatures.at(i);

        let pub_key: felt252 = recover_public_key(message_hash,r,s,y).unwrap();
        if self.chakra_validators_pubkey.read(pub_key) > 0{
            pass_count += 1;
        }
        i += 1;
    };
    assert(pass_count >= self.required_validators_num.read(), 'Not enough validate signatures');
}

Here, settlement::receive_cross_chain_msg also can be called directly from a single validator and bypass all the checks, stealing the entire balance of the handler.

The caller of the functions should only give valid chains and handlers, the payload will also be valid so it can be decoded but the amount will be the entire balance (this is the max impact, even 1 wei is considered steal because the caller has not burned or locked any tokens by first calling the send_cross_chain_msg function) of the handler.

In case both issues are separated because they happen in different places we split this one into two, covering both languages, so it is to be accepted by both.

NOTE: Keep in mind that the issue shows that any validator can drain the entire protocol due to a missing duplicate signatures (used) check that allows him to do anything with only 1 signed message. The problem is not only that validators are not trusted, when the problem is fixed as below, even if a validator decides to do something like that, he can’t achieve it, that’s why required_validators is defined at all to protect the protocol from situations like this.

In the end this single validator doesn’t lose anything, nowhere in the README it is mentioned that they are trusted, or any token stake is required from them, the only thing that can lose is their rights to continue signing transactions. On the other hand, they will receive the entire token supply or entire locked token balance.

Recommended Mitigation Steps

For Solidity:

+ mapping (bytes32 msgHash => mapping(address validator => bool usedSig)) public isSignatureUsed;

  function verifyECDSA(
      bytes32 msgHash,
      bytes calldata signatures
  ) internal view returns (bool) {
      require(
          signatures.length % 65 == 0,
          "Signature length must be a multiple of 65"
      );

      uint256 len = signatures.length;
      uint256 m = 0;
      for (uint256 i = 0; i < len; i += 65) {
          bytes memory sig = signatures[i:i + 65];

+         address validator = msgHash.recover(sig);
          if (
-             validators[msgHash.recover(sig)] && ++m >= required_validators
+             !isSignatureUsed[msgHash][validator] && validators[validator]
          ) {
+             isSignatureUsed[msgHash][validator] = true;
+             m++;
          }

+         if (m >= required_validators) {
+             return true;
+         }
      }

      return false;
  }

For Cairo:

fn check_chakra_signatures(
    self: @ContractState,
    message_hash: felt252,
    signatures: Array<(felt252, felt252, bool)>
) {
    let mut pass_count = 0;
    let mut i = 0;

    // A set-like structure to keep track of processed public keys.
+   let mut processed_pub_keys: LegacyMap<felt252, u8> = LegacyMap::new();

    loop {
        if i > signatures.len() - 1 {
            break;
        }

        let (r, s, y) = *signatures.at(i);
        let pub_key: felt252 = recover_public_key(message_hash, r, s, y).unwrap();
-       if self.chakra_validators_pubkey.read(pub_key) > 0{
+       if processed_pub_keys.get(pub_key) == 0 && self.chakra_validators_pubkey.read(pub_key) > 0 {
            pass_count += 1;
+           processed_pub_keys.append(pub_key);
        }

        i += 1;
    }

    assert(pass_count >= self.required_validators_num.read(), 'Not enough valid signatures');
}

pidb (Chakra) disputed and commented on duplicate issue #4:

We added a nonce to the txid, and then will the txid be included in the signature? I think this can prevent this problem.

0xsomeone (judge) increased severity to High and commented:

The Warden and its duplicates have properly identified that a single signature can be repeated an arbitrary number of times to satisfy the required validator threshold with a single validator.

I believe a high-risk rating is appropriate for this submission as it represents a significant invariant breach and a compromise of the signature verification system employed within the system.


The original report as published on Code4rena, by SBSecurity. 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 signature verification loop does not track which validators have already contributed, so the same valid signature can be counted multiple times toward the threshold.

Impact

A single validator could drain the entire balance of a handler, either by minting an unlimited amount of tokens or by stealing all locked tokens, and could also block the handler entirely.

What the attacker needed

Attacker is a validator; Attacker can call receive_cross_chain_msg

The fix

The signature verification should track which validators have already signed and only count each validator once, as shown in the recommended mitigation.

How it was found

  1. A single validator signs a message hash containing a malicious ERC20TransferPayload that sets the amount to the entire handler balance.
  2. The validator calls ChakraSettlement::receive_cross_chain_msg on the destination chain, passing the same signature repeated enough times to meet the required_validators threshold.
  3. The signature verification passes because duplicate signatures are not checked.
  4. ChakraSettlementHandler::receive_cross_chain_msg executes the malicious payload, minting tokens to the attacker or transferring the entire locked balance.

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.

In a cross-chain bridge, multiple validators must sign a message before funds are released. This code counts each signature without checking if it's from a different validator, so one validator can sign the same message many times and trick the system into thinking multiple validators approved. That lets a single malicious validator steal all the funds locked in the bridge.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by SBSecurity

View all

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

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.