* net/9p/mux.c: use-after-free
@ 2007-07-23 1:20 Adrian Bunk
2007-07-25 18:43 ` Eric Van Hensbergen
0 siblings, 1 reply; 5+ messages in thread
From: Adrian Bunk @ 2007-07-23 1:20 UTC (permalink / raw)
To: Latchesar Ionkov, Eric Van Hensbergen
Cc: v9fs-developer, netdev, linux-kernel
The Coverity checker spotted the following use-after-free
in net/9p/mux.c:
<-- snip -->
...
struct p9_conn *p9_conn_create(struct p9_transport *trans, int msize,
unsigned char *extended)
{
...
if (!m->tagpool) {
kfree(m);
return ERR_PTR(PTR_ERR(m->tagpool));
}
...
<-- snip -->
cu
Adrian
--
"Is there not promise of rain?" Ling Tan asked suddenly out
of the darkness. There had been need of rain for many days.
"Only a promise," Lao Er said.
Pearl S. Buck - Dragon Seed
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: net/9p/mux.c: use-after-free
2007-07-23 1:20 net/9p/mux.c: use-after-free Adrian Bunk
@ 2007-07-25 18:43 ` Eric Van Hensbergen
2007-07-25 19:13 ` Latchesar Ionkov
[not found] ` <20070725202030.7ecf339c.akpm@linux-foundation.org>
0 siblings, 2 replies; 5+ messages in thread
From: Eric Van Hensbergen @ 2007-07-25 18:43 UTC (permalink / raw)
To: Adrian Bunk; +Cc: Latchesar Ionkov, v9fs-developer, netdev, linux-kernel
On 7/22/07, Adrian Bunk <bunk@stusta.de> wrote:
> The Coverity checker spotted the following use-after-free
> in net/9p/mux.c:
>
> <-- snip -->
>
> ...
> struct p9_conn *p9_conn_create(struct p9_transport *trans, int msize,
> unsigned char *extended)
> {
> ...
> if (!m->tagpool) {
> kfree(m);
> return ERR_PTR(PTR_ERR(m->tagpool));
> }
> ...
>
> <-- snip -->
>
I've got a fix for this one:
if (!m->tagpool) {
mtmp = ERR_PTR(PTR_ERR(m->tagpool));
kfree(m);
return mtmp;
}
but I was wondering about one of the other returns further down the function:
...
memset(&m->poll_waddr, 0, sizeof(m->poll_waddr));
m->poll_task = NULL;
n = p9_mux_poll_start(m);
if (n)
return ERR_PTR(n);
n = trans->poll(trans, &m->pt);
...
lucho: doesn't that constitute a leak? Shouldn't we be doing:
if (n) {
kfree(m);
return ERR_PTR(n);
}
-eric
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: net/9p/mux.c: use-after-free
2007-07-25 18:43 ` Eric Van Hensbergen
@ 2007-07-25 19:13 ` Latchesar Ionkov
2007-07-25 19:45 ` Eric Van Hensbergen
[not found] ` <20070725202030.7ecf339c.akpm@linux-foundation.org>
1 sibling, 1 reply; 5+ messages in thread
From: Latchesar Ionkov @ 2007-07-25 19:13 UTC (permalink / raw)
To: Eric Van Hensbergen; +Cc: Adrian Bunk, v9fs-developer, netdev, linux-kernel
Yep, it's a leak.
Thanks,
Lucho
On 7/25/07, Eric Van Hensbergen <ericvh@gmail.com> wrote:
> On 7/22/07, Adrian Bunk <bunk@stusta.de> wrote:
> > The Coverity checker spotted the following use-after-free
> > in net/9p/mux.c:
> >
> > <-- snip -->
> >
> > ...
> > struct p9_conn *p9_conn_create(struct p9_transport *trans, int msize,
> > unsigned char *extended)
> > {
> > ...
> > if (!m->tagpool) {
> > kfree(m);
> > return ERR_PTR(PTR_ERR(m->tagpool));
> > }
> > ...
> >
> > <-- snip -->
> >
>
> I've got a fix for this one:
> if (!m->tagpool) {
> mtmp = ERR_PTR(PTR_ERR(m->tagpool));
> kfree(m);
> return mtmp;
> }
>
> but I was wondering about one of the other returns further down the function:
>
> ...
> memset(&m->poll_waddr, 0, sizeof(m->poll_waddr));
> m->poll_task = NULL;
> n = p9_mux_poll_start(m);
> if (n)
> return ERR_PTR(n);
>
> n = trans->poll(trans, &m->pt);
> ...
>
> lucho: doesn't that constitute a leak? Shouldn't we be doing:
>
> if (n) {
> kfree(m);
> return ERR_PTR(n);
> }
>
> -eric
>
^ permalink raw reply [flat|nested] 5+ messages in thread[parent not found: <20070725202030.7ecf339c.akpm@linux-foundation.org>]
* Re: net/9p/mux.c: use-after-free
[not found] ` <20070725202030.7ecf339c.akpm@linux-foundation.org>
@ 2007-07-26 14:29 ` Eric Van Hensbergen
0 siblings, 0 replies; 5+ messages in thread
From: Eric Van Hensbergen @ 2007-07-26 14:29 UTC (permalink / raw)
To: Andrew Morton; +Cc: Latchesar Ionkov, V9FS Developers, kernel list
On 7/25/07, Andrew Morton <akpm@linux-foundation.org> wrote:
> On Wed, 25 Jul 2007 13:43:16 -0500 "Eric Van Hensbergen" <ericvh@gmail.com> wrote:
>
> > mtmp = ERR_PTR(PTR_ERR(m->tagpool));
>
> odd. What does ERR_PTR(PTR_ERR(...)) do?
>
I kind of assumed it was a necessry evil to get the casting right. A
quick grep shows it in 42 other places within the kernel. Unpacking
the macros it looks like:
(void *)(long)(struct p9_idpool *)
So all that you would really need is (void *) or ERR_PTR -- but that
might look confusing in the code. Of course, broadening the context a
bit:
m->tagpool = p9_idpool_create();
if (!m->tagpool) {
mtmp = ERR_PTR(PTR_ERR(m->tagpool));
kfree(m);
return mtmp;
}
m->tagpool must be zero to enter the code at all, so we are returning
a NULL pointer, not really an error -- which is probably wrong (I
don't think it will properly trigger IS_ERR_VALUE) -- so we should
probably be returning -ENOMEM.
Of course, we really should be seeing an ERR_PTR returned from
p9_idpool_create, not 0 -- checking that code, it either returns
-ENOMEM or the correct value, never 0, so the check is wrong as well.
It should be:
m->tagpool = p9_idpool_create();
if (IS_ERR(m->tagpool)) {
mtmp = ERR_PTR(-ENOMEM);
kfree(m);
return mtmp;
}
We could have done:
ERR_PTR(m->tagpool);
or kept the long:
ERR_PTR(PTR_ERR(m->tagpool));
but I think returning an explicit error code keeps the code more clear.
So, which is the correct approach?
-eric
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2007-07-26 14:29 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-07-23 1:20 net/9p/mux.c: use-after-free Adrian Bunk
2007-07-25 18:43 ` Eric Van Hensbergen
2007-07-25 19:13 ` Latchesar Ionkov
2007-07-25 19:45 ` Eric Van Hensbergen
[not found] ` <20070725202030.7ecf339c.akpm@linux-foundation.org>
2007-07-26 14:29 ` Eric Van Hensbergen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox