lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250313063247.681-1-rakie.kim@sk.com>
Date: Thu, 13 Mar 2025 15:31:38 +0900
From: Rakie Kim <rakie.kim@...com>
To: Gregory Price <gourry@...rry.net>
Cc: akpm@...ux-foundation.org,
	linux-mm@...ck.org,
	linux-kernel@...r.kernel.org,
	linux-cxl@...r.kernel.org,
	joshua.hahnjy@...il.com,
	dan.j.williams@...el.com,
	ying.huang@...ux.alibaba.com,
	kernel_team@...ynix.com,
	honggyu.kim@...com,
	yunjeong.mun@...com,
	Rakie Kim <rakie.kim@...com>
Subject: Re: [PATCH v2 1/4] mm/mempolicy: Fix memory leaks in mempolicy_sysfs_init()

On Wed, 12 Mar 2025 11:49:06 -0400 Gregory Price <gourry@...rry.net> wrote:

Hi Gregory
Thank you for your response regarding this patch.

> On Wed, Mar 12, 2025 at 04:56:24PM +0900, Rakie Kim wrote:
> > Improper cleanup of sysfs attributes caused kobject and memory leaks when
> > initialization failed or nodes were removed.
> > 
> > This patch ensures proper deallocation of kobjects and memory, preventing
> > resource leaks and improving stability.
> >
> 
> This patch does multiple things, please split these changes into
> multiple patches.

This patch should remain as a single patch since all changes address
kobject-related memory issues in mempolicy_sysfs_init(). If you still
believe it should be split, I would appreciate your suggestion on
which parts should be separated.

> 
> > Fixes: dce41f5ae253 ("mm/mempolicy: implement the sysfs-based weighted_interleave interface")
> > Signed-off-by: Rakie Kim <rakie.kim@...com>
> > ---
> >  mm/mempolicy.c | 29 +++++++++++++++--------------
> >  1 file changed, 15 insertions(+), 14 deletions(-)
> > 
> > diff --git a/mm/mempolicy.c b/mm/mempolicy.c
> > index bbaadbeeb291..1691748badb2 100644
> > --- a/mm/mempolicy.c
> > +++ b/mm/mempolicy.c
> > @@ -3541,39 +3541,40 @@ static int __init mempolicy_sysfs_init(void)
> >  	int err;
> >  	static struct kobject *mempolicy_kobj;
> >  
> > -	mempolicy_kobj = kzalloc(sizeof(*mempolicy_kobj), GFP_KERNEL);
> > -	if (!mempolicy_kobj) {
> > +	node_attrs = kcalloc(nr_node_ids, sizeof(struct iw_node_attr *),
> > +			     GFP_KERNEL);
> > +	if (!node_attrs) {
> >  		err = -ENOMEM;
> >  		goto err_out;
> >  	}
> >  
> > -	node_attrs = kcalloc(nr_node_ids, sizeof(struct iw_node_attr *),
> > -			     GFP_KERNEL);
> > -	if (!node_attrs) {
> > +	mempolicy_kobj = kzalloc(sizeof(*mempolicy_kobj), GFP_KERNEL);
> > +	if (!mempolicy_kobj) {
> >  		err = -ENOMEM;
> > -		goto mempol_out;
> > +		goto node_out;
> >  	}
> 
> It's not clear to me why you re-ordered these allocations, it seems
> superfluous.
> 
> >  
> >  	err = kobject_init_and_add(mempolicy_kobj, &mempolicy_ktype, mm_kobj,
> >  				   "mempolicy");
> > -	if (err)
> > -		goto node_out;
> > +	if (err) {
> > +		kobject_put(mempolicy_kobj);
> 
> Is this correct? If kobject_init_and_add fails, from other examples we
> need only free the mempolicy_kobj - because it failed to initialize and
> therefore should not have any references.  I think this causes an
> underflow.

Regarding the reordering of mempolicy_kobj allocation:
1) In kobject_init_and_add(), kobject_init() is always called, which
   increments the kobject's refcount. Therefore, even if
   kobject_init_and_add() fails, kobject_put() must be called to ensure
   proper memory cleanup.

   int kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
                           struct kobject *parent, const char *fmt, ...)
   {
   ...
       kobject_init(kobj, ktype);
       retval = kobject_add_varg(kobj, parent, fmt, args);
   ...
       return retval;
   }

2) The release function for mempolicy_kobj is responsible for freeing
   associated memory:

   static void mempolicy_kobj_release(struct kobject *kobj)
   {
       ...
       kfree(ngrp->nattrs);
       kfree(ngrp);
       kfree(kobj);
   }

   Once mempolicy_kobj is passed to kobject_init_and_add(), the memory
   for ngrp->attrs and ngrp should be released via mempolicy_kobj_release().
   The allocation order was changed to ensure that kobject_put() properly
   invokes mempolicy_kobj_release() when required.

> 
> > +		goto err_out;
> > +	}
> >  
> >  	err = add_weighted_interleave_group(mempolicy_kobj);
> >  	if (err) {
> > -		pr_err("mempolicy sysfs structure failed to initialize\n");
> >  		kobject_put(mempolicy_kobj);
> > -		return err;
> > +		goto err_out;
> >  	}
> >  
> > -	return err;
> > +	return 0;
> > +
> 
> Please keep functional changes and refactors separate.  There's more
> churn in this patch than is needed to accomplish what the changelog
> states is the intent.

As mentioned earlier, I believe this patch does not need to be split.
However, if you have further concerns or suggestions, I would
appreciate your input.

> 
> >  node_out:
> >  	kfree(node_attrs);
> > -mempol_out:
> > -	kfree(mempolicy_kobj);
> >  err_out:
> > -	pr_err("failed to add mempolicy kobject to the system\n");
> > +	pr_err("mempolicy sysfs structure failed to initialize\n");
> >  	return err;
> > +
> >  }
> >  
> >  late_initcall(mempolicy_sysfs_init);
> > 
> > base-commit: 80e54e84911a923c40d7bee33a34c1b4be148d7a
> > -- 
> > 2.34.1
> > 

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