Skip to contentSkip to conversation

nvdimm: btt_devs: fix a NULL pointer dereference and a memory leak

PATCH In review Kangjie Lu 3 participants 5 messages Mar 12, 2019 – Mar 22, 2019

Subsystems (2)
lore
Share & tools
lore

Kangjie Lu

author
#0 lore raw

PATCH 1/1 nvdimm: btt_devs: fix a NULL pointer dereference and a memory leak

In case kmemdup fails, the fix releases resources and returns to avoid the NULL pointer dereference.
Also, the error paths in the following code should release resources to avoid memory leaks.

---
 drivers/nvdimm/btt_devs.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/nvdimm/btt_devs.c +7 -1
index 795ad4ff35ca..565ea0b6f765 100644
@@ -196,8 +196,13 @@ static struct device *__nd_btt_create(struct nd_region *nd_region,
 	}
 
 	nd_btt->lbasize = lbasize;
-	if (uuid)
+	if (uuid) {
 		uuid = kmemdup(uuid, 16, GFP_KERNEL);
+		if (!uuid) {
+			kfree(nd_btt);
+			return NULL;
+		}
+	}
 	nd_btt->uuid = uuid;
 	dev = &nd_btt->dev;
 	dev_set_name(dev, "btt%d.%d", nd_region->id, nd_btt->id);
@@ -209,6 +214,7 @@ static struct device *__nd_btt_create(struct nd_region *nd_region,
 		dev_dbg(&ndns->dev, "failed, already claimed by %s\n",
 				dev_name(ndns->claim));
 		put_device(dev);
+		kfree(uuid);
 		return NULL;
 	}
 	return dev;
Signature · 1 line
2.17.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Kangjie Lu

In case kmemdup fails, the fix releases resources and returns to avoid the NULL pointer dereference.
Also, the error paths in the following code should release resources to avoid memory leaks.

---
 drivers/nvdimm/btt_devs.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

Looks good,

Quoted 28 lines
drivers/nvdimm/btt_devs.c +6 -1
index 795ad4ff35ca..565ea0b6f765 100644
@@ -196,8 +196,13 @@ static struct device *__nd_btt_create(struct
 nd_region *nd_region,
 	}
 
 	nd_btt->lbasize = lbasize;
-	if (uuid)
+	if (uuid) {
 		uuid = kmemdup(uuid, 16, GFP_KERNEL);
+		if (!uuid) {
+			kfree(nd_btt);
+			return NULL;
+		}
+	}
 	nd_btt->uuid = uuid;
 	dev = &nd_btt->dev;
drivers/nvdimm/btt_devs.c +1 -0
 	dev_set_name(dev, "btt%d.%d", nd_region->id, nd_btt->id);
@@ -209,6 +214,7 @@ static struct device *__nd_btt_create(struct
 nd_region *nd_region,
 		dev_dbg(&ndns->dev, "failed, already claimed by %s\n",
 				dev_name(ndns->claim));
 		put_device(dev);
+		kfree(uuid);
 		return NULL;
 	}
Quoted 1 line

return dev;

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm

maintainer
#2 lore raw
Quoted 6 lines · Vishal L
Quoted 5 lines · Kangjie Lu

In case kmemdup fails, the fix releases resources and returns to avoid the NULL pointer dereference.
Also, the error paths in the following code should release resources to avoid memory leaks.

Quoted 8 lines
Quoted 5 lines
---
 drivers/nvdimm/btt_devs.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

Looks good,

Looking again, there is another kmemdup() call in __nd_btt_probe which is lacking error checking.
And referring to your other related patch to namespace_devs, that has /several/ instances of the same thing.

Instead of fixing just a couple of these, it might be worthwhile to send a wider cleanup patch to catch all of these, at least within drivers/nvdimm/ for starters, instead of a select few.

