Re: net/9p/mux.c: use-after-free

From: Eric Van Hensbergen
Date: Thu Jul 26 2007 - 10:30:00 EST


On 7/25/07, Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> wrote:
On Wed, 25 Jul 2007 13:43:16 -0500 "Eric Van Hensbergen" <ericvh@xxxxxxxxx> 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
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/