Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp5333243ybi; Wed, 12 Jun 2019 00:23:57 -0700 (PDT) X-Google-Smtp-Source: APXvYqwC6bL+T/juUcR7oc1wPpgT0NzAMp4WF1428p+BleYJ/hoGiF3BcrPD+UOqzmvqh3ssofWA X-Received: by 2002:a17:902:6bcb:: with SMTP id m11mr52663231plt.318.1560324236970; Wed, 12 Jun 2019 00:23:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560324236; cv=none; d=google.com; s=arc-20160816; b=GPHLqycm0bKAE6K/16cn9UVV7F9jV8/G9NC8uKhCx/dsRsFyTZX24CvOU9KA+oXB9J Cdb7CwM3SGlf+UB9H9kkWm5zWnWnaMlweqLBZIJEVR/lBlv6o4IehyYvrpqIibkttAJ9 Cos9g9akYB/w7b2KOzQbLOumSzvdJWDyzRsR1PYN/p73+Hx4l9/7nYSftDAmWquzlczZ LrxQD4xn5r86JaccbwA0rubExO5KzflGZy8NF0C9XHPbEWcbaxJBJtZHV1rDqllWWNwi ooMm9jPutzSrsRp/YPenJNAq/G4m5zrIzap8oK1JzSe9RSAITYUnIi5Dbq1taS820ZVc sAlg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=+TbyuYhdnylf9oiCeZiWS7RPN7CvlWe5pqsZjICz6OE=; b=MMZGkFd3Xt1n3QG/IfkQRcbNYlsJEOB9xOadVop41Fq/bMu05UTVsjBihc56bwufPJ tH9ijRMRN7a90ZAfB+0BJVlHb530pEUsA2rz5ZAw73Jn93g6HgzKmiawzyrXzXYOiqiu gG+tJIdv0sViWv8oQ44gIBp2kf8lK2MbsHuTI8GnX5xNsgx7bhaJw4lMfZEpx1I1SQC0 j/SCmVcfnG02M7Oh8CeDUpis2Vg05RtA/w3IVdbNSKAST4AqD3JHO0K/cTgqlkoi09Ow meYwiKgcpL6fChM8XcPl2bPmjkGudBt4m7s8B0TaWdH43c1Qt7mfYlme5EVy7cjKHa/y uj/w== 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 k14si15391191pfa.206.2019.06.12.00.23.41; Wed, 12 Jun 2019 00:23:56 -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 S2406761AbfFLB1k (ORCPT + 99 others); Tue, 11 Jun 2019 21:27:40 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:18130 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S2406215AbfFLB1k (ORCPT ); Tue, 11 Jun 2019 21:27:40 -0400 Received: from DGGEMS409-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 7885718CA3B9CC4C27AE; Wed, 12 Jun 2019 09:27:38 +0800 (CST) Received: from localhost (10.133.213.239) by DGGEMS409-HUB.china.huawei.com (10.3.19.209) with Microsoft SMTP Server id 14.3.439.0; Wed, 12 Jun 2019 09:27:28 +0800 From: YueHaibing To: , , CC: , YueHaibing Subject: [PATCH v4] kernel/module: Fix mem leak in module_add_modinfo_attrs Date: Tue, 11 Jun 2019 23:00:07 +0800 Message-ID: <20190611150007.21064-1-yuehaibing@huawei.com> X-Mailer: git-send-email 2.10.2.windows.1 In-Reply-To: <20190603144554.18168-1-yuehaibing@huawei.com> References: <20190603144554.18168-1-yuehaibing@huawei.com> MIME-Version: 1.0 Content-Type: text/plain 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 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 --- v4: call module_remove_modinfo_attrs only while i > 0 v3: reuse module_remove_modinfo_attrs v2: free from '--i' instead of 'i--' --- kernel/module.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/kernel/module.c b/kernel/module.c index f954d72..3310a39 100644 --- a/kernel/module.c +++ b/kernel/module.c @@ -1696,6 +1696,8 @@ static int add_usage_links(struct module *mod) return ret; } +static void module_remove_modinfo_attrs(struct module *mod, int end); + static int module_add_modinfo_attrs(struct module *mod) { struct module_attribute *attr; @@ -1710,24 +1712,34 @@ 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: + if (i > 0) + module_remove_modinfo_attrs(mod, --i); return error; } -static void module_remove_modinfo_attrs(struct module *mod) +static void module_remove_modinfo_attrs(struct module *mod, int end) { struct module_attribute *attr; int i; for (i = 0; (attr = &mod->modinfo_attrs[i]); i++) { + if (end >= 0 && i > end) + break; /* pick a field to test for end of list */ if (!attr->attr.name) break; @@ -1815,7 +1827,7 @@ static int mod_sysfs_setup(struct module *mod, return 0; out_unreg_modinfo_attrs: - module_remove_modinfo_attrs(mod); + module_remove_modinfo_attrs(mod, -1); out_unreg_param: module_param_sysfs_remove(mod); out_unreg_holders: @@ -1851,7 +1863,7 @@ static void mod_sysfs_fini(struct module *mod) { } -static void module_remove_modinfo_attrs(struct module *mod) +static void module_remove_modinfo_attrs(struct module *mod, int end) { } @@ -1867,7 +1879,7 @@ static void init_param_lock(struct module *mod) static void mod_sysfs_teardown(struct module *mod) { del_usage_links(mod); - module_remove_modinfo_attrs(mod); + module_remove_modinfo_attrs(mod, -1); module_param_sysfs_remove(mod); kobject_put(mod->mkobj.drivers_dir); kobject_put(mod->holders_dir); -- 2.7.4