Merge bitcoin/bitcoin#36716: net: only monitor select exceptions while connecting
What changed, and why it matters
This change fixes a networking quirk in Bitcoin Core where the program could be woken up repeatedly by a rare TCP 'urgent data' signal, even though there was no real data to read. The fix makes the program only watch for that signal while a connection is still being established, not afterward. It is more of a robustness and denial-of-service-hardening fix than an exploitable remote code execution bug.
Backport to supported release branches as indicated by the PR (straightforward backport to 29.x; 31.x and earlier need adjustment for a prior scripted diff). No immediate emergency response is warranted, but the fix should ride the normal release/backport process.
Security signals we found
Avoids spurious wakeups from TCP urgent data on select()-based builds
Reduces CPU-waste / small-scale denial-of-service surface from malicious or misbehaving peers
Adds explicit ErrorEvent opt-in to Sock::Wait() API
Includes regression unit test for urgent-data behavior
Evidence from the diff
The commit modifies Sock::Wait()/WaitMany() so that a socket is added to the select() exception (error) fd_set only when ErrorEvent is explicitly requested. ConnectToSocket() now requests RecvEvent | SendEvent | ErrorEvent while waiting for a non-blocking connect() to complete. Established connections no longer monitor the exception set. This prevents TCP urgent/OOB data from causing select() to keep reporting the socket as ready when recv() has nothing normal to return. A unit test verifies that an urgent byte followed by a normal byte does not cause Wait() to report ErrorEvent when only RecvEvent was requested.
Changed components
src/util/sock.cppsrc/util/sock.hsrc/netbase.cppsrc/test/sock_tests.cppInspect captured patch +52 / −8
### src/netbase.cpp
@@ -604,7 +604,7 @@ static bool ConnectToSocket(const Sock& sock,
// Connection didn't actually fail, but is being established
// asynchronously. Thus, use async I/O api (select/poll)
// synchronously to check for successful connection with a timeout.
- const Sock::Event requested = Sock::RecvEvent | Sock::SendEvent;
+ const Sock::Event requested = Sock::RecvEvent | Sock::SendEvent | Sock::ErrorEvent;
Sock::Event occurred;
if (!sock.Wait(timeout, requested, &occurred)) {
LogDebug(BCLog::NET, "wait for connect to %s failed: %s",
### src/test/sock_tests.cpp
@@ -57,6 +57,37 @@ BOOST_AUTO_TEST_CASE(move_constructor)
BOOST_CHECK(SocketIsClosed(s));
}
+// This test sets up its own sockets and sits above the socket pair tests.
+// That keeps it easy to backport: older branches build those for POSIX only.
+BOOST_AUTO_TEST_CASE(wait_ignores_urgent_data)
+{
+ sockaddr_in addr{};
+ addr.sin_family = AF_INET;
+ addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ socklen_t addr_len{sizeof(addr)};
+
+ const Sock listener{CreateSocket()};
+ BOOST_REQUIRE_EQUAL(listener.Bind(reinterpret_cast<sockaddr*>(&addr), addr_len), 0);
+ BOOST_REQUIRE_EQUAL(listener.Listen(1), 0);
+ BOOST_REQUIRE_EQUAL(listener.GetSockName(reinterpret_cast<sockaddr*>(&addr), &addr_len), 0);
+
+ const Sock sender{CreateSocket()};
+ BOOST_REQUIRE_EQUAL(sender.Connect(reinterpret_cast<sockaddr*>(&addr), addr_len), 0);
+
+ const auto receiver{listener.Accept(nullptr, nullptr)};
+ BOOST_REQUIRE(receiver);
+
+ BOOST_REQUIRE_EQUAL(sender.Send("U", 1, MSG_OOB), 1);
+ BOOST_REQUIRE_EQUAL(sender.Send("x", 1, 0), 1);
+
+ // Wait for the regular byte sent after the urgent byte. Once it is
+ // readable, the urgent byte has arrived too. Readiness is level-triggered,
+ // so if the urgent byte is ever reported as an error, it is by now.
+ Sock::Event occurred;
+ BOOST_REQUIRE(receiver->Wait(1min, Sock::RecvEvent, &occurred));
+ BOOST_CHECK(occurred == Sock::RecvEvent);
+}
+
BOOST_AUTO_TEST_CASE(move_assignment)
{
const SOCKET s1 = CreateSocket();
### src/util/sock.cpp
@@ -216,12 +216,18 @@ bool Sock::WaitMany(std::chrono::milliseconds timeout, EventsPerSock& events_per
const auto& s = sock->m_socket;
if (events.requested & RecvEvent) {
FD_SET(s, &recv);
- FD_SET(s, &err);
}
if (events.requested & SendEvent) {
FD_SET(s, &send);
}
- if (events.requested & (RecvEvent | SendEvent)) {
+ if (events.requested & ErrorEvent) {
+ // Windows reports a failed non-blocking connect() in the exception
+ // set and nowhere else. On an established connection the set also
+ // reports TCP urgent data, which recv() does not return, so
+ // Wait() could keep returning with nothing to read.
+ FD_SET(s, &err);
+ }
+ if (events.requested & (RecvEvent | SendEvent | ErrorEvent)) {
socket_max = std::max(socket_max, s);
}
}
### src/util/sock.h
@@ -156,8 +156,14 @@ class Sock
static constexpr Event SendEvent = 0b010;
/**
- * Ignored if passed to `Wait()`, but could be set in the occurred events if an
- * exceptional condition has occurred on the socket or if it has been disconnected.
+ * Could be set in the occurred events. On Linux this happens when the
+ * socket has an error or has been disconnected, whichever events were
+ * requested. On other platforms (Windows, macOS, BSDs) this only happens
+ * when it was passed to `Wait()` and an exceptional condition has occurred
+ * on the socket.
+ *
+ * Only pass it to `Wait()` while waiting for a non-blocking connect() to
+ * complete: Windows does not report a failed connect otherwise.
*/
static constexpr Event ErrorEvent = 0b100;
@@ -173,10 +179,11 @@ class Sock
* Wait for readiness for input (recv) or output (send).
* @param[in] timeout Wait this much for at least one of the requested events to occur.
* Capped at `MAX_WAIT_TIMEOUT`.
- * @param[in] requested Wait for those events, bitwise-or of `RecvEvent` and `SendEvent`.
+ * @param[in] requested Wait for those events, bitwise-or of `RecvEvent` and `SendEvent`,
+ * optionally with `ErrorEvent`.
* @param[out] occurred If not nullptr and the function returns `true`, then this
- * indicates which of the requested events occurred (`ErrorEvent` will be added, even if
- * not requested, if an exceptional event occurs on the socket).
+ * indicates which of the requested events occurred. On Linux, `ErrorEvent` may be
+ * added without being requested.
* A timeout is indicated by return value of `true` and `occurred` being set to 0.
* @return true on success (or timeout, if `occurred` of 0 is returned), false otherwise
*/Why this scored 37/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.