[openssh-commits] [openssh] 12/14: upstream: limit the number and total size of packets that we will

git+noreply at mindrot.org git+noreply at mindrot.org
Thu Nov 26 10:20:57 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 de9d075097d49ec88cad09b17d066efb2ac79f29
Author: djm at openbsd.org <djm at openbsd.org>
AuthorDate: Wed Nov 25 22:14:23 2026 +0000

    upstream: 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
    
    OpenBSD-Commit-ID: d56090cde7758a5623828ad7428b4b27556f8cc6
---
 channels.c   |  4 ++--
 packet.c     | 42 +++++++++++++++++++++++++++++++++++++++++-
 packet.h     |  3 ++-
 serverloop.c |  6 ++++--
 4 files changed, 49 insertions(+), 6 deletions(-)

diff --git a/channels.c b/channels.c
index 16340ded6..9cfc7bda9 100644
--- a/channels.c
+++ b/channels.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: channels.c,v 1.480 2026/11/25 21:53:11 djm Exp $ */
+/* $OpenBSD: channels.c,v 1.481 2026/11/25 22:14:23 djm Exp $ */
 /*
  * Author: Tatu Ylonen <ylo at cs.hut.fi>
  * Copyright (c) 1995 Tatu Ylonen <ylo at cs.hut.fi>, Espoo, Finland
@@ -2835,7 +2835,7 @@ channel_handler(struct ssh *ssh, int table, struct timespec *timeout)
 		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)
+		if (ssh_packet_can_enqueue(ssh) && c->type != SSH_CHANNEL_OPEN)
 			continue;
 		if (c->delayed) {
 			if (table == CHAN_PRE)
diff --git a/packet.c b/packet.c
index 1c3e72df8..1d0e5b1b0 100644
--- a/packet.c
+++ b/packet.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: packet.c,v 1.351 2026/11/25 06:57:05 djm Exp $ */
+/* $OpenBSD: packet.c,v 1.352 2026/11/25 22:14:23 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 fdf2a9d80..431367452 100644
--- a/packet.h
+++ b/packet.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: packet.h,v 1.110 2026/11/25 06:57:05 djm Exp $ */
+/* $OpenBSD: packet.h,v 1.111 2026/11/25 22:14:23 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 ca38cbfc6..affd5feec 100644
--- a/serverloop.c
+++ b/serverloop.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: serverloop.c,v 1.251 2026/11/25 08:26:59 djm Exp $ */
+/* $OpenBSD: serverloop.c,v 1.252 2026/11/25 22:14:23 djm Exp $ */
 /*
  * Author: Tatu Ylonen <ylo at cs.hut.fi>
  * Copyright (c) 1995 Tatu Ylonen <ylo at cs.hut.fi>, Espoo, Finland
@@ -389,7 +389,9 @@ server_loop2(struct ssh *ssh, Authctxt *authctxt)
 		if (sigprocmask(SIG_SETMASK, &osigset, NULL) == -1)
 			error_f("osigset sigprocmask: %s", strerror(errno));
 
-		channel_after_poll(ssh, pfd, npfd_active);
+		if (ssh_packet_can_enqueue(ssh))
+			channel_after_poll(ssh, pfd, npfd_active);
+
 		if (conn_in_ready &&
 		    process_input(ssh, connection_in) < 0)
 			break;

-- 
To stop receiving notification emails like this one, please contact
djm at mindrot.org.


More information about the openssh-commits mailing list