Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp2118405ybl; Thu, 29 Aug 2019 03:55:57 -0700 (PDT) X-Google-Smtp-Source: APXvYqzYd3onW9ovMIcobmIToL172U55lkLjmurVTV0sLMYhj/ztSmjuA8vduNcBPYA7Pig5Sw0t X-Received: by 2002:a17:902:8e8b:: with SMTP id bg11mr9046377plb.93.1567076156966; Thu, 29 Aug 2019 03:55:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1567076156; cv=none; d=google.com; s=arc-20160816; b=daEddQoglHdJRLtElBmutyw17COELrdYNZsICNZQXqFT5vz1ngJ2GOB8A1pW0hqGxG CHGqzJuGQwaF79WhnxWCe2FFOkDFd3/QrYsVeoCtyoiRyz5fXlCorhw9tX46bq6RsJCk FU5dknaUst573z9U+sMCCJBNzjMM/5gqpGMew7WDhCDqQS0AVdkzKsdtyS5Ba5uIa1Jf +pwg+WFNka6tslflP5m1N2d/cysSNR/dFtySIy6WltakGH5T7FTdhptpbvnWfBpjiBCd USF1V+uVMbnULtjnbLMJe4R5n9ofGO6VLduxo8YNQqOlj+MrsL4XNexmwwlaPveSezm+ KyFA== 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:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=oewbj1KqtwiamB7PYu23vztsJSnpGaZZwNj4d2IEA7g=; b=WuKyhTEgnY3tDFVengXqXMtutHPMfJjUkh8VoJQEevUFp99QVpPjwjx0dnV5H6FSdj ZOL67KpZFKMcHvvvT7YFfQ4/tiBQAsFlQZMT2u+EIJ1l5SzggR3sNm61x0eNhsnD6qK0 +yoYUXMxeUpj7S8244YQZZudpZUyNMxhMJPfFUMoQM3bMjHD3Dhwn5Fi9uDEk3ZqWQeM sAoG98LA8xc1iIKU++lvj6ZRJGuFfrrK8bU7KAq8TS9a2/oFPrhtPHlwOXJcnjBmbUL5 Sy86ZIcaz5Jv4xdYvjtA37n0MiSCxPXVo6q5mtpkgmQjoo9InVa6AMMerRcEgHlnKgoz PujQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=0lMpSRt9; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x20si1777058pjn.49.2019.08.29.03.55.41; Thu, 29 Aug 2019 03:55: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; dkim=pass header.i=@kernel.org header.s=default header.b=0lMpSRt9; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728463AbfH2Kw7 (ORCPT + 99 others); Thu, 29 Aug 2019 06:52:59 -0400 Received: from mail.kernel.org ([198.145.29.99]:58606 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728378AbfH2KvB (ORCPT ); Thu, 29 Aug 2019 06:51:01 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 7E0082173E; Thu, 29 Aug 2019 10:50:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1567075860; bh=8E1ZaQQdv9Tgy5lwu31euuxv9rkT1in47LdivFwcPEE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0lMpSRt9i7K1xZV/BgEfl20mEXABhz8/7G2nKdTfZteVs3OJ0+b73QLmEJsYAZbkZ eJiDiQXcAOgYkvZ5Rgdll1zh4HDRFzk8LihbzAHpM7ob1Q0U302BQYtejpTIqSXqx9 x5glDl0LTfBlF/Xnu88qZ0aJNxSMsofDElqa3sN4= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: YueHaibing , Miroslav Benes , Jessica Yu , Sasha Levin Subject: [PATCH AUTOSEL 4.14 14/14] kernel/module: Fix mem leak in module_add_modinfo_attrs Date: Thu, 29 Aug 2019 06:50:43 -0400 Message-Id: <20190829105043.2508-14-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190829105043.2508-1-sashal@kernel.org> References: <20190829105043.2508-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: YueHaibing [ Upstream commit bc6f2a757d525e001268c3658bd88822e768f8db ] 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") Reviewed-by: Miroslav Benes Signed-off-by: YueHaibing Signed-off-by: Jessica Yu Signed-off-by: Sasha Levin --- kernel/module.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/kernel/module.c b/kernel/module.c index 4b372c14d9a1f..4685675912414 100644 --- a/kernel/module.c +++ b/kernel/module.c @@ -1695,6 +1695,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; @@ -1709,24 +1711,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; @@ -1814,7 +1826,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: @@ -1850,7 +1862,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) { } @@ -1866,7 +1878,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.20.1