Skip to contentSkip to conversation

drivers/nvme/host/rdma.c: Fix double freeing of async event data

PATCH New Prabhath Sajeepa 3 participants 7 messages Nov 20, 2018 – Nov 22, 2018 2 messages not in the archive NVMe

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drivers/nvme/host/rdma.c: Fix double freeing of async event data

Some error paths in configuration of admin queue free data buffer associated with async request SQE without resetting the data buffer pointer to NULL, This buffer is also freed up again if the controller is shutdown or reset.

---
 drivers/nvme/host/rdma.c | 1 +
 1 file changed, 1 insertion(+)
drivers/nvme/host/rdma.c +1 -0
index d181caf..b61ae21 100644
@@ -823,6 +823,7 @@ static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl,
 out_free_async_qe:
 	nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe,
 		sizeof(struct nvme_command), DMA_TO_DEVICE);
+	ctrl->async_event_sqe.data = NULL;
 out_free_queue:
 	nvme_rdma_free_queue(&ctrl->queues[0]);
 	return error;
Signature · 1 line
2.7.4
maintainer
#1 lore raw

PATCH 1/1 drivers/nvme/host/rdma.c: Fix double freeing of async event data

Reviewed-by Sagi Grimberg <sagi at grimberg.me>
#2 lore raw

PATCH 1/1 drivers/nvme/host/rdma.c: Fix double freeing of async event data

In reply to Prabhath Sajeepa

drivers/nvme/host/rdma.c static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl, · line 829

Quoted 13 lines · Prabhath Sajeepa

Some error paths in configuration of admin queue free data buffer associated with async request SQE without resetting the data buffer pointer to NULL, This buffer is also freed up again if the controller is shutdown or reset.

drivers/nvme/host/rdma.c +1 -0
@@ -823,6 +823,7 @@ static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl,
 out_free_async_qe:
        nvme_rdma_free_qe(ctrl->device->dev, &ctrl->async_event_sqe,
                sizeof(struct nvme_command), DMA_TO_DEVICE);
+       ctrl->async_event_sqe.data = NULL;
 out_free_queue:
        nvme_rdma_free_queue(&ctrl->queues[0]);
        return error;

Fix looks good to me, although while reviewing this I noticed we have a related bug too.

In nvme_rdma_alloc_qe() we do:

    qe->data = kzalloc(capsule_size, GFP_KERNEL);
    if (!qe->data)
        return -ENOMEM;

    qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir);
    if (ib_dma_mapping_error(ibdev, qe->dma)) {
        kfree(qe->data);
        return -ENOMEM;
    }

if the ib_dma_map_single() fails then we'll free qe->data but not NULL it out. If this happens for async_event_sqe in nvme_rdma_configure_admin_queue() then we'll be vulnerable to the same double free in nvme_rdma_destroy_admin_queue() that this patch is fixing.

Not sure if that's worth fixing in the same patch here; but I guess we should NULL out qe->data if the DMA mapping fails in alloc_qe...

- R.

maintainer
#3 lore raw

PATCH 1/1 drivers/nvme/host/rdma.c: Fix double freeing of async event data

Quoted 16 lines

Fix looks good to me, although while reviewing this I noticed we have a related bug too.

In nvme_rdma_alloc_qe() we do:

     qe->data = kzalloc(capsule_size, GFP_KERNEL);
     if (!qe->data)
         return -ENOMEM;

     qe->dma = ib_dma_map_single(ibdev, qe->data, capsule_size, dir);
     if (ib_dma_mapping_error(ibdev, qe->dma)) {
         kfree(qe->data);
         return -ENOMEM;
     }
Quoted 5 lines

if the ib_dma_map_single() fails then we'll free qe->data but not NULL it out. If this happens for async_event_sqe in nvme_rdma_configure_admin_queue() then we'll be vulnerable to the same double free in nvme_rdma_destroy_admin_queue() that this patch is fixing.

Fully agree, good catch!

Quoted 2 lines

Not sure if that's worth fixing in the same patch here; but I guess we should NULL out qe->data if the DMA mapping fails in alloc_qe...

Probably makes sense to have it in the same patch.

A message that is not in the archive (sent off-list or not yet received): CAE=VkfB4G8PFZuNihQR0fei9BSr5ayqOrMWjFq9Xo5anQNgk7w@mail.gmail.com

#5 lore raw

PATCH 1/1 drivers/nvme/host/rdma.c: Fix double freeing of async event data

Quoted 2 lines · Prabhath Sajeepa

So, Does it make sense to set
qe->data = NULL immediately after kfree(qe->data) in nvme_rdma_free_qe() ?

I thought about suggesting that; I have no objection although most calls of free_qe are in nvme_rdma_free_ring() where we free the containing structure immediately after.

However that doesn't fix the bug I spotted where alloc_qe fails the DMA mapping, because we don't call free_qe in that path. I think we should set qe->data to NULL in alloc_qe when the DMA mapping fails, or else the caller needs to know that alloc_qe can return an error but leave a non-NULL pointer in qe->data.

- R.

A message that is not in the archive (sent off-list or not yet received): CAE=VkfBvD9gG3MuwptCdpZso3t0JsFTvO-s1q+aW9=Zm4QTR=Q@mail.gmail.com

#7 lore raw

PATCH 1/1 drivers/nvme/host/rdma.c: Fix double freeing of async event data

Quoted 2 lines · Prabhath Sajeepa

Yes, we need to set qe->data to NULL in alloc_qe failure path as well.
I was suggesting setting of qe->data to NULL in nvme_rdma_free_qe() in addition to the fix suggested by you.

I'm ambivalent, I think either way of handling clearing data after free_qe is fine. Two out of the three calls to free_qe need the "set to NULL", so I personally don't have a strong feeling about whether open-coding the data=NULL or putting it in the free function is better.

- R.

maintainer
#8 lore raw

PATCH 1/1 drivers/nvme/host/rdma.c: Fix double freeing of async event data

Quoted 10 lines
Quoted 2 lines · <[email protected]> wrote:

Yes, we need to set qe->data to NULL in alloc_qe failure path as well.
I was suggesting setting of qe->data to NULL in nvme_rdma_free_qe() in addition to the fix suggested by you.

I'm ambivalent, I think either way of handling clearing data after free_qe is fine. Two out of the three calls to free_qe need the "set to NULL", so I personally don't have a strong feeling about whether open-coding the data=NULL or putting it in the free function is better.

How about simply NULLing for the specific flow that Roland spotted in addition to yours?