From: James Bottomley <James.Bottomley@HansenPartnership.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Matthew Wilcox <willy@infradead.org>,
Wang Hai <wanghai38@huawei.com>,
cl@linux.com, penberg@kernel.org, rientjes@google.com,
iamjoonsoo.kim@lge.com, akpm@linux-foundation.org,
khlebnikov@yandex-team.ru, linux-mm@kvack.org,
linux-kernel@vger.kernel.org
Subject: Re: kobject_init_and_add is easy to misuse
Date: Tue, 02 Jun 2020 17:04:43 -0700 [thread overview]
Message-ID: <1591142683.16819.44.camel@HansenPartnership.com> (raw)
In-Reply-To: <1591134670.16819.18.camel@HansenPartnership.com>
On Tue, 2020-06-02 at 14:51 -0700, James Bottomley wrote:
> On Tue, 2020-06-02 at 22:07 +0200, Greg Kroah-Hartman wrote:
> > On Tue, Jun 02, 2020 at 12:54:16PM -0700, James Bottomley wrote:
>
> [...]
> > > I think the only way we can make the failure semantics consistent
> > > is to have the kobject_init() ones (so kfree on failure). That
> > > means for the add part, the function would have to unwind
> > > everything it did from init on so kfree() is still an option. If
> > > people agree, then I can produce the patch ... it's just the
> > > current drive to transform everyone who's doing kfree() into
> > > kobject_put() would become wrong ...
> >
> > Everyone should be putting their kfree into the kobject release
> > anyway, right?
>
> No, that's the problem ... for a static kobject you can't free it;
> and the release path may make assumption which aren't valid depending
> on the kobject state.
>
> > Anyway, let's see your patch before I start to object further :)
>
> My first thought was "what? I got suckered into creating a patch",
> thanks ;-) But now I look, all the error paths do unwind back to the
> initial state, so kfree() on error looks to be completely correct.
Actually, I spoke too soon. I did another analysis of the syzkaller
flow in b8eb718348b8 ("net-sysfs: Fix reference count leak in
rx|netdev_queue_add_kobject") and it turns out there is a single piece
of state that's not correctly unwound: the kobj->name which, thanks to
additions after kobject_init_and_add() was created, is now allocated
via kmalloc if it's not a rodata string and is always and freed in
kobject_cleanup via kfree_const(). This problem can be fixed by
unwinding the name allocation at the end of kobject_init_and_add() ...
or it could be unwound in kobject_add_varg, which would also make
kobject_add() unwind correctly.
The unwind step is to kfree_const(kobj->name); kobj->name = NULL; so it
won't interfere if the kobject_put() is called instead of a simple
kfree.
Would you prefer the unwind in kobject_init_and_add() like the patch
below or in kobject_add_varg()?
James
---
diff --git a/lib/kobject.c b/lib/kobject.c
index 65fa7bf70c57..9991baf43d27 100644
--- a/lib/kobject.c
+++ b/lib/kobject.c
@@ -472,6 +472,10 @@ int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
va_start(args, fmt);
retval = kobject_add_varg(kobj, parent, fmt, args);
va_end(args);
+ if (retval && kobj->name) {
+ kfree_const(kobj->name);
+ kobj->name = NULL;
+ }
return retval;
}
next prev parent reply other threads:[~2020-06-03 0:04 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-06-02 11:50 [PATCH] mm/slub: fix a memory leak in sysfs_slab_add() Wang Hai
2020-06-02 12:10 ` kobject_init_and_add is easy to misuse Matthew Wilcox
2020-06-02 13:48 ` Konstantin Khlebnikov
2020-06-02 14:04 ` Greg Kroah-Hartman
2020-06-02 14:57 ` Matthew Wilcox
2020-06-02 15:25 ` James Bottomley
2020-06-02 17:36 ` Greg Kroah-Hartman
2020-06-02 19:54 ` James Bottomley
2020-06-02 20:07 ` Greg Kroah-Hartman
2020-06-02 21:51 ` James Bottomley
2020-06-03 0:04 ` James Bottomley [this message]
2020-06-03 0:22 ` Jason Gunthorpe
2020-06-03 18:04 ` James Bottomley
2020-06-03 18:36 ` Jason Gunthorpe
2020-06-03 19:02 ` James Bottomley
2020-06-03 19:30 ` Jason Gunthorpe
2020-06-03 20:56 ` James Bottomley
2020-06-04 0:23 ` Jason Gunthorpe
2020-06-02 19:46 ` Jason Gunthorpe
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=1591142683.16819.44.camel@HansenPartnership.com \
--to=james.bottomley@hansenpartnership.com \
--cc=akpm@linux-foundation.org \
--cc=cl@linux.com \
--cc=gregkh@linuxfoundation.org \
--cc=iamjoonsoo.kim@lge.com \
--cc=khlebnikov@yandex-team.ru \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=penberg@kernel.org \
--cc=rientjes@google.com \
--cc=wanghai38@huawei.com \
--cc=willy@infradead.org \
/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