Skip to contentSkip to conversation

pci: fix overflow in printf string formatting

PATCH In review Claudio Fontana 3 participants 6 messages May 31, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pci: fix overflow in printf string formatting

---
 hw/pci/pci.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
hw/pci/pci.c +9 -9
index a9b37f8000..6e7015329c 100644
@@ -2640,15 +2640,15 @@ static char *pci_dev_fw_name(DeviceState *dev, char *buf, int len)
 static char *pcibus_get_fw_dev_path(DeviceState *dev)
 {
     PCIDevice *d = (PCIDevice *)dev;
-    char path[50], name[33];
-    int off;
-
-    off = snprintf(path, sizeof(path), "%s@%x",
-                   pci_dev_fw_name(dev, name, sizeof name),
-                   PCI_SLOT(d->devfn));
-    if (PCI_FUNC(d->devfn))
-        snprintf(path + off, sizeof(path) + off, ",%x", PCI_FUNC(d->devfn));
-    return g_strdup(path);
+    char name[33];
+    int has_func = !!PCI_FUNC(d->devfn);
+
+    return g_strdup_printf("%s@%x%s%.*x",
+                           pci_dev_fw_name(dev, name, sizeof(name)),
+                           PCI_SLOT(d->devfn),
+                           has_func ? "," : "",
+                           has_func,
+                           PCI_FUNC(d->devfn));
 }
 
 static char *pcibus_get_dev_path(DeviceState *dev)
Signature · 1 line
2.26.2
#1 lore raw

PATCH 1/1 pci: fix overflow in printf string formatting

---
 hw/pci/pci.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
hw/pci/pci.c +9 -9
index a9b37f8000..6e7015329c 100644
@@ -2640,15 +2640,15 @@ static char *pci_dev_fw_name(DeviceState *dev, char *buf, int len)
 static char *pcibus_get_fw_dev_path(DeviceState *dev)
 {
     PCIDevice *d = (PCIDevice *)dev;
-    char path[50], name[33];
-    int off;
-
-    off = snprintf(path, sizeof(path), "%s@%x",
-                   pci_dev_fw_name(dev, name, sizeof name),
-                   PCI_SLOT(d->devfn));
-    if (PCI_FUNC(d->devfn))
-        snprintf(path + off, sizeof(path) + off, ",%x", PCI_FUNC(d->devfn));
-    return g_strdup(path);
+    char name[33];
+    int has_func = !!PCI_FUNC(d->devfn);
+
+    return g_strdup_printf("%s@%x%s%.*x",
+                           pci_dev_fw_name(dev, name, sizeof(name)),
+                           PCI_SLOT(d->devfn),
+                           has_func ? "," : "",
+                           has_func,
+                           PCI_FUNC(d->devfn));
 }
 
 static char *pcibus_get_dev_path(DeviceState *dev)
Signature · 1 line
2.26.2
#3 lore raw
Quoted 10 lines · Peter Maydell
Quoted 2 lines · Claudio Fontana

It would be helpful to note in the commit message how bad the overflow is, in what situations it can happen, and how it was detected.

thanks
-- PMM

Hi Peter,

sorry I should have linked to this previous message by Dario:

https://lists.gnu.org/archive/html/qemu-devel/2022-05/msg05518.html

It was detected when building QEMU with FORTIFY_SOURCE=3.

Thanks,

Claudio

#4 lore raw
Quoted 16 lines · Claudio Fontana
Quoted 10 lines · Peter Maydell
Quoted 2 lines · Claudio Fontana

It would be helpful to note in the commit message how bad the overflow is, in what situations it can happen, and how it was detected.

thanks
-- PMM

Hi Peter,

sorry I should have linked to this previous message by Dario:

Quoted 9 lines

https://lists.gnu.org/archive/html/qemu-devel/2022-05/msg05518.html

It was detected when building QEMU with FORTIFY_SOURCE=3.

Thanks,

Claudio

Will resend with more explanation in the commit message btw.

#5 lore raw
In reply to Claudio Fontana
Quoted 24 lines · Claudio Fontana
---
  hw/pci/pci.c | 18 +++++++++---------
  1 file changed, 9 insertions(+), 9 deletions(-)
hw/pci/pci.c +2 -9
index a9b37f8000..6e7015329c 100644
@@ -2640,15 +2640,15 @@ static char *pci_dev_fw_name(DeviceState *dev, char *buf, int len)
  static char *pcibus_get_fw_dev_path(DeviceState *dev)
  {
      PCIDevice *d = (PCIDevice *)dev;
-    char path[50], name[33];
-    int off;
-
-    off = snprintf(path, sizeof(path), "%s@%x",
-                   pci_dev_fw_name(dev, name, sizeof name),
-                   PCI_SLOT(d->devfn));
-    if (PCI_FUNC(d->devfn))
-        snprintf(path + off, sizeof(path) + off, ",%x", PCI_FUNC(d->devfn));
-    return g_strdup(path);
+    char name[33];
+    int has_func = !!PCI_FUNC(d->devfn);

hw/pci/pci.c static char *pci_dev_fw_name(DeviceState *dev, char *buf, int len) · line 2654

Quoted 10 lines
hw/pci/pci.c +7 -0
+
+    return g_strdup_printf("%s@%x%s%.*x",
+                           pci_dev_fw_name(dev, name, sizeof(name)),
+                           PCI_SLOT(d->devfn),
+                           has_func ? "," : "",
+                           has_func,
+                           PCI_FUNC(d->devfn));
  }
 
  static char *pcibus_get_dev_path(DeviceState *dev)