p2p/enode: add validation for binary.Varint/Uvarint in nodedb operations #33141
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds missing error checking for
binary.Varintandbinary.Uvarintcalls innodedb.goto ensure data integrity and prevent incorrect behavior when reading corrupted database entries.Problem
Missing validation in two locations leads to inconsistency and potential issues:
fetchUint64): Ignoresnreturn value - inconsistent withfetchInt64which properly validatesexpireNodes): Ignoresnreturn value when reading pong timestampsWhen
binary.Varint/Uvarintreturnsn <= 0, it indicates:n == 0: Buffer too small (incomplete varint)n < 0: Value overflow (> 64 bits)Without checks:
fetchUint64()can return garbage values from corrupted dataexpireNodes()can use invalid timestamps leading to incorrect node expiration decisionsfetchInt64()(which validates) andfetchUint64()(which doesn't)Solution
Added validation following the existing
fetchInt64()pattern (lines 203-206):For
expireNodes(), added validation with silent skip:Testing
Added comprehensive tests:
TestDBFetchUint64Corrupted: Tests truncated varint, overflow varint, empty data, and valid data casesTestDBExpireNodesCorrupted: Tests that expireNodes handles corrupted pong timestamps gracefullyContext
This fixes an inconsistency where
fetchInt64()at lines 203-206 properly validates the return value butfetchUint64()does not.