summaryrefslogtreecommitdiff
path: root/channels.c
diff options
context:
space:
mode:
Diffstat (limited to 'channels.c')
-rw-r--r--channels.c40
1 files changed, 30 insertions, 10 deletions
diff --git a/channels.c b/channels.c
index 935625c74..3ab4823a9 100644
--- a/channels.c
+++ b/channels.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: channels.c,v 1.366 2017/08/30 03:59:08 djm Exp $ */ 1/* $OpenBSD: channels.c,v 1.368 2017/09/12 06:35:31 djm Exp $ */
2/* 2/*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi> 3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
@@ -252,14 +252,14 @@ channel_by_id(struct ssh *ssh, int id)
252} 252}
253 253
254Channel * 254Channel *
255channel_by_remote_id(struct ssh *ssh, int remote_id) 255channel_by_remote_id(struct ssh *ssh, u_int remote_id)
256{ 256{
257 Channel *c; 257 Channel *c;
258 u_int i; 258 u_int i;
259 259
260 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { 260 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
261 c = ssh->chanctxt->channels[i]; 261 c = ssh->chanctxt->channels[i];
262 if (c != NULL && c->remote_id == remote_id) 262 if (c != NULL && c->have_remote_id && c->remote_id == remote_id)
263 return c; 263 return c;
264 } 264 }
265 return NULL; 265 return NULL;
@@ -387,7 +387,6 @@ channel_new(struct ssh *ssh, char *ctype, int type, int rfd, int wfd, int efd,
387 c->local_window = window; 387 c->local_window = window;
388 c->local_window_max = window; 388 c->local_window_max = window;
389 c->local_maxpacket = maxpack; 389 c->local_maxpacket = maxpack;
390 c->remote_id = -1;
391 c->remote_name = xstrdup(remote_name); 390 c->remote_name = xstrdup(remote_name);
392 c->ctl_chan = -1; 391 c->ctl_chan = -1;
393 c->delayed = 1; /* prevent call to channel_post handler */ 392 c->delayed = 1; /* prevent call to channel_post handler */
@@ -703,7 +702,7 @@ channel_find_open(struct ssh *ssh)
703 702
704 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) { 703 for (i = 0; i < ssh->chanctxt->channels_alloc; i++) {
705 c = ssh->chanctxt->channels[i]; 704 c = ssh->chanctxt->channels[i];
706 if (c == NULL || c->remote_id < 0) 705 if (c == NULL || !c->have_remote_id)
707 continue; 706 continue;
708 switch (c->type) { 707 switch (c->type) {
709 case SSH_CHANNEL_CLOSED: 708 case SSH_CHANNEL_CLOSED:
@@ -778,9 +777,10 @@ channel_open_message(struct ssh *ssh)
778 case SSH_CHANNEL_MUX_PROXY: 777 case SSH_CHANNEL_MUX_PROXY:
779 case SSH_CHANNEL_MUX_CLIENT: 778 case SSH_CHANNEL_MUX_CLIENT:
780 if ((r = sshbuf_putf(buf, " #%d %.300s " 779 if ((r = sshbuf_putf(buf, " #%d %.300s "
781 "(t%d r%d i%u/%zu o%u/%zu fd %d/%d cc %d)\r\n", 780 "(t%d %s%u i%u/%zu o%u/%zu fd %d/%d cc %d)\r\n",
782 c->self, c->remote_name, 781 c->self, c->remote_name,
783 c->type, c->remote_id, 782 c->type,
783 c->have_remote_id ? "r" : "nr", c->remote_id,
784 c->istate, sshbuf_len(c->input), 784 c->istate, sshbuf_len(c->input),
785 c->ostate, sshbuf_len(c->output), 785 c->ostate, sshbuf_len(c->output),
786 c->rfd, c->wfd, c->ctl_chan)) != 0) 786 c->rfd, c->wfd, c->ctl_chan)) != 0)
@@ -838,6 +838,9 @@ channel_request_start(struct ssh *ssh, int id, char *service, int wantconfirm)
838 logit("%s: %d: unknown channel id", __func__, id); 838 logit("%s: %d: unknown channel id", __func__, id);
839 return; 839 return;
840 } 840 }
841 if (!c->have_remote_id)
842 fatal(":%s: channel %d: no remote id", __func__, c->self);
843
841 debug2("channel %d: request %s confirm %d", id, service, wantconfirm); 844 debug2("channel %d: request %s confirm %d", id, service, wantconfirm);
842 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_REQUEST)) != 0 || 845 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_REQUEST)) != 0 ||
843 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || 846 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
@@ -930,6 +933,9 @@ channel_set_fds(struct ssh *ssh, int id, int rfd, int wfd, int efd,
930 933
931 if (c == NULL || c->type != SSH_CHANNEL_LARVAL) 934 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
932 fatal("channel_activate for non-larval channel %d.", id); 935 fatal("channel_activate for non-larval channel %d.", id);
936 if (!c->have_remote_id)
937 fatal(":%s: channel %d: no remote id", __func__, c->self);
938
933 channel_register_fds(ssh, c, rfd, wfd, efd, extusage, nonblock, is_tty); 939 channel_register_fds(ssh, c, rfd, wfd, efd, extusage, nonblock, is_tty);
934 c->type = SSH_CHANNEL_OPEN; 940 c->type = SSH_CHANNEL_OPEN;
935 c->local_window = c->local_window_max = window_max; 941 c->local_window = c->local_window_max = window_max;
@@ -1698,6 +1704,8 @@ channel_post_connecting(struct ssh *ssh, Channel *c,
1698 1704
1699 if (!FD_ISSET(c->sock, writeset)) 1705 if (!FD_ISSET(c->sock, writeset))
1700 return; 1706 return;
1707 if (!c->have_remote_id)
1708 fatal(":%s: channel %d: no remote id", __func__, c->self);
1701 1709
1702 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) { 1710 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) {
1703 err = errno; 1711 err = errno;
@@ -1983,6 +1991,9 @@ channel_check_window(struct ssh *ssh, Channel *c)
1983 c->local_maxpacket*3) || 1991 c->local_maxpacket*3) ||
1984 c->local_window < c->local_window_max/2) && 1992 c->local_window < c->local_window_max/2) &&
1985 c->local_consumed > 0) { 1993 c->local_consumed > 0) {
1994 if (!c->have_remote_id)
1995 fatal(":%s: channel %d: no remote id",
1996 __func__, c->self);
1986 if ((r = sshpkt_start(ssh, 1997 if ((r = sshpkt_start(ssh,
1987 SSH2_MSG_CHANNEL_WINDOW_ADJUST)) != 0 || 1998 SSH2_MSG_CHANNEL_WINDOW_ADJUST)) != 0 ||
1988 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || 1999 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
@@ -2338,6 +2349,9 @@ channel_output_poll_input_open(struct ssh *ssh, Channel *c)
2338 return; 2349 return;
2339 } 2350 }
2340 2351
2352 if (!c->have_remote_id)
2353 fatal(":%s: channel %d: no remote id", __func__, c->self);
2354
2341 if (c->datagram) { 2355 if (c->datagram) {
2342 /* Check datagram will fit; drop if not */ 2356 /* Check datagram will fit; drop if not */
2343 if ((r = sshbuf_peek_string_direct(c->input, NULL, &dlen)) != 0) 2357 if ((r = sshbuf_peek_string_direct(c->input, NULL, &dlen)) != 0)
@@ -2404,6 +2418,8 @@ channel_output_poll_extended_read(struct ssh *ssh, Channel *c)
2404 len = c->remote_maxpacket; 2418 len = c->remote_maxpacket;
2405 if (len == 0) 2419 if (len == 0)
2406 return; 2420 return;
2421 if (!c->have_remote_id)
2422 fatal(":%s: channel %d: no remote id", __func__, c->self);
2407 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_EXTENDED_DATA)) != 0 || 2423 if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_EXTENDED_DATA)) != 0 ||
2408 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || 2424 (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
2409 (r = sshpkt_put_u32(ssh, SSH2_EXTENDED_DATA_STDERR)) != 0 || 2425 (r = sshpkt_put_u32(ssh, SSH2_EXTENDED_DATA_STDERR)) != 0 ||
@@ -2570,6 +2586,7 @@ channel_proxy_downstream(struct ssh *ssh, Channel *downstream)
2570 c->mux_ctx = downstream; /* point to mux client */ 2586 c->mux_ctx = downstream; /* point to mux client */
2571 c->mux_downstream_id = id; 2587 c->mux_downstream_id = id;
2572 c->remote_id = remote_id; 2588 c->remote_id = remote_id;
2589 c->have_remote_id = 1;
2573 if ((r = sshbuf_put_u32(modified, remote_id)) != 0 || 2590 if ((r = sshbuf_put_u32(modified, remote_id)) != 0 ||
2574 (r = sshbuf_put_u32(modified, c->self)) != 0 || 2591 (r = sshbuf_put_u32(modified, c->self)) != 0 ||
2575 (r = sshbuf_putb(modified, original)) != 0) { 2592 (r = sshbuf_putb(modified, original)) != 0) {
@@ -2713,8 +2730,10 @@ channel_proxy_upstream(Channel *c, int type, u_int32_t seq, struct ssh *ssh)
2713 switch (type) { 2730 switch (type) {
2714 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION: 2731 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2715 /* record remote_id for SSH2_MSG_CHANNEL_CLOSE */ 2732 /* record remote_id for SSH2_MSG_CHANNEL_CLOSE */
2716 if (cp && len > 4) 2733 if (cp && len > 4) {
2717 c->remote_id = PEEK_U32(cp); 2734 c->remote_id = PEEK_U32(cp);
2735 c->have_remote_id = 1;
2736 }
2718 break; 2737 break;
2719 case SSH2_MSG_CHANNEL_CLOSE: 2738 case SSH2_MSG_CHANNEL_CLOSE:
2720 if (c->flags & CHAN_CLOSE_SENT) 2739 if (c->flags & CHAN_CLOSE_SENT)
@@ -2923,14 +2942,15 @@ channel_input_open_confirmation(int type, u_int32_t seq, struct ssh *ssh)
2923 * Record the remote channel number and mark that the channel 2942 * Record the remote channel number and mark that the channel
2924 * is now open. 2943 * is now open.
2925 */ 2944 */
2926 c->remote_id = channel_parse_id(ssh, __func__, "open confirmation"); 2945 if ((r = sshpkt_get_u32(ssh, &c->remote_id)) != 0 ||
2927 if ((r = sshpkt_get_u32(ssh, &remote_window)) != 0 || 2946 (r = sshpkt_get_u32(ssh, &remote_window)) != 0 ||
2928 (r = sshpkt_get_u32(ssh, &remote_maxpacket)) != 0) { 2947 (r = sshpkt_get_u32(ssh, &remote_maxpacket)) != 0) {
2929 error("%s: window/maxpacket: %s", __func__, ssh_err(r)); 2948 error("%s: window/maxpacket: %s", __func__, ssh_err(r));
2930 packet_disconnect("Invalid open confirmation message"); 2949 packet_disconnect("Invalid open confirmation message");
2931 } 2950 }
2932 ssh_packet_check_eom(ssh); 2951 ssh_packet_check_eom(ssh);
2933 2952
2953 c->have_remote_id = 1;
2934 c->remote_window = remote_window; 2954 c->remote_window = remote_window;
2935 c->remote_maxpacket = remote_maxpacket; 2955 c->remote_maxpacket = remote_maxpacket;
2936 c->type = SSH_CHANNEL_OPEN; 2956 c->type = SSH_CHANNEL_OPEN;