Skip to content

fix(udp): name the listener, not the backend, as PPv2 destination - #1927

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
fix/udp-proxy-protocol-destination
Nov 18, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
fix/udp-proxy-protocol-destination

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

Problem

The PROXY protocol v2 header Sōzu prepends to UDP datagrams (send_proxy_protocol, first datagram or every datagram) named the backend as its destination. The spec defines the destination as the address the client contacted, and Sōzu's TCP serializer already does that: SendProxyProtocol::back_writable (lib/src/protocol/proxy_protocol/send.rs) uses the accepted socket's local_addr().

On the UDP side, UdpManager::forward_admission_datagram and UdpManager::forward_on_existing_flow (lib/src/protocol/udp/manager.rs) called prepend_dgram_header(&mut out, flow.client, backend).

This was observed on 2026-11-17 against a production load balancer (loadbalancer-agent 0.7.5, Sōzu 2.3) with proxyProtocol: Send. These are the first 28 bytes of the first datagram:

UDP  0d0a0d0a000d0a515549540a 21 12 000c 5bd0cf21 0a650006 …   dst 10.101.0.6   (the backend node)
TCP  0d0a0d0a000d0a515549540a 21 11 000c 5bd0cf21 522ffa14 …   dst 82.47.250.20 (the LB's public IP)

The source address, the real client, was right in both captures. Only the UDP destination was wrong.

Fix

  • ManagerInput::ClientDatagram gains dst: SocketAddr, the local address the datagram arrived on. The core passes it to prepend_dgram_header at both forward sites, so first-datagram-only and every-datagram modes both carry it. The Transmit still goes to the backend; only the header changes.
  • The shell fills dst from a new UdpListener::local_address field (lib/src/udp.rs). That field is read once from local_addr() when UdpListener::activate registers the socket, so the receive path pays no getsockname per datagram. Reading the socket rather than the config keeps an ephemeral-port bind and an inherited socket correct, the same way the TCP side reads the accepted socket.
  • dgram_header / prepend_dgram_header rename their second parameter from backend to destination, and their docs now say what it is.
  • Docs: lib/src/protocol/udp/LIFECYCLE.md (the message contract and §8), the send_proxy_protocol row in doc/configure.md, and CHANGELOG.md under Unreleased → Fixed.

dst is carried per datagram, not stored per flow. That leaves room to read the real destination from the packet later as a change to the shell alone.

Wildcard binds: a known limitation, stated

A listener bound to 0.0.0.0 or :: has no single address the client contacted. Sōzu does not read the datagram's real destination from the packet (IP_PKTINFO / IPV6_RECVPKTINFO on recvmsg(2)). On such a listener the header now names the wildcard IP with the listener's port. That is honest but incomplete: it is never the backend any more, and the IP is unspecified.

The return path already has the matching limitation: on a wildcard socket, send_to lets the kernel choose the reply's source address.

Doing this properly means:

  • replacing mio's recv_from with recvmsg and cmsg parsing in the hot path, which is new unsafe;
  • covering v4, v6 and dual-stack;
  • setting the socket option in activate so that inherited sockets get it as well.

That is its own change, so it is left out here and documented in LIFECYCLE.md §8 and the changelog.

For the case that was reported, the listener is bound to the LB's public IP. loadbalancer-agent binds public_ipv4.unwrap_or(0.0.0.0), and the TCP capture shows that the LB has a public IPv4. The wildcard case only arises for an address family the LB has no public address for.

Tests

  • New unit test proxy_protocol_destination_is_the_listener_not_the_backend in lib/src/protocol/udp/manager.rs. It runs both modes through UdpManager::handle_input with pairwise-distinct client, listener and backend addresses. It asserts that the header's source is the client, that its destination is the listener and not the backend, and that the transmit still goes to the backend.
  • E2E test_udp_proxy_protocol_to_backend now also asserts the advertised destination is the listener. To support that, the mock backend's PPv2 parser (e2e/src/mock/udp_backend.rs) now also returns the destination, and its own unit test checks it.
  • Consumers of ClientDatagram were updated: the unit tests, the sozu-sim harness, fuzz_udp_flow (with a fixed listener address, so the input layout is unchanged) and one test in lib/src/server.rs.

Red with the fix reverted (both call sites back to backend), then green:

test protocol::udp::manager::tests::proxy_protocol_destination_is_the_listener_not_the_backend ... FAILED
assertion `left == right` failed: every_datagram=false: PPv2 destination is the listener
  left: 10.0.0.1:5353
 right: 192.0.2.10:53

test tests::udp_tests::test_udp_proxy_protocol_to_backend ... FAILED
PPv2 destination mismatch: saw Some(127.0.0.1:20001), expected the listener 127.0.0.1:20000
test protocol::udp::manager::tests::proxy_protocol_destination_is_the_listener_not_the_backend ... ok
test tests::udp_tests::test_udp_proxy_protocol_to_backend ... ok

Commands run

Local runs. The workstation's environment forced toolchain 1.99.1, so clippy was also run on the repo-pinned 1.93.1.

cargo +nightly fmt --all -- --check                                              -> exit 0
cargo clippy --workspace --all-targets --all-features --locked -- -D warnings    -> exit 0 (1.99.1)
cargo clippy --all-targets --locked -- -D warnings                               -> exit 0 (1.93.1)
cargo test --workspace --locked                                                  -> 1732 passed, 4 failed, 48 ignored
RUSTFLAGS="--cfg tokio_unstable" cargo test -p sozu-sim --locked                 -> exit 0 (all ok)
cargo +nightly fuzz run fuzz_udp_flow -- -max_total_time=10                      -> Done 178924 runs in 11 second(s)
python3 .github/scripts/check_doc_citations.py --base origin/main                -> all OK

The 4 workspace failures are the tests::fuzz_tests::* wrappers that CI skips (--skip tests::fuzz_tests::). On this workstation RUSTUP_TOOLCHAIN pins a stable toolchain over +nightly, so the wrappers fail with "the option Z is only accepted on the nightly compiler". With nightly forced, fuzz_udp_flow itself passes; that run is shown above.

cargo test --workspace --all-features additionally fails 411 e2e tests on rustls's "Could not automatically determine the process-level CryptoProvider". --all-features enables several crypto providers at once, and CI never tests that combination: it runs one crypto cell per job.

Impact

  • Protocol: a backend that parses PPv2-over-UDP now receives the address the client contacted as destination. A backend that used the old destination as "my own address" sees a different value. That old value was not what the spec defines.
  • Library API: ManagerInput::ClientDatagram gains a field, so code that constructs it no longer compiles until it passes dst.
  • Security: none. No new unsafe, no new syscall per datagram, and no change to admission, limits or the return path.

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator Author

Additional evidence beyond the description:

Every-datagram path seen red on its own. Reverting only the UdpManager::forward_on_existing_flow call site to backend, and keeping the admission site fixed, fails the new test in the every-datagram iteration:

test protocol::udp::manager::tests::proxy_protocol_destination_is_the_listener_not_the_backend ... FAILED
assertion `left == right` failed: every_datagram=true: PPv2 destination is the listener
  left: 10.0.0.1:5353
 right: 192.0.2.10:53

--all-features workspace run. The 411 e2e failures break down into 416 panics:

  • 392 at rustls crypto/mod.rs ("Could not automatically determine the process-level CryptoProvider").
  • 4 in the fuzz_tests wrappers.
  • 20 assertion or fixture failures in TLS, H2 and TCP e2e tests, one of which is a rabbitmq container timeout.

No unit-test binary failed. All 17 udp_tests passed in that run.

In the default-features run (cargo test --workspace --locked), every e2e test passed except the 4 fuzz_tests wrappers: 693 passed, 4 failed.

Formatting. Re-checked with the nightly toolchain invoked directly, because this workstation's environment overrides +nightly:

rustfmt 1.10.0-nightly (574ff7d98b 2026-09-14)
cargo fmt --all -- --check -> exit 0

The PROXY protocol v2 header prepended to UDP datagrams carried the
backend the datagram was forwarded to as its destination. The spec
defines the destination as the address the client contacted, and the
TCP serializer (SendProxyProtocol::back_writable) already uses the
accepted socket's local address. A production capture on 2026-11-17
showed dst 10.101.0.6 (the backend node) over UDP against the load
balancer's public IP over TCP.

ManagerInput::ClientDatagram gains a per-datagram `dst`, the address
the datagram arrived on, which both forward paths pass to
prepend_dgram_header, so first-datagram-only and every-datagram modes
are fixed alike. The shell fills it from UdpListener::local_address,
read once from local_addr() when the listener socket is activated.

A listener bound to 0.0.0.0 or :: advertises that wildcard IP with the
listener port: the datagram's real destination (IP_PKTINFO /
IPV6_RECVPKTINFO) is not read. Documented in the UDP LIFECYCLE.md,
doc/configure.md and the changelog.

