Skip to contentSkip to conversation

libcxl: fix a segfault when memdev->pmem is absent

PATCH In review Vishal Verma 3 participants 3 messages Jun 2, 2022 – Jun 16, 2022 libnvdimm: non-volatile memory device

lore
Share & tools
lore

Vishal Verma

author maintainer
#0 lore raw

PATCH 1/1 libcxl: fix a segfault when memdev->pmem is absent

A CXL memdev may not have any persistent capacity, and in this case it is possible that a 'pmem' object never gets instantiated. Such a scenario would cause free_pmem () to dereference a NULL pointer and segfault.

Fix this by only proceeding in free_pmem() if 'pmem' was valid.

Fixes cd1aed6cefe8 ("libcxl: add representation for an nvdimm bridge object")
Cc Dan Williams <[email protected]>
---
 cxl/lib/libcxl.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
cxl/lib/libcxl.c +5 -3
index 1ad4a0b..2578a43 100644
@@ -50,9 +50,11 @@ struct cxl_ctx {
 
 static void free_pmem(struct cxl_pmem *pmem)
 {
-	free(pmem->dev_buf);
-	free(pmem->dev_path);
-	free(pmem);
+	if (pmem) {
+		free(pmem->dev_buf);
+		free(pmem->dev_path);
+		free(pmem);
+	}
 }
 
 static void free_memdev(struct cxl_memdev *memdev, struct list_head *head)

base-commit: 4229f2694e8887a47c636a54130cff0d65f2e995

Signature · 1 line
2.36.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Vishal Verma

A CXL memdev may not have any persistent capacity, and in this case it is possible that a 'pmem' object never gets instantiated. Such a scenario would cause free_pmem () to dereference a NULL pointer and segfault.

Quoted 7 lines

Fix this by only proceeding in free_pmem() if 'pmem' was valid.

Fixes cd1aed6cefe8 ("libcxl: add representation for an nvdimm bridge object")
Cc Dan Williams <[email protected]>

---

Quoted 28 lines
 cxl/lib/libcxl.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
cxl/lib/libcxl.c +5 -3
index 1ad4a0b..2578a43 100644
@@ -50,9 +50,11 @@ struct cxl_ctx {
 
 static void free_pmem(struct cxl_pmem *pmem)
 {
-	free(pmem->dev_buf);
-	free(pmem->dev_path);
-	free(pmem);
+	if (pmem) {
+		free(pmem->dev_buf);
+		free(pmem->dev_path);
+		free(pmem);
+	}
 }
 
 static void free_memdev(struct cxl_memdev *memdev, struct list_head *head)

base-commit: 4229f2694e8887a47c636a54130cff0d65f2e995

Signature · 1 line
2.36.1
Reviewed-by on v1 1/1
#2 lore raw
In reply to Vishal Verma
Quoted 5 lines · Vishal Verma

A CXL memdev may not have any persistent capacity, and in this case it is possible that a 'pmem' object never gets instantiated. Such a scenario would cause free_pmem () to dereference a NULL pointer and segfault.

Quoted 7 lines

Fix this by only proceeding in free_pmem() if 'pmem' was valid.

Fixes cd1aed6cefe8 ("libcxl: add representation for an nvdimm bridge object")
Cc Dan Williams <[email protected]>