Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp657511ybi; Thu, 30 May 2019 04:48:38 -0700 (PDT) X-Google-Smtp-Source: APXvYqw0c3vJuMFQ4tJeIRPHbq2v3XRkC+L/pb/RBGMWckuzY4xEWUtfs0vVsB8KsiFWvwTBYYtQ X-Received: by 2002:a17:90a:338e:: with SMTP id n14mr3051769pjb.35.1559216917961; Thu, 30 May 2019 04:48:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559216917; cv=none; d=google.com; s=arc-20160816; b=D0tff8bhcZSpMJOlrHI2Qi91TVq/459pwS293wmegxx2Teyj/pwnjkOMTLest7nkpP k02Gns5x6QlLKQdOpkYE6juGLs8/QIOzDxe785/FyaptAnfjfwQfOnR1HmVJW4aO4gHp +iu+U8Fx6Jp/UShSt+WAlHpaIUTVrU8ZwvSYRd685GHZgBYETOfwDtDuh2SjubiKWtq+ +bg20SUBe5KyMBv57ACxVjYxoxku9mdnEkOgS8l9CVU0mQal2xVChQJe20yU3kAbk4IY +MhDQyvXQ+ZDdjuXlM6mODv85WRLA/n3tnJDsFgQ4xv5lUeKOcWKU1Uq6WJT+GD1ruio i/nQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=hgQKzGcBzHlPbjGTQaV8Taeqo1sjQRxPdvQg3aVLjMQ=; b=wZTZTJowSRutB30+HMyYW6z65bslrTFxrtt0sy//lHInfy6Lhe2ermODQeftx1UUru xVZ9s7LoK/4hsg9Adv8zXa+fLmulj/oCY0hglvIqdcwZqaSrvpjyUjWqr62dH2W4Lt2P kvxHvc9iVZNR+w6gRRFOFuheLIkYyAEoUn/DJdkXf9l5N2HK4+2qGo7iQjYsKLYu7GHJ lrVDa7oF+kpw3PgM+9OCaSea5N77kQzJ66yrZgCTiDktZGRHzTbBnAyWNCXqDKqlXUN7 0NBkUPLRsFi8GF1XxuRK7DAbRQnqtcOJjj+u3O2WfoDI0tcsH9mNikWar1CuNJgys/hX jx/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=dH0gu+IC; 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 n65si2731494pji.53.2019.05.30.04.48.22; Thu, 30 May 2019 04:48:37 -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=dH0gu+IC; 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 S1726899AbfE3Lpn (ORCPT + 99 others); Thu, 30 May 2019 07:45:43 -0400 Received: from mail.kernel.org ([198.145.29.99]:55922 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726753AbfE3Lpn (ORCPT ); Thu, 30 May 2019 07:45:43 -0400 Received: from linux-8ccs (ip5f5adbeb.dynamic.kabel-deutschland.de [95.90.219.235]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 6CB0425870; Thu, 30 May 2019 11:45:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1559216743; bh=PhpF5tEM5wgtOzjgxk8gsiTVeBRVqa+xs5QiBuiQcrw=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=dH0gu+IC0B4jHeNfuao4WOi7JoZZlITyASxX335fAyu8Uk5+341UNgBy6Cs1fUtUc w5pJmmMRWYkRzQjI4bUwfYhBSf2cBriWhb3O8bortGeBgm3YofafT8zLFWJUpitNzK nGte1hr23hVHiqm9//hCUjmlSCX4mFeX49a6ux3s= Date: Thu, 30 May 2019 13:45:38 +0200 From: Jessica Yu To: YueHaibing Cc: gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org, paulmck@linux.ibm.com Subject: Re: [PATCH] kernel/module: Fix mem leak in module_add_modinfo_attrs Message-ID: <20190530114537.GA16012@linux-8ccs> References: <20190515161212.28040-1-yuehaibing@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20190515161212.28040-1-yuehaibing@huawei.com> X-OS: Linux linux-8ccs 5.1.0-rc1-lp150.12.28-default+ x86_64 User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org +++ 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. >+ 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 > >