From 2cf26e52791315844596adddbfc953e8c2030daf Mon Sep 17 00:00:00 2001 From: Damian Peckett Date: Sat, 28 Nov 2026 16:58:58 +0100 Subject: [PATCH] async: don't reply to a channel EOF with our own EOF Receiving CHANNEL_EOF made sunset send its own EOF right away. EOF only ends the peer's direction (RFC 4254 5.3), so anything the application wrote after reading to EOF was sent after our EOF: OpenSSH discards it. That loses the output of `ssh host cmd < /dev/null`, `cat f | ssh host cmd`, or any command that reads stdin to EOF before answering. Writes were also still accepted after our EOF, putting data on the wire that the peer must ignore. Since f3fd0a5 closing a channel or dropping all its handles sends EOF and Close, so channels still terminate without the automatic reply. --- async/src/async_channel.rs | 28 +++++++++++++++++++++++ async/src/async_sunset.rs | 23 +++++++++++++++++++ async/tests/channel_close.rs | 41 +++++++++++++++++++++++++++++++++ src/channel.rs | 44 ++++++++++++++++++++++-------------- src/runner.rs | 20 +++++++++++++--- 5 files changed, 136 insertions(+), 20 deletions(-) diff --git a/async/src/async_channel.rs b/async/src/async_channel.rs index f8d9d158..61d7f13c 100644 --- a/async/src/async_channel.rs +++ b/async/src/async_channel.rs @@ -56,6 +56,10 @@ impl ChanIO<'_> { poll_fn(|cx| self.sunset.poll_term_window_change(cx, self.num, &winch)).await } + pub async fn send_eof(&self) -> Result<()> { + poll_fn(|cx| self.sunset.poll_send_eof(cx, self.num)).await + } + pub async fn close_channel(&self) -> Result<()> { poll_fn(|cx| self.sunset.poll_close_channel(cx, self.num)).await } @@ -200,6 +204,18 @@ impl<'g> ChanOut<'g> { self.0.term_window_change(winch).await } + /// Send EOF, leaving the channel open for reading. + /// + /// Further non-empty writes (including stderr) fail with + /// `sunset::Error::ChannelEOF`. + /// Repeated calls have no effect. + /// + /// Do not overlap with writes or channel-control operations on the + /// same channel, as this shares the normal write waker. + pub async fn send_eof(&self) -> Result<()> { + self.0.send_eof().await + } + /// Close the channel. /// /// Sends EOF and Close, further writes will fail. @@ -277,6 +293,18 @@ impl<'g> ChanInOut<'g> { self.0.term_window_change(winch).await } + /// Send EOF, leaving the channel open for reading. + /// + /// Further non-empty writes (including stderr) fail with + /// `sunset::Error::ChannelEOF`. + /// Repeated calls have no effect. + /// + /// Do not overlap with writes or channel-control operations on the + /// same channel, as this shares the normal write waker. + pub async fn send_eof(&self) -> Result<()> { + self.0.send_eof().await + } + /// Close the channel. /// /// Sends EOF and Close, further writes will fail. diff --git a/async/src/async_sunset.rs b/async/src/async_sunset.rs index c0f1db9f..12ae74ed 100644 --- a/async/src/async_sunset.rs +++ b/async/src/async_sunset.rs @@ -507,6 +507,8 @@ pub(crate) trait ChanCore: MaybeSend { buf: &[u8], ) -> Poll>; + fn poll_send_eof(&self, cx: &mut Context, num: ChanNum) -> Poll>; + // Client only fn poll_term_window_change( &self, @@ -671,6 +673,27 @@ impl<'a, CS: CliServ> ChanCore for AsyncSunset<'a, CS> { Poll::Ready(runner.term_window_change(h, winch)) } + fn poll_send_eof(&self, cx: &mut Context, num: ChanNum) -> Poll> { + // Attempt to lock .inner + let i = self.inner.lock(); + let i = pin!(i); + let Ready(mut inner) = i.poll(cx) else { + return Pending; + }; + let (runner, h) = inner.fetch(num)?; + match runner.channel_send_eof(h) { + Err(sunset::Error::BusySend { .. }) => { + // Shares the normal write waker, like poll_close_channel(). + runner.set_channel_write_waker(h, Normal, cx.waker()); + Poll::Pending + } + r => { + self.wake_progress(); + Poll::Ready(r) + } + } + } + fn poll_close_channel( &self, cx: &mut Context, diff --git a/async/tests/channel_close.rs b/async/tests/channel_close.rs index 10c3e206..7bd240df 100644 --- a/async/tests/channel_close.rs +++ b/async/tests/channel_close.rs @@ -161,3 +161,44 @@ async fn close_channel_with_handle() { }; run(&server, &client, shells, authed, opened, body).await; } + +/// EOF only ends the sending direction; a reply can still be received. +#[tokio::test] +async fn reply_after_peer_eof() { + let server = SSHServer::new_owned(); + let client = SSHClient::new_owned(); + let (shells, mut shell) = unbounded_channel(); + let (authed, mut authed_rx) = unbounded_channel(); + let (opened, mut opened_rx) = unbounded_channel(); + let (ready, ready_rx) = tokio::sync::oneshot::channel(); + + let server_session = async { + let (mut io, mut ext) = + server.stdio_stderr(shell.recv().await.unwrap()).await.unwrap(); + ready.send(()).unwrap(); + assert_eq!(read_to_eof(&mut io).await, b"request"); + io.write_all(b"reply").await.unwrap(); + ext.send_eof().await.unwrap(); + assert!(matches!(io.write(b"more").await, Err(sunset::Error::ChannelEOF))); + assert!(matches!(ext.write(b"more").await, Err(sunset::Error::ChannelEOF))); + // Dropping the handles still closes the channel. + }; + + let client_session = async { + authed_rx.recv().await.unwrap(); + let (mut io, _ext) = client.open_session_nopty().await.unwrap(); + opened_rx.recv().await.unwrap(); + ready_rx.await.unwrap(); + io.write_all(b"request").await.unwrap(); + io.send_eof().await.unwrap(); + io.send_eof().await.unwrap(); + assert!(matches!(io.write(b"more").await, Err(sunset::Error::ChannelEOF))); + assert_eq!(read_to_eof(&mut io).await, b"reply"); + io.until_closed().await.unwrap(); + }; + + let body = async { + tokio::join!(server_session, client_session); + }; + run(&server, &client, shells, authed, opened, body).await; +} diff --git a/src/channel.rs b/src/channel.rs index f3ca3bc4..f9c27c59 100644 --- a/src/channel.rs +++ b/src/channel.rs @@ -118,6 +118,11 @@ impl Channels { self.get_mut(num)?.send_close(s) } + /// Send EOF if not sent yet. The channel stays open for reading. + pub fn send_eof(&mut self, num: ChanNum, s: &mut TrafSend) -> Result<()> { + self.get_mut(num)?.send_eof(s) + } + fn remove_any(&mut self, num: ChanNum) -> Result<()> { trace!("remove_any channel {}", num); self.ch[num.0 as usize] = None; @@ -505,7 +510,7 @@ impl Channels { } Packet::ChannelEof(p) => { let ch = self.get_mut(ChanNum(p.num))?; - ch.handle_eof(s, is_client)?; + ch.handle_eof(is_client); } Packet::ChannelClose(p) => { let is_client = self.is_client; @@ -1042,22 +1047,16 @@ impl Channel { } } - fn handle_eof(&mut self, s: &mut TrafSend, is_client: bool) -> Result<()> { - //TODO: check existing state? - if !self.sent_eof { - s.send(packets::ChannelEof { num: self.send_num()? })?; - self.sent_eof = true; - } + fn handle_eof(&mut self, is_client: bool) { + // EOF only ends the peer's sending direction (RFC 4254 5.3). + + self.state = ChanState::RecvEof; // Wake readers on EOF self.wake_read(ChanData::Normal, is_client); if is_client { self.wake_read(ChanData::Stderr, is_client); } - - self.state = ChanState::RecvEof; - // todo!(); - Ok(()) } fn handle_close(&mut self, s: &mut TrafSend, is_client: bool) -> Result<()> { @@ -1090,9 +1089,9 @@ impl Channel { matches!(self.state, ChanState::RecvClose) } - // None on close + // None after EOF or close was sent fn send_allowed(&self) -> Option { - if self.sent_close { + if self.sent_eof || self.sent_close { return None; } let r = self.send.as_ref().map(|s| usize::min(s.window, s.max_packet)); @@ -1100,6 +1099,20 @@ impl Channel { r } + /// Send EOF if required. + fn send_eof(&mut self, s: &mut TrafSend) -> Result<()> { + if self.sent_eof || self.sent_close { + return Ok(()); + } + let Ok(num) = self.send_num() else { + // not open yet + return Ok(()); + }; + s.send(packets::ChannelEof { num })?; + self.sent_eof = true; + Ok(()) + } + /// Send EOF and Close if required. fn send_close(&mut self, s: &mut TrafSend) -> Result<()> { if self.sent_close { @@ -1110,10 +1123,7 @@ impl Channel { return Ok(()); }; - if !self.sent_eof { - s.send(packets::ChannelEof { num })?; - self.sent_eof = true; - } + self.send_eof(s)?; s.send(packets::ChannelClose { num })?; self.sent_close = true; Ok(()) diff --git a/src/runner.rs b/src/runner.rs index b172d092..6480aa2f 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -505,7 +505,7 @@ impl<'a, CS: CliServ> Runner<'a, CS> { /// Send data from this application out the wire. /// - /// Returns `Ok(len)` consumed, `Err(Error::ChannelEof)` on EOF, + /// Returns `Ok(len)` consumed, `Err(Error::ChannelEOF)` on EOF, /// or other errors. pub fn write_channel( &mut self, @@ -540,7 +540,7 @@ impl<'a, CS: CliServ> Runner<'a, CS> { /// Receive data coming from the wire into this application. /// - /// Returns `Ok(len)` received, `Err(Error::ChannelEof)` on EOF, + /// Returns `Ok(len)` received, `Err(Error::ChannelEOF)` on EOF, /// or other errors. Ok(0) indicates no data available, ie pending. /// TODO: EOF is unimplemented pub fn read_channel( @@ -621,7 +621,7 @@ impl<'a, CS: CliServ> Runner<'a, CS> { /// Returns the maximum data that may be sent to a channel /// - /// Returns `Ok(None)` on channel closed. + /// Returns `Ok(None)` when no more data can be sent, including after EOF. /// /// May fail with `BadChannelData` if dt is invalid for this session. pub fn write_channel_ready( @@ -694,6 +694,20 @@ impl<'a, CS: CliServ> Runner<'a, CS> { Ok(()) } + /// Send EOF, leaving the channel open for reading. + /// + /// Further non-empty writes (including stderr) fail with `Error::ChannelEOF`. + /// Does nothing if EOF or Close was already sent. + /// + /// Independent of the channel send window. On `BusySend`, retry after + /// transport progress. + pub fn channel_send_eof(&mut self, chan: &ChanHandle) -> Result<()> { + let mut s = self.traf_out.sender(&mut self.keys); + self.conn.channels.send_eof(chan.0, &mut s)?; + self.wake(); + Ok(()) + } + /// Must be called when an application has finished with a channel. /// /// Closes the channel if it hasn't been already.