All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] scsi: target: use kref_get_unless_zero() in PR deve lookups
@ 2026-12-02  7:38 Bin Luo (University of Electronic Science and Technology of China)
  0 siblings, 0 replies; only message in thread
From: Bin Luo (University of Electronic Science and Technology of China) @ 2026-12-02  7:38 UTC (permalink / raw)
  To: mkp; +Cc: michael.christie, bvanassche, linux-scsi, target-devel,
	linux-kernel

core_get_se_deve_from_rtpi(), __core_scsi3_do_alloc_registration(), and
__core_scsi3_check_aptpl_registration() look up a struct se_dev_entry on
nacl->lun_entry_hlist under rcu_read_lock() without holding
nacl->lun_entry_mutex, and then unconditionally call kref_get() on
deve->pr_kref.

Concurrently, core_disable_device_list_for_node() (and the dynamic-to-
explicit replacement path in core_enable_device_list_for_node()) unlinks
the entry via hlist_del_rcu(), drops the last pr_kref via kref_put(),
waits on orig->pr_comp, and frees the entry via RCU.  If an RCU reader
finds the entry before hlist_del_rcu() completes and reaches kref_get()
after pr_kref has already dropped to zero, it resurrects a zero refcount
and later dereferences a freed se_dev_entry after rcu_read_unlock():

  refcount_t: addition on 0; use-after-free.
  WARNING: lib/refcount.c:34 at refcount_warn_saturate+0xc0/0xe0
  Workqueue: target_submission target_queued_submit_work
  Call Trace:
   __core_scsi3_do_alloc_registration.isra.0+0x6ce/0x8e0
   __core_scsi3_alloc_registration+0x9b/0xd60
   core_scsi3_emulate_pro_register+0x5c4/0xe60
   target_scsi3_emulate_pr_out+0xe93/0x2040

Use kref_get_unless_zero() in all three RCU readers, skip entries whose
pr_kref has already reached zero, and avoid storing a dying se_dev_entry
pointer into pr_reg->pr_reg_deve before a reference is acquired.

Fixes: 29a05deebf6c ("target: Convert se_node_acl->device_list[] to RCU hlist")
Signed-off-by: Bin Luo (University of Electronic Science and Technology of China) <luob87709@gmail.com>
---
Found by static call-chain audit and reproduced on v7.4-rc4 (same code in
v7.4-rc5 / current master) in QEMU x86_64 with CONFIG_KASAN=y,
CONFIG_TARGET_CORE=y, CONFIG_LOOPBACK_TARGET=y, and preempt=full using
tcm_loop + configfs mapped LUN unlink/relink racing against SG_IO
PERSISTENT RESERVE OUT (with a kretprobe scheduling-delay helper on
target_nacl_find_deve()).

Validation:
- scripts/checkpatch.pl --strict: 0 errors, 0 warnings, 0 checks
- W=1 KASAN build of drivers/target/target_core_device.o and
  drivers/target/target_core_pr.o: 0 warnings, 0 errors
- Patched QEMU regression: 1000 PR iterations with workqueue preemptions,
  0 refcount warnings and 0 crashes.

 drivers/target/target_core_device.c |  3 ++-
 drivers/target/target_core_pr.c     | 20 +++++++++++---------
 2 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/drivers/target/target_core_device.c b/drivers/target/target_core_device.c
index c5d5d6a..46ade15 100644
--- a/drivers/target/target_core_device.c
+++ b/drivers/target/target_core_device.c
@@ -220,7 +220,8 @@ struct se_dev_entry *core_get_se_deve_from_rtpi(
 		if (lun->lun_tpg->tpg_rtpi != rtpi)
 			continue;
 
-		kref_get(&deve->pr_kref);
+		if (!kref_get_unless_zero(&deve->pr_kref))
+			continue;
 		rcu_read_unlock();
 
 		return deve;
diff --git a/drivers/target/target_core_pr.c b/drivers/target/target_core_pr.c
index ca1a280..3a866af 100644
--- a/drivers/target/target_core_pr.c
+++ b/drivers/target/target_core_pr.c
@@ -626,6 +626,7 @@ static struct t10_pr_registration *__core_scsi3_do_alloc_registration(
 	int aptpl)
 {
 	struct t10_pr_registration *pr_reg;
+	struct se_dev_entry *deve;
 
 	pr_reg = kmem_cache_zalloc(t10_pr_reg_cache, GFP_ATOMIC);
 	if (!pr_reg) {
@@ -652,15 +653,15 @@ static struct t10_pr_registration *__core_scsi3_do_alloc_registration(
 		pr_reg->pr_reg_deve = dest_deve;
 	} else {
 		rcu_read_lock();
-		pr_reg->pr_reg_deve = target_nacl_find_deve(nacl, mapped_lun);
-		if (!pr_reg->pr_reg_deve) {
+		deve = target_nacl_find_deve(nacl, mapped_lun);
+		if (!deve || !kref_get_unless_zero(&deve->pr_kref)) {
 			rcu_read_unlock();
 			pr_err("Unable to locate PR deve %s mapped_lun: %llu\n",
-				nacl->initiatorname, mapped_lun);
+			       nacl->initiatorname, mapped_lun);
 			kmem_cache_free(t10_pr_reg_cache, pr_reg);
 			return NULL;
 		}
-		kref_get(&pr_reg->pr_reg_deve->pr_kref);
+		pr_reg->pr_reg_deve = deve;
 		rcu_read_unlock();
 	}
 	pr_reg->pr_res_mapped_lun = mapped_lun;
@@ -927,6 +928,7 @@ static int __core_scsi3_check_aptpl_registration(
 {
 	struct t10_pr_registration *pr_reg, *pr_reg_tmp;
 	struct t10_reservation *pr_tmpl = &dev->t10_pr;
+	struct se_dev_entry *deve;
 	unsigned char i_port[PR_APTPL_MAX_IPORT_LEN] = { };
 	unsigned char t_port[PR_APTPL_MAX_TPORT_LEN] = { };
 	u16 tpgt;
@@ -958,14 +960,14 @@ static int __core_scsi3_check_aptpl_registration(
 			 * is released by __core_scsi3_add_registration() below.
 			 */
 			rcu_read_lock();
-			pr_reg->pr_reg_deve = target_nacl_find_deve(nacl, mapped_lun);
-			if (!pr_reg->pr_reg_deve) {
-				pr_err("Unable to locate PR APTPL %s mapped_lun:"
-					" %llu\n", nacl->initiatorname, mapped_lun);
+			deve = target_nacl_find_deve(nacl, mapped_lun);
+			if (!deve || !kref_get_unless_zero(&deve->pr_kref)) {
+				pr_err("Unable to locate PR APTPL %s mapped_lun: %llu\n",
+				       nacl->initiatorname, mapped_lun);
 				rcu_read_unlock();
 				continue;
 			}
-			kref_get(&pr_reg->pr_reg_deve->pr_kref);
+			pr_reg->pr_reg_deve = deve;
 			rcu_read_unlock();
 
 			pr_reg->pr_reg_nacl = nacl;
-- 
2.43.0


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-02  7:38 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02  7:38 [PATCH] scsi: target: use kref_get_unless_zero() in PR deve lookups Bin Luo (University of Electronic Science and Technology of China)

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.