mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Yaohan Li <shunsheng1218@gmail.com>
To: "Martin K . Petersen" <mkp@kernel.org>
Cc: Hannes Reinecke <hare@suse.de>,
	linux-scsi@vger.kernel.org, target-devel@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH] scsi: target: core: use list_for_each_entry() in __core_scsi3_alloc_registration()
Date: Thu, 26 Nov 2026 09:36:50 +0800	[thread overview]
Message-ID: <20261126013650.2169132-1-shunsheng1218@gmail.com> (raw)

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")
Signed-off-by: Yaohan Li <shunsheng1218@gmail.com>
---
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(-)

diff --git a/drivers/target/target_core_pr.c b/drivers/target/target_core_pr.c
index ca1a2802aa228..660772d4674aa 100644
--- a/drivers/target/target_core_pr.c
+++ b/drivers/target/target_core_pr.c
@@ -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);
-- 
2.34.1


                 reply	other threads:[~2026-11-26  1:36 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=20261126013650.2169132-1-shunsheng1218@gmail.com \
    --to=shunsheng1218@gmail.com \
    --cc=hare@suse.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=mkp@kernel.org \
    --cc=target-devel@vger.kernel.org \
    /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®