mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 10:29:40 +00:00
net: receive TCP urgent data inline #36688
Labels
No labels
Android
Backport
Block storage
Brainstorming
Bug
Build system
CI failed
Consensus
Data corruption
Descriptors
Docs
DrahtBot Guix build requested
Feature
Fuzzing
GUI
Good First Review
IPC
Insufficient Review
Kernel
Linux/Unix
Mempool
Mining
Needs Backport (31.x)
Needs Backport (32.x)
Needs Benchmark
Needs Conceptual Review
Needs backport (30.x)
Needs rebase
Needs release note
Net
P2P
PSBT
Privacy
Private Broadcast
Questions and Help
RPC/REST/ZMQ
Refactoring
Resource usage
Review club
Scripts and tools
Settings
TX fees and policy
Tests
Tracking Issue
UTXO Db and Indexes
Up for grabs
Upstream
Utils/log/libs
Validation
Waiting for author
Wallet
Windows
good first issue
interfaces
macOS
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin/bitcoin!36688
Loading…
Reference in a new issue
No description provided.
Delete branch "2610_oobinline"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
There is currently nothing preventing us to receive TCP urgent data, which would never be returned by
recv(). Linux'spoll()is indifferent to this, but theselect()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_OOBINLINEon our sockets, sorecv()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_OOBINLINEwas conservatively made a warning and not a failure, because its absence does not prevent node operations, it's just annoying.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.
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:
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.
concept ACK "urgent" data is dumb
https://datatracker.ietf.org/doc/html/rfc6093#section-6
🔵 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 reportsACK
4655c2305eRan the test and checked it fails with the
SO_OOBINLINEchange revertedACK
4655c2305eReviewed 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
SetSockOptusages.Looked into why
TCP_NODELAYoption is set again after accept, whileSO_OOBINLINEis not. Apparently even settingTCP_NODELAYagain is extra cautious, and not needed on any of the supported platforms. Inheritance ofSO_OOBINLINEshould 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 unsettingSO_OOBINLINEright after theAcceptin 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
4655c230Receiving urgent data inline makes total sense. Nice test and code change.
ACK
4655c2305eDo you plan to completely remove the
select()branch and usepoll()on all platforms?Show Signature
vasild's public key is on openpgp.org
I think it would be useful to mention which platforms report it as pending.
nit:
GetSockName()fillsaddrwith some data and sets its size in the out parameteraddr_len. I think logically,Connect()should takeaddr_len, notsizeof(addr). I guess this is mostly theoretical concern because my understanding is that both will be equal. Maybe addBOOST_REQUIRE_EQUAL(addr_len, sizeof(addr));, ors/sizeof(addr)/addr_len/or just ignore.Yes, using
addr_lenis cleaner.Done.
Updated comment to apply to all platforms using
select()to monitor.While all the reviews are stale, quick question: should the test live in
sock_tests.cpp? There is astruct TcpSocketPairthat might be handyThought about this, but
CreateSockOSis 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.ACK
de87cb8407Show Signature
vasild's public key is on openpgp.org
reACK
de87cb8407ACK
de87cb8407built 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
pinheadmz's public key is on openpgp.org
nit if u touch,
onandsetabove could be cleaned up like we did in httpserver.cppstatic constexpr int SOCKET_OPTION_TRUE{1};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).
ACK
de87cb8407re-ACK
de87cb8407reACK
de87cb8407Let's hold off on merging this, i think i found something wrong here.
The patch here is correct, but insufficient to address the issue entirely on macOS/BSD.
This comment from @pinheadmz got me wondering:
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:
poll()withoutPOLLPRI)SO_OOBINLINE)SO_OOBINLINE)Thankfully the exception set is only necessary on Windows, to detect a failed connection attempt. So we could
ifdefit 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 usingpoll()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.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. ↩︎
Looks like Ralph had caught the issue, too. cc @willcl-ark
ACK
de87cb8407Would it make sense to add this?
In other words: hang up if we receive an urgent byte, on platforms with
select().We could avoid
SO_OOBINLINEaltogether if we treat the presence of URG (after a connection is established) as misbehavior. We could do that onpoll()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 sendingURG, besides what we already handle inConnectToSocket. That might cause an "underserved" disconnect. But withSO_OOBINLINEsuch 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_OOBINLINEto be a confusing feature. So if we don't need it, that would be nice.The patch is fine and I retain my ACK as it is, but this angle is worth considering:
So, we do not use out-of-bound data and we do not care about it, but we would set
SO_OOBINLINEeven 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_POLLsnippet would naturally go away if we usepoll(2)on all platforms.@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.
I would say the opposite, actually. It's the default of pulling the urgent byte out of the stream which is confusing, and
SO_OOBINLINErestores the socket API to behave as you would expect. As mentioned above, even RFC 6093 calls the default "broken" and states thatSO_OOBINLINErestores 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.
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.
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):
Heh, I missed @Sjors's comment. I agree with that. My suggestion is the same as his, just with
poll's behavior unified.You missed this part:
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.
@theuni i did not. I am not suggesting to set
SO_OOBINLINEbecause our application needs urgent data, on the contrary.SO_OOBINLINEis 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.)
This PR does disconnect on an urgent byte read in non-adversarial cases too, because it would break parsing unless it is strategically positioned. ↩︎
I'm not following. Or at least,
select()already violates this implicitly. Adding it as an error forpollmakes it explicit that we consider this (previously unspecified) case should be treated as an exception, asselectdoes. 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?
Relying on our checksum to notice the extra data seems like a much more substantial layer violation :)
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.
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?
Watching for exceptions is only necessary as a
select()-specific workaround for Windows. Starting to watch for out-of-band data inpoll()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.Heh, fair enough :), but the disconnection is merely an additional nice-to-have, not the core mechanism of the fix here.
I agree, consumers shouldn't keep reading on
ErrorEventregardless 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 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.
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.
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_OOBINLINEso v2 decryption can keep going).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.
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.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.
fwiw, I described a mechanism by which it wouldn't actually do that, over here:
Comment in PR 36716
I strongly recommend treating oob data as a banning disconnect for the above reason. The implications based on historical data handling are default unfriendly.
Yes please! :-)