Skip to contentSkip to conversation

staging: dax: change sprintf to snprintf

PATCH In review Keerthi Reddy 3 participants 3 messages Sep 13, 2017 Device direct access (DAX)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: dax: change sprintf to snprintf

To avoid buffer overflow bugs like (CVE-120), we need to use snprintf function by also explicitly mentioning the size of the buffer.

found using flawfinder

---
 drivers/dax/device.c | 16 ++++++++--------
 drivers/dax/super.c  |  6 +++---
 2 files changed, 11 insertions(+), 11 deletions(-)
drivers/dax/device.c +8 -8
index e9f3b3e..f34e543 100644
@@ -30,31 +30,31 @@ static struct class *dax_class;
  * is cleared to assume that drvdata is always valid.
  */
 static ssize_t id_show(struct device *dev,
-		struct device_attribute *attr, char *buf)
+		struct device_attribute *attr, char *buf, size_t size)
 {
 	struct dax_region *dax_region = dev_get_drvdata(dev);
 
-	return sprintf(buf, "%d\n", dax_region->id);
+	return snprintf(buf, size, "%d\n", dax_region->id);
 }
 static DEVICE_ATTR_RO(id);
 
 static ssize_t region_size_show(struct device *dev,
-		struct device_attribute *attr, char *buf)
+		struct device_attribute *attr, char *buf, size_t size)
 {
 	struct dax_region *dax_region = dev_get_drvdata(dev);
 
-	return sprintf(buf, "%llu\n", (unsigned long long)
+	return snprintf(buf, size, "%llu\n", (unsigned long long)
 			resource_size(&dax_region->res));
 }
 static struct device_attribute dev_attr_region_size = __ATTR(size, 0444,
 		region_size_show, NULL);
 
 static ssize_t align_show(struct device *dev,
-		struct device_attribute *attr, char *buf)
+		struct device_attribute *attr, char *buf, size_t size)
 {
 	struct dax_region *dax_region = dev_get_drvdata(dev);
 
-	return sprintf(buf, "%u\n", dax_region->align);
+	return snprintf(buf, size, "%u\n", dax_region->align);
 }
 static DEVICE_ATTR_RO(align);
 
@@ -149,7 +149,7 @@ static struct dev_dax *to_dev_dax(struct device *dev)
 }
 
 static ssize_t size_show(struct device *dev,
-		struct device_attribute *attr, char *buf)
+		struct device_attribute *attr, char *buf, size_t buf_size)
 {
 	struct dev_dax *dev_dax = to_dev_dax(dev);
 	unsigned long long size = 0;
@@ -158,7 +158,7 @@ static ssize_t size_show(struct device *dev,
 	for (i = 0; i < dev_dax->num_resources; i++)
 		size += resource_size(&dev_dax->res[i]);
 
-	return sprintf(buf, "%llu\n", size);
+	return snprintf(buf, buf_size, "%llu\n", size);
 }
 static DEVICE_ATTR_RO(size);
 
drivers/dax/super.c +3 -3
index 938eb48..10bc198 100644
@@ -142,7 +142,7 @@ struct dax_device {
 };
 
 static ssize_t write_cache_show(struct device *dev,
-		struct device_attribute *attr, char *buf)
+		struct device_attribute *attr, char *buf, size_t size)
 {
 	struct dax_device *dax_dev = dax_get_by_host(dev_name(dev));
 	ssize_t rc;
@@ -151,8 +151,8 @@ static ssize_t write_cache_show(struct device *dev,
 	if (!dax_dev)
 		return -ENXIO;
 
-	rc = sprintf(buf, "%d\n", !!test_bit(DAXDEV_WRITE_CACHE,
-				&dax_dev->flags));
+	rc = snprintf(buf, size, "%d\n",
+		      !!test_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags));
 	put_dax(dax_dev);
 	return rc;
 }
Signature · 1 line
2.7.4
#1 lore raw
Quoted 10 lines · Keerthi Reddy

To avoid buffer overflow bugs like (CVE-120), we need to use snprintf function by also explicitly mentioning the size of the buffer.

found using flawfinder

---
 drivers/dax/device.c | 16 ++++++++--------
 drivers/dax/super.c  |  6 +++---

These don't look like staging drivers, nor drivers associated with any of the projects offered in this round. You can feel free to submit the patch to the maintainers of the driver, but it would have to go to some other people. There are instructions about how to get their names in the tutorial.

Please stick to staging drivers for the application period. We don't have any control over how quickly your patches are merged for other parts of the kernel.

julia

Quoted 96 lines
 2 files changed, 11 insertions(+), 11 deletions(-)
