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