musig: invalidate output if pubnonce/aggnonce parsing fails #1991

pull amartya1523 wants to merge 4 commits into bitcoin-core:master from amartya1523:musig-parse-invalidate-output changing 3 files +56 −6
  1. amartya1523 commented at 2:31 PM on November 6, 2026: contributor

    musig_pubnonce_parse and musig_aggnonce_parse return 0 on invalid input but leave the output object untouched. Every other parse function in the library (ec_pubkey_parse, xonly_pubkey_parse, the ECDSA signature parsers, silentpayments_recipient_label_parse and musig_partial_sig_parse) clears its output first, so a failed parse leaves an object that is rejected when used.

    Without that, if the object already holds a nonce (e.g. a coordinator parsing incoming nonces into a reused array, as the test vectors do), the old nonce survives a failed parse and is accepted by nonce_agg / nonce_process without any error. If the object was never initialized, a later load reads uninitialized memory.

    Commits:

    • clear the output right after the nonce != NULL check, as ec_pubkey_parse does, so it is also cleared when in66 is NULL
    • test that a failed parse (and a NULL in66) leaves a zeroed object that serialize and nonce_agg / nonce_process reject. An all-zero aggnonce is valid (infinity), so that test uses a bad prefix byte. These checks fail without the first commit.
    • partial_sig_parse cleared only after both ARG_CHECKs, so move its memset up the same way
    • document "If not, it's set to an invalid value" for the three musig parsers, matching xonly_pubkey_parse

    This is narrower than #1850: it doesn't propose a general clear-on-failure rule, it only makes the musig parsers behave like the other parsers.

  2. musig: invalidate nonce output if pubnonce/aggnonce parsing fails
    Every other parse function in the library (ec_pubkey_parse,
    xonly_pubkey_parse, the ECDSA signature parsers,
    silentpayments_recipient_label_parse and musig_partial_sig_parse)
    clears its output object before parsing, so that a failed parse leaves
    an object that is rejected when it is used later.
    
    musig_pubnonce_parse and musig_aggnonce_parse return 0 but leave the
    output untouched instead. If the object was not initialized, a later
    load reads uninitialized memory. If it holds an earlier nonce, for
    example when a coordinator parses incoming nonces into a reused array,
    that stale nonce stays valid and is accepted by nonce_agg and
    nonce_process without any error.
    
    Clear the output right after checking that it is non-NULL, as
    ec_pubkey_parse and xonly_pubkey_parse do, so that it is also cleared
    if in66 is NULL.
    a6a7f0a121
  3. musig: test that failed nonce parsing invalidates the output
    Parse a valid nonce into an object, then parse an invalid encoding into
    the same object, and check that the object is cleared and rejected by
    serialize and by nonce_agg (for a pubnonce) or nonce_process (for an
    aggnonce). Also check the case where in66 is NULL.
    
    An all-zero encoding is a valid aggnonce (both points at infinity), so
    the aggnonce test uses an invalid prefix byte instead.
    
    These checks fail without the previous commit.
    2b4660bb6f
  4. musig: clear partial sig output before checking in32
    partial_sig_parse cleared its output only after both ARG_CHECKs, so a
    NULL in32 left a previously parsed signature in place. Clear it right
    after checking sig, as the nonce parsers now do. Adjust the NULL in32
    test to use a temporary object and check that it is cleared.
    2287dd9c34
  5. doc: say that musig parse functions invalidate the output on failure
    xonly_pubkey_parse documents that its output is set to an invalid value
    if parsing fails. The musig parse functions now all behave this way, so
    document it for pubnonce_parse, aggnonce_parse and partial_sig_parse as
    well.
    d33f69730f
  6. Yudis-bit commented at 2:46 PM on November 9, 2026: contributor

    Zeroing the nonce buffer at function entry leaves an invalid magic header on bad inputs, matching xonly_pubkey_parse and preventing stale nonces from passing downstream checks.

    Unit tests pass locally under ctest, and ctime_tests is clean under Valgrind. ACK d33f697.

  7. theStack added the label user-documentation on Nov 11, 2026
  8. theStack added the label tweak/refactor on Nov 11, 2026
  9. theStack commented at 1:18 PM on November 12, 2026: contributor

    Concept ACK

    Makes sense to be consistent in all _parse functions and clear the output buffer if inputs are invalid. As for the API documentation, could you adapt it for the following functions as well:

    • secp256k1_ec_pubkey_parse (it already says "If not, its value is undefined", but "set to an invalid value" is more specific)
    • secp256k1_silentpayments_recipient_label_parse (missing)
    • secp256k1_ecdsa_recoverable_signature_parse_compact (missing)
  10. fametrano commented at 3:22 PM on November 12, 2026: contributor

    @theStack For ecdsa_recoverable_signature_parse_compact, that doc line would hold only if the clearing moves up as well. At this head it returns through ARG_CHECK(input64 != NULL) and ARG_CHECK(recid >= 0 && recid <= 3) (src/modules/recovery/main_impl.h:45-46) before touching sig, and the memset at line 55 runs only on overflow. ec_pubkey_parse and silentpayments_recipient_label_parse already clear right after checking their output, so the line fits them as it is.

  11. fametrano commented at 3:22 PM on November 12, 2026: contributor

    ACK d33f69730f5db4e6044ae3896d646feb12d21379

    On #1850, real-or-random said the point of writing outputs is to avoid uninitialized memory, so I measured that case, through the public API with the parse result ignored.

    On master (bf3160c), a pubnonce or aggnonce that fails to parse into a never-written heap object makes the next nonce_agg or nonce_process branch on uninitialized bytes: Valgrind memcheck flags secp256k1_memcmp_var in pubnonce_load and aggnonce_load. At this head both cases reach the illegal callback, and memcheck reports no error.

    The probe, the commands and their output are in https://gist.github.com/fametrano/9971679b94a35591c8656e54b7174630.

  12. in src/modules/musig/tests_impl.h:498 in 2287dd9c34
     495 | @@ -496,7 +496,13 @@ static void musig_api_tests(void) {
     496 |          CHECK(secp256k1_musig_partial_sig_parse(CTX, &tmp, max64) == 0);
     497 |          CHECK(secp256k1_memcmp_var(&tmp, zeros132, sizeof(partial_sig[0])) == 0);
     498 |      }
    


    IsaqueFranklin commented at 11:36 PM on November 19, 2026:

    nit: Could we also add a CHECK_ILLEGAL on partial_sig_serialize here to verify it rejects tmp? That way we can be consistent with what the nonce parsing tests in 2b4660bb6ff9d1685a1bdab0083d6e315edec606 are doing.

  13. IsaqueFranklin commented at 11:40 PM on November 19, 2026: none

    ACK d33f69730f5db4e6044ae3896d646feb12d21379. Ran the tests and reviewed the code. Looks good to me.


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin-core/secp256k1. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-12-02 10:15 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me