[PATCH 4/4] nvme-fcloop: initialize port structures before publishing them
Guixin Liu
kanie at linux.alibaba.com
Tue Dec 1 03:53:45 PST 2026
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 at vger.kernel.org
Signed-off-by: Guixin Liu <kanie at 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
More information about the Linux-nvme
mailing list