9ae3a8
From a254fe579062a9eaf97f2b81474c19b4b46ddc85 Mon Sep 17 00:00:00 2001
9ae3a8
From: Alex Williamson <alex.williamson@redhat.com>
9ae3a8
Date: Fri, 10 Apr 2015 16:33:56 +0200
9ae3a8
Subject: [PATCH 04/14] vfio: Fix overrun after readlink() fills buffer
9ae3a8
 completely
9ae3a8
9ae3a8
Message-id: <20150410163356.15324.47617.stgit@gimli.home>
9ae3a8
Patchwork-id: 64786
9ae3a8
O-Subject: [RHEL7.2 qemu-kvm PATCH 4/8] vfio: Fix overrun after readlink() fills buffer completely
9ae3a8
Bugzilla: 1210504
9ae3a8
RH-Acked-by: Thomas Huth <thuth@redhat.com>
9ae3a8
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
9ae3a8
RH-Acked-by: Bandan Das <bsd@redhat.com>
9ae3a8
9ae3a8
From: Markus Armbruster <armbru@redhat.com>
9ae3a8
9ae3a8
Upstream: 13665a2d2f675341e73618fcd7f9d36b6c68b509
9ae3a8
9ae3a8
readlink() returns the number of bytes written to the buffer, and it
9ae3a8
doesn't write a terminating null byte.  vfio_init() writes it itself.
9ae3a8
Overruns the buffer when readlink() filled it completely.
9ae3a8
9ae3a8
Fix by treating readlink() filling the buffer completely as error,
9ae3a8
like we do in pci-assign.c's assign_failed_examine().
9ae3a8
9ae3a8
Spotted by Coverity.
9ae3a8
9ae3a8
Signed-off-by: Markus Armbruster <armbru@redhat.com>
9ae3a8
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 hw/misc/vfio.c | 6 +++---
9ae3a8
 1 file changed, 3 insertions(+), 3 deletions(-)
9ae3a8
9ae3a8
diff --git a/hw/misc/vfio.c b/hw/misc/vfio.c
9ae3a8
index 57bc974..81e6594 100644
9ae3a8
--- a/hw/misc/vfio.c
9ae3a8
+++ b/hw/misc/vfio.c
9ae3a8
@@ -3767,10 +3767,10 @@ static int vfio_initfn(PCIDevice *pdev)
9ae3a8
 
9ae3a8
     strncat(path, "iommu_group", sizeof(path) - strlen(path) - 1);
9ae3a8
 
9ae3a8
-    len = readlink(path, iommu_group_path, PATH_MAX);
9ae3a8
-    if (len <= 0) {
9ae3a8
+    len = readlink(path, iommu_group_path, sizeof(path));
9ae3a8
+    if (len <= 0 || len >= sizeof(path)) {
9ae3a8
         error_report("vfio: error no iommu_group for device");
9ae3a8
-        return -errno;
9ae3a8
+        return len < 0 ? -errno : ENAMETOOLONG;
9ae3a8
     }
9ae3a8
 
9ae3a8
     iommu_group_path[len] = 0;
9ae3a8
-- 
9ae3a8
1.8.3.1
9ae3a8