Skip to contentSkip to conversation

nvme-pci: fix NULL pointer dereference in nvme_free_host_mem()

PATCH New Minwoo Im 1 participant 2 messages Nov 24, 2017 – Nov 30, 2017 NVMe

lore
Share & tools
lore

Minwoo Im

author
#0 lore raw

PATCH 1/1 nvme-pci: fix NULL pointer dereference in nvme_free_host_mem()

Following condition which will cause NULL pointer dereference will occur in nvme_free_host_mem() when it tries to remove pci device via nvme_remove() especially after a failure of host memory allocation for HMB.

    "(host_mem_descs == NULL) && (nr_host_mem_descs != 0)"

It's because __nr_host_mem_descs__ is not cleared to 0 unlike __host_mem_descs__ is so.

Signed-off-by Minwoo Im <minwoo.im.dev at gmail.com>
---
 drivers/nvme/host/pci.c | 1 +
 1 file changed, 1 insertion(+)
drivers/nvme/host/pci.c +1 -0
index 6173747..f5800c3 100644
@@ -1759,6 +1759,7 @@ static void nvme_free_host_mem(struct nvme_dev *dev)
 			dev->nr_host_mem_descs * sizeof(*dev->host_mem_descs),
 			dev->host_mem_descs, dev->host_mem_descs_dma);
 	dev->host_mem_descs = NULL;
+	dev->nr_host_mem_descs = 0;
 }
 
 static int __nvme_alloc_host_mem(struct nvme_dev *dev, u64 preferred,
Signature · 1 line
2.7.4
author
#1 lore raw

PATCH 1/1 nvme-pci: fix NULL pointer dereference in nvme_free_host_mem()

Quoted 27 lines · Minwoo Im

Following condition which will cause NULL pointer dereference will occur in nvme_free_host_mem() when it tries to remove pci device via nvme_remove() especially after a failure of host memory allocation for HMB.

    "(host_mem_descs == NULL) && (nr_host_mem_descs != 0)"

It's because __nr_host_mem_descs__ is not cleared to 0 unlike __host_mem_descs__ is so.

Signed-off-by Minwoo Im <minwoo.im.dev at gmail.com>
---
 drivers/nvme/host/pci.c | 1 +
 1 file changed, 1 insertion(+)
drivers/nvme/host/pci.c +1 -1
index 6173747..f5800c3 100644
@@ -1759,6 +1759,7 @@ static void nvme_free_host_mem(struct nvme_dev *dev)
                        dev->nr_host_mem_descs * sizeof(*dev->host_mem_descs),
                        dev->host_mem_descs, dev->host_mem_descs_dma);
        dev->host_mem_descs = NULL;
+       dev->nr_host_mem_descs = 0;
 }
 
 static int __nvme_alloc_host_mem(struct nvme_dev *dev, u64 preferred,
--
Quoted 2 lines

2.7.4

Hi Christoph,

I saw a commit (7e5dd57ef3081ff6c03908d786ed5087f6fbb7ae) for this patch on nvme-4.15.
Thanks to you for picking it up.

Is there anything I missed or need to know about "freeing host memory"?

Thanks,