From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7467B261B8D; Thu, 15 Oct 2026 23:06:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1792105605; cv=none; b=GD0cXJISNmf78HgFmX/btvt13X9V5cObTYL3DRDz5xf7c+uCJgeFZEAGE/xyOyAWrm7ACBBsycJ8s32d3mUNMTYS68EjUKJSNmgS6K6ywmBnfwGB+wEmL4JVI1JmMw2fUtjunCjZRCJ9XxEev+jsAdtvMoaV9mm6G9YFnV9EdEA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1792105605; c=relaxed/simple; bh=fhiJ0rg+A/cnPRaFz88V9kFoHVcptEqpB77OA5hTkP8=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=YZBquoQHMSukvJH58BDcU0txM6V/uMqTTER75MY0z3LSs0kdcYDAIPNYPYYjM12p+nfddJt1LX0ndTZulMdlJBX1KUpchDJQxK8J9VfQhuaO4drkdUnEeqNSzx6DG3wcyhhoAwC131/GpjCLFV0A0pjW+Zku0fbYD+7Wk1Sd0cs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=rczsvbwV; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="rczsvbwV" Received: from vm-dev.d0aziqou4eyuzblxqhi51nu2sc.xx.internal.cloudapp.net (unknown [4.246.87.102]) by linux.microsoft.com (Postfix) with ESMTPSA id 561C620B7167; Thu, 15 Oct 2026 16:06:39 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 561C620B7167 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1792105599; bh=Pzs3NBpcOk5ot9pXk28NWkBWsinkGHdDqizIJZj5RAE=; h=From:To:Cc:Subject:Date:From; b=rczsvbwVXh0yEK1F7eo+CspMM66P6MlHR0deTr/LYwLIfUNOCm8DUCoLuTcBqJFDv 9RJbEuinox5tExJsG2i2oqSk33JzTsY0xl8g1RNrB9eWSvsdWEDXqDb5zA2wqoZUyS AiPY53KCVSL0xCW5JxgWvPEkMQIJbvp4xgpdV1ro= From: Tahera Fahimi To: stable@vger.kernel.org Cc: davem@davemloft.net, steffen.klassert@secunet.com, herbert@gondor.apana.org.au, kuba@kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, apais@linux.microsoft.com, edumazet@google.com, pabeni@redhat.com Subject: [PATCH 5.15.y] xfrm: prevent policy_hthresh.work from racing with netns teardown Date: Thu, 15 Oct 2026 16:06:34 -0700 Message-ID: <20261015230634.2364063-1-taherafahimi@linux.microsoft.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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. Fixes: 880a6fab8f6b ("xfrm: configure policy hash table thresholds by netlink") Assisted-by: GitHub-Copilot:GPT-5.6 Sol Signed-off-by: Tahera Fahimi Reviewed-by: Allen Pais --- include/net/netns/xfrm.h | 1 + net/xfrm/xfrm_policy.c | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/include/net/netns/xfrm.h b/include/net/netns/xfrm.h index 7922ec72eaa68..eb9edddd73092 100644 --- a/include/net/netns/xfrm.h +++ b/include/net/netns/xfrm.h @@ -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; diff --git a/net/xfrm/xfrm_policy.c b/net/xfrm/xfrm_policy.c index 536b945e1eeb6..15d50b5a22ef1 100644 --- a/net/xfrm/xfrm_policy.c +++ b/net/xfrm/xfrm_policy.c @@ -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); -- 2.43.0