@@ -187,6 +187,8 @@ fullpacket(LIBSSH2_SESSION * session, int encrypted /* 1 or 0 */ )
187187 const LIBSSH2_MAC_METHOD * remote_mac = NULL ;
188188 uint32_t seq = session -> remote .seqno ;
189189
190+ memset (macbuf , '\0' , sizeof (macbuf ));
191+
190192 if (!encrypted || (!CRYPT_FLAG_R (session , REQUIRES_FULL_PACKET ) &&
191193 !CRYPT_FLAG_R (session , INTEGRATED_MAC ))) {
192194 remote_mac = session -> remote .mac ;
@@ -258,6 +260,12 @@ fullpacket(LIBSSH2_SESSION * session, int encrypted /* 1 or 0 */ )
258260 /* grab padding length and copy anything else
259261 into target buffer */
260262 p -> padding_length = first_block [0 ];
263+
264+ if (p -> padding_length > p -> packet_length - 1 ) {
265+ LIBSSH2_FREE (session , decrypt_buffer );
266+ return LIBSSH2_ERROR_PROTO ;
267+ }
268+
261269 if (blocksize > 1 ) {
262270 memcpy (decrypt_buffer , first_block + 1 , blocksize - 1 );
263271 }
@@ -301,8 +309,8 @@ fullpacket(LIBSSH2_SESSION * session, int encrypted /* 1 or 0 */ )
301309 * cannot decompress.
302310 */
303311
304- unsigned char * data ;
305- size_t data_len ;
312+ unsigned char * data = NULL ;
313+ size_t data_len = 0 ;
306314 rc = session -> remote .comp -> decomp (session ,
307315 & data , & data_len ,
308316 LIBSSH2_PACKET_MAXDECOMP ,
@@ -608,6 +616,10 @@ int _libssh2_transport_read(LIBSSH2_SESSION * session)
608616 }
609617
610618 if (etm ) {
619+ /* don't know what padding is until we decrypt the full
620+ packet */
621+ p -> padding_length = 0 ;
622+
611623 /* we collect entire undecrypted packet including the
612624 packet length field that we run MAC over */
613625 p -> packet_length = _libssh2_ntohu32 (block );
0 commit comments