Skip to content

Commit 865d67b

Browse files
committed
/* OpenSSH usings 256KB packet size max but that consumes a
* lot of memory with the buffers we are using. However, we need * a large packet size if the banner that's being sent is large. * So we need a 256KB packet pre authentication and a smaller one * in this case SSH_IOBUFSZ + 1KB, afterwards. So we change * PACKET_MAX_SIZE from a #define to a global. Then, in the function * ssh_packet_set_authentcated we reduce the size to something * more memory efficient. -cjr 04/07/23 */
1 parent 20da8e9 commit 865d67b

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

auth2.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ static char *authmethods_get(Authctxt *authctxt);
105105
#define MATCH_PARTIAL 3 /* method matches, submethod can't be checked */
106106
static int list_starts_with(const char *, const char *, const char *);
107107

108+
/* read the user banner from the path in sshd_config
109+
* this isn't to read it on the client side but to read
110+
* it into what we are going to send on the server side */
108111
char *
109112
auth2_read_banner(void)
110113
{

packet.c

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,16 @@
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

113117
struct 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
22412245
ssh_packet_set_authenticated(struct ssh *ssh)
22422246
{
22432247
ssh->state->after_authentication = 1;
2248+
packet_max_size = SSH_IOBUFSZ + 1024;
22442249
}
22452250

22462251
void *

regress/banner.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ verbose "test $tid: missing banner file"
1313
cmp $OBJ/empty.in $OBJ/banner.out ) || \
1414
fail "missing banner file"
1515

16-
for s in 0 10 100 1000 10000 ; do
16+
for s in 0 10 100 1000 10000 100000; do
1717
if [ "$s" = "0" ]; then
1818
# create empty banner
1919
touch $OBJ/banner.in

0 commit comments

Comments
 (0)