Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp569159ybj; Thu, 7 May 2020 02:28:05 -0700 (PDT) X-Google-Smtp-Source: APiQypLXBflC5GN/9fsUVtouxB2OfRTQd4mvgQWGvVeSnSoMvre/o7CzzLRGAU1+ROKtjz1XOeCS X-Received: by 2002:a17:906:a418:: with SMTP id l24mr11102334ejz.362.1588843684932; Thu, 07 May 2020 02:28:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588843684; cv=none; d=google.com; s=arc-20160816; b=O/Nm0OZZGLWRcgxfQKuGL86C+ja2q9/MlZAFM+m4dyBxfLj2vrq+md4UDBb0JlBSr4 WUPBc3NqpPVFdQDISJqoDD5ApRN5XRZRkJKNtRPOBC/Szz8aF3gOG2avSEPfyNG7PORq qRIKwjmaC4eEtoIzFLGxyJT4PqEkUkNMz9uqdwzEHgRdrQEr3K9SoJf/BbBu4Vp5KlB4 kD+UVeUd0Xqo5pF5JuxfFUpSbQbTq+KKdu1Uj9Y1HMcsxmX2hvYovoNYaTO9eVhV2Q7S gCcoN7WRxQouNuXOVUfxcng+IEpRKUvwmPv96c1nwdsohxl0tfO0I1J49KWj0EhtCdQC PfMw== 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=af6aZTt+KJ7DQxADdEaqad+xuF7UKLYpiX9sZdeb850=; b=v3JfMw2LYAU8ecRvEMls2MngPNY9yvY/wFU0ZFWBqjWNN+L3TNITWE1c7CQTYoTPA9 DdlSERyawYIfszk1oe3B+eStSTD3phUlnbPYtIxztU7XGXGy2ZmZzYFioJpgRvfrtLzm svthBamp/J36X31OT6WEPzLOiT4qW95F07lJfY82e9/gzCbeMrQzp4A3rUy9cwoaB/8g bGc007pBhrXhd76iZNmOMsyw6xYStvuIIpBvK+zJCXRp6YeL9KJ/ebPoZFi8y4fMIE0P 3HyzHAr0jJHaXxcjWQoyi+O4uGbNR8Rx+TgdHIKVhDtS0AjKwMPaMSjpL/+nQdJVGrIn s+Vg== 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 n16si3096908ejl.134.2020.05.07.02.27.42; Thu, 07 May 2020 02:28:04 -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 S1726437AbgEGJXr (ORCPT + 99 others); Thu, 7 May 2020 05:23:47 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:3839 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725857AbgEGJXr (ORCPT ); Thu, 7 May 2020 05:23:47 -0400 Received: from DGGEMS413-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 2E810F2A30BCBDF2A368; Thu, 7 May 2020 17:23:44 +0800 (CST) Received: from [127.0.0.1] (10.166.215.237) by DGGEMS413-HUB.china.huawei.com (10.3.19.213) with Microsoft SMTP Server id 14.3.487.0; Thu, 7 May 2020 17:23:37 +0800 To: , CC: , From: Yunfeng Ye Subject: [PATCH] tools/bootconfig: fix resource leak in apply_xbc() Message-ID: <583a49c9-c27a-931d-e6c2-6f63a4b18bea@huawei.com> Date: Thu, 7 May 2020 17:23:36 +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 The @data and @fd is leak in the error path of apply_xbc(), so this patch fix it. Signed-off-by: Yunfeng Ye --- tools/bootconfig/main.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tools/bootconfig/main.c b/tools/bootconfig/main.c index 16b9a420e6fd..001076c51712 100644 --- a/tools/bootconfig/main.c +++ b/tools/bootconfig/main.c @@ -314,6 +314,7 @@ 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); + free(data); return ret; } @@ -321,24 +322,26 @@ int apply_xbc(const char *path, const char *xbc_path) fd = open(path, O_RDWR | O_APPEND); if (fd < 0) { pr_err("Failed to open %s: %d\n", path, fd); + free(data); return fd; } /* 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 out; } /* Write a magic word of the bootconfig */ ret = write(fd, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN); if (ret < 0) { pr_err("Failed to apply a boot config magic: %d\n", ret); - return ret; + goto out; } +out: close(fd); free(data); - return 0; + return ret; } int usage(void) -- 1.8.3.1