net: fix xfrm_state refcount underflow and leak in skb_ext COW
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>
Link: https://patch.msgid.link/20261125020718.261041-1-bsdhenrymartin@gmail.com
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
1 file changed