A manager-level test covers both modes with distinct client, listener
and backend addresses, and the PPv2 e2e test now asserts the
advertised destination is the listener.

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the fix/udp-proxy-protocol-destination branch from 3634499 to ca4d701 Compare November 18, 2026 11:30
@FlorentinDUBOIS
FlorentinDUBOIS merged commit fe758a9 into main Nov 18, 2026
46 checks passed
FlorentinDUBOIS added a commit that referenced this pull request Nov 18, 2026
Prepare Sōzu 2.3.6: #1927 (the PROXY protocol v2 header Sōzu prepends
to UDP datagrams names the UDP listener's address as its destination,
not the backend it forwards to) and #1928 (libz-sys 1.1.33 for
RUSTSEC-2026-0544; the sozu-e2e Kafka test moves from rskafka to
kafka-protocol, dropping chrono and RUSTSEC-2026-0543).

Align all six package versions, workspace dependency minima, lockfiles,
RPM metadata and changelog. Advance the previous-stable two-worker
upgrade gate from published 2.3.4 to published 2.3.5 (archive
63332cdb…, executable aa63f2ce…, `sozu 2.3.5 (768fad4 HEAD)`), keeping
the 2.2.1 bridge and 2.3.1 metadata cases.

Signed-off-by: Florentin Dubois <[email protected]>
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.

1 participant