* [PATCH net v3] net: fix xfrm_state refcount underflow and leak in skb_ext COW
@ 2026-11-25 2:07 Henry Martin
0 siblings, 0 replies; only message in thread
From: Henry Martin @ 2026-11-25 2:07 UTC (permalink / raw)
To: davem, edumazet, kuba, pabeni, fw, steffen.klassert, herbert
Cc: horms, netdev, security, stable, Henry Martin
skb_ext_maybe_cow() re-acquires the SEC_PATH xfrm_state references for a
copied extension chunk, gated on the source skb's active bit, while the
release side (__skb_ext_put()) is gated on the chunk's
offset[SEC_PATH].
__skb_ext_del(SKB_EXT_SEC_PATH) on a shared chunk (refcnt > 1) only
clears the skb's active bit: the chunk keeps offset[SEC_PATH] and
sp->len, because the remaining sharers still use them. A later
skb_ext_add() COWs such a chunk; the copy carries the SEC_PATH data, but
with the active bit clear no references are taken for it. When the copy
is freed, __skb_ext_put() puts sp->len references that were never
acquired, underflowing the refcount of every xfrm_state in the sec_path.
The state is destroyed while still hashed in the SAD and is dereferenced
by later packet input. On v7.4-rc4, as an unprivileged user:
WARNING: net/xfrm/xfrm_state.c:805 at __xfrm_state_destroy
BUG: KASAN: slab-use-after-free in xfrm_input_state_lookup
...
Freed by task ...:
xfrm_state_gc_task
__xfrm_state_destroy
The MCTP flow slot has the same take/put asymmetry, so fix both slots:
- skb_ext_maybe_cow(): a copy of a slot that exists but is not active
keeps no references (sp->len = 0, flow->key = NULL); they stay with
the old chunk. The slot itself is kept, since skb_ext_add() must not
append a second slot for the same id.
- skb_ext_add(): when it reuses such a slot, the chunk is not shared,
so release the references the slot still owns before reusing it.
Otherwise secpath_set() zeroes sp->len in place and leaks them; this
case predates the COW path.
Nothing passes SKB_EXT_MCTP to __skb_ext_del() today, so the MCTP half
is not reachable yet, but nothing in the code enforces that.
This issue was discovered by Tencent CodeBuddy Security.
Reported-by: zeroday zhao <zerodayzhao@tencent.com>
Fixes: 4165079ba328d ("net: switch secpath to use skb extension infrastructure")
Cc: stable@vger.kernel.org
Signed-off-by: Henry Martin <bsdhenrymartin@gmail.com>
---
This supersedes the one-line patch in the report of 2026-11-05. Eric
Dumazet described this two-part fix and asked me to take it over while he
is out.
Verified on v7.4-rc4 (KASAN, unprivileged user, userns + netns):
unpatched: WARNING at net/xfrm/xfrm_state.c:805, then
BUG: KASAN: slab-use-after-free in xfrm_input_state_lookup
(freed by xfrm_state_gc_task)
patched: the same trigger sequence completes without warning or splat
The skb_ext_add() reuse path needs a stale slot on an unshared chunk; the
reproducer only reaches the COW path, so that part is covered by
inspection. The MCTP slot is not reachable today.
v2 -> v3:
- cover the MCTP slot too, as requested by Florian Westphal
- factor the per-slot reference handling into skb_ext_put_slot(),
skb_ext_put_refs() and skb_ext_copy_refs(), as suggested by Paolo
Abeni, which also drops the forward declaration
- document the invariant above skb_ext_maybe_cow()
---
net/core/skbuff.c | 186 +++++++++++++++++++++++++++++++---------------
1 file changed, 127 insertions(+), 59 deletions(-)
diff --git a/net/core/skbuff.c b/net/core/skbuff.c
index 74e9a96bc5df..dc2e1a39d46b 100644
--- a/net/core/skbuff.c
+++ b/net/core/skbuff.c
@@ -7300,6 +7300,126 @@ struct skb_ext *__skb_ext_alloc(gfp_t flags)
return new;
}
+#ifdef CONFIG_XFRM
+static void skb_ext_put_sp(struct sec_path *sp)
+{
+ unsigned int i;
+
+ if (!sp->len)
+ return;
+
+ for (i = 0; i < sp->len; i++)
+ xfrm_state_put(sp->xvec[i]);
+ sp->len = 0;
+}
+#endif
+
+#ifdef CONFIG_MCTP_FLOWS
+static void skb_ext_put_mctp(struct mctp_flow *flow)
+{
+ if (!flow->key)
+ return;
+
+ mctp_key_unref(flow->key);
+ flow->key = NULL;
+}
+#endif
+
+/* Release the references owned by one slot of an extension chunk. */
+static void skb_ext_put_slot(struct skb_ext *ext, enum skb_ext_id id)
+{
+ if (!__skb_ext_exist(ext, id))
+ return;
+
+ switch (id) {
+#ifdef CONFIG_XFRM
+ case SKB_EXT_SEC_PATH:
+ skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH));
+ break;
+#endif
+#ifdef CONFIG_MCTP_FLOWS
+ case SKB_EXT_MCTP:
+ skb_ext_put_mctp(skb_ext_get_ptr(ext, SKB_EXT_MCTP));
+ break;
+#endif
+ default:
+ break;
+ }
+}
+
+/* Release the references of every slot of an extension chunk. */
+static void skb_ext_put_refs(struct skb_ext *ext)
+{
+ enum skb_ext_id id;
+
+ for (id = 0; id < SKB_EXT_NUM; id++)
+ skb_ext_put_slot(ext, id);
+}
+
+/*
+ * A COW copy carries the data of every slot, but only the slots that
+ * are active on the source skb may own references. For an inactive
+ * slot the references stay with the old chunk.
+ */
+static void skb_ext_copy_refs(struct skb_ext *old, struct skb_ext *new,
+ unsigned int old_active)
+{
+ enum skb_ext_id id;
+
+ for (id = 0; id < SKB_EXT_NUM; id++) {
+ if (!__skb_ext_exist(old, id))
+ continue;
+
+ switch (id) {
+#ifdef CONFIG_XFRM
+ case SKB_EXT_SEC_PATH: {
+ struct sec_path *sp;
+
+ sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH);
+ if (old_active & (1 << id)) {
+ unsigned int i;
+
+ for (i = 0; i < sp->len; i++)
+ xfrm_state_hold(sp->xvec[i]);
+ } else {
+ struct sec_path *new_sp;
+
+ new_sp = skb_ext_get_ptr(new, SKB_EXT_SEC_PATH);
+ new_sp->len = 0;
+ }
+ break;
+ }
+#endif
+#ifdef CONFIG_MCTP_FLOWS
+ case SKB_EXT_MCTP: {
+ struct mctp_flow *flow;
+
+ flow = skb_ext_get_ptr(old, SKB_EXT_MCTP);
+ if (old_active & (1 << id)) {
+ if (flow->key)
+ refcount_inc(&flow->key->refs);
+ } else {
+ struct mctp_flow *new_flow;
+
+ new_flow = skb_ext_get_ptr(new, SKB_EXT_MCTP);
+ new_flow->key = NULL;
+ }
+ break;
+ }
+#endif
+ default:
+ break;
+ }
+ }
+}
+
+/*
+ * A slot that exists in the chunk but is not active must not own
+ * references: they belong to the chunk and are dropped by
+ * __skb_ext_put() when the chunk is freed, or released by
+ * skb_ext_add() when the slot is reused. A COW copy of such a slot
+ * must therefore not take references for it.
+ */
static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old,
unsigned int old_active)
{
@@ -7315,23 +7435,7 @@ static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old,
memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE);
refcount_set(&new->refcnt, 1);
-#ifdef CONFIG_XFRM
- if (old_active & (1 << SKB_EXT_SEC_PATH)) {
- struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH);
- unsigned int i;
-
- for (i = 0; i < sp->len; i++)
- xfrm_state_hold(sp->xvec[i]);
- }
-#endif
-#ifdef CONFIG_MCTP_FLOWS
- if (old_active & (1 << SKB_EXT_MCTP)) {
- struct mctp_flow *flow = skb_ext_get_ptr(old, SKB_EXT_MCTP);
-
- if (flow->key)
- refcount_inc(&flow->key->refs);
- }
-#endif
+ skb_ext_copy_refs(old, new, old_active);
__skb_ext_put(old);
return new;
}
@@ -7387,8 +7491,11 @@ void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id)
if (!new)
return NULL;
- if (__skb_ext_exist(new, id))
+ if (__skb_ext_exist(new, id)) {
+ if (!(skb->active_extensions & (1 << id)))
+ skb_ext_put_slot(new, id);
goto set_active;
+ }
newoff = new->chunks;
} else {
@@ -7410,31 +7517,6 @@ void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id)
}
EXPORT_SYMBOL(skb_ext_add);
-#ifdef CONFIG_XFRM
-static void skb_ext_put_sp(struct sec_path *sp)
-{
- unsigned int i;
-
- if (!sp->len)
- return;
-
- for (i = 0; i < sp->len; i++)
- xfrm_state_put(sp->xvec[i]);
- sp->len = 0;
-}
-#endif
-
-#ifdef CONFIG_MCTP_FLOWS
-static void skb_ext_put_mctp(struct mctp_flow *flow)
-{
- if (!flow->key)
- return;
-
- mctp_key_unref(flow->key);
- flow->key = NULL;
-}
-#endif
-
void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id)
{
struct skb_ext *ext = skb->extensions;
@@ -7449,14 +7531,7 @@ void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id)
if (refcount_read(&ext->refcnt) > 1)
return;
-#ifdef CONFIG_XFRM
- if (id == SKB_EXT_SEC_PATH)
- skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH));
-#endif
-#ifdef CONFIG_MCTP_FLOWS
- if (id == SKB_EXT_MCTP)
- skb_ext_put_mctp(skb_ext_get_ptr(ext, SKB_EXT_MCTP));
-#endif
+ skb_ext_put_slot(ext, id);
}
EXPORT_SYMBOL(__skb_ext_del);
@@ -7471,14 +7546,7 @@ void __skb_ext_put(struct skb_ext *ext)
if (!refcount_dec_and_test(&ext->refcnt))
return;
free_now:
-#ifdef CONFIG_XFRM
- if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH))
- skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH));
-#endif
-#ifdef CONFIG_MCTP_FLOWS
- if (__skb_ext_exist(ext, SKB_EXT_MCTP))
- skb_ext_put_mctp(skb_ext_get_ptr(ext, SKB_EXT_MCTP));
-#endif
+ skb_ext_put_refs(ext);
kmem_cache_free(skbuff_ext_cache, ext);
}
--
2.43.7
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-25 2:07 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 2:07 [PATCH net v3] net: fix xfrm_state refcount underflow and leak in skb_ext COW Henry Martin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox