From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.2 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2A3C8C38A2A for ; Fri, 8 May 2020 06:51:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0DD7E208D6 for ; Fri, 8 May 2020 06:51:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726770AbgEHGvZ (ORCPT ); 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