Skip to content

addrman: detect on-disk corrupted nNew and nTried during unserialization - #22455

Merged
maflcko merged 1 commit into
bitcoin:masterfrom
vasild:addrman_detect_negative
Jul 19, 2021
Merged

maflcko merged 1 commit into
bitcoin:masterfrom
vasild:addrman_detect_negative

Conversation

@vasild

@vasild vasild commented Jul 15, 2021

Copy link
Copy Markdown
Contributor

Negative nNew or nTried are not possible during normal operation.
So, if we read such values during unserialize, report addrman
corruption.

Fixes #22450

Negative `nNew` or `nTried` are not possible during normal operation.
So, if we read such values during unserialize, report addrman
corruption.

Fixes bitcoin#22450
@jnewbery

Copy link
Copy Markdown
Contributor

I don't think this is the right approach, and doesn't fix the underlying bug in #22450. It's addressing the symptom, rather than the fact that addrman's internal datastructures can get into an inconsistent state.

@maflcko

maflcko commented Jul 15, 2021

Copy link
Copy Markdown
Member

cr ACK 816f29e

can't hurt to check for corrupt data during deserialize

@jonatack jonatack left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ACK 816f29e

(nTried and nNew are declared as int but, like some of the int iterators in addrman.{h,cpp}, arguably should be size_t / unsigned)

@vasild

vasild commented Jul 16, 2021

Copy link
Copy Markdown
Contributor Author

@jnewbery, considering #22450 (comment), do you think that this PR fixes the underlying bug in #22450?

@maflcko

maflcko commented Jul 16, 2021

Copy link
Copy Markdown
Member

It looks like an improvement, but it doesn't fix the other issue(s) in #22450: #22450 (comment)

@jnewbery

Copy link
Copy Markdown
Contributor

do you think that this PR fixes the underlying bug

No. I agree with Marco that this may be an improvement, but it's not fixing the bugs in #22450.

@vasild

vasild commented Jul 16, 2021

Copy link
Copy Markdown
Contributor Author

I am talking about the bug that is reported in the #22450 OP/description. Others would need separate PRs. I need to be sure that the #22450 OP/description is fixed properly, which I believe this PR does, but if you disagree, then maybe I am wrong and this PR needs an update.

@maflcko

maflcko commented Jul 16, 2021

Copy link
Copy Markdown
Member

Maybe remove the "Fixes #22450" from OP to avoid closing the issue. We can then use it as meta issue for addrman deserialize bugs.

@maflcko maflcko added this to the 22.0 milestone Jul 16, 2021
@sipa

sipa commented Jul 16, 2021

Copy link
Copy Markdown
Member

I'm quite anxious about the fact that we're now modifying addrman in a way that apparently introduces inconsistencies.

Would it be possible to just revert that part, and rely on the higher level code to not insert invalid I2P address/ports? That means existing nodes running master won't be able to connect to the I2P addresses existing in their addrman, but I don't think that's much of an issue as I2P support isn't in any release yet?

@vasild

vasild commented Jul 16, 2021

Copy link
Copy Markdown
Contributor Author

Maybe remove the "Fixes #22450" from OP to avoid closing the issue. We can then use it as meta issue for addrman deserialize bugs.

