mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-12-08 03:33:32 +01:00
Merge bitcoin/bitcoin#30568: addrman: change internal id counting to int64_t
51f7668d31addrman: change nid_type from int to int64_t (Martin Zumsande)051ba3290eaddrman, 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: ACK51f7668d31stratospher: ACK51f7668d31. I think it's a good change to make the nId space large(64 bits) so that the nId values are distinct. achow101: ACK51f7668d31Tree-SHA512: 68d4b8b0269a01a9544bedfa7c1348ffde00a288537e4c8bf2b88372ac7d96c4566a44dd6b06285f2fcf31b4f9336761e3bca7253fbc20db5e0d04e887156224
This commit is contained in:
@@ -188,7 +188,7 @@ void AddrManImpl::Serialize(Stream& s_) const
|
||||
|
||||
int nUBuckets = ADDRMAN_NEW_BUCKET_COUNT ^ (1 << 30);
|
||||
s << nUBuckets;
|
||||
std::unordered_map<int, int> mapUnkIds;
|
||||
std::unordered_map<nid_type, int> mapUnkIds;
|
||||
int nIds = 0;
|
||||
for (const auto& entry : mapInfo) {
|
||||
mapUnkIds[entry.first] = nIds;
|
||||
@@ -398,7 +398,7 @@ void AddrManImpl::Unserialize(Stream& s_)
|
||||
}
|
||||
}
|
||||
|
||||
AddrInfo* AddrManImpl::Find(const CService& addr, int* pnId)
|
||||
AddrInfo* AddrManImpl::Find(const CService& addr, nid_type* pnId)
|
||||
{
|
||||
AssertLockHeld(cs);
|
||||
|
||||
@@ -413,11 +413,11 @@ AddrInfo* AddrManImpl::Find(const CService& addr, int* pnId)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
AddrInfo* AddrManImpl::Create(const CAddress& addr, const CNetAddr& addrSource, int* pnId)
|
||||
AddrInfo* AddrManImpl::Create(const CAddress& addr, const CNetAddr& addrSource, nid_type* pnId)
|
||||
{
|
||||
AssertLockHeld(cs);
|
||||
|
||||
int nId = nIdCount++;
|
||||
nid_type nId = nIdCount++;
|
||||
mapInfo[nId] = AddrInfo(addr, addrSource);
|
||||
mapAddr[addr] = nId;
|
||||
mapInfo[nId].nRandomPos = vRandom.size();
|
||||
@@ -438,8 +438,8 @@ void AddrManImpl::SwapRandom(unsigned int nRndPos1, unsigned int nRndPos2) const
|
||||
|
||||
assert(nRndPos1 < vRandom.size() && nRndPos2 < vRandom.size());
|
||||
|
||||
int nId1 = vRandom[nRndPos1];
|
||||
int nId2 = vRandom[nRndPos2];
|
||||
nid_type nId1 = vRandom[nRndPos1];
|
||||
nid_type nId2 = vRandom[nRndPos2];
|
||||
|
||||
const auto it_1{mapInfo.find(nId1)};
|
||||
const auto it_2{mapInfo.find(nId2)};
|
||||
@@ -453,7 +453,7 @@ void AddrManImpl::SwapRandom(unsigned int nRndPos1, unsigned int nRndPos2) const
|
||||
vRandom[nRndPos2] = nId1;
|
||||
}
|
||||
|
||||
void AddrManImpl::Delete(int nId)
|
||||
void AddrManImpl::Delete(nid_type nId)
|
||||
{
|
||||
AssertLockHeld(cs);
|
||||
|
||||
@@ -476,7 +476,7 @@ void AddrManImpl::ClearNew(int nUBucket, int nUBucketPos)
|
||||
|
||||
// if there is an entry in the specified bucket, delete it.
|
||||
if (vvNew[nUBucket][nUBucketPos] != -1) {
|
||||
int nIdDelete = vvNew[nUBucket][nUBucketPos];
|
||||
nid_type nIdDelete = vvNew[nUBucket][nUBucketPos];
|
||||
AddrInfo& infoDelete = mapInfo[nIdDelete];
|
||||
assert(infoDelete.nRefCount > 0);
|
||||
infoDelete.nRefCount--;
|
||||
@@ -488,7 +488,7 @@ void AddrManImpl::ClearNew(int nUBucket, int nUBucketPos)
|
||||
}
|
||||
}
|
||||
|
||||
void AddrManImpl::MakeTried(AddrInfo& info, int nId)
|
||||
void AddrManImpl::MakeTried(AddrInfo& info, nid_type nId)
|
||||
{
|
||||
AssertLockHeld(cs);
|
||||
|
||||
@@ -515,7 +515,7 @@ void AddrManImpl::MakeTried(AddrInfo& info, int nId)
|
||||
// first make space to add it (the existing tried entry there is moved to new, deleting whatever is there).
|
||||
if (vvTried[nKBucket][nKBucketPos] != -1) {
|
||||
// find an item to evict
|
||||
int nIdEvict = vvTried[nKBucket][nKBucketPos];
|
||||
nid_type nIdEvict = vvTried[nKBucket][nKBucketPos];
|
||||
assert(mapInfo.count(nIdEvict) == 1);
|
||||
AddrInfo& infoOld = mapInfo[nIdEvict];
|
||||
|
||||
@@ -554,7 +554,7 @@ bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, std::c
|
||||
if (!addr.IsRoutable())
|
||||
return false;
|
||||
|
||||
int nId;
|
||||
nid_type nId;
|
||||
AddrInfo* pinfo = Find(addr, &nId);
|
||||
|
||||
// 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
|
||||
{
|
||||
AssertLockHeld(cs);
|
||||
|
||||
int nId;
|
||||
nid_type nId;
|
||||
|
||||
m_last_good = time;
|
||||
|
||||
@@ -758,7 +758,8 @@ std::pair<CAddress, NodeSeconds> AddrManImpl::Select_(bool new_only, const std::
|
||||
|
||||
// Iterate over the positions of that bucket, starting at the initial one,
|
||||
// and looping around.
|
||||
int i, position, node_id;
|
||||
int i, position;
|
||||
nid_type node_id;
|
||||
for (i = 0; i < ADDRMAN_BUCKET_SIZE; ++i) {
|
||||
position = (initial_position + i) % ADDRMAN_BUCKET_SIZE;
|
||||
node_id = GetEntry(search_tried, bucket, position);
|
||||
@@ -791,7 +792,7 @@ std::pair<CAddress, NodeSeconds> AddrManImpl::Select_(bool new_only, const std::
|
||||
}
|
||||
}
|
||||
|
||||
int AddrManImpl::GetEntry(bool use_tried, size_t bucket, size_t position) const
|
||||
nid_type AddrManImpl::GetEntry(bool use_tried, size_t bucket, size_t position) const
|
||||
{
|
||||
AssertLockHeld(cs);
|
||||
|
||||
@@ -854,7 +855,7 @@ std::vector<std::pair<AddrInfo, AddressPosition>> AddrManImpl::GetEntries_(bool
|
||||
std::vector<std::pair<AddrInfo, AddressPosition>> infos;
|
||||
for (int bucket = 0; bucket < bucket_count; ++bucket) {
|
||||
for (int position = 0; position < ADDRMAN_BUCKET_SIZE; ++position) {
|
||||
int id = GetEntry(from_tried, bucket, position);
|
||||
nid_type id = GetEntry(from_tried, bucket, position);
|
||||
if (id >= 0) {
|
||||
AddrInfo info = mapInfo.at(id);
|
||||
AddressPosition location = AddressPosition(
|
||||
@@ -909,8 +910,8 @@ void AddrManImpl::ResolveCollisions_()
|
||||
{
|
||||
AssertLockHeld(cs);
|
||||
|
||||
for (std::set<int>::iterator it = m_tried_collisions.begin(); it != m_tried_collisions.end();) {
|
||||
int id_new = *it;
|
||||
for (std::set<nid_type>::iterator it = m_tried_collisions.begin(); it != m_tried_collisions.end();) {
|
||||
nid_type id_new = *it;
|
||||
|
||||
bool erase_collision = false;
|
||||
|
||||
@@ -928,7 +929,7 @@ void AddrManImpl::ResolveCollisions_()
|
||||
} else if (vvTried[tried_bucket][tried_bucket_pos] != -1) { // The position in the tried bucket is not empty
|
||||
|
||||
// Get the to-be-evicted address that is being tested
|
||||
int id_old = vvTried[tried_bucket][tried_bucket_pos];
|
||||
nid_type id_old = vvTried[tried_bucket][tried_bucket_pos];
|
||||
AddrInfo& info_old = mapInfo[id_old];
|
||||
|
||||
const auto current_time{Now<NodeSeconds>()};
|
||||
@@ -974,11 +975,11 @@ std::pair<CAddress, NodeSeconds> AddrManImpl::SelectTriedCollision_()
|
||||
|
||||
if (m_tried_collisions.size() == 0) return {};
|
||||
|
||||
std::set<int>::iterator it = m_tried_collisions.begin();
|
||||
std::set<nid_type>::iterator it = m_tried_collisions.begin();
|
||||
|
||||
// Selects a random element from m_tried_collisions
|
||||
std::advance(it, insecure_rand.randrange(m_tried_collisions.size()));
|
||||
int id_new = *it;
|
||||
nid_type id_new = *it;
|
||||
|
||||
// If id_new not found in mapInfo remove it from m_tried_collisions
|
||||
if (mapInfo.count(id_new) != 1) {
|
||||
@@ -1063,15 +1064,15 @@ int AddrManImpl::CheckAddrman() const
|
||||
LOG_TIME_MILLIS_WITH_CATEGORY_MSG_ONCE(
|
||||
strprintf("new %i, tried %i, total %u", nNew, nTried, vRandom.size()), BCLog::ADDRMAN);
|
||||
|
||||
std::unordered_set<int> setTried;
|
||||
std::unordered_map<int, int> mapNew;
|
||||
std::unordered_set<nid_type> setTried;
|
||||
std::unordered_map<nid_type, int> mapNew;
|
||||
std::unordered_map<Network, NewTriedCount> local_counts;
|
||||
|
||||
if (vRandom.size() != (size_t)(nTried + nNew))
|
||||
return -7;
|
||||
|
||||
for (const auto& entry : mapInfo) {
|
||||
int n = entry.first;
|
||||
nid_type n = entry.first;
|
||||
const AddrInfo& info = entry.second;
|
||||
if (info.fInTried) {
|
||||
if (!TicksSinceEpoch<std::chrono::seconds>(info.m_last_success)) {
|
||||
|
||||
Reference in New Issue
Block a user