Skip to contentSkip to conversation

Fix use-after-free bug when ports are removed

PATCH Superseded Logan Gunthorpe 2 participants 8 messages Jul 3, 2019

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Fix use-after-free bug when ports are removed

Hey,

NVME target ports can be removed while there are still active controllers. Largely this is fine, except some admin commands can access the req->port (for example, id-ctrl uses the port's inline date size as part of it's response). This was found while testing with KASAN.

Two patches follow which disconnect active controllers when the ports are removed for loop and rdma. I'm not sure if fc has the same issue and have no way to test this.

Alternatively, we could add reference counting to the struct port, but I think this is a more involved change and could be done later after we fix the bug quickly.

Thanks,

Logan

--

Logan Gunthorpe (2):
nvmet-loop: Fix use-after-free bug when a port is removed
nvmet-rdma: Fix use-after-free bug when a port is removed

 drivers/nvme/target/loop.c | 11 +++++++++++
 drivers/nvme/target/rdma.c | 16 ++++++++++++++++
 2 files changed, 27 insertions(+)

--
2.20.1

#1 lore raw

PATCH 1/2 nvmet-loop: Fix use-after-free bug when a port is removed

When a port is removed through configfs, any connected controllers are still active and can still send commands. This causes a use-after-free bug which is detected by KASAN for any admin command that dereferences req->port (like in nvmet_execute_identify_ctrl).

To fix this, delete all active controllers for that port in nvme_loop_remove_port().

Signed-off-by Logan Gunthorpe <logang at deltatee.com>
---
 drivers/nvme/target/loop.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/nvme/target/loop.c +11 -0
