AEAD-2022 TCP client can pick padding 0 for an empty first payload, which its own server rejects (~1/901 handshakes fail)
Summary
With 2022-blake3-* methods, the TCP client and server in the shadowsocks crate disagree about the padding of a request header that carries no payload:
- The client chooses the padding length with
random_range(0..=AEAD2022_MAX_PADDING_SIZE)when the first payload is empty (crates/shadowsocks/src/relay/mod.rs,get_aead_2022_padding_size), so it picks0with probability 1/901. - The server rejects exactly that case: when
padding_size == 0and the first data chunk ends right after the header, it returnsno payload in first data chunk, and padding is 0and closes the connection (crates/shadowsocks/src/relay/tcprelay/proxy_stream/server.rs, inProxyServerStream::handshake).
A client that triggers the lazy handshake with an empty write — which proxy_stream/client.rs itself recommends for server-first protocols (the note referencing issue #232) — therefore gets about 1 in 901 connections silently rejected by a ssserver built from the same crate.
Observed with shadowsocks 1.24.0 on both sides; the same code is present in 1.25.0.
Server log
WARN tcp handshake failed. peer: <addr>, no payload in first data chunk, and padding is 0How to reproduce
- Create a
ProxyClientStreamwith a2022-blake3-aes-256-gcmconfig over any stream connected to aProxyServerStream(an in-memorytokio::io::duplexworks). - Call
write(&[])on the client stream to send the header without payload, then runhandshake()on the server. - Repeat a few thousand times: roughly 1/901 of the server handshakes fail with the error above.
In an in-memory test, 7 of 9,010 empty-payload handshakes were rejected and 0 of 9,010 with payload.
Sending the header together with a non-empty payload never fails, because the client then uses padding 0 and the server only enforces non-zero padding when there is no payload.
Expected
The client should never produce a header the server rejects. When the first payload is empty, the padding length should be drawn from 1..=AEAD2022_MAX_PADDING_SIZE instead of 0..=AEAD2022_MAX_PADDING_SIZE.
Workaround we use
Wait briefly for the application's first bytes and send them with the header; only fall back to the empty write for server-first protocols. This removes the failure for client-first traffic but not for server-first protocols.
Source: shadowsocks/shadowsocks-rust