Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp4241100ybi; Mon, 3 Jun 2019 07:48:50 -0700 (PDT) X-Google-Smtp-Source: APXvYqzTARiOQKsrENSy/PKbujDEPIP8HwprE0ykBWwgmy+txDZFbY7LyFdNQTps6HMFz+fGjOIA X-Received: by 2002:a63:fb01:: with SMTP id o1mr4859268pgh.410.1559573330649; Mon, 03 Jun 2019 07:48:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559573330; cv=none; d=google.com; s=arc-20160816; b=So8NbGvlGsNKlLlkTrMjN6uSgITTGS2XEKNOEgN3mA+W5OtDh6JbCcIAJitZxZIyDC jKmyRDtVrxFdVxsovp57JTAG8tkBQusONE2UcxGVgEXjH5zMG0E3JC9lhy+grcmg0IOT jEcGvOtpmppJc6Wh1RKgo5Q5g3HX/ZJ8AghmNCySd77mQAINgwSYKyPyt2PqMhkTKKH+ +FOuR4Rio+FVl6XPc+Yf/8t9RDjBoZTf5xswMur4T7QuSRlI4L7UKJt5iiuADx+WbLss mILicaPCBVF8uTXvehpQ4KS08mR9sY58Ah5Kd89tF1v5u4JEe+jZNh4GqEx9VZmuVKHm wgEg== 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=sXgHCWPijkZq17I/gCggLsBX32d/eqMrVTnopvXNAm0=; b=uC2SL4LUEnjXFlbvcZ8I9jQ7zgxVddy1qP4OLGr8+3FWX6W8lEED9nW/8d7G0jg83g kbb+MP2JBXxywzm27N7hNcaxWqA+RPLJOCTJoVVOt0Nk9Q8hlFwhnghqutSXCT31YR4l fcCGj6L8Yo6hVLPbp+wXcdtMNxNqmyYx15YNfQ5uZRFyupXJRum9ZyjmwPGrdXhYd0V0 BVp9XuO4MOyXYpCZqgR4feSKIoaxLZJG0WTUkNnqgL2XZ6axpbGe459lCiAMSQdbt8af 27o9mgrtXDo2Y2kIuar9vnCUp7qSN0UHc/+DzDeHymrHfiB6s8qk9wyzhmB/YgGzqAND t7iw== 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 q8si15096792pll.205.2019.06.03.07.48.34; Mon, 03 Jun 2019 07:48:50 -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 S1729291AbfFCOpO (ORCPT + 99 others); Mon, 3 Jun 2019 10:45:14 -0400 Received: from szxga04-in.huawei.com ([45.249.212.190]:18074 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727650AbfFCOpN (ORCPT ); Mon, 3 Jun 2019 10:45:13 -0400 Received: from DGGEMS407-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id B5E9311A6A4288644CC; Mon, 3 Jun 2019 22:45:11 +0800 (CST) Received: from [127.0.0.1] (10.133.213.239) by DGGEMS407-HUB.china.huawei.com (10.3.19.207) with Microsoft SMTP Server id 14.3.439.0; Mon, 3 Jun 2019 22:45:09 +0800 Subject: Re: [PATCH v2] kernel/module: Fix mem leak in module_add_modinfo_attrs To: Miroslav Benes References: <20190515161212.28040-1-yuehaibing@huawei.com> <20190530134304.4976-1-yuehaibing@huawei.com> CC: , , , From: Yuehaibing Message-ID: <27d47cab-b40b-3566-1a01-db11ada9815b@huawei.com> Date: Mon, 3 Jun 2019 22:45:08 +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: 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/6/3 20:11, Miroslav Benes wrote: > On Thu, 30 May 2019, YueHaibing wrote: > >> 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 >> --- >> v2: free from '--i' instead of 'i--' >> --- >> kernel/module.c | 16 +++++++++++++++- >> 1 file changed, 15 insertions(+), 1 deletion(-) >> >> diff --git a/kernel/module.c b/kernel/module.c >> index 6e6712b..78e21a7 100644 >> --- a/kernel/module.c >> +++ b/kernel/module.c >> @@ -1723,15 +1723,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) { >> + 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; >> } > > Hi, > > would not be better to reuse the existing code in > module_remove_modinfo_attrs() instead of duplication? You could add a new > parameter "limit" or something and call the function here. I suppose the > order does not matter and if it does you could rename it "start" and go > backwards like in your patch. This make sense, try do it in v3, thanks! > > Btw, looking more into it, it would also be possible to let > mod_sysfs_setup() go to out_unreg_modinfo_attrs in case of an error from > module_add_modinfo_attrs() (and then clean the error handling in > mod_sysfs_setup() a bit). module_remove_modinfo_attrs() almost does the > right thing, because it checks attr->attr.name. The only problem is the > last failing attribute, because it would have attr.name set, but its > sysfs_create_file() failed. So calling sysfs_remove_file() and the rest > would not be correct in that case. > > Miroslav > > . >