[openssh-commits] [openssh] 07/14: upstream: use the remote address returned from accept(2) for

git+noreply at mindrot.org git+noreply at mindrot.org
Thu Nov 26 10:20:52 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 ca22af23dfdf08800224608fce4738612a8cf7c6
Author: djm at openbsd.org <djm at openbsd.org>
AuthorDate: Wed Nov 25 21:08:49 2026 +0000

    upstream: use the remote address returned from accept(2) for
    
    PerSourcePenalties rather than doing an explicit getpeername(2) lookup later.
    
    Avoids very minor PerSourcePenalties bypass, where the peer connects,
    does nothing and then disconnects before getpeername() has a chance
    to complete. Also saves a syscall.
    
    Anthropic ANT-2026-MFL5Y7HK
    
    OpenBSD-Commit-ID: 130e249330f10182752b237020092a5489ba46e9
---
 srclimit.c | 30 +++++++++---------------------
 srclimit.h |  4 ++--
 sshd.c     | 32 ++++++++++++++++++++------------
 3 files changed, 31 insertions(+), 35 deletions(-)

diff --git a/srclimit.c b/srclimit.c
index 05f22ee13..58501b71d 100644
--- a/srclimit.c
+++ b/srclimit.c
@@ -81,20 +81,6 @@ srclimit_mask_addr(const struct xaddr *addr, int bits, struct xaddr *masked)
 	return 0;
 }
 
-static int
-srclimit_peer_addr(int sock, struct xaddr *addr)
-{
-	struct sockaddr_storage storage;
-	socklen_t addrlen = sizeof(storage);
-	struct sockaddr *sa = (struct sockaddr *)&storage;
-
-	if (getpeername(sock, sa, &addrlen) != 0)
-		return 1;	/* not remote socket? */
-	if (addr_sa_to_xaddr(sa, addrlen, addr) != 0)
-		return 1;	/* unknown address family? */
-	return 0;
-}
-
 void
 srclimit_init(int max, int persource, int ipv4len, int ipv6len,
     struct per_source_penalty *penalty_conf, const char *penalty_exempt_conf)
