Received: from eggs.gnu.org ([2001:4830:134:3::10]:47287) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WGtjJ-0000DL-KK for qemu-devel@nongnu.org; Fri, 21 Feb 2014 12:17:47 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WGtjD-0007DQ-Ks for qemu-devel@nongnu.org; Fri, 21 Feb 2014 12:17:41 -0500 Received: from mx1.redhat.com ([209.132.183.28]:19418) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WGtjD-0007CY-Dq for qemu-devel@nongnu.org; Fri, 21 Feb 2014 12:17:35 -0500 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s1LHHXax032093 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Fri, 21 Feb 2014 12:17:33 -0500 Message-ID: <1393003052.9111.81.camel@ul30vt.home> From: Alex Williamson Date: Fri, 21 Feb 2014 10:17:32 -0700 In-Reply-To: <1393000925-8446-2-git-send-email-armbru@redhat.com> References: <1393000925-8446-1-git-send-email-armbru@redhat.com> <1393000925-8446-2-git-send-email-armbru@redhat.com> Content-Type: text/plain; charset="UTF-8" Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [PATCH 1/2] vfio: Fix overrun after readlink() fills buffer completely List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Markus Armbruster Cc: qemu-devel@nongnu.org On Fri, 2014-02-21 at 17:42 +0100, Markus Armbruster wrote: > 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 reserving space for the null byte when calling readlink(), like > we do elsewhere. > > Spotted by Coverity. > > Signed-off-by: Markus Armbruster > --- > hw/misc/vfio.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/hw/misc/vfio.c b/hw/misc/vfio.c > index 8db182f..8e56785 100644 > --- a/hw/misc/vfio.c > +++ b/hw/misc/vfio.c > @@ -3681,7 +3681,7 @@ static int vfio_initfn(PCIDevice *pdev) > > strncat(path, "iommu_group", sizeof(path) - strlen(path) - 1); > > - len = readlink(path, iommu_group_path, PATH_MAX); > + len = readlink(path, iommu_group_path, PATH_MAX - 1); > if (len <= 0) { > error_report("vfio: error no iommu_group for device"); > return -errno; I'm not sure why we wouldn't follow the same logic as pci-assign here. If we fill to the length provided we have no idea if we have the full path. We certainly expect it to fit well within PATH_MAX, so let's handle it as an error if we reach PATH_MAX. Thanks, Alex