Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp1396185ybj; Thu, 7 May 2020 23:53:06 -0700 (PDT) X-Google-Smtp-Source: APiQypKEziDzM9APRak1Dwp3Xer/SFqjf8Y1BXIrvS1QBC0oVJqM0UtQED5zuHE5+Ttsqx1+a+uK X-Received: by 2002:a17:906:c7c5:: with SMTP id dc5mr687563ejb.50.1588920786128; Thu, 07 May 2020 23:53:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588920786; cv=none; d=google.com; s=arc-20160816; b=YK//7RDIwyuZF3oOpAJfsq+gOnUpyQDqAJS5x0YAWRQILCYVpXp+KiMe8myGlgcrcK sFWxxEKh2QmhzepN1tmxI+rVFRsxfwOBHvuAKUt+Rfm9lUjCNoFPb8G/IneWxg1D7ZoA ArCE68xMSjP0KqaWpM1ZpzC98MfGh6kzv9gaU9MU+B/bKC+NnKhOOqOVZq93ijZV8f1V Tk21ld/c5i3LwSdCeJxRpzQrVLsHVET7sawcgCyq0x5ESYg64aSyTrUKYGXIzZIxhgPT 6tOlc8RLEM41w03XuRtMYmfCFCQETIYBEFoSzcy3Hn/KzIurEqmKIhfjgoSZSVY+0v+R HxAA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:mime-version:user-agent:date:message-id:subject :from:to; bh=ZNh+z6Aj4mRinSKeL5ACL6Y+P1uU+qfmsYpkDZrYd7U=; b=Uh1+DGd9UKKbC5P5Xt87RImt7rQO71p0ww2R0W3wxuqqX4P4fX9JnZrM0CujaCuOEv MVGSawD2cwQ/fsMdNgk6BijkZiK3ul9RwyLkqJKABailvj907ouQcUiGd2BfhgmfGvZG gyDb5P4aqmMRvtSrmawh3usn37nj38GE9sbNKACkn7PjxstE57yPIakIGifDTjsVe8Vj C735D7ag1FJz9E1IKydFf9UHl2e8pRhbOsIZNMRxw/qcVfS9pCi/yXnffPfNjsUbLUNK LfdjgddMSh0VnaDNt/Q/tcHgGrZQt4N+51uRejLtYpn1UAuS5cs6vEI97E/hWuK9BHbh SREw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y10si430022ejf.464.2020.05.07.23.52.42; Thu, 07 May 2020 23:53:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726365AbgEHGvZ (ORCPT + 99 others); Fri, 8 May 2020 02:51:25 -0400 Received: from szxga07-in.huawei.com ([45.249.212.35]:48270 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725971AbgEHGvY (ORCPT ); Fri, 8 May 2020 02:51:24 -0400 Received: from DGGEMS408-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 5A4FDF0DB85854F959CA; Fri, 8 May 2020 14:51:22 +0800 (CST) Received: from [127.0.0.1] (10.166.215.237) by DGGEMS408-HUB.china.huawei.com (10.3.19.208) with Microsoft SMTP Server id 14.3.487.0; Fri, 8 May 2020 14:51:16 +0800 To: , , , , , , Shiyuan Hu , Hewenliang From: Yunfeng Ye Subject: [PATCH v2] tools/bootconfig: fix resource leak in apply_xbc() Message-ID: <189d719f-a8b8-6e10-ae2f-8120c3d2b7a9@huawei.com> Date: Fri, 8 May 2020 14:51:15 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.166.215.237] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Fix the @data and @fd allocations that are leaked in the error path of apply_xbc(). Fixes: 85c46b78da58 ("bootconfig: Add bootconfig magic word for indicating bootconfig explicitly") Fixes: 950313ebf79c ("tools: bootconfig: Add bootconfig command") Acked-by: Masami Hiramatsu Signed-off-by: Yunfeng Ye Signed-off-by: Steven Rostedt (VMware) --- tools/bootconfig/main.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/tools/bootconfig/main.c b/tools/bootconfig/main.c index 16b9a420e6fd..d034f86022b7 100644 --- a/tools/bootconfig/main.c +++ b/tools/bootconfig/main.c @@ -314,31 +314,33 @@ int apply_xbc(const char *path, const char *xbc_path) ret = delete_xbc(path); if (ret < 0) { pr_err("Failed to delete previous boot config: %d\n", ret); - return ret; + goto free_data; } /* Apply new one */ fd = open(path, O_RDWR | O_APPEND); if (fd < 0) { pr_err("Failed to open %s: %d\n", path, fd); - return fd; + ret = fd; + goto free_data; } /* TODO: Ensure the @path is initramfs/initrd image */ ret = write(fd, data, size + 8); if (ret < 0) { pr_err("Failed to apply a boot config: %d\n", ret); - return ret; + goto close_fd; } /* Write a magic word of the bootconfig */ ret = write(fd, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN); - if (ret < 0) { + if (ret < 0) pr_err("Failed to apply a boot config magic: %d\n", ret); - return ret; - } + +close_fd: close(fd); +free_data: free(data); - return 0; + return ret; } int usage(void) -- 1.8.3.1