Skip to contentSkip to conversation

nvmet: fixup buffer overrun in nvmet_subsys_attr_serial()

PATCH Superseded Hannes Reinecke 2 participants 4 messages Aug 27, 2021 – Sep 6, 2021

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvmet: fixup buffer overrun in nvmet_subsys_attr_serial()

The serial number is copied into the buffer via memcpy_and_pad() with the length NVMET_SN_MAX_SIZE. So when printing out we also need to take just that length as anything beyond that will be uninitialized.

---
 drivers/nvme/target/configfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/nvme/target/configfs.c +1 -1
index 008be02fbac9..255db21b73ee 100644
@@ -1113,7 +1113,7 @@ static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item,
 {
 	struct nvmet_subsys *subsys = to_subsys(item);
 
-	return snprintf(page, PAGE_SIZE, "%s\n", subsys->serial);
+	return snprintf(page, NVMET_SN_MAX_SIZE, "%s\n", subsys->serial);
 }
 
 static ssize_t
Signature · 7 lines
2.29.2


_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
#1 lore raw
Quoted 6 lines · Hannes Reinecke

The serial number is copied into the buffer via memcpy_and_pad() with the length NVMET_SN_MAX_SIZE. So when printing out we also need to take just that length as anything beyond that will be uninitialized.

The normal way to print a potentially unterminated fixed length string would be something like:

drivers/nvme/target/configfs.c +2 -1
index f74485c705ff2..d784f3c200b4c 100644
@@ -1067,7 +1067,8 @@ static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item,
 {
 	struct nvmet_subsys *subsys = to_subsys(item);
 
-	return snprintf(page, PAGE_SIZE, "%s\n", subsys->serial);
+	return snprintf(page, PAGE_SIZE, "%*s\n",
+			NVMET_SN_MAX_SIZE, subsys->serial);
 }
 
 static ssize_t

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#2 lore raw
Quoted 10 lines · Christoph Hellwig
Quoted 6 lines · Hannes Reinecke

The serial number is copied into the buffer via memcpy_and_pad() with the length NVMET_SN_MAX_SIZE. So when printing out we also need to take just that length as anything beyond that will be uninitialized.

The normal way to print a potentially unterminated fixed length string would be something like:

Does this version still look ok to you?

Quoted 6 lines
drivers/nvme/target/configfs.c +0 -0
index f74485c705ff2..d784f3c200b4c 100644

drivers/nvme/target/configfs.c static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item, · line 1074

Quoted 9 lines
drivers/nvme/target/configfs.c +2 -1
@@ -1067,7 +1067,8 @@ static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item,
 {
 	struct nvmet_subsys *subsys = to_subsys(item);
 
-	return snprintf(page, PAGE_SIZE, "%s\n", subsys->serial);
+	return snprintf(page, PAGE_SIZE, "%*s\n",
+			NVMET_SN_MAX_SIZE, subsys->serial);
 }
 
 static ssize_t

---end quoted text---

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#3 lore raw
Quoted 23 lines · Christoph Hellwig
Quoted 10 lines · Christoph Hellwig
Quoted 6 lines · Hannes Reinecke

The serial number is copied into the buffer via memcpy_and_pad() with the length NVMET_SN_MAX_SIZE. So when printing out we also need to take just that length as anything beyond that will be uninitialized.

The normal way to print a potentially unterminated fixed length string would be something like:

Does this version still look ok to you?

Quoted 9 lines
drivers/nvme/target/configfs.c +0 -0
index f74485c705ff2..d784f3c200b4c 100644
@@ -1067,7 +1067,8 @@ static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item,
  {
  	struct nvmet_subsys *subsys = to_subsys(item);
 
Quoted 8 lines

drivers/nvme/target/configfs.c static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item, · line 1074

Quoted 6 lines
drivers/nvme/target/configfs.c +2 -1
-	return snprintf(page, PAGE_SIZE, "%s\n", subsys->serial);
+	return snprintf(page, PAGE_SIZE, "%*s\n",
+			NVMET_SN_MAX_SIZE, subsys->serial);
  }
 
  static ssize_t

---end quoted text---

Yes, it does; will be sending an updated patch.

Cheers,

Hannes

Signature · 9 lines
Dr. Hannes Reinecke                Kernel Storage Architect
[email protected]                              +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme