Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.224.44 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796095331; b=c8qd14B4Az/0ucgGJGTgKlYm2Jw0PjRTaSJROQpVDZ7jPHPNP4XIweUFXoqiptpDNnlF mx3GoAyAB46D/lc00iM9OGVYsP3M1o5OY0oopvIazRpGaBBfz0wAWJZd1iiQDc07xsaxv jFSqM1YlnBwsJtbKn4oxqLliMw8jbXZ/FrTWOG6D/0oJaI9rvC8C7JFqT66sZoovUpSS8 CYpt5/ZzBQCGnKClHk9YYVf39PE1Zr2/cz8u3O5JV+DVMYkuwHyIlzGfOtl+wLxko7/3r WwZT1S5VDS7rAH0Jz2aV9CFvOTYI2sKnhMGN6uZqtLUeHM+u3lDAwnealfGGvd8lY8A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796095331; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted: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=+vmg5pLH/ScgkE1vtWRINxqv1x8zsdq4xGKHZeU61Iw=; b=dDwZV4kMhwRqsh0YX1CcIwP4GNEJB7PK9H0kFwQmJpbCR3OAflunlU834HR2gJNP3Y48 XZJHz2+8ORyjpw6p8Wb92OmQPaHz+a/Rys0XhZAGFSg2Q3z/VZf5QUSnEtBCGSrPhJn9g h+yzWDu57hGpboJ5Tb56mHDFwnH1mPosGX9kiauFLLmACy5f8Z0oIHVypf3bwp2GoFfQn +uBrOXcWsFyWHeQHiIYzunWx3zKA55fJgw1SaxvXaNMLTgn7JUvVe2X20anYefgX6PQTT JeyDWvih4X6R3CgSezF9IwNmXT8hOiCktc+NG+q9knQhyospWC9Q+sSAurlVcrOtS/A== 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=baDpCwD2; arc=none smtp.remote-ip=74.125.224.44 Received: from mail-yx1-f44.google.com (mail-yx1-f44.google.com [74.125.224.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 B8229606A337 for ; Tue, 1 Dec 2026 03:22:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B8229606A337 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=baDpCwD2 Received: by mail-yx1-f44.google.com with SMTP id 956f58d0204a3-67da564d9f2so14556495d50.1 for ; Mon, 30 Nov 2026 19:22:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796095330; x=1796700130; darn=lists.linux.dev; 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=+vmg5pLH/ScgkE1vtWRINxqv1x8zsdq4xGKHZeU61Iw=; b=baDpCwD2FJsK8n88VWrd+zutDkcbLhp9IcBrG16odrlTMHHemZ/nKM5dPQOj1Kpfxk OLn6r8I/zXGxJ3SHjiRjzgVjOb+9RjeXItXPAKJyH9KUvcu8V2yCwTmGkk6jJDDT8z3T QS//jDX2Yrja//xH/oV9vY1Pqxi4FR8g7K5SEyOtuggkL8T0AqOpTrXfSGveSQS4T8Q4 jufJ9AyDuF0X9wnQ2Dvqf1sRasPKmrXY1pkZ9KY9NuuTotx7bjNGd6dS0sNikdgn2Eeb 9LgRC6Jun9thl/GLONrkpRn+iktkTjIKB7Gs/hlTb7Ig5QtLsDhrFOmwrN3bDjVwqRBV CLAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796095330; x=1796700130; 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=+vmg5pLH/ScgkE1vtWRINxqv1x8zsdq4xGKHZeU61Iw=; b=nxB6zgVH/8X0Sz7iqahiukZZmgk18VSm9qqcth5LjtQ5Cx322r6MYbpgbgGGC5iXQL AAIP8Tl1tZIS7BIKli5DIN+5iWjcEfC9aZGlfh27OUQ6+DGC3halZNMG9Sh5E0Pc9AhI QREGcx/xSkiOCqjcrCAkBRS5KycPkDsJ6UmValQ9LzqRKRLJQ7SVSRuTFZQ/RZBXUXi0 v8lyG3q0AnWZ1zdYUWygF5PPyhZsruhYbKIOmDqGnq+sp5OmlqJdBvTS5QUmHuC85Rt9 5xtO6yz06j+1g7gMhAKT50CYjIHsI3oCLaUE7m9IdavIX6qVusz18SWNG6PIXLzePQRq 8CHA== X-Forwarded-Encrypted: i=1; AL+hErFIEh0JAlpfEuXcAvrGszMe9sThcuBR6tWUKKspeOGF64HkBAmQgb8BYO9H/1V3JjwaS9kVDQ==@lists.linux.dev X-Gm-Message-State: APuU4Z8wjb+iZ/xIBR9OEWCI6jfSD9IwqQV/HgGnWGr27gWTBHvvpyOt 3bw5LCFFma2wBnWHlht2IAO6d0rSrKN6X3RlURSa7ZCmVOazLnDQAQsH X-Gm-Gg: AZFdTZeTIU4ODvP/0w+AEqwuc86yV+5dlql3A2M+qrEo5lmofLPdiBnWBIIkXk1PtkI nh8Wvi4jUQGyT1OMpd5n9fB/T8Fi871kiQGICplD2lwC4wUZxod42q7obIwFZo0YC6QO5Yn3B8H 5UpAipXyIAVkMXl4XDbFj/qnZ+r1uRUE+dP2H5fQ/MXtyoTGsksUBSkAWZH2CzVGTDCFMOaowpn 7c6IyM3EHYRh1pox/5/HGjFKj7jbK++GAyFePyWywvc/izxR6Oldh6FxFFNqk8yGkxNYEf8c11G D5r3To7VFNSuzTuUK04P6LsDVoP4uXCZhKtA0m5ziyzCMVW9UK1HiCrwYf4VR0b8lacpAwTCDPj KZvwHXrgHPIGjAaW2WLErD/tmCmkEKxMOLqULxUaiy8dDVxNh+fC0hPVMA+7DdKk+UbM0zSxNWo 8V2l0FOefy4Pu5y0yxtfhVn25cST6RWxI1AfIdxzm3PhJMR0oXZ1HAB+VrY4UNHgSpE7mC5w/ZF 797a4AKsxMpVs3ocdistmkKxjpDB4qBdZwNG1T0gnkXaOthyo+T+gBeAT8LhN6RxXh20WkWx9gb 0AJGzD1J5kX0ASiWtKQeFsGvr1wc5WSe7XooGWD4z48FkQ2y1DD00TNOt2NyQ+UjiJrG7s0dcg= = X-Received: by 2002:a05:690e:441c:b0:67d:bfa8:5d7a with SMTP id 956f58d0204a3-67df4493953mr9712258d50.91.1796095329590; Mon, 30 Nov 2026 19:22:09 -0800 (PST) Received: from VPN-156-19.vpn.usf.edu ([2607:fe50:0:f802::4046]) by smtp.gmail.com with ESMTPSA id 956f58d0204a3-67df30e4407sm16484916d50.0.2026.11.30.19.22.06 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 30 Nov 2026 19:22:09 -0800 (PST) From: Juan Agustin Niccolini To: Steffen Klassert , Herbert Xu , "David S . Miller" Cc: netdev@vger.kernel.org, ipsec@lists.linux.dev, Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Simon Horman , Christian Hopps , Antony Antony , Sabrina Dubroca , Shaomin Chen , Roshan Kumar Subject: [PATCH ipsec 1/4] xfrm: iptfs: free held packets on SA delete to fix a netns UAF Date: Mon, 30 Nov 2026 22:21:57 -0500 Message-ID: <20261201032200.37030-2-juanagusnicco@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261201032200.37030-1-juanagusnicco@gmail.com> References: <20261201032200.37030-1-juanagusnicco@gmail.com> X-Mailing-List: ipsec@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit IP-TFS keeps received packets beyond the call that delivered them: the reorder window keeps out-of-order packets until the missing one arrives or the drop timer fires, and an inner packet that spans several outer packets is kept in ra_newskb until it is complete. A held packet keeps its SA alive through the secpath, and IP-TFS frees what it holds only when the SA is destroyed, which cannot happen while the packets hold it. So when the network namespace is dismantled, the SA is deleted but survives, and the drop timer later processes the held packets after the namespace and its devices have been freed, a use-after-free of both: BUG: KASAN: slab-use-after-free in xfrm_input+0xadd/0x2620 Read of size 8 at addr ff11000011ecbc80 by task swapper/1/0 xfrm_input() reads the freed device, takes the namespace from it, and counts the dead SA through the freed struct net, a per-CPU increment through a pointer read from freed memory. An unprivileged user can trigger this from a user and network namespace, by holding a packet on an IP-TFS SA and leaving the namespace before the drop timer fires. Fix this with a new delete_state mode callback, called from __xfrm_state_delete(). IP-TFS frees the held packets there and marks the SA so that a packet still in flight on another CPU is not held afterwards. Because the callback runs under x->lock, it cancels the drop timer without waiting for it. Namespace exit now also flushes all SAs in pre_exit, next to the policies; the flush in exit stays and finds nothing. The two changes are made together because freeing the held packets from exit would release their routes after IPv6 has torn down its dst_ops, and conntrack teardown would wait on their entries. cleanup_net() waits for an RCU grace period between pre_exit and exit, which also covers a drop timer that is still running. Fixes: 6be02e3e4f37 ("xfrm: iptfs: handle reordering of received packets") Fixes: 075694765446 ("xfrm: iptfs: handle received fragmented inner packets") Cc: stable@vger.kernel.org Signed-off-by: Juan Agustin Niccolini --- Reproducer: https://github.com/AgusNicco/iptfs-uafs/blob/main/iptfs_netns_uaf.c include/net/xfrm.h | 3 +++ net/xfrm/xfrm_iptfs.c | 48 +++++++++++++++++++++++++++++++++++++++++- net/xfrm/xfrm_policy.c | 1 + net/xfrm/xfrm_state.c | 3 +++ 4 files changed, 54 insertions(+), 1 deletion(-) diff --git a/include/net/xfrm.h b/include/net/xfrm.h index 5fbb73474e32..97f90eba73c2 100644 --- a/include/net/xfrm.h +++ b/include/net/xfrm.h @@ -490,6 +490,8 @@ static inline void xfrm_unset_type_offload(struct xfrm_state *x) * @owner: module owner or NULL * @init_state: Add/init mode specific state in `xfrm_state *x` * @clone_state: Copy mode specific values from `orig` to new state `x` + * @delete_state: Release mode specific resources when `x` is deleted. Called + * with `x->lock` held. * @destroy_state: Cleanup mode specific state from `xfrm_state *x` * @user_init: Process mode specific netlink attributes from user * @copy_to_user: Add netlink attributes to `attrs` based on state in `x` @@ -509,6 +511,7 @@ struct xfrm_mode_cbs { struct module *owner; int (*init_state)(struct xfrm_state *x); int (*clone_state)(struct xfrm_state *x, struct xfrm_state *orig); + void (*delete_state)(struct xfrm_state *x); void (*destroy_state)(struct xfrm_state *x); int (*user_init)(struct net *net, struct xfrm_state *x, struct nlattr **attrs, diff --git a/net/xfrm/xfrm_iptfs.c b/net/xfrm/xfrm_iptfs.c index 76272b0a1829..0dde380eb16c 100644 --- a/net/xfrm/xfrm_iptfs.c +++ b/net/xfrm/xfrm_iptfs.c @@ -141,6 +141,7 @@ struct skb_wseq { * @drop_lock: lock to protect reorder queue. * @drop_timer: timer for considering next packet lost. * @drop_time_ns: timer intervan in nanoseconds. + * @dead: SA deleted, do not hold any more packets. * @ra_newskb: new pkt being reassembled. * @ra_wantseq: expected next sequence for reassembly. * @ra_runt: last pkt bytes from very end of last skb. @@ -168,6 +169,7 @@ struct xfrm_iptfs_data { spinlock_t drop_lock; struct hrtimer drop_timer; u64 drop_time_ns; + bool dead; /* Tunnel input reassembly */ struct sk_buff *ra_newskb; /* new pkt being reassembled */ @@ -1308,6 +1310,13 @@ done: skb_abort_seq_read(&skbseq); kfree_skb(skb); } + + if (unlikely(READ_ONCE(xtfs->dead))) { + spin_lock(&xtfs->drop_lock); + if (xtfs->ra_newskb) + iptfs_reassem_abort(xtfs); + spin_unlock(&xtfs->drop_lock); + } } /* ------------------------------- */ @@ -1749,7 +1758,10 @@ static int iptfs_input(struct xfrm_state *x, struct sk_buff *skb) INIT_LIST_HEAD(&freelist); spin_lock(&xtfs->drop_lock); - iptfs_input_reorder(xtfs, skb, &list, &freelist); + if (likely(!xtfs->dead)) + iptfs_input_reorder(xtfs, skb, &list, &freelist); + else + list_add_tail(&skb->list, &freelist); spin_unlock(&xtfs->drop_lock); list_for_each_entry_safe(skb, next, &list, list) { @@ -2715,6 +2727,7 @@ static int iptfs_clone_state(struct xfrm_state *x, struct xfrm_state *orig) xtfs->w_seq_set = false; xtfs->w_wantseq = 0; xtfs->w_savedlen = 0; + xtfs->dead = false; xtfs->ra_newskb = NULL; xtfs->ra_wantseq = 0; xtfs->ra_runtlen = 0; @@ -2744,6 +2757,38 @@ static int iptfs_init_state(struct xfrm_state *x) return 0; } +static void iptfs_delete_state(struct xfrm_state *x) +{ + struct xfrm_iptfs_data *xtfs = x->mode_data; + struct sk_buff_head list; + struct skb_wseq *s, *se; + struct sk_buff *skb; + + if (!xtfs) + return; + + __skb_queue_head_init(&list); + + spin_lock(&xtfs->drop_lock); + WRITE_ONCE(xtfs->dead, true); + if (xtfs->ra_newskb) { + __skb_queue_tail(&list, xtfs->ra_newskb); + xtfs->ra_newskb = NULL; + } + for (s = xtfs->w_saved, se = s + xtfs->w_savedlen; s < se; s++) { + if (s->skb) { + __skb_queue_tail(&list, s->skb); + s->skb = NULL; + } + } + xtfs->w_savedlen = 0; + hrtimer_try_to_cancel(&xtfs->drop_timer); + spin_unlock(&xtfs->drop_lock); + + while ((skb = __skb_dequeue(&list))) + kfree_skb(skb); +} + static void iptfs_destroy_state(struct xfrm_state *x) { struct xfrm_iptfs_data *xtfs = x->mode_data; @@ -2784,6 +2829,7 @@ static const struct xfrm_mode_cbs iptfs_mode_cbs = { .owner = THIS_MODULE, .init_state = iptfs_init_state, .clone_state = iptfs_clone_state, + .delete_state = iptfs_delete_state, .destroy_state = iptfs_destroy_state, .user_init = iptfs_user_init, .copy_to_user = iptfs_copy_to_user, diff --git a/net/xfrm/xfrm_policy.c b/net/xfrm/xfrm_policy.c index d2e938d4db9a..af17985647b4 100644 --- a/net/xfrm/xfrm_policy.c +++ b/net/xfrm/xfrm_policy.c @@ -4318,6 +4318,7 @@ static void __net_exit xfrm_net_pre_exit(struct net *net) xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false); #endif xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false); + xfrm_state_flush(net, 0, false); } static void xfrm_policy_fini(struct net *net) diff --git a/net/xfrm/xfrm_state.c b/net/xfrm/xfrm_state.c index eb984ed11888..065c1fefb562 100644 --- a/net/xfrm/xfrm_state.c +++ b/net/xfrm/xfrm_state.c @@ -846,6 +846,9 @@ int __xfrm_state_delete(struct xfrm_state *x) xfrm_state_delete_tunnel(x); + if (x->mode_cbs && x->mode_cbs->delete_state) + x->mode_cbs->delete_state(x); + /* All xfrm_state objects are created by xfrm_state_alloc. * The xfrm_state_alloc call gives a reference, and that * is what we are dropping here. -- 2.53.0