Skip to content

net: reject oversized outbound messages - #35888

Open
l0rinc wants to merge 5 commits into
bitcoin:masterfrom
l0rinc:l0rinc/net-reject-oversized-message-types
Open

l0rinc wants to merge 5 commits into
bitcoin:masterfrom
l0rinc:l0rinc/net-reject-oversized-message-types

Conversation

@l0rinc

@l0rinc l0rinc commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Problem: Outbound transports encode message types in a fixed 12-byte field, but CConnman did not ensure internal callers respected this limit before queuing messages.
An oversized type aborts V1 encoding. V2 sends it under a truncated type with its payload intact, or writes past its encoding buffer when the payload is shorter than the excess type bytes.
The send path also did not enforce the 4 MB payload limit.
Private broadcast could reach this path with an oversized witness variant of a transaction already in the mempool.

Fix: Treat oversized types and payloads as failed assumptions in CConnman::PushMessage() before they enter the send queue.
Use the validated mempool transaction when privately rebroadcasting a transaction already in the mempool.
The testing-only sendmsgtopeer RPC rejects oversized payloads before calling PushMessage().

@DrahtBot DrahtBot added the P2P label Aug 4, 2026
@DrahtBot

DrahtBot commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

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

External sites

Reviews

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

Type Reviewers
Concept ACK ajtowns, naiyoma

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:

  • #36607 (private broadcast: Extra guard against unintended non-private broadcast of private transactions by optout21)
  • #36429 ([RFC] Scheduled transaction broadcast, minimal by optout21)
  • #33954 (test: add functional test for outbound connection management by mzumsande)

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.

@l0rinc
l0rinc marked this pull request as draft August 5, 2026 04:41
@l0rinc
l0rinc force-pushed the l0rinc/net-reject-oversized-message-types branch from d849c8e to 18ce84e Compare August 5, 2026 19:50
@l0rinc
l0rinc marked this pull request as ready for review August 5, 2026 19:56
Comment thread src/test/net_tests.cpp Resolved
Comment thread src/net.cpp Resolved
@ajtowns

ajtowns commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Concept ACK

Can you explain your thinking / where you were coming from in relation to #35880 (review) some more? Even if whatever the concerns were are resolved to your satisfaction already, it makes me think there's a hole in my understanding here that I'd like to fill before reviewing properly.

@l0rinc

l0rinc commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor Author

it makes me think there's a hole in my understanding here

I don't think you're missing anything on the receive side. I had already found that the related send path was missing checks, so when this fuzz failure showed another oversized-message case, I wanted to verify both paths before excluding it. At that point I hadn't established that the fuzzer was bypassing a receive-side guarantee, so I treated the outbound failure as something the send path should reject.

There was also a more directly related reason I did not want to assume every receive-side limit was enforced early: #35936 found that getblocks and getheaders enforced MAX_LOCATOR_SZ only after deserialization, so a truncated oversized locator could allocate and throw before reaching the disconnect check. (edited)

I was also cautious because similar fuzz constraints and sanitizer workarounds had hidden actual problems before:

After tracing this case, I agree that an oversized transaction cannot reach this path from a real peer, so restricting the fuzz target is correct. Thanks for the pushback and feedback.

Concept ACK

Addressed your suggestions, thanks, pushed.
Edit: it seems libc++ has some problems with constexpr strings, so I changed it to std::string_view and materialized on call site.

@l0rinc
l0rinc force-pushed the l0rinc/net-reject-oversized-message-types branch from bfa10f0 to 321b590 Compare August 6, 2026 20:33
@DrahtBot

DrahtBot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task No wallet: https://github.com/bitcoin/bitcoin/actions/runs/31127401132/job/92703875334
LLM reason (✨ experimental): CI failed due to a Clang build error in src/test/net_tests.cpp: a constexpr std::string is not a constant expression, causing static_assert(MAX_MESSAGE_TYPE.size()...) to fail.

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

