Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp1544693ybj; Fri, 8 May 2020 03:58:16 -0700 (PDT) X-Google-Smtp-Source: APiQypJnFd/eVki5l4XvRDvbSrXYlCrexZ29haodvjKG7hJUYDj755lwBoiglDIDhyo9TPWs0JkO X-Received: by 2002:a17:907:4063:: with SMTP id nl3mr1387594ejb.237.1588935495937; Fri, 08 May 2020 03:58:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588935495; cv=none; d=google.com; s=arc-20160816; b=EHHXeSFDGiks2nI2k4IDPD6wi1scMqOsrLY6vpgqFAWgMa+4EhzVA8aMCiau3AX6M9 KS1iuU4xH/uz1FyhB6rbQwSeusU9XwS6L6uzcSTLsQ4j/akzIJb3xeSIDD/YcBLKmcJb xyzGwVLfdJwstj6jX1cW3S8QBfExAt/jrX1cTChtVEIQCWjPRnd22L/KXi+oadDws2Tn z4a4NUpfT5PEpyt9CPomP/gj5e8vB5net6jB4Q4kvASNoNlUzBe3vUPBM0aEvH5TMQJf mnQg1lzLRWBFJEWNbulhenpMF8bayYDxoie/FndyRmanO/KnZHBcH03Rkr6Kyg51B7TR GJgQ== 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:to:subject :from; bh=REt5iItPoMk/fY4ELMYllgM9ba1rMMKD8PATuS+JedM=; b=j1iU9/D365Rdx0XdE4UuL2h3eyHjL389m9b9VTQla/tEhvmlj4rzCaLWMYLyBMEkN3 jd7n9udQ2tgltqXWOg58UjyzJIVNUDgT6RaIy2t1zvxERf1WCQS01p2/R0Wiw3qvabu+ q+4H6NHubaJ7e9lW9SIYs5pJbwVjjyZDXxdfcakpzG9eGIc+zWUGWZxp3edoVGiL6pHZ faIRNoGDXQOMupe+a8eAnh+XKWHA8xI7YdhImoBNKBYFCsst85ZlYHS4bV+PMEfGkxi+ cxhPNRIWYAAGLFgIYEIml0gfVC7D0rjuiQH7SiPIXPWNWogzmlIHefXvbyTtN5c+GSVu jgoQ== 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 m10si693147edr.459.2020.05.08.03.57.44; Fri, 08 May 2020 03:58:15 -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 S1726900AbgEHKxF (ORCPT + 99 others); Fri, 8 May 2020 06:53:05 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:4302 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726325AbgEHKxF (ORCPT ); Fri, 8 May 2020 06:53:05 -0400 Received: from DGGEMS401-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id C14AC10F0AFAD4AF2FD8; Fri, 8 May 2020 18:53:02 +0800 (CST) Received: from [127.0.0.1] (10.166.215.237) by DGGEMS401-HUB.china.huawei.com (10.3.19.201) with Microsoft SMTP Server id 14.3.487.0; Fri, 8 May 2020 18:52:53 +0800 From: Yunfeng Ye Subject: [PATCH v3] tools/bootconfig: fix resource leak in apply_xbc() To: , , , , , , Shiyuan Hu , Hewenliang Message-ID: <3569aa33-8963-966d-9247-ec79b3b3d56d@huawei.com> Date: Fri, 8 May 2020 18:52:53 +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) --- 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