Skip to content

Commit 0d81b3d

Browse files
committed
Merge bitcoin/bitcoin#30568: addrman: change internal id counting to int64_t
51f7668 addrman: change nid_type from int to int64_t (Martin Zumsande) 051ba32 addrman, refactor: introduce user-defined type for internal nId (Martin Zumsande) Pull request description: With `nIdCount` being incremented for each addr received, an attacker could cause an overflow in the past, see https://bitcoincore.org/en/2024/07/31/disclose-addrman-int-overflow/ Even though that attack was made infeasible indirectly by addr rate-limiting (PR #22387), to be on the safe side and prevent any regressions change the `nId`s used internally to `int64_t`. This is being done by first introducing a user-defined type for `nId`s in the first commit, and then updating it to `int64_t` (thanks sipa for help with this!). Note that `nId` is only used internally, it is not part of the serialization, so `peers.dat` should not be affected by this. I assume that the only reason this was not done in the past is to not draw attention to this previously undisclosed issue. ACKs for top commit: naumenkogs: ACK 51f7668 stratospher: ACK 51f7668. I think it's a good change to make the nId space large(64 bits) so that the nId values are distinct. achow101: ACK 51f7668 Tree-SHA512: 68d4b8b0269a01a9544bedfa7c1348ffde00a288537e4c8bf2b88372ac7d96c4566a44dd6b06285f2fcf31b4f9336761e3bca7253fbc20db5e0d04e887156224
2 parents c985a34 + 51f7668 commit 0d81b3d

File tree

3 files changed

+45
-37
lines changed

3 files changed

+45
-37
lines changed

src/addrman.cpp

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ void AddrManImpl::Serialize(Stream& s_) const
188188

189189
int nUBuckets = ADDRMAN_NEW_BUCKET_COUNT ^ (1 << 30);
190190
s << nUBuckets;
191-
std::unordered_map<int, int> mapUnkIds;
191+
std::unordered_map<nid_type, int> mapUnkIds;
192192
int nIds = 0;
193193
for (const auto& entry : mapInfo) {
194194
mapUnkIds[entry.first] = nIds;
@@ -398,7 +398,7 @@ void AddrManImpl::Unserialize(Stream& s_)
398398
}
399399
}
400400

401-
AddrInfo* AddrManImpl::Find(const CService& addr, int* pnId)
401+
AddrInfo* AddrManImpl::Find(const CService& addr, nid_type* pnId)
402402
{
403403
AssertLockHeld(cs);
404404

@@ -413,11 +413,11 @@ AddrInfo* AddrManImpl::Find(const CService& addr, int* pnId)
413413
return nullptr;
414414
}
415415

416-
AddrInfo* AddrManImpl::Create(const CAddress& addr, const CNetAddr& addrSource, int* pnId)
416+
AddrInfo* AddrManImpl::Create(const CAddress& addr, const CNetAddr& addrSource, nid_type* pnId)
417417
{
418418
AssertLockHeld(cs);
419419

420-
int nId = nIdCount++;
420+
nid_type nId = nIdCount++;
421421
mapInfo[nId] = AddrInfo(addr, addrSource);
422422
mapAddr[addr] = nId;
423423
mapInfo[nId].nRandomPos = vRandom.size();
@@ -438,8 +438,8 @@ void AddrManImpl::SwapRandom(unsigned int nRndPos1, unsigned int nRndPos2) const
438438

439439
assert(nRndPos1 < vRandom.size() && nRndPos2 < vRandom.size());
440440

441-
int nId1 = vRandom[nRndPos1];
442-
int nId2 = vRandom[nRndPos2];
441+
nid_type nId1 = vRandom[nRndPos1];
442+
nid_type nId2 = vRandom[nRndPos2];
443443

444444
const auto it_1{mapInfo.find(nId1)};
445445
const auto it_2{mapInfo.find(nId2)};
@@ -453,7 +453,7 @@ void AddrManImpl::SwapRandom(unsigned int nRndPos1, unsigned int nRndPos2) const
453453
vRandom[nRndPos2] = nId1;
454454
}
455455