drivers/dax/device.c +8 -8
index e9f3b3e..f34e543 100644
@@ -30,31 +30,31 @@ static struct class *dax_class;
  * is cleared to assume that drvdata is always valid.
  */
 static ssize_t id_show(struct device *dev,
-		struct device_attribute *attr, char *buf)
+		struct device_attribute *attr, char *buf, size_t size)
 {
 	struct dax_region *dax_region = dev_get_drvdata(dev);
 
-	return sprintf(buf, "%d\n", dax_region->id);
+	return snprintf(buf, size, "%d\n", dax_region->id);
 }
 static DEVICE_ATTR_RO(id);
 
 static ssize_t region_size_show(struct device *dev,
-		struct device_attribute *attr, char *buf)
+		struct device_attribute *attr, char *buf, size_t size)
 {
 	struct dax_region *dax_region = dev_get_drvdata(dev);
 
-	return sprintf(buf, "%llu\n", (unsigned long long)
+	return snprintf(buf, size, "%llu\n", (unsigned long long)
 			resource_size(&dax_region->res));
 }
 static struct device_attribute dev_attr_region_size = __ATTR(size, 0444,
 		region_size_show, NULL);
 
 static ssize_t align_show(struct device *dev,
-		struct device_attribute *attr, char *buf)
+		struct device_attribute *attr, char *buf, size_t size)
 {
 	struct dax_region *dax_region = dev_get_drvdata(dev);
 
-	return sprintf(buf, "%u\n", dax_region->align);
+	return snprintf(buf, size, "%u\n", dax_region->align);
 }
 static DEVICE_ATTR_RO(align);
 
@@ -149,7 +149,7 @@ static struct dev_dax *to_dev_dax(struct device *dev)
 }
 
 static ssize_t size_show(struct device *dev,
-		struct device_attribute *attr, char *buf)
+		struct device_attribute *attr, char *buf, size_t buf_size)
 {
 	struct dev_dax *dev_dax = to_dev_dax(dev);
 	unsigned long long size = 0;
@@ -158,7 +158,7 @@ static ssize_t size_show(struct device *dev,
 	for (i = 0; i < dev_dax->num_resources; i++)
 		size += resource_size(&dev_dax->res[i]);
 
-	return sprintf(buf, "%llu\n", size);
+	return snprintf(buf, buf_size, "%llu\n", size);
 }
 static DEVICE_ATTR_RO(size);
 
drivers/dax/super.c +3 -4
index 938eb48..10bc198 100644
@@ -142,7 +142,7 @@ struct dax_device {
 };
 
 static ssize_t write_cache_show(struct device *dev,
-		struct device_attribute *attr, char *buf)
+		struct device_attribute *attr, char *buf, size_t size)
 {
 	struct dax_device *dax_dev = dax_get_by_host(dev_name(dev));
 	ssize_t rc;
@@ -151,8 +151,8 @@ static ssize_t write_cache_show(struct device *dev,
 	if (!dax_dev)
 		return -ENXIO;
 
-	rc = sprintf(buf, "%d\n", !!test_bit(DAXDEV_WRITE_CACHE,
-				&dax_dev->flags));
+	rc = snprintf(buf, size, "%d\n",
+		      !!test_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags));
 	put_dax(dax_dev);
 	return rc;
 }
--

2.7.4

--
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/20170913122100.GA5821%40manoharreddy.
For more options, visit https://groups.google.com/d/optout.

#2 lore raw
In reply to Keerthi Reddy
Quoted 3 lines · Keerthi Reddy

To avoid buffer overflow bugs like (CVE-120), we need to use snprintf function by also explicitly mentioning the size of the buffer.

No yo don't :)

Quoted 1 line

found using flawfinder

Bad tool, don't use it, it is wrong here.

Quoted 12 lines
---
 drivers/dax/device.c | 16 ++++++++--------
 drivers/dax/super.c  |  6 +++---
 2 files changed, 11 insertions(+), 11 deletions(-)
drivers/dax/device.c +0 -0
index e9f3b3e..f34e543 100644

drivers/dax/device.c static struct class *dax_class; · line 37

Quoted 10 lines
drivers/dax/device.c +2 -2
@@ -30,31 +30,31 @@ static struct class *dax_class;
  * is cleared to assume that drvdata is always valid.
  */
 static ssize_t id_show(struct device *dev,
-		struct device_attribute *attr, char *buf)
+		struct device_attribute *attr, char *buf, size_t size)
 {
 	struct dax_region *dax_region = dev_get_drvdata(dev);
 
-	return sprintf(buf, "%d\n", dax_region->id);
+	return snprintf(buf, size, "%d\n", dax_region->id);

It's impossible to overflow here, sorry. Please read the sysfs documentation for why.

Also, as Julia said, don't do outreachy patches for code that is not in drivers/staging/, as we can not accept it here.

good luck!

greg k-h