Skip to content

Commit

Permalink
Merge bitcoin/bitcoin#22455: addrman: detect on-disk corrupted nNew a…
Browse files Browse the repository at this point in the history
…nd nTried 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/bitcoin#22450

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

Tree-SHA512: 01bdd72d2d86a0ef770a319fee995fd1e147b24a8db84ddb8cd121688e7f94fed73fddc0084758e7183c4f8d08e971f0b1b224f5adb10928a5aa4dbbc8709d74
  • Loading branch information
MarcoFalke committed Jul 19, 2021
2 parents d3474b8 + 816f29e commit 54e3174
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/addrman.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,18 @@ class CAddrMan
nUBuckets ^= (1 << 30);
}

if (nNew > ADDRMAN_NEW_BUCKET_COUNT * ADDRMAN_BUCKET_SIZE) {
throw std::ios_base::failure("Corrupt CAddrMan serialization, nNew exceeds limit.");
if (nNew > ADDRMAN_NEW_BUCKET_COUNT * ADDRMAN_BUCKET_SIZE || nNew < 0) {
throw std::ios_base::failure(
strprintf("Corrupt CAddrMan serialization: nNew=%d, should be in [0, %u]",
nNew,
ADDRMAN_NEW_BUCKET_COUNT * ADDRMAN_BUCKET_SIZE));
}

if (nTried > ADDRMAN_TRIED_BUCKET_COUNT * ADDRMAN_BUCKET_SIZE) {
throw std::ios_base::failure("Corrupt CAddrMan serialization, nTried exceeds limit.");
if (nTried > ADDRMAN_TRIED_BUCKET_COUNT * ADDRMAN_BUCKET_SIZE || nTried < 0) {
throw std::ios_base::failure(
strprintf("Corrupt CAddrMan serialization: nTried=%d, should be in [0, %u]",
nTried,
ADDRMAN_TRIED_BUCKET_COUNT * ADDRMAN_BUCKET_SIZE));
}

// Deserialize entries from the new table.
Expand Down

0 comments on commit 54e3174

Please sign in to comment.