Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp234055ybi; Fri, 7 Jun 2019 07:14:06 -0700 (PDT) X-Google-Smtp-Source: APXvYqz5+w3Scw9/ROkzcBvTb4Uk2hmbAjGU0cWeyUJ0Aj667N67vuBg64BczuSZhAi6puHmjl6p X-Received: by 2002:a17:902:bc47:: with SMTP id t7mr43416979plz.336.1559916846611; Fri, 07 Jun 2019 07:14:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559916846; cv=none; d=google.com; s=arc-20160816; b=NFH28PuIppephwUKwIfHZ51mGv4DOOomfJ1dUczGyamIar+u5Ipo4o44Jby2mwGbaI WPLyytLxtw2VV1y2ODUYDitF8jyO79ycJaO9ywiCUjWcjtti+nAYYMkDDDNer6WgX3Iz R52F6fRIf6Nvc6eW9Od1FRRsCeAe06UOouO2aqaLFNkhls08FYg3+BT22xlzQq6C3+Tr cvuX+Te20CCwyljpRr0taFDvbEUF6jv/W72Yn7NrCfuCka5v5XgbL0TsySIugB8tT89V F/He5SmgJUrt/sVsa/WyMWulVNWHKEVwrkxbdD1YpdxjSEMakFIbpckzTY9RD7NI4ewm GG7w== 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=NffGV6pH3PijDnLhtmjUh0dN7h6YCVJJqoeDF+HMAMU=; b=aDTUvdS1EmZM9c3r+kdGpnBeL3+n8eNyfLj307o57Ir+ekFGPP8EXkg6vbDrXZTJR9 NaXg67M7y/UPNP+MempA3YKHd78r56w1pndul9bOJcEGq9Ah7+Eo9k1RlktI+T5Z+kLc hshzFMv0NL3Ff3GZfQ+4rkI2M1Vgm4dinsjxKvFmjSSyI02kTH6N7LxjJg0T9pJr6aXQ N57p6MraVPUdDjfIZSBdozZeo2476BvtnV40xvLRfFpqLLtaWNpOXt64oA5+86CED3KI oMPy9yM8CQQLVXdGsM6BCE4fapp7Gdp2y+uDl2j5aVdza94LfEKHlJXdX+ZnWfMCyKnw 8Xhg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Xf4RGhvO; 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 o6si1751040pjp.76.2019.06.07.07.13.47; Fri, 07 Jun 2019 07:14:06 -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=Xf4RGhvO; 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 S1729297AbfFGOCz (ORCPT + 99 others); Fri, 7 Jun 2019 10:02:55 -0400 Received: from mail.kernel.org ([198.145.29.99]:33960 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729125AbfFGOCz (ORCPT ); Fri, 7 Jun 2019 10:02:55 -0400 Received: from linux-8ccs (charybdis-ext.suse.de [195.135.221.2]) (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 EE132208E3; Fri, 7 Jun 2019 14:02:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1559916173; bh=yANB6XpweTjBX/1ZhMGTcILFCBjA6g8yFg9pmt84a2s=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=Xf4RGhvOB+GfodvUhSWUM0Ps9zCJzbmqMyi5WIzZLyDKA8FgQxHcVgHPCWZDlA4nb s1ffLNX1MNPhbnDaVNE94P6T7yPXl3nhq2eDAJbaugFeGY1Nv4WhO/TG2GYqg1IEQ3 Fc6EdmJ5OvxBo0W/lDrKpO/IhJ0t5HMpzoO2cGn4= Date: Fri, 7 Jun 2019 16:02:50 +0200 From: Jessica Yu To: Miroslav Benes Cc: YueHaibing , gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3] kernel/module: Fix mem leak in module_add_modinfo_attrs Message-ID: <20190607140250.GB4211@linux-8ccs> References: <20190530134304.4976-1-yuehaibing@huawei.com> <20190603144554.18168-1-yuehaibing@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: 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 +++ Miroslav Benes [04/06/19 12:46 +0200]: >On Mon, 3 Jun 2019, YueHaibing wrote: > >> 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 >> --- >> v3: reuse module_remove_modinfo_attrs >> v2: free from '--i' instead of 'i--' >> --- >> kernel/module.c | 21 ++++++++++++++++----- >> 1 file changed, 16 insertions(+), 5 deletions(-) > >I'm afraid it is not completely correct. > >> diff --git a/kernel/module.c b/kernel/module.c >> index 80c7c09..c6b8912 100644 >> --- a/kernel/module.c >> +++ b/kernel/module.c >> @@ -1697,6 +1697,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; >> @@ -1711,24 +1713,33 @@ 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; > >sysfs_create_file() failed, so we need to clear all previously processed >attrs and not the current one. > >> ++temp_attr; >> } >> } >> + >> + return 0; >> + >> +error_out: >> + module_remove_modinfo_attrs(mod, --i); > >It says "call sysfs_remove_file() on all attrs ending with --i included >(all correctly processed attrs). > >> 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; > >If end == 0, you break the loop without calling sysfs_remove_file(), which >is a bug if you called module_remove_modinfo_attrs(mod, 0). > >Calling module_remove_modinfo_attrs(mod, i); in module_add_modinfo_attrs() >under error_out label and changing the condition here to > >if (end >= 0 && i >= end) > break; > >should work as expected. > >But let me ask another question and it might be more to Jessica. Why is >there even a call to attr->free(mod); (if it exists) in >module_remove_modinfo_attrs()? The same is in free_modinfo() (as opposed >to setup_modinfo() where attr->setup(mod) is called. Is it because >free_modinfo() is called only in load_module()'s error path, while >module_remove_modinfo_attrs() is called even in free_module() path? > >kfree() checks for NULL pointer, so there is no bug, but it is certainly >not nice and it calls for cleanup. But I may be missing something. No, you are right in that it is a bit clumsy and and the sysfs error path handling is asymmetrical. I think it could be cleaned up a bit. IMO, I think the attr->free() calls should either be (1) removed from module_remove_modinfo_attrs() as free_modinfo() takes care of that, otherwise we could potentially call attr->free() twice (once in the internal error handling of mod_sysfs_setup() and once again in the free_modinfo: label in load_module()) or option (2) would be to merge the attr->setup() calls into module_add_modinfo_attrs() so that it is symmetrical to module_remove_modinfo_attrs(). I'm leaning towards option 2 but have not carefully checked yet if moving the attr->setup() calls into module_add_modinfo_attrs() would break anything. In any case I will prepare some cleanup patches for this. Thanks! Jessica