Skip to contentSkip to conversation

nvdimm: fix possible null-ptr-deref in nd_dax_probe()

PATCH In review Yi Yang 4 participants 7 messages Oct 26, 2024 – Nov 13, 2024 libnvdimm: non-volatile memory device

lore
Share & tools
lore

Yi Yang

author
#0 lore raw

PATCH 1/1 nvdimm: fix possible null-ptr-deref in nd_dax_probe()

It will cause null-ptr-deref when nd_dax_alloc() returns NULL, fix it by add check for nd_dax_alloc().

Fixes c5ed9268643c ("libnvdimm, dax: autodetect support")
---
 drivers/nvdimm/dax_devs.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/nvdimm/dax_devs.c +4 -0
index 6b4922de3047..70a7e401f90d 100644
@@ -106,6 +106,10 @@ int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns)
 
 	nvdimm_bus_lock(&ndns->dev);
 	nd_dax = nd_dax_alloc(nd_region);
+	if (!nd_dax) {
+		nvdimm_bus_unlock(&ndns->dev);
+		return -ENOMEM;
+	}
 	nd_pfn = &nd_dax->nd_pfn;
 	dax_dev = nd_pfn_devinit(nd_pfn, ndns);
 	nvdimm_bus_unlock(&ndns->dev);
Signature · 1 line
2.25.1
#1 lore raw
Quoted 2 lines · Yi Yang

It will cause null-ptr-deref when nd_dax_alloc() returns NULL, fix it by add check for nd_dax_alloc().

Was this found with a real workload or just by inspection?

Ira

Quoted 26 lines
Fixes c5ed9268643c ("libnvdimm, dax: autodetect support")
---
 drivers/nvdimm/dax_devs.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/nvdimm/dax_devs.c +4 -0
index 6b4922de3047..70a7e401f90d 100644
@@ -106,6 +106,10 @@ int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns)
 
 	nvdimm_bus_lock(&ndns->dev);
 	nd_dax = nd_dax_alloc(nd_region);
+	if (!nd_dax) {
+		nvdimm_bus_unlock(&ndns->dev);
+		return -ENOMEM;
+	}
 	nd_pfn = &nd_dax->nd_pfn;
 	dax_dev = nd_pfn_devinit(nd_pfn, ndns);
 	nvdimm_bus_unlock(&ndns->dev);
Signature · 1 line
2.25.1
author
#2 lore raw
Quoted 8 lines · Ira Weiny
Quoted 2 lines · Yi Yang

It will cause null-ptr-deref when nd_dax_alloc() returns NULL, fix it by add check for nd_dax_alloc().

Was this found with a real workload or just by inspection?

Ira

I just found the problem by reading the code.

Regards,
Yiyang

Quoted 31 lines
Quoted 26 lines
Fixes c5ed9268643c ("libnvdimm, dax: autodetect support")
---
  drivers/nvdimm/dax_devs.c | 4 ++++
  1 file changed, 4 insertions(+)
drivers/nvdimm/dax_devs.c +4 -0
index 6b4922de3047..70a7e401f90d 100644
@@ -106,6 +106,10 @@ int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns)
 
  	nvdimm_bus_lock(&ndns->dev);
  	nd_dax = nd_dax_alloc(nd_region);
+	if (!nd_dax) {
+		nvdimm_bus_unlock(&ndns->dev);
+		return -ENOMEM;
+	}
  	nd_pfn = &nd_dax->nd_pfn;
  	dax_dev = nd_pfn_devinit(nd_pfn, ndns);
  	nvdimm_bus_unlock(&ndns->dev);
Signature · 1 line
2.25.1

.

#3 lore raw
In reply to Yi Yang
Quoted 14 lines · Yi Yang

It will cause null-ptr-deref when nd_dax_alloc() returns NULL, fix it by add check for nd_dax_alloc().

Fixes c5ed9268643c ("libnvdimm, dax: autodetect support")
---
 drivers/nvdimm/dax_devs.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/nvdimm/dax_devs.c +0 -0
index 6b4922de3047..70a7e401f90d 100644

drivers/nvdimm/dax_devs.c int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns) · line 113

Quoted 8 lines
drivers/nvdimm/dax_devs.c +4 -0
@@ -106,6 +106,10 @@ int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns)
 
 	nvdimm_bus_lock(&ndns->dev);
 	nd_dax = nd_dax_alloc(nd_region);
+	if (!nd_dax) {
+		nvdimm_bus_unlock(&ndns->dev);
+		return -ENOMEM;
+	}
 	nd_pfn = &nd_dax->nd_pfn;

No, this isn't a NULL pointer de-reference, but it is indeed unreasonably subtle.

If nd_dax is NULL, then nd_pfn is NULL because nd_dax is just a type-wrapper around nd_pfn.

When nd_pfn is NULL then nd_pfn_devinit will fail.

What I think this needs to make this clear is something like this:

---

