From 85631013fbb28dea7e9c89e5a3b51684defbdd11 Mon Sep 17 00:00:00 2001
From: Valery Kharseko <vharseko@3a-systems.ru>
Date: Sat, 11 Jul 2026 07:41:55 +0000
Subject: [PATCH] [#728] Reject TCP self-connects in replication connect paths (#729)

---
 opendj-server-legacy/src/main/java/org/opends/server/util/StaticUtils.java |   21 ++++++++++++++++++++-
 1 files changed, 20 insertions(+), 1 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/util/StaticUtils.java b/opendj-server-legacy/src/main/java/org/opends/server/util/StaticUtils.java
index fd38a8c..2147926 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/util/StaticUtils.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/util/StaticUtils.java
@@ -13,6 +13,7 @@
  *
  * Copyright 2006-2010 Sun Microsystems, Inc.
  * Portions Copyright 2011-2016 ForgeRock AS.
+ * Portions Copyrighted 2026 3A Systems, LLC.
  */
 package org.opends.server.util;
 
@@ -1319,7 +1320,25 @@
     return true;
   }
 
-
+  /**
+   * Indicates whether the provided connected socket is connected to itself,
+   * i.e. its local and remote endpoints are identical.
+   * <p>
+   * Connecting to a local port from the TCP ephemeral range while nothing
+   * listens on it can make the kernel pick that very port as the local port
+   * of the connecting socket: TCP simultaneous open then "establishes" the
+   * connection to itself (observed on Linux). Such a socket occupies the
+   * listen port its target service is about to bind, so callers retrying
+   * connections to a temporarily stopped service must detect and close it.
+   *
+   * @param socket a connected socket
+   * @return true if the socket is connected to itself
+   */
+  public static boolean isSelfConnection(Socket socket)
+  {
+    return socket.getLocalPort() == socket.getPort()
+        && socket.getLocalAddress().equals(socket.getInetAddress());
+  }
 
   /**
    * Returns a lower-case string representation of a given string, verifying for null input string.

--
Gitblit v1.10.0