Skip to content

qa: check more socket API invariants for each platform we support - #36737

Open
darosior wants to merge 7 commits into
bitcoin:masterfrom
darosior:2610_sock_invariants
Open

darosior wants to merge 7 commits into
bitcoin:masterfrom
darosior:2610_sock_invariants

Conversation

@darosior

@darosior darosior commented Nov 2, 2026

Copy link
Copy Markdown
Member

This improves our Sock test coverage following CVE-2026-117350.

This leaves out the test for the specific case that led to the OOM until we fix select() reporting for urgent data, but i'm also trying not to fight the last battle too much. Instead i focused on trying to pin down all invariants Claude and I could come up with that could be reasonably tested. This will help us understand our code better, and get more assurance we do not break some assumption a (security critical) caller may be relying on further down the road.

Another goal is to tie the events our fuzzing mock can report to the events each OS actually reports, and when. A follow-up will extend the mock to match what these tests pin down. This wouldn’t have caught the CVE-2026-117350 bug: it was in the very logic the mock replaces, so a fuzz target could only have found it if the mock had been written with that bug in mind. But the next bug may well be somewhere else, and a mock that can report everything the real socket APIs can, lets the fuzzer reach corner cases which may lack a proper unit test.

A lot more coverage could be added. I chose not to add tests specific to the logic used for trusted interfaces (Tor control, the SOCKS5 proxy, I2P SAM) or to a single platform. I also left out the test which led me to find #36688 (comment), as this needs to be fixed first.

@DrahtBot DrahtBot added the Tests label Nov 2, 2026
@DrahtBot

DrahtBot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

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

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
Concept ACK sedited, jeanpablojp

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36946 (scripted-diff: [test] Add util/check.h includes for assertions by maflcko)
  • #35558 (p2p: Prefill compact blocks by davidgumberg)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

LLM Linter (✨ experimental)

Possible typos and grammar issues:

  • src/test/sock_tests.cpp: overriden → overridden [misspelling]

2026-11-03 03:58:39

@DrahtBot

DrahtBot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task test ancestor commits: https://github.com/bitcoin/bitcoin/actions/runs/41545254532/job/125619164838
LLM reason (✨ experimental): The build failed because sock_tests.cpp references undeclared AcceptedSock and set_options identifiers.

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.

@darosior
darosior force-pushed the 2610_sock_invariants branch from cbfd014 to 7b4153b Compare November 3, 2026 03:58
Test what Sock::Wait() and Sock::WaitMany() report, under a matrix of
different conditions about the state of the socket and the requested
events.

A few notable invariants exercised here:
- Only what's requested is reported (in particular, no unrequested
  RecvEvent when we took care not to ask for it, see CVE-2026-117350).
- Nothing readable means no RecvEvent (an idle connection never wakes a
  wait for receiving).
- A reset wakes a wait for SendEvent, and the send() fails for good.
- A reported event always means we can make progress (avoid spins).
- Wait() and WaitMany() are equivalent.

Readiness is level-triggered, so the tests first make sure that what the
peer did has arrived and then check what a wait reports. They do not
rely on anything not happening within some time, which would be brittle.

Where the select() and poll() backends differ, the difference is spelled
out in one place ahead of the tests, so that a change of backend has to
update it. Callers cope with these differences, except with known
quirks. For instance, Windows' select() fails when no socket requests
anything.
With many connections in random states, each waiting for random events,
check that a single WaitMany() reports for each socket the events of
that socket.
Without events, a wait returns true once its timeout expires and reports
nothing, also when a socket on which nothing is requested is readable.
Callers rely on it to pace their loops (net.cpp, httpserver.cpp) and to
detect a timeout (ConnectToSocket() in netbase.cpp, i2p.cpp, pcp.cpp).

A negative timeout is not tested: poll() would wait forever and select()
fails. No caller passes one.

Remove the wait test: it exercises strictly less than the long_timeout
test introduced in this commit.
A listening socket reports RecvEvent when, and only when, accept() will
succeed. net.cpp accepts one connection per wake-up and httpserver.cpp
accepts until accept() fails.
A non-blocking connect() that succeeds, or that is refused, wakes up a
wait for RecvEvent | SendEvent and SO_ERROR then tells the outcome, as
ConnectToSocket() expects.
What is left after a partial read, and then the end of the stream, are
reported again. net.cpp reads at most 64 KiB per wake-up and relies on
this.
Check that CreateSockOS() sockets are non-blocking and have TCP_NODELAY
and, where it exists, SO_NOSIGPIPE, and which of these a socket accepted
from one inherits.

net.cpp and httpserver.cpp do not make accepted sockets non-blocking.
On Linux they are not, as accept() does not pass O_NONBLOCK on, and the
callers rely on MSG_DONTWAIT instead. Check that Recv() and Send() with
MSG_DONTWAIT return would-block rather than wait on a socket as accept()
returns it. Where MSG_DONTWAIT does not exist (Windows), check at compile
time that accepted sockets inherit non-blocking mode, and at run time
that they do.
@sedited

sedited commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Nice, Concept ACK

@jeanpablojp jeanpablojp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK

Comment thread src/test/sock_tests.cpp
Comment on lines +686 to +688
for (const std::chrono::milliseconds timeout : {std::chrono::milliseconds{Sock::MAX_WAIT_TIMEOUT},
std::chrono::milliseconds{Sock::MAX_WAIT_TIMEOUT + 1s},
std::chrono::milliseconds::max()}) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Linux this test still passes with the cap removed from WaitMany(). With 2^32 ms, which reaches poll() as 0, the test fails without the cap.

Suggested change
for (const std::chrono::milliseconds timeout : {std::chrono::milliseconds{Sock::MAX_WAIT_TIMEOUT},
std::chrono::milliseconds{Sock::MAX_WAIT_TIMEOUT + 1s},
std::chrono::milliseconds::max()}) {
for (const std::chrono::milliseconds timeout : {std::chrono::milliseconds{Sock::MAX_WAIT_TIMEOUT},
std::chrono::milliseconds{Sock::MAX_WAIT_TIMEOUT + 1s},
std::chrono::milliseconds{int64_t{1} << 32},
std::chrono::milliseconds::max()}) {

Comment thread src/test/sock_tests.cpp
char c;
BOOST_CHECK(WouldBlock(conn.sock->Recv(&c, 1, MSG_DONTWAIT)));
conn.FillSendBuffer(MSG_NOSIGNAL | MSG_DONTWAIT);
BOOST_CHECK(WouldBlock(conn.sock->Send("x", 1, MSG_NOSIGNAL | MSG_DONTWAIT)));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check fails on macOS in 141 of 3200 runs with 8 processes at once. FillSendBuffer() already requires a would-block before it returns, so could this check go?

Comment thread src/test/sock_tests.cpp

// Otherwise, it must fail eventually. NOTE: Windows retries for about two seconds before giving up.
Sock::Event occurred{0xff};
BOOST_REQUIRE(sock.Wait(LONG_WAIT, Sock::RecvEvent | Sock::SendEvent, &occurred));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After #36716, the exception set, where FAILED_CONNECT_EVENTS expects Windows to report the failure, is only watched when ErrorEvent is requested. Once rebased on master, could this wait request ErrorEvent too?

Suggested change
BOOST_REQUIRE(sock.Wait(LONG_WAIT, Sock::RecvEvent | Sock::SendEvent, &occurred));
BOOST_REQUIRE(sock.Wait(LONG_WAIT, Sock::RecvEvent | Sock::SendEvent | Sock::ErrorEvent, &occurred));

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants