From efd3b96dc8e378a5155cb42f4869de85f4153135 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Fri, 18 Apr 2014 16:58:00 -0500 Subject: [PATCH] SSH2: improve handling of incomplete packets --- phpseclib/Net/SSH2.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/phpseclib/Net/SSH2.php b/phpseclib/Net/SSH2.php index 346dca92..25b7b9c0 100644 --- a/phpseclib/Net/SSH2.php +++ b/phpseclib/Net/SSH2.php @@ -2683,6 +2683,11 @@ class Net_SSH2 $buffer = ''; while ($remaining_length > 0) { $temp = fread($this->fsock, $remaining_length); + if ($temp === false || feof($this->fsock)) { + user_error('Error reading from socket'); + $this->bitmap = 0; + return false; + } $buffer.= $temp; $remaining_length-= strlen($temp); } @@ -2696,7 +2701,11 @@ class Net_SSH2 if ($this->hmac_check !== false) { $hmac = fread($this->fsock, $this->hmac_size); - if ($hmac != $this->hmac_check->hash(pack('NNCa*', $this->get_seq_no, $packet_length, $padding_length, $payload . $padding))) { + if ($hmac === false || strlen($hmac) != $this->hmac_size) { + user_error('Error reading socket'); + $this->bitmap = 0; + return false; + } elseif ($hmac != $this->hmac_check->hash(pack('NNCa*', $this->get_seq_no, $packet_length, $padding_length, $payload . $padding))) { user_error('Invalid HMAC'); return false; }