index 9e211ad6bdd3..b1bee71bee47 100644
@@ -651,9 +651,20 @@ static int nvme_loop_add_port(struct nvmet_port *port)
 
 static void nvme_loop_remove_port(struct nvmet_port *port)
 {
+	struct nvme_loop_ctrl *ctrl;
+
 	mutex_lock(&nvme_loop_ports_mutex);
 	list_del_init(&port->entry);
 	mutex_unlock(&nvme_loop_ports_mutex);
+
+	mutex_lock(&nvme_loop_ctrl_mutex);
+	list_for_each_entry(ctrl, &nvme_loop_ctrl_list, list) {
+		if (ctrl->port == port)
+			nvme_delete_ctrl(&ctrl->ctrl);
+	}
+	mutex_unlock(&nvme_loop_ctrl_mutex);
+
+	flush_workqueue(nvme_delete_wq);
 }
 
 static const struct nvmet_fabrics_ops nvme_loop_ops = {
Signature · 1 line
2.20.1
#2 lore raw

PATCH 2/2 nvmet-rdma: Fix use-after-free bug when a port is removed

In reply to Logan Gunthorpe

When a port is removed through configfs, any connected controllers are still active and can still send commands. This causes a use-after-free bug which is detected by KASAN for any admin command that dereferences req->port (like in nvmet_execute_identify_ctrl).

To fix this, disconnect all active queues that use the same port in nvme_rdma_remove_port().

Signed-off-by Logan Gunthorpe <logang at deltatee.com>
---
 drivers/nvme/target/rdma.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
drivers/nvme/target/rdma.c +16 -0
index 36d906a7f70d..6db9f9586ca7 100644
@@ -1580,9 +1580,25 @@ static int nvmet_rdma_add_port(struct nvmet_port *port)
 static void nvmet_rdma_remove_port(struct nvmet_port *port)
 {
 	struct rdma_cm_id *cm_id = xchg(&port->priv, NULL);
+	struct nvmet_rdma_queue *queue;
 
 	if (cm_id)
 		rdma_destroy_id(cm_id);
+
+restart:
+	mutex_lock(&nvmet_rdma_queue_mutex);
+
+	list_for_each_entry(queue, &nvmet_rdma_queue_list, queue_list) {
+		if (queue->port == port) {
+			list_del_init(&queue->queue_list);
+			mutex_unlock(&nvmet_rdma_queue_mutex);
+
+			__nvmet_rdma_queue_disconnect(queue);
+			goto restart;
+		}
+	}
+
+	mutex_unlock(&nvmet_rdma_queue_mutex);
 }
 
 static void nvmet_rdma_disc_port_addr(struct nvmet_req *req,
Signature · 1 line
2.20.1
maintainer
#3 lore raw
In reply to Logan Gunthorpe
Quoted 1 line

Hey,

Hey Logan,

Quoted 6 lines

NVME target ports can be removed while there are still active controllers. Largely this is fine, except some admin commands can access the req->port (for example, id-ctrl uses the port's inline date size as part of it's response). This was found while testing with KASAN.

Quoted 7 lines

Two patches follow which disconnect active controllers when the ports are removed for loop and rdma. I'm not sure if fc has the same issue and have no way to test this.

Alternatively, we could add reference counting to the struct port, but I think this is a more involved change and could be done later after we fix the bug quickly.

I don't think that when removing a port the expectation is that all associated controllers remain intact (although they can, which was why we did not remove them), so I think its fine to change that if it causes issues.

Can we handle this in the core instead (also so we'd be consistent across transports)?

How about this untested patch instead?
--

drivers/nvme/target/core.c +0 -0
index 0587707b1a25..12b58e568810 100644
@@ -277,6 +277,21 @@ void nvmet_unregister_transport(const struct 

nvmet_fabrics_ops *ops)
}
EXPORT_SYMBOL_GPL(nvmet_unregister_transport);

+void nvmet_port_del_ctrls(struct nvmet_port *port) +{
+ struct nvmet_subsys_link *l;
+ struct nvmet_ctrl *ctrl;
+
+ list_for_each_entry(l, &port->subsystems, entry) {
+ mutex_lock(&l->subsys->lock);
+ list_for_each_entry(ctrl, &l->subsys->ctrls, subsys_entry) {
+ if (ctrl->port == port)
+ ctrl->ops->delete_ctrl(ctrl);
+ }
+ mutex_unlock(&l->subsys->lock);
+ }
+}
+
int nvmet_enable_port(struct nvmet_port *port)

  {
         const struct nvmet_fabrics_ops *ops;

@@ -321,6 +336,8 @@ void nvmet_disable_port(struct nvmet_port *port)

         lockdep_assert_held(&nvmet_config_sem);

+ nvmet_port_del_ctrls(port);
+

         port->enabled = false;
         port->tr_ops = NULL;

--

#4 lore raw
Quoted 14 lines · Sagi Grimberg
Quoted 6 lines

NVME target ports can be removed while there are still active controllers. Largely this is fine, except some admin commands can access the req->port (for example, id-ctrl uses the port's inline date size as part of it's response). This was found while testing with KASAN.

Quoted 7 lines

Two patches follow which disconnect active controllers when the ports are removed for loop and rdma. I'm not sure if fc has the same issue and have no way to test this.

Alternatively, we could add reference counting to the struct port, but I think this is a more involved change and could be done later after we fix the bug quickly.

Quoted 7 lines

I don't think that when removing a port the expectation is that all associated controllers remain intact (although they can, which was why we did not remove them), so I think its fine to change that if it causes issues.

Can we handle this in the core instead (also so we'd be consistent across transports)?

Yes, that would be much better if we can sort out some other issues below...

Quoted 1 line

How about this untested patch instead?

I've found a couple of problems with the patch:

1) port->subsystems will always be empty before nvmet_disable_port() is called. We'd have to restructure things a little perhaps so that when a subsystem is removed from a port, all the active controllers for that subsys/port combo would be removed.

2) loop needs to call flush_workqueue(nvme_delete_wq) somewhere, otherwise there's a small window after the port disappears while commands can still be submitted. We can actually still hit the bug with a tight loop.

Maybe there's other cleanup that could be done to solve this: it does seem like all three transports need to keep their own lists of open controllers and loops through them to delete them. In theory, that could be made common so there's common code to manage the list per transport which would remove some boiler plate code. If we want to go this route though, I'd suggest using my patches as is for now and doing the cleanup in the next cycle.

Logan

maintainer
#5 lore raw
Quoted 6 lines
Quoted 3 lines

Can we handle this in the core instead (also so we'd be consistent across transports)?

Yes, that would be much better if we can sort out some other issues below...

Quoted 8 lines
Quoted 1 line

How about this untested patch instead?

I've found a couple of problems with the patch:

1) port->subsystems will always be empty before nvmet_disable_port() is called. We'd have to restructure things a little perhaps so that when a subsystem is removed from a port, all the active controllers for that subsys/port combo would be removed.

Yes, that is better.

Quoted 4 lines

2) loop needs to call flush_workqueue(nvme_delete_wq) somewhere, otherwise there's a small window after the port disappears while commands can still be submitted. We can actually still hit the bug with a tight loop.

We could simply flush the workqueue in nvme_loop_delete_ctrl for each controller?

Might be an overkill though, and its risking circular locking in case we are going via the fatal error path (work context flushing a different workqueue always annoys lockdep even when its perfectly safe)

Quoted 7 lines

Maybe there's other cleanup that could be done to solve this: it does seem like all three transports need to keep their own lists of open controllers and loops through them to delete them. In theory, that could be made common so there's common code to manage the list per transport which would remove some boiler plate code. If we want to go this route though, I'd suggest using my patches as is for now and doing the cleanup in the next cycle.

Then please fix tcp as well.

#6 lore raw
Quoted 9 lines · Sagi Grimberg
Quoted 9 lines
Quoted 3 lines

Can we handle this in the core instead (also so we'd be consistent across transports)?

Yes, that would be much better if we can sort out some other issues below...

Quoted 1 line

How about this untested patch instead?

Quoted 8 lines
Quoted 6 lines

I've found a couple of problems with the patch:

1) port->subsystems will always be empty before nvmet_disable_port() is called. We'd have to restructure things a little perhaps so that when a subsystem is removed from a port, all the active controllers for that subsys/port combo would be removed.

Yes, that is better.

Ok, if you like this solution I'll try and come up with a patch like that. It *may* not be too intrusive compared to the cleanup I suggested below.

Quoted 20 lines
Quoted 4 lines

2) loop needs to call flush_workqueue(nvme_delete_wq) somewhere, otherwise there's a small window after the port disappears while commands can still be submitted. We can actually still hit the bug with a tight loop.

