mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Bilal Hashim <AeeSec@126.com>
To: Christoph Hellwig <hch@lst.de>, Sagi Grimberg <sagi@grimberg.me>,
	Keith Busch <kbusch@kernel.org>,
	Chaitanya Kulkarni <kch@nvidia.com>,
	Guixin Liu <kanie@linux.alibaba.com>
Cc: linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org
Subject: [PATCH] nvmet: pr: fix use-after-free of per-controller ref during preempt-and-abort
Date: Wed, 02 Dec 2026 01:21:50 +0800	[thread overview]
Message-ID: <179614571010.1268.3635657573115762278@126.com> (raw)

In nvmet_ctrl_free(), nvmet_ctrl_destroy_pr() erases each controller's
struct nvmet_pr_per_ctrl_ref from ns->pr_per_ctrl_refs and immediately
calls percpu_ref_exit(&pc_ref->ref) and kfree(pc_ref) while holding only
subsys->lock (not ns->pr.pr_sem).

Concurrently, a Reservation Acquire with PREEMPT_AND_ABORT from another
host holds ns->pr.pr_sem (not subsys->lock) and iterates
ns->pr_per_ctrl_refs via xa_for_each() in both nvmet_pr_set_ctrl_to_abort()
and nvmet_pr_do_abort(). Because xa_for_each() only protects the XArray
lookup itself, nvmet_ctrl_destroy_pr() can erase, exit, and free pc_ref
while nvmet_pr_set_ctrl_to_abort() or nvmet_pr_do_abort() is dereferencing
pc_ref, waiting on pc_ref->confirm_done / pc_ref->free_done, calling
reinit_completion(), or calling percpu_ref_resurrect(&pc_ref->ref),
resulting in a slab use-after-free.

Note that holding subsys->lock across
wait_for_completion(&pc_ref->free_done) in nvmet_pr_do_abort() would
invert locking with commands that already hold pc_ref (via
nvmet_req_find_ns()) and subsequently acquire subsys->lock (such as
nvmet_execute_identify_ns_zns()).

Fix this by reference-counting struct nvmet_pr_per_ctrl_ref with a kref
and deferring percpu_ref_exit() and kfree() via call_rcu(). Acquire a
kref under rcu_read_lock() using kref_get_unless_zero() before dropping
rcu_read_lock() to operate on pc_ref in nvmet_pr_set_ctrl_to_abort() and
nvmet_pr_do_abort().

Found by manual code inspection. Verified in a 4-vCPU QEMU guest using
nvmet + nvme-loop with CONFIG_PROVE_LOCKING=y and
CONFIG_DEBUG_ATOMIC_SLEEP=y by running concurrent PREEMPT_AND_ABORT
commands against controller teardown across multiple rounds with zero
lockdep, RCU, or sleeping-in-atomic warnings.

Fixes: 5a47c2080a73 ("nvmet: support reservation feature")
Cc: stable@vger.kernel.org
Signed-off-by: Bilal Hashim <AeeSec@126.com>
---
 drivers/nvme/target/nvmet.h |  2 ++
 drivers/nvme/target/pr.c    | 62 +++++++++++++++++++++++++++++++++++----------
 2 files changed, 50 insertions(+), 14 deletions(-)

diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
index e33884a..ced6832 100644
--- a/drivers/nvme/target/nvmet.h
+++ b/drivers/nvme/target/nvmet.h
@@ -96,6 +96,8 @@ struct nvmet_pr_per_ctrl_ref {
 	struct completion	free_done;
 	struct completion	confirm_done;
 	uuid_t			hostid;
+	struct kref		kref;
+	struct rcu_head		rcu;
 };
 
 struct nvmet_ns {
diff --git a/drivers/nvme/target/pr.c b/drivers/nvme/target/pr.c
index 8c818f2..e5eb83a 100644
--- a/drivers/nvme/target/pr.c
+++ b/drivers/nvme/target/pr.c
@@ -474,19 +474,45 @@ static void nvmet_pr_confirm_ns_pc_ref(struct percpu_ref *ref)
 	complete(&pc_ref->confirm_done);
 }
 
