Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp1391757ybj; Thu, 7 May 2020 23:44:14 -0700 (PDT) X-Google-Smtp-Source: APiQypL5rZaubBM29AaeUdXuNw5KS5My8PQyp+ML6u6ePUoR6StBTxy46wpDBOsvqdFxfWN3wo6H X-Received: by 2002:a50:f61c:: with SMTP id c28mr837431edn.365.1588920254186; Thu, 07 May 2020 23:44:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588920254; cv=none; d=google.com; s=arc-20160816; b=T7DTu7oRx1i2xM5RxDvArEM/hEbqt5lf3kQXX40BGawQZM6oym26KSTinYDw3zL5K3 7CgfGqD8yj9weE0OwobgpkmpnFFBwFEriGrR1vL+pr8tcbRdrsE7fcypjW7xJsl1wv6/ h7Q8MvvidXARBfK2CPfb6tkfN/+J+iRvnGSNidPCZ1Aqy7uGXGpq5Mu0p2pfxoLi4fVq ZOFMLCkguugRVOHJJQnq3eaGYEwwRLIYh4lZu4oW5sJwCPuLZl+Dcd6+4bt6tqlxOlcm qRIH4ykUO0TKXSnQeT2vywSBUX4V3FtKMKv52ol71lXdHu4dwqxdRhmBYdHvwpxR6qqT MysQ== 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:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=BYCMMRu4C2I+dfrtUobmGuexToo/jlHOBWCuhksdnto=; b=kpTgb7Rz6Y37AAV+hkmE5yiqvoB7t6Fu8NrfEu4DzFNB4YxWKk3Cv1lLG6VLCXrMQd uhNodkt9qd0dfZ0eC3utn8nqV4xmzJXn1YGrZKqWPCuyN5J+JbFZlpZfMesx73/ok/Yd sY5z+4Qv/qzXLUcrSw15dry6s64iGyJMPAhMj2DRP5go1XtH7Y5cww+SkUuBrLi+pimL C1r3rr4Dz8aadhMhb4rBN2BFqK5otaZaT2tDg9Lhctu5JS5B0AQn1Ex4PN9v94JnYPTG +glnUWfMs0N5TqqAhh6MxkD8hHXUX9Yv7NS/76+5gd4GrFzGWMf3HaLS2UBiu3xnLRTI nPaw== 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 p17si435510ejd.260.2020.05.07.23.43.51; Thu, 07 May 2020 23:44:14 -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 S1727103AbgEHGlo (ORCPT + 99 others); Fri, 8 May 2020 02:41:44 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:4292 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725988AbgEHGln (ORCPT ); Fri, 8 May 2020 02:41:43 -0400 Received: from DGGEMS411-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 63B79A0D0C0777DF1379; Fri, 8 May 2020 14:41:40 +0800 (CST) Received: from [127.0.0.1] (10.166.215.237) by DGGEMS411-HUB.china.huawei.com (10.3.19.211) with Microsoft SMTP Server id 14.3.487.0; Fri, 8 May 2020 14:41:32 +0800 Subject: Re: [PATCH] tools/bootconfig: fix resource leak in apply_xbc() To: Markus Elfring , Steven Rostedt , Masami Hiramatsu CC: , , Hewenliang , Shiyuan Hu References: <63755ce8-5e1b-6c54-2f74-649cc2546371@web.de> From: Yunfeng Ye Message-ID: <9359694c-57bf-729f-593f-6e25c4b115f6@huawei.com> Date: Fri, 8 May 2020 14:41:31 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: <63755ce8-5e1b-6c54-2f74-649cc2546371@web.de> Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 8bit 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 On 2020/5/8 4:30, Markus Elfring wrote: >> The @data and @fd is leak in the error path of apply_xbc(), so this >> patch fix it. > > I suggest to improve this change description. > > * Please use an imperative wording. > > * Would you like to add the tag “Fixes”? > > >> +++ 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; >> } > > I propose to adjust the exception handling. > https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?id=6e7f2eacf09811d092c1b41263108ac7fe0d089d#n450 > > - return ret; > + goto free_data; > > >> @@ -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; > > - 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 out; > > + goto free_data; > > >> } >> /* Write a magic word of the bootconfig */ >> ret = write(fd, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN); >> if (ret < 0) { > > - if (ret < 0) { > + if (ret < 0) > > >> pr_err("Failed to apply a boot config magic: %d\n", ret); >> - return ret; >> + goto out; > > I suggest to avoid an extra jump at such a place. > > >> } > > - } > + > > >> +out: > > +close_fd: >> close(fd); > > +free_data: >> free(data); > > > How do you think about to complete the error handling also at other > source code places? > ok, I will modify and send the patch v2, thanks. > Regards, > Markus > >