Skip to content

fix(command): close the channel on an oversized declared frame length instead of wedging the session - #1438

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
fix/channel-oversized-delimiter-resync
Sep 22, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
fix/channel-oversized-delimiter-resync

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

try_read_delimited_message rejected an oversized declared length with
ChannelError::MessageTooLarge but left the eight-byte header at the head of
front_buf and moved no readiness bit, unlike the sibling
MessageLengthUnderDelimiter branch twelve lines below, which consumes the
bogus delimiter precisely so the channel can re-sync.

Nothing downstream compensated: read_message_nonblocking propagated the
error with ? and no log, extract_messages discarded it with a bare
Err(_), and ClientSession::ready / WorkerSession::ready close only on
readiness.is_error() || is_hup(), which no parse error sets. The session
stayed registered for the life of the process, re-parsing the same header on
every later read, pinning its file descriptor and up to
max_command_buffer_size of buffer, with nothing logged.

The recovery is deliberately not the sibling's consume-and-re-sync. An
oversized length can never be satisfied by a buffer bounded at
max_buffer_size, and the bytes behind the header may be payload rather than
a fresh delimiter, so dropping the header and re-framing on them would decode
peer-chosen bytes as a control-plane request. It is now fatal for that peer:
logged, then readiness.insert(Ready::ERROR), which is the stance the write
side already took for the identical condition (is_transient_overflow) and
reuses the existing close path. The bit is inserted rather than assigned
because on a worker's own channel interest holds only READABLE and WRITABLE,
so readiness() masks ERROR away and nothing there reads is_error();
assigning would only clear WRITABLE and stall Server::send_queue until the
next edge-triggered writability event.

Closing is not a verdict on the peer. The two ends size their channels from
their own max_command_buffer_size, so a peer configured higher emits such a
frame through the ordinary write path while conforming perfectly: the two
configurations shipped here already disagree tenfold, bin/config.toml at
163_840 against os-build/config.toml's 1_638_400, so a sozu ctl --config
pointed at one while the supervisor was started from the other overruns it on
a large LoadState. Two binaries built with different SOZU_CONFIG defaults
(bin/build.rs, bin/src/util.rs) diverge the same way; without --config,
sozu ctl uses the configuration path baked in at build time and fails
outright when none was, so it never falls back to a compiled buffer default.
The receiving end still cannot complete or re-sync the frame, so it still
closes -- before this change that connection wedged silently, which is worse
-- but the error! line now names max_command_buffer_size and
command_buffer_size on both ends instead of implying hostility.

The branch's own comment claimed the rejection made "the read loop disconnect
cleanly"; that half was false and is corrected, as is the same claim in
doc/configure_admin_ops.md section 5.2. That section also said a dropped
worker "observes the close as an EOF on its channel": it observes nothing.
handle_worker_close calls close_worker, which sends SIGKILL, and
workers_to_spawn replaces the process only when worker_automatic_restart
is set.

The same return-before-consume shape sat six lines below, on Rx::decode: a
valid declared length with an undecodable payload -- eight good length bytes
plus garbage, or protobuf skew between two builds -- wedged the session
identically. That one now consumes and re-syncs instead of closing. Not
because the boundary is proven: message_len is only range-checked, so
consuming it can re-frame on bytes the sender never framed. Because the
decode-success path four lines down already consumes that same peer-supplied
length and re-frames on whatever follows, so the failure path now re-frames on
exactly the bytes the success path already did, and closing only on the
failing half would punish the case more likely to be honest protobuf skew. Its
regression test is a channel-level one: extract_messages returns on the
first Err(_) with the capacity unchanged, so a valid frame already pipelined
behind the malformed one reaches the session only on its next readable event,
and that residual is deliberately untested and unfixed here.

The third instance, the BufferFull arm at the ceiling, is known-remaining
and deliberately untouched, filed as #1436: its correct recovery is a
Buffer::shift that makes room for a frame which can still complete, not a
close, so closing there would drop a conforming peer over an internal
buffer-management detail.

Reachability: the command socket is a unix socket chmod'd 0o600 and there is
no control-plane TCP listener, so this is a robustness fix rather than a
remotely triggerable one; the same parser also serves the master/worker
socketpair, where a worker compromised through the traffic it proxies could
wedge its supervisor session.

Closes #1428

Signed-off-by: Florentin Dubois [email protected]

@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the fix/channel-oversized-delimiter-resync branch from 68d2abf to 917e0fe Compare September 22, 2026 11:09
…ceiling

