Repository navigation
Description
When an application calls Connection::set_stream_max_data() with a value below the credit the peer already holds, neqo-transport's stream flow control breaks. The next MAX_STREAM_DATA frame can panic with attempt to subtract with overflow at neqo-transport/src/fc.rs:424 in builds with overflow checks. In release builds it either sets the stream window to the 10 MiB auto-tuning maximum instead of the requested value, or sends a lower limit than before and then closes the connection with FLOW_CONTROL_ERROR although the peer stayed within the earlier limit. Only applications that call this API on neqo_transport::Connection themselves are affected: nothing in neqo-http3 or elsewhere in the tree outside tests calls it, so Firefox cannot reach it, and there is no memory-safety effect.
RecvStream::set_stream_max_data() passes the value to ReceiverFlowControl::set_max_active() (fc.rs:341), which stores it unconditionally. auto_tune_inner() and next_limit() assume max_allowed - retired <= max_active. A later raising call or a retransmission after the last MAX_STREAM_DATA frame is lost then triggers a frame while that assumption is false. Since auto-tuning (4c373aa, #1868, first in v0.13.0) raises the window without the application knowing, two calls that both ask for more than the configured window can still lower it. Whether they do depends on how fast the peer sent. The lower-then-raise path that retracts advertised credit is older and goes back to the setter's introduction in e516489 (v0.4.23). The peer influences the outcome through its sending rate and by not acknowledging the last MAX_STREAM_DATA frame, but it gains only the panic in builds with overflow checks. The attached reproducer is a neqo-transport integration test with a default client and a stock server, with neqo unmodified. It shows the panic in auto_tune_inner() from inside Connection::process_output(), and the client closing with Transport(FlowControl) after the server sent only up to the offset it had been allowed. A patch that keeps next_limit() from going below the limit already sent and saturates the subtraction is included.
Details
Connection::set_stream_max_data() stores the value it is given as the stream's receive window, also when that is
less than the credit the peer already holds. The code that writes the next MAX_STREAM_DATA frame assumes the window
never shrinks below the outstanding credit. If a frame is written while it has, a subtraction in the auto-tuning code
underflows, or the client sends a lower limit than before and then treats the peer's use of the earlier limit as a
flow control violation.
A default client (1048576-byte stream window) receives a unidirectional stream from a stock neqo server. The
application calls set_stream_max_data(stream, …):
| situation | calls | default test profile | --release |
|---|---|---|---|
| quiet stream | 1548576, then 2048576 | server may send 2048576 more bytes | same |
| server sent fast for 600 ms, the client auto-tuned the window, server paused holding more than 8 MiB of credit | 1548576, then 2048576 | panicked at neqo-transport/src/fc.rs:424:33: attempt to subtract with overflow |
server may send 10485760 more bytes |
| stream the application is not reading | 524288 | server sends up to offset 1048576, client stays Confirmed |
same |
| stream the application is not reading | 524288, then 786432 | server sends up to offset 1048576, client closes with Transport(FlowControl) |
same |
The first two rows make the same two calls, both asking for more than the configured window. In the second row the
application cannot know that its values are below the auto-tuned window: there is no API to read it.
The setter (neqo-transport/src/fc.rs:341, reached through RecvStream::set_stream_max_data(),
recv_stream.rs:1143):
pub const fn set_max_active(&mut self, max: u64) {
// If max_active has been increased, send an update immediately.
self.frame_pending |= self.max_active < max;
self.max_active = max;
}Everywhere else max_allowed - retired <= max_active holds, because a limit is always written as
retired + max_active and auto-tuning only raises max_active. A lower value breaks that, and this is a supported
input: increase_decrease_flow_control (connection/tests/stream.rs:1220) lowers a window of 300 to 200 and expects
the next window to be 200 once the old one has drained. That works, because should_send_update() (fc.rs:308) and
send_flowc_update() (fc.rs:302) ask for a frame only when the limit would grow. Two other paths ask for a frame
while max_allowed - retired > max_active still holds:
- a later call with a higher value:
self.max_active < maxabove compares with the lowered window, not with what
was advertised; - loss of the last MAX_STREAM_DATA frame (
frame_lost(),fc.rs:330).
ReceiverFlowControl<StreamId>::write_frames() (fc.rs:516) then runs:
self.auto_tune(now, rtt);
let max_allowed = self.next_limit(); // min(self.retired + self.max_active, MAX_VARINT)
if builder.write_varint_frame(&[FrameType::MaxStreamData.into(), self.subject.as_u64(), max_allowed]) {
…
self.frame_sent(max_allowed); // self.max_allowed = new_maxUnderflow in auto-tuning (auto_tune_inner(), fc.rs:424):
let window_bytes_used = self.max_active - (self.max_allowed - self.retired);In the second row max_active is 2048576 and max_allowed - retired is more than 8 MiB. With overflow checks this
panics inside Connection::process_output(). Without them the difference wraps to almost 2^64,
excess * WINDOW_INCREASE_MULTIPLIER wraps again, and min(…, max_window) clamps the result to
MAX_LOCAL_MAX_STREAM_DATA: the application asked for 2048576 and the window becomes 10485760. When the higher
value is set shortly after a MAX_STREAM_DATA frame was sent, the wrapped sum comes out below max_active instead,
the function returns at fc.rs:436, and the lower limit is sent as described next; a release build closed the
connection with Transport(FlowControl) for a raise followed 2 ms later by lowering the window by 1000 and raising
it by 1, on a stream the application was not reading. The frame-loss path reaches the same line: with the client's
packets dropped after a raise, followed by one lowering call, the retransmission panics at fc.rs:424 in the default
test profile.
A lower limit is sent and enforced. auto_tune_inner() returns at once when the stream has not sent a
MAX_STREAM_DATA frame yet (last_update is None, fc.rs:367). In the fourth row the application has read 5
bytes, so next_limit() is 5 + 786432 = 786437, below the 1048576 from the client's transport parameters. The frame
goes out with that value (TX -> MaxStreamData { stream_id: StreamId(3), maximum_stream_data: 786437 } in the
client's log with RUST_LOG=neqo_transport=trace), the server ignores it as RFC 9000 Section 4.1 requires, and
frame_sent() records 786437 as max_allowed. The server's next STREAM frame past that offset fails the check in
set_consumed() (fc.rs:577, called from recv_stream.rs:719; the log shows Stream RX window exceeded: 787415):
if consumed > self.max_allowed {
trace!("Stream RX window exceeded: {consumed}");
return Err(Error::FlowControl);
}The setter for the stream-count limit has the guard this one lacks: Streams::set_remote_max_streams()
(streams.rs:583) calls set_max_active() only when the new value is higher.
The underflow, and with it the case where two raising calls lower the window, was introduced in 4c373aa95fae
("perf(transport): auto-tune stream receive window (#1868)", 2025-03-31), first in v0.13.0. The lower-then-raise
case (fourth row) is older: it goes back to the setter itself, e5164895733f ("Add API to increase a stream receiver
buffer (#1125)", 2021-03-17), first in v0.4.23.
Impact
An application that adjusts a stream's receive window with Connection::set_stream_max_data() can have its
connection closed with FLOW_CONTROL_ERROR although the peer stayed within the advertised limit, can get the 10 MiB
maximum window instead of the one it asked for, and panics in builds with overflow checks. The peer has a part in it:
how far auto-tuning has raised the window, and so whether a call lowers it, depends on how fast the peer sends, and a
peer that does not acknowledge the last MAX_STREAM_DATA frame causes the retransmission. What the peer gains is the
panic in a build with overflow checks; the other outcomes give it nothing that closing the connection would not.
There is no memory-safety effect. Nothing in neqo-http3 calls this function (the Http3Client wrapper was removed
in c535de9, "Remove dead code"), and nothing else in the tree outside tests does, so Firefox cannot reach it and only applications that drive neqo_transport::Connection themselves and use
this call are affected. The doc comment on the function says "Increases max_stream_data"
(connection/mod.rs:4094), but the second row shows that increasing values are enough. The connection-level window
has no setter and is not affected.
Suggested fix
The attached patch changes ReceiverFlowControl in neqo-transport/src/fc.rs, where the broken assumption lives:
next_limit() never returns less than the limit already sent (max_allowed), which covers both the later-raise and
the lost-frame paths, and the auto-tuning subtraction saturates. The fix, without its tests:
--- a/neqo-transport/src/fc.rs
+++ b/neqo-transport/src/fc.rs
@@ -8,7 +8,7 @@
use std::{
- cmp::min,
+ cmp::{max, min},
fmt::{Debug, Display},
@@ -316,7 +316,10 @@ where
pub fn next_limit(&self) -> u64 {
min(
- self.retired + self.max_active,
+ // A limit that has been sent cannot be taken back, so never go
+ // below it. `max_active` can be lowered to less than the credit
+ // that the peer still holds, see `set_max_active`.
+ max(self.retired + self.max_active, self.max_allowed),
// Flow control limits are encoded as QUIC varints and are thus
@@ -421,7 +424,11 @@ where
- let window_bytes_used = self.max_active - (self.max_allowed - self.retired);
+ // If `max_active` was lowered manually, the peer can hold more credit
+ // than `max_active`. None of the window is in use then.
+ let window_bytes_used = self
+ .max_active
+ .saturating_sub(self.max_allowed - self.retired);
let Some(excess) = window_bytes_used.checked_sub(window_bytes_expected) else {Lowering a window keeps working as before (it takes effect once the old credit has drained), so no existing test
changes; connection-level and stream-count flow control share next_limit() but never lower max_active. The patch
adds two unit tests in fc.rs, lowering_max_active_does_not_lower_limit and
auto_tune_with_max_active_below_limit_sent (the frame-loss path), which fail without the fix.
With this change repro prints OK: the second test no longer panics and the server's credit is unchanged by the
two calls, and the client in lower_then_raise stays Confirmed. cargo test --locked -p neqo-transport passes,
and cargo +nightly fmt --check and cargo clippy --locked --all-targets -- -D warnings are clean. A frame that
repeats the current limit is still written after a call that raises max_active without raising the limit.
A raise-only setter, as set_remote_max_streams() is, would also stop the reproducer, but it removes a supported
behaviour: increase_decrease_flow_control expects a lowered window to take effect once the old one has drained
(assert_eq!(to_u64(written3), new_fc), connection/tests/stream.rs:1216), and fc.rs would still depend on every
caller of set_max_active() never lowering it. The attached patch does not take that route.
Proposed patches
patches/1.patch
# neqo-transport/src/fc.rs
# fix(transport/fc): never send a flow control limit below the one already sent, and saturate the auto-tuning
# subtraction, after max_active was lowered
#
# ReceiverFlowControl assumed that the outstanding credit (max_allowed - retired) never exceeds max_active,
# but set_max_active() may lower max_active below it, which is supported and tested
# (increase_decrease_flow_control, changing_max_active). Any frame written in that state, whether after a
# later raise through set_stream_max_data() or after loss of the last MAX_STREAM_DATA, underflowed in
# auto_tune_inner() and then sent and recorded a lower limit, so a peer within the earlier limit got
# FLOW_CONTROL_ERROR. The patch restores the invariant where the limit is computed: next_limit() never
# returns less than max_allowed, which covers both paths, and the window_bytes_used subtraction saturates.
# Lowering the window keeps working as before (it takes effect once the old credit has drained), so no
# existing test changes; connection- and stream-count flow control share next_limit() but never lower
# max_active, so they are unaffected. Two unit tests are added that fail without the fix (lower limit
# returned; subtract-with-overflow panic on the frame-loss path); the workspace tests, cargo +nightly fmt
# --check and clippy -D warnings pass. One thing left as is: raising max_active while still below the
# outstanding credit writes one frame that repeats the current limit, which is harmless.
# reviewer: good — Fixes it where the wrong assumption lives (next_limit never goes below the limit already
# sent; the auto-tuning subtraction saturates), covers both the later-raise and the lost-frame paths, keeps
# lowering working, adds two unit tests; transport tests, format check and clippy are clean.
# our check: FIXED · confidence high
#
diff --git a/neqo-transport/src/fc.rs b/neqo-transport/src/fc.rs
index 6cd884f5..d4cf286c 100644
--- a/neqo-transport/src/fc.rs
+++ b/neqo-transport/src/fc.rs
@@ -8,7 +8,7 @@
// into flow control frames needing to be sent to the remote.
use std::{
- cmp::min,
+ cmp::{max, min},
fmt::{Debug, Display},
num::NonZeroU64,
ops::{Deref, DerefMut, Index, IndexMut},
@@ -316,7 +316,10 @@ where
pub fn next_limit(&self) -> u64 {
min(
- self.retired + self.max_active,
+ // A limit that has been sent cannot be taken back, so never go
+ // below it. `max_active` can be lowered to less than the credit
+ // that the peer still holds, see `set_max_active`.
+ max(self.retired + self.max_active, self.max_allowed),
// Flow control limits are encoded as QUIC varints and are thus
// limited to the maximum QUIC varint value.
MAX_VARINT,
@@ -421,7 +424,11 @@ where
// of what `max_active` might allow.
let window_bytes_expected = (effective_window * elapsed) / (rtt);
- let window_bytes_used = self.max_active - (self.max_allowed - self.retired);
+ // If `max_active` was lowered manually, the peer can hold more credit
+ // than `max_active`. None of the window is in use then.
+ let window_bytes_used = self
+ .max_active
+ .saturating_sub(self.max_allowed - self.retired);
let Some(excess) = window_bytes_used.checked_sub(window_bytes_expected) else {
// Used below expected. No auto-tuning needed.
return;
@@ -977,6 +984,27 @@ mod test {
assert_eq!(fc.next_limit(), 196);
}
+ #[test]
+ fn lowering_max_active_does_not_lower_limit() {
+ let mut fc = ReceiverFlowControl::new((), 100);
+ fc.set_max_active(50);
+ assert!(!fc.frame_needed());
+
+ // Increase max_active, but not beyond the limit that was sent.
+ fc.set_max_active(60);
+ assert!(fc.frame_needed());
+ assert_eq!(fc.next_limit(), 100);
+ fc.frame_sent(fc.next_limit());
+
+ // All of the limit that was sent can still be used.
+ fc.consume(100).unwrap();
+
+ // The new `max_active` applies once that is used up.
+ fc.retire(100);
+ assert!(fc.frame_needed());
+ assert_eq!(fc.next_limit(), 160);
+ }
+
fn remote_stream_limits(role: Role, bidi: u64, unidi: u64) {
let mut fc = RemoteStreamLimits::new(2, 1, role);
assert!(
@@ -1546,6 +1574,35 @@ mod test {
assert_eq!(fc.max_allowed, MAX_STREAMS_LIMIT);
}
+ #[test]
+ fn auto_tune_with_max_active_below_limit_sent() -> Res<()> {
+ let rtt = Duration::from_millis(40);
+ let now = test_fixture::now();
+ let mut fc =
+ ReceiverFlowControl::new(StreamId::new(0), to_u64(INITIAL_LOCAL_MAX_STREAM_DATA));
+
+ // Send first window update to give auto-tuning algorithm a baseline.
+ let consumed = fc.set_consumed(fc.next_limit())?;
+ fc.add_retired(consumed);
+ assert_eq!(write_frames(&mut fc, rtt, now), 1);
+ let max_allowed = fc.max_allowed;
+
+ // Lower max_active to less than the credit the peer holds, then lose the update.
+ let lowered_max_active = fc.max_active() / 2;
+ fc.set_max_active(lowered_max_active);
+ fc.frame_lost(max_allowed);
+
+ // The same limit is sent again, without auto-tuning.
+ assert_eq!(write_frames(&mut fc, rtt, now), 1);
+ assert_eq!(fc.max_allowed, max_allowed);
+ assert_eq!(fc.max_active(), lowered_max_active);
+
+ // The peer can use all of it.
+ fc.set_consumed(max_allowed)?;
+
+ Ok(())
+ }
+
#[test]
fn auto_tune_never_decreases_large_manually_set_max_active() -> Res<()> {
let rtt = Duration::from_millis(40);When fixing this, please carry this line into the commit message:
Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-GVV73C4E.
Metadata
Metadata
Assignees
Labels
Type
Fields
Priority
Activity