mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 11:53:39 +00:00
http: disconnect clients that never finish a request #36204
No reviewers
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!36204
Loading…
Reference in a new issue
No description provided.
Delete branch "http-request-deadline"
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?
The -rpcservertimeout timer only measures inactivity. Every read from a client resets it, so a client that keeps sending bytes without ever completing a request can hold the connection for ever.
Connection slots are capped by default 16 or by -rpcmaxconnections. Once those slots are taken, the server stops accepting new connections. Because no request ever completes the authentication functions are never hit.
The impact is low because RPC port binds to localhost by default, so this is a robustness fix rather than a remotely exploitable security issue.
How to reproduce
To reproduce: start a node with -rpcmaxconnections=2, open two sockets, and write "GET / HTTP/1.1\r\n" one byte at a time with ten seconds between bytes. bitcoin-cli getblockcount then hangs until one of the (trickling) sockets is closed.
The fix adds a second deadline next to the idle timer. It is armed on the first byte of a request and expires after -rpcservertimeout (the same value as the idle timer). This timer also does not resets or is extended by later reads. Completing request parsing clears the deadline, so a long-running RPC keeps its connection as before. The deadline is deferred while a worker holds the request, for the same reason the idle timer is. It is restarted while response bytes
are written, because the client cannot start a new request during that window. -rpcservertimeout=0 disables both timers. No new option is added.
Disclaimer this bug is found by utilizing ASTRA. Verified using KIMI 3 and Claude.
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
537944cb78remark: Was wondering whether we were missing another call to
RestartRequestDeadline()whenHTTPRemoteClient::Send()callsMaybeSendBytesFromBuffer()and always setsm_req_busy = false. If it succeeds in emptying the send bufferReadyToSend()will returnfalseand we will not setSock::SendEvent, so the request deadline will not be reset. Butm_request_sincewill benulloptwhen the request completes parsing and is handed to the worker, so no need to bump it along in that case.nanonit: Feels like
!m_req_busywould be the cheapest check and be the first one (same foris_idle).Slightly more accurate?
An alternative would be to always add
rpcservertimeoutto it when updating the value and call itm_request_deadlineorm_req_read_deadline, which is easier to wrap one's head around?Not sure time drift is an issue with these tests, but #36159's
9afe15897eswitches to the mockable steady clock and freezes time in784ac4fb9busing aFakeSteadyClock. Worth adopting here?nitlinator:
(Actually the first time in this file where the casing was messed up).
Seems to work fine without send the first byte here and instead sending it in the thread?
If the test fails for some reason we don't need to clean up the
tricklersright? See659671ac3dTaken thanks
Rebased to latests master
Made change for the changes made by pr #36174
Incoporated suggestions made by @hodlinator
Reworked functional tests
Yes and I reworked the test, thanks
correct, thanks, see above
If you do not count Line 326 it's the first messed up casing indeed, stupid mistake, rectified!
Not sure what to do with this, yes you are correct. But to do this correctly that PR has to land first right?
Have to pushback on this one, m_req_busy is mostly false so the negate makes that true. It would cause an extra check that is mostly true and therefor does not short the evaluation. I think the
rpcservertimeout.count()is the cheapest one, given it's already in a register.After some checking, changed the comment. Do not think extra action is needed in send(), the extra call to
RestartRequestDeadline()would not do a thing because of the empty m_reqeust_since. Imho, happy to be wrong.🐙 This pull request conflicts with the target branch and needs rebase.
Rebased and adjusted the function naming.
Would expect
m_req_busyto be true most of the time, a worker thread is busy processing the request. (The exception being very bad connections with larger requests which take time to receive). But maybe there's something with keep-alive connections that make them tend to linger after the response was sent.Agree on this.
Or you could incorporate parts of it into this one? I'm happy however the changes get in.
Reviewed
99e880356bNice how you cleaned up the test, makes it easy to grasp.
nit: Maybe could switch commits from {fix+test} to {characterization-test+fix}? That way the ancestor commits CI job can also verify the pre-fix behavior (edit: and humans can clearly see the behavior-change). See https://github.com/bitcoin/bitcoin/compare/master...hodlinator:bitcoin:pr/36204_suggestions
Why would we allow less than
RPCSERVERTIMEOUT(which is only2)?Earlier concerns neither republished nor resolved: 1 suggestion. See the Run archive linked from the Review report for their assessments.
🟠 Minor ·
-rpcservertimeoutalso cuts off progressing body uploadsThe new completion budget can disconnect legitimate body uploads despite continued receive progress; no affected deployment has been identified.
Location:
src/httpserver.cpp:1241(head).🟠 Minor · The final clock advance can race with send bookkeeping
The unit test can exhaust its disconnect retries if the I/O thread records both timer timestamps after the final mock-clock advance; this failure has not been observed.
Location:
src/test/httpserver_tests.cpp:1075(head).🟠 Minor · Debug-log capture starts after the deadline can fire
A roughly two-second main-thread stall can make the functional test miss the expected deadline message and fail despite correct server behavior; this failure has not been observed.
Location:
test/functional/interface_http.py:493(head).🔵 Suggestion · The I/O-loop test's final disconnect does not isolate the deadline
The final
timeout + 1sadvance expires the idle timer too, so the I/O-loop test can pass without a working completion deadline for the second pipelined request.Location:
src/test/httpserver_tests.cpp:1075(head).Reviewed:
1dfe11b66622· Review report · Previous reportspermalink
🐙 This pull request conflicts with the target branch and needs rebase.
concept ACK, this mitigates slow-loris and perhaps most importantly, mitigates the LLM "findings" reports we keep getting 👍
Given your PR was merged, I now changed to code to use mockable steady clock
Reviewed
bfbe9a6474nit: Clearer through one of these?
nit: Clarify that
NotIdleAt()is critical?nit: Show that timeout argument is indeed the deciding factor?
nit: Might as well skip
string_view?Why are non of the other unit tests doing this cleanup? I tried variants of the following diff with the
BOOST_REQUIRE(false)in different places but the unit tests seem to terminate okay without deadlocking:Good catch, not sure why i though this was needed. Taken
Because of the comment I do not think 2 separate assignments are not needed for clarity, have removed those to just do one without concatenation.
Taken and have added extra reset so that the test become stronger.
Taken
took option A
taken (not all), have aligned it with the other tests.
Incorporated the suggested changes by the review from hodlinator.
Changed a line in httpserver.cpp (L 1242)
!m_req_busy=>Assume(!m_req_busy)because that state (armed and busy) cannot happen on production, reduces test code.Agree, the paths /first & /second are enough. Could return to
constexpr std::string_view:taken
ACK
2f699f91f1Built and tested on macos/arm64. Played with the new timeout using
socatto manually make very slow (and then fast) HTTP requests. Mutated the patch a few places to observe test failure, and watched the packets in wireshark throughout to see the trickle.Reviewed the code and left a few aesthetic nits, no blockers. I found the unit test with the token counter a bit hard to follow but after trying to simplify it myself a few different ways I ended up understanding it and agreeing with the approach.
kimi-k3 gave up trying to write a slow loris attack against this patch ;-)
Show Signature
pinheadmz's public key is on openpgp.org
b5104d4d41optional style nit, could use a one-liner and keep the function name
b5104d4d41Could use a
DebugLogHelperto assert the reason for disconnectingb5104d4d41nit, I'd say the deadline is actually
now + timeout, the reset call moves the interval start time to now.taken, makes it a bit clearer.
you are correct sir, taken
taken
Pushed update to fix the lint issue & suggestions of @pinheadmz taken.
Was about to write something similar,
usingseems good!However, the first commit still introduces
NotIdleAtwhile the second one changes it toSetIdleSince. Seems like all changes to src/test/httpserver_tests.cpp in the second commit could be cherrypicked into the first commit.If you touch again:
ACK
b6dfbd6f71Nice to avert slow-loris attacks even though RPC is a slightly trusted interface.
I think one could tighten up the functional test a little bit more, but not blocking.
Could add comment to explain why we don't store
send_tokensdirectly in the socket type.remark: Would be nice if the ctor parameter where called
request_dispatcherso we could goBut unfortunately it's
func.Might as well declare
stepassize_t:It's slightly unclear whether the trickling completes or not. One could assert the return value from the trickling function:
Taken and adapted
taken
taken
If correct and CI builds now, the form is now changed to {characterization-test , fix + flipped assertions + new deadline tests)
Reviewers, sorry. I have changed the form now to {characterization-test , fix + flipped assertions + new deadline tests). (where new is not new from a reviewer viewpoint but from master / code viewpoint)
So mainly the order is changed not the tests & code.
Pushed new updates that addresses suggestions by @hodlinator and drahtbot
approach ACK
on NetBSD 11.0,
check_trickled_request_timeout()fails.waiting_conn.recv_raw()raisesTimeoutErrorbecause the client-side timeout (RPCSERVERTIMEOUT + 2) expires before a response shows upon NetBSD, increasing the client-side timeout to any value >=
RPCSERVERTIMEOUT * 3makes the test pass.RPCSERVERTIMEOUT * 2is still not enough. the test also passes if i remove the client-side timeout altogether. Linux passes without any of these changes, using the originalRPCSERVERTIMEOUT + 2what i cannot tell yet is why it breaks. in the failing run's
debug.log, i never seeHTTP client took too long to send a complete requestsome logs:
test output log
debug.logexcerptdebug.loghere -- the tail has the lines of interest