Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp4378469ybi; Mon, 3 Jun 2019 09:52:56 -0700 (PDT) X-Google-Smtp-Source: APXvYqztWiw9LC9nH6LZUJRJziRdEkl+IuGr6CSLuw5BqgGgoXOSKwA2TKIeQQWeBpmvea4aOe98 X-Received: by 2002:a17:902:5c2:: with SMTP id f60mr31135195plf.61.1559580776506; Mon, 03 Jun 2019 09:52:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559580776; cv=none; d=google.com; s=arc-20160816; b=LNi3SttTJS815ZZVZCHT1R0s09kV+2CxXkbNEUrTrHx7Q+x/hLsPWsEwyPGIDM2sAX B/9k9raQxRykKYtvlA7vEj/KuC0b2pa2pat9dNHsN9gQ34jx+ccgfv+Ed0Cks997PeHO nimzZ8/NsyGaPPXLzqNvS7zt3cyZq/wHorx0F4j/4ZkdgLb3IvEKQBRlWOSF9L9XS93Z kxiYLfeoQ3hi+daA/ulEba4oZviFF7FluXqR+anYY8PjSGSxbIE6mXtysYhYDUez646Y 93bEUFBcoiSrgEMy8tGGJg5BK/LlQAN4QBi8Ji0pa/nx37st9B3RnP4LNhAgXfrazht9 wjYQ== 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=KNqHAJw6Fvsmp2cyTS15kY3JEPJQ/XNBXoFX+YeB8Ho=; b=xkJGPQKX5m3CVlJVfuARd+STK2xbWJrcaN1K1Ko2LBmznmHQENsrhwQaCa+HQRdl+f HAZZsuzZlh9QgAUv3W4Nol0IFBvbmphDaFhmDcBvHbwJuwJK4PuTT6/SASgLpsnD1in3 hrLLGk9ssEs5uyxCkNIw3fzIr7imsdT0Z+o60n2rNhiZSCRbXEO3yn1KYVj8kGbEAzCp ygd6pmhBi2qGNO6O0618bXG6+BbQnYswFk3KTSsRhGShpPlQBLbsIWdb5QfLjzGSRtov nc6g3cwQjHqYLGoaLKLwF6ZNxvpsfEo/k4L1r8Pp/AASXXw7q9VH8dG0o/d1Sg0Ugio4 g9zQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=xXCKGzQD; 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 az8si19163874plb.219.2019.06.03.09.52.36; Mon, 03 Jun 2019 09:52: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=xXCKGzQD; 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 S1727339AbfFCKrV (ORCPT + 99 others); Mon, 3 Jun 2019 06:47:21 -0400 Received: from mail.kernel.org ([198.145.29.99]:51300 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727190AbfFCKrV (ORCPT ); Mon, 3 Jun 2019 06:47:21 -0400 Received: from linux-8ccs (ip5f5ade7c.dynamic.kabel-deutschland.de [95.90.222.124]) (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 CB5C6280B9; Mon, 3 Jun 2019 10:47:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1559558840; bh=Cql2az1OBhkCOJT4G/r6/wJd19LJBexin+i8h1nPh+8=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=xXCKGzQDL3hP2iNqU4OyO7vRk37Le8G4Jimzfb16BbHS5FXG0fL1KFIhqn/gv28Ci NmNmCLxgUFW/cc4HgULvQqOIleJTD16Xrg8UtVSsa2VoOH4j738w+dC78v7mX1VMgv Y4cLVlQFf3kcun0H2yqiZ2NlHBECuLSy5cTmiZvA= Date: Mon, 3 Jun 2019 12:47:16 +0200 From: Jessica Yu To: YueHaibing Cc: gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org, paulmck@linux.ibm.com Subject: Re: [PATCH v2] kernel/module: Fix mem leak in module_add_modinfo_attrs Message-ID: <20190603104716.GA21759@linux-8ccs> References: <20190515161212.28040-1-yuehaibing@huawei.com> <20190530134304.4976-1-yuehaibing@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20190530134304.4976-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 [30/05/19 21:43 +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 >--- >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) { The increment step is executed after the body of the loop, so this is still starting at i instead of i - 1. I think we need: for (--i; (attr = &mod->modinfo_attrs[i]) && i >= 0; i--) Thanks, Jessica >+ 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 > >