[openssh-commits] [openssh] 01/03: upstream: Let's try this again:
git+noreply at mindrot.org
git+noreply at mindrot.org
Thu Nov 26 14:47:00 AEDT 2026
This is an automated email from the git hooks/post-receive script.
djm pushed a commit to branch master
in repository openssh.
commit b05f4ba1ad93e613c2b756dd5176004e27cc5b50
Author: djm at openbsd.org <djm at openbsd.org>
AuthorDate: Thu Nov 26 03:44:28 2026 +0000
upstream: Let's try this again:
limit the number and total size of packets that we will queue
while KEX is running. Avoids possible DoS when KEX stalls and local
processes continue generating channel data for too long
Anthropic ANT-2026-Q1334JFN but with a separate threshold after which
the channels stop producing packets too
Now with at least one fewer redundant and one fewer wrong test to break
mux.
OpenBSD-Commit-ID: 45440ce9a2e62bee7e1707a61a38e9292b602eff
---
channels.c | 11 ++++++++---
packet.c | 42 +++++++++++++++++++++++++++++++++++++++++-
packet.h | 3 ++-
serverloop.c | 2 +-
4 files changed, 52 insertions(+), 6 deletions(-)
diff --git a/channels.c b/channels.c
index 48fb49024..44997bdcd 100644
--- a/channels.c
+++ b/channels.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: channels.c,v 1.482 2026/11/26 03:12:24 djm Exp $ */
+/* $OpenBSD: channels.c,v 1.483 2026/11/26 03:44:28 djm Exp $ */
/*
* Author: Tatu Ylonen <ylo at cs.hut.fi>
* Copyright (c) 1995 Tatu Ylonen <ylo at cs.hut.fi>, Espoo, Finland
@@ -2834,8 +2834,13 @@ channel_handler(struct ssh *ssh, int table, struct timespec *timeout)
/* Count open channels in bulk state */
if (c->type == SSH_CHANNEL_OPEN && c->bulk)
sc->nbulk++;
- /* Try to keep IO going while rekeying */
- if (ssh_packet_is_rekeying(ssh) && c->type != SSH_CHANNEL_OPEN)
+ /*
+ * During rekey, keep I/O going on open channels until
+ * the packet queue reaches its soft limit.
+ */
+ if (ssh_packet_is_rekeying(ssh) &&
+ (c->type != SSH_CHANNEL_OPEN ||
+ !ssh_packet_can_enqueue(ssh)))
continue;
if (c->delayed) {
if (table == CHAN_PRE)
diff --git a/packet.c b/packet.c
index ed0d40270..f260c09c5 100644
--- a/packet.c
+++ b/packet.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: packet.c,v 1.353 2026/11/26 03:12:24 djm Exp $ */
+/* $OpenBSD: packet.c,v 1.354 2026/11/26 03:44:28 djm Exp $ */
/*
* Author: Tatu Ylonen <ylo at cs.hut.fi>
* Copyright (c) 1995 Tatu Ylonen <ylo at cs.hut.fi>, Espoo, Finland
@@ -99,6 +99,18 @@
#define PACKET_MAX_SIZE (256 * 1024)
+/*
+ * Limits on the packets that may be queued for sending until a key exchange
+ * completes. The queue is flushed to the output buffer in one go, so there
+ * is no point in holding more data than could fit there.
+ */
+#define PACKET_MAX_QUEUED (64 * 1024)
+#define PACKET_MAX_QUEUED_BYTES SSHBUF_SIZE_MAX
+
+/* Channels will keep enqueuing data until these thresholds are hit */
+#define PACKET_QUEUED_FULL ((3 * PACKET_MAX_QUEUED) / 4)
+#define PACKET_QUEUED_BYTES ((3 * PACKET_MAX_QUEUED_BYTES) / 4)
+
struct packet_state {
uint32_t seqnr;
uint32_t packets;
@@ -236,7 +248,10 @@ struct session_state {
ssh_packet_hook_fn *hook_in;
void *hook_in_ctx;
+ /* Packets held by ssh_packet_send2() until key exchange completes */
TAILQ_HEAD(, packet) outgoing;
+ u_int outgoing_npackets;
+ size_t outgoing_nbytes;
};
struct ssh *
@@ -301,6 +316,16 @@ ssh_packet_is_rekeying(struct ssh *ssh)
(ssh->kex != NULL && ssh->kex->done == 0);
}
+/* Returns nonzero if a packet can be enqueued at this time */
+int
+ssh_packet_can_enqueue(struct ssh *ssh)
+{
+ if (ssh == NULL || ssh->state == NULL || !ssh_packet_is_rekeying(ssh))
+ return 1;
+ return ssh->state->outgoing_npackets < PACKET_QUEUED_FULL &&
+ ssh->state->outgoing_nbytes < PACKET_QUEUED_BYTES;
+}
+
/*
* Sets the descriptors used for communication.
*/
@@ -1435,12 +1460,25 @@ ssh_packet_send2(struct ssh *ssh)
if (need_rekey)
debug3_f("rekex triggered");
debug("enqueue packet: %u", type);
+ /*
+ * The queue only drains once the peer has completed the key
+ * exchange. Don't let a peer that stalls it exhaust our memory.
+ */
+ if (state->outgoing_npackets >= PACKET_MAX_QUEUED ||
+ sshbuf_len(state->outgoing_packet) >
+ PACKET_MAX_QUEUED_BYTES - state->outgoing_nbytes) {
+ error("too many packets queued while waiting for "
+ "peer to complete key exchange");
+ return SSH_ERR_NO_BUFFER_SPACE;
+ }
p = calloc(1, sizeof(*p));
if (p == NULL)
return SSH_ERR_ALLOC_FAIL;
p->type = type;
p->payload = state->outgoing_packet;
TAILQ_INSERT_TAIL(&state->outgoing, p, next);
+ state->outgoing_npackets++;
+ state->outgoing_nbytes += sshbuf_len(p->payload);
state->outgoing_packet = sshbuf_new();
if (state->outgoing_packet == NULL)
return SSH_ERR_ALLOC_FAIL;
@@ -1482,6 +1520,8 @@ ssh_packet_send2(struct ssh *ssh)
sshbuf_free(state->outgoing_packet);
state->outgoing_packet = p->payload;
TAILQ_REMOVE(&state->outgoing, p, next);
+ state->outgoing_npackets--;
+ state->outgoing_nbytes -= sshbuf_len(p->payload);
memset(p, 0, sizeof(*p));
free(p);
if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
diff --git a/packet.h b/packet.h
index a1287185c..f3de148d1 100644
--- a/packet.h
+++ b/packet.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: packet.h,v 1.112 2026/11/26 03:12:24 djm Exp $ */
+/* $OpenBSD: packet.h,v 1.113 2026/11/26 03:44:28 djm Exp $ */
/*
* Author: Tatu Ylonen <ylo at cs.hut.fi>
@@ -104,6 +104,7 @@ void ssh_packet_clear_keys(struct ssh *);
void ssh_clear_newkeys(struct ssh *, int);
int ssh_packet_is_rekeying(struct ssh *);
+int ssh_packet_can_enqueue(struct ssh *);
int ssh_packet_check_rekey(struct ssh *);
void ssh_packet_set_protocol_flags(struct ssh *, u_int);
u_int ssh_packet_get_protocol_flags(struct ssh *);
diff --git a/serverloop.c b/serverloop.c
index 6f02fc1a6..25f210482 100644
--- a/serverloop.c
+++ b/serverloop.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: serverloop.c,v 1.253 2026/11/26 03:12:24 djm Exp $ */
+/* $OpenBSD: serverloop.c,v 1.254 2026/11/26 03:44:28 djm Exp $ */
/*
* Author: Tatu Ylonen <ylo at cs.hut.fi>
* Copyright (c) 1995 Tatu Ylonen <ylo at cs.hut.fi>, Espoo, Finland
--
To stop receiving notification emails like this one, please contact
djm at mindrot.org.
More information about the openssh-commits
mailing list