net: receive TCP urgent data inline #36688

Closed
darosior wants to merge 2 commits from 2610_oobinline into master AGit
darosior commented 2026-10-28 18:58:34 +00:00 (Migrated from github.com)

There is currently nothing preventing us to receive TCP urgent data, which would never be returned by recv(). Linux's poll() is indifferent to this, but the select() socket monitoring may keep reporting it as pending on some platforms.

This would not prevent progress nor lead to a crash anymore after #36665, but it's still undesirable. Clean this up by setting SO_OOBINLINE on our sockets, so recv() returns any urgent data sent to us (and likely leads us to disconnect the peer).

This is a slight behaviour change on Linux, but i think a desirable one. First of all because if a peer is sending us funky data we might as well hear about it and disconnect them, but also because i think one lesson to draw from the recent socket related security vulnerability is that we should reduce the amount of custom logic for less well-tested platforms.

Failing to set SO_OOBINLINE was conservatively made a warning and not a failure, because its absence does not prevent node operations, it's just annoying.

There is currently nothing preventing us to receive TCP urgent data, which would never be returned by `recv()`. Linux's `poll()` is indifferent to this, but the `select()` socket monitoring may keep reporting it as pending on some platforms. This would not prevent progress nor lead to a crash anymore after #36665, but it's still undesirable. Clean this up by setting `SO_OOBINLINE` on our sockets, so `recv()` returns any urgent data sent to us (and likely leads us to disconnect the peer). This is a slight behaviour change on Linux, but i think a desirable one. First of all because if a peer is sending us funky data we might as well hear about it and disconnect them, but also because i think one lesson to draw from the recent [socket related security vulnerability](https://bitcoincore.org/en/2026/10/28/disclose-tcp-oom/) is that we should reduce the amount of custom logic for less well-tested platforms. Failing to set `SO_OOBINLINE` was conservatively made a warning and not a failure, because its absence does not prevent node operations, it's just annoying.
DrahtBot commented 2026-10-28 18:58:43 +00:00 (Migrated from github.com)

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
ACK vasild, optout21, pinheadmz, winterrdog, xyzconstant, ViniciusCestarii, jpk68

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:

  • #36434 (http: support unix sockets by pinheadmz)

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.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36688 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36688.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36688#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [vasild](https://github.com/bitcoin/bitcoin/pull/36688#pullrequestreview-5699233010), [optout21](https://github.com/bitcoin/bitcoin/pull/36688#issuecomment-6499737451), [pinheadmz](https://github.com/bitcoin/bitcoin/pull/36688#pullrequestreview-5699743185), [winterrdog](https://github.com/bitcoin/bitcoin/pull/36688#pullrequestreview-5700109579), [xyzconstant](https://github.com/bitcoin/bitcoin/pull/36688#issuecomment-6501230925), [ViniciusCestarii](https://github.com/bitcoin/bitcoin/pull/36688#pullrequestreview-5701628322), [jpk68](https://github.com/bitcoin/bitcoin/pull/36688#issuecomment-6510439356) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#36434](https://github.com/bitcoin/bitcoin/pull/36434) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36434.svg"></sub> (http: support unix sockets by pinheadmz) 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. <!--5faf32d7da4f0f540f40219e4f7537a3-->
pinheadmz commented 2026-10-28 19:07:00 +00:00 (Migrated from github.com)
concept ACK "urgent" data is dumb https://datatracker.ietf.org/doc/html/rfc6093#section-6
First-time contributor

🔵 Suggestion · An urgent pointer ahead of received data can still spin the macOS/BSD net thread

A peer that crafts TCP segments can keep the macOS/BSD select-based loop waking without readable data; this pre-existing behavior is harder to trigger than before, and its impact is uncertain.

Location: src/netbase.cpp:573 (head).

🔵 Suggestion · Urgent-data test does not check that the pending state clears

src/test/netbase_tests.cpp:651-658 waits once and reads the inline byte but never waits again. A select() platform that still flags an exception after the byte is consumed would pass, so the PR's stated select() rationale is not directly tested.

Location: src/test/netbase_tests.cpp:651 (head).

Review coverage was incomplete.


Reviewed: de87cb840791 · Full review report · Previous reports

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:7ba49ca38b129bc35f71e566efbd136b17ae87b582376d9eebd04f18959e8274:9b5e0237a3fe9e1d05225291551198f07780ddb92e41e55ade9e98c2687fc127 --> <!-- ralph-base:6ed5aad06111177c39f9b9f07a52634476380ae1 --> <!-- ralph-head:de87cb84079105968d10c8208565e65d5e7670c5 --> <!-- ralph-case:fc81fa7f978ca95784530bcaab114d57dfe2520b7768028d145ef5193da0cb1c --> **🔵 Suggestion · An urgent pointer ahead of received data can still spin the macOS/BSD net thread** A peer that crafts TCP segments can keep the macOS/BSD select-based loop waking without readable data; this pre-existing behavior is harder to trigger than before, and its impact is uncertain. Location: [`src/netbase.cpp:573`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/de87cb84079105968d10c8208565e65d5e7670c5/src/netbase.cpp#L573>) (head). **🔵 Suggestion · Urgent-data test does not check that the pending state clears** src/test/netbase_tests.cpp:651-658 waits once and reads the inline byte but never waits again. A select() platform that still flags an exception after the byte is consumed would pass, so the PR's stated select() rationale is not directly tested. Location: [`src/test/netbase_tests.cpp:651`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/de87cb84079105968d10c8208565e65d5e7670c5/src/test/netbase_tests.cpp#L651>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`de87cb840791`](<https://git.fish.foo/bitcoin/bitcoin/commit/de87cb84079105968d10c8208565e65d5e7670c5>) · [Full review report](<https://review.fish.foo/traces/36688-de87cb84079105968d10c8208565e65d5e7670c5-1747-3.html>) · [Previous reports](<https://review.fish.foo/traces/36688/>)
ViniciusCestarii (Migrated from github.com) reviewed 2026-10-28 20:12:25 +00:00
ViniciusCestarii (Migrated from github.com) left a comment

ACK 4655c2305e

Ran the test and checked it fails with the SO_OOBINLINE change reverted

ACK 4655c2305e80ef19353739a6f66e49442945ed44 Ran the test and checked it fails with the `SO_OOBINLINE` change reverted
optout21 commented 2026-10-28 22:08:31 +00:00 (Migrated from github.com)

ACK 4655c2305e

Reviewed the code, verified the test locally (on Linux).
Checked that without setting the option the new test fails.
Checked other socket creation places -- other instance found only in ipc/capnp.
Checked other SetSockOpt usages.
Looked into why TCP_NODELAY option is set again after accept, while SO_OOBINLINE is not. Apparently even setting TCP_NODELAY again is extra cautious, and not needed on any of the supported platforms. Inheritance of SO_OOBINLINE should be even more reliable, as it is a socket-level option, not TCP level. If the socket option is not inherited, the test would fail. Verified this by unsetting SO_OOBINLINE right after the Accept in the test (that causes the test to fail).

I would've liked more adding the test first, then adding the fix and updating the test. It's fine this way too.

ACK 4655c2305e80ef19353739a6f66e49442945ed44 Reviewed the code, verified the test locally (on Linux). Checked that without setting the option the new test fails. Checked other socket creation places -- other instance found only in `ipc`/`capnp`. Checked other `SetSockOpt` usages. Looked into why `TCP_NODELAY` option is set again after accept, while `SO_OOBINLINE` is not. Apparently even setting `TCP_NODELAY` again is extra cautious, and not needed on any of the supported platforms. Inheritance of `SO_OOBINLINE` should be even more reliable, as it is a socket-level option, not TCP level. If the socket option is not inherited, the test would fail. Verified this by unsetting `SO_OOBINLINE` right after the `Accept` in the test (that causes the test to fail). I would've liked more adding the test first, then adding the fix and updating the test. It's fine this way too.
xyzconstant commented 2026-10-29 02:10:04 +00:00 (Migrated from github.com)

ACK 4655c230

Receiving urgent data inline makes total sense. Nice test and code change.

ACK 4655c230 Receiving urgent data inline makes total sense. Nice test and code change.
vasild (Migrated from github.com) approved these changes 2026-10-29 09:13:22 +00:00
vasild (Migrated from github.com) left a comment

ACK 4655c2305e

Linux's poll() is indifferent to this, but the select() socket monitoring may keep reporting it as pending on some platforms.

Do you plan to completely remove the select() branch and use poll() on all platforms?

Show Signature
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

ACK 4655c2305e80ef19353739a6f66e49442945ed44

> Linux's poll() is indifferent to this, but the select() socket monitoring may keep reporting it as pending on some platforms.

Do you plan to completely remove the `select()` branch and use `poll()` on all platforms?
-----BEGIN PGP SIGNATURE-----

iQRPBAEBCAA5FiEE5k2NRWFNsHVF2czBVN8G9ktVy78FAmrjDhEbFIAAAAAABAAO
bWFudTIsMi41KzEuMTIsMiwzAAoJEFTfBvZLVcu/EzMgAIP+VL+22/8xlzvwT4oj
s96hKoKR5iukh2Lyd7M3VIhCG/ltDcQD93bO92TrvJABHxRTeAWOzYGogJlg02js
LlIF9/6iliUXlB8JyhFpWaAAb8cygAVjmK0IsAFzWOmaH96b+7PDpLR0mgN7yce9
Yrv5Ag/PNcsd0APnGJSuONGfonPWr98QFL2yVQ/0RoSt9PUh4pjF74Ejvj5h0pSr
23C0zMGudapj3TgBcdsmi455YD11DInkJdltBex3QnCa/zQIcroYv1RAYsZx+lIu
ZlZbcJlI7SiRLPfGCaseQv8RMa3WO5CD6HgO7F8gQlWuvTa+RXTZKIgTyMSkxIaU
hw1roQ/Fv9LdiC6Z7Tn/vQzBhKSc6EeUSJKE1O8d8FdABHkOo1cxJJLzOVxiGh6u
7RR7fZIfhSrb4oN7H6kfVyS5PZmuE4FlH2y5UakdJHqq3T99SkQnz/3syFSvay9J
zGRcMC2a5+lv38guZHHNYJLGSSxD0z8DbT+jU8uP0ji6UslhHY8LRbk04UbFQKh5
h27p+PpNoP3TmleXWLbJZga9eCNSQXul37JV7+lgqciYGGJcrKUYYV8r8wkdpR5T
kxw1SDSdIM5KQBnSPIH29fU5FN2DXqrPtyYXPPGC8Gg01e76KBtGzNk27Wb/LSP7
iu0Zyx1QruhsHdF4IvUkPVlFWlsRtDPxF6pZSE/OUD6uaJ2jwuuQdB01sqbTaDjf
QlRnddEci9GMAWJ8cvyenNowgDruk6U9FbeTUvCematHrcrXd6IntAZHQbH5bRxy
HQ5RPdpUAqMgMXr01hlnZEHmi3wHSdOBONVOUUi+JLa5aFmkD/18cBsjIevv22wq
VA9ETbhKeNfQNWm4GrBrAfCKJkU48kHe11qHUqq8Ige7z3GDUlzk4ELSpjALGkL/
UYdeoEk1QZpuc9jt+rmFmi7QxWWCdn26E95Lxd9kC3SZZI18rVriF/oEmxt1hvTx
vhWKA+zfcU7d3u0K89DI3MsBykuW0417RJIFAdNCc5f5LekgB7oTWNLvKUqO3q92
HOlDivcg07kL4BrkCd8gZ459yAb3J+rrOn+OzhxZox1a6TiT53H0MlvQajv3liMK
ybGyu8VBl0g32ajm6CS6y0bjIneZ8++dUJh1p1RtGnkh1ux4Fun/MIeWUZnGFRW/
vXdQ0oPT4AKfEuWMriOD48yECYJe7E0XPwVzQTtO2MforCnf5GwD+z23cUhh+D3x
chKd0CpITKAVayBDz3dBBNv/ITL1b58vP7u0D6kZ4Pu6wcbQsaqiwTehihKC9QJk
4Hy0yzspZ3g6wodtLGd4+CGTegZmVh8rQbndyaMijdfcCb9EqtVHSaIUOWAfymHE
5WI=
=aXRI
-----END PGP SIGNATURE-----

vasild's public key is on openpgp.org

ACK 4655c2305e80ef19353739a6f66e49442945ed44 > Linux's poll() is indifferent to this, but the select() socket monitoring may keep reporting it as pending on some platforms. Do you plan to completely remove the `select()` branch and use `poll()` on all platforms? <details> <summary>Show Signature</summary> ``` -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 ACK 4655c2305e80ef19353739a6f66e49442945ed44 > Linux's poll() is indifferent to this, but the select() socket monitoring may keep reporting it as pending on some platforms. Do you plan to completely remove the `select()` branch and use `poll()` on all platforms? -----BEGIN PGP SIGNATURE----- iQRPBAEBCAA5FiEE5k2NRWFNsHVF2czBVN8G9ktVy78FAmrjDhEbFIAAAAAABAAO bWFudTIsMi41KzEuMTIsMiwzAAoJEFTfBvZLVcu/EzMgAIP+VL+22/8xlzvwT4oj s96hKoKR5iukh2Lyd7M3VIhCG/ltDcQD93bO92TrvJABHxRTeAWOzYGogJlg02js LlIF9/6iliUXlB8JyhFpWaAAb8cygAVjmK0IsAFzWOmaH96b+7PDpLR0mgN7yce9 Yrv5Ag/PNcsd0APnGJSuONGfonPWr98QFL2yVQ/0RoSt9PUh4pjF74Ejvj5h0pSr 23C0zMGudapj3TgBcdsmi455YD11DInkJdltBex3QnCa/zQIcroYv1RAYsZx+lIu ZlZbcJlI7SiRLPfGCaseQv8RMa3WO5CD6HgO7F8gQlWuvTa+RXTZKIgTyMSkxIaU hw1roQ/Fv9LdiC6Z7Tn/vQzBhKSc6EeUSJKE1O8d8FdABHkOo1cxJJLzOVxiGh6u 7RR7fZIfhSrb4oN7H6kfVyS5PZmuE4FlH2y5UakdJHqq3T99SkQnz/3syFSvay9J zGRcMC2a5+lv38guZHHNYJLGSSxD0z8DbT+jU8uP0ji6UslhHY8LRbk04UbFQKh5 h27p+PpNoP3TmleXWLbJZga9eCNSQXul37JV7+lgqciYGGJcrKUYYV8r8wkdpR5T kxw1SDSdIM5KQBnSPIH29fU5FN2DXqrPtyYXPPGC8Gg01e76KBtGzNk27Wb/LSP7 iu0Zyx1QruhsHdF4IvUkPVlFWlsRtDPxF6pZSE/OUD6uaJ2jwuuQdB01sqbTaDjf QlRnddEci9GMAWJ8cvyenNowgDruk6U9FbeTUvCematHrcrXd6IntAZHQbH5bRxy HQ5RPdpUAqMgMXr01hlnZEHmi3wHSdOBONVOUUi+JLa5aFmkD/18cBsjIevv22wq VA9ETbhKeNfQNWm4GrBrAfCKJkU48kHe11qHUqq8Ige7z3GDUlzk4ELSpjALGkL/ UYdeoEk1QZpuc9jt+rmFmi7QxWWCdn26E95Lxd9kC3SZZI18rVriF/oEmxt1hvTx vhWKA+zfcU7d3u0K89DI3MsBykuW0417RJIFAdNCc5f5LekgB7oTWNLvKUqO3q92 HOlDivcg07kL4BrkCd8gZ459yAb3J+rrOn+OzhxZox1a6TiT53H0MlvQajv3liMK ybGyu8VBl0g32ajm6CS6y0bjIneZ8++dUJh1p1RtGnkh1ux4Fun/MIeWUZnGFRW/ vXdQ0oPT4AKfEuWMriOD48yECYJe7E0XPwVzQTtO2MforCnf5GwD+z23cUhh+D3x chKd0CpITKAVayBDz3dBBNv/ITL1b58vP7u0D6kZ4Pu6wcbQsaqiwTehihKC9QJk 4Hy0yzspZ3g6wodtLGd4+CGTegZmVh8rQbndyaMijdfcCb9EqtVHSaIUOWAfymHE 5WI= =aXRI -----END PGP SIGNATURE----- ``` vasild's public key is [on openpgp.org](https://keys.openpgp.org/vks/v1/by-fingerprint/850E138BA9C0923881DFD55FCE712013582E2230) </details>
vasild (Migrated from github.com) commented 2026-10-29 09:11:44 +00:00

I think it would be useful to mention which platforms report it as pending.

I think it would be useful to mention which platforms report it as pending.
vasild (Migrated from github.com) commented 2026-10-29 09:04:50 +00:00

nit: GetSockName() fills addr with some data and sets its size in the out parameter addr_len. I think logically, Connect() should take addr_len, not sizeof(addr). I guess this is mostly theoretical concern because my understanding is that both will be equal. Maybe add BOOST_REQUIRE_EQUAL(addr_len, sizeof(addr));, or s/sizeof(addr)/addr_len/ or just ignore.

nit: `GetSockName()` fills `addr` with some data and sets its size in the out parameter `addr_len`. I think logically, `Connect()` should take `addr_len`, not `sizeof(addr)`. I guess this is mostly theoretical concern because my understanding is that both will be equal. Maybe add `BOOST_REQUIRE_EQUAL(addr_len, sizeof(addr));`, or `s/sizeof(addr)/addr_len/` or just ignore.
darosior (Migrated from github.com) reviewed 2026-10-29 13:43:07 +00:00
darosior (Migrated from github.com) commented 2026-10-29 13:43:07 +00:00

Yes, using addr_len is cleaner.

Yes, using `addr_len` is cleaner.
darosior (Migrated from github.com) reviewed 2026-10-29 13:58:58 +00:00
darosior (Migrated from github.com) commented 2026-10-29 13:58:58 +00:00

Done.

Done.
darosior (Migrated from github.com) reviewed 2026-10-29 13:59:18 +00:00
darosior (Migrated from github.com) commented 2026-10-29 13:59:18 +00:00

Updated comment to apply to all platforms using select() to monitor.

Updated comment to apply to all platforms using `select()` to monitor.
pinheadmz commented 2026-10-29 14:01:11 +00:00 (Migrated from github.com)

While all the reviews are stale, quick question: should the test live in sock_tests.cpp? There is a struct TcpSocketPair that might be handy

While all the reviews are stale, quick question: should the test live in `sock_tests.cpp`? There is a `struct TcpSocketPair` that might be handy
darosior commented 2026-10-29 14:06:46 +00:00 (Migrated from github.com)

Thought about this, but CreateSockOS is netbase's function, which is a caller of util/sock. I think sock_tests should be contained to testing util/sock: it would awkward to include netbase.h there when we have netbase_tests.

Thought about this, but `CreateSockOS` is netbase's function, which is a caller of util/sock. I think sock_tests should be contained to testing util/sock: it would awkward to include netbase.h there when we have netbase_tests.
vasild (Migrated from github.com) reviewed 2026-10-29 14:18:02 +00:00
vasild (Migrated from github.com) left a comment

ACK de87cb8407

Show Signature
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

ACK de87cb84079105968d10c8208565e65d5e7670c5
-----BEGIN PGP SIGNATURE-----

iQRPBAEBCAA5FiEE5k2NRWFNsHVF2czBVN8G9ktVy78FAmrjVXUbFIAAAAAABAAO
bWFudTIsMi41KzEuMTIsMiwzAAoJEFTfBvZLVcu/sVkf/2Zmy+bfcvipHHZ7qLpp
QWKUY5CgrX85+C1t9lffaI3Jc4fJvE1srgHE8sukvJmcgR2GaRUt/di3k7pqNjh6
i8uT1Dc4aE6PvKbXwCVZIBtaLjYo3Kl6UH7ZmHrpg/qHMMLuOaigJGp0nQv02TT7
zaK8la7Zd6jnL8RJ95ZChSxfHWE0B0iVpaQ84js8r7ZOiApOBbt9eWP5GfluGxHc
T/YNHE0VpR4tpzu9qoL4qBQf+iIFjvWnzLKr7yFEHWKpaRh82Yh3/Lb2v5zixr4O
iP5I/xthMtsKBRhfHRLZLLXi10Doc+2JVRG3sArtseBeQMZNXnWz8vjvNhsh2ilu
PoRDWKRguFS32/PwLinYpm1cRqYeH4wDd8DSuVzexLF+m9QxOj7fdtityfar8k1O
/1GAgcCceMKyMW9SoA3BczNFoCNgRiHB7RCLfWEwfCHRV5LKl76aKZjsMMr4hs4E
3C1YQ6mbCXF7hmTVzB0EMJUvZQ9CmUgqYsrvF6O9xRswPm6E8PfWMLXkQS83ffbm
bL0D1znMGEb40S8R3oF4CiEApPSUK8OZjpDpYxhKQBEm17SUovhINDHB2aoewltw
hLIORRnN2R7MwHVSUDLYpaRG30NqSggsOe7bZNwRGbddLjeRxNsRK6bUqLe056Zb
WNhlHIknpq/2zrOqe2iDUAaJNVKm+fzSs0YuyGyOqfUfqx9v9ewoNClNQ/ZZTimm
5fEvmmVCzRaf3FdCrDtgnLM+ntZS5oD/0ZG0mlwB3jsj4qXss274lIQP8bdN5JC/
XheETOx1Ygd41Zky6Q517exC40RJ2GgW7GmF10n37bfFjx/P0/R/bVfSIfKUGJQo
fHiBuwWmIW/sxIoBq3A60ShwsJwhuAquxGOAjvjh4MfHuQGNyEtia18qwKclSKJt
9jQMl/Y1zdC9cEq7u7E/fD5NBvT88R79Y/loL8c++G/QaAEDaYiepnZ9oiT/KNQB
Oqth3TW2IzXo9tpiAHCa901a3BhlS1kowoolvU21d9ZOnerUxoenyxQ/au5eNUax
wwBYmV/KDlExKvqKHTZI0l5IFh8KgZ3meHdcBnuc4x4bulraruS3JLdDtItJdKLe
fEnBqR2BdPIfLavYFcdNR9KjvQovLsFMPRTz6ODvhGv4lnF/8sZyioZ5GQt0SDV+
K3178BgwBofYHITQGIXUGE+h2mOh/1lswAXz92pVS92CZsj+v5GjN7GbnLThwdtq
Eg7GhhuSdpGURPe5+ztLn5WnqW/n0IJ6Ywtuoev3Hcdo+HAicF9DosrWt0ZvVGzz
B+6oCqs/McCBPKziR6bCT14Dqh9BeS4tFJDPizKYgzWrG1Cv5iAzg/SMBO4p8DYd
qhg=
=qCHp
-----END PGP SIGNATURE-----

vasild's public key is on openpgp.org

ACK de87cb84079105968d10c8208565e65d5e7670c5 <details> <summary>Show Signature</summary> ``` -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 ACK de87cb84079105968d10c8208565e65d5e7670c5 -----BEGIN PGP SIGNATURE----- iQRPBAEBCAA5FiEE5k2NRWFNsHVF2czBVN8G9ktVy78FAmrjVXUbFIAAAAAABAAO bWFudTIsMi41KzEuMTIsMiwzAAoJEFTfBvZLVcu/sVkf/2Zmy+bfcvipHHZ7qLpp QWKUY5CgrX85+C1t9lffaI3Jc4fJvE1srgHE8sukvJmcgR2GaRUt/di3k7pqNjh6 i8uT1Dc4aE6PvKbXwCVZIBtaLjYo3Kl6UH7ZmHrpg/qHMMLuOaigJGp0nQv02TT7 zaK8la7Zd6jnL8RJ95ZChSxfHWE0B0iVpaQ84js8r7ZOiApOBbt9eWP5GfluGxHc T/YNHE0VpR4tpzu9qoL4qBQf+iIFjvWnzLKr7yFEHWKpaRh82Yh3/Lb2v5zixr4O iP5I/xthMtsKBRhfHRLZLLXi10Doc+2JVRG3sArtseBeQMZNXnWz8vjvNhsh2ilu PoRDWKRguFS32/PwLinYpm1cRqYeH4wDd8DSuVzexLF+m9QxOj7fdtityfar8k1O /1GAgcCceMKyMW9SoA3BczNFoCNgRiHB7RCLfWEwfCHRV5LKl76aKZjsMMr4hs4E 3C1YQ6mbCXF7hmTVzB0EMJUvZQ9CmUgqYsrvF6O9xRswPm6E8PfWMLXkQS83ffbm bL0D1znMGEb40S8R3oF4CiEApPSUK8OZjpDpYxhKQBEm17SUovhINDHB2aoewltw hLIORRnN2R7MwHVSUDLYpaRG30NqSggsOe7bZNwRGbddLjeRxNsRK6bUqLe056Zb WNhlHIknpq/2zrOqe2iDUAaJNVKm+fzSs0YuyGyOqfUfqx9v9ewoNClNQ/ZZTimm 5fEvmmVCzRaf3FdCrDtgnLM+ntZS5oD/0ZG0mlwB3jsj4qXss274lIQP8bdN5JC/ XheETOx1Ygd41Zky6Q517exC40RJ2GgW7GmF10n37bfFjx/P0/R/bVfSIfKUGJQo fHiBuwWmIW/sxIoBq3A60ShwsJwhuAquxGOAjvjh4MfHuQGNyEtia18qwKclSKJt 9jQMl/Y1zdC9cEq7u7E/fD5NBvT88R79Y/loL8c++G/QaAEDaYiepnZ9oiT/KNQB Oqth3TW2IzXo9tpiAHCa901a3BhlS1kowoolvU21d9ZOnerUxoenyxQ/au5eNUax wwBYmV/KDlExKvqKHTZI0l5IFh8KgZ3meHdcBnuc4x4bulraruS3JLdDtItJdKLe fEnBqR2BdPIfLavYFcdNR9KjvQovLsFMPRTz6ODvhGv4lnF/8sZyioZ5GQt0SDV+ K3178BgwBofYHITQGIXUGE+h2mOh/1lswAXz92pVS92CZsj+v5GjN7GbnLThwdtq Eg7GhhuSdpGURPe5+ztLn5WnqW/n0IJ6Ywtuoev3Hcdo+HAicF9DosrWt0ZvVGzz B+6oCqs/McCBPKziR6bCT14Dqh9BeS4tFJDPizKYgzWrG1Cv5iAzg/SMBO4p8DYd qhg= =qCHp -----END PGP SIGNATURE----- ``` vasild's public key is [on openpgp.org](https://keys.openpgp.org/vks/v1/by-fingerprint/850E138BA9C0923881DFD55FCE712013582E2230) </details>
optout21 commented 2026-10-29 14:41:10 +00:00 (Migrated from github.com)

reACK de87cb8407

reACK de87cb84079105968d10c8208565e65d5e7670c5
pinheadmz (Migrated from github.com) approved these changes 2026-10-29 14:52:00 +00:00
pinheadmz (Migrated from github.com) left a comment

ACK de87cb8407

built and tested on macos/arm64. Generated functional tests that send urgent bytes to the http server: https://gist.github.com/pinheadmz/f5bb77865f870865e44c54d5751b569a these pass on the branch and fail on master. I also wanted to check what happens if the "urgent" flag is set but the urgent byte itself has not been sent yet.

Show Signature
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

ACK de87cb84079105968d10c8208565e65d5e7670c5
-----BEGIN PGP SIGNATURE-----

iQJPBAEBCAA5FiEE5hdzzW4BBA4vG9eM5+KYS2KJyToFAmrjXRUbFIAAAAAABAAO
bWFudTIsMi41KzEuMTIsMCwzAAoJEOfimEtiick6itYQAKeKz4XjNHEcyiGMmrYC
iBu3JrcyY5qKZONatgo+vMIJurzwKxZTR7DPrqVr7ZjnBx1AhJpDUTWEed3fkLj0
leoDWGFnt+PUnn8BX8RzRQPdjmddSuXVEVHyVTYVJriUVpSUTwQ1PQC2TaK+c4jF
Biyry9lWry+onb6s4V3nKqmF3GcYwc08LhFaev+6L6s7SjlzEJge0oh9dNUKXH67
XUWXb9w34TowVuC5z6bicjzgYKDIeVH1y/jXDFNjaSW7aF4D7P6bknvEAmjT1uRx
zLZ63F754VZQd47s6A4gEsYdUZzwoxt1TRsMQ7Lyr+jNIwJogVL7kh+Z+ZI82LqZ
8szhu7EjoIs8ORr10a9UpKSagkHvq4X8T4EyVhBmGpowLu0P+0wSxotQkRTaFVfx
wZ8Wc046sTg85+R2LTyxbnzh90l2opPiXNyKrcGmlW9Y+bxgasuuo4TNocyUtwvm
YchBjVpgyzn2e6PzM8AODLCbrtplncZdQbTqC5lzE8bbhG+xY5uTGOlYpy3FqKjf
ccS7ohwgKSz+gKeV7coZ4jFUrwARfTGS4909TDpkz9ytFneDt0vkF2y+DjBDeRzj
s13N8gVSJBfmqifuTOjnjQbPYAnFBXcPxn8fInzvCkQk7G3Y3wmLoSF4O5/k7r7a
B3oQYUzIGxxBIvOK9GbEptfr
=Z1pC
-----END PGP SIGNATURE-----

pinheadmz's public key is on openpgp.org

ACK de87cb84079105968d10c8208565e65d5e7670c5 built and tested on macos/arm64. Generated functional tests that send urgent bytes to the http server: https://gist.github.com/pinheadmz/f5bb77865f870865e44c54d5751b569a these pass on the branch and fail on master. I also wanted to check what happens if the "urgent" flag is set but the urgent byte itself has not been sent yet. <details><summary>Show Signature</summary> ``` -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 ACK de87cb84079105968d10c8208565e65d5e7670c5 -----BEGIN PGP SIGNATURE----- iQJPBAEBCAA5FiEE5hdzzW4BBA4vG9eM5+KYS2KJyToFAmrjXRUbFIAAAAAABAAO bWFudTIsMi41KzEuMTIsMCwzAAoJEOfimEtiick6itYQAKeKz4XjNHEcyiGMmrYC iBu3JrcyY5qKZONatgo+vMIJurzwKxZTR7DPrqVr7ZjnBx1AhJpDUTWEed3fkLj0 leoDWGFnt+PUnn8BX8RzRQPdjmddSuXVEVHyVTYVJriUVpSUTwQ1PQC2TaK+c4jF Biyry9lWry+onb6s4V3nKqmF3GcYwc08LhFaev+6L6s7SjlzEJge0oh9dNUKXH67 XUWXb9w34TowVuC5z6bicjzgYKDIeVH1y/jXDFNjaSW7aF4D7P6bknvEAmjT1uRx zLZ63F754VZQd47s6A4gEsYdUZzwoxt1TRsMQ7Lyr+jNIwJogVL7kh+Z+ZI82LqZ 8szhu7EjoIs8ORr10a9UpKSagkHvq4X8T4EyVhBmGpowLu0P+0wSxotQkRTaFVfx wZ8Wc046sTg85+R2LTyxbnzh90l2opPiXNyKrcGmlW9Y+bxgasuuo4TNocyUtwvm YchBjVpgyzn2e6PzM8AODLCbrtplncZdQbTqC5lzE8bbhG+xY5uTGOlYpy3FqKjf ccS7ohwgKSz+gKeV7coZ4jFUrwARfTGS4909TDpkz9ytFneDt0vkF2y+DjBDeRzj s13N8gVSJBfmqifuTOjnjQbPYAnFBXcPxn8fInzvCkQk7G3Y3wmLoSF4O5/k7r7a B3oQYUzIGxxBIvOK9GbEptfr =Z1pC -----END PGP SIGNATURE----- ``` pinheadmz's public key is [on openpgp.org](https://keys.openpgp.org/vks/v1/by-fingerprint/E61773CD6E01040E2F1BD78CE7E2984B6289C93A) </details>
pinheadmz (Migrated from github.com) commented 2026-10-29 14:49:42 +00:00

nit if u touch, on and set above could be cleaned up like we did in httpserver.cpp

static constexpr int SOCKET_OPTION_TRUE{1};

nit if u touch, `on` and `set` above could be cleaned up like we did in httpserver.cpp `static constexpr int SOCKET_OPTION_TRUE{1};`
darosior commented 2026-10-29 15:03:09 +00:00 (Migrated from github.com)

Do you plan to completely remove the select() branch and use poll() on all platforms?

Yes. I am currently working on pinning down all the OS-specific behaviour each implementation is relying on first, to provide more assurance a switch to poll() is safe (and help minimize the risk of more security vulns in our socket logic in the future).

> Do you plan to completely remove the `select()` branch and use `poll()` on all platforms? Yes. I am currently working on pinning down all the OS-specific behaviour each implementation is relying on first, to provide more assurance a switch to poll() is safe (and help minimize the risk of more security vulns in our socket logic in the future).
winterrdog (Migrated from github.com) reviewed 2026-10-29 15:12:07 +00:00
winterrdog (Migrated from github.com) left a comment
ACK de87cb84079105968d10c8208565e65d5e7670c5
xyzconstant commented 2026-10-29 15:40:09 +00:00 (Migrated from github.com)

re-ACK de87cb8407

re-ACK de87cb84079105968d10c8208565e65d5e7670c5
ViniciusCestarii (Migrated from github.com) reviewed 2026-10-29 16:51:10 +00:00
ViniciusCestarii (Migrated from github.com) left a comment

reACK de87cb8407

reACK de87cb84079105968d10c8208565e65d5e7670c5
darosior commented 2026-10-29 20:38:54 +00:00 (Migrated from github.com)

Let's hold off on merging this, i think i found something wrong here.

Let's hold off on merging this, i think i found something wrong here.
darosior commented 2026-10-29 22:04:04 +00:00 (Migrated from github.com)

The patch here is correct, but insufficient to address the issue entirely on macOS/BSD.

This comment from @pinheadmz got me wondering:

I also wanted to check what happens if the "urgent" flag is set but the urgent byte itself has not been sent yet.

So i checked this as part of my socket tests hardening work, and it turns out that if you also set the urgent data pointer in the TCP segment header past the last byte you sent, select() on macOS / BSD keeps waking up while the socket is waiting to receive, until data past the mark has been read. Testing this is annoying (and can't reliably be done in our test suite) because the socket API won't let you set that pointer arbitrarily. So this requires writing IP packets directly in a raw socket, or some more involved hackery on Windows 1.

So a peer receiving such a packet on:

  • Linux is unaffected (poll() without POLLPRI)
  • Windows will not raise the exception set (with SO_OOBINLINE)
  • macOS will raise the exception set (it does so as soon as it sees the pointer, even if the byte itself hasn't arrived yet, regardless of SO_OOBINLINE)

Thankfully the exception set is only necessary on Windows, to detect a failed connection attempt. So we could ifdef it away, which would fix the macOS/BSD issue. But this is a bigger change, and i think the right solution is to just move to using poll() for all platforms.

Given the patch in this PR is simple, reviewed, and already fixes Windows, i think the complete fix for macOS should happen in a followup that either switches it to using poll() or gates the exception set to only be set for Windows.


  1. My bot was able to test this by crafting Ethernet frame and sending them to QEMU over UDP for it to inject them into the emulated NIC of the Windows VM.. Whatever. ↩︎

The patch here is correct, but insufficient to address the issue entirely on macOS/BSD. This comment from @pinheadmz got me wondering: > I also wanted to check what happens if the "urgent" flag is set but the urgent byte itself has not been sent yet. So i checked this as part of my socket tests hardening work, and it turns out that if you also set the urgent data pointer in the TCP segment header past the last byte you sent, `select()` on macOS / BSD keeps waking up while the socket is waiting to receive, until data past the mark has been read. Testing this is annoying (and can't reliably be done in our test suite) because the socket API won't let you set that pointer arbitrarily. So this requires writing IP packets directly in a raw socket, or some more involved hackery on Windows [^1]. So a peer receiving such a packet on: - Linux is unaffected (`poll()` without `POLLPRI`) - Windows will not raise the exception set (with `SO_OOBINLINE`) - macOS will raise the exception set (it does so as soon as it sees the pointer, even if the byte itself hasn't arrived yet, regardless of `SO_OOBINLINE`) Thankfully the exception set is only necessary on Windows, to detect a failed connection attempt. So we could `ifdef` it away, which would fix the macOS/BSD issue. But this is a bigger change, and i think the right solution is to just move to using `poll()` for all platforms. Given the patch in this PR is simple, reviewed, and already fixes Windows, i think the complete fix for macOS should happen in a followup that either switches it to using `poll()` or gates the exception set to only be set for Windows. [^1]: My bot was able to test this by crafting Ethernet frame and sending them to QEMU over UDP for it to inject them into the emulated NIC of the Windows VM.. Whatever.
darosior commented 2026-10-29 22:05:40 +00:00 (Migrated from github.com)

Looks like Ralph had caught the issue, too. cc @willcl-ark

Looks like Ralph [had caught the issue](https://git.fish.foo/bitcoin/bitcoin/pulls/36688#issuecomment-612918), too. cc @willcl-ark
jpk68 commented 2026-10-29 22:55:39 +00:00 (Migrated from github.com)
ACK de87cb84079105968d10c8208565e65d5e7670c5
Sjors commented 2026-10-30 07:29:59 +00:00 (Migrated from github.com)

Would it make sense to add this?

diff --git a/src/net.cpp b/src/net.cpp
index 9bc7887a3c..3e6c1a0ae3 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -2200,4 +2200,15 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
         }
 
+#ifndef USE_POLL
+        if (errorSet) {
+            // select() can report an exceptional condition that recv() cannot clear,
+            // such as urgent data if SO_OOBINLINE failed, or an urgent pointer to
+            // data not yet received. Disconnect instead of repeatedly waking.
+            LogDebug(BCLog::NET, "socket exceptional condition, %s", pnode->DisconnectMsg());
+            pnode->CloseSocketDisconnect();
+            continue;
+        }
+#endif
+
         if (sendSet) {
             // Send data

In other words: hang up if we receive an urgent byte, on platforms with select().

We could avoid SO_OOBINLINE altogether if we treat the presence of URG (after a connection is established) as misbehavior. We could do that on poll() implementations too, even though it's not a problem there currently.

I'm not sure if the errorSet() can be triggered by issues other than a peer sending URG, besides what we already handle in ConnectToSocket. That might cause an "underserved" disconnect. But with SO_OOBINLINE such conditions would probably put random bytes in the buffer that would lead to a disconnect, per the original motivation of this PR.

More subjectively, I find SO_OOBINLINE to be a confusing feature. So if we don't need it, that would be nice.

Would it make sense to add this? ```diff diff --git a/src/net.cpp b/src/net.cpp index 9bc7887a3c..3e6c1a0ae3 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -2200,4 +2200,15 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes, } +#ifndef USE_POLL + if (errorSet) { + // select() can report an exceptional condition that recv() cannot clear, + // such as urgent data if SO_OOBINLINE failed, or an urgent pointer to + // data not yet received. Disconnect instead of repeatedly waking. + LogDebug(BCLog::NET, "socket exceptional condition, %s", pnode->DisconnectMsg()); + pnode->CloseSocketDisconnect(); + continue; + } +#endif + if (sendSet) { // Send data ``` In other words: hang up if we receive an urgent byte, on platforms with `select()`. We could avoid `SO_OOBINLINE` altogether if we treat the presence of URG (after a connection is established) as misbehavior. We could do that on `poll()` implementations too, even though it's not a problem there currently. I'm not sure if the `errorSet()` can be triggered by issues other than a peer sending `URG`, besides what we already handle in `ConnectToSocket`. That might cause an "underserved" disconnect. But with `SO_OOBINLINE` such conditions would probably put random bytes in the buffer that would lead to a disconnect, per the original motivation of this PR. More subjectively, I find `SO_OOBINLINE` to be a confusing feature. So if we don't need it, that would be nice.
vasild commented 2026-10-30 08:42:23 +00:00 (Migrated from github.com)

The patch is fine and I retain my ACK as it is, but this angle is worth considering:

More subjectively, I find SO_OOBINLINE to be a confusing feature. So if we don't need it, that would be nice.

So, we do not use out-of-bound data and we do not care about it, but we would set SO_OOBINLINE even though we do not expect to receive any OOB, just to fix a problem elsewhere, hmm?

From this angle, @Sjors's idea to disconnect if we receive OOB seems more in line with our expectations to not receive OOB. Also, the #ifndef USE_POLL snippet would naturally go away if we use poll(2) on all platforms.

The patch is fine and I retain my ACK as it is, but this angle is worth considering: > More subjectively, I find `SO_OOBINLINE` to be a confusing feature. So if we don't need it, that would be nice. So, we do not use out-of-bound data and we do not care about it, but we would set `SO_OOBINLINE` even though we do not expect to receive any OOB, just to fix a problem elsewhere, hmm? From this angle, @Sjors's idea to disconnect if we receive OOB seems more in line with our expectations to not receive OOB. Also, the `#ifndef USE_POLL` snippet would naturally go away if we use `poll(2)` on all platforms.
darosior commented 2026-10-30 14:31:48 +00:00 (Migrated from github.com)

@Sjors @vasild i think that works but is fragile. The exception set is not specific to urgent data, it is a general set of descriptors being watched for "exceptional conditions". In practice, it is only used for urgent data for sockets on POSIX, but Windows also uses it to report failed outgoing connections for non-blocking sockets. So your patch works because it only disconnects on exceptions after the connection was established. I think this is brittle to rely on the exception set to always indicate presence of urgent data, and to implement a low-level socket check at the connection handler level.

More subjectively, I find SO_OOBINLINE to be a confusing feature. So if we don't need it, that would be nice.

I would say the opposite, actually. It's the default of pulling the urgent byte out of the stream which is confusing, and SO_OOBINLINE restores the socket API to behave as you would expect. As mentioned above, even RFC 6093 calls the default "broken" and states that SO_OOBINLINE restores the "semantics intended by the IETF specifications".

I think a better approach is what i mentioned in my earlier message: at the socket level, only ever register the exception set for Windows.

--- a/src/util/sock.cpp
+++ b/src/util/sock.cpp
@@ -216,7 +216,11 @@ bool Sock::WaitMany(std::chrono::milliseconds timeout, EventsPerSock& events_per
         const auto& s = sock->m_socket;
         if (events.requested & RecvEvent) {
             FD_SET(s, &recv);
+#ifdef WIN32
+            // Windows reports a failed non-blocking connect() only in the exception set. Other
+            // platforms report it as readable and writable.
             FD_SET(s, &err);
+#endif
         }
         if (events.requested & SendEvent) {
             FD_SET(s, &send);

That would solve the remaining issue in this PR. I would still prefer ditching select() for macOS/BSD, but i guess we could do both, so we can backport that smaller change.

@Sjors @vasild i think that works but is fragile. The exception set is not specific to urgent data, it is a general set of descriptors being watched for "exceptional conditions". In practice, it is only used for urgent data for sockets on POSIX, but Windows also uses it to report failed outgoing connections for non-blocking sockets. So your patch works because it only disconnects on exceptions after the connection was established. I think this is brittle to rely on the exception set to always indicate presence of urgent data, and to implement a low-level socket check at the connection handler level. > More subjectively, I find `SO_OOBINLINE` to be a confusing feature. So if we don't need it, that would be nice. I would say the opposite, actually. It's the default of pulling the urgent byte out of the stream which is confusing, and `SO_OOBINLINE` restores the socket API to behave as you would expect. As [mentioned](https://github.com/bitcoin/bitcoin/pull/36688#issuecomment-6478096874) above, even RFC 6093 calls the default "broken" and states that `SO_OOBINLINE` restores the "semantics intended by the IETF specifications". I think a better approach is what i mentioned in my earlier message: at the socket level, only ever register the exception set for Windows. ```diff --- a/src/util/sock.cpp +++ b/src/util/sock.cpp @@ -216,7 +216,11 @@ bool Sock::WaitMany(std::chrono::milliseconds timeout, EventsPerSock& events_per const auto& s = sock->m_socket; if (events.requested & RecvEvent) { FD_SET(s, &recv); +#ifdef WIN32 + // Windows reports a failed non-blocking connect() only in the exception set. Other + // platforms report it as readable and writable. FD_SET(s, &err); +#endif } if (events.requested & SendEvent) { FD_SET(s, &send); ``` That would solve the remaining issue in this PR. I would still prefer ditching select() for macOS/BSD, but i guess we could do both, so we can backport that smaller change.
theuni commented 2026-10-30 15:41:55 +00:00 (Migrated from github.com)

So, we do not use out-of-bound data and we do not care about it, but we would set SO_OOBINLINE even though we do not expect to receive any OOB, just to fix a problem elsewhere, hmm?

I agree with @vasild here. OOB data has no meaning in our protocol. We would never set it ourselves, and we would never take any action based on it.

Either the data was never necessary and can be ignored, or the data was never necessary and should be treated like an error. But I don't see "the data was previously ignored but is now considered valuable", as this PR does, as a solution.

Imo, we shouldn't be worrying ourselves by having to research what platforms might be setting OOB data and what it may mean. Because it has no value to us, I'd rather just always treat it like an error. And same goes for any other exception cases.

Something like (simplified and untested):

diff --git a/src/net.cpp b/src/net.cpp
index 1201d3bcb47..f47a7277560 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -2184,6 +2184,14 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
             }
         }

+        if (errorSet) {
+            if (!pnode->fDisconnect) {
+                LogDebug(BCLog::NET, "socket recv exception, %s", pnode->DisconnectMsg());
+            }
+            pnode->CloseSocketDisconnect();
+            continue;
+        }
+
         if (sendSet) {
             // Send data
             auto [bytes_sent, data_left] = WITH_LOCK(pnode->cs_vSend, return SocketSendData(*pnode));
@@ -2201,7 +2209,7 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
             }
         }

-        if (recvSet || errorSet)
+        if (recvSet)
         {
             // typical socket buffer is 8K-64K
             uint8_t pchBuf[0x10000];
diff --git a/src/util/sock.cpp b/src/util/sock.cpp
index 4204ffe399c..99dc1488dc5 100644
--- a/src/util/sock.cpp
+++ b/src/util/sock.cpp
@@ -196,7 +196,7 @@ bool Sock::WaitMany(std::chrono::milliseconds timeout, EventsPerSock& events_per
         if (pfds[i].revents & POLLOUT) {
             events.occurred |= SendEvent;
         }
-        if (pfds[i].revents & (POLLERR | POLLHUP)) {
+        if (pfds[i].revents & (POLLERR | POLLHUP | POLLPRI)) {
             events.occurred |= ErrorEvent;
         }
         ++i;
> So, we do not use out-of-bound data and we do not care about it, but we would set `SO_OOBINLINE` even though we do not expect to receive any OOB, just to fix a problem elsewhere, hmm? I agree with @vasild here. OOB data has no meaning in our protocol. We would never set it ourselves, and we would never take any action based on it. Either the data was never necessary and can be ignored, or the data was never necessary and should be treated like an error. But I don't see "the data was previously ignored but is now considered valuable", as this PR does, as a solution. Imo, we shouldn't be worrying ourselves by having to research what platforms might be setting OOB data and what it may mean. Because it has no value to us, I'd rather just always treat it like an error. And same goes for any other exception cases. Something like (simplified and untested): ```diff diff --git a/src/net.cpp b/src/net.cpp index 1201d3bcb47..f47a7277560 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -2184,6 +2184,14 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes, } } + if (errorSet) { + if (!pnode->fDisconnect) { + LogDebug(BCLog::NET, "socket recv exception, %s", pnode->DisconnectMsg()); + } + pnode->CloseSocketDisconnect(); + continue; + } + if (sendSet) { // Send data auto [bytes_sent, data_left] = WITH_LOCK(pnode->cs_vSend, return SocketSendData(*pnode)); @@ -2201,7 +2209,7 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes, } } - if (recvSet || errorSet) + if (recvSet) { // typical socket buffer is 8K-64K uint8_t pchBuf[0x10000]; diff --git a/src/util/sock.cpp b/src/util/sock.cpp index 4204ffe399c..99dc1488dc5 100644 --- a/src/util/sock.cpp +++ b/src/util/sock.cpp @@ -196,7 +196,7 @@ bool Sock::WaitMany(std::chrono::milliseconds timeout, EventsPerSock& events_per if (pfds[i].revents & POLLOUT) { events.occurred |= SendEvent; } - if (pfds[i].revents & (POLLERR | POLLHUP)) { + if (pfds[i].revents & (POLLERR | POLLHUP | POLLPRI)) { events.occurred |= ErrorEvent; } ++i; ```
theuni commented 2026-10-30 15:49:20 +00:00 (Migrated from github.com)

Heh, I missed @Sjors's comment. I agree with that. My suggestion is the same as his, just with poll's behavior unified.

Heh, I missed @Sjors's comment. I agree with that. My suggestion is the same as his, just with `poll`'s behavior unified.
theuni commented 2026-10-30 16:08:55 +00:00 (Migrated from github.com)

I would say the opposite, actually. It's the default of pulling the urgent byte out of the stream which is confusing, and SO_OOBINLINE restores the socket API to behave as you would expect. As mentioned above, even RFC 6093 calls the default "broken" and states that SO_OOBINLINE restores the "semantics intended by the IETF specifications".

You missed this part:

Even though applications SHOULD NOT employ the urgent mechanism

The brokenness of the semantics is only relevant if you're interested in the OOB data to begin with, which we're not. Following the spec would be to ignore the urgent mechanism or treat it as an error.

> I would say the opposite, actually. It's the default of pulling the urgent byte out of the stream which is confusing, and `SO_OOBINLINE` restores the socket API to behave as you would expect. As [mentioned](https://github.com/bitcoin/bitcoin/pull/36688#issuecomment-6478096874) above, even RFC 6093 calls the default "broken" and states that `SO_OOBINLINE` restores the "semantics intended by the IETF specifications". You missed this part: > Even though applications SHOULD NOT employ the urgent mechanism The brokenness of the semantics is only relevant if you're interested in the OOB data to begin with, which we're not. Following the spec would be to ignore the urgent mechanism or treat it as an error.
darosior commented 2026-10-30 16:20:46 +00:00 (Migrated from github.com)

@theuni i did not. I am not suggesting to set SO_OOBINLINE because our application needs urgent data, on the contrary. SO_OOBINLINE is simply the standard way to opt out of the broken default urgent-data handling, as RFC 6093 puts it.

I agree we could instead disconnect on an urgent data notification 1, but because Windows also needs the exception set, what you suggest leads to a layer violation by which the connection handler needs to know that an exception raised by the socket handler is necessary related to TCP urgent data. Instead of watching for urgent data on more platforms, why not go the other way instead, and only register the exception set on Windows where it's actually necessary?

I think we agree on the observation. Our application does not need urgent data, and we should discard it and preferably disconnect anyone trying to send it to us. I think your suggestion is acceptable, to be clear, but i think it actually bakes more assumption on this obscure TCP urgent data mechanism, and more platform-specific assumptions at a higher level of our stack, than is necessary to discard such data. (And now this platform-specific assumption needs to spread to other higher-level parts of our stack, such as the HTTP server, which your patch lacks.)


  1. This PR does disconnect on an urgent byte read in non-adversarial cases too, because it would break parsing unless it is strategically positioned. ↩︎

@theuni i did not. I am not suggesting to set `SO_OOBINLINE` because our application needs urgent data, on the contrary. `SO_OOBINLINE` is simply the standard way to opt out of the broken default urgent-data handling, as RFC 6093 [puts it](https://datatracker.ietf.org/doc/html/rfc6093#section-3.1). I agree we could instead disconnect on an urgent data notification [^1], but because Windows also needs the exception set, what you suggest leads to a layer violation by which the connection handler needs to know that an exception raised by the socket handler is necessary related to TCP urgent data. Instead of watching for urgent data on more platforms, why not go the other way instead, and only register the exception set on Windows where it's actually necessary? I think we agree on the observation. Our application does not need urgent data, and we should discard it and preferably disconnect anyone trying to send it to us. I think your suggestion is acceptable, to be clear, but i think it actually bakes more assumption on this obscure TCP urgent data mechanism, and more platform-specific assumptions at a higher level of our stack, than is necessary to discard such data. (And now this platform-specific assumption needs to spread to other higher-level parts of our stack, such as the HTTP server, which your patch lacks.) [^1]: This PR does disconnect on an urgent byte read in non-adversarial cases too, because it would break parsing unless it is strategically positioned.
theuni commented 2026-10-30 16:35:08 +00:00 (Migrated from github.com)

because Windows also needs the exception set, what you suggest leads to a layer violation by which the connection handler needs to know that an exception raised by the socket handler is necessary related to TCP urgent data

I'm not following. Or at least, select() already violates this implicitly. Adding it as an error for poll makes it explicit that we consider this (previously unspecified) case should be treated as an exception, as select does. That makes it so that any exception can be treated as an error, which means that Windows will just do the right thing and disconnect.

Am I missing something?

This PR does disconnect on an urgent byte read in non-adversarial cases too, because it would break parsing unless it is strategically positioned

Relying on our checksum to notice the extra data seems like a much more substantial layer violation :)

> because Windows also needs the exception set, what you suggest leads to a layer violation by which the connection handler needs to know that an exception raised by the socket handler is necessary related to TCP urgent data I'm not following. Or at least, `select()` already violates this implicitly. Adding it as an error for `poll` makes it explicit that we consider this (previously unspecified) case should be treated as an exception, as `select` does. That makes it so that any exception can be treated as an error, which means that Windows will just do the right thing and disconnect. Am I missing something? > This PR does disconnect on an urgent byte read in non-adversarial cases too, because it would break parsing unless it is strategically positioned Relying on our checksum to notice the extra data seems like a much more substantial layer violation :)
theuni commented 2026-10-30 16:49:02 +00:00 (Migrated from github.com)

And now this platform-specific assumption needs to spread to other higher-level parts of our stack, such as the HTTP server, which your patch lacks.

I don't see how it's platform-specific assumption. Imo lumping the read/exception cases together has always been wrong, and should indeed be fixed in both places.

> And now this platform-specific assumption needs to spread to other higher-level parts of our stack, such as the HTTP server, which your patch lacks. I don't see how it's platform-specific assumption. Imo lumping the read/exception cases together has always been wrong, and should indeed be fixed in both places.
darosior commented 2026-10-30 17:44:46 +00:00 (Migrated from github.com)

I'll reply because i want to understand if i'm missing something, but if you all prefer the Cory-Sjors suggestion, i am fine with that too. Fixing and backporting that is more important than the details here, i think. And anyways, down the line, i think we should get rid of the select() exception set in favour of using poll() everywhere.

Could you PR your suggestion?

Adding it as an error for poll makes it explicit that we consider this (previously unspecified) case should be treated as an exception, as select does.

Watching for exceptions is only necessary as a select()-specific workaround for Windows. Starting to watch for out-of-band data in poll() is in my view a step backwards in the direction of not needing it anywhere. Especially as the alternative is to register it only where needed, fixing the BSD/macOS issue created by the Windows-specific workaround.

Relying on our checksum to notice the extra data seems like a much more substantial layer violation :)

Heh, fair enough :), but the disconnection is merely an additional nice-to-have, not the core mechanism of the fix here.

Imo lumping the read/exception cases together has always been wrong, and should indeed be fixed in both places.

I agree, consumers shouldn't keep reading on ErrorEvent regardless of the decision here. I fixed one such case in #36676, but there are more (for instance I2P and SOCKS5 ignore what Wait() reports).

I'll reply because i want to understand if i'm missing something, but if you all prefer the Cory-Sjors suggestion, i am fine with that too. Fixing and backporting that is more important than the details here, i think. And anyways, down the line, i think we should get rid of the select() exception set in favour of using poll() everywhere. Could you PR your suggestion? > Adding it as an error for `poll` makes it explicit that we consider this (previously unspecified) case should be treated as an exception, as `select` does. Watching for exceptions is only necessary as a `select()`-specific workaround for Windows. Starting to watch for out-of-band data in `poll()` is in my view a step backwards in the direction of not needing it anywhere. Especially as the alternative is to register it only where needed, fixing the BSD/macOS issue created by the Windows-specific workaround. > Relying on our checksum to notice the extra data seems like a much more substantial layer violation :) Heh, fair enough :), but the disconnection is merely an additional nice-to-have, not the core mechanism of the fix here. > Imo lumping the read/exception cases together has always been wrong, and should indeed be fixed in both places. I agree, consumers shouldn't keep reading on `ErrorEvent` regardless of the decision here. I fixed one such case in #36676, but there are more (for instance I2P and SOCKS5 ignore what Wait() reports).
darosior commented 2026-10-30 17:57:23 +00:00 (Migrated from github.com)