drivers/nvdimm/dax_devs.c +2 -2
index 6b4922de3047..37b743acbb7b 100644
@@ -106,12 +106,12 @@ int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns)
 
 	nvdimm_bus_lock(&ndns->dev);
 	nd_dax = nd_dax_alloc(nd_region);
-	nd_pfn = &nd_dax->nd_pfn;
-	dax_dev = nd_pfn_devinit(nd_pfn, ndns);
+	dax_dev = nd_dax_devinit(nd_dax, ndns);
 	nvdimm_bus_unlock(&ndns->dev);
 	if (!dax_dev)
 		return -ENOMEM;
 	pfn_sb = devm_kmalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
+	nd_pfn = &nd_dax->nd_pfn;
 	nd_pfn->pfn_sb = pfn_sb;
 	rc = nd_pfn_validate(nd_pfn, DAX_SIG);
 	dev_dbg(dev, "dax: %s\n", rc == 0 ? dev_name(dax_dev) : "<none>");
drivers/nvdimm/nd.h +7 -0
index 2dbb1dca17b5..5ca06e9a2d29 100644
@@ -600,6 +600,13 @@ struct nd_dax *to_nd_dax(struct device *dev);
 int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns);
 bool is_nd_dax(const struct device *dev);
 struct device *nd_dax_create(struct nd_region *nd_region);
+static inline struct device *nd_dax_devinit(struct nd_dax *nd_dax,
+					    struct nd_namespace_common *ndns)
+{
+	if (!nd_dax)
+		return NULL;
+	return nd_pfn_devinit(&nd_dax->nd_pfn, ndns);
+}
 #else
 static inline int nd_dax_probe(struct device *dev,
 		struct nd_namespace_common *ndns)
author
#4 lore raw
Quoted 74 lines · Dan Williams
Quoted 14 lines · Yi Yang

It will cause null-ptr-deref when nd_dax_alloc() returns NULL, fix it by add check for nd_dax_alloc().

Fixes c5ed9268643c ("libnvdimm, dax: autodetect support")
---
  drivers/nvdimm/dax_devs.c | 4 ++++
  1 file changed, 4 insertions(+)
drivers/nvdimm/dax_devs.c +0 -0
index 6b4922de3047..70a7e401f90d 100644

drivers/nvdimm/dax_devs.c int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns) · line 113

Quoted 8 lines
drivers/nvdimm/dax_devs.c +4 -0
@@ -106,6 +106,10 @@ int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns)
 
  	nvdimm_bus_lock(&ndns->dev);
  	nd_dax = nd_dax_alloc(nd_region);
+	if (!nd_dax) {
+		nvdimm_bus_unlock(&ndns->dev);
+		return -ENOMEM;
+	}
  	nd_pfn = &nd_dax->nd_pfn;

No, this isn't a NULL pointer de-reference, but it is indeed unreasonably subtle.

If nd_dax is NULL, then nd_pfn is NULL because nd_dax is just a type-wrapper around nd_pfn.

When nd_pfn is NULL then nd_pfn_devinit will fail.

What I think this needs to make this clear is something like this:

---

drivers/nvdimm/dax_devs.c +2 -2
index 6b4922de3047..37b743acbb7b 100644
@@ -106,12 +106,12 @@ int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns)
 
  	nvdimm_bus_lock(&ndns->dev);
  	nd_dax = nd_dax_alloc(nd_region);
-	nd_pfn = &nd_dax->nd_pfn;
-	dax_dev = nd_pfn_devinit(nd_pfn, ndns);
+	dax_dev = nd_dax_devinit(nd_dax, ndns);
  	nvdimm_bus_unlock(&ndns->dev);
  	if (!dax_dev)
  		return -ENOMEM;
  	pfn_sb = devm_kmalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
+	nd_pfn = &nd_dax->nd_pfn;
  	nd_pfn->pfn_sb = pfn_sb;
  	rc = nd_pfn_validate(nd_pfn, DAX_SIG);
  	dev_dbg(dev, "dax: %s\n", rc == 0 ? dev_name(dax_dev) : "<none>");
drivers/nvdimm/nd.h +7 -0
index 2dbb1dca17b5..5ca06e9a2d29 100644
@@ -600,6 +600,13 @@ struct nd_dax *to_nd_dax(struct device *dev);
  int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns);
  bool is_nd_dax(const struct device *dev);
  struct device *nd_dax_create(struct nd_region *nd_region);
+static inline struct device *nd_dax_devinit(struct nd_dax *nd_dax,
+					    struct nd_namespace_common *ndns)
+{
+	if (!nd_dax)
+		return NULL;
+	return nd_pfn_devinit(&nd_dax->nd_pfn, ndns);
+}
  #else
  static inline int nd_dax_probe(struct device *dev,
  		struct nd_namespace_common *ndns)
Quoted 2 lines

.

LGTM,.
Your code is better.

Best regards,
Yiyang

author
#5 lore raw

PATCH 1/1 nvdimm: rectify the illogical code within nd_dax_probe()

