Skip to content

docs(adr): draft ADR-0023 — Phase 1 channel layer scope (single-exec) - #49

Merged
gonzafg2 merged 16 commits into
mainfrom
docs/adr-0023-channel-layer-scope
Jun 12, 2026
Merged

gonzafg2 merged 16 commits into
mainfrom
docs/adr-0023-channel-layer-scope

Conversation

@gonzafg2

Copy link
Copy Markdown
Owner

Summary

Adds ADR-0023, fulfilling the "ADR-TBD (scope of single-command execution)" placeholder that RFC-0003 §"Reference-level explanation" left open. It fixes the exact RFC 4254 subset Phase 1's channel module implements.

Decision in one line

One session channel per connection, carrying exactly one exec request, and nothing else.

In scope (Phase 1) Refused / deferred
session channel open (one at a time) second concurrent channel → OPEN_FAILURE
exec request pty-req, shell, subsystem, env, signal → CHANNEL_FAILURE (Phase 2)
exit-status before CLOSE exit-signal (Phase 2)
stdin + stdout + stderr (EXTENDED_DATA) global requests → REQUEST_FAILURE
real flow control (WINDOW_ADJUST) port/agent/X11 forwarding (off by default, §5.4)

The two non-obvious calls

  1. exit-status and stdin are in scope. Without exit-status the ADR-0020 interop gate can't verify ssh host cmd; echo $?; without stdin, … | ssh host cat breaks. Both are cheap and squarely inside "single-command execution".
  2. Flow control is real, not stubbed. Initial window 2 MiB, max packet 32 KiB — verified against OpenSSH channels.h (CHAN_SES_WINDOW_DEFAULT = 64 × CHAN_SES_PACKET_DEFAULT, CHAN_SES_PACKET_DEFAULT = 32*1024), so the interop client's assumptions hold. A stubbed window is a correctness bug a real client surfaces under load.

Type of change

  • Documentation only

Checklist

  • Commits signed off under the DCO.
  • Conventional Commits.
  • Documentation updated (this ADR is the change).

Security considerations

The channel message/request boundary is the list of things the post-auth attacker in threat-model §5.4 can reach. Refusing pty-req/shell/subsystem/forwarding at the protocol level (not just by convention) is what keeps the §5.4 surface closed in Phase 1. The child runs as the service-account UID per §8.12 (per-user UID isolation is the Phase 3 non-goal tracked in #43).

Notes for reviewers

Fulfils RFC-0003's deferred "ADR-TBD (scope of single-command
execution)". Phase 1 implements exactly one `session` channel per
connection carrying one `exec` request: eleven channel messages
(OPEN..FAILURE), exec-only requests, exit-status sent, and explicit
SSH_MSG_CHANNEL_FAILURE for pty-req/shell/subsystem/env/signal.

