Skip to contentSkip to conversation

dax: replace sprintf() by scnprintf()

PATCH In review Salah Triki 2 participants 5 messages Jul 10, 2021 – Jul 12, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dax: replace sprintf() by scnprintf()

Replace sprintf() by scnprintf() in order to avoid buffer overflows.

---
 drivers/dax/bus.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/dax/bus.c +1 -1
index 5aee26e1bbd6..bcae4be6ae76 100644
@@ -76,7 +76,7 @@ static ssize_t do_id_store(struct device_driver *drv, const char *buf,
 	fields = sscanf(buf, "dax%d.%d", &region_id, &id);
 	if (fields != 2)
 		return -EINVAL;
-	sprintf(devname, "dax%d.%d", region_id, id);
+	scnprintf(devname, DAX_NAME_LEN, "dax%d.%d", region_id, id);
 	if (!sysfs_streq(buf, devname))
 		return -EINVAL;
 
Signature · 1 line
2.25.1
#1 lore raw
Quoted 1 line · Salah Triki

Replace sprintf() by scnprintf() in order to avoid buffer overflows.

OK but also not strictly necessary. DAX_NAME_LEN is 30.

Are you finding and changing these manually or with a script?

drivers/dax/bus.c

Quoted 1 line
drivers/dax/bus.c +0 -0

[]

Quoted 10 lines
drivers/dax/bus.c +1 -1
@@ -76,7 +76,7 @@ static ssize_t do_id_store(struct device_driver *drv, const char *buf,
  	fields = sscanf(buf, "dax%d.%d", &region_id, &id);
  	if (fields != 2)
  		return -EINVAL;
-	sprintf(devname, "dax%d.%d", region_id, id);
+	scnprintf(devname, DAX_NAME_LEN, "dax%d.%d", region_id, id);
  	if (!sysfs_streq(buf, devname))
  		return -EINVAL;

#2 lore raw
Quoted 9 lines · Joe Perches
Quoted 1 line · Salah Triki

Replace sprintf() by scnprintf() in order to avoid buffer overflows.

OK but also not strictly necessary. DAX_NAME_LEN is 30.

Are you finding and changing these manually or with a script?

drivers/dax/bus.c

Quoted 1 line
drivers/dax/bus.c +0 -0

[]

Quoted 12 lines
Quoted 10 lines
drivers/dax/bus.c +1 -1
@@ -76,7 +76,7 @@ static ssize_t do_id_store(struct device_driver *drv, const char *buf,
  	fields = sscanf(buf, "dax%d.%d", &region_id, &id);
  	if (fields != 2)
  		return -EINVAL;
-	sprintf(devname, "dax%d.%d", region_id, id);
+	scnprintf(devname, DAX_NAME_LEN, "dax%d.%d", region_id, id);
  	if (!sysfs_streq(buf, devname))
  		return -EINVAL;

since region_id and id are unsigned long may be devname should be char[21].

I'm finding and changing these manually.

Thanx

#3 lore raw
Quoted 23 lines · Salah Triki
Quoted 9 lines · Joe Perches
Quoted 1 line · Salah Triki

Replace sprintf() by scnprintf() in order to avoid buffer overflows.

OK but also not strictly necessary. DAX_NAME_LEN is 30.

Are you finding and changing these manually or with a script?

drivers/dax/bus.c

Quoted 1 line
drivers/dax/bus.c +0 -0

[]

Quoted 12 lines
Quoted 10 lines
drivers/dax/bus.c +1 -1
@@ -76,7 +76,7 @@ static ssize_t do_id_store(struct device_driver *drv, const char *buf,
  	fields = sscanf(buf, "dax%d.%d", &region_id, &id);
  	if (fields != 2)
  		return -EINVAL;
-	sprintf(devname, "dax%d.%d", region_id, id);
+	scnprintf(devname, DAX_NAME_LEN, "dax%d.%d", region_id, id);
  	if (!sysfs_streq(buf, devname))
  		return -EINVAL;

Quoted 2 lines

since region_id and id are unsigned long may be devname should be char[21].

I think you need to look at the code a bit more carefully.

	unsigned int region_id, id;

Also the output is %d, so the maximum length of each output int is 10 with a possible leading minus sign.

3 + 10 + 1 + 10 + 1. So 25 not 21 which is too small.

The %d uses could be changed to %u to make it 23. But really it hardly matters as 30 is sufficent and the function call depth here isn't particularly high.

Quoted 1 line

I'm finding and changing these manually.

coccinelle could help.
https://coccinelle.gitlabpages.inria.fr/website/

#4 lore raw
Quoted 36 lines · Joe Perches
Quoted 23 lines · Salah Triki
Quoted 9 lines · Joe Perches
Quoted 1 line · Salah Triki

Replace sprintf() by scnprintf() in order to avoid buffer overflows.

OK but also not strictly necessary. DAX_NAME_LEN is 30.

Are you finding and changing these manually or with a script?

drivers/dax/bus.c

Quoted 1 line
drivers/dax/bus.c +0 -0

[]

Quoted 12 lines
Quoted 10 lines
drivers/dax/bus.c +1 -1
@@ -76,7 +76,7 @@ static ssize_t do_id_store(struct device_driver *drv, const char *buf,
  	fields = sscanf(buf, "dax%d.%d", &region_id, &id);
  	if (fields != 2)
  		return -EINVAL;
-	sprintf(devname, "dax%d.%d", region_id, id);
+	scnprintf(devname, DAX_NAME_LEN, "dax%d.%d", region_id, id);
  	if (!sysfs_streq(buf, devname))
  		return -EINVAL;

Quoted 2 lines

since region_id and id are unsigned long may be devname should be char[21].

I think you need to look at the code a bit more carefully.

	unsigned int region_id, id;

Also the output is %d, so the maximum length of each output int is 10 with a possible leading minus sign.

3 + 10 + 1 + 10 + 1. So 25 not 21 which is too small.

Quoted 10 lines

The %d uses could be changed to %u to make it 23. But really it hardly matters as 30 is sufficent and the function call depth here isn't particularly high.

Quoted 1 line

I'm finding and changing these manually.

coccinelle could help.
https://coccinelle.gitlabpages.inria.fr/website/

Thanx