I think Cory is right here. Callers continuing to read on error/exception is the root cause here, and it's cleaner to fix that than to try and let them doing this by carefully patching what we watch for at the socket level.

I still think we shouldn't be watching for urgent data anywhere, but i agree that for as long as we are forced to do it on one platform (Windows), we should be consistent across platforms and do it everywhere.

Therefore i don't suggest pulling the change in this branch anymore at this time. Thanks @theuni.

I think Cory is right here. Callers continuing to read on error/exception is the root cause here, and it's cleaner to fix that than to try and let them doing this by carefully patching what we watch for at the socket level. I still think we shouldn't be watching for urgent data anywhere, but i agree that for as long as we are forced to do it on one platform (Windows), we should be consistent across platforms and do it everywhere. Therefore i don't suggest pulling the change in this branch anymore at this time. Thanks @theuni.
Sjors commented 2026-10-31 03:31:50 +00:00 (Migrated from github.com)

It makes sense for us to follow the RFC 6093 recommendation of setting SO_OOBINLINE, just to be consistent with other software, in order to make our code is easier to understand for experts in this area.

Update: the recommendation is conditional on using the urgent mechanism in the first place. So we shouldn't set it, unless there's a good reason.

Even though applications SHOULD NOT employ the urgent mechanism

We have an additional reason to never deploy the urgent mechanism: having cleartext interrupts at the tcp level goes against the design of v2 encrypted traffic.

