net/9p/mux.c: use-after-free
Subsystems (2)
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
Signature · 4 lines
"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 SeedQuoted 10 lines · Adrian Bunk
The Coverity checker spotted the following use-after-free in
net/9p/mux.c:<-- snip -->
...
structp9_conn*p9_conn_create(structp9_transport*trans, int msize,
unsigned char *extended)
{
...
Quoted 8 lines
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);
}
-ericYep, it's a leak.
Thanks,
Lucho
Quoted 48 lines · Eric Van Hensbergen
Quoted 10 lines · Adrian Bunk
The Coverity checker spotted the following use-after-free in
net/9p/mux.c:<-- snip -->
...
structp9_conn*p9_conn_create(structp9_transport*trans, int msize,
unsigned char *extended)
{
...Quoted 8 lines
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
Quoted 2 lines · Latchesar Ionkov
Yep, it's a leak.
Okay, I'll roll that into the patch as well.
-eric
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 6 lines · Andrew Morton
Quoted 1 line · On Wed, 25 Jul 2007 13:43:16 -0500 "Eric Van Hensbergen" <[email protected]>…
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