Skip to content

[p2p] Fix signed integer overflow in LocalServiceInfo::nScore - #33072

Closed
b-l-u-e wants to merge 1 commit into
bitcoin:masterfrom
b-l-u-e:p2p-fix-nscore-overflow-24049
Closed

b-l-u-e wants to merge 1 commit into
bitcoin:masterfrom
b-l-u-e:p2p-fix-nscore-overflow-24049

Conversation

@b-l-u-e

@b-l-u-e b-l-u-e commented Jul 28, 2025 •

Copy link
Copy Markdown
Contributor

The nScore field in LocalServiceInfo struct was defined as int (32-bit signed integer), which could overflow from INT_MAX (2,147,483,647) to INT_MIN (-2,147,483,648) when incremented by SeenLocal() during version handshakes. This is undefined behavior in C++ and could affect address selection logic.

Implement saturation in SeenLocal() to cap nScore at std::numeric_limits<int>::max() instead of allowing overflow. This prevents undefined behavior while maintaining memory efficiency and logical correctness - after 2 billion connections, the matter is settled.

Fixes #24049

@DrahtBot

DrahtBot commented Jul 28, 2025 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage & Benchmarks

For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/33072.

Reviews

See the guideline for information on the review process.
A summary of reviews will appear here.

Conflicts

No conflicts as of last run.

@DrahtBot

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task lint: https://github.com/bitcoin/bitcoin/runs/46822277337
LLM reason (✨ experimental): The CI failure is caused by a lint check detecting trailing whitespace errors.

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

@b-l-u-e
b-l-u-e force-pushed the p2p-fix-nscore-overflow-24049 branch 2 times, most recently from 95f83cd to 76295ef Compare July 28, 2025 02:07
@gmaxwell

gmaxwell commented Jul 28, 2025 •

Copy link
Copy Markdown
Contributor

This doesn't compile, and why not just saturate? e.g. if(nScore < std::max<int>)nScore++

"No performance impact (64-bit integers are efficient on modern systems)" sounds like LLM blather, the relevant criteria is not the performance of 64-bit integers considering how little it's used. Memory usage might matter if this was a per-peer (or per-block/header) field, but it isn't.

The purpose of it is to track input addresses to see that they were, in fact, reachable. I'm pretty sure that by the 2 billionth connection claiming to connect to it that the matter is settled.

@b-l-u-e
b-l-u-e force-pushed the p2p-fix-nscore-overflow-24049 branch from 76295ef to c5c16ad Compare July 28, 2025 10:12
@b-l-u-e

b-l-u-e commented Jul 28, 2025

Copy link
Copy Markdown
Contributor Author

@gmaxwell Thank you for the excellent feedback!
The saturation is a much better approach than changing to int64_t.
I've implemented the saturation fix as you suggested using if (nScore < std::numeric_limits::max()) nScore++.
This prevents undefined behavior while maintaining memory efficiency and logical correctness after 2 billion connections, the matter is indeed settled.
I've also added a test case to verify the saturation behavior works correctly.
Thank you for pointing me in the right direction!

@achow101

Copy link
Copy Markdown
Member

Closing this as it has not had any activity in a while. If you are interested in continuing work on this, please leave a comment so that it can be reopened.

Marking as up for grabs.

@b-l-u-e

b-l-u-e commented Dec 3, 2025

Copy link
Copy Markdown
Contributor Author

Closing this as it has not had any activity in a while. If you are interested in continuing work on this, please leave a comment so that it can be reopened.

Marking as up for grabs.

I would like to continue to work on this..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

net: signed-integer-overflow in LocalServiceInfo

5 participants