Linux-NVME Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Guixin Liu <kanie@linux.alibaba.com>
To: Keith Busch <kbusch@kernel.org>, Jens Axboe <axboe@kernel.dk>,
	Christoph Hellwig <hch@lst.de>, Sagi Grimberg <sagi@grimberg.me>,
	Nilay Shroff <nilay@linux.ibm.com>,
	Daniel Wagner <dwagner@suse.de>, Hannes Reinecke <hare@suse.de>
Cc: linux-nvme@lists.infradead.org
Subject: [PATCH 4/4] nvme-fcloop: initialize port structures before publishing them
Date: Tue,  1 Dec 2026 19:53:45 +0800	[thread overview]
Message-ID: <20261201115345.2349875-5-kanie@linux.alibaba.com> (raw)
In-Reply-To: <20261201115345.2349875-1-kanie@linux.alibaba.com>

fcloop_create_remote_port() and fcloop_create_target_port() publish the
new port in nport before initializing its flags, lock, LS work and LS
list, and do so without fcloop_lock. A concurrent del_remote_port or
del_target_port write can pick up the half-initialized port and call
flush_work() on an uninitialized work_struct and list_empty() on an
uninitialized list head. The private area comes from kmalloc(), so
these fields contain heap garbage.

Initialize everything before publishing, and publish under fcloop_lock
so the unlink side sees either NULL or a fully initialized port.

Fixes: 38803fcffb5b ("nvme-fcloop: fix deallocation of working context")
Fixes: 437c0b824dbd ("nvme-fcloop: add target to host LS request support")
Cc: stable@vger.kernel.org
Signed-off-by: Guixin Liu <kanie@linux.alibaba.com>
---

Reproduced in a QEMU/KVM guest with nvme-fcloop. A TEST-ONLY
msleep(2000) in the test tree (not part of this patch) widens the
window between publishing nport->rport / nport->tport and initializing
the port fields; a concurrent del_remote_port / del_target_port write
0.5s into the window picks up the half-initialized port.

Unpatched: every iteration hits "WARNING: __flush_work" (workqueue.c)
and "WARNING: fcloop_targetport_delete" (list_empty on a garbage list
head; LIST_POISON values observed). The private area is kmalloc'd, not
zeroed. Patched, same recipe with the sleep moved before the locked
publish: the in-window delete finds no port (-ENOENT), 16 iterations
produce no warnings, and the normal add/delete sequence still works.

A separate KASAN use-after-free in nvmet_fc_rcv_ls_req() showed up when
udev auto-connect (70-nvmf-autoconnect.rules) raced the port deletion;
it is a pre-existing tgtport/LS lifetime issue unrelated to this patch
and does not occur with auto-connect disabled.

Regression without the injection: add/delete and fcloop IO pass.
 drivers/nvme/target/fcloop.c | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)

diff --git a/drivers/nvme/target/fcloop.c b/drivers/nvme/target/fcloop.c
index a9cde536c33ba..e0ce7c5d5ed5a 100644
--- a/drivers/nvme/target/fcloop.c
+++ b/drivers/nvme/target/fcloop.c
@@ -1445,6 +1445,7 @@ fcloop_create_remote_port(struct device *dev, struct device_attribute *attr,
 	struct fcloop_nport *nport;
 	struct fcloop_rport *rport;
 	struct nvme_fc_port_info pinfo;
+	unsigned long flags;
 	int ret;
 
 	nport = fcloop_alloc_nport(buf, count, true);
@@ -1467,19 +1468,22 @@ fcloop_create_remote_port(struct device *dev, struct device_attribute *attr,
 	/* success */
 	rport = remoteport->private;
 	rport->remoteport = remoteport;
-	rport->targetport = (nport->tport) ?  nport->tport->targetport : NULL;
-	if (nport->tport) {
-		nport->tport->remoteport = remoteport;
-		nport->tport->lport = nport->lport;
-	}
 	rport->nport = nport;
 	rport->lport = nport->lport;
-	nport->rport = rport;
 	rport->flags = 0;
 	spin_lock_init(&rport->lock);
 	INIT_WORK(&rport->ls_work, fcloop_rport_lsrqst_work);
 	INIT_LIST_HEAD(&rport->ls_list);
 
+	spin_lock_irqsave(&fcloop_lock, flags);
+	rport->targetport = (nport->tport) ?  nport->tport->targetport : NULL;
+	if (nport->tport) {
+		nport->tport->remoteport = remoteport;
+		nport->tport->lport = nport->lport;
+	}
+	nport->rport = rport;
+	spin_unlock_irqrestore(&fcloop_lock, flags);
+
 	return count;
 }
 
@@ -1547,6 +1551,7 @@ fcloop_create_target_port(struct device *dev, struct device_attribute *attr,
 	struct fcloop_nport *nport;
 	struct fcloop_tport *tport;
 	struct nvmet_fc_port_info tinfo;
+	unsigned long flags;
 	int ret;
 
 	nport = fcloop_alloc_nport(buf, count, false);
@@ -1567,17 +1572,20 @@ fcloop_create_target_port(struct device *dev, struct device_attribute *attr,
 	/* success */
 	tport = targetport->private;
 	tport->targetport = targetport;
-	tport->remoteport = (nport->rport) ?  nport->rport->remoteport : NULL;
-	if (nport->rport)
-		nport->rport->targetport = targetport;
 	tport->nport = nport;
 	tport->lport = nport->lport;
-	nport->tport = tport;
 	tport->flags = 0;
 	spin_lock_init(&tport->lock);
 	INIT_WORK(&tport->ls_work, fcloop_tport_lsrqst_work);
 	INIT_LIST_HEAD(&tport->ls_list);
 
+	spin_lock_irqsave(&fcloop_lock, flags);
+	tport->remoteport = (nport->rport) ?  nport->rport->remoteport : NULL;
+	if (nport->rport)
+		nport->rport->targetport = targetport;
+	nport->tport = tport;
+	spin_unlock_irqrestore(&fcloop_lock, flags);
+
 	return count;
 }
 
-- 
2.43.7



  parent reply	other threads:[~2026-12-01 11:54 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 11:53 [PATCH 0/4] nvme: small fixes from a drivers/nvme audit Guixin Liu
2026-12-01 11:53 ` [PATCH 1/4] nvme: guard nvme_strlen() against all-space strings Guixin Liu
2026-12-02  6:32   ` Christoph Hellwig
2026-12-01 11:53 ` [PATCH 2/4] nvme: trace the Set Features Save bit from the right position Guixin Liu
2026-12-02  6:32   ` Christoph Hellwig
2026-12-01 11:53 ` [PATCH 3/4] nvmet-fc: clear the DMA address of the fod that failed mapping Guixin Liu
2026-12-02  6:32   ` Christoph Hellwig
2026-12-01 11:53 ` Guixin Liu [this message]
2026-12-02  6:33   ` [PATCH 4/4] nvme-fcloop: initialize port structures before publishing them Christoph Hellwig

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=20261201115345.2349875-5-kanie@linux.alibaba.com \
    --to=kanie@linux.alibaba.com \
    --cc=axboe@kernel.dk \
    --cc=dwagner@suse.de \
    --cc=hare@suse.de \
    --cc=hch@lst.de \
    --cc=kbusch@kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    --cc=nilay@linux.ibm.com \
    --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