xref: /openbsd/usr.bin/ssh/nchan.c (revision 116a8944)
1 /* $OpenBSD: nchan.c,v 1.76 2024/07/25 22:40:08 djm Exp $ */
2 /*
3  * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <sys/queue.h>
29 
30 #include <errno.h>
31 #include <string.h>
32 #include <stdarg.h>
33 
34 #include "ssh2.h"
35 #include "sshbuf.h"
36 #include "ssherr.h"
37 #include "packet.h"
38 #include "channels.h"
39 #include "compat.h"
40 #include "log.h"
41 
42 /*
43  * SSH Protocol 1.5 aka New Channel Protocol
44  * Thanks to Martina, Axel and everyone who left Erlangen, leaving me bored.
45  * Written by Markus Friedl in October 1999
46  *
47  * Protocol versions 1.3 and 1.5 differ in the handshake protocol used for the
48  * tear down of channels:
49  *
50  * 1.3:	strict request-ack-protocol:
51  *	CLOSE	->
52  *		<-  CLOSE_CONFIRM
53  *
54  * 1.5:	uses variations of:
55  *	IEOF	->
56  *		<-  OCLOSE
57  *		<-  IEOF
58  *	OCLOSE	->
59  *	i.e. both sides have to close the channel
60  *
61  * 2.0: the EOF messages are optional
62  *
63  * See the debugging output from 'ssh -v' and 'sshd -d' of
64  * ssh-1.2.27 as an example.
65  *
66  */
67 
68 /* functions manipulating channel states */
69 /*
70  * EVENTS update channel input/output states execute ACTIONS
71  */
72 /*
73  * ACTIONS: should never update the channel states
74  */
75 static void	chan_send_eof2(struct ssh *, Channel *);
76 static void	chan_send_eow2(struct ssh *, Channel *);
77 
78 /* helper */
79 static void	chan_shutdown_write(struct ssh *, Channel *);
80 static void	chan_shutdown_read(struct ssh *, Channel *);
81 static void	chan_shutdown_extended_read(struct ssh *, Channel *);
82 
83 static const char * const ostates[] = {
84 	"open", "drain", "wait_ieof", "closed",
85 };
86 static const char * const istates[] = {
87 	"open", "drain", "wait_oclose", "closed",
88 };
89 
90 static void
chan_set_istate(Channel * c,u_int next)91 chan_set_istate(Channel *c, u_int next)
92 {
93 	if (c->istate > CHAN_INPUT_CLOSED || next > CHAN_INPUT_CLOSED)
94 		fatal("chan_set_istate: bad state %d -> %d", c->istate, next);
95 	debug2("channel %d: input %s -> %s", c->self, istates[c->istate],
96 	    istates[next]);
97 	c->istate = next;
98 }
99 
100 static void
chan_set_ostate(Channel * c,u_int next)101 chan_set_ostate(Channel *c, u_int next)
102 {
103 	if (c->ostate > CHAN_OUTPUT_CLOSED || next > CHAN_OUTPUT_CLOSED)
104 		fatal("chan_set_ostate: bad state %d -> %d", c->ostate, next);
105 	debug2("channel %d: output %s -> %s", c->self, ostates[c->ostate],
106 	    ostates[next]);
107 	c->ostate = next;
108 }
109 
110 void
chan_read_failed(struct ssh * ssh,Channel * c)111 chan_read_failed(struct ssh *ssh, Channel *c)
112 {
113 	debug2("channel %d: read failed", c->self);
114 	switch (c->istate) {
115 	case CHAN_INPUT_OPEN:
116 		chan_shutdown_read(ssh, c);
117 		chan_set_istate(c, CHAN_INPUT_WAIT_DRAIN);
118 		break;
119 	default:
120 		error("channel %d: chan_read_failed for istate %d",
121 		    c->self, c->istate);
122 		break;
123 	}
124 }
125 
126 void
chan_ibuf_empty(struct ssh * ssh,Channel * c)127 chan_ibuf_empty(struct ssh *ssh, Channel *c)
128 {
129 	debug2("channel %d: ibuf empty", c->self);
130 	if (sshbuf_len(c->input)) {
131 		error("channel %d: chan_ibuf_empty for non empty buffer",
132 		    c->self);
133 		return;
134 	}
135 	switch (c->istate) {
136 	case CHAN_INPUT_WAIT_DRAIN:
137 		if (!(c->flags & (CHAN_CLOSE_SENT|CHAN_LOCAL)))
138 			chan_send_eof2(ssh, c);
139 		chan_set_istate(c, CHAN_INPUT_CLOSED);
140 		break;
141 	default:
142 		error("channel %d: chan_ibuf_empty for istate %d",
143 		    c->self, c->istate);
144 		break;
145 	}
146 }
147 
148 void
chan_obuf_empty(struct ssh * ssh,Channel * c)149 chan_obuf_empty(struct ssh *ssh, Channel *c)
150 {
151 	debug2("channel %d: obuf empty", c->self);
152 	if (sshbuf_len(c->output)) {
153 		error("channel %d: chan_obuf_empty for non empty buffer",
154 		    c->self);
155 		return;
156 	}
157 	switch (c->ostate) {
158 	case CHAN_OUTPUT_WAIT_DRAIN:
159 		chan_shutdown_write(ssh, c);
160 		chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
161 		break;
162 	default:
163 		error("channel %d: internal error: obuf_empty for ostate %d",
164 		    c->self, c->ostate);
165 		break;
166 	}
167 }
168 
169 void
chan_rcvd_eow(struct ssh * ssh,Channel * c)170 chan_rcvd_eow(struct ssh *ssh, Channel *c)
171 {
172 	debug2("channel %d: rcvd eow", c->self);
173 	switch (c->istate) {
174 	case CHAN_INPUT_OPEN:
175 		chan_shutdown_read(ssh, c);
176 		chan_set_istate(c, CHAN_INPUT_CLOSED);
177 		break;
178 	}
179 }
180 
181 static void
chan_send_eof2(struct ssh * ssh,Channel * c)182 chan_send_eof2(struct ssh *ssh, Channel *c)
183 {
184 	int r;
185 
186 	debug2("channel %d: send eof", c->self);
187 	switch (c->istate) {
188 	case CHAN_INPUT_WAIT_DRAIN:
189 		if (!c->have_remote_id)
190 			fatal_f("channel %d: no remote_id", c->self);
191 		if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_EOF)) != 0 ||
192 		    (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
193 		    (r = sshpkt_send(ssh)) != 0)
194 			fatal_fr(r, "send CHANNEL_EOF");
195 		c->flags |= CHAN_EOF_SENT;
196 		break;
197 	default:
198 		error("channel %d: cannot send eof for istate %d",
199 		    c->self, c->istate);
200 		break;
201 	}
202 }
203 
204 static void
chan_send_close2(struct ssh * ssh,Channel * c)205 chan_send_close2(struct ssh *ssh, Channel *c)
206 {
207 	int r;
208 
209 	debug2("channel %d: send_close2", c->self);
210 	if (c->ostate != CHAN_OUTPUT_CLOSED ||
211 	    c->istate != CHAN_INPUT_CLOSED) {
212 		error("channel %d: cannot send close for istate/ostate %d/%d",
213 		    c->self, c->istate, c->ostate);
214 	} else if (c->flags & CHAN_CLOSE_SENT) {
215 		error("channel %d: already sent close", c->self);
216 	} else {
217 		if (!c->have_remote_id)
218 			fatal_f("channel %d: no remote_id", c->self);
219 		debug2("channel %d: send close for remote id %u", c->self,
220 		    c->remote_id);
221 		if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_CLOSE)) != 0 ||
222 		    (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
223 		    (r = sshpkt_send(ssh)) != 0)
224 			fatal_fr(r, "send CHANNEL_EOF");
225 		c->flags |= CHAN_CLOSE_SENT;
226 	}
227 }
228 
229 static void
chan_send_eow2(struct ssh * ssh,Channel * c)230 chan_send_eow2(struct ssh *ssh, Channel *c)
231 {
232 	int r;
233 
234 	debug2("channel %d: send eow", c->self);
235 	if (c->ostate == CHAN_OUTPUT_CLOSED) {
236 		error("channel %d: must not sent eow on closed output",
237 		    c->self);
238 		return;
239 	}
240 	if (!(ssh->compat & SSH_NEW_OPENSSH))
241 		return;
242 	if (!c->have_remote_id)
243 		fatal_f("channel %d: no remote_id", c->self);
244 	if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_REQUEST)) != 0 ||
245 	    (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
246 	    (r = sshpkt_put_cstring(ssh, "eow@openssh.com")) != 0 ||
247 	    (r = sshpkt_put_u8(ssh, 0)) != 0 ||
248 	    (r = sshpkt_send(ssh)) != 0)
249 		fatal_fr(r, "send CHANNEL_EOF");
250 }
251 
252 /* shared */
253 
254 void
chan_rcvd_ieof(struct ssh * ssh,Channel * c)255 chan_rcvd_ieof(struct ssh *ssh, Channel *c)
256 {
257 	debug2("channel %d: rcvd eof", c->self);
258 	c->flags |= CHAN_EOF_RCVD;
259 	if (c->ostate == CHAN_OUTPUT_OPEN)
260 		chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
261 	if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN &&
262 	    sshbuf_len(c->output) == 0 &&
263 	    !CHANNEL_EFD_OUTPUT_ACTIVE(c))
264 		chan_obuf_empty(ssh, c);
265 }
266 
267 void
chan_rcvd_oclose(struct ssh * ssh,Channel * c)268 chan_rcvd_oclose(struct ssh *ssh, Channel *c)
269 {
270 	debug2("channel %d: rcvd close", c->self);
271 	if (!(c->flags & CHAN_LOCAL)) {
272 		if (c->flags & CHAN_CLOSE_RCVD)
273 			error("channel %d: protocol error: close rcvd twice",
274 			    c->self);
275 		c->flags |= CHAN_CLOSE_RCVD;
276 	}
277 	if (c->type == SSH_CHANNEL_LARVAL) {
278 		/* tear down larval channels immediately */
279 		chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
280 		chan_set_istate(c, CHAN_INPUT_CLOSED);
281 		return;
282 	}
283 	switch (c->ostate) {
284 	case CHAN_OUTPUT_OPEN:
285 		/*
286 		 * wait until a data from the channel is consumed if a CLOSE
287 		 * is received
288 		 */
289 		chan_set_ostate(c, CHAN_OUTPUT_WAIT_DRAIN);
290 		break;
291 	}
292 	switch (c->istate) {
293 	case CHAN_INPUT_OPEN:
294 		chan_shutdown_read(ssh, c);
295 		chan_shutdown_extended_read(ssh, c);
296 		chan_set_istate(c, CHAN_INPUT_CLOSED);
297 		break;
298 	case CHAN_INPUT_WAIT_DRAIN:
299 		if (!(c->flags & CHAN_LOCAL))
300 			chan_send_eof2(ssh, c);
301 		chan_shutdown_extended_read(ssh, c);
302 		chan_set_istate(c, CHAN_INPUT_CLOSED);
303 		break;
304 	}
305 }
306 
307 void
chan_write_failed(struct ssh * ssh,Channel * c)308 chan_write_failed(struct ssh *ssh, Channel *c)
309 {
310 	debug2("channel %d: write failed", c->self);
311 	switch (c->ostate) {
312 	case CHAN_OUTPUT_OPEN:
313 	case CHAN_OUTPUT_WAIT_DRAIN:
314 		chan_shutdown_write(ssh, c);
315 		if (strcmp(c->ctype, "session") == 0)
316 			chan_send_eow2(ssh, c);
317 		chan_set_ostate(c, CHAN_OUTPUT_CLOSED);
318 		break;
319 	default:
320 		error("channel %d: chan_write_failed for ostate %d",
321 		    c->self, c->ostate);
322 		break;
323 	}
324 }
325 
326 void
chan_mark_dead(struct ssh * ssh,Channel * c)327 chan_mark_dead(struct ssh *ssh, Channel *c)
328 {
329 	c->type = SSH_CHANNEL_ZOMBIE;
330 }
331 
332 int
chan_is_dead(struct ssh * ssh,Channel * c,int do_send)333 chan_is_dead(struct ssh *ssh, Channel *c, int do_send)
334 {
335 	if (c->type == SSH_CHANNEL_ZOMBIE) {
336 		debug2("channel %d: zombie", c->self);
337 		return 1;
338 	}
339 	if (c->istate != CHAN_INPUT_CLOSED || c->ostate != CHAN_OUTPUT_CLOSED)
340 		return 0;
341 	if ((ssh->compat & SSH_BUG_EXTEOF) &&
342 	    c->extended_usage == CHAN_EXTENDED_WRITE &&
343 	    c->efd != -1 &&
344 	    sshbuf_len(c->extended) > 0) {
345 		debug2("channel %d: active efd: %d len %zu",
346 		    c->self, c->efd, sshbuf_len(c->extended));
347 		return 0;
348 	}
349 	if (c->flags & CHAN_LOCAL) {
350 		debug2("channel %d: is dead (local)", c->self);
351 		return 1;
352 	}
353 	if (!(c->flags & CHAN_CLOSE_SENT)) {
354 		if (do_send) {
355 			chan_send_close2(ssh, c);
356 		} else {
357 			/* channel would be dead if we sent a close */
358 			if (c->flags & CHAN_CLOSE_RCVD) {
359 				debug2("channel %d: almost dead",
360 				    c->self);
361 				return 1;
362 			}
363 		}
364 	}
365 	if ((c->flags & CHAN_CLOSE_SENT) &&
366 	    (c->flags & CHAN_CLOSE_RCVD)) {
367 		debug2("channel %d: is dead", c->self);
368 		return 1;
369 	}
370 	return 0;
371 }
372 
373 /* helper */
374 static void
chan_shutdown_write(struct ssh * ssh,Channel * c)375 chan_shutdown_write(struct ssh *ssh, Channel *c)
376 {
377 	sshbuf_reset(c->output);
378 	if (c->type == SSH_CHANNEL_LARVAL)
379 		return;
380 	/* shutdown failure is allowed if write failed already */
381 	debug2_f("channel %d: (i%d o%d sock %d wfd %d efd %d [%s])",
382 	    c->self, c->istate, c->ostate, c->sock, c->wfd, c->efd,
383 	    channel_format_extended_usage(c));
384 	if (c->sock != -1) {
385 		if (shutdown(c->sock, SHUT_WR) == -1) {
386 			debug2_f("channel %d: shutdown() failed for "
387 			    "fd %d [i%d o%d]: %.100s", c->self, c->sock,
388 			    c->istate, c->ostate, strerror(errno));
389 		}
390 	} else {
391 		if (channel_close_fd(ssh, c, &c->wfd) < 0) {
392 			logit_f("channel %d: close() failed for "
393 			    "fd %d [i%d o%d]: %.100s", c->self, c->wfd,
394 			    c->istate, c->ostate, strerror(errno));
395 		}
396 	}
397 }
398 
399 static void
chan_shutdown_read(struct ssh * ssh,Channel * c)400 chan_shutdown_read(struct ssh *ssh, Channel *c)
401 {
402 	if (c->type == SSH_CHANNEL_LARVAL)
403 		return;
404 	debug2_f("channel %d: (i%d o%d sock %d wfd %d efd %d [%s])",
405 	    c->self, c->istate, c->ostate, c->sock, c->rfd, c->efd,
406 	    channel_format_extended_usage(c));
407 	if (c->sock != -1) {
408 		if (shutdown(c->sock, SHUT_RD) == -1) {
409 			error_f("channel %d: shutdown() failed for "
410 			    "fd %d [i%d o%d]: %.100s", c->self, c->sock,
411 			    c->istate, c->ostate, strerror(errno));
412 		}
413 	} else {
414 		if (channel_close_fd(ssh, c, &c->rfd) < 0) {
415 			logit_f("channel %d: close() failed for "
416 			    "fd %d [i%d o%d]: %.100s", c->self, c->rfd,
417 			    c->istate, c->ostate, strerror(errno));
418 		}
419 	}
420 }
421 
422 static void
chan_shutdown_extended_read(struct ssh * ssh,Channel * c)423 chan_shutdown_extended_read(struct ssh *ssh, Channel *c)
424 {
425 	if (c->type == SSH_CHANNEL_LARVAL || c->efd == -1)
426 		return;
427 	if (c->extended_usage != CHAN_EXTENDED_READ &&
428 	    c->extended_usage != CHAN_EXTENDED_IGNORE)
429 		return;
430 	debug_f("channel %d: (i%d o%d sock %d wfd %d efd %d [%s])",
431 	    c->self, c->istate, c->ostate, c->sock, c->rfd, c->efd,
432 	    channel_format_extended_usage(c));
433 	if (channel_close_fd(ssh, c, &c->efd) < 0) {
434 		logit_f("channel %d: close() failed for "
435 		    "extended fd %d [i%d o%d]: %.100s", c->self, c->efd,
436 		    c->istate, c->ostate, strerror(errno));
437 	}
438 }
439