Skip to contentSkip to conversation

scsi: target: core: use list_for_each_entry() in __core_scsi3_alloc_registration()

PATCH New Yaohan Li 1 participant 1 message Nov 26

Subsystems (2)
lore
Share & tools
lore

Yaohan Li

author
#0 lore raw

PATCH 1/1 scsi: target: core: use list_for_each_entry() in __core_scsi3_alloc_registration()

In __core_scsi3_alloc_registration(), the ALL_TG_PT=1 loop iterates over dev->dev_sep_list with list_for_each_entry_safe() while dropping dev->se_port_lock across each iteration.

list_for_each_entry_safe() caches the next entry pointer before the loop body runs. While dev->se_port_lock is dropped, another CPU can remove that cached next LUN via core_tpg_remove_lun(), calling list_del(&lun->lun_dev_link) and poisoning or freeing it. On the next iteration, the loop dereferences the stale cached pointer, causing a wild-memory-access / general protection fault (or use-after-free).

Meanwhile, the current entry lun_tmp cannot be unlinked or freed while dev->se_port_lock is dropped: the loop holds a live percpu_ref reference on lun_tmp->lun_ref, and core_tpg_remove_lun() (the only site that unlinks lun_dev_link) waits for transport_clear_lun_ref(lun) before acquiring dev->se_port_lock and calling list_del(&lun->lun_dev_link). After re-acquiring dev->se_port_lock and calling percpu_ref_put(), core_tpg_remove_lun() still cannot unlink lun_tmp until dev->se_port_lock is released.

Switch the loop to list_for_each_entry() so each step reads the current lun_tmp->lun_dev_link.next under dev->se_port_lock. This prevents dereferencing removed entries without skipping remaining live LUNs.

Fixes c66ac9db8d4a ("[SCSI] target: Add LIO target core v4.0.0-rc6")

---
Testing:
Reproduced KASAN wild-memory-access [0xdead000000000100-0xdead000000000107] and general protection fault in __core_scsi3_alloc_registration() on unmodified 666539eecdf4 under QEMU/KVM x86_64 (CONFIG_KASAN=y, CONFIG_PROVE_LOCKING=y, CONFIG_PROVE_RCU=y, CONFIG_DEBUG_ATOMIC_SLEEP=y) using tcm_loop + configfs (4 loopback HBAs, SG_IO PROUT REGISTER ALL_TG_PT=1 racing with concurrent configfs port symlink unlink of the middle LUN HBA3 while a timing-only kprobe parks the walker at target_depend_item() on HBA2).

With this patch applied:
- inject (middle LUN HBA3 removed mid-walk): PROUT REGISTER returns SAM_STAT_GOOD (status=0); PR IN READ KEYS and PR IN READ FULL STATUS on all 3 surviving target ports (HBA1, HBA2, HBA4) confirm 3 registrations with key 0x1234567890abcdef and ALL_TG_PT=1; direct PROUT RESERVE, PR IN READ RESERVATION, and PROUT REGISTER key update on HBA4 (the surviving LUN after removed middle LUN HBA3) all return status=0.
- ctl (same kprobe, no unlink): 4 keys / 4 full-status descriptors.
- 0 KASAN, 0 GPF, 0 lockdep, 0 WARNINGs.
- Compiled drivers/target/target_core_pr.o with W=1 (0 warnings).
AI assistance:
AI tools assisted with initial defect triage, patch/commit-message drafting, and QEMU test-harness preparation; the author reviewed the locking and percpu_ref invariants, verified the historical Fixes commit and QEMU KASAN/lockdep results, and takes full responsibility for the patch.

 drivers/target/target_core_pr.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/target/target_core_pr.c +2 -2
index ca1a2802aa228..660772d4674aa 100644
@@ -703,7 +703,7 @@ static struct t10_pr_registration *__core_scsi3_alloc_registration(
 	struct se_dev_entry *deve_tmp;
 	struct se_node_acl *nacl_tmp;
 	struct se_lun_acl *lacl_tmp;
-	struct se_lun *lun_tmp, *next, *dest_lun;
+	struct se_lun *lun_tmp, *dest_lun;
 	const struct target_core_fabric_ops *tfo = nacl->se_tpg->se_tpg_tfo;
 	struct t10_pr_registration *pr_reg, *pr_reg_atp, *pr_reg_tmp, *pr_reg_tmp_safe;
 	int ret;
@@ -726,7 +726,7 @@ static struct t10_pr_registration *__core_scsi3_alloc_registration(
 	 * for ALL_TG_PT=1
 	 */
 	spin_lock(&dev->se_port_lock);
-	list_for_each_entry_safe(lun_tmp, next, &dev->dev_sep_list, lun_dev_link) {
+	list_for_each_entry(lun_tmp, &dev->dev_sep_list, lun_dev_link) {
 		if (!percpu_ref_tryget_live(&lun_tmp->lun_ref))
 			continue;
 		spin_unlock(&dev->se_port_lock);
Signature · 1 line
2.34.1