Quoted 27 lines
Quoted 27 lines
drivers/nvdimm/btt_devs.c +6 -1
index 795ad4ff35ca..565ea0b6f765 100644
@@ -196,8 +196,13 @@ static struct device *__nd_btt_create(struct
 nd_region *nd_region,
 	}
 
 	nd_btt->lbasize = lbasize;
-	if (uuid)
+	if (uuid) {
 		uuid = kmemdup(uuid, 16, GFP_KERNEL);
+		if (!uuid) {
+			kfree(nd_btt);
+			return NULL;
+		}
+	}
 	nd_btt->uuid = uuid;
 	dev = &nd_btt->dev;
drivers/nvdimm/btt_devs.c +1 -0
 	dev_set_name(dev, "btt%d.%d", nd_region->id, nd_btt->id);
@@ -209,6 +214,7 @@ static struct device *__nd_btt_create(struct
 nd_region *nd_region,
 		dev_dbg(&ndns->dev, "failed, already claimed by %s\n",
 				dev_name(ndns->claim));
 		put_device(dev);
+		kfree(uuid);
Quoted 8 lines
Quoted 3 lines
drivers/nvdimm/btt_devs.c +0 -0
 		return NULL;
 	}

return dev;

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm

_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm

maintainer
#3 lore raw
Quoted 16 lines · Vishal L
Quoted 6 lines · Vishal L
Quoted 5 lines · Kangjie Lu

In case kmemdup fails, the fix releases resources and returns to avoid the NULL pointer dereference.
Also, the error paths in the following code should release resources to avoid memory leaks.

Quoted 8 lines
Quoted 5 lines
---
 drivers/nvdimm/btt_devs.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

Looks good,

Quoted 8 lines

Looking again, there is another kmemdup() call in __nd_btt_probe which is lacking error checking.
And referring to your other related patch to namespace_devs, that has /several/ instances of the same thing.

Instead of fixing just a couple of these, it might be worthwhile to send a wider cleanup patch to catch all of these, at least within drivers/nvdimm/ for starters, instead of a select few.

Hm, ignore this comment - these are the only two unchecked instances.
_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm

#4 lore raw
In reply to Kangjie Lu
Quoted 16 lines · Kangjie Lu

In case kmemdup fails, the fix releases resources and returns to avoid the NULL pointer dereference.
Also, the error paths in the following code should release resources to avoid memory leaks.

---
 drivers/nvdimm/btt_devs.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/nvdimm/btt_devs.c +0 -0
index 795ad4ff35ca..565ea0b6f765 100644

drivers/nvdimm/btt_devs.c static struct device *__nd_btt_create(struct nd_region *nd_region, · line 203

Quoted 9 lines
drivers/nvdimm/btt_devs.c +4 -1
@@ -196,8 +196,13 @@ static struct device *__nd_btt_create(struct nd_region *nd_region,
        }
 
        nd_btt->lbasize = lbasize;
-       if (uuid)
+       if (uuid) {
                uuid = kmemdup(uuid, 16, GFP_KERNEL);
+               if (!uuid) {
+                       kfree(nd_btt);
+                       return NULL;

What about nd_btt->id? That needs to be released as well.

drivers/nvdimm/btt_devs.c static struct device *__nd_btt_create(struct nd_region *nd_region, · line 217

Quoted 10 lines
drivers/nvdimm/btt_devs.c +3 -0
+               }
+       }
        nd_btt->uuid = uuid;
        dev = &nd_btt->dev;
        dev_set_name(dev, "btt%d.%d", nd_region->id, nd_btt->id);
@@ -209,6 +214,7 @@ static struct device *__nd_btt_create(struct nd_region *nd_region,
                dev_dbg(&ndns->dev, "failed, already claimed by %s\n",
                                dev_name(ndns->claim));
                put_device(dev);
+               kfree(uuid);

This will be a double free because put_device() will arrange for nd_btt_release() to be called which does kfree(nd_btt->uuid);
_______________________________________________
Linux-nvdimm mailing list
[email protected]
https://lists.01.org/mailman/listinfo/linux-nvdimm