456-
void AddrManImpl::Delete(int nId)
456+
void AddrManImpl::Delete(nid_type nId)
457457
{
458458
AssertLockHeld(cs);
459459

@@ -476,7 +476,7 @@ void AddrManImpl::ClearNew(int nUBucket, int nUBucketPos)
476476

477477
// if there is an entry in the specified bucket, delete it.
478478
if (vvNew[nUBucket][nUBucketPos] != -1) {
479-
int nIdDelete = vvNew[nUBucket][nUBucketPos];
479+
nid_type nIdDelete = vvNew[nUBucket][nUBucketPos];
480480
AddrInfo& infoDelete = mapInfo[nIdDelete];
481481
assert(infoDelete.nRefCount > 0);
482482
infoDelete.nRefCount--;
@@ -488,7 +488,7 @@ void AddrManImpl::ClearNew(int nUBucket, int nUBucketPos)
488488
}
489489
}
490490

491-
void AddrManImpl::MakeTried(AddrInfo& info, int nId)
491+
void AddrManImpl::MakeTried(AddrInfo& info, nid_type nId)
492492
{
493493
AssertLockHeld(cs);
494494

@@ -515,7 +515,7 @@ void AddrManImpl::MakeTried(AddrInfo& info, int nId)
515515
// first make space to add it (the existing tried entry there is moved to new, deleting whatever is there).
516516
if (vvTried[nKBucket][nKBucketPos] != -1) {
517517
// find an item to evict
518-
int nIdEvict = vvTried[nKBucket][nKBucketPos];
518+
nid_type nIdEvict = vvTried[nKBucket][nKBucketPos];
519519
assert(mapInfo.count(nIdEvict) == 1);
520520
AddrInfo& infoOld = mapInfo[nIdEvict];
521521

@@ -554,7 +554,7 @@ bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, std::c
554554
if (!addr.IsRoutable())
555555
return false;
556556

557-
int nId;
557+
nid_type nId;
558558
AddrInfo* pinfo = Find(addr, &nId);
559559

560560
// Do not set a penalty for a source's self-announcement
@@ -627,7 +627,7 @@ bool AddrManImpl::Good_(const CService& addr, bool test_before_evict, NodeSecond
627627
{
628628
AssertLockHeld(cs);
629629

630-
int nId;
630+
nid_type nId;
631631

632632
m_last_good = time;
633633

@@ -758,7 +758,8 @@ std::pair<CAddress, NodeSeconds> AddrManImpl::Select_(bool new_only, const std::
758758

759759
// Iterate over the positions of that bucket, starting at the initial one,
760760
// and looping around.
761-
int i, position, node_id;
761+
int i, position;
762+
nid_type node_id;
762763
for (i = 0; i < ADDRMAN_BUCKET_SIZE; ++i) {
763764
position = (initial_position + i) % ADDRMAN_BUCKET_SIZE;
764765
node_id = GetEntry(search_tried, bucket, position);
@@ -791,7 +792,7 @@ std::pair<CAddress, NodeSeconds> AddrManImpl::Select_(bool new_only, const std::
791792
}
792793
}
793794

794-
int AddrManImpl::GetEntry(bool use_tried, size_t bucket, size_t position) const
795+
nid_type AddrManImpl::GetEntry(bool use_tried, size_t bucket, size_t position) const
795796
{
796797
AssertLockHeld(cs);
797798

@@ -854,7 +855,7 @@ std::vector<std::pair<AddrInfo, AddressPosition>> AddrManImpl::GetEntries_(bool
854855
std::vector<std::pair<AddrInfo, AddressPosition>> infos;
855856
for (int bucket = 0; bucket < bucket_count; ++bucket) {
856857
for (int position = 0; position < ADDRMAN_BUCKET_SIZE; ++position) {
857-
int id = GetEntry(from_tried, bucket, position);
858+
nid_type id = GetEntry(from_tried, bucket, position);
858859
if (id >= 0) {
859860
AddrInfo info = mapInfo.at(id);
860861
AddressPosition location = AddressPosition(
@@ -909,8 +910,8 @@ void AddrManImpl::ResolveCollisions_()
909910
{
910911
AssertLockHeld(cs);
911912

912-
for (std::set<int>::iterator it = m_tried_collisions.begin(); it != m_tried_collisions.end();) {
913-
int id_new = *it;
913+
for (std::set<nid_type>::iterator it = m_tried_collisions.begin(); it != m_tried_collisions.end();) {
914+
nid_type id_new = *it;
914915

915916
bool erase_collision = false;
916917

@@ -928,7 +929,7 @@ void AddrManImpl::ResolveCollisions_()
928929
} else if (vvTried[tried_bucket][tried_bucket_pos] != -1) { // The position in the tried bucket is not empty
929930

930931
// Get the to-be-evicted address that is being tested
931-
int id_old = vvTried[tried_bucket][tried_bucket_pos];
932+
nid_type id_old = vvTried[tried_bucket][tried_bucket_pos];
932933
AddrInfo& info_old = mapInfo[id_old];
933934

934935
const auto current_time{Now<NodeSeconds>()};
@@ -974,11 +975,11 @@ std::pair<CAddress, NodeSeconds> AddrManImpl::SelectTriedCollision_()
974975

975976
if (m_tried_collisions.size() == 0) return {};
976977

977-
std::set<int>::iterator it = m_tried_collisions.begin();
978+
std::set<nid_type>::iterator it = m_tried_collisions.begin();
978979

979980
// Selects a random element from m_tried_collisions
980981
std::advance(it, insecure_rand.randrange(m_tried_collisions.size()));
981-
int id_new = *it;
982+
nid_type id_new = *it;
982983

983984
// If id_new not found in mapInfo remove it from m_tried_collisions
984985
if (mapInfo.count(id_new) != 1) {
@@ -1063,15 +1064,15 @@ int AddrManImpl::CheckAddrman() const
10631064
LOG_TIME_MILLIS_WITH_CATEGORY_MSG_ONCE(
10641065
strprintf("new %i, tried %i, total %u", nNew, nTried, vRandom.size()), BCLog::ADDRMAN);
10651066

1066-
std::unordered_set<int> setTried;
1067-
std::unordered_map<int, int> mapNew;
1067+
std::unordered_set<nid_type> setTried;
1068+
std::unordered_map<nid_type, int> mapNew;
10681069
std::unordered_map<Network, NewTriedCount> local_counts;
10691070

10701071
if (vRandom.size() != (size_t)(nTried + nNew))
10711072
return -7;
10721073

10731074
for (const auto& entry : mapInfo) {
1074-
int n = entry.first;
1075+
nid_type n = entry.first;
10751076
const AddrInfo& info = entry.second;
10761077
if (info.fInTried) {
10771078
if (!TicksSinceEpoch<std::chrono::seconds>(info.m_last_success)) {

src/addrman_impl.h

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ static constexpr int ADDRMAN_NEW_BUCKET_COUNT{1 << ADDRMAN_NEW_BUCKET_COUNT_LOG2
3232
static constexpr int32_t ADDRMAN_BUCKET_SIZE_LOG2{6};
3333
static constexpr int ADDRMAN_BUCKET_SIZE{1 << ADDRMAN_BUCKET_SIZE_LOG2};
3434

35+
/**
36+
* User-defined type for the internally used nIds
37+
* This used to be int, making it feasible for attackers to cause an overflow,
38+
* see https://bitcoincore.org/en/2024/07/31/disclose-addrman-int-overflow/
39+
*/
40+
using nid_type = int64_t;
41+
3542
/**
3643
* Extended statistics about a CAddress
3744
*/
@@ -179,36 +186,36 @@ class AddrManImpl
179186
static constexpr uint8_t INCOMPATIBILITY_BASE = 32;
180187

181188
//! last used nId
182-
int nIdCount GUARDED_BY(cs){0};
189+
nid_type nIdCount GUARDED_BY(cs){0};
183190

184191
//! table with information about all nIds
185-
std::unordered_map<int, AddrInfo> mapInfo GUARDED_BY(cs);
192+
std::unordered_map<nid_type, AddrInfo> mapInfo GUARDED_BY(cs);
186193

187194
//! find an nId based on its network address and port.
188-
std::unordered_map<CService, int, CServiceHash> mapAddr GUARDED_BY(cs);
195+
std::unordered_map<CService, nid_type, CServiceHash> mapAddr GUARDED_BY(cs);
189196

190197
//! randomly-ordered vector of all nIds
191198
//! This is mutable because it is unobservable outside the class, so any
192199
//! changes to it (even in const methods) are also unobservable.
193-
mutable std::vector<int> vRandom GUARDED_BY(cs);
200+
mutable std::vector<nid_type> vRandom GUARDED_BY(cs);
194201

195202
// number of "tried" entries
196203
int nTried GUARDED_BY(cs){0};
197204

198205
//! list of "tried" buckets
199-
int vvTried[ADDRMAN_TRIED_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE] GUARDED_BY(cs);
206+
nid_type vvTried[ADDRMAN_TRIED_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE] GUARDED_BY(cs);
200207

201208
//! number of (unique) "new" entries
202209
int nNew GUARDED_BY(cs){0};
203210

204211
//! list of "new" buckets
205-
int vvNew[ADDRMAN_NEW_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE] GUARDED_BY(cs);
212+
nid_type vvNew[ADDRMAN_NEW_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE] GUARDED_BY(cs);
206213

207214
//! last time Good was called (memory only). Initially set to 1 so that "never" is strictly worse.
208215
NodeSeconds m_last_good GUARDED_BY(cs){1s};
209216

210217
//! Holds addrs inserted into tried table that collide with existing entries. Test-before-evict discipline used to resolve these collisions.
211-
std::set<int> m_tried_collisions;
218+
std::set<nid_type> m_tried_collisions;
212219

213220
/** Perform consistency checks every m_consistency_check_ratio operations (if non-zero). */
214221
const int32_t m_consistency_check_ratio;
@@ -225,22 +232,22 @@ class AddrManImpl
225232
std::unordered_map<Network, NewTriedCount> m_network_counts GUARDED_BY(cs);
226233

227234
//! Find an entry.
228-
AddrInfo* Find(const CService& addr, int* pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
235+
AddrInfo* Find(const CService& addr, nid_type* pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
229236

230237
//! Create a new entry and add it to the internal data structures mapInfo, mapAddr and vRandom.
231-
AddrInfo* Create(const CAddress& addr, const CNetAddr& addrSource, int* pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
238+
AddrInfo* Create(const CAddress& addr, const CNetAddr& addrSource, nid_type* pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
232239

233240
//! Swap two elements in vRandom.
234241
void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) const EXCLUSIVE_LOCKS_REQUIRED(cs);
235242

236243
//! Delete an entry. It must not be in tried, and have refcount 0.
237-
void Delete(int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
244+
void Delete(nid_type nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
238245

239246
//! Clear a position in a "new" table. This is the only place where entries are actually deleted.
240247
void ClearNew(int nUBucket, int nUBucketPos) EXCLUSIVE_LOCKS_REQUIRED(cs);
241248

242249
//! Move an entry from the "new" table(s) to the "tried" table
243-
void MakeTried(AddrInfo& info, int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
250+
void MakeTried(AddrInfo& info, nid_type nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
244251

245252
/** Attempt to add a single address to addrman's new table.
246253
* @see AddrMan::Add() for parameters. */
@@ -256,9 +263,9 @@ class AddrManImpl
256263

257264
/** Helper to generalize looking up an addrman entry from either table.
258265
*
259-
* @return int The nid of the entry. If the addrman position is empty or not found, returns -1.
266+
* @return nid_type The nid of the entry. If the addrman position is empty or not found, returns -1.
260267
* */
261-
int GetEntry(bool use_tried, size_t bucket, size_t position) const EXCLUSIVE_LOCKS_REQUIRED(cs);
268+
nid_type GetEntry(bool use_tried, size_t bucket, size_t position) const EXCLUSIVE_LOCKS_REQUIRED(cs);
262269

263270
std::vector<CAddress> GetAddr_(size_t max_addresses, size_t max_pct, std::optional<Network> network, const bool filtered = true) const EXCLUSIVE_LOCKS_REQUIRED(cs);
264271

src/test/fuzz/addrman.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ class AddrManDeterministic : public AddrMan
186186
return false;
187187
}
188188

189-
auto IdsReferToSameAddress = [&](int id, int other_id) EXCLUSIVE_LOCKS_REQUIRED(m_impl->cs, other.m_impl->cs) {
189+
auto IdsReferToSameAddress = [&](nid_type id, nid_type other_id) EXCLUSIVE_LOCKS_REQUIRED(m_impl->cs, other.m_impl->cs) {
190190
if (id == -1 && other_id == -1) {
191191
return true;
192192
}

0 commit comments

Comments
 (0)