xfrm: prevent policy_hthresh.work from racing with netns teardown
Subsystems (3)
PATCH 1/1 xfrm: prevent policy_hthresh.work from racing with netns teardown
An XFRM_MSG_NEWSPDINFO request can queue the per-net work item policy_hthresh.work onto the system workqueue. xfrm_hash_rebuild() retrieves the enclosing struct net from the work item, so the callback can dereference freed memory if it runs after net namespace teardown.
Mainline commit 29fe3a61bcdc ("xfrm: prevent policy_hthresh.work from racing with netns teardown") fixes this issue using disable_work_sync(). That API is not available in Linux 5.15, so the mainline change cannot be directly backported.
Provide the same guarantee with an XFRM-local work_disabled flag. Protect the flag and schedule_work() with the existing policy_hthresh seqlock so teardown can atomically stop new queueing attempts. Then use cancel_work_sync() to synchronize work that was queued before the flag was set. This ensures policy_hthresh.work cannot outlive its struct net.
--- include/net/netns/xfrm.h | 1 + net/xfrm/xfrm_policy.c | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-)
include/net/netns/xfrm.h +1 -0
@@ -23,6 +23,7 @@ struct xfrm_policy_hash {struct xfrm_policy_hthresh {struct work_struct work;seqlock_t lock; +bool work_disabled;u8 lbits4;u8 rbits4;u8 lbits6;
net/xfrm/xfrm_policy.c +18 -1
@@ -1378,7 +1378,10 @@ static void xfrm_hash_rebuild(struct work_struct *work)void xfrm_policy_hash_rebuild (struct net *net) { -schedule_work (&net->xfrm.policy_hthresh.work); +write_seqlock (&net->xfrm.policy_hthresh.lock); +if (!net->xfrm.policy_hthresh.work_disabled) +schedule_work (&net->xfrm.policy_hthresh.work); +write_sequnlock (&net->xfrm.policy_hthresh.lock); }EXPORT_SYMBOL (xfrm_policy_hash_rebuild);
@@ -4088,6 +4091,7 @@ static int __net_init xfrm_policy_init(struct net *net) net->xfrm.policy_hthresh.rbits6 =128 ;seqlock_init (&net->xfrm.policy_hthresh.lock); + net->xfrm.policy_hthresh.work_disabled = false;INIT_LIST_HEAD (&net->xfrm.policy_all);INIT_LIST_HEAD (&net->xfrm.inexact_bins);
@@ -4113,6 +4117,19 @@ static void xfrm_policy_fini(struct net *net)unsigned int sz;int dir; +/* Prevent new policy hash rebuilds before draining the work item. +* +* The upstream fix uses disable_work_sync(), which is unavailable +* in v5.15. Protecting work_disabled and schedule_work() with the +* same seqlock closes the check-to-queue race, while the subsequent +* cancel_work_sync() drains work that was queued or running before +* teardown disabled it. +*/ +write_seqlock (&net->xfrm.policy_hthresh.lock); + net->xfrm.policy_hthresh.work_disabled = true; +write_sequnlock (&net->xfrm.policy_hthresh.lock); +cancel_work_sync (&net->xfrm.policy_hthresh.work); +flush_work (&net->xfrm.policy_hash_work);#ifdef CONFIG_XFRM_SUB_POLICY xfrm_policy_flush (net,XFRM_POLICY_TYPE_SUB , false);
Signature · 1 line
2.43.0
Quoted 3 lines
An
XFRM_MSG_NEWSPDINFOrequest can queue the per-net work item policy_hthresh.work onto the system workqueue.xfrm_hash_rebuild()retrieves the enclosing struct net from the work item, so the callback
Queued for 5.15, thanks.
Signature · 2 lines
Thanks, Sasha