Skip to contentSkip to conversation

thunderbolt: property: fix a buffer overflow and a missing check

PATCH In review Kangjie Lu 3 participants 3 messages Mar 24, 2019 – Mar 27, 2019 Thunderbolt driver

lore
Share & tools
lore

Kangjie Lu

author
#0 lore raw

PATCH 1/1 thunderbolt: property: fix a buffer overflow and a missing check

First, no memory is allocated for "property->value.text"; the following strcpy will lead to a buffer overflow.

Second, no check is enforced for the return value of kzalloc, which may lead to NULL-pointer dereference.

The patch fixes the two issues.

---
 drivers/thunderbolt/property.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/thunderbolt/property.c +6 -1
index 841314deb446..d5b0cdb8f0b1 100644
@@ -587,7 +587,12 @@ int tb_property_add_text(struct tb_property_dir *parent, const char *key,
 		return -ENOMEM;
 
 	property->length = size / 4;
-	property->value.data = kzalloc(size, GFP_KERNEL);
+	property->value.text = kzalloc(size, GFP_KERNEL);
+	if (!property->value.text) {
+		kfree(property);
+		return -ENOMEM;
+	}
+
 	strcpy(property->value.text, text);
 
 	list_add_tail(&property->list, &parent->properties);
Signature · 1 line
2.17.1
#1 lore raw
Quoted 2 lines · Kangjie Lu

First, no memory is allocated for "property->value.text"; the following strcpy will lead to a buffer overflow.

It is actually member of union so assigning via value.txt or value.data is the same.

So no buffer overflow.

Quoted 2 lines

Second, no check is enforced for the return value of kzalloc, which may lead to NULL-pointer dereference.

Yes, this is valid.

Can you fix the changelog accordingly and resend?

Quoted 28 lines

The patch fixes the two issues.

---
 drivers/thunderbolt/property.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/thunderbolt/property.c +6 -1
index 841314deb446..d5b0cdb8f0b1 100644
@@ -587,7 +587,12 @@ int tb_property_add_text(struct tb_property_dir *parent, const char *key,
 		return -ENOMEM;
 
 	property->length = size / 4;
-	property->value.data = kzalloc(size, GFP_KERNEL);
+	property->value.text = kzalloc(size, GFP_KERNEL);
+	if (!property->value.text) {
+		kfree(property);
+		return -ENOMEM;
+	}
+
 	strcpy(property->value.text, text);
 
 	list_add_tail(&property->list, &parent->properties);
Signature · 1 line
2.17.1
Reviewed-by on v1 1/1
#2 lore raw
In reply to Kangjie Lu
Quoted 2 lines · Kangjie Lu

First, no memory is allocated for "property->value.text"; the following strcpy will lead to a buffer overflow.

Fix the commit text as there is no  overflow.

only the check and resource cleanp is the fix.

Quoted 7 lines

Second, no check is enforced for the return value of kzalloc, which may lead to NULL-pointer dereference.

The patch fixes the two issues.

Otherwise looks good.

-Mukesh

Quoted 22 lines
---
  drivers/thunderbolt/property.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
drivers/thunderbolt/property.c +6 -1
index 841314deb446..d5b0cdb8f0b1 100644
@@ -587,7 +587,12 @@ int tb_property_add_text(struct tb_property_dir *parent, const char *key,
  		return -ENOMEM;
 
  	property->length = size / 4;
-	property->value.data = kzalloc(size, GFP_KERNEL);
+	property->value.text = kzalloc(size, GFP_KERNEL);
+	if (!property->value.text) {
+		kfree(property);
+		return -ENOMEM;
+	}
+
  	strcpy(property->value.text, text);
 
  	list_add_tail(&property->list, &parent->properties);