Fix mem access violation merkleblock #9980

pull Christewart wants to merge 1 commits into bitcoin:master from Christewart:fix_mem_access_violation_merkleblock changing 2 files +5 −0
  1. Christewart commented at 4:56 PM on March 12, 2017: member

    Fixing a possible memory access violation in CPartialMerkleTree::CalcHash().

    This can happen if we some how a merkle tree with zero txids. I don't think this can happen in practice as we only send merkle block messages on the p2p network as of now -- we cannot receive them.

    This was found with #8469, specifically using this generator which will cause a memory access violation on this test case.

  2. laanwj added the label Bug on Mar 13, 2017
  3. laanwj added the label Tests on Mar 13, 2017
  4. dcousens commented at 11:22 PM on March 13, 2017: contributor

    If it shouldn't happen, maybe an assert is better?

  5. pstratem commented at 12:22 AM on March 14, 2017: contributor

    @TheBlueMatt what was the behavior before if the elements list was empty?

  6. pstratem commented at 12:24 AM on March 14, 2017: contributor

    wait no that's @sipa

  7. pstratem commented at 12:26 AM on March 14, 2017: contributor

    nvm it returned 0 so this maintains the original behavior

  8. TheBlueMatt commented at 11:32 PM on March 17, 2017: member

    Probably best to assert that vTxid has non-zero length, as that would represent generating a tree for an invalid block.

  9. Christewart force-pushed on Mar 25, 2017
  10. Christewart commented at 2:09 PM on March 26, 2017: member

    Changed to what @dcousens and @TheBlueMatt suggested. I think that makes sense at the end of the day. Also, from what I can tell, that assert cannot be hit from the p2p code.

  11. NicolasDorier commented at 6:20 AM on March 27, 2017: contributor

    I would have set fBad, so we don't risk later to reach a network reachable assert(0) by inadvertance. :)

  12. jnewbery commented at 3:33 PM on April 6, 2017: member

    Looks good. Lightly tested ACK 51802ccc4561bd5a94499c665a333517f5eadd10

    if you're adding an assert, can you put some commenting in explaining the assumptions that you're testing (either around the assert or the function declaration).

    Also please remove the merge commits from this PR.

    EDIT: cool that this was found using your property-based testing. I've been meaning to review #8469 for a while.

  13. Christewart force-pushed on Apr 7, 2017
  14. Christewart commented at 2:13 AM on April 7, 2017: member

    @jnewbery Addressed the concerns you had.

    If you want to chat about 8469 don't be afraid to ping me on irc. I think it will allow us to streamline testing AND exhaustively test invariants better than we are now.

  15. NicolasDorier commented at 8:44 AM on April 7, 2017: contributor

    why no just setting fBad and returning ? I really don't like having an assert, this code might be reached from the network one day for some reason (maybe unsuspecting new feature). Putting an assert here is a tragedy waiting to happen.

  16. jnewbery commented at 1:36 PM on April 7, 2017: member

    @NicolasDorier - I had the same concerns as you, so I spent some time reading around the code to convince myself that this was ok. This code is only called from the CMerkleBlock constructor, which is only called from the gettxoutproof RPC or in response to a MSG_FILTERED_BLOCK getdata request. In both cases, we're constructing the Merkle Block from a block that's already in our mapBlockIndex. Therefore to hit this assert, we'd need there to be an invalid block in the mapBlockIndex, which would mean:

    • our internal data is corrupted; and
    • we've probably fallen pretty badly out of consensus

    in which case, asserting is appropriate.

    As for concerns about future code calling this without knowing about the assert - they'd have to be constructing a merkle block for a block that they hadn't already validated, which is a very bad idea! Just to make sure, Chris has now also added a comment above the assert to clarify assumptions.

    Hope that allays your concerns!

  17. TheBlueMatt commented at 11:18 PM on July 11, 2017: member

    utACK bbdd771e4c1a9ae405a266c0b158bf6825acef15, though it would be nice to add a comment to CMerkleBlock noting that the block containing transactions will be asserted-on.

  18. Christewart force-pushed on Jul 12, 2017
  19. Christewart commented at 3:14 PM on July 12, 2017: member

    @TheBlueMatt Added the comment, is this ready for merge?

    EDIT: Also rebased

  20. Adding assert to avoid a memory access violation inside of PartialMerkleTree::CalcHash()
    Adding comment to assert in PartialMerkleTree::CalcHash()
    
    Adding comment on CMerkleBlock indicating it calls something that contains an assert
    
    Removing EOL whitespace
    8276e70de1
  21. in src/merkleblock.h:125 in e3013b17e8 outdated
     120 | @@ -121,6 +121,8 @@ class CPartialMerkleTree
     121 |  /**
     122 |   * Used to relay blocks as header + vector<merkle branch>
     123 |   * to filtered nodes.
     124 | + *
     125 | + * NOTE: This is asserted on when building CPartialMerkleTree 
    


    TheBlueMatt commented at 3:34 PM on July 12, 2017:

    Wait, what is asserted? Also note extra space at EOL here.

  22. Christewart force-pushed on Jul 12, 2017
  23. gmaxwell approved
  24. gmaxwell commented at 4:35 PM on July 17, 2017: contributor

    utACK.

  25. TheBlueMatt commented at 5:54 PM on July 17, 2017: member

    utACK 8276e70de15c5c3a7525471ad619edd1237b424a

  26. sipa commented at 6:07 PM on July 17, 2017: member

    utACK 8276e70de15c5c3a7525471ad619edd1237b424a

  27. sipa merged this on Jul 17, 2017
  28. sipa closed this on Jul 17, 2017

  29. sipa referenced this in commit fee0d803fb on Jul 17, 2017
  30. PastaPastaPasta referenced this in commit ddcad06416 on Jul 6, 2019
  31. PastaPastaPasta referenced this in commit 5dd6ebb8b1 on Jul 8, 2019
  32. PastaPastaPasta referenced this in commit bec9fb1324 on Jul 9, 2019
  33. PastaPastaPasta referenced this in commit 479f58c9f4 on Jul 11, 2019
  34. PastaPastaPasta referenced this in commit 0272594216 on Jul 13, 2019
  35. PastaPastaPasta referenced this in commit f1be961886 on Jul 13, 2019
  36. PastaPastaPasta referenced this in commit 220e2506a6 on Jul 17, 2019
  37. PastaPastaPasta referenced this in commit 9ad51edd55 on Jul 23, 2019
  38. PastaPastaPasta referenced this in commit 644274fded on Jul 24, 2019
  39. barrystyle referenced this in commit 88916a17d7 on Jan 22, 2020
  40. DrahtBot locked this on Sep 8, 2021

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-21 06:15 UTC

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