Skip to content

mux: Fix OOM crash on UDP keep frames - #5902

Closed
herenickname wants to merge 1 commit into
XTLS:mainfrom
herenickname:fix-mux-udp-oom
Closed

herenickname wants to merge 1 commit into
XTLS:mainfrom
herenickname:fix-mux-udp-oom

Conversation

@herenickname

Copy link
Copy Markdown

Fixes #5901.

FrameMetadata.WriteTo on the UDP keep-frame path dereferences b.UDP.Address (a non-atomic two-word interface) while the upstream UDP reader is free to mutate the same *net.Destination for its next packet. The resulting torn read can produce a DomainAddress whose backing string header has a garbage (ptr, len) pair.

isDomainTooLong then fails to catch it because it uses a signed len(domain) > 256 comparison: a negative torn length is < 256, slips through, and reaches runtime.stringtoslicebyte with a uint length of ~9 EB, aborting the process with fatal error: out of memory.

The error from WriteAddressPort on this branch was also discarded, so even the existing guard would have produced a malformed frame instead of a clean failure.

This PR fixes both halves:

  • common/protocol/headers.go — switch to uint(len(domain)) > 255 so any negative or absurd length is rejected, and tighten the bound to the actual one-byte wire-format limit.
  • common/mux/frame.go — snapshot *b.UDP into a local before writing the address bytes (closes the window between the network-type byte and the address bytes), and actually return WriteAddressPort's error instead of dropping it.

Full root-cause writeup, production stack trace, and a deterministic standalone reproducer are in #5901. With this PR applied the reproducer cleanly returns Super long domain is not supported: ... instead of crashing.

Note on the underlying race

This patch stops the crash and stops the malformed frame, but it does not fix the root of the data race — a producer that keeps mutating a *net.Destination after handing the pointer to a buffer is fundamentally unsafe, and the proper long-term fix is to make UDP producers either allocate a fresh Destination per packet or store it by value on buf.Buffer. I haven't tracked down the specific producer that triggers it; happy to follow up in a separate PR if you'd like.

FrameMetadata.WriteTo dereferences b.UDP.Address (a non-atomic two-word
interface) on the UDP keep-frame path while the upstream UDP reader is
free to mutate the same *net.Destination for its next packet. The
resulting torn read produces a DomainAddress whose backing string header
has a garbage (ptr, len) pair.

isDomainTooLong used a signed `len(domain) > 256` comparison, so a
negative torn length slipped through and reached
runtime.stringtoslicebyte with a uint length of ~9 EB, aborting the
process with "fatal error: out of memory".

The error from WriteAddressPort on this branch was also discarded, so
even the existing guard would have produced a malformed frame instead
of a clean failure.

Fix both:

  * common/protocol/headers.go: switch to `uint(len(domain)) > 255` so
    any negative or absurd length is rejected, and tighten the bound to
    the actual one-byte wire-format limit.

  * common/mux/frame.go: snapshot *b.UDP into a local before writing
    the address bytes (closes the window between the network-type byte
    and the address bytes), and actually return WriteAddressPort's
    error instead of dropping it.

Refs #5901
@Fangliding

Fangliding commented Apr 10, 2026 •

Copy link
Copy Markdown
Member

除了你手动使用unsafe构造的非法数据并触发panic 我没在核心发现任何有尝试在赋值后修改UDP携带数据的位置 b.UDP 应该是一个不变量 如果你发现有对其的覆盖操作应该修复这点 至少我翻了一下核心没有发现 如果你发现可以指出

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mux: server crashes with OOM panic on UDP keep frames — race on b.UDP + signed-int bypass in isDomainTooLong

2 participants