Settles the non-obvious bits the ADR-0020 interop gate needs: stdin
accepted, exit-status emitted before CLOSE, and real flow control
(2 MiB initial window / 32 KiB max packet, matching OpenSSH's
channels.h defaults so the interop client's assumptions hold). One
channel per connection; global requests refused; exit-signal and env
deferred to Phase 2. Child runs as the service-account UID per §8.12.

Signed-off-by: Gonzalo Fleming <[email protected]>
@gonzafg2 gonzafg2 added documentation Improvements or additions to documentation security Security-sensitive issue or change labels Jun 11, 2026
…c audit boundary

Review findings from #49, applied:

- A second exec on the same channel (child running or exited) gets
  SSH_MSG_CHANNEL_FAILURE — the channel's single exec is consumed by
  the first accepted request; previously only 'other request types'
  were specified.
- One channel per CONNECTION LIFETIME: a CHANNEL_OPEN after the first
  channel's close handshake also gets OPEN_FAILURE, preserving the
  'exactly one exec per connection' invariant end to end; previously
  the rule was scoped to 'while a session channel is live'.
- The exec acceptance point is now explicitly the audit boundary,
  binding exec.started/exec.finished with authenticated_identity and
  executing_uid as separate structured fields and command never
  interpolated (ADR-0024, TBD in review; CLAUDE.md hard rule).

The ADR-0021 links resolve on this branch (merged to main); the
ADR-0022 links resolve once #47 merges first (merge order:

Signed-off-by: Gonzalo Fleming <[email protected]>
#61 RFC-0004 -> #47 -> this PR -> #50).
gonzafg2 added 2 commits June 11, 2026 21:04
…connect, backpressure

Review finding from #49: 'flow control is real' covered outbound
correctness but not the receiver-side policy, which is the
security-relevant half. Now binding: per-channel buffered memory is
bounded by the advertised inbound window; a peer exceeding it
violates RFC 4254 5.2 and gets SSH_MSG_DISCONNECT
(SSH_DISCONNECT_PROTOCOL_ERROR) rather than over-buffering; inbound
WINDOW_ADJUST is granted only as the child's stdin consumes data, so
a stalled child backpressures the wire instead of growing a queue.

Signed-off-by: Gonzalo Fleming <[email protected]>
…stall semantics

Four review findings from #49, applied:

- Inbound EXTENDED_DATA rejected: RFC 4254 defines no client-to-server
  extended-data type, so message 95 inbound terminates the connection
  (PROTOCOL_ERROR) — no accept-and-branch path.
- Inbound DATA before exec is buffered within the window (legitimate
  stdin pipelining) and delivered when the child starts; discarded if
  the channel closes without an exec.
- DATA after the client's EOF violates RFC 4254 5.3 -> disconnect;
  client CLOSE with a live child (no prior EOF, RFC-permitted) closes
  stdin, terminates the child, replies CLOSE, frees the channel,
  omits exit-status — no orphaned child survives its channel.
- The stdin-ignoring-child window stall is documented as intentional
  backpressure (draining unread stdin would silently discard data and
  unbound the memory the window bounds); messages for unknown channel
  numbers fail closed with PROTOCOL_ERROR.

Signed-off-by: Gonzalo Fleming <[email protected]>
Review finding from #49: RFC 4254 5.3 lets the two CHANNEL_CLOSEs
cross in flight, so frames arriving after the server's CLOSE but
before the client's are legitimate — yet the child has exited and
there is no sink. Decision: accept and silently discard channel
messages during that window; free the channel on the client's CLOSE;
messages addressed to the freed channel number after that fall under
the unknown-channel rule and terminate the connection.

Signed-off-by: Gonzalo Fleming <[email protected]>
…arly close

Three review findings from #49, applied:

- RFC 4254 5.1 distinguishes the codes: non-session types (no Phase 1
  handler) get SSH_OPEN_UNKNOWN_CHANNEL_TYPE (3); a second session —
  understood but policy-refused — keeps ADMINISTRATIVELY_PROHIBITED
  (1). One code for both cases was a conformance bug.
- exec.finished is emitted on every path that produced exec.started,
  including client-initiated early close (with the terminated child's
  conventional non-zero exit status) — omitting the wire exit-status
  never skips the audit record.
- The audit-boundary sentence no longer cites TBD ADR-0024 as the
  normative authority ('per'): the binding authority is CLAUDE.md's
  hard rule; ADR-0024 (in review) fixes the full schema.

Signed-off-by: Gonzalo Fleming <[email protected]>
@gonzafg2
gonzafg2 merged commit dc6e28d into main Jun 12, 2026
9 checks passed
Comment on lines +79 to +80

Implement the full multiplexing model now, so Phase 2 features slot in without revisiting channel bookkeeping. Rejected: concurrent channels bring channel-id allocation, per-channel window state, and interleaving concerns that are pure cost for a single-exec skeleton. The transport's type-state design (ADR-0021) makes adding the second channel in Phase 2 a localised change; paying for it now violates YAGNI and MANIFIESTO #4.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong cross-reference — type-state design is RFC-0003, not ADR-0021

The citation (ADR-0021) here points at the negotiation profile ADR (KEXINIT name-lists, cipher preferences, strict-kex). ADR-0021 does not define or motivate the Expect<Stage> type-state transport machine; that design is in RFC-0003 (§"Why type-state for the transport state machine"). CLAUDE.md itself anchors the rule to RFC-0003 explicitly:

"The transport is a type-state machine ([RFC-0003]): an Expect<Stage> exposes only the messages valid in that stage."

A reader following the citation to ADR-0021 will not find the type-state rationale the sentence claims. This matters for auditability: ADRs exist to preserve the decision trail, and a wrong citation breaks that trail.

Suggested change
Implement the full multiplexing model now, so Phase 2 features slot in without revisiting channel bookkeeping. Rejected: concurrent channels bring channel-id allocation, per-channel window state, and interleaving concerns that are pure cost for a single-exec skeleton. The transport's type-state design (ADR-0021) makes adding the second channel in Phase 2 a localised change; paying for it now violates YAGNI and MANIFIESTO #4.
Implement the full multiplexing model now, so Phase 2 features slot in without revisiting channel bookkeeping. Rejected: concurrent channels bring channel-id allocation, per-channel window state, and interleaving concerns that are pure cost for a single-exec skeleton. The transport's type-state design ([RFC-0003](../rfcs/0003-phase-1-ssh-stack-greenfield-vs-russh.md)) makes adding the second channel in Phase 2 a localised change; paying for it now violates YAGNI and MANIFIESTO #4.

Comment on lines +27 to +28
| `SSH_MSG_CHANNEL_EXTENDED_DATA` | 95 | Child stderr streamed to the client with `data_type_code == SSH_EXTENDED_DATA_STDERR` (1). **Inbound** extended data is rejected: RFC 4254 defines no client→server extended-data type, so receiving message 95 terminates the connection (`SSH_DISCONNECT_PROTOCOL_ERROR`). |
| `SSH_MSG_CHANNEL_EOF` | 96 | Sent when the child's output is fully drained; accepted from the client (closes the child's stdin). Inbound `CHANNEL_DATA` **after** the client's EOF violates RFC 4254 §5.3 and terminates the connection (`SSH_DISCONNECT_PROTOCOL_ERROR`). |

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RFC §5.3 citation is inaccurate — this is a stricter-than-spec policy, not an RFC violation