We could simply flush the workqueue in nvme_loop_delete_ctrl for each controller?

Might be an overkill though, and its risking circular locking in case we are going via the fatal error path (work context flushing a different workqueue always annoys lockdep even when its perfectly safe)

Quoted 7 lines

Maybe there's other cleanup that could be done to solve this: it does seem like all three transports need to keep their own lists of open controllers and loops through them to delete them. In theory, that could be made common so there's common code to manage the list per transport which would remove some boiler plate code. If we want to go this route though, I'd suggest using my patches as is for now and doing the cleanup in the next cycle.

Quoted 1 line

Then please fix tcp as well.

Ok, I'll try to send either a destroy controller on subsystem-removal patch or I'll resend these with TCP included sometime today or tomorrow.

Thanks,

Logan

#7 lore raw
In reply to Logan Gunthorpe
Quoted 11 lines · Logan Gunthorpe

Hey,

NVME target ports can be removed while there are still active controllers. Largely this is fine, except some admin commands can access the req->port (for example, id-ctrl uses the port's inline date size as part of it's response). This was found while testing with KASAN.

Two patches follow which disconnect active controllers when the ports are removed for loop and rdma. I'm not sure if fc has the same issue and have no way to test this.

Oh, I also should have done a similar patch for tcp... Forgot about that one. It looks fairly straight forward, though (unlike fc).

Logan