@l0rinc l0rinc closed this Aug 6, 2026
@l0rinc l0rinc reopened this Aug 6, 2026
@DrahtBot DrahtBot closed this Aug 7, 2026
@DrahtBot DrahtBot reopened this Aug 7, 2026
@l0rinc
l0rinc force-pushed the l0rinc/net-reject-oversized-message-types branch from 321b590 to 84eddd6 Compare August 7, 2026 20:30
@DrahtBot DrahtBot removed the CI failed label Aug 7, 2026
Comment thread src/rpc/net.cpp
throw JSONRPCError(RPC_INVALID_PARAMETER, "Error parsing input for msg");
}
if (msg->size() > MAX_PROTOCOL_MESSAGE_LENGTH) {
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Error: msg too large, max size is %u bytes", MAX_PROTOCOL_MESSAGE_LENGTH));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think it's a "problem" per se that sendmsgtopeer accepts payloads above the 4 MB protocol limit - it's a test RPC after all. The story should be that you want to add the assumption to prevent future regressions etc., and need to change the rpc for that - which is no loss because it's just a test rpc and oversized messages are being tested elsewhere.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Pushed, let me know if I understood the request.

Comment thread src/net.cpp Outdated
{
AssertLockNotHeld(m_send_mutex);
if (!Assume(msg.m_type.size() <= CMessageHeader::MESSAGE_TYPE_SIZE)) return false;
if (!Assume(msg.IsWithinLimits())) return false;

@mzumsande mzumsande Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What is the reason for the non-debug build return instead of only adding the Assume?
I think if this was somehow reached in production, returning false would result in a stall of the connection for minutes (until it get disconnected for inactivity), since SocketSendData will never make progress.

Also, wouldn't it be nicer from an architectural point of view if arbitrary application-level rules such as the 4MB maximum message length wouldn't be enforced at the transport level, but only at the higher level (PushMessage) anyway?
Then we wouldn't need the same assumption 3 different places.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

What is the reason for the non-debug build return instead of only adding the Assume?

It was suggested in #35880 (comment).
I initially applied it because SetMessageToSend() already returns a boolean, and I treated false as a safe fallback in release builds while Assume exposed invalid callers in debug and fuzz builds.
Good catch that false will just try again later, so a permanently invalid message would remain queued and stall the connection.
I moved both size assumptions to PushMessage(), before the message enters the send queue.
This also keeps the 4 MB application-level rule out of the transports - added you as coauthor, thanks for the review.

@l0rinc
l0rinc force-pushed the l0rinc/net-reject-oversized-message-types branch from 84eddd6 to 5270b3e Compare August 14, 2026 20:00

@l0rinc l0rinc left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Rebased and addressed @mzumsande's review: outbound limits are now enforced only in CConnman::PushMessage(), before messages enter the send queue, so a permanent rejection cannot stall SocketSendData() through SetMessageToSend(false). The sendmsgtopeer behavior and the PR rationale now reflect the RPC's testing-only role.

Comment thread src/net.cpp Outdated
{
AssertLockNotHeld(m_send_mutex);
if (!Assume(msg.m_type.size() <= CMessageHeader::MESSAGE_TYPE_SIZE)) return false;
if (!Assume(msg.IsWithinLimits())) return false;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

What is the reason for the non-debug build return instead of only adding the Assume?

It was suggested in #35880 (comment).
I initially applied it because SetMessageToSend() already returns a boolean, and I treated false as a safe fallback in release builds while Assume exposed invalid callers in debug and fuzz builds.
Good catch that false will just try again later, so a permanently invalid message would remain queued and stall the connection.
I moved both size assumptions to PushMessage(), before the message enters the send queue.
This also keeps the 4 MB application-level rule out of the transports - added you as coauthor, thanks for the review.

Comment thread src/rpc/net.cpp
throw JSONRPCError(RPC_INVALID_PARAMETER, "Error parsing input for msg");
}
if (msg->size() > MAX_PROTOCOL_MESSAGE_LENGTH) {
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Error: msg too large, max size is %u bytes", MAX_PROTOCOL_MESSAGE_LENGTH));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Pushed, let me know if I understood the request.

@l0rinc
l0rinc force-pushed the l0rinc/net-reject-oversized-message-types branch from 5270b3e to f918811 Compare September 6, 2026 21:00
@l0rinc

l0rinc commented Sep 6, 2026

Copy link
Copy Markdown
Contributor Author

AI review flagged that I was missing coverage so added PushMessage() tests because the transport tests bypassed the guard and didn’t verify rejection before queueing. Also rebased and split the type and payload characterization/fix pairs so each fix updates its own expectations (otherwise it's not obvious why multiple fixes are needed).

@naiyoma naiyoma left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Concept ACK

nit: Maybe it also makes sense to restrict the message type copy to MESSAGE_TYPE_SIZE?

-        std::copy(msg.m_type.begin(), msg.m_type.end(), contents.data() + 1);
+        std::copy_n(msg.m_type.begin(),
+                std::min(msg.m_type.size(), CMessageHeader::MESSAGE_TYPE_SIZE),
+                contents.data() + 1);

@l0rinc

l0rinc commented Sep 29, 2026

Copy link
Copy Markdown
Contributor Author

Rebased and simplified the tests and fuzz setup, production fix is unchanged, with the message type added to the log.

nit: Maybe it also makes sense to restrict the message type copy

PushMessage already rejects oversized types before they reach v2, and clamping there would silently truncate an invalid type, so it's not required for this fix

@mzumsande mzumsande left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like the Ralph finding ("With -privatebroadcast, an oversized witness variant of a mempool transaction can reach the new assumption.") is real., so the added Assume could be hit by a user.

This is probably more of a minor private broadcast bug and not a real problem because the user themselves would need to create a version of a mempool tx with its witness padded past 4 MB, and rpc is a trusted interface.

Not sure how to best fix this, if at all. Maybe privately broadcast the mempool's version of the transaction in this case, not the user-supplied one?

When a transaction is already in the mempool, BroadcastTransaction skips validation and uses its wtxid for normal relay.
Private broadcast still queues the caller's transaction, so a different witness can bypass validation and exceed the outbound payload limit.

Use the mempool transaction for rebroadcasting and extend the existing functional test to submit an oversized witness variant.
Check that the broadcast witness hash and retained transaction bytes match the mempool version.

Co-authored-by: Martin Zumsande <[email protected]>
@l0rinc
l0rinc force-pushed the l0rinc/net-reject-oversized-message-types branch from f1d99d9 to 8bc3502 Compare November 13, 2026 21:21
@l0rinc

l0rinc commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

Thanks mr Ralph (cc: @willcl-ark) and @mzumsande: private rebroadcast now uses the validated mempool transaction when available, with a regression test covering oversized witness variants (cc: @vasild, @andrewtoth).
I've also re-reviewed and simplified the code and tests with latest LLMs before push - removing redundant hash state, reusing the transport fixture, and sharing the fuzz setup.

l0rinc and others added 4 commits November 13, 2026 22:30
Round-trip a 12-byte type and a 4 MB payload through the transports.
Record that `PushMessage()` queues oversized types and payloads, and that `sendmsgtopeer` sends an oversized payload which disconnects its peer.
Keep a valid message pending to observe queueing without encoding the oversized type.
Outbound transports encode message types in a fixed 12-byte wire field.
V1 aborts on a longer type, while V2 sends a truncated type with the payload intact or writes past its encoding buffer when the payload is shorter than the excess type bytes.
Normal outbound types are internal constants, and `sendmsgtopeer` already rejects types longer than 12 bytes.

Treat oversized types from internal callers as failed assumptions in `CConnman::PushMessage()` before they enter the send queue.
Debug and fuzz builds expose invalid internal callers, while release builds drop the messages.

Co-authored-by: Anthony Towns <[email protected]>
Co-authored-by: Martin Zumsande <[email protected]>
Constrain connman payloads and both private-broadcast transaction sources to the send path limit.
The seed limits also bound echoed TX messages, making the receive-side size filter redundant.
Treat an oversized payload from an internal caller as a failed assumption in `CConnman::PushMessage()` before it enters the send queue.
Debug and fuzz builds expose invalid internal callers, while release builds log and drop the message.

This requires the testing-only `sendmsgtopeer` RPC to reject oversized payloads before calling `PushMessage()`.
Oversized inbound messages remain testable through raw P2P inputs, so this does not remove coverage of their receive-side handling.

Nonstandard test-chain configurations can still admit transactions above this limit.
Relaying them fails the assumption in debug builds.

Co-authored-by: Anthony Towns <[email protected]>
Co-authored-by: Martin Zumsande <[email protected]>
@l0rinc
l0rinc force-pushed the l0rinc/net-reject-oversized-message-types branch from 8bc3502 to da042ca Compare November 13, 2026 21:33
@DrahtBot

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task ASan + LSan + UBSan + integer: https://github.com/bitcoin/bitcoin/actions/runs/43509977467/job/132177388263
LLM reason (✨ experimental): The build fails because net_tests.cpp declares a constexpr std::string that Clang cannot evaluate as a constant expression.

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants