Skip to contentSkip to conversation

Two buffer overruns in device assignment

PATCH In review Markus Armbruster 3 participants 6 messages Feb 24, 2014

lore
Share & tools
lore
#1 lore raw

PATCH v2 1/2 vfio: Fix overrun after readlink() fills buffer completely

In reply to Markus Armbruster

readlink() returns the number of bytes written to the buffer, and it doesn't write a terminating null byte. vfio_init() writes it itself. Overruns the buffer when readlink() filled it completely.

Fix by treating readlink() filling the buffer completely as error, like we do in pci-assign.c's assign_failed_examine().

Spotted by Coverity.

---
 hw/misc/vfio.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/misc/vfio.c +3 -3
index 8db182f..e669bbe 100644
@@ -3681,10 +3681,10 @@ static int vfio_initfn(PCIDevice *pdev)
 
     strncat(path, "iommu_group", sizeof(path) - strlen(path) - 1);
 
-    len = readlink(path, iommu_group_path, PATH_MAX);
-    if (len <= 0) {
+    len = readlink(path, iommu_group_path, sizeof(path));
+    if (len <= 0 || len >= sizeof(path)) {
         error_report("vfio: error no iommu_group for device");
-        return -errno;
+        return len < 0 ? -errno : ENAMETOOLONG;
     }
 
     iommu_group_path[len] = 0;
Signature · 1 line
1.8.1.4
#2 lore raw

PATCH v2 2/2 pci-assign: Fix potential read beyond buffer on -EBUSY

In reply to Markus Armbruster

readlink() doesn't write a terminating null byte. assign_failed_examine() passes the unterminated string to strrchr(). Oops. Terminate it.

Spotted by Coverity.

---
 hw/i386/kvm/pci-assign.c | 1 +
 1 file changed, 1 insertion(+)
hw/i386/kvm/pci-assign.c +1 -0
index 9686801..a825871 100644
@@ -743,6 +743,7 @@ static void assign_failed_examine(AssignedDevice *dev)
         goto fail;
     }
 
+    driver[r] = 0;
     ns = strrchr(driver, '/');
     if (!ns) {
         goto fail;
Signature · 1 line
1.8.1.4
#0 lore raw

v2 cover Two buffer overruns in device assignment

v2:
* Treat readlink() filling buffer as error (Alex)
* Use sizeof() rather than PATH_MAX (Peter)
* PATCH 2/2 unchanged

Markus Armbruster (2):
vfio: Fix overrun after readlink() fills buffer completely
pci-assign: Fix potential read beyond buffer on -EBUSY

 hw/i386/kvm/pci-assign.c | 1 +
 hw/misc/vfio.c           | 6 +++---
 2 files changed, 4 insertions(+), 3 deletions(-)
Signature · 1 line
1.8.1.4
Acked-by on the v2 series
#3 lore raw
Quoted 5 lines · Markus Armbruster

v2:
* Treat readlink() filling buffer as error (Alex)
* Use sizeof() rather than PATH_MAX (Peter)
* PATCH 2/2 unchanged

Quoted 7 lines

Markus Armbruster (2):
vfio: Fix overrun after readlink() fills buffer completely
pci-assign: Fix potential read beyond buffer on -EBUSY

 hw/i386/kvm/pci-assign.c | 1 +
 hw/misc/vfio.c           | 6 +++---
 2 files changed, 4 insertions(+), 3 deletions(-)

For both:

Peter, if you want to grab these directly, feel free, otherwise I can shepherd them in through vfio. Thanks,

Alex

#4 lore raw
Quoted 14 lines · Alex Williamson
Quoted 5 lines · Markus Armbruster

v2:
* Treat readlink() filling buffer as error (Alex)
* Use sizeof() rather than PATH_MAX (Peter)
* PATCH 2/2 unchanged

Quoted 7 lines

Markus Armbruster (2):
vfio: Fix overrun after readlink() fills buffer completely
pci-assign: Fix potential read beyond buffer on -EBUSY

 hw/i386/kvm/pci-assign.c | 1 +
 hw/misc/vfio.c           | 6 +++---
 2 files changed, 4 insertions(+), 3 deletions(-)
Quoted 6 lines

For both:

Peter, if you want to grab these directly, feel free, otherwise I can shepherd them in through vfio. Thanks,

Via vfio would be easiest for me.

thanks
-- PMM

#5 lore raw
Quoted 15 lines · Peter Maydell
Quoted 14 lines · Alex Williamson
Quoted 12 lines · Markus Armbruster

v2:
* Treat readlink() filling buffer as error (Alex)
* Use sizeof() rather than PATH_MAX (Peter)
* PATCH 2/2 unchanged

Markus Armbruster (2):
vfio: Fix overrun after readlink() fills buffer completely
pci-assign: Fix potential read beyond buffer on -EBUSY

 hw/i386/kvm/pci-assign.c | 1 +
 hw/misc/vfio.c           | 6 +++---
 2 files changed, 4 insertions(+), 3 deletions(-)
Quoted 8 lines
Quoted 6 lines

For both:

Peter, if you want to grab these directly, feel free, otherwise I can shepherd them in through vfio. Thanks,

Via vfio would be easiest for me.

Ok