Linux-NVME Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] nvmet: pr: fix use-after-free of per-controller ref during preempt-and-abort
@ 2026-12-01 17:21 Bilal Hashim
  2026-12-01 17:41 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Bilal Hashim @ 2026-12-01 17:21 UTC (permalink / raw)
  To: Christoph Hellwig, Sagi Grimberg, Keith Busch, Chaitanya Kulkarni,
	Guixin Liu
  Cc: linux-nvme, linux-kernel

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);



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] nvmet: pr: fix use-after-free of per-controller ref during preempt-and-abort
  2026-12-01 17:21 [PATCH] nvmet: pr: fix use-after-free of per-controller ref during preempt-and-abort Bilal Hashim
@ 2026-12-01 17:41 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-01 17:41 UTC (permalink / raw)
  To: Bilal Hashim; +Cc: linux-nvme

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] nvmet: pr: missing rcu_barrier() on module exit for call_rcu()
  callbacks
--

commit ebe935e60f135c9929e022f96b4706cc10465422
Author: Bilal Hashim <AeeSec@126.com>

nvmet: pr: fix use-after-free of per-controller ref during preempt-and-abort

This commit adds a kref and RCU-deferred teardown to struct
nvmet_pr_per_ctrl_ref so controller teardown cannot free pc_ref while
PREEMPT_AND_ABORT is iterating ns->pr_per_ctrl_refs.

> diff --git a/drivers/nvme/target/pr.c b/drivers/nvme/target/pr.c
> index 8c818f24815bd..e5eb83a3169d1 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);
> +}

[Severity: High]
Can nvmet_pr_free_pc_ref_rcu() run after the nvmet module is unloaded?

Neither nvmet_pr_exit_ns() nor nvmet_exit() calls rcu_barrier(), and
free_module() only calls synchronize_rcu(), which does not wait for
pending call_rcu() callbacks.  If nvmet.ko is unloaded right after tearing
down a controller or namespace with reservations enabled, the deferred
nvmet_pr_free_pc_ref_rcu() callback can execute after the module text has
been unmapped.  Adding rcu_barrier() to nvmet_exit() would ensure all
queued callbacks finish before the module is freed.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/179614571010.1268.3635657573115762278@126.com?part=1


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-01 17:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 17:21 [PATCH] nvmet: pr: fix use-after-free of per-controller ref during preempt-and-abort Bilal Hashim
2026-12-01 17:41 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox