Repository navigation
Description
Related: #894, #1152, #1185, #2887
Code has moved since f53c12bc; still present at e09257ab neqo-transport/src/connection/mod.rs:1903.
See also: #4338
When a server puts a Handshake packet in front of the Initial packet that carries its ServerHello in the same UDP datagram, a neqo client processes neither packet, and its handshake makes no progress on that datagram. A single datagram in this order costs the same as a lost one, because the client recovers when the Initial data is retransmitted without a Handshake packet ahead of it. Against a server that always coalesces in this order, the connection cannot complete and ends with the 30 s idle timeout. No deployed server is known to do this, and nothing crashes.
The cause is the KeysPending branch of Connection::input_path() in neqo-transport/src/connection/mod.rs. When a packet needs keys that are not installed yet, that branch saves the packet together with everything behind it in the datagram, files them under the first packet's epoch, and returns. RFC 9000 Section 12.2 instead says the receiver MUST attempt to process the remaining packets. Here the Handshake keys the saved unit is waiting for can only come from the ServerHello inside that same unit, so it is never released. The attached reproducer hands a default client a stock neqo server's own packets in the order Handshake then Initial, once as one datagram and once as two:
- With two datagrams, the client processes both CRYPTO frames and raises
AuthenticationNeeded. - With one datagram, it processes none and is still in
WaitInitialafter 20 s of timers.
The behaviour dates from 6b6d82585ad8 ("1rtt reordering test (#894)", 2020-08-25) and first shipped in v0.4.11. A patch that saves only the waiting packet and continues with the rest of the datagram is attached.
Details
This is an ordinary bug, not a security issue: only the server the client is connecting to (or a
device on the path that could just as well drop the datagram) can send packets in this order, and the
result is a stalled handshake that ends in an idle timeout, with no crash, no unbounded memory use and
no effect on keys or authentication.
Connection::input_path() stops working on a datagram at the first packet whose keys are not installed
yet, and saves that packet together with everything behind it until those keys exist.
A client with default parameters is given one datagram that holds two of a neqo server's own packets in the
order Handshake (EncryptedExtensions to Finished), Initial (ACK and ServerHello). The client processes
neither packet: Stats::frame_rx.crypto does not move, the state stays WaitInitial, no
AuthenticationNeeded event is raised, and 20 seconds of timer callbacks change nothing. The same two
packets in the same order, each in a datagram of its own, are both processed at once and the client asks
for the certificate to be verified.
RFC 9000, Section 12.2 requires the two deliveries to be treated alike:
The receiver of coalesced QUIC packets MUST individually process each QUIC packet and separately
acknowledge them, as if they were received as the payload of different UDP datagrams. For example, if
decryption fails (because the keys are not available or for any other reason), the receiver MAY either
discard or buffer the packet for later processing and MUST attempt to process the remaining packets.
Connection::input_path() walks the packets of a datagram in a loop. When packet::Public::decrypt()
reports that the keys for a packet are not installed yet, the loop is left instead of continued
(neqo-transport/src/connection/mod.rs:1892):
Error::KeysPending(epoch) => {
// This packet can't be decrypted because we don't have the keys yet.
// Don't check this packet for a stateless reset, just return.
let remaining = slc_len;
self.save_datagram(epoch, d, remaining, now);
return Ok(());
}slc_len (mod.rs:1820) is the length of the datagram from the start of the current packet to its end,
so save_datagram() stores the packet that waits for keys and every packet behind it as one unit, filed
under the epoch of the first (mod.rs:1492):
let d = Datagram::new(
d.source(),
d.destination(),
d.tos(),
d[d.len() - remaining..].to_vec(),
);
self.saved_datagrams.save(epoch, d, now);Saved datagrams are fed back into input() by process_saved() (mod.rs:1463) only after
SavedDatagrams::make_available() was called for their epoch, and for Epoch::Handshake that happens in
one place, when Connection::handshake() installs the Handshake keys (mod.rs:3377):
if self.crypto.install_keys()? {
self.saved_datagrams.make_available(Epoch::Handshake);
}With two datagrams this works: the Handshake packet is saved alone, the Initial packet arrives next and is
decrypted with the Initial keys the client has had all along, its ServerHello yields the Handshake keys,
and process_multiple_input() runs process_saved() before it returns (mod.rs:1168), which processes
the saved Handshake packet.
With one datagram, the Initial packet is inside the unit saved under Epoch::Handshake. The Handshake
keys can only come from the ServerHello in that Initial packet, so make_available(Epoch::Handshake) is
never reached on account of this datagram. It stays in the store until the same ServerHello bytes arrive
again in an Initial packet that is not behind a Handshake packet.
If the store for the epoch already holds SavedDatagrams::CAPACITY (4) datagrams, save()
(neqo-transport/src/saved.rs:46) does not keep the new one, and the packets behind the waiting packet
are dropped without having been looked at.
KeysPending is returned for two epochs (CryptoStates::rx_pending(), neqo-transport/src/crypto.rs:1083):
Handshake and 1-RTT. A 1-RTT packet has no length field and is always the last packet of a datagram, so
the arrangement that is affected is a Handshake packet in front of an Initial packet, received before
the Handshake keys exist. RFC 9000 describes the usual order Initial, Handshake, 1-RTT as one that "makes
it more likely that the receiver will be able to process all the packets in a single pass"; it is not a
requirement on the sender, and the receiver rule quoted above covers the other orders.
The behaviour was introduced in 6b6d82585ad8 ("1rtt reordering test (#894)", 2020-08-25), which
replaced the earlier handling (a key-less packet at the start of a datagram was dropped and the loop
went on to the next packet) with the KeysPending branch that saves the rest of the datagram and
returns; it was first released in v0.4.11.
Impact
This is a deviation from a MUST in RFC 9000 with an interoperability consequence. A neqo client makes
no handshake progress on a datagram in which the server placed a Handshake packet in front of the
Initial packet that completes the ServerHello. One such datagram costs what a lost datagram costs: the
client recovers when the server retransmits that Initial data without a Handshake packet in front of
it. A server that always coalesces in this order cannot complete a handshake with a neqo client, so
for a Firefox user the HTTP/3 connection attempt to that server fails after the 30 s idle timeout. No
deployed server is known to order its packets this way, so users are unlikely to meet this often. In
a simulated handshake (50 ms round trip) between a default client and
the server of the reproducer in which nothing but the order of the packets in the server's
Initial+Handshake datagrams was changed, the client processed no CRYPTO frame and closed with an idle
timeout about 30 seconds later; in the server's own order the same handshake is confirmed after 125 ms.
The code is shared by both roles, but a server lacks Handshake keys only while a ClientHello is
incomplete, when the client has no Handshake packets to send, so in practice this concerns the client.
Nothing crashes, in debug or optimised builds, and the store of saved datagrams is capped at 4 per
epoch.
Suggested fix
The attached patch changes the KeysPending arm of Connection::input_path()
(neqo-transport/src/connection/mod.rs:1892) to save only the packet that waits for keys, as a
datagram of its own with the original addresses and TOS, and to continue with the packets behind it:
Error::KeysPending(epoch) => {
// This packet can't be decrypted because we don't have the keys yet.
- // Don't check this packet for a stateless reset, just return.
- let remaining = slc_len;
- self.save_datagram(epoch, d, remaining, now);
- return Ok(());
+ // Save it, without checking it for a stateless reset, and move on
+ // to any packets that are coalesced after it.
+ // … (quotes the RFC 9000, Section 12.2 rule)
+ let saved = Datagram::new(remote, local, tos, e.data.to_vec());
+ self.save_datagram(epoch, saved, now);
+ dcid = Some(e.dcid);
+ slc = remainder;
+ continue;
}To support this, save_datagram() (mod.rs:1485) now takes the one-packet Datagram as built by the
caller instead of slicing the tail off the received one, so its remaining parameter, the
needless_pass_by_value expectation and slc_len (mod.rs:1820) go away, and input_path() keeps
d.destination() in a new local. save_datagram() has no other caller, and KeysPending was the
only arm that left the loop this way; the other decryption failures already continue.
e.data is the slice of the one packet and is still as received, because decrypt() returns
KeysPending before it removes header protection (neqo-transport/src/packet/mod.rs:959). Setting
dcid keeps the same-DCID check for the packets that follow and, as before, leaves a datagram that
started with such a packet out of the stateless reset check after the loop. Stats::saved_datagrams
then counts saved packets, which is what its doc comment already says; packets_rx accounting is
unchanged. One behaviour change to be aware of: a datagram with several packets that wait for keys now
takes one of the SavedDatagrams::CAPACITY (4) slots per packet instead of one for the whole tail.
The patch also adds the unit test coalesced_handshake_before_initial to
neqo-transport/src/connection/tests/handshake.rs: it puts the server's Handshake packet in front of
its Initial packet in one datagram and checks that the client saves one packet, drops none, reaches
Connected, and that the server then reaches Confirmed.
With the patch on a clean checkout the reproducer prints OK, cargo test --locked --workspace --exclude mtu passes, and cargo +nightly fmt --check and
cargo clippy --locked --all-targets -- -D warnings are clean.
Proposed patches
patches/1.patch
# neqo-transport/src/connection/mod.rs
# Save only the packet that waits for keys in Connection::input_path() and keep processing the packets
# coalesced after it, with a regression test
#
# On Error::KeysPending, Connection::input_path() saved the packet that waits for keys together with every
# packet coalesced behind it and returned, so an Initial packet behind a Handshake packet was filed under
# the Handshake epoch and never processed, although it carries the ServerHello that yields those very keys;
# RFC 9000 Section 12.2 says the receiver MUST attempt to process the remaining packets. The fix is in that
# loop, where the decision to stop is made: only the one packet (e.data, still as received because decrypt()
# returns KeysPending before removing header protection) is saved as a datagram of its own with the original
# addresses and TOS, dcid is set so that the same-DCID rule still applies to the following packets and the
# saved packet is still not treated as a stateless reset, and the loop continues. KeysPending is the only
# arm that left the loop this way (the other decryption failures already continue), and save_datagram() has
# no other caller. Behaviour changes to know about: a datagram with several packets that wait for keys now
# takes one slot of SavedDatagrams (CAPACITY 4 per epoch) per packet instead of one for the whole tail, and
# Stats::saved_datagrams counts those packets, which is what its doc comment already says; packets_rx
# accounting is unchanged. Adds the unit test coalesced_handshake_before_initial (fails before, passes
# after, also with disable-encryption); cargo test --locked --workspace --exclude mtu, cargo +nightly fmt
# --check and cargo clippy --locked --all-targets -- -D warnings are clean with the patch.
# reviewer: good — Fixes the loop where the decision to stop is made (save only the one packet, carry on with
# the rest), keeps the stateless-reset and same-connection-ID handling as before, adds a regression test,
# and the whole test suite, formatter check and linter pass with it; the only side effect is that several
# key-less packets in one datagram now use one of the 4 save slots each instead of one in total.
# our check: FIXED · confidence high
#
diff --git a/neqo-transport/src/connection/mod.rs b/neqo-transport/src/connection/mod.rs
index c7261652..c5ea4b33 100644
--- a/neqo-transport/src/connection/mod.rs
+++ b/neqo-transport/src/connection/mod.rs
@@ -1476,28 +1476,12 @@ impl Connection {
}
}
- /// In case a datagram arrives that we can only partially process, save any
- /// part that we don't have keys for.
- #[expect(
- clippy::needless_pass_by_value,
- reason = "To consume an owned datagram below."
- )]
- fn save_datagram(
- &mut self,
- epoch: Epoch,
- d: Datagram<impl AsRef<[u8]>>,
- remaining: usize,
- now: Instant,
- ) {
- let d = Datagram::new(
- d.source(),
- d.destination(),
- d.tos(),
- d[d.len() - remaining..].to_vec(),
- );
+ /// In case a datagram arrives that we can only partially process, save the
+ /// packet that we don't have keys for, as a datagram of its own.
+ fn save_datagram(&mut self, epoch: Epoch, d: Datagram, now: Instant) {
self.saved_datagrams.save(epoch, d, now);
self.stats.borrow_mut().saved_datagrams += 1;
- // We already counted the datagram as received in [`input_path`]. We
+ // We already counted the packet as received in [`input_path`]. We
// will do so again when we (re-)process it, so reduce the count now.
self.stats.borrow_mut().packets_rx -= 1;
}
@@ -1808,6 +1792,7 @@ impl Connection {
trace!("[{self}] {} input {}", path.borrow(), Hex::new(&d));
let tos = d.tos();
let remote = d.source();
+ let local = d.destination();
let mut slc = d.as_mut();
self.stats.borrow_mut().bytes_rx += slc.len();
let mut dcid = None;
@@ -1817,7 +1802,6 @@ impl Connection {
while !slc.is_empty() {
self.stats.borrow_mut().packets_rx += 1;
self.stats.borrow_mut().dscp_rx[tos.into()] += 1;
- let slc_len = slc.len();
let (packet, remainder) =
match packet::Public::decode(slc, &*self.cid_manager.decoder()) {
Ok((packet, remainder)) => {
@@ -1891,10 +1875,20 @@ impl Connection {
match e.error {
Error::KeysPending(epoch) => {
// This packet can't be decrypted because we don't have the keys yet.
- // Don't check this packet for a stateless reset, just return.
- let remaining = slc_len;
- self.save_datagram(epoch, d, remaining, now);
- return Ok(());
+ // Save it, without checking it for a stateless reset, and move on
+ // to any packets that are coalesced after it.
+ //
+ // > For example, if decryption fails (because the keys are not
+ // > available or for any other reason), the receiver MAY either
+ // > discard or buffer the packet for later processing and MUST
+ // > attempt to process the remaining packets.
+ //
+ // <https://www.rfc-editor.org/rfc/rfc9000.html#section-12.2>
+ let saved = Datagram::new(remote, local, tos, e.data.to_vec());
+ self.save_datagram(epoch, saved, now);
+ dcid = Some(e.dcid);
+ slc = remainder;
+ continue;
}
// Exhausting read keys is fatal. So is a packet that
// authenticated but broke the protocol (e.g. reserved
diff --git a/neqo-transport/src/connection/tests/handshake.rs b/neqo-transport/src/connection/tests/handshake.rs
index 00d106d7..e4a44587 100644
--- a/neqo-transport/src/connection/tests/handshake.rs
+++ b/neqo-transport/src/connection/tests/handshake.rs
@@ -442,6 +442,45 @@ fn reorder_05rtt_with_0rtt() {
// Initial packet number space.
}
+/// Test that a client processes an Initial packet that is coalesced after a
+/// Handshake packet that it can't process yet, rather than saving both.
+#[test]
+fn coalesced_handshake_before_initial() {
+ // The server's first flight needs to fit in a single datagram for this test.
+ let mut client = new_client(ConnectionParameters::default().mlkem(false));
+ let mut server = new_server(ConnectionParameters::default().mlkem(false));
+
+ let c1 = client.process_output(now()).dgram();
+ assert!(c1.is_some());
+ let s1 = server.process(c1, now()).dgram().unwrap();
+
+ // Swap the Initial and Handshake packets, dropping any padding.
+ let (s_initial, s_rest) = split_datagram(&s1);
+ let (s_handshake, _) = split_datagram(&s_rest.unwrap());
+ assert_initial(&s_initial, false);
+ assert_handshake(&s_handshake);
+ let swapped = Datagram::new(
+ s1.source(),
+ s1.destination(),
+ s1.tos(),
+ [&s_handshake[..], &s_initial[..]].concat(),
+ );
+
+ // The client saves the Handshake packet, as it has no keys for it.
+ // The Initial packet that follows provides those keys, after which
+ // the client can go back and process the saved Handshake packet.
+ client.process_input(swapped, now());
+ assert_eq!(client.stats().saved_datagrams, 1);
+ assert_eq!(client.stats().packets_rx, 2);
+ assert_eq!(client.stats().dropped_rx, 0);
+ assert!(maybe_authenticate(&mut client));
+ assert_eq!(*client.state(), State::Connected);
+
+ let c2 = client.process_output(now()).dgram();
+ server.process_input(c2.unwrap(), now());
+ assert_eq!(*server.state(), State::Confirmed);
+}
+
/// Test that a server that coalesces 0.5 RTT with handshake packets
/// doesn't cause the client to drop application data.
#[test]When fixing this, please carry this line into the commit message:
Discovered by Anthropic's OSS Scanner, as bug ANT-2026-BGTLTF6Z.
Metadata
Metadata
Assignees
Labels
Type
Fields
Priority
Activity