It would help to have a code comment that references both the RFC and the v2 rationale.

That should address my concern about intuitiveness (if we did support interrupts, rather than just disconnect on them, we would not want SO_OOBINLINE so v2 decryption can keep going).

i think that works but is fragile. The exception set is not specific to urgent data, it is a general set of descriptors being watched for "exceptional conditions".

I also agree with this. In general it's probably fine to disconnect when something exceptional happens, it does introduce a risk of spurious disconnects.

The robust strategy is to ignore urgent data, except when we're specifically interested in it (like the Windows case), in a way that can't hog resources.

I think a better approach is what I mentioned in my earlier message: at the socket level, only ever register the exception set for Windows.

Agreed. Though I still need to study @theuni's suggestion.

Ignoring OOB data, rather than disconnecting on it, has the additional benefit that fake data can be used for traffic shaping. We don't get that with SO_OOBINLINE, but that's not a strong argument against it.

It makes sense for us to follow the RFC 6093 recommendation of setting `SO_OOBINLINE`, just to be consistent with other software, in order to make our code is easier to understand for experts in this area. _Update_: the recommendation is conditional on using the urgent mechanism in the first place. So we _shouldn't_ set it, unless there's a good reason. >> Even though applications SHOULD NOT employ the urgent mechanism We have an additional reason to never deploy the urgent mechanism: having cleartext interrupts at the tcp level goes against the design of v2 encrypted traffic. It would help to have a code comment that references both the RFC and the v2 rationale. That should address my concern about intuitiveness (if we did support interrupts, rather than just disconnect on them, we would not want `SO_OOBINLINE` so v2 decryption can keep going). > i think that works but is fragile. The exception set is not specific to urgent data, it is a general set of descriptors being watched for "exceptional conditions". I also agree with this. In general it's probably fine to disconnect when something exceptional happens, it does introduce a risk of spurious disconnects. The robust strategy is to ignore urgent data, except when we're specifically interested in it (like the Windows case), in a way that can't hog resources. > I think a better approach is what I mentioned in my earlier message: at the socket level, only ever register the exception set for Windows. Agreed. Though I still need to study @theuni's suggestion. Ignoring OOB data, rather than disconnecting on it, has the additional benefit that fake data can be used for traffic shaping. We don't get that with `SO_OOBINLINE`, but that's not a strong argument against it.
Sjors commented 2026-10-31 04:14:55 +00:00 (Migrated from github.com)
- https://github.com/bitcoin/bitcoin/pull/36716
okonendurance20-sudo (Migrated from github.com) reviewed 2026-11-05 04:49:35 +00:00
okonendurance20-sudo (Migrated from github.com) left a comment