In reply to Dan Williams

When nd_dax is NULL, nd_pfn is consequently NULL as well. Nevertheless, it is inadvisable to perform pointer arithmetic or address-taking on a NULL pointer.
Introduce the nd_dax_devinit() function to enhance the code's logic and improve its readability.

---
 drivers/nvdimm/dax_devs.c | 4 ++--
 drivers/nvdimm/nd.h       | 7 +++++++
 2 files changed, 9 insertions(+), 2 deletions(-)
drivers/nvdimm/dax_devs.c +2 -2
index 6b4922de3047..37b743acbb7b 100644
@@ -106,12 +106,12 @@ int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns)
 
 	nvdimm_bus_lock(&ndns->dev);
 	nd_dax = nd_dax_alloc(nd_region);
-	nd_pfn = &nd_dax->nd_pfn;
-	dax_dev = nd_pfn_devinit(nd_pfn, ndns);
+	dax_dev = nd_dax_devinit(nd_dax, ndns);
 	nvdimm_bus_unlock(&ndns->dev);
 	if (!dax_dev)
 		return -ENOMEM;
 	pfn_sb = devm_kmalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
+	nd_pfn = &nd_dax->nd_pfn;
 	nd_pfn->pfn_sb = pfn_sb;
 	rc = nd_pfn_validate(nd_pfn, DAX_SIG);
 	dev_dbg(dev, "dax: %s\n", rc == 0 ? dev_name(dax_dev) : "<none>");
drivers/nvdimm/nd.h +7 -0
index 2dbb1dca17b5..5ca06e9a2d29 100644
@@ -600,6 +600,13 @@ struct nd_dax *to_nd_dax(struct device *dev);
 int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns);
 bool is_nd_dax(const struct device *dev);
 struct device *nd_dax_create(struct nd_region *nd_region);
+static inline struct device *nd_dax_devinit(struct nd_dax *nd_dax,
+					    struct nd_namespace_common *ndns)
+{
+	if (!nd_dax)
+		return NULL;
+	return nd_pfn_devinit(&nd_dax->nd_pfn, ndns);
+}
 #else
 static inline int nd_dax_probe(struct device *dev,
 		struct nd_namespace_common *ndns)
Signature · 1 line
2.25.1
maintainer Reviewed-by on v1 1/1
#6 lore raw

Re: [PATCH] nvdimm: rectify the illogical code within nd_dax_probe()

Quoted 7 lines · Yi Yang

When nd_dax is NULL, nd_pfn is consequently NULL as well. Nevertheless, it is inadvisable to perform pointer arithmetic or address-taking on a NULL pointer.
Introduce the nd_dax_devinit() function to enhance the code's logic and improve its readability.

but a few comments for future patches:

Is this v2 of the patch? Please label patch subject prefix as so.

Quoted 1 line

---

Please include change history here from previous versions.

Quoted 41 lines
 drivers/nvdimm/dax_devs.c | 4 ++--
 drivers/nvdimm/nd.h       | 7 +++++++
 2 files changed, 9 insertions(+), 2 deletions(-)
drivers/nvdimm/dax_devs.c +2 -2
index 6b4922de3047..37b743acbb7b 100644
@@ -106,12 +106,12 @@ int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns)
 
 	nvdimm_bus_lock(&ndns->dev);
 	nd_dax = nd_dax_alloc(nd_region);
-	nd_pfn = &nd_dax->nd_pfn;
-	dax_dev = nd_pfn_devinit(nd_pfn, ndns);
+	dax_dev = nd_dax_devinit(nd_dax, ndns);
 	nvdimm_bus_unlock(&ndns->dev);
 	if (!dax_dev)
 		return -ENOMEM;
 	pfn_sb = devm_kmalloc(dev, sizeof(*pfn_sb), GFP_KERNEL);
+	nd_pfn = &nd_dax->nd_pfn;
 	nd_pfn->pfn_sb = pfn_sb;
 	rc = nd_pfn_validate(nd_pfn, DAX_SIG);
 	dev_dbg(dev, "dax: %s\n", rc == 0 ? dev_name(dax_dev) : "<none>");
drivers/nvdimm/nd.h +7 -0
index 2dbb1dca17b5..5ca06e9a2d29 100644
@@ -600,6 +600,13 @@ struct nd_dax *to_nd_dax(struct device *dev);
 int nd_dax_probe(struct device *dev, struct nd_namespace_common *ndns);
 bool is_nd_dax(const struct device *dev);
 struct device *nd_dax_create(struct nd_region *nd_region);
+static inline struct device *nd_dax_devinit(struct nd_dax *nd_dax,
+					    struct nd_namespace_common *ndns)
+{
+	if (!nd_dax)
+		return NULL;
+	return nd_pfn_devinit(&nd_dax->nd_pfn, ndns);
+}
 #else
 static inline int nd_dax_probe(struct device *dev,
 		struct nd_namespace_common *ndns)