You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Can be reproduced locally in a few CPU hours via FUZZ=addrman_deserialize ./src/test/fuzz/fuzz -use_value_profile=1 (using the qa-assets seed as starting point).
The repositioning/rebucketing approach of ResetI2PPorts() introduced in #22112 seems very brittle. I think it would make much more sense just to remove any I2P addresses which don't have port 0, and then perhaps add them back through the public (and well-tested) Add() interface.
I don't know the exact bug that leads to this internal addrman inconsistency, but this loop seems suspect to me:
for (int bucket = 0; bucket < ADDRMAN_NEW_BUCKET_COUNT; ++bucket) {
for (int i = 0; i < ADDRMAN_BUCKET_SIZE; ++i) {
const auto id = vvNew[bucket][i];
if (id == -1) {
continue;
}
auto it = mapInfo.find(id);
if (it == mapInfo.end()) {
return;
}
auto& addr_info = it->second;
if (!addr_info.IsI2P() || addr_info.GetPort() == I2P_SAM31_PORT) {
continue;
}
auto addr_info_newport = addr_info;
// The below changes addr_info_newport.GetKey(), which is used in finding a
// bucket and a position within that bucket. So a re-bucketing may be necessary.
addr_info_newport.port = I2P_SAM31_PORT;
// Reposition entries of vvNew within the same bucket because we don't know the source
// address which led to the decision to store the entry in vvNew[bucket] so we can't
// re-evaluate that decision, but even if we could, CAddrInfo::GetNewBucket() does not
// use CAddrInfo::GetKey() so it would end up in the same bucket as before the port
// change.
const auto i_target = addr_info_newport.GetBucketPosition(nKey, true, bucket);
if (i_target == i) { // No need to re-position.
addr_info = addr_info_newport;
continue;
}
// Reposition from i to i_target, removing the entry from i_target (if any).
ClearNew(bucket, i_target);
vvNew[bucket][i_target] = id;
vvNew[bucket][i] = -1;
addr_info = addr_info_newport;
}
}
If the same I2P address appears in multiple new buckets, then the first time this loop finds that address, it'll update the port and reposition it in that bucket. Any subsequent times that the address is encountered, it will already have had its port updated so the inner loop will continue due to addr_info.GetPort() == I2P_SAM31_PORT, but it'll be in the wrong position in its bucket. If we later try to promote the entry from new to tried, then this loop in MakeTried() will fail to find the entry in those buckets:
// remove the entry from all new buckets
for (int bucket = 0; bucket < ADDRMAN_NEW_BUCKET_COUNT; bucket++) {
int pos = info.GetBucketPosition(nKey, true, bucket);
if (vvNew[bucket][pos] == nId) {
vvNew[bucket][pos] = -1;
info.nRefCount--;
}
}
nNew--;
I'm not sure whether this is the exact cause of this assert, but it seems like it could at least lead to inconsistencies. That's always a risk when reaching into the internal data structures of a complex class like addrman.
This is what the fuzzed addrman data produces at the end of Unserialize(), before RemoveInvalid() and before ResetI2PPorts():
nNew=-1, nTried=5
id=3, addr_info=[::]:0, in tried=1
id=2, addr_info=4ucsbzifedsqkihfauqokbja4ucsbzifedsqkihfauqokbja4ucq.b32.i2p:8192, in tried=1
id=1, addr_info=[::]:229, in tried=1
id=0, addr_info=4ucsbzifedsqkihfauqokbja4ucsbzifedsqkihfauqokbja4ucq.b32.i2p:256, in tried=1
id=-1, addr_info=[::]:8421, in tried=1
vRandom=-1,0,1,2,3,
After RemoveInvalid():
nNew=-1, nTried=3
id=2, addr_info=4ucsbzifedsqkihfauqokbja4ucsbzifedsqkihfauqokbja4ucq.b32.i2p:8192, in tried=1
id=0, addr_info=4ucsbzifedsqkihfauqokbja4ucsbzifedsqkihfauqokbja4ucq.b32.i2p:256, in tried=1
id=-1, addr_info=[::]:8421, in tried=1
vRandom=-1,0,2,
After ResetI2PPorts():
nNew=0, nTried=2
id=2, addr_info=4ucsbzifedsqkihfauqokbja4ucsbzifedsqkihfauqokbja4ucq.b32.i2p:0, in tried=0
id=0, addr_info=4ucsbzifedsqkihfauqokbja4ucsbzifedsqkihfauqokbja4ucq.b32.i2p:0, in tried=1
id=-1, addr_info=[::]:8421, in tried=1
vRandom=-1,0,2,
Later Serialize() chokes on this because there is at least one entry in mapInfo[] with nRefCount > 0 and nNew is 0. The problem originated from the unserialized negative nNew. ResetI2PPorts() correctly moved one entry from "tried" to "new" and incremented nNew (from -1 to 0).
TODO:
Why RemoveInvalid() did not remove id=-1, addr_info=[::]:8421, in tried=1?
remove any I2P addresses which don't have port 0, and then perhaps add them back through the public (and well-tested) Add() interface
I considered this approach and ditched it for some reason, but I do not remember why :/ It could have been that deleting an entry has to be done again by fiddling directly with the internal structures and was about as complicated as the current code. Will look at this again.
Activity
jonatack commentedon Jul 15, 2021
Another win for fuzzing, oh wow.
fanquake commentedon Jul 15, 2021
cc @vasild
vasild commentedon Jul 15, 2021
Fuzzer rulez!
jnewbery commentedon Jul 15, 2021
The repositioning/rebucketing approach of
ResetI2PPorts()introduced in #22112 seems very brittle. I think it would make much more sense just to remove any I2P addresses which don't have port 0, and then perhaps add them back through the public (and well-tested)Add()interface.I don't know the exact bug that leads to this internal addrman inconsistency, but this loop seems suspect to me:
If the same I2P address appears in multiple new buckets, then the first time this loop finds that address, it'll update the port and reposition it in that bucket. Any subsequent times that the address is encountered, it will already have had its port updated so the inner loop will
continuedue toaddr_info.GetPort() == I2P_SAM31_PORT, but it'll be in the wrong position in its bucket. If we later try to promote the entry from new to tried, then this loop inMakeTried()will fail to find the entry in those buckets:I'm not sure whether this is the exact cause of this assert, but it seems like it could at least lead to inconsistencies. That's always a risk when reaching into the internal data structures of a complex class like addrman.
addrman: detect on-disk corrupted nNew and nTried during unserialization
maflcko commentedon Jul 15, 2021
More crashes:
crash-ef7d4a4f940b8fdb4999f6a92969260cf8e1405f.log
vasild commentedon Jul 15, 2021
This is what the fuzzed addrman data produces at the end of
Unserialize(), beforeRemoveInvalid()and beforeResetI2PPorts():After
RemoveInvalid():After
ResetI2PPorts():Later
Serialize()chokes on this because there is at least one entry inmapInfo[]withnRefCount > 0andnNewis0. The problem originated from the unserialized negativenNew.ResetI2PPorts()correctly moved one entry from "tried" to "new" and incrementednNew(from-1to0).TODO:
RemoveInvalid()did not removeid=-1, addr_info=[::]:8421, in tried=1?@jnewbery
I considered this approach and ditched it for some reason, but I do not remember why :/ It could have been that deleting an entry has to be done again by fiddling directly with the internal structures and was about as complicated as the current code. Will look at this again.
maflcko commentedon Jul 15, 2021
Maybe related: #22362
vasild commentedon Jul 16, 2021
Just for reference, attaching the fuzzer input to reproduce the problem that is reported in the OP/description of this issue.
clusterfuzz-testcase-minimized-addrman_deserialize-5090634410098688.log
vasild commentedon Jul 16, 2021
Fix pending at #22455
jonatack commentedon Jul 17, 2021
Reproduced on my old laptop with the addrman_deserialize fuzzer
5 remaining items
addrman: detect on-disk corrupted nNew and nTried during unserialization
Merge bitcoin#22455: addrman: detect on-disk corrupted nNew and nTrie…
addrman: detect on-disk corrupted nNew and nTried during unserialization
addrman: detect on-disk corrupted nNew and nTried during unserialization
Merge bitcoin/bitcoin#22455: addrman: detect on-disk corrupted nNew a…
addrman: detect on-disk corrupted nNew and nTried during unserialization
Merge bitcoin/bitcoin#22455: addrman: detect on-disk corrupted nNew a…
Merge bitcoin/bitcoin#22455: addrman: detect on-disk corrupted nNew a…
addrman: detect on-disk corrupted nNew and nTried during unserialization
Merge bitcoin/bitcoin#22455: addrman: detect on-disk corrupted nNew a…