From patchwork Thu Sep 18 03:33:42 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhanghailiang X-Patchwork-Id: 390641 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 DE355140216 for ; Thu, 18 Sep 2014 13:35:21 +1000 (EST) Received: from localhost ([::1]:48321 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XUSV4-0002la-41 for incoming@patchwork.ozlabs.org; Wed, 17 Sep 2014 23:35:18 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49562) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XUSUk-0002Q2-MX for qemu-devel@nongnu.org; Wed, 17 Sep 2014 23:35:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XUSUf-0001MN-Ri for qemu-devel@nongnu.org; Wed, 17 Sep 2014 23:34:58 -0400 Received: from szxga01-in.huawei.com ([119.145.14.64]:44325) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XUSUf-0001Li-5S; Wed, 17 Sep 2014 23:34:53 -0400 Received: from 172.24.2.119 (EHLO szxeml423-hub.china.huawei.com) ([172.24.2.119]) by szxrg01-dlp.huawei.com (MOS 4.3.7-GA FastPath queued) with ESMTP id CBV14387; Thu, 18 Sep 2014 11:34:40 +0800 (CST) Received: from localhost (10.177.22.69) by szxeml423-hub.china.huawei.com (10.82.67.162) with Microsoft SMTP Server id 14.3.158.1; Thu, 18 Sep 2014 11:34:30 +0800 From: zhanghailiang To: Date: Thu, 18 Sep 2014 11:33:42 +0800 Message-ID: <1411011222-5116-1-git-send-email-zhang.zhanghailiang@huawei.com> X-Mailer: git-send-email 1.9.2.msysgit.0 MIME-Version: 1.0 X-Originating-IP: [10.177.22.69] X-CFilter-Loop: Reflected X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 119.145.14.64 Cc: mdroth@linux.vnet.ibm.com, armbru@redhat.com, luonengjun@huawei.com, peter.huangpeng@huawei.com, qemu-stable@nongnu.org, lcapitulino@redhat.com, zhanghailiang Subject: [Qemu-devel] [PATCH] qga: Fix possible freed memory accessing 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 If readdir_r fails, error_setg_errno will reference the freed pointer *dirpath*. Signed-off-by: zhanghailiang Reviewed-by: Markus Armbruster --- qga/commands-posix.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 7eed7f4..3082eae 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -965,7 +965,6 @@ static void build_guest_fsinfo_for_virtual_device(char const *syspath, g_free(dirpath); return; } - g_free(dirpath); for (;;) { if (readdir_r(dir, &entry, &result) != 0) { @@ -977,10 +976,12 @@ static void build_guest_fsinfo_for_virtual_device(char const *syspath, } if (entry.d_type == DT_LNK) { + char *path; + g_debug(" slave device '%s'", entry.d_name); - dirpath = g_strdup_printf("%s/slaves/%s", syspath, entry.d_name); - build_guest_fsinfo_for_device(dirpath, fs, errp); - g_free(dirpath); + path = g_strdup_printf("%s/slaves/%s", syspath, entry.d_name); + build_guest_fsinfo_for_device(path, fs, errp); + g_free(path); if (*errp) { break; @@ -988,6 +989,7 @@ static void build_guest_fsinfo_for_virtual_device(char const *syspath, } } + g_free(dirpath); closedir(dir); }