nvmet: use the enabled transport when disabling a port
Subsystems (2)
PATCH 1/1 nvmet: use the enabled transport when disabling a port
nvmet_disable_port() looks up the transport from the current disc_addr.trtype, while nvmet_enable_port() records the transport in port->tr_ops. If trtype is changed while a port is being enabled, the port can be removed through a different transport, passing the private data of the original transport to the wrong ->remove_port().
The same mismatch is reachable with loop and RDMA: loop add_port() leaves port->priv NULL, and RDMA remove_port() dereferences it.
Use port->tr_ops for removal and serialize trtype changes with nvmet_config_sem in nvmet_addr_trtype_store(), so the transport that added the port is also the one that removes it.
--- drivers/nvme/target/configfs.c | 16 ++++++++++++---- drivers/nvme/target/core.c | 8 +++++--- 2 files changed, 17 insertions(+), 7 deletions(-)
drivers/nvme/target/configfs.c +12 -4
@@ -387,10 +387,15 @@ static ssize_t nvmet_addr_trtype_store(struct config_item *item, const char *page, size_t count) {struct nvmet_port *port =to_nvmet_port (item); +ssize_t ret = count;int i; -if (nvmet_is_port_enabled (port, __func__)) -return -EACCES ; +down_write (&nvmet_config_sem); + +if (nvmet_is_port_enabled (port, __func__)) { + ret = -EACCES ; + goto out; + }for (i =0 ; i <ARRAY_SIZE (nvmet_transport); i++) {if (sysfs_streq (page, nvmet_transport[i].name))
@@ -398,7 +403,8 @@ static ssize_t nvmet_addr_trtype_store(struct config_item *item, }pr_err ("Invalid value '%s' for trtype\n" , page); -return -EINVAL ; + ret = -EINVAL ; + goto out; found:memset (&port->disc_addr.tsas,0 ,NVMF_TSAS_SIZE );
@@ -407,7 +413,9 @@ static ssize_t nvmet_addr_trtype_store(struct config_item *item,nvmet_port_init_tsas_rdma (port); else if (port->disc_addr.trtype == NVMF_TRTYPE_TCP) nvmet_port_init_tsas_tcp(port, NVMF_TCP_SECTYPE_NONE); -return count; +out: +up_write (&nvmet_config_sem); +return ret; }CONFIGFS_ATTR (nvmet_, addr_trtype);
drivers/nvme/target/core.c +5 -3
@@ -393,11 +393,13 @@ void nvmet_disable_port(struct nvmet_port *port)lockdep_assert_held (&nvmet_config_sem); - port->enabled = false; - port->tr_ops =NULL ; +if (!port->enabled) +return ; - ops = nvmet_transports[port->disc_addr.trtype]; + ops = port->tr_ops; + port->enabled = false; ops->remove_port (port); + port->tr_ops =NULL ;module_put (ops->owner); }
base-commit: 864c07de0c7dcbca58b61625dfa2e461ae63b242
Signature · 1 line
2.39.5 (Apple Git-154)
Quoted 6 lines · Rongzheng Wang
nvmet_disable_port()looks up the transport from the current disc_addr.trtype, whilenvmet_enable_port()records the transport inport->tr_ops. If trtype is changed while a port is being enabled, the port can be removed through a different transport, passing the private data of the original transport to the wrong ->remove_port().
Quoted 6 lines
The same mismatch is reachable with loop and RDMA: loop
add_port()leavesport->privNULL, and RDMAremove_port()dereferences it.Use
port->tr_opsfor removal and serialize trtype changes withnvmet_config_seminnvmet_addr_trtype_store(), so the transport that added the port is also the one that removes it.
Thanks, applied to nvme-7.4.