From: Greg KH <gregkh@linuxfoundation.org>
To: Yang Yingliang <yangyingliang@huawei.com>
Cc: linux-kernel@vger.kernel.org, qemu-devel@nongnu.org,
linux-f2fs-devel@lists.sourceforge.net,
linux-erofs@lists.ozlabs.org, ocfs2-devel@oss.oracle.com,
linux-mtd@lists.infradead.org, amd-gfx@lists.freedesktop.org,
rafael@kernel.org, somlo@cmu.edu, mst@redhat.com,
jaegeuk@kernel.org, chao@kernel.org, hsiangkao@linux.alibaba.com,
huangjianan@oppo.com, mark@fasheh.com, jlbec@evilplan.org,
joseph.qi@linux.alibaba.com, akpm@linux-foundation.org,
alexander.deucher@amd.com, luben.tuikov@amd.com, richard@nod.at,
liushixin2@huawei.com
Subject: Re: [PATCH v3] kset: fix memory leak when kset_register() returns error
Date: Tue, 25 Oct 2022 18:57:51 +0200 [thread overview]
Message-ID: <Y1gVj6Uq2NDScYdY@kroah.com> (raw)
In-Reply-To: <20221025071549.1280528-1-yangyingliang@huawei.com>
On Tue, Oct 25, 2022 at 03:15:49PM +0800, Yang Yingliang wrote:
> Inject fault while loading module, kset_register() may fail.
> If it fails, the kset.kobj.name allocated by kobject_set_name()
> which must be called before a call to kset_register() may be
> leaked, since refcount of kobj was set in kset_init().
>
> To mitigate this, we free the name in kset_register() when an
> error is encountered, i.e. when kset_register() returns an error.
>
> A kset may be embedded in a larger structure which may be dynamically
> allocated in callers, it needs to be freed in ktype.release() or error
> path in callers, in this case, we can not call kset_put() in kset_register(),
> or it will cause double free, so just call kfree_const() to free the
> name and set it to NULL to avoid accessing bad pointer in callers.
>
> With this fix, the callers don't need care about freeing the name
> and may call kset_put() if kset_register() fails.
>
> Suggested-by: Luben Tuikov <luben.tuikov@amd.com>
> Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
> ---
> v2 -> v3:
> Update commit message and comment of kset_register().
>
> v1 -> v2:
> Free name inside of kset_register() instead of calling kset_put()
> in drivers.
Thank you for all of this, it's a much nicer and cleaner fix than
forcing all callers to try to handle it instead.
greg k-h
prev parent reply other threads:[~2022-10-25 16:58 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-25 7:15 Yang Yingliang
2022-10-25 16:51 ` Luben Tuikov
2022-10-25 16:57 ` Greg KH [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Y1gVj6Uq2NDScYdY@kroah.com \
--to=gregkh@linuxfoundation.org \
--cc=akpm@linux-foundation.org \
--cc=alexander.deucher@amd.com \
--cc=amd-gfx@lists.freedesktop.org \
--cc=chao@kernel.org \
--cc=hsiangkao@linux.alibaba.com \
--cc=huangjianan@oppo.com \
--cc=jaegeuk@kernel.org \
--cc=jlbec@evilplan.org \
--cc=joseph.qi@linux.alibaba.com \
--cc=linux-erofs@lists.ozlabs.org \
--cc=linux-f2fs-devel@lists.sourceforge.net \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mtd@lists.infradead.org \
--cc=liushixin2@huawei.com \
--cc=luben.tuikov@amd.com \
--cc=mark@fasheh.com \
--cc=mst@redhat.com \
--cc=ocfs2-devel@oss.oracle.com \
--cc=qemu-devel@nongnu.org \
--cc=rafael@kernel.org \
--cc=richard@nod.at \
--cc=somlo@cmu.edu \
--cc=yangyingliang@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®