Received: by 2002:a17:90a:728b:0:0:0:0 with SMTP id e11csp8886321pjg; Fri, 8 May 2020 05:12:13 -0700 (PDT) X-Google-Smtp-Source: APiQypJnIT/z7PE27Kw5v/+7tJfjfwLQJKImN/HeZEj26FBUe9CvRsAYGK2axISt9CiUy5nEU9pU X-Received: by 2002:a05:6402:6c4:: with SMTP id n4mr1916609edy.368.1588939933411; Fri, 08 May 2020 05:12:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588939933; cv=none; d=google.com; s=arc-20160816; b=t9TRZmo9bZMUDkZG7eZBRKVrfD54OQwTtLXfKJkNHtwgedCwnQT4gp8JYXtLjWjsJB VWIN/dxvsyzEToJ4tu0LmhcuDnAqleuCTwsthz1gqFSeFLSFSw4BlaCU8DPdNnZ0R8f6 d0ADV0+ask+xSGJfeU1EmviWrR8QG8Ha999OYhB/gfzjxT3ObMbcr8ufXXA2eYiHy3wK SHIF79uZobAgqUJcIf/7lKomlekzgSOJIbXEbrQNcktpD84j55Y/fDEs3XgFGX3gvI9k OwNSTlQyJY/w44zuNujgP/ApDvNRCN8hrF6QDQomDwgzKo9F2DiQ1rWMPBl2hgz5oN97 pVFw== 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:cc:to; bh=j+r0Yie+0N6RhjhSXvGDW7aPUyPuDKRdJgCmDOSuel4=; b=i3m2xmkeEHGxiWH8de11QYQ5ut1eq7vah1OZT8fUqQNJNkIloOt3rSRWeO0pAirilB H9dPAxg1PMA410UGPIgaqQzReBImQy44dVHV7CzevvzXE1zan92YzRnj1V5SzbOmlsPF 6Tsq3wZ3GMY42my0RULFTJodlqHD0Gsfzrn2ZFcv6EOUaxkUjPf+KGwK/Q1ON1IXTf5Q 7Nh1DrmkxssFNlNbwQ7QKrB4iIm/mhNRQ8/QGLHQQAB75k3l1z1wrcRRUOVVHN5yj3Ix zZKdR0ToCjksq1iP7vcnRUdDH6ENgVtnALEIgtuGULKDq10yPt/EXbRfr5q7jfYxnLlZ DnOA== 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 ck13si136756ejb.403.2020.05.08.05.11.49; Fri, 08 May 2020 05:12:13 -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 S1726843AbgEHMKJ (ORCPT + 99 others); Fri, 8 May 2020 08:10:09 -0400 Received: from szxga07-in.huawei.com ([45.249.212.35]:39142 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726627AbgEHMKJ (ORCPT ); Fri, 8 May 2020 08:10:09 -0400 Received: from DGGEMS414-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 75BC7E1BE2FFC8B5EA40; Fri, 8 May 2020 20:10:07 +0800 (CST) Received: from [127.0.0.1] (10.166.215.237) by DGGEMS414-HUB.china.huawei.com (10.3.19.214) with Microsoft SMTP Server id 14.3.487.0; Fri, 8 May 2020 20:09:58 +0800 To: , , , , , CC: Shiyuan Hu , Hewenliang From: Yunfeng Ye Subject: [PATCH v4] tools/bootconfig: fix resource leak in apply_xbc() Message-ID: Date: Fri, 8 May 2020 20:09:46 +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 An error is found by a internel analysis tool: "Memory leak: data" and "Resource leak: fd" in tools/bootconfig/main.c 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) --- v3 -> v4: - update the commit message v2 -> v3: - set 'ret' to 0 before returning on success v1 -> v2: - complete the error handling at other error path - add "Fixes" tag tools/bootconfig/main.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/tools/bootconfig/main.c b/tools/bootconfig/main.c index 16b9a420e6fd..17a9837dcfaa 100644 --- a/tools/bootconfig/main.c +++ b/tools/bootconfig/main.c @@ -314,31 +314,35 @@ 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; - } + + ret = 0; + +close_fd: close(fd); +free_data: free(data); - return 0; + return ret; } int usage(void) -- 1.8.3.1