Article View: linux.kernel.netdev
Article #12244[PATCH net-next v2 1/2] tcp: avoid false bind exhaustion with odd-sized port ranges
From: Florin Badita-Nistor
Date: Fri, 06 Nov 2026 17:14
Date: Fri, 06 Nov 2026 17:14
57 lines
13528 bytes
13528 bytes
inet_csk_find_open_port() rounds the range length down to an even
number but keeps the original exclusive upper bound for wrapping.
For an odd-sized range, the two scans can therefore miss a valid port
and return EADDRINUSE while an eligible port remains free.
For example, restrict a TCP socket to IP_LOCAL_PORT_RANGE [40000, 40002]
and hold explicit binds on 40000 and 40001. bind(..., 0) always fails,
although an explicit bind to 40002 succeeds. Both IPv4 and IPv6 TCP and
MPTCP are affected. A namespace-wide odd-sized ip_local_port_range can
also trigger the failure without a per-socket range setting.
Keep the full length for the scan bound and wrapping. The two scans
then cover every candidate in an odd-sized range, with at most one
repeated candidate when exhausted. Even-sized candidate sequences
remain unchanged.
For odd-sized ranges, subtracting an odd remaining value when wrapping
flips parity. The first scan can therefore also select offsets of the
parity preferred by __inet_hash_connect(). Namespace-wide ranges with
same-parity endpoints already trigger a sysctl warning, while connect()
uses step 1 for a per-socket IP_LOCAL_PORT_RANGE.
This only fixes bind() allocation. The rounding in __inet_hash_connect()
for namespace-wide ranges is unchanged, so connect() can still miss a
port in an odd-sized namespace range.
The issue was triggered while developing port-range selftests with
Codex assistance, then confirmed using userspace reproducers on
unmodified kernels. No kernel instrumentation or fault injection was
needed. Booted x86-64 KVM kernels were tested against the regression
selftest, the full ip_local_port_range suite, and a Go net.Listen
reproducer. The Go reproducer fails 100/100 times per IP version before
the fix and 0/100 afterwards with a three-port namespace range and two
listeners occupying its first two ports.
Fixes: ea8add2b1903 ("tcp/dccp: better use of ephemeral ports in bind()")
Signed-off-by: Florin Badita-Nistor <baditaflorin@gmail.com>
---
net/ipv4/inet_connection_sock.c | 2 --
1 file changed, 2 deletions(-)
diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c
index e56beec43..755fcea35 100644
--- a/net/ipv4/inet_connection_sock.c
+++ b/net/ipv4/inet_connection_sock.c
@@ -343,8 +343,6 @@ inet_csk_find_open_port(const struct sock *sk, struct inet_bind_bucket **tb_ret,
low = half;
}
remaining = high - low;
- if (likely(remaining > 1))
- remaining &= ~1U;
offset = get_random_u32_below(remaining);
/* __inet_hash_connect() favors ports having @low parity
--
2.51.0
Technical Details
Message-ID:
<20261106171442.31811-2-baditaflorin@gmail.com>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-358080-linux-kernel-netdev=dewy.ruReferences:
<20261106171442.31811-1-baditaflorin@gmail.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-358080-linux-kernel-netdev=dewy.ru
From: baditaflorin@gmail.com (Florin Badita-Nistor)
Newsgroups: linux.kernel.netdev
Subject: [PATCH net-next v2 1/2] tcp: avoid false bind exhaustion with odd-sized port ranges
Date: Fri, 6 Nov 2026 19:14:41 +0200
Organization: Usenet Network
Message-ID: <20261106171442.31811-2-baditaflorin@gmail.com>
References: <20261106171442.31811-1-baditaflorin@gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
logging-data="3931069"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:4mqRyfsUUem91d1pR4dyfBxK/+U= sha256:A/YDkKpXqcNpJJeqZNGD6FfOtDeLZhqSG+7iT3ZauCs=
sha1:eeaRSeJQqsGgEWH4sOdki8hJY7Q= sha256:Ra0w11OgUk5DKYa3S/N6dzHghQcvscSXJx/EyHNapRQ=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
by mail.dewy.ru (Postfix) with ESMTP id 2A4F51132B3; Fri, 6 Nov 2026 20:15:48 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.049
X-Spam-Status: No, score=-3.049 tagged_above=-9999 required=6.31
tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
DKIM_VALID_AU=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.001,
FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249,
MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
header.d=gmail.com
X-Received: from mail.dewy.ru ([127.0.0.1])
by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id Vzcnb0XK0aNm;
Fri, 6 Nov 2026 20:15:45 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-358080-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN>
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=gmail.com
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
by mail.dewy.ru (Postfix) with ESMTPS id EAA361132A2; Fri, 6 Nov 2026 20:15:36 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
by sea.lore.kernel.org (Postfix) with ESMTP id DC11F31587; Fri, 6 Nov 2026 17:14:49 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
t=1793985289;
b=jGzLIZY93KaoY/c4j4CzoliQPJIKM8Efv32xH1klqGR4PKgH7NnQLN7DJ+OfuJnPBv/i
m7PCbhMOLazL53fKl1rATVRUZf0wiEvut5lAaS+xkdWvVXoE62D/eC+DGmsM/7mTphWYY
p08BChvwIgGsEpbsRQmiQ8hdWzL18kOXSAsPj8QNAL156QOYmRi4JlveN0ifJVO5wwCKo
Z9Rh28upuqsU+BeeUQCy1kTEsbRcfuz1aX5whrz974W79o0UUniS53xwxKsOR85365qa3
z6pd+mjzLF3xzv2VRISPIzCsufjOJTV3jw0SFdgVS8apFX7Qa6dQy2jUfadTlnn9fug==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1793985289;
h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:
X-Google-DKIM-Signature:X-Gm-Message-State:X-Gm-Gg:X-Received:
Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:
References:X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe:
List-Unsubscribe:List-Owner:Precedence:MIME-Version:
Content-Transfer-Encoding;
bh=npSSNgqjeDjy8yeV7j2uv0PIl6Tg9TTaL41tLSP/LLk=;
b=VtUmkHQ5gipL9/dRzbVpq1GbYxiWq0gdMGGjthKCcINHFD36JqfulwKWTaGFzCbX0Gza
z3spXahBmHpYnR7IFBe8HdyIvZ6C6vUpJsKzSlfWaiZt2Rx5QgAPmPNSE3VP8RcaTOhhf
IBf939boy5YAj18TC0XZ8atkbuLMJj5jyhfm7A8jRkwrXzTYg7RJJ4K4NxlHFMpZnnp30
6GdTjkKfghYZg4GrjHGvTHROpp6nbog91IfBcfuHIGl0CpGjsbYMtGfZGIho1IxL4lHnM
YixTsi9TeCxYa9hK0uzwIRaK5t1+u5Nw33e9jMp7N8OJSMRVM4e9iOdJVA0tkJfor/Q==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org;
dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=fzybGRN7;
dmarc=pass header.from=gmail.com;
spf=pass smtp.mailfrom=gmail.com;
arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
by smtp.subspace.kernel.org (Postfix) with ESMTP id 220E6606A326;
Fri, 6 Nov 2026 17:14:49 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=fzybGRN7
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.44
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
t=1793985288;
b=p8ybR7tG1pUgJJ7eg2I9HhDg0yl8wTAGADTUL63Qbc4SBJ5etBplNQfqdepwowXHZFUw
y4VQMSBkUShbD9CudzzTo/QIbzJOflvknA95lnBGQ+zZnp99Uvm5i1bEVXo7SJon7T8TT
DJQsIO6ODBGWbPU+4L4Hrq77RzxTedUB74e6744gMTShQu5MGx4LYpVgW32eu5v8BWv9l
+3TJNBFJRkpg1GJDDQK3EcpF5uvOi5mMv0AcrWL9pwFXFW3O4o8hu6wUDHOCUFViahoIr
jiPIlX5qrRrlIb3g96xKWqFHQb6GagA5KiqbdEqRBDf7K9aiQT0CO6t0DU8TwQpDTAw==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1793985288;
h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature:
X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject:
Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version:
Content-Transfer-Encoding;
bh=npSSNgqjeDjy8yeV7j2uv0PIl6Tg9TTaL41tLSP/LLk=;
b=F3pHRzTWn9t0dLRStgOqZGX3OnuFRzvRw7b/cJwHpB7IKwlDLCsMAnyHobMRFFO4owhm
Kx45mQr7LkX8N6qNQbFLHfoO46dBU5dWuqSiYzAdiq1wAot6hjWwi9aIUwksw0TUCI/7h
INbtZr5DJ1JVnpzb66aZWodZHDs4lVynEHWA/sc79F9cwPoAYVdWoKjymHJEnL9fYi7BO
HXHFTq/P4YFo1aRifacYvIAGcDf9505085NVakXCQIvz9jha/v9NfCNde4woKYuOwIAsb
68MZrXwzvgpju2AlmQdHqnStPIX6cKNfF9XshbeltBzDyZUSXO9Nw0V9GIKly9RU6Bg==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org;
dmarc=pass header.from=gmail.com;
spf=pass smtp.mailfrom=gmail.com;
dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=fzybGRN7;
arc=none smtp.remote-ip=209.85.128.44
X-Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44])
(using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)
key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256)
(No client certificate requested)
by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6B0A8606A337
for <netdev@vger.kernel.org>; Fri, 6 Nov 2026 17:14:47 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6B0A8606A337
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com
X-Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-4a2d6d01ecbso5733385e9.0
for <netdev@vger.kernel.org>; Fri, 06 Nov 2026 09:14:47 -0800 (PST)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20260707; t=1793985286; x=1794590086; darn=vger.kernel.org;
h=content-transfer-encoding:mime-version:references:in-reply-to
:message-id:date:subject:cc:to:from:from:to:cc:subject:date
:message-id:reply-to:content-type;
bh=npSSNgqjeDjy8yeV7j2uv0PIl6Tg9TTaL41tLSP/LLk=;
b=fzybGRN7Vrij7P5ZSgvy+kjz2qjkPQLHTUD+avlpUTUi+3GXQwFtpab7vPsfLxMvY/
7G/t5jTCVR6KSoLIERkw6KsBaX5X1B017xkHIFQBVTi9QwVMS6d4O/GJE8WjL0w8fQyN
F6lpO994JG1Saksrnbk0vIO01qesDJ1IK2z4astuNkvPORb7ElXGnXztH0yt9DiD7DNL
HajcbBhwT53x0XMnEfNWA0uzRagFhJR+bXvl3QUoUD7RpAPMrRyHGLF4fesvG3AaHIAw
IJHdHsAWxj80QNhhZ5VxyoqSS+LrZBMsjhhc0SQ/Y8bYjyxAi31m9TsdllbkzSolTel0
YSvQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20260707; t=1793985286; x=1794590086;
h=content-transfer-encoding:mime-version:references:in-reply-to
:message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from
:to:cc:subject:date:message-id:reply-to:content-type;
bh=npSSNgqjeDjy8yeV7j2uv0PIl6Tg9TTaL41tLSP/LLk=;
b=B03ofQAtttkipYhbgMoafeVXcJdRt0XBL1eavm7WPPUoUuXQdY+nL7N/N5pS190UTe
2LjVbCxAK5s6UHUOUbsPltDbsWNamGnoTPYAjZxLo4hLIxwhkNzP//QWUyMCfz5aG+m/
PEEy/DmqKE9gzgjGhHmx1fmSWI9qMvOCr+h/9n/MHd4jOaDbhvPqKj1CczqgIIjc7ffm
ROPeucuGPQISX2sUPC7zptCJy12Z0ByyLDl5cak0QJmX82gDrPjYAYBj8hfMHht/ZCTH
hi4z3orZWMnKLqliOS9J1E8Kvg5nmo7fSXdAcdlPfj4vdu2Dg47CXsWl6w70wvqMAqtb
9OXw==
X-Gm-Message-State: APuU4Z/R479uumD1GAdmWLNPQJ3DiTp7fBJCbfZfXErtZizLA9F1G0SA
Q1zi2KBOOcl4y68ZMEZ7IZ14h7QxLHYND8ZOgTAORvxm7ZuUmkDrXDgf4Bpy6FJz
X-Gm-Gg: AYBFou3JJNgw2JizgTiyE7zBVvltrDdPPB81JSbyzLHBTBiGs/9gjidKSD9ms6nYsnJ
JH2tqGKmDcMNMoCprUqfnq7bTc+wnRdFkNEuyJ8KNf+jGDm4nprrUzgTFjFQVIT86gsrxHIeIFq
Z+2+z3uLLQSfu0YJXi50OLJpYRxHn7JSbyG0KrSOPo8EohHhNVvlDQ9bM1KxInzJHpklV3ru0AV
NoZKuik9PkZB6yBxf31wlp0UDysIB1+0z1g9ZZB2QO6aMpypYEau5l9YTkLfDQ/9KQkJiWsYeOO
MtkAw7QSQX0ezcQSVFCLsU6eD+6jg5KSJtTwFdbcs5++FQ0dYaYzE8kSZpAjbcL9q51GPOt8yxF
jt1ihA6yfl0wEGhf9ONolBEM5NGq2K4KgRj2ErVcS8/FltNFsjtTLIqXj8+jqCIGRPjLjCkEsLM
4YJvsI2F8BKZt4daBpfFCw63ymSchtm/5sBNa9vFfo8FuQ/64AEk9pB5Fw8UjTvUtjBDxS/bgkV
xdyc+ebbbz/2AGX0T+2h8g1ugwEjQMut5QxXFcHGhSBvaGCr50A3KI2cEPg1C90ZxkY+NHsW3i4
QEV2PubWK/igPwhgvYWBJVBeRfA7Hd/FouDTH5SrbEe8cxFOX+kudt9M4UD5Yh9zE0AvJx4+7zI
TFGMTNTU4szFYa9P74WHd0Y+6UicjB+AvayNFQQmw5hMQWhbdJV0AobMoE3ZMfQO9HWDsQZF1P3
NR4gflks7kf7eBjV5m4G4+pZdOdpfbxeRItx3NeeFeVxGbquJCzSHlsm/zlEktSA==
X-Received: by 2002:a05:600c:c172:b0:4a3:59c9:a747 with SMTP id 5b1f17b1804b1-4b6df5e88admr49637345e9.17.1793985285709;
Fri, 06 Nov 2026 09:14:45 -0800 (PST)
X-Received: from localhost.localdomain ([90.95.39.175])
by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4b571f46489sm148071985e9.3.2026.11.06.09.14.44
(version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256);
Fri, 06 Nov 2026 09:14:45 -0800 (PST)
X-Cc: edumazet@kernel.org,
ncardwell@google.com,
kuniyu@google.com,
davem@davemloft.net,
kuba@kernel.org,
pabeni@redhat.com,
horms@kernel.org,
shuah@kernel.org,
linux-kernel@vger.kernel.org,
linux-kselftest@vger.kernel.org
X-Mailer: git-send-email 2.51.0
X-In-Reply-To: <20261106171442.31811-1-baditaflorin@gmail.com>
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list