FAILED: patch "[PATCH] netfilter: cttimeout: prevent UAF during module unload" failed to apply to 5.15-stable tree
Subsystems (3)
[email protected]
maintainerThe patch below does not apply to the 5.15-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-5.15.y
git checkout FETCH_HEAD
git cherry-pick -x fec9b1de0d02de8dafa3cc344bcb91cf28660643
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026091646-antonym-acid-ca2a@gregkh' --subject-prefix 'PATCH 5.15.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From fec9b1de0d02de8dafa3cc344bcb91cf28660643 Mon Sep 17 00:00:00 2001
From: Chengfeng Ye <[email protected]>
Date: Mon, 24 Aug 2026 20:12:38 +0800
Subject: [PATCH] netfilter: cttimeout: prevent UAF during module unload
nf_ct_set_timeout() protects the timeout hook dereference and policy lookup with rcu_read_lock(). cttimeout_exit(), however, unregisters the per-net operations before it clears the hook.
This allows the following interleaving:
CPU 0 CPU 1
cttimeout_exit() nf_ct_set_timeout()
unregister_pernet_subsys() rcu_read_lock()
kfree(pernet) h = nf_ct_timeout_hook
h->timeout_find_get()
nfct_timeout_pernet()
The hook still points to ctnl_timeout_find_get() when CPU 1 looks up the already freed per-net timeout list. KASAN reported:
BUG: KASAN: slab-use-after-free in ctnl_timeout_find_get Read of size 8 by task poc/90 Call Trace: ctnl_timeout_find_get+0x271/0x2a0 [nfnetlink_cttimeout] nf_ct_set_timeout+0x7b/0x3c0 xt_ct_tg_check+0x724/0xb20 xt_check_target+0x234/0xa90 do_ipt_set_ctl+0x570/0x1270 Allocated by task 89: __kmalloc_noprof+0x16e/0x460 ops_init+0x6d/0x420 register_pernet_operations+0x2f6/0x670 Freed by task 91: kfree+0x131/0x390 ops_undo_list+0x3d4/0x730 unregister_pernet_operations+0x232/0x490 unregister_pernet_subsys+0x1c/0x30 cttimeout_exit+0x52/0x970 [nfnetlink_cttimeout]
Clear the hook and wait for existing readers before unregistering the per-net operations. This blocks new policy lookups and ensures readers that observed the hook finish before the per-net storage is freed.
net/netfilter/nfnetlink_cttimeout.c +1 -1
@@ -652,9 +652,9 @@ static void __exit cttimeout_exit(void) {nfnetlink_subsys_unregister (&cttimeout_subsys); -unregister_pernet_subsys (&cttimeout_ops);RCU_INIT_POINTER (nf_ct_timeout_hook,NULL );synchronize_net (); +unregister_pernet_subsys (&cttimeout_ops); }module_init (cttimeout_init);
Sasha Levin
maintainerPATCH 1/5 netfilter: cttimeout: decouple unlink and free on netns destruction
From: Florian Westphal <[email protected]>
[ Upstream commit 78222bacfca97cb18505df1ba5f3591864498a7e ]
Make it so netns pre_exit unlinks the objects from the pernet list, so they cannot be found anymore.
netns core issues a synchronize_rcu() before calling the exit hooks so any the time the exit hooks run unconfirmed nf_conn entries have been free'd or they have been committed to the hashtable.
The exit hook still tags unconfirmed entries as dying, this can now be removed in a followup change.
Stable-dep-of: fec9b1de0d02 ("netfilter: cttimeout: prevent UAF during module unload")
--- include/net/netfilter/nf_conntrack_timeout.h | 8 ------ net/netfilter/nfnetlink_cttimeout.c | 30 ++++++++++++++++++-- 2 files changed, 28 insertions(+), 10 deletions(-)
include/net/netfilter/nf_conntrack_timeout.h +0 -8
@@ -18,14 +18,6 @@ struct nf_ct_timeout {char data[]; }; -struct ctnl_timeout { -struct list_head head; -struct rcu_head rcu_head; -refcount_t refcnt; -char name[CTNL_TIMEOUT_NAME_MAX ]; -struct nf_ct_timeout timeout; -}; -struct nf_conn_timeout {struct nf_ct_timeout __rcu *timeout; };
net/netfilter/nfnetlink_cttimeout.c +28 -2
@@ -33,8 +33,19 @@static unsigned int nfct_timeout_id__read_mostly ; +struct ctnl_timeout { +struct list_head head; +struct rcu_head rcu_head; +refcount_t refcnt; +char name[CTNL_TIMEOUT_NAME_MAX ]; +struct nf_ct_timeout timeout; + +struct list_head free_head; +}; +struct nfct_timeout_pernet {struct list_head nfct_timeout_list; +struct list_head nfct_timeout_freelist; };MODULE_LICENSE ("GPL" );
@@ -578,10 +589,24 @@ static int __net_init cttimeout_net_init(struct net *net)struct nfct_timeout_pernet *pernet =nfct_timeout_pernet (net);INIT_LIST_HEAD (&pernet->nfct_timeout_list); +INIT_LIST_HEAD (&pernet->nfct_timeout_freelist);return 0 ; } +static void __net_exit cttimeout_net_pre_exit(struct net *net) +{ +struct nfct_timeout_pernet *pernet =nfct_timeout_pernet (net); +struct ctnl_timeout *cur, *tmp; + +list_for_each_entry_safe (cur, tmp, &pernet->nfct_timeout_list, head) { +list_del_rcu (&cur->head); +list_add (&cur->free_head, &pernet->nfct_timeout_freelist); + } + +/* core calls synchronize_rcu() after this */ +} + static void __net_exit cttimeout_net_exit(struct net *net) {struct nfct_timeout_pernet *pernet =nfct_timeout_pernet (net);
@@ -590,8 +615,8 @@ static void __net_exit cttimeout_net_exit(struct net *net)nf_ct_unconfirmed_destroy (net);nf_ct_untimeout (net,NULL ); -list_for_each_entry_safe (cur, tmp, &pernet->nfct_timeout_list, head) { -list_del_rcu (&cur->head); +list_for_each_entry_safe (cur, tmp, &pernet->nfct_timeout_freelist, head) { +list_del (&cur->free_head);if (refcount_dec_and_test (&cur->refcnt))kfree_rcu (cur, rcu_head);
@@ -600,6 +625,7 @@ static void __net_exit cttimeout_net_exit(struct net *net)static struct pernet_operations cttimeout_ops = { .init = cttimeout_net_init, + .pre_exit = cttimeout_net_pre_exit, .exit = cttimeout_net_exit, .id = &nfct_timeout_id, .size =sizeof (struct nfct_timeout_pernet ),
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/5 netfilter: remove nf_ct_unconfirmed_destroy helper
From: Florian Westphal <[email protected]>
[ Upstream commit 17438b42ce14cb60ceda9ae62ad5dd022d55a216 ]
This helper tags connections not yet in the conntrack table as dying. These nf_conn entries will be dropped instead when the core attempts to insert them from the input or postrouting 'confirm' hook.
After the previous change, the entries get unlinked from the list earlier, so that by the time the actual exit hook runs, new connections no longer have a timeout policy assigned.
Its enough to walk the hashtable instead.
Stable-dep-of: fec9b1de0d02 ("netfilter: cttimeout: prevent UAF during module unload")
--- include/net/netfilter/nf_conntrack.h | 3 --- net/netfilter/nf_conntrack_core.c | 14 -------------- net/netfilter/nfnetlink_cttimeout.c | 4 +++- 3 files changed, 3 insertions(+), 18 deletions(-)
include/net/netfilter/nf_conntrack.h +0 -3
@@ -240,9 +240,6 @@ static inline bool nf_ct_kill(struct nf_conn *ct)return nf_ct_delete (ct,0 ,0 ); } -/* Set all unconfirmed conntrack as dying */ -void nf_ct_unconfirmed_destroy (struct net *); -/* Iterate over all conntracks: if iter returns true, it's deleted. */ void nf_ct_iterate_cleanup_net(struct net *net, int (*iter)(struct nf_conn *i, void *data),
net/netfilter/nf_conntrack_core.c +0 -14
@@ -2446,20 +2446,6 @@ __nf_ct_unconfirmed_destroy(struct net *net) } } -void nf_ct_unconfirmed_destroy (struct net *net) -{ -struct nf_conntrack_net *cnet =nf_ct_pernet (net); - -might_sleep (); - -if (atomic_read (&cnet->count) >0 ) { -__nf_ct_unconfirmed_destroy (net); -nf_queue_nf_hook_drop (net); -synchronize_net (); - } -} -EXPORT_SYMBOL_GPL (nf_ct_unconfirmed_destroy); -void nf_ct_iterate_cleanup_net (struct net *net,int (*iter)(struct nf_conn *i,void *data),void *data,u32 portid,int report)
net/netfilter/nfnetlink_cttimeout.c +3 -1
@@ -612,7 +612,9 @@ static void __net_exit cttimeout_net_exit(struct net *net)struct nfct_timeout_pernet *pernet =nfct_timeout_pernet (net);struct ctnl_timeout *cur, *tmp; -nf_ct_unconfirmed_destroy (net); +if (list_empty (&pernet->nfct_timeout_freelist)) +return ; +nf_ct_untimeout (net,NULL );list_for_each_entry_safe (cur, tmp, &pernet->nfct_timeout_freelist, head) {
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 3/5 netfilter: cttimeout: fix slab-out-of-bounds read in cttimeout_net_exit
In reply to Sasha LevinFrom: Florian Westphal <[email protected]>
[ Upstream commit aeed55a08d0b2bf2e3986b110b04d2a5aca192a6 ]
syzbot reports:
BUG: KASAN: slab-out-of-bounds in __list_del_entry_valid+0xcc/0xf0 lib/list_debug.c:42 [..] list_del include/linux/list.h:148 [inline] cttimeout_net_exit+0x211/0x540 net/netfilter/nfnetlink_cttimeout.c:617
No reproducer so far. Looking at recent changes in this area its clear that the free_head must not be at the end of the structure because nf_ct_timeout structure has variable size.
Stable-dep-of: fec9b1de0d02 ("netfilter: cttimeout: prevent UAF during module unload")
--- net/netfilter/nfnetlink_cttimeout.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
net/netfilter/nfnetlink_cttimeout.c +3 -2
@@ -35,12 +35,13 @@ static unsigned int nfct_timeout_id __read_mostly;struct ctnl_timeout {struct list_head head; +struct list_head free_head;struct rcu_head rcu_head;refcount_t refcnt;char name[CTNL_TIMEOUT_NAME_MAX ]; -struct nf_ct_timeout timeout; -struct list_head free_head; +/* must be at the end */ +struct nf_ct_timeout timeout; };struct nfct_timeout_pernet {
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 4/5 netfilter: cttimeout: detach dataplane timeout policy and repurpose refcount
In reply to Sasha LevinFrom: Pablo Neira Ayuso <[email protected]>
[ Upstream commit 7d6a9cdb8d3a51d9cfe546a09a518ab3d2671549 ]
Add a refcount for struct nf_ct_timeout which is used by ct extension to set the custom ct timeout policy, this tells us that the ct timeout is being used by a conntrack entry. When the last conntrack entry drops the refcount on the ct timeout, the ct timeout is released.
Remove the refcount for control plane which controls if the ruleset refers to the timeout policy. After this update, it is possible to remove the ct timeout policy from nfnetlink_cttimeout immediately. This is for simplicity not to handle two refcounts on a single object.
Remove nf_queue_nf_hook_drop(): a packet sitting in nfqueue will just hold a reference to the nf_ct_timeout object until packet is reinjected, since this is part of the ct extension, this will be released by the time the conntrack is freed.
nf_ct_untimeout() is still called to clean up in a best effort basis:
the ct timeout on existing entries gets removed when the ct timeout goes away, but as long as the iptables ruleset still refers to the ct timeout through a template, new conntracks may keep attaching it and extend its lifetime until the rule is removed.
nf_ct_untimeout() is not called anymore from module removal path, this is unlikely to find timeouts give module refcount is bumped, and the new refcount already tracks the ct timeout policy use so it is released when unused.
This dependency is deliberately narrowed for the 5.15 backport of fec9b1de0d02 ("netfilter: cttimeout: prevent UAF during module unload"). The target only needs the common timeout hook and the synchronize_net() module-exit context; it does not depend on the dataplane refcount redesign.
Fold in the timeout hook representation from 7afa38831aee2 ("netfilter: cttimeout: use option structure"), with the __rcu annotations from 6976890e8998. Convert the existing hook users and provider together, and retain synchronize_net() from this dependency. Leave per-net unregistration in its existing position so the target applies unchanged and moves it after hook withdrawal and the reader grace period.
Drop the separate policy allocation, dataplane refcount, conntrack-free, and nftables/xtables queue-handling hunks. These require newer lifetime infrastructure and are unnecessary for the target. Keep the 5.15 extension destructors, policy ownership and module references. No functions are added.
Also correct the existing nfct_timeout_freelist traversal to use free_head, matching the member used to link and unlink its entries. Using head computes the wrong ctnl_timeout address during teardown.
[ sashal: Reduced backport -- upstream 7d6a9cdb8d3a5 touches 6 file(s), this
backport carries 3. Not backported here:
net/netfilter/nf_conntrack_core.c
net/netfilter/nft_ct.c
net/netfilter/xt_CT.c
This note is generated from the file lists only; see the resolution record for the reasoning. ]
Stable-dep-of: fec9b1de0d02 ("netfilter: cttimeout: prevent UAF during module unload")
--- include/net/netfilter/nf_conntrack_timeout.h | 8 +++-- net/netfilter/nf_conntrack_timeout.c | 31 ++++++++------------ net/netfilter/nfnetlink_cttimeout.c | 15 ++++++---- 3 files changed, 28 insertions(+), 26 deletions(-)
include/net/netfilter/nf_conntrack_timeout.h +6 -2
@@ -113,8 +113,12 @@ static inline void nf_ct_destroy_timeout(struct nf_conn *ct)#endif /* CONFIG_NF_CONNTRACK_TIMEOUT */ #ifdef CONFIG_NF_CONNTRACK_TIMEOUT -extern struct nf_ct_timeout *(*nf_ct_timeout_find_get_hook)(struct net *net,const char *name); -extern void (*nf_ct_timeout_put_hook)(struct nf_ct_timeout *timeout); +struct nf_ct_timeout_hooks { +struct nf_ct_timeout *(*timeout_find_get)(struct net *net,const char *name); +void (*timeout_put)(struct nf_ct_timeout *timeout); +}; + +extern const struct nf_ct_timeout_hooks __rcu *nf_ct_timeout_hook;#endif #endif /* _NF_CONNTRACK_TIMEOUT_H */
net/netfilter/nf_conntrack_timeout.c +13 -18
@@ -22,12 +22,8 @@#include <net/netfilter/nf_conntrack_l4proto.h> #include <net/netfilter/nf_conntrack_timeout.h> -struct nf_ct_timeout * -(*nf_ct_timeout_find_get_hook)(struct net *net,const char *name)__read_mostly ; -EXPORT_SYMBOL_GPL (nf_ct_timeout_find_get_hook); - -void (*nf_ct_timeout_put_hook)(struct nf_ct_timeout *timeout)__read_mostly ; -EXPORT_SYMBOL_GPL (nf_ct_timeout_put_hook); +const struct nf_ct_timeout_hooks __rcu *nf_ct_timeout_hook__read_mostly ; +EXPORT_SYMBOL_GPL (nf_ct_timeout_hook);static int untimeout (struct nf_conn *ct,void *timeout) {
@@ -48,31 +44,30 @@ EXPORT_SYMBOL_GPL(nf_ct_untimeout);static void __nf_ct_timeout_put (struct nf_ct_timeout *timeout) { -typeof (nf_ct_timeout_put_hook) timeout_put; +const struct nf_ct_timeout_hooks *h =rcu_dereference (nf_ct_timeout_hook); - timeout_put =rcu_dereference (nf_ct_timeout_put_hook); -if (timeout_put) -timeout_put (timeout); +if (h) + h->timeout_put (timeout); }int nf_ct_set_timeout (struct net *net,struct nf_conn *ct,u8 l3num,u8 l4num,const char *timeout_name) { -typeof (nf_ct_timeout_find_get_hook) timeout_find_get; +const struct nf_ct_timeout_hooks *h;struct nf_ct_timeout *timeout;struct nf_conn_timeout *timeout_ext;const char *errmsg =NULL ;int ret =0 ;rcu_read_lock (); - timeout_find_get =rcu_dereference (nf_ct_timeout_find_get_hook); -if (!timeout_find_get) { + h =rcu_dereference (nf_ct_timeout_hook); +if (!h) { ret = -ENOENT ; errmsg ="Timeout policy base is empty" ; goto out; } - timeout =timeout_find_get (net, timeout_name); + timeout = h->timeout_find_get (net, timeout_name);if (!timeout) { ret = -ENOENT ; pr_info_ratelimited("No such timeout policy \"%s\"\n",
@@ -119,15 +114,15 @@ EXPORT_SYMBOL_GPL(nf_ct_set_timeout);void nf_ct_destroy_timeout (struct nf_conn *ct) {struct nf_conn_timeout *timeout_ext; -typeof (nf_ct_timeout_put_hook) timeout_put; +const struct nf_ct_timeout_hooks *h;rcu_read_lock (); - timeout_put =rcu_dereference (nf_ct_timeout_put_hook); + h =rcu_dereference (nf_ct_timeout_hook); -if (timeout_put) { +if (h) { timeout_ext =nf_ct_timeout_find (ct);if (timeout_ext) { -timeout_put (timeout_ext->timeout); + h->timeout_put (timeout_ext->timeout);RCU_INIT_POINTER (timeout_ext->timeout,NULL ); } }
net/netfilter/nfnetlink_cttimeout.c +9 -6
@@ -618,7 +618,7 @@ static void __net_exit cttimeout_net_exit(struct net *net)nf_ct_untimeout (net,NULL ); -list_for_each_entry_safe (cur, tmp, &pernet->nfct_timeout_freelist, head) { +list_for_each_entry_safe (cur, tmp, &pernet->nfct_timeout_freelist, free_head) {list_del (&cur->free_head);if (refcount_dec_and_test (&cur->refcnt))
@@ -634,6 +634,11 @@ static struct pernet_operations cttimeout_ops = { .size = sizeof(struct nfct_timeout_pernet), }; +static const struct nf_ct_timeout_hooks hooks = { + .timeout_find_get = ctnl_timeout_find_get, + .timeout_put = ctnl_timeout_put, +}; +static int __init cttimeout_init (void ) {int ret;
@@ -648,8 +653,7 @@ static int __init cttimeout_init(void) "nfnetlink.\n"); goto err_out; } -RCU_INIT_POINTER (nf_ct_timeout_find_get_hook, ctnl_timeout_find_get); -RCU_INIT_POINTER (nf_ct_timeout_put_hook, ctnl_timeout_put); +RCU_INIT_POINTER (nf_ct_timeout_hook, &hooks);return 0 ; err_out:
@@ -662,9 +666,8 @@ static void __exit cttimeout_exit(void)nfnetlink_subsys_unregister (&cttimeout_subsys);unregister_pernet_subsys (&cttimeout_ops); -RCU_INIT_POINTER (nf_ct_timeout_find_get_hook,NULL ); -RCU_INIT_POINTER (nf_ct_timeout_put_hook,NULL ); -synchronize_rcu (); +RCU_INIT_POINTER (nf_ct_timeout_hook,NULL ); +synchronize_net (); }module_init (cttimeout_init);
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 5/5 netfilter: cttimeout: prevent UAF during module unload
In reply to Sasha LevinFrom: Chengfeng Ye <[email protected]>
[ Upstream commit fec9b1de0d02de8dafa3cc344bcb91cf28660643 ]
nf_ct_set_timeout() protects the timeout hook dereference and policy lookup with rcu_read_lock(). cttimeout_exit(), however, unregisters the per-net operations before it clears the hook.
This allows the following interleaving:
CPU 0 CPU 1
cttimeout_exit() nf_ct_set_timeout()
unregister_pernet_subsys() rcu_read_lock()
kfree(pernet) h = nf_ct_timeout_hook
h->timeout_find_get()
nfct_timeout_pernet()
The hook still points to ctnl_timeout_find_get() when CPU 1 looks up the already freed per-net timeout list. KASAN reported:
BUG: KASAN: slab-use-after-free in ctnl_timeout_find_get Read of size 8 by task poc/90 Call Trace: ctnl_timeout_find_get+0x271/0x2a0 [nfnetlink_cttimeout] nf_ct_set_timeout+0x7b/0x3c0 xt_ct_tg_check+0x724/0xb20 xt_check_target+0x234/0xa90 do_ipt_set_ctl+0x570/0x1270 Allocated by task 89: __kmalloc_noprof+0x16e/0x460 ops_init+0x6d/0x420 register_pernet_operations+0x2f6/0x670 Freed by task 91: kfree+0x131/0x390 ops_undo_list+0x3d4/0x730 unregister_pernet_operations+0x232/0x490 unregister_pernet_subsys+0x1c/0x30 cttimeout_exit+0x52/0x970 [nfnetlink_cttimeout]
Clear the hook and wait for existing readers before unregistering the per-net operations. This blocks new policy lookups and ensures readers that observed the hook finish before the per-net storage is freed.
--- net/netfilter/nfnetlink_cttimeout.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nfnetlink_cttimeout.c +1 -1
@@ -665,9 +665,9 @@ static void __exit cttimeout_exit(void) {nfnetlink_subsys_unregister (&cttimeout_subsys); -unregister_pernet_subsys (&cttimeout_ops);RCU_INIT_POINTER (nf_ct_timeout_hook,NULL );synchronize_net (); +unregister_pernet_subsys (&cttimeout_ops); }module_init (cttimeout_init);
Signature · 1 line
2.53.0