Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp803012ybi; Thu, 30 May 2019 07:03:06 -0700 (PDT) X-Google-Smtp-Source: APXvYqz1WQGtP7mVamdPPZkSbxb8Xe7fQxlrmL9nFnTBBc9NfQnWUwJJU+jVvNez1gpiapXFEQiL X-Received: by 2002:a65:4c07:: with SMTP id u7mr3717357pgq.93.1559224985989; Thu, 30 May 2019 07:03:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559224985; cv=none; d=google.com; s=arc-20160816; b=NFy6KAYS2ND4QkyRdOwxTRNzCob5cIeTZi2+rb/VIOjYNKJjtJ/Uxi1nkYAzlpPqwh Iaai629ba8DKg8rZxsOhBitjQf6fq0icqRaeL+saLUWo0cU/+cMNeAVhdKmXegMkzkx8 TnMMJZnDTFTUMvIK1lCfmrnwitOteQ5o9fBPcKmBD3nV1woMRJYJPeg6BimYztLGEq0T rF1Ra/u6M0hNR8PAy7BJhnqSceOekcqwTgUai3Dv+L/jWF0PARWt0I5eUk6ub4fZjiaR cjcSdMXIJtSIC7Bu2rOQHSUzTZbWuTcmNKm2W4rcn2xJqyy1qFeCek/BE1vkOtb/lVIZ SKMA== 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=mpb491u4Xvg2J5louPuWXWIpNFevSZtw3TDxLAgjHF0=; b=mHaQgocDkCTglEaw+U8rRbB9DHGjHoBjSJuZv6mVtOz98NktncCrSkYPQkr6aApdSW j6wJ8pWXC7ZMV++nHRfvxHznCqww6PFby+OI19AMDoG8XQJFLjk1Iz+26zw8QoOSCAqw OAYT5/x7aeV8DamiDxFvdYECoNs/5HSLsiwRzLN2SLLa7VhYnXFQdF4EJZfl2x3QQQuB osZPk5p+wDw+HHIuXi1rqgO2L/3tDrm8CigAWWQTuetIUTKjZGHNBERMKrabZV1r8Uu8 bvrenw7a76W+0pCrdTE3ko/rih8rXETKL4daFxnHAbKLkQ7Ju5zujE2jCKPeP42mWoyN qbEA== 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 j10si3146096pgh.273.2019.05.30.07.02.36; Thu, 30 May 2019 07:03:05 -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 S1727307AbfE3N7W (ORCPT + 99 others); Thu, 30 May 2019 09:59:22 -0400 Received: from szxga04-in.huawei.com ([45.249.212.190]:18056 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726232AbfE3N7V (ORCPT ); Thu, 30 May 2019 09:59:21 -0400 Received: from DGGEMS401-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id 6E5F5EED7AC29741A111; Thu, 30 May 2019 21:59:19 +0800 (CST) Received: from localhost (10.133.213.239) by DGGEMS401-HUB.china.huawei.com (10.3.19.201) with Microsoft SMTP Server id 14.3.439.0; Thu, 30 May 2019 21:59:10 +0800 From: YueHaibing To: , CC: , , YueHaibing Subject: [PATCH v2] kernel/module: Fix mem leak in module_add_modinfo_attrs Date: Thu, 30 May 2019 21:43:04 +0800 Message-ID: <20190530134304.4976-1-yuehaibing@huawei.com> X-Mailer: git-send-email 2.10.2.windows.1 In-Reply-To: <20190515161212.28040-1-yuehaibing@huawei.com> References: <20190515161212.28040-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 --- 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; } -- 2.7.4