The MIT License (MIT)

Copyright (c) 2009-2026 The Bitcoin Core developers
Copyright (c) 2009-2026 Bitcoin Developers

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

The MIT License (MIT) Copyright (c) 2009-2026 The Bitcoin Core developers Copyright (c) 2009-2026 Bitcoin Developers Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
midnightmagic commented 2026-11-06 00:59:42 +00:00 (Migrated from github.com)

@theuni i did not. I am not suggesting to set SO_OOBINLINE because our application needs urgent data, on the contrary. SO_OOBINLINE is simply the standard way to opt out of the broken default urgent-data handling, as RFC 6093 puts it.

fwiw, I described a mechanism by which it wouldn't actually do that, over here:

Comment in PR 36716

I agree we could instead disconnect on an urgent data notification 1, but because Windows also needs the exception set, what you suggest leads to a layer violation by which the connection handler needs to know that an exception raised by the socket handler is necessary related to TCP urgent data. Instead of watching for urgent data on more platforms, why not go the other way instead, and only register the exception set on Windows where it's actually necessary?

I strongly recommend treating oob data as a banning disconnect for the above reason. The implications based on historical data handling are default unfriendly.

Footnotes

  1. This PR does disconnect on an urgent byte read in non-adversarial cases too, because it would break parsing unless it is strategically positioned. ↩

