diff --git a/src/channel.c b/src/channel.c index 4854c211..2966845c 100644 --- a/src/channel.c +++ b/src/channel.c @@ -230,14 +230,21 @@ LIBSSH2_CHANNEL *ssh2_channel_open(LIBSSH2_SESSION *session, goto channel_error; } + packet_size = ssh2_ntohu32(session->open_data + 13); + + if(!packet_size) { + ssh2_err(session, LIBSSH2_ERROR_CHANNEL_FAILURE, + "Invalid packet size received from server"); + goto channel_error; + } + session->open_channel->remote.id = ssh2_ntohu32(session->open_data + 5); session->open_channel->local.window_size = ssh2_ntohu32(session->open_data + 9); session->open_channel->local.window_size_initial = ssh2_ntohu32(session->open_data + 9); - session->open_channel->local.packet_size = - ssh2_ntohu32(session->open_data + 13); + session->open_channel->local.packet_size = packet_size; ssh2_deb((session, LIBSSH2_TRACE_CONN, "Connection Established - ID: %u/%u win: %u/%u" " pack: %u/%u", diff --git a/src/packet.c b/src/packet.c index cdadefec..6ad352ce 100644 --- a/src/packet.c +++ b/src/packet.c @@ -132,6 +132,14 @@ static SSH2_INLINE int packet_queue_listener( break; } + if(!listen_state->packet_size) { + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "Invalid packet size received from server")); + failure_code = SSH_OPEN_CONNECT_FAILED; + listen_state->state = ssh2_NB_state_sent; + break; + } + channel = SSH2_CALLOC(session, sizeof(LIBSSH2_CHANNEL)); if(!channel) { ssh2_err(session, LIBSSH2_ERROR_ALLOC, @@ -325,6 +333,13 @@ static SSH2_INLINE int packet_x11_open( if(session->x11) { if(x11open_state->state == ssh2_NB_state_allocated) { + if(!x11open_state->packet_size) { + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "Invalid packet size received from server")); + failure_code = SSH_OPEN_CONNECT_FAILED; + goto x11_exit; + } + channel = SSH2_CALLOC(session, sizeof(LIBSSH2_CHANNEL)); if(!channel) { ssh2_err(session, LIBSSH2_ERROR_ALLOC, @@ -476,6 +491,13 @@ static SSH2_INLINE int packet_authagent_open( if(session->authagent) { if(authagent_state->state == ssh2_NB_state_allocated) { + if(!authagent_state->packet_size) { + ssh2_deb((session, LIBSSH2_TRACE_CONN, + "Invalid packet size received from server")); + failure_code = SSH_OPEN_CONNECT_FAILED; + goto authagent_exit; + } + channel = SSH2_CALLOC(session, sizeof(LIBSSH2_CHANNEL)); authagent_state->channel = channel;