+static void nvmet_pr_free_pc_ref_rcu(struct rcu_head *rcu)
+{
+	struct nvmet_pr_per_ctrl_ref *pc_ref =
+		container_of(rcu, struct nvmet_pr_per_ctrl_ref, rcu);
+
+	percpu_ref_exit(&pc_ref->ref);
+	kfree(pc_ref);
+}
+
+static void nvmet_pr_free_pc_ref(struct kref *kref)
+{
+	struct nvmet_pr_per_ctrl_ref *pc_ref =
+		container_of(kref, struct nvmet_pr_per_ctrl_ref, kref);
+
+	call_rcu(&pc_ref->rcu, nvmet_pr_free_pc_ref_rcu);
+}
+
 static void nvmet_pr_set_ctrl_to_abort(struct nvmet_req *req, uuid_t *hostid)
 {
 	struct nvmet_pr_per_ctrl_ref *pc_ref;
 	struct nvmet_ns *ns = req->ns;
 	unsigned long idx;
 
+	rcu_read_lock();
 	xa_for_each(&ns->pr_per_ctrl_refs, idx, pc_ref) {
-		if (uuid_equal(&pc_ref->hostid, hostid)) {
-			percpu_ref_kill_and_confirm(&pc_ref->ref,
-						nvmet_pr_confirm_ns_pc_ref);
-			wait_for_completion(&pc_ref->confirm_done);
-		}
+		if (!uuid_equal(&pc_ref->hostid, hostid))
+			continue;
+		if (!kref_get_unless_zero(&pc_ref->kref))
+			continue;
+		rcu_read_unlock();
+
+		percpu_ref_kill_and_confirm(&pc_ref->ref,
+					    nvmet_pr_confirm_ns_pc_ref);
+		wait_for_completion(&pc_ref->confirm_done);
+		kref_put(&pc_ref->kref, nvmet_pr_free_pc_ref);
+
+		rcu_read_lock();
 	}
+	rcu_read_unlock();
 }
 
 static u16 nvmet_pr_unreg_all_host_by_prkey(struct nvmet_req *req, u64 prkey,
@@ -631,14 +657,23 @@ static void nvmet_pr_do_abort(struct work_struct *w)
 	/*
 	 * The target does not support abort, just wait per-controller ref to 0.
 	 */
+	rcu_read_lock();
 	xa_for_each(&ns->pr_per_ctrl_refs, idx, pc_ref) {
+		if (!kref_get_unless_zero(&pc_ref->kref))
+			continue;
+		rcu_read_unlock();
+
 		if (percpu_ref_is_dying(&pc_ref->ref)) {
 			wait_for_completion(&pc_ref->free_done);
 			reinit_completion(&pc_ref->confirm_done);
 			reinit_completion(&pc_ref->free_done);
 			percpu_ref_resurrect(&pc_ref->ref);
 		}
+		kref_put(&pc_ref->kref, nvmet_pr_free_pc_ref);
+
+		rcu_read_lock();
 	}
+	rcu_read_unlock();
 
 	up(&ns->pr.pr_sem);
 	nvmet_req_complete(req, NVME_SC_SUCCESS);
@@ -1058,6 +1093,7 @@ static int nvmet_pr_alloc_and_insert_pc_ref(struct nvmet_ns *ns,
 	init_completion(&pc_ref->free_done);
 	init_completion(&pc_ref->confirm_done);
 	uuid_copy(&pc_ref->hostid, hostid);
+	kref_init(&pc_ref->kref);
 
 	ret = xa_insert(&ns->pr_per_ctrl_refs, idx, pc_ref, GFP_KERNEL);
 	if (ret)
@@ -1104,8 +1140,7 @@ free_per_ctrl_refs:
 		if (ns->pr.enable) {
 			pc_ref = xa_erase(&ns->pr_per_ctrl_refs, ctrl->cntlid);
 			if (pc_ref)
-				percpu_ref_exit(&pc_ref->ref);
-			kfree(pc_ref);
+				kref_put(&pc_ref->kref, nvmet_pr_free_pc_ref);
 		}
 	}
 	return ret;
@@ -1124,8 +1159,7 @@ void nvmet_ctrl_destroy_pr(struct nvmet_ctrl *ctrl)
 		if (ns->pr.enable) {
 			pc_ref = xa_erase(&ns->pr_per_ctrl_refs, ctrl->cntlid);
 			if (pc_ref)
-				percpu_ref_exit(&pc_ref->ref);
-			kfree(pc_ref);
+				kref_put(&pc_ref->kref, nvmet_pr_free_pc_ref);
 		}
 	}
 }
@@ -1156,9 +1190,9 @@ int nvmet_pr_init_ns(struct nvmet_ns *ns)
 
 free_per_ctrl_refs:
 	xa_for_each(&ns->pr_per_ctrl_refs, idx, pc_ref) {
-		xa_erase(&ns->pr_per_ctrl_refs, idx);
-		percpu_ref_exit(&pc_ref->ref);
-		kfree(pc_ref);
+		pc_ref = xa_erase(&ns->pr_per_ctrl_refs, idx);
+		if (pc_ref)
+			kref_put(&pc_ref->kref, nvmet_pr_free_pc_ref);
 	}
 	return ret;
 }
@@ -1180,8 +1214,8 @@ void nvmet_pr_exit_ns(struct nvmet_ns *ns)
 		 * No command on ns here, we can safely free pc_ref.
 		 */
 		pc_ref = xa_erase(&ns->pr_per_ctrl_refs, idx);
-		percpu_ref_exit(&pc_ref->ref);
-		kfree(pc_ref);
+		if (pc_ref)
+			kref_put(&pc_ref->kref, nvmet_pr_free_pc_ref);
 	}
 
 	xa_destroy(&ns->pr_per_ctrl_refs);


                 reply	other threads:[~2026-12-01 17:23 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=179614571010.1268.3635657573115762278@126.com \
    --to=aeesec@126.com \
    --cc=hch@lst.de \
    --cc=kanie@linux.alibaba.com \
    --cc=kbusch@kernel.org \
    --cc=kch@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    --cc=sagi@grimberg.me \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®