context: prevent clones from copying allocator residue #1913

pull l0rinc wants to merge 2 commits into bitcoin-core:master from l0rinc:l0rinc/context-initialize-padding changing 2 files +6 −0
  1. l0rinc commented at 9:12 PM on August 12, 2026: contributor

    Problem: Context creation initializes every member but leaves padding bytes untouched. Cloning a dynamically allocated context into caller-provided storage may copy undefined allocator bytes with the other members. The caller remains responsible for that storage after destroying the context, so memory checkers report the undefined bytes when the block is compared or written out. This is a local initialization defect, no remote or Bitcoin Core path is known.

    Fix: Zero the complete context representation before member initialization and clone assignment. Extend the existing context test to require every byte to be defined after both creation modes and both clone modes.

  2. tests: characterize context object padding
    Context creation and cloning initialize every member but leave padding undefined.
    Record that behavior after both operations by marking the returned context defined, with TODOs for requiring complete initialization.
    6bcb56016c
  3. context: initialize object padding
    Context creation initializes every member but leaves padding untouched.
    Cloning a dynamically allocated context into caller-provided storage may copy undefined allocator bytes with the other members.
    
    Zero the complete context before member initialization and clone assignment.
    This defines its padding whether structure assignment copies source padding or preserves destination padding.
    The existing context test checks every byte after creation and cloning.
    73399fd44c
  4. theStack commented at 1:18 PM on August 13, 2026: contributor

    It's unclear to me if this is a problem in the first place, as users are not meant to inspect the raw bytes of context objects (or any other opaque objects for that matter), so the mentioned "block is compared or written out" scenario which would presumably trigger a report by memory checkers seems one that is outside of the API contract. After destroying the context, that contract arguably ends, but I can't see why users would want to perform any reads from the memory either before freeing (I'd imagine they just do nothing if it's stack/global storage, or calling a free equivalent if it was dynamically allocated by the user), so it seems a bit far-fetched. If we are really worried about that latter scenario, maybe do the memset only at the end of secp256k1_context_preallocated_destroy, and leave the other functions unchanged? Curious what other reviewers think.

    During the lifetime of a context object, uninitialized padding bytes seem to be just fine in any case.

  5. l0rinc commented at 4:43 AM on August 14, 2026: contributor

    Thank you, closing, if other reviewers disagree we can reopen

  6. l0rinc closed this on Aug 14, 2026

  7. real-or-random added the label assurance on Aug 15, 2026
  8. real-or-random added the label tweak/refactor on Aug 15, 2026

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-08-20 02:15 UTC

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