@@ -127,7 +113,7 @@ srclimit_init(int max, int persource, int ipv4len, int ipv6len,
 
 /* returns 1 if connection allowed, 0 if not allowed. */
 int
-srclimit_check_allow(int sock, int id)
+srclimit_check_allow(const struct xaddr *peer, int id)
 {
 	struct xaddr xa, xb;
 	int i, bits, first_unused, count = 0;
@@ -136,9 +122,10 @@ srclimit_check_allow(int sock, int id)
 	if (max_persource == INT_MAX)	/* no limit */
 		return 1;
 
-	debug_f("sock %d id %d limit %d", sock, id, max_persource);
-	if (srclimit_peer_addr(sock, &xa) != 0)
-		return 1;
+	debug_f("id %d limit %d", id, max_persource);
+	if (peer == NULL)
+		return 1;	/* not remote socket? */
+	xa = *peer;
 	bits = xa.af == AF_INET ? ipv4_masklen : ipv6_masklen;
 	if (srclimit_mask_addr(&xa, bits, &xb) != 0)
 		return 1;
@@ -256,7 +243,7 @@ addr_masklen_ntop(struct xaddr *addr, int masklen, char *s, size_t slen)
 }
 
 int
-srclimit_penalty_check_allow(int sock, const char **reason)
+srclimit_penalty_check_allow(const struct xaddr *peer, const char **reason)
 {
 	struct xaddr addr;
 	struct penalty find, *penalty;
@@ -268,8 +255,9 @@ srclimit_penalty_check_allow(int sock, const char **reason)
 
 	if (!penalty_cfg.enabled)
 		return 1;
-	if (srclimit_peer_addr(sock, &addr) != 0)
-		return 1;
+	if (peer == NULL)
+		return 1;	/* not remote socket? */
+	addr = *peer;
 	if (penalty_exempt != NULL) {
 		if (addr_ntop(&addr, addr_s, sizeof(addr_s)) != 0)
 			return 1; /* shouldn't happen */
diff --git a/srclimit.h b/srclimit.h
index 3e083df44..1841f3be5 100644
--- a/srclimit.h
+++ b/srclimit.h
@@ -19,7 +19,7 @@ struct per_source_penalty;
 
 void	srclimit_init(int, int, int, int,
     struct per_source_penalty *, const char *);
-int	srclimit_check_allow(int, int);
+int	srclimit_check_allow(const struct xaddr *, int);
 void	srclimit_done(int);
 
 #define SRCLIMIT_PENALTY_NONE			0
@@ -38,5 +38,5 @@ void	srclimit_done(int);
 #define EXIT_INVALID_USER	7	/* invalid user supplied */
 
 void	srclimit_penalise(struct xaddr *, int);
-int	srclimit_penalty_check_allow(int, const char **);
+int	srclimit_penalty_check_allow(const struct xaddr *, const char **);
 void	srclimit_penalty_info(void);
diff --git a/sshd.c b/sshd.c
index da2aba936..63299a7bc 100644
--- a/sshd.c
+++ b/sshd.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: sshd.c,v 1.635 2026/09/16 07:47:29 jsg Exp $ */
+/* $OpenBSD: sshd.c,v 1.636 2026/11/25 21:08:49 djm Exp $ */
 /*
  * Copyright (c) 2000, 2001, 2002 Markus Friedl.  All rights reserved.
  * Copyright (c) 2002 Niels Provos.  All rights reserved.
@@ -222,14 +222,11 @@ child_alloc(void)
 
 /* Register a new connection in the children array; child pid comes later */
 static struct early_child *
-child_register(int pipefd, int sockfd)
+child_register(int pipefd, int sockfd, const struct xaddr *peer)
 {
 	int i, lport, rport;
 	char *laddr = NULL, *raddr = NULL;
 	struct early_child *child = NULL;
-	struct sockaddr_storage addr;
-	socklen_t addrlen = sizeof(addr);
-	struct sockaddr *sa = (struct sockaddr *)&addr;
 
 	for (i = 0; i < options.max_startups; i++) {
 		if (children[i].pipefd != -1 ||
@@ -249,9 +246,10 @@ child_register(int pipefd, int sockfd)
 	if ((child->config = sshbuf_fromb(config)) == NULL)
 		fatal_f("sshbuf_fromb failed");
 	/* record peer address, if available */
-	if (getpeername(sockfd, sa, &addrlen) == 0 &&
-	   addr_sa_to_xaddr(sa, addrlen, &child->addr) == 0)
+	if (peer != NULL) {
+		child->addr = *peer;
 		child->have_addr = 1;
+	}
 	/* format peer address string for logs */
 	if ((lport = get_local_port(sockfd)) == 0 ||
 	    (rport = get_peer_port(sockfd)) == 0) {
@@ -586,7 +584,8 @@ should_drop_connection(int startups)
  * while in that state.
  */
 static int
-drop_connection(int sock, int startups, int notify_pipe)
+drop_connection(int sock, const struct xaddr *peer, int startups,
+    int notify_pipe)
 {
 	static struct log_ratelimit_ctx ratelimit_maxstartups;
 	static struct log_ratelimit_ctx ratelimit_penalty;
@@ -605,13 +604,13 @@ drop_connection(int sock, int startups, int notify_pipe)
 	}
 
 	/* PerSourcePenalties */
-	if (!srclimit_penalty_check_allow(sock, &subreason)) {
+	if (!srclimit_penalty_check_allow(peer, &subreason)) {
 		reason = "PerSourcePenalties";
 		rl = &ratelimit_penalty;
 	} else {
 		/* MaxStartups */
 		if (!should_drop_connection(startups) &&
-		    srclimit_check_allow(sock, notify_pipe) == 1)
+		    srclimit_check_allow(peer, notify_pipe) == 1)
 			return 0;
 		reason = "MaxStartups";
 		rl = &ratelimit_maxstartups;
@@ -920,6 +919,7 @@ server_accept_loop(int *sock_in, int *sock_out, int *newsock, int *config_s,
 	const u_char *ptr;
 	char c = 0;
 	struct sockaddr_storage from;
+	struct xaddr peer_addr, *peer;
 	struct early_child *child;
 	struct sshbuf *buf;
 	socklen_t fromlen;
@@ -1149,7 +1149,15 @@ server_accept_loop(int *sock_in, int *sock_out, int *newsock, int *config_s,
 				close(*newsock);
 				continue;
 			}
-			if (drop_connection(*newsock,
+			/*
+			 * Use the peer address returned by accept(); the
+			 * peer may already have reset the connection, in
+			 * which case getpeername() would fail and the
+			 * connection would escape the per-source limits.
+			 */
+			peer = addr_sa_to_xaddr((struct sockaddr *)&from,
+			    fromlen, &peer_addr) == 0 ? &peer_addr : NULL;
+			if (drop_connection(*newsock, peer,
 			    children_active, config_s[0])) {
 				close(*newsock);
 				close(config_s[0]);
@@ -1186,7 +1194,7 @@ server_accept_loop(int *sock_in, int *sock_out, int *newsock, int *config_s,
 			platform_pre_fork();
 			set_nonblock(config_s[0]);
 			listening++;
-			child = child_register(config_s[0], *newsock);
+			child = child_register(config_s[0], *newsock, peer);
 			if ((child->pid = fork()) == 0) {
 				/*
 				 * Child.  Close the listening and

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


More information about the openssh-commits mailing list