Skip to content

Conversation

dergoegge
Copy link
Member

This PR fixes a bug in the headers sync logic that enables submitting headers to a nodes block index that don't lead to a chain that surpasses our DoS limit.

The issue is that we ignore the return value on the first IsContinuationOfLowWorkHeadersSync call after a new headers sync is started, which leads to us passing headers to ProcessNewBlockHeaders when that initial IsContinuationOfLowWorkHeadersSync call returns false. One easy way (maybe the only?) to trigger this is by sending 2000 headers where the last header has a different nBits value than the prior headers (which fails the pre-sync logic here). Those 2000 headers will be passed to ProcessNewBlockHeaders.

I haven't included a test here so far because we can't test this without changing the default value for CRegTestParams::consensus.fPowAllowMinDifficultyBlocks or doing some more involved refactoring.

@glozow glozow added the P2P label Oct 20, 2022
@glozow glozow added this to the 24.0 milestone Oct 20, 2022
@glozow glozow requested review from sdaftuar and sipa October 20, 2022 18:11
…ue correctly when new headers sync is started
@dergoegge dergoegge force-pushed the 2022-10-nbits-headerssync branch from 91b0fdd to 7ad15d1 Compare October 21, 2022 10:28
@sipa
Copy link
Member

sipa commented Oct 21, 2022

ACK 7ad15d1

Copy link
Member

@glozow glozow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 7ad15d1

@glozow
Copy link
Member

glozow commented Oct 24, 2022

Backport in #26382

Copy link
Contributor

@mzumsande mzumsande left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Post-Merge ACK 7ad15d1

Good catch!

sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Oct 25, 2022
…eturn value correctly when new headers sync is started

7ad15d1 [net processing] Handle IsContinuationOfLowWorkHeadersSync return value correctly when new headers sync is started (dergoegge)

Pull request description:

  This PR fixes a bug in the headers sync logic that enables submitting headers to a nodes block index that don't lead to a chain that surpasses our DoS limit.

  The issue is that we ignore the return value on [the first `IsContinuationOfLowWorkHeadersSync` call after a new headers sync is started](https://github.com/bitcoin/bitcoin/blob/fabc0310480b49e159a15d494525c5aa15072cba/src/net_processing.cpp#L2553-L2568), which leads to us passing headers to [`ProcessNewBlockHeaders`](https://github.com/bitcoin/bitcoin/blob/fabc0310480b49e159a15d494525c5aa15072cba/src/net_processing.cpp#L2856) when that initial `IsContinuationOfLowWorkHeadersSync` call returns `false`. One easy way (maybe the only?) to trigger this is by sending 2000 headers where the last header has a different `nBits` value than the prior headers (which fails the pre-sync logic [here](https://github.com/bitcoin/bitcoin/blob/fabc0310480b49e159a15d494525c5aa15072cba/src/headerssync.cpp#L189)). Those 2000 headers will be passed to `ProcessNewBlockHeaders`.

  I haven't included a test here so far because we can't test this without changing the default value for `CRegTestParams::consensus.fPowAllowMinDifficultyBlocks` or doing some more involved refactoring.

ACKs for top commit:
  sipa:
    ACK 7ad15d1
  glozow:
    ACK 7ad15d1

Tree-SHA512: 9aabb8bf3700401e79863d0accda0befd2a83c4d469a53f97d827e51139e2f826aee08cdfbc8866b311b153f61fdac9b7aa515fcfa2a21c5e2812c2bf3c03664
Copy link
Member

@sdaftuar sdaftuar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch, thanks again for fixing this. I think it should be possible to write a functional test to exercise the code path that was broken before, so I'll give that a try.

I think the code merged is correct but I think it could be cleaned up a little bit, left one comment.

glozow added a commit that referenced this pull request Oct 27, 2022
…eturn value correctly when new headers sync is started

e23def8 [net processing] Handle IsContinuationOfLowWorkHeadersSync return value correctly when new headers sync is started (dergoegge)

Pull request description:

  Backport of #26355.

ACKs for top commit:
  dergoegge:
    ACK e23def8
  stickies-v:
    ACK e23def8

Tree-SHA512: 051ecb08f1f96557b5b6d01cc9d29a5dfabbb48afffd52cba662251c23277938fcbb6f207fc7575774ef627a9484ceb056cc75476861b920723c35c2f5da36c8
fanquake added a commit to bitcoin-core/gui that referenced this pull request Oct 31, 2022
784b023 [net processing] Simplify use of IsContinuationOfLowWorkHeadersSync in TryLowWorkHeaderSync (dergoegge)
e891aab [net processing] Fixup TryLowWorkHeadersSync comment (dergoegge)

Pull request description:

  See bitcoin/bitcoin#26355 (comment) and bitcoin/bitcoin#26355 (comment)

ACKs for top commit:
  hernanmarino:
    ACK 784b023
  brunoerg:
    crACK 784b023
  mzumsande:
    ACK 784b023

Tree-SHA512: b47ac0d78a09ca3a1806e38c5d2e2fcf1e5f0668f202450b5079c5cb168e168ac6828c0948d23f3610696375134986d75ef3c6098858173023bcb743aec8004c
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Oct 31, 2022
784b023 [net processing] Simplify use of IsContinuationOfLowWorkHeadersSync in TryLowWorkHeaderSync (dergoegge)
e891aab [net processing] Fixup TryLowWorkHeadersSync comment (dergoegge)

Pull request description:

  See bitcoin#26355 (comment) and bitcoin#26355 (comment)

ACKs for top commit:
  hernanmarino:
    ACK 784b023
  brunoerg:
    crACK 784b023
  mzumsande:
    ACK 784b023

Tree-SHA512: b47ac0d78a09ca3a1806e38c5d2e2fcf1e5f0668f202450b5079c5cb168e168ac6828c0948d23f3610696375134986d75ef3c6098858173023bcb743aec8004c
@bitcoin bitcoin locked and limited conversation to collaborators Oct 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants