103103#define DBG (x )
104104#endif
105105
106- /* SSH_IOBUFSZ + 1k of head room */
107106/* OpenSSH usings 256KB packet size max but that consumes a
108- * lot of memory wiht the buffers we are using. This keeps it
109- * in check. Doesn't seem to have an impact on performance or
110- * functionality cjr 04/06/2023 */
111- #define PACKET_MAX_SIZE (SSH_IOBUFSZ + 1024)
107+ * lot of memory with the buffers we are using. However, we need
108+ * a large packet size if the banner that's being sent is large.
109+ * So we need a 256KB packet pre authentication and a smaller one
110+ * in this case SSH_IOBUFSZ + 1KB, afterwards. So we change
111+ * PACKET_MAX_SIZE from a #define to a global. Then, in the function
112+ * ssh_packet_set_authentcated we reduce the size to something
113+ * more memory efficient. -cjr 04/07/23
114+ */
115+ u_int packet_max_size = 256 * 1024 ;
112116
113117struct packet_state {
114118 u_int32_t seqnr ;
@@ -397,7 +401,7 @@ ssh_packet_stop_discard(struct ssh *ssh)
397401
398402 if (state -> packet_discard_mac ) {
399403 char buf [1024 ];
400- size_t dlen = PACKET_MAX_SIZE ;
404+ size_t dlen = packet_max_size ;
401405
402406 if (dlen > state -> packet_discard_mac_already )
403407 dlen -= state -> packet_discard_mac_already ;
@@ -1504,7 +1508,7 @@ ssh_packet_read_poll2_mux(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
15041508 return 0 ; /* packet is incomplete */
15051509 state -> packlen = PEEK_U32 (cp );
15061510 if (state -> packlen < 4 + 1 ||
1507- state -> packlen > PACKET_MAX_SIZE )
1511+ state -> packlen > packet_max_size )
15081512 return SSH_ERR_MESSAGE_INCOMPLETE ;
15091513 }
15101514 need = state -> packlen + 4 ;
@@ -1563,7 +1567,7 @@ ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
15631567 sshbuf_ptr (state -> input ), sshbuf_len (state -> input )) != 0 )
15641568 return 0 ;
15651569 if (state -> packlen < 1 + 4 ||
1566- state -> packlen > PACKET_MAX_SIZE ) {
1570+ state -> packlen > packet_max_size ) {
15671571#ifdef PACKET_DEBUG
15681572 sshbuf_dump (state -> input , stderr );
15691573#endif
@@ -1590,7 +1594,7 @@ ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
15901594 goto out ;
15911595 state -> packlen = PEEK_U32 (sshbuf_ptr (state -> incoming_packet ));
15921596 if (state -> packlen < 1 + 4 ||
1593- state -> packlen > PACKET_MAX_SIZE ) {
1597+ state -> packlen > packet_max_size ) {
15941598#ifdef PACKET_DEBUG
15951599 fprintf (stderr , "input: \n" );
15961600 sshbuf_dump (state -> input , stderr );
@@ -1599,7 +1603,7 @@ ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
15991603#endif
16001604 logit ("Bad packet length %u." , state -> packlen );
16011605 return ssh_packet_start_discard (ssh , enc , mac , 0 ,
1602- PACKET_MAX_SIZE );
1606+ packet_max_size );
16031607 }
16041608 if ((r = sshbuf_consume (state -> input , block_size )) != 0 )
16051609 goto out ;
@@ -1622,7 +1626,7 @@ ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
16221626 logit ("padding error: need %d block %d mod %d" ,
16231627 need , block_size , need % block_size );
16241628 return ssh_packet_start_discard (ssh , enc , mac , 0 ,
1625- PACKET_MAX_SIZE - block_size );
1629+ packet_max_size - block_size );
16261630 }
16271631 /*
16281632 * check if the entire packet has been received and
@@ -1666,11 +1670,11 @@ ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
16661670 if (r != SSH_ERR_MAC_INVALID )
16671671 goto out ;
16681672 logit ("Corrupted MAC on input." );
1669- if (need + block_size > PACKET_MAX_SIZE )
1673+ if (need + block_size > packet_max_size )
16701674 return SSH_ERR_INTERNAL_ERROR ;
16711675 return ssh_packet_start_discard (ssh , enc , mac ,
16721676 sshbuf_len (state -> incoming_packet ),
1673- PACKET_MAX_SIZE - need - block_size );
1677+ packet_max_size - need - block_size );
16741678 }
16751679 /* Remove MAC from input buffer */
16761680 DBG (debug ("MAC #%d ok" , state -> p_read .seqnr ));
@@ -1842,7 +1846,7 @@ ssh_packet_process_read(struct ssh *ssh, int fd)
18421846 int r ;
18431847 size_t rlen ;
18441848
1845- if ((r = sshbuf_read (fd , state -> input , PACKET_MAX_SIZE , & rlen )) != 0 )
1849+ if ((r = sshbuf_read (fd , state -> input , packet_max_size , & rlen )) != 0 )
18461850 return r ;
18471851
18481852 if (state -> packet_discard ) {
@@ -2241,6 +2245,7 @@ void
22412245ssh_packet_set_authenticated (struct ssh * ssh )
22422246{
22432247 ssh -> state -> after_authentication = 1 ;
2248+ packet_max_size = SSH_IOBUFSZ + 1024 ;
22442249}
22452250
22462251void *
0 commit comments