From patchwork Wed Feb 26 18:25:55 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Williamson X-Patchwork-Id: 324561 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id DD8E02C0096 for ; Thu, 27 Feb 2014 05:26:36 +1100 (EST) Received: from localhost ([::1]:42401 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WIjBi-0003qP-J1 for incoming@patchwork.ozlabs.org; Wed, 26 Feb 2014 13:26:34 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41025) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WIjBC-0003gY-Tq for qemu-devel@nongnu.org; Wed, 26 Feb 2014 13:26:08 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WIjB6-0007ax-Tk for qemu-devel@nongnu.org; Wed, 26 Feb 2014 13:26:02 -0500 Received: from mx1.redhat.com ([209.132.183.28]:47261) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WIjB6-0007ap-H1 for qemu-devel@nongnu.org; Wed, 26 Feb 2014 13:25:56 -0500 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s1QIPtNb012845 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 26 Feb 2014 13:25:56 -0500 Received: from bling.home (ovpn-113-32.phx2.redhat.com [10.3.113.32]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s1QIPtKu015900; Wed, 26 Feb 2014 13:25:55 -0500 From: Alex Williamson To: anthony@redhat.com Date: Wed, 26 Feb 2014 11:25:55 -0700 Message-ID: <20140226182555.8018.44958.stgit@bling.home> In-Reply-To: <20140226182246.8018.81349.stgit@bling.home> References: <20140226182246.8018.81349.stgit@bling.home> User-Agent: StGit/0.17-dirty MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Markus Armbruster , qemu-devel@nongnu.org Subject: [Qemu-devel] [PULL 1/4] vfio: Fix overrun after readlink() fills buffer completely X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: 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. Signed-off-by: Markus Armbruster Signed-off-by: Alex Williamson --- hw/misc/vfio.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/hw/misc/vfio.c b/hw/misc/vfio.c index 8db182f..e669bbe 100644 --- a/hw/misc/vfio.c +++ b/hw/misc/vfio.c @@ -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;