I think it is better to report different problems as separate bug entries, so that none gets forgotten and the conversations do not mix in one bug report. So, now we have:

  1. addrman serialize: nNew was wrong, oh ow #22450 (the text "Introduced in commit e0a2b39" in the OP is misleading), fixed in addrman: detect on-disk corrupted nNew and nTried during unserialization #22455. Not relevant to I2P.

  2. Assertion `nRndPos1 < vRandom.size() && nRndPos2 < vRandom.size()' failed #22467, fixed in addrman: don't overwrite addr_info when resetting I2P ports #22468. A bug in CAddrMan::ResetI2PPorts(), found by fuzzing.

  3. Changing I2P ports in addrman may wrongly skip some entries from "new" buckets #22470, fixed in addrman: reset I2P ports in all "new" buckets #22471. A bug in CAddrMan::ResetI2PPorts(), found by @jnewbery looking at the code.

@lsilva01 lsilva01 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review ACK 816f29e. This change provides a more accurate description of the error.

If I'm understanding the problem so far, I agree with @sipa.

I don't see a need for this logic to keep existing I2P connections when the support for them hasn't even been released yet.
It seems simpler to me not to change the logic of a critical component like CAddrMan and only accept I2P connections with port = 0.

@maflcko

maflcko commented Jul 19, 2021

Copy link
Copy Markdown
Member

Upgrading my review ACK to tested ACK using commit fa740252d2dcc272204b745906785fe26e9a0b77 from #22493 and the following diff:

diff --git a/src/addrman.cpp b/src/addrman.cpp
index 8192b4eba6..4e21062444 100644
--- a/src/addrman.cpp
+++ b/src/addrman.cpp
@@ -468,6 +468,5 @@ int CAddrMan::Check_()
     std::unordered_map<int, int> mapNew;
 
-    if (vRandom.size() != (size_t)(nTried + nNew))
-        return -7;
+    assert(vRandom.size() == (size_t)(nTried + nNew));
 
     for (const auto& entry : mapInfo) {

crash-ce5f3356346c37f5a1eb17cf252105a87f6264ea.log

Before (commit fa740252d2dcc272204b745906785fe26e9a0b77 + patch):

$ FUZZ=addrman ./src/test/fuzz/fuzz ./crash-ce5f3356346c37f5a1eb17cf252105a87f6264ea.log 
INFO: Running with entropic power schedule (0xFF, 100).
INFO: Seed: 1743330088
INFO: Loaded 1 modules   (229079 inline 8-bit counters): 229079 [0x556cd64990f8, 0x556cd64d0fcf), 
INFO: Loaded 1 PC tables (229079 PCs): 229079 [0x556cd64d0fd0,0x556cd684fd40), 
./src/test/fuzz/fuzz: Running 1 inputs 1 time(s) each.
Running: ./crash-ce5f3356346c37f5a1eb17cf252105a87f6264ea.log
fuzz: addrman.cpp:470: int CAddrMan::Check_(): Assertion `vRandom.size() == (size_t)(nTried + nNew)' failed.

After (commit fa740252d2dcc272204b745906785fe26e9a0b77 + patch + 816f29e cherry-picked):

$ FUZZ=addrman ./src/test/fuzz/fuzz ./crash-ce5f3356346c37f5a1eb17cf252105a87f6264ea.log 
INFO: Running with entropic power schedule (0xFF, 100).
INFO: Seed: 3797513599
INFO: Loaded 1 modules   (229109 inline 8-bit counters): 229109 [0x5619431f80f8, 0x56194322ffed), 
INFO: Loaded 1 PC tables (229109 PCs): 229109 [0x56194322fff0,0x5619435aef40), 
./src/test/fuzz/fuzz: Running 1 inputs 1 time(s) each.
Running: ./crash-ce5f3356346c37f5a1eb17cf252105a87f6264ea.log
Executed ./crash-ce5f3356346c37f5a1eb17cf252105a87f6264ea.log in 3 ms
***
*** NOTE: fuzzing was not performed, you have only
***       executed the target code on a fixed set of inputs.
***

@maflcko
maflcko merged commit 54e3174 into bitcoin:master Jul 19, 2021
@vasild
vasild deleted the addrman_detect_negative branch July 19, 2021 12:28
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Mar 7, 2022
…d during unserialization

816f29e addrman: detect on-disk corrupted nNew and nTried during unserialization (Vasil Dimov)

Pull request description:

  Negative `nNew` or `nTried` are not possible during normal operation.
  So, if we read such values during unserialize, report addrman
  corruption.

  Fixes bitcoin#22450

ACKs for top commit:
  MarcoFalke:
    cr ACK 816f29e
  jonatack:
    ACK 816f29e
  lsilva01:
    Code Review ACK bitcoin@816f29e.  This change provides a more accurate description of the error.

Tree-SHA512: 01bdd72d2d86a0ef770a319fee995fd1e147b24a8db84ddb8cd121688e7f94fed73fddc0084758e7183c4f8d08e971f0b1b224f5adb10928a5aa4dbbc8709d74
gades pushed a commit to cosanta/cosanta-core that referenced this pull request May 8, 2022
…d during unserialization

816f29e addrman: detect on-disk corrupted nNew and nTried during unserialization (Vasil Dimov)

Pull request description:

  Negative `nNew` or `nTried` are not possible during normal operation.
  So, if we read such values during unserialize, report addrman
  corruption.

  Fixes bitcoin#22450

ACKs for top commit:
  MarcoFalke:
    cr ACK 816f29e
  jonatack:
    ACK 816f29e
  lsilva01:
    Code Review ACK bitcoin@816f29e.  This change provides a more accurate description of the error.

Tree-SHA512: 01bdd72d2d86a0ef770a319fee995fd1e147b24a8db84ddb8cd121688e7f94fed73fddc0084758e7183c4f8d08e971f0b1b224f5adb10928a5aa4dbbc8709d74
gwillen pushed a commit to ElementsProject/elements that referenced this pull request Jun 1, 2022
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

addrman serialize: nNew was wrong, oh ow

6 participants