From patchwork Fri Sep 18 14:27:27 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cengiz Can X-Patchwork-Id: 2308109 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ubuntu.com (client-ip=185.125.189.65; helo=lists.ubuntu.com; envelope-from=kernel-team-bounces@lists.ubuntu.com; receiver=patchwork.ozlabs.org) Received: from lists.ubuntu.com (lists.ubuntu.com [185.125.189.65]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4hmr1654Hsz1xwp for ; Sat, 19 Sep 2026 10:27:38 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=lists.ubuntu.com) by lists.ubuntu.com with esmtp (Exim 4.86_2) (envelope-from ) id 1x7ivV-0005t2-N1; Sat, 19 Sep 2026 00:27:33 +0000 Received: from smtp-relay-internal-0.internal ([10.131.114.225] helo=smtp-relay-internal-0.canonical.com) by lists.ubuntu.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1x7ivT-0005s0-T5 for kernel-team@lists.ubuntu.com; Sat, 19 Sep 2026 00:27:31 +0000 Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id B0BED3F52D for ; Sat, 19 Sep 2026 00:27:31 +0000 (UTC) Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-49fcd86b8d3so1422155e9.3 for ; Fri, 18 Sep 2026 17:27:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789777651; x=1790382451; 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=H4XxYmFlok8kxW3ikshC1J7Z9URTkqqCZFJKQQfuhYA=; b=ifgPU+85QZLe3yp3O/bwA7jGWsGgpzScIBklPfDuDwSmbpCdZrUhobUHAKNfs5zPWi dZN9ARjpGVKJ/KasF7CMWHNKV+/1q2ANc05XzWNJCqNEEU7V5sPq5qjcP06UMWelehV9 L2gqaq+LtuZWn40q519Ymo+kux2GN5AmjLiTanwO2ldYqf3EVe0Uti2NdRraiLHTiQiJ Q7Ku018jYXXEicuC7hNbflrN/xcGhNj3RlMKbxh8LUw/8mjOD8r1OjgnXIPOxzUj3ZOR rfC3uS3SZflZzHUCeFzA6V6UmF8q6IsggIw6+zyrA39KjtqvBqYzF4TYYwjUKeQDXkX0 nCsQ== X-Gm-Message-State: AFuF++lRqol+SKWNmmIHMqUJKJTgtxdvJHqhl8FE8wLYu8z2/7N8Bd+G uyLkSI4VsfF8psMI9cEJlp1K4VxMYCkws3JcM9aqzkdD3uSyXdtXl0UpKytg0pVww170qFY/kmU +flHz8Fl4Aqanx8JLzxPh8Bz2Vysb1g7DbbzFOutfn4kR0NB61cNhk4j+R6ttQe1EZ8w81BTBtE OsW/6c6SYuoedsxg== X-Gm-Gg: AYBFou3XzfKQgHWegTQ/nHfBPaxArtKToohzeqvMnQvC7lHBseZE5F++isiemIt9Cva Bmw9PA1GxdY2RZUOzBNc+gspCzuL7rENq2IrSnaJvJB6gQWeYhydLOvv1vihijzMFzL2TEHqYle 9FaS189zGlQr40BuD+3mBaA4KuVuvQZB/K7sLvRnFl+mFfRZC4cERq/jieIXf25Rfs+gLCs78bx w8SFKLug20krtjxQODwOd/n684UhJRzSn3NyoXUQ9XSz5Ee46vcItbVyywEUf25CNjBkGKUf4wQ S06k36kgQlA13S3g05ja793ofLZrE6IIGo+48npixpCct0SQ0gxUZ18k00ugARsFvf1WBCCF1Z0 9v39AatlqmwObwMOxDwXooPNNLzw6AqrRhVww7pdBVPeXInHVma41ESJcN7B+hdYTzDjgG6Vb0t qqAuZapsrXUw== X-Received: by 2002:a05:600c:3588:b0:49c:d26a:cf70 with SMTP id 5b1f17b1804b1-49fcc3772bamr38530225e9.15.1789777651275; Fri, 18 Sep 2026 17:27:31 -0700 (PDT) X-Received: by 2002:a05:600c:3588:b0:49c:d26a:cf70 with SMTP id 5b1f17b1804b1-49fcc3772bamr38529935e9.15.1789777650858; Fri, 18 Sep 2026 17:27:30 -0700 (PDT) Received: from kybele-google.c.canonical-req-10660.internal (50.34.62.34.bc.googleusercontent.com. [34.62.34.50]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-49fc6ca43edsm94800725e9.0.2026.09.18.17.27.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Sep 2026 17:27:28 -0700 (PDT) To: kernel-team@lists.ubuntu.com Subject: [SRU][J][PATCH 1/1] xfrm: Duplicate SPI Handling Date: Sat, 19 Sep 2026 00:27:27 +0000 Message-ID: X-Mailer: git-send-email 2.53.0 In-Reply-To: References: MIME-Version: 1.0 X-BeenThere: kernel-team@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Kernel team discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Cengiz Can via kernel-team From: Cengiz Can Reply-To: Cengiz Can Errors-To: kernel-team-bounces@lists.ubuntu.com Sender: "kernel-team" From: Aakash Kumar S The issue originates when Strongswan initiates an XFRM_MSG_ALLOCSPI Netlink message, which triggers the kernel function xfrm_alloc_spi(). This function is expected to ensure uniqueness of the Security Parameter Index (SPI) for inbound Security Associations (SAs). However, it can return success even when the requested SPI is already in use, leading to duplicate SPIs assigned to multiple inbound SAs, differentiated only by their destination addresses. This behavior causes inconsistencies during SPI lookups for inbound packets. Since the lookup may return an arbitrary SA among those with the same SPI, packet processing can fail, resulting in packet drops. According to RFC 4301 section 4.4.2 , for inbound processing a unicast SA is uniquely identified by the SPI and optionally protocol. Reproducing the Issue Reliably: To consistently reproduce the problem, restrict the available SPI range in charon.conf : spi_min = 0x10000000 spi_max = 0x10000002 This limits the system to only 2 usable SPI values. Next, create more than 2 Child SA. each using unique pair of src/dst address. As soon as the 3rd Child SA is initiated, it will be assigned a duplicate SPI, since the SPI pool is already exhausted. With a narrow SPI range, the issue is consistently reproducible. With a broader/default range, it becomes rare and unpredictable. Current implementation: xfrm_spi_hash() lookup function computes hash using daddr, proto, and family. So if two SAs have the same SPI but different destination addresses, then they will: a. Hash into different buckets b. Be stored in different linked lists (byspi + h) c. Not be seen in the same hlist_for_each_entry_rcu() iteration. As a result, the lookup will result in NULL and kernel allows that Duplicate SPI Proposed Change: xfrm_state_lookup_spi_proto() does a truly global search - across all states, regardless of hash bucket and matches SPI and proto. Signed-off-by: Aakash Kumar S Acked-by: Herbert Xu Signed-off-by: Steffen Klassert (backported from commit 94f39804d891cffe4ce17737d295f3b195bc7299) [bot_kybele: Adapted to this tree: xfrm_alloc_spi() has no extack param (dropped NL_SET_ERR_MSG), replaced get_random_u32_inclusive() with low + prandom_u32() % range, and XFRM_STATE_INSERT() with hlist_add_head_rcu(); the new xfrm_state_lookup_spi_proto() applied verbatim.] CVE-2025-39797 Assisted-by: kybele:claude-opus-4.8 Signed-off-by: Cengiz Can --- net/xfrm/xfrm_state.c | 67 ++++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 26 deletions(-) diff --git a/net/xfrm/xfrm_state.c b/net/xfrm/xfrm_state.c index 25503d069a64..66d53cefa94f 100644 --- a/net/xfrm/xfrm_state.c +++ b/net/xfrm/xfrm_state.c @@ -1306,6 +1306,26 @@ struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi, } EXPORT_SYMBOL(xfrm_state_lookup_byspi); +static struct xfrm_state *xfrm_state_lookup_spi_proto(struct net *net, __be32 spi, u8 proto) +{ + struct xfrm_state *x; + unsigned int i; + + rcu_read_lock(); + for (i = 0; i <= net->xfrm.state_hmask; i++) { + hlist_for_each_entry_rcu(x, &net->xfrm.state_byspi[i], byspi) { + if (x->id.spi == spi && x->id.proto == proto) { + if (!xfrm_state_hold_rcu(x)) + continue; + rcu_read_unlock(); + return x; + } + } + } + rcu_read_unlock(); + return NULL; +} + static void __xfrm_state_insert(struct xfrm_state *x) { struct net *net = xs_net(x); @@ -2093,10 +2113,8 @@ int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high) unsigned int h; struct xfrm_state *x0; int err = -ENOENT; - __be32 minspi = htonl(low); - __be32 maxspi = htonl(high); + u32 range = high - low + 1; __be32 newspi = 0; - u32 mark = x->mark.v & x->mark.m; spin_lock_bh(&x->lock); if (x->km.state == XFRM_STATE_DEAD) @@ -2108,33 +2126,30 @@ int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high) err = -ENOENT; - if (minspi == maxspi) { - x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family); - if (x0) { - xfrm_state_put(x0); + for (h = 0; h < range; h++) { + u32 spi = (low == high) ? low : low + prandom_u32() % range; + newspi = htonl(spi); + + spin_lock_bh(&net->xfrm.xfrm_state_lock); + x0 = xfrm_state_lookup_spi_proto(net, newspi, x->id.proto); + if (!x0) { + x->id.spi = newspi; + h = xfrm_spi_hash(net, &x->id.daddr, newspi, x->id.proto, x->props.family); + hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h); + spin_unlock_bh(&net->xfrm.xfrm_state_lock); + err = 0; goto unlock; } - newspi = minspi; - } else { - u32 spi = 0; - for (h = 0; h < high-low+1; h++) { - spi = low + prandom_u32()%(high-low+1); - x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family); - if (x0 == NULL) { - newspi = htonl(spi); - break; - } - xfrm_state_put(x0); - } - } - if (newspi) { - spin_lock_bh(&net->xfrm.xfrm_state_lock); - x->id.spi = newspi; - h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family); - hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h); + xfrm_state_put(x0); spin_unlock_bh(&net->xfrm.xfrm_state_lock); - err = 0; + if (signal_pending(current)) { + err = -ERESTARTSYS; + goto unlock; + } + + if (low == high) + break; } unlock: