nvmet-tcp: fix use-after-free when a port is removed
authorIsrael Rukshin <israelr@nvidia.com>
Wed, 6 Oct 2021 08:09:45 +0000 (08:09 +0000)
committerChristoph Hellwig <hch@lst.de>
Wed, 20 Oct 2021 17:16:00 +0000 (19:16 +0200)
When removing a port, all its controllers are being removed, but there
are queues on the port that doesn't belong to any controller (during
connection time). This causes a use-after-free bug for any command
that dereferences req->port (like in nvmet_alloc_ctrl). Those queues
should be destroyed before freeing the port via configfs. Destroy
the remaining queues after the accept_work was cancelled guarantees
that no new queue will be created.

Signed-off-by: Israel Rukshin <israelr@nvidia.com>
Reviewed-by: Max Gurtovoy <mgurtovoy@nvidia.com>
Signed-off-by: Christoph Hellwig <hch@lst.de>
drivers/nvme/target/tcp.c

index 07ee347ea3f3cf6d7cd5142e740dfa07ac9e5293..6eb0b31534773cf37b357b840b546ce169ff7ba8 100644 (file)
@@ -1737,6 +1737,17 @@ err_port:
        return ret;
 }
 
+static void nvmet_tcp_destroy_port_queues(struct nvmet_tcp_port *port)
+{
+       struct nvmet_tcp_queue *queue;
+
+       mutex_lock(&nvmet_tcp_queue_mutex);
+       list_for_each_entry(queue, &nvmet_tcp_queue_list, queue_list)
+               if (queue->port == port)
+                       kernel_sock_shutdown(queue->sock, SHUT_RDWR);
+       mutex_unlock(&nvmet_tcp_queue_mutex);
+}
+
 static void nvmet_tcp_remove_port(struct nvmet_port *nport)
 {
        struct nvmet_tcp_port *port = nport->priv;
@@ -1746,6 +1757,11 @@ static void nvmet_tcp_remove_port(struct nvmet_port *nport)
        port->sock->sk->sk_user_data = NULL;
        write_unlock_bh(&port->sock->sk->sk_callback_lock);
        cancel_work_sync(&port->accept_work);
+       /*
+        * Destroy the remaining queues, which are not belong to any
+        * controller yet.
+        */
+       nvmet_tcp_destroy_port_queues(port);
 
        sock_release(port->sock);
        kfree(port);