RFC 4254 §5.3 describes SSH_MSG_CHANNEL_EOF as a SHOULD-level signal ("a party will no longer send more data") and explicitly states "the channel remains open after this message, and more data may still be sent in the other direction." The RFC contains no MUST NOT for post-EOF data and specifies no consequence (no PROTOCOL_ERROR, no disconnect) if a peer sends data afterward.

Calling this a "violation of RFC 4254 §5.3" is therefore inaccurate — §5.3 has no such prohibition to violate. Contrast with the inbound window-overrun in the SSH_MSG_CHANNEL_DATA row (line 44), where the RFC genuinely says MUST NOT send more data than the window allows and the cite is correct.

The behavior (disconnecting) is a perfectly valid fail-closed policy choice — it's consistent with the type-state posture and the project's approach to every other out-of-spec input. The fix is to reframe it as deliberate policy rather than an RFC mandate.

Suggested change
| `SSH_MSG_CHANNEL_EXTENDED_DATA` | 95 | Child stderr streamed to the client with `data_type_code == SSH_EXTENDED_DATA_STDERR` (1). **Inbound** extended data is rejected: RFC 4254 defines no client→server extended-data type, so receiving message 95 terminates the connection (`SSH_DISCONNECT_PROTOCOL_ERROR`). |
| `SSH_MSG_CHANNEL_EOF` | 96 | Sent when the child's output is fully drained; accepted from the client (closes the child's stdin). Inbound `CHANNEL_DATA` **after** the client's EOF violates RFC 4254 §5.3 and terminates the connection (`SSH_DISCONNECT_PROTOCOL_ERROR`). |
| `SSH_MSG_CHANNEL_EOF` | 96 | Sent when the child's output is fully drained; accepted from the client (closes the child's stdin). Inbound `CHANNEL_DATA` **after** the client's EOF is rejected as a deliberate fail-closed policy (the client has declared it will send no more data; the server holds the peer to that commitment) and terminates the connection (`SSH_DISCONNECT_PROTOCOL_ERROR`). RFC 4254 §5.3 does not mandate this — it is stricter-than-spec by design. |

Comment on lines +37 to +38
- **`exit-status` is sent** as a `SSH_MSG_CHANNEL_REQUEST` (`want_reply = false`) carrying the child's exit code before `SSH_MSG_CHANNEL_CLOSE`. This is required for `ssh host cmd; echo $?` to report correctly and is asserted by the ADR-0020 interop gate.
- **All other requests are refused** with `SSH_MSG_CHANNEL_FAILURE` when they set `want_reply`, and ignored otherwise. This explicitly includes `pty-req` (Phase 2), `shell` (Phase 2), `subsystem` (SFTP is Phase 2), `env`, `signal`, and `window-change`. Refusing `pty-req` and `shell` is what makes "single-command execution, no interactive shell" true at the protocol level.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential "fail closed" gap: want_reply=false channel requests are silently swallowed with no rate limit or termination

The ADR specifies that unknown channel requests with want_reply=false are "ignored otherwise" — no response, no connection termination. OpenSSH sends env this way by default, and the ADR acknowledges this on line 59 ("ignored when want_reply is false, as OpenSSH sends it").

The concern is that want_reply=false is set by the client, not negotiated. A malicious peer could stream arbitrarily many SSH_MSG_CHANNEL_REQUEST packets (any unknown request type, want_reply=false) and receive zero pushback — unbounded state-machine activity with no cost to the attacker and no terminating response from the server.

This sits in mild tension with two CLAUDE.md principles:

  • "Fail closed" — "no downgrade path may exist" and the general posture that unknown/unexpected input produces a hard failure, not silent acceptance. Every other boundary violation in this ADR (unknown channel number, window overflow, post-EOF data, second CHANNEL_OPEN) terminates the connection; this one does not.
  • Loosening the type-state model — "do not loosen the type-state transport machine to accept-and-branch"; silently absorbing semantically-meaningful channel-request types with no cost is the weakest form of that pattern.

Suggested resolution (either):

  • (a) Prefer: treat any SSH_MSG_CHANNEL_REQUEST with an unrecognised type as a protocol error and terminate (SSH_DISCONNECT_PROTOCOL_ERROR) regardless of want_reply, consistent with how all other unexpected messages in the ADR are handled. Real clients only send env and signal this way, and both are out of Phase 1 scope — a legitimate client will not break.
  • (b) Accept the current behavior but add an explicit per-channel cap (e.g., refuse the channel after N unrecognised requests) so the attacker's path is bounded. Record the cap value here so it is a normative part of the ADR.

CLAUDE.md – Fail closed rule

@gonzafg2
gonzafg2 deleted the docs/adr-0023-channel-layer-scope branch July 18, 2026 04:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation security Security-sensitive issue or change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant