From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.224.49 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796095332; b=Ii3gF0OydDkE2u5/Z8AdVhNw6gyX9BWJC38t5nzaZpSM8Z3D5hk6CszAc+nlpdhA0Nkw BF8qt5cy00EsOeJI0x66n2S23B/CcscLpzKsEnjmVEr+x2b5ypkGSzP7phSRC89GcKgmJ JRdq0V7vXLMeld3EM9Arfg5zN5YTz4CqH0UXW2NqE5a7AunnxXmvEiCc3Hx8S51K9fCDE JYYjCaeh01nchNxNQabDmXO4gdd2sr1IvOp8pvNmu/ZO2b/Y2YWpPzMsIYOoJgihPFPg8 BjOj1GsfYBsx8LGmeJSZjnDmS2FZEYWeLZvRiL5LKfcHzN1EbY9hrrSmVwIAm6W7lEQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796095332; 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=+vmg5pLH/ScgkE1vtWRINxqv1x8zsdq4xGKHZeU61Iw=; b=zBX4/m35htr+szmbPyhH4kYBDluHxbS2+vm+HFqFPXOb3Z9ddihg5f+dUVjp6sKzDEUR 420Zy5AZ4eR2s+ytX/pmGbH0ItrgvNGvBaQV4V+Lpl1dMn7lTbC1sYstZfHUKQ7+EqbpE 1h/rsMQvKZtjTx9yG73dptaSJuxa7Gg/1GWdJboIqc5FO4M1vtyGIzzSA6oNgeob7R5NE M6n8nM92m/n1ftU63FzNbrneOSJR9YQNGTGzhJFmSwPS3ocKHzC0W1O2RDWztmxtnNQr+ +MMe0j8XHo2CUQnaNacCf5XXWDYZIqUwpYa0QE7qoVQo3OHWt5hvRr3G3z8NPuKVzDw== 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=o6MvkwLq; arc=none smtp.remote-ip=74.125.224.49 Received: from mail-yx1-f49.google.com (mail-yx1-f49.google.com [74.125.224.49]) (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 B72DF606A336 for ; Tue, 1 Dec 2026 03:22:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B72DF606A336 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=o6MvkwLq Received: by mail-yx1-f49.google.com with SMTP id 956f58d0204a3-67c0bd676eeso7823566d50.3 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=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=+vmg5pLH/ScgkE1vtWRINxqv1x8zsdq4xGKHZeU61Iw=; b=o6MvkwLqTeCnb6E22ZwaOQUdbJAJ2hXgnnj7R8CcnszJ6wG0S8qhYf0EzJsbcgFn1j gJs4JLTmGkVCVtg+v9DF6L0y//TrxDuyLc+MskLjreAv13EVhNGgZObL60lmUYw3wXKj qs6L0YcLz/1jeuwrBOh5CL+LgQp17H52dUdudTJt2ZwNjw3ssmUVlVfFvObjetXD46ar 71A47iyDcTtXXPfIS8Zo6tvZSZpfxduiaSSg81Mpk+ENDOqUif7wK0wHw1cPJQeSeRTf ewE0Lg3q3N98VGR2FSo1PZc72lZux0+G+ldgPbCRNkZmFVuLxL7SNkT9fJ9SZqBTbfr9 roZw== 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=zkgYJqXfRQm4ehqxtBeeq8zJYW09b+LRsLaIegb0JXLYTyvPtXo12eE7C8RX3Qkext dPcXqxrp7CQMFxrBjPbxKjuijOVna3z2WDp2o2LvsNKsoEfqsorEjrCWdjo24r2R2nq2 Q0tadRWvbVybpk6LlSOgSC2GAfKRgLxhfez5btERfYs+L1ZSypbBIc9OhgdvtUWIx1DX FHSO5iCDIdZO26jFYNQsyY3hQ6k8ZITlOaRSnD5U7e92/vT9y6I5Dq4xxw2yP5x5Gd6A Qx4hVG3Q3ypZsG9sWAhGUoi9nSaMcSxIiTobrRQ0ZEv2mnaNG7iEoIXqQmqn3+72FR/2 wOVQ== X-Gm-Message-State: APuU4Z/768sumrFx2at80oR+tLGykGzVBx/BCFMXtQvVVaOWZXNDM0cW LZdqVzkc//hI3lrpQFPE9WO0KnLeg0neQ6MnthyRXJBBqwqnDYEEOS2h X-Gm-Gg: AZFdTZdRSkWt4mPc5BTz6JxBlyH1MyhWBIzTHBfe+sw4RhvAY5CULDA822Ip/SeZ99s 0thQBHdvm32s7j8vdGkzfZM4dVJADKHpXNsorP0ChANzf3w1xGouX+gO/7GideaRQySg4iC8+QE 9bLn2TLqYvDc8BX4HoOzIVQ6reiVSsjw3E7pIHFsuLHCnTQUnM+xTBqA4NO2EBDZypAiX31UbUJ 8WGfU5E8XHyRNJIn2MCekOxSMiKE0Ol6CzzEk+Os/BRS9mIOQ9Q7FsFHRo2IzIja61z2PPdqO3H AQTjJxmmPEIyV+173GGOW5kjsgRxj/V9I8Wiizlw/e9dMKr8u5r6HLI8egkhOUf2Tw2xI6i2GGR RujI+qs5hty+fj2EjXDpaPft0UmA8+5BV9XLhijXVevz4A1ADWZThAnFRHlQj4VRk2UlvlA2JlU DT7OK3QBg4/1XeNATpYBpuyMOdB1AT3slMSF96ROA26TN64C+h8bshzbPBbOjqVSf/lT2Yt3TFa a6lpns3saGESrYEwPw5H330Wv/9J2W11s3dYfcLZRAetu8Ho6HyV1L5KD2sicHYcvIOqMLRJweZ sqHf7w6lKw1uXWNfDnxoc2I7PJPjLPfv7VlD6yeIkQ79SUqEn4jxyTMw1wmkFE/UE0gZ45qsEA= = 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: netdev@vger.kernel.org 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