`try_read_delimited_message` rejected an oversized declared length with
`ChannelError::MessageTooLarge` but left the eight-byte header at the head of
`front_buf` and moved no readiness bit, unlike the sibling
`MessageLengthUnderDelimiter` branch twelve lines below, which consumes the
bogus delimiter precisely so the channel can re-sync.

Nothing downstream compensated: `read_message_nonblocking` propagated the
error with `?` and no log, `extract_messages` discarded it with a bare
`Err(_)`, and `ClientSession::ready` / `WorkerSession::ready` close only on
`readiness.is_error() || is_hup()`, which no parse error sets. The session
stayed registered for the life of the process, re-parsing the same header on
every later read, pinning its file descriptor and up to
`max_command_buffer_size` of buffer, with nothing logged.

The recovery is deliberately not the sibling's consume-and-re-sync. An
oversized length can never be satisfied by a buffer bounded at
`max_buffer_size`, and the bytes behind the header may be payload rather than
a fresh delimiter, so dropping the header and re-framing on them would decode
peer-chosen bytes as a control-plane request. It is now fatal for that peer:
logged, then `readiness.insert(Ready::ERROR)`, which is the stance the write
side already took for the identical condition (`is_transient_overflow`) and
reuses the existing close path. The bit is inserted rather than assigned
because on a worker's own channel `interest` holds only READABLE and WRITABLE,
so `readiness()` masks ERROR away and nothing there reads `is_error()`;
assigning would only clear WRITABLE and stall `Server::send_queue` until the
next edge-triggered writability event.

Closing is not a verdict on the peer. The two ends size their channels from
their own `max_command_buffer_size`, so a peer configured higher emits such a
frame through the ordinary write path while conforming perfectly: the two
configurations shipped here already disagree tenfold, `bin/config.toml` at
163_840 against `os-build/config.toml`'s 1_638_400, so a `sozu ctl --config`
pointed at one while the supervisor was started from the other overruns it on
a large `LoadState`. Two binaries built with different `SOZU_CONFIG` defaults
(`bin/build.rs`, `bin/src/util.rs`) diverge the same way; without `--config`,
`sozu ctl` uses the configuration path baked in at build time and fails
outright when none was, so it never falls back to a compiled buffer default.
The receiving end still cannot complete or re-sync the frame, so it still
closes -- before this change that connection wedged silently, which is worse
-- but the `error!` line now names `max_command_buffer_size` and
`command_buffer_size` on both ends instead of implying hostility.

The branch's own comment claimed the rejection made "the read loop disconnect
cleanly"; that half was false and is corrected, as is the same claim in
doc/configure_admin_ops.md section 5.2. That section also said a dropped
worker "observes the close as an EOF on its channel": it observes nothing.
`handle_worker_close` calls `close_worker`, which sends SIGKILL, and
`workers_to_spawn` replaces the process only when `worker_automatic_restart`
is set.

The same return-before-`consume` shape sat six lines below, on `Rx::decode`: a
valid declared length with an undecodable payload -- eight good length bytes
plus garbage, or protobuf skew between two builds -- wedged the session
identically. That one now consumes and re-syncs instead of closing. Not
because the boundary is proven: `message_len` is only range-checked, so
consuming it can re-frame on bytes the sender never framed. Because the
decode-success path four lines down already consumes that same peer-supplied
length and re-frames on whatever follows, so the failure path now re-frames on
exactly the bytes the success path already did, and closing only on the
failing half would punish the case more likely to be honest protobuf skew. Its
regression test is a channel-level one: `extract_messages` returns on the
first `Err(_)` with the capacity unchanged, so a valid frame already pipelined
behind the malformed one reaches the session only on its next readable event,
and that residual is deliberately untested and unfixed here.

The third instance, the `BufferFull` arm at the ceiling, is known-remaining
and deliberately untouched, filed as #1436: its correct recovery is a
`Buffer::shift` that makes room for a frame which can still complete, not a
close, so closing there would drop a conforming peer over an internal
buffer-management detail.

Reachability: the command socket is a unix socket chmod'd 0o600 and there is
no control-plane TCP listener, so this is a robustness fix rather than a
remotely triggerable one; the same parser also serves the master/worker
socketpair, where a worker compromised through the traffic it proxies could
wedge its supervisor session.

Closes #1428

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the fix/channel-oversized-delimiter-resync branch from 917e0fe to a7fe5cd Compare September 22, 2026 12:11
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 50ee52f into main Sep 22, 2026
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant