Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp774327ybi; Thu, 30 May 2019 06:35:10 -0700 (PDT) X-Google-Smtp-Source: APXvYqwHSLuCNwTsz70s5R0eqLKDbd403j17WSKStiVzPkwhtlBQRDZ3DRJRPEiL3d/KI4AqEFxp X-Received: by 2002:a17:90a:bd10:: with SMTP id y16mr3480689pjr.92.1559223310786; Thu, 30 May 2019 06:35:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559223310; cv=none; d=google.com; s=arc-20160816; b=f/At91drHvz1YxCbgeN2IGNk31+5uOQGCx3HHB9CgK1ch5kLbA7/DPuCivvoeUdDEX VIIaUzotYxkOajNr74ZHvepCGd+OmA5SoNYuiqjGS94lzKxWVDeKUWatJ+a4VYV45jze a5Nd3ysTRhVuMeuqAXj2g/EkskVI4Gk43zCcgF8ffbcg61yT1trqOuXuD2Q3XzOn87LR yih2HpD267G6cJzUngqLA4y5eNHgxI9p4fovwVfwP3lgMokmBnZpS81tf6oVvQGqsSo2 Bwtvtc985wvPUhi18t+B+gdtMXZQsE0TrWrhFK8HyoGYeFJyf49sJyIiveysR9QX0Jtk 1pGQ== 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:in-reply-to :mime-version:user-agent:date:message-id:from:cc:references:to :subject; bh=v9bv9HBl2yHpyPuz7XhDnJykgz0VsE8MUTUAoRbN1bI=; b=xNYNansogquh+ZUsYPJ5WBieP55K77yC2+SNqeos2F3tmlXTiAO83Sv+HAFQ6+8lqh 9s/MzLG3SDr1Lk0TTBLHpshpRhRiGtDRbr+XjL2QKutcKyjsjasFPWVnSjBG7iBwjRe+ 0nM8o2Qs5gTRIbO0j9w06OdEr9WLbalMorv1PvI2nnwvFxcQE/2l14V78jYGbDaGOh82 IMcrPhW/zGkTObUSkOVlByEOgWZ1BVzrslzwScUt9g5ITu6j7bXQcdUxUkomFUq5tSsT qzL3kyRucC/Y9PyazckGOQQFtg/TZu813Tcbx9kS4PSvzMRn/fjlS++WRiWOhtgcC0Kl WwBA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z18si3220073pgv.229.2019.05.30.06.34.54; Thu, 30 May 2019 06:35:10 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727220AbfE3Ncw (ORCPT + 99 others); Thu, 30 May 2019 09:32:52 -0400 Received: from szxga07-in.huawei.com ([45.249.212.35]:42008 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726253AbfE3Ncw (ORCPT ); Thu, 30 May 2019 09:32:52 -0400 Received: from DGGEMS404-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id EDA533D0C73414C97BFB; Thu, 30 May 2019 21:32:49 +0800 (CST) Received: from [127.0.0.1] (10.133.213.239) by DGGEMS404-HUB.china.huawei.com (10.3.19.204) with Microsoft SMTP Server id 14.3.439.0; Thu, 30 May 2019 21:32:45 +0800 Subject: Re: [PATCH] kernel/module: Fix mem leak in module_add_modinfo_attrs To: Jessica Yu References: <20190515161212.28040-1-yuehaibing@huawei.com> <20190530114537.GA16012@linux-8ccs> CC: , , From: Yuehaibing Message-ID: <957d2c2f-2eff-fc89-00dd-6cdee6c2bf34@huawei.com> Date: Thu, 30 May 2019 21:32:45 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 MIME-Version: 1.0 In-Reply-To: <20190530114537.GA16012@linux-8ccs> Content-Type: text/plain; charset="windows-1252" Content-Transfer-Encoding: 7bit X-Originating-IP: [10.133.213.239] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2019/5/30 19:45, Jessica Yu wrote: > +++ YueHaibing [16/05/19 00:12 +0800]: >> In module_add_modinfo_attrs if sysfs_create_file >> fails, we forget to free allocated modinfo_attrs >> and roll back the sysfs files. >> >> Fixes: 03e88ae1b13d ("[PATCH] fix module sysfs files reference counting") >> Signed-off-by: YueHaibing >> --- >> kernel/module.c | 16 +++++++++++++++- >> 1 file changed, 15 insertions(+), 1 deletion(-) >> >> diff --git a/kernel/module.c b/kernel/module.c >> index 0b9aa8a..7da73c4 100644 >> --- a/kernel/module.c >> +++ b/kernel/module.c >> @@ -1714,15 +1714,29 @@ static int module_add_modinfo_attrs(struct module *mod) >> return -ENOMEM; >> >> temp_attr = mod->modinfo_attrs; >> - for (i = 0; (attr = modinfo_attrs[i]) && !error; i++) { >> + for (i = 0; (attr = modinfo_attrs[i]); i++) { >> if (!attr->test || attr->test(mod)) { >> memcpy(temp_attr, attr, sizeof(*temp_attr)); >> sysfs_attr_init(&temp_attr->attr); >> error = sysfs_create_file(&mod->mkobj.kobj, >> &temp_attr->attr); >> + if (error) >> + goto error_out; >> ++temp_attr; >> } >> } >> + >> + return 0; >> + >> +error_out: >> + for (; (attr = &mod->modinfo_attrs[i]) && i >= 0; i--) { > > I think we need to start at --i. If sysfs_create_file() returned > an error at index i, we call sysfs_remove_file() starting from the > previously successful call to sysfs_create_file(), i.e. at i - 1. Indeed, you are right. will fix it in v2, thanks! > >> + if (!attr->attr.name) >> + break; >> + sysfs_remove_file(&mod->mkobj.kobj, &attr->attr); >> + if (attr->free) >> + attr->free(mod); >> + } >> + kfree(mod->modinfo_attrs); >> return error; >> } >> >> -- >> 1.8.3.1 >> >> > > . >