Yes please! :-)

> @theuni i did not. I am not suggesting to set `SO_OOBINLINE` because our application needs urgent data, on the contrary. `SO_OOBINLINE` is simply the standard way to opt out of the broken default urgent-data handling, as RFC 6093 [puts it](https://datatracker.ietf.org/doc/html/rfc6093#section-3.1). fwiw, I described a mechanism by which it wouldn't actually do that, over here: [Comment in PR 36716](https://github.com/bitcoin/bitcoin/pull/36716#issuecomment-6673573479) > I agree we could instead disconnect on an urgent data notification [1](#user-content-fn-1-609e4dfd0021aa343e3eaaf3338f6fee), but because Windows also needs the exception set, what you suggest leads to a layer violation by which the connection handler needs to know that an exception raised by the socket handler is necessary related to TCP urgent data. Instead of watching for urgent data on more platforms, why not go the other way instead, and only register the exception set on Windows where it's actually necessary? I strongly recommend treating oob data as a banning disconnect for the above reason. The implications based on historical data handling are default unfriendly. > ## Footnotes > 1. This PR does disconnect on an urgent byte read in non-adversarial cases too, because it would break parsing unless it is strategically positioned. [↩](#user-content-fnref-1-609e4dfd0021aa343e3eaaf3338f6fee) Yes please! :-)
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36688
No description provided.