Skip to content
Crypto Audit finding Advanced

LibMuon Signature hash collision

The signature verification functions in LibMuon use abi.encodePacked without type prefixes, allowing different data structures to produce the same hash. An attacker could reuse a valid signature from one function as a signature for another, potentially bypassing checks and using illegal values.

bin2chen Follow with shaka

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-06-symmetrical-judging/issues/214

Found by

bin2chen, shaka

Summary

In LibMuon , all signatures do not distinguish between type prefixes, and abi.encodePacked is used when calculating the hash Cause when abi.encodePacked, if there is a dynamic array, different structures but the same hash value may be obtained Due to conflicting hash values, signatures can be substituted for each other, making malicious use of illegal signatures possible

Vulnerability Detail

The following two methods are examples

1.verifyPrices:

Solidity
    function verifyPrices(PriceSig memory priceSig, address partyA) internal view {
        MuonStorage.Layout storage muonLayout = MuonStorage.layout();
        require(priceSig.prices.length == priceSig.symbolIds.length, "LibMuon: Invalid length");
        bytes32 hash = keccak256(
            abi.encodePacked(
                muonLayout.muonAppId,
                priceSig.reqId,
                address(this),
@>              partyA,
@>              priceSig.upnl,
@>              priceSig.totalUnrealizedLoss,
@>              priceSig.symbolIds,
@>              priceSig.prices,
                priceSig.timestamp,
                getChainId()
            )
        );
        verifyTSSAndGateway(hash, priceSig.sigs, priceSig.gatewaySignature);
    }

2.verifyPartyAUpnlAndPrice

Solidity
    function verifyPartyAUpnlAndPrice(
        SingleUpnlAndPriceSig memory upnlSig,
        address partyA,
        uint256 symbolId
    ) internal view {
        MuonStorage.Layout storage muonLayout = MuonStorage.layout();
//        require(
//            block.timestamp <= upnlSig.timestamp + muonLayout.upnlValidTime,
//            "LibMuon: Expired signature"
//        );
        bytes32 hash = keccak256(
            abi.encodePacked(
                muonLayout.muonAppId,
                upnlSig.reqId,
                address(this),
@>              partyA,
@>              AccountStorage.layout().partyANonces[partyA],
@>              upnlSig.upnl,
@>              symbolId,
@>              upnlSig.price,
                upnlSig.timestamp,
                getChainId()
            )
        );
        verifyTSSAndGateway(hash, upnlSig.sigs, upnlSig.gatewaySignature);
    }

We exclude the same common part (muonAppId/reqId/address (this)/timestamp/getChainId ())

Through the following simplified test code, although the structure is different, the hash value is the same at that time

Solidity
  function test() external {
    address verifyPrices_partyA = address(0x1);
    int256 verifyPrices_upnl = 100;
    int256 verifyPrices_totalUnrealizedLoss = 100;
    uint256 [] memory verifyPrices_symbolIds = new uint256[](1);
    verifyPrices_symbolIds[0]=1;
    uint256 [] memory verifyPrices_prices = new uint256[](1);
    verifyPrices_prices[0]=1000;  

    bytes32 verifyPrices  = keccak256(abi.encodePacked(
            verifyPrices_partyA,
            verifyPrices_upnl,
            verifyPrices_totalUnrealizedLoss,
            verifyPrices_symbolIds,
            verifyPrices_prices
            ));

    address verifyPartyAUpnlAndPrice_partyA = verifyPrices_partyA;
    int256  verifyPartyAUpnlAndPrice_partyANonces = verifyPrices_upnl;
    int256  verifyPartyAUpnlAndPrice_upnl = verifyPrices_totalUnrealizedLoss;
    uint256 verifyPartyAUpnlAndPrice_symbolId = verifyPrices_symbolIds[0];
    uint256 verifyPartyAUpnlAndPrice_price = verifyPrices_prices[0];


    bytes32 verifyPartyAUpnlAndPrice  = keccak256(abi.encodePacked(
            verifyPartyAUpnlAndPrice_partyA,
            verifyPartyAUpnlAndPrice_partyANonces,
            verifyPartyAUpnlAndPrice_upnl,
            verifyPartyAUpnlAndPrice_symbolId,
            verifyPartyAUpnlAndPrice_price
            ));

    console.log("verifyPrices == verifyPartyAUpnlAndPrice:",verifyPrices == verifyPartyAUpnlAndPrice);

  }
CONSOLE
$ forge test -vvv

Running 1 test for test/Counter.t.sol:CounterTest
[PASS] test() (gas: 4991)
Logs:
  verifyPrices == verifyPartyAUpnlAndPrice: true

Test result: ok. 1 passed; 0 failed; finished in 11.27ms

From the above test example, we can see that the verifyPrices and verifyPartyAUpnlAndPrice signatures can be used interchangeably If we get a legal verifyPartyAUpnlAndPrice , it can be used as the signature of verifyPrices () Use partyANonces as upnl, etc

Impact

Signatures can be reused due to hash collisions, through illegal signatures, using illegal unpl, etc

Code Snippet

https://github.com/sherlock-audit/2023-06-symmetrical/blob/main/symmio-core/contracts/libraries/LibMuon.sol#L12

Tool used

Manual Review

Recommendation

It is recommended to add the prefix of the hash, or use api.encode Such as:

Solidity
    function verifyPrices(PriceSig memory priceSig, address partyA) internal view {
        MuonStorage.Layout storage muonLayout = MuonStorage.layout();
        require(priceSig.prices.length == priceSig.symbolIds.length, "LibMuon: Invalid length");
        bytes32 hash = keccak256(
            abi.encodePacked(
+              "verifyPrices",
                muonLayout.muonAppId,
                priceSig.reqId,
                address(this),
                partyA,
                priceSig.upnl,
                priceSig.totalUnrealizedLoss,
                priceSig.symbolIds,
                priceSig.prices,
                priceSig.timestamp,
                getChainId()
            )
        );
        verifyTSSAndGateway(hash, priceSig.sigs, priceSig.gatewaySignature);
    }

Discussion

sherlock-admin2

Escalate

This issue and duplicate are invalid: These issues suggest that a collision is possible between hashes being used in verifyPrices and verifyPartyAUpnlAndPrice.

The following check: https://github.com/sherlock-audit/2023-06-symmetrical/blob/main/symmio-core/contracts/libraries/LibMuon.sol#L52

ensures that priceSig.symbolIds.length + priceSig.prices.length != 3 and so:

Solidity
abi.encodePacked(
    muonLayout.muonAppId,
    priceSig.reqId,
    address(this),
    partyA,
    priceSig.upnl,
    priceSig.totalUnrealizedLoss,
    priceSig.symbolIds,
    priceSig.prices,
    priceSig.timestamp,
    getChainId()
) != abi.encodePacked(
    muonLayout.muonAppId,
    upnlSig.reqId,
    address(this),
    partyA,
    AccountStorage.layout().partyANonces[partyA],
    upnlSig.upnl,
    upnlSig.timestamp,
    getChainId()
)

because the two structures cannot be of the same length given the check

You've deleted an escalation for this issue.

MoonKnightDev

Fixed code PR link: https://github.com/SYMM-IO/symmio-core/pull/11

The original report as published on Sherlock, by bin2chen. Images and videos are our own copies; raw HTML is shown as text. View it on Sherlock.

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

Likely the use of abi.encodePacked without type prefixes or separators allows different data structures to produce identical hashes.

Impact

Signatures could be reused across functions due to hash collisions, allowing an attacker to use illegal signatures and manipulate values such as upnl.

What the attacker needed

A valid signature for one function

The fix

The code was fixed by adding a type prefix to the hash input or using abi.encode instead of abi.encodePacked.

How it was found

  1. Obtain a valid signature for verifyPartyAUpnlAndPrice.
  2. Use that signature as if it were for verifyPrices, because the hashes collide.
  3. The contract accepts the signature and processes the call with manipulated values.

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 program checks a digital signature, it creates a hash of the data. If two different sets of data produce the same hash, a signature for one set can be used for the other. Here, the code did not clearly separate different types of data before hashing, so a signature meant for one function could be accepted by another.

What is Crypto?

Encryption, signatures, randomness or certificate checks that do not protect what they claim to.

More by bin2chen

View all

139 more disclosed reports, best first.

More from Symmetrical Update

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Cryptographic weaknesses

The lesson behind this report.

Encryption, signatures, randomness or certificate checks that do not protect what they claim to.