Skip to contentSkip to conversation

Fix a use after free bug in kernel->userspace relay file support

PATCH In review Jesper Juhl 3 participants 4 messages Jul 18, 2007 – Jul 20, 2007 Relay

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix a use after free bug in kernel->userspace relay file support

Hi,

Coverity spotted what looks like a real possible case of using a variable after it has been freed.
The problem is in kernel/relay.c::relay_open_buf()

If the code hits "goto free_buf;" it ends up in this code :

free_buf:

    	relay_destroy_buf(buf);	<--- calls kfree() on 'buf'.

free_name:
kfree(tmpname);
end:
return buf; <-- use after free of 'buf'.

I read through the callers and they all handle a NULL return from this function as an error (and hitting the 'free_buf' label only happens on failure to chan->cb->create_buf_file(), so that looks like a clear error to me).

The patch simply sets 'buf' to NULL after the call to relay_destroy_buf(buf); - as far as I can see that should take care of the problem.

The patch also corrects a reference to a documentation file while I was at it.

Warning: I don't know this code at all and I've only read through it quickly to try and work out what to do here, so I'd really apreciate it if someone who actually knows how this code should behave could ACK or NACK the patch before it gets merged anywhere since I did this pretty blind.

Patch has been compile tested only.

---

kernel/relay.c +2 -1
index a615a8f..c55e399 100644
@@ -1,7 +1,7 @@
 /*
  * Public API and common code for kernel->userspace relay file support.
  *
- * See Documentation/filesystems/relayfs.txt for an overview of relayfs.
+ * See Documentation/filesystems/relay.txt for an overview.
  *
  * Copyright (C) 2002-2005 - Tom Zanussi ([email protected]), IBM Corp
  * Copyright (C) 1999-2005 - Karim Yaghmour ([email protected])
@@ -427,6 +427,7 @@ static struct rchan_buf *relay_open_buf(struct rchan *chan, unsigned int cpu)
 
 free_buf:
  	relay_destroy_buf(buf);
+ 	buf = NULL;
 free_name:
  	kfree(tmpname);
 end:
#1 lore raw

ACK
Thanks for catching this. Your patch looks fine. I tested for regression, no problems. I also tested the error path and had the expected results. Thanks
Dave

Quoted 58 lines · Jesper Juhl

Hi,

Coverity spotted what looks like a real possible case of using a variable after it has been freed. The problem is in kernel/relay.c::relay_open_buf()

If the code hits "goto free_buf;" it ends up in this code :

free_buf:

    	relay_destroy_buf(buf);	<--- calls kfree() on 'buf'.
  free_name:
   	kfree(tmpname);

end:
return buf; <-- use after free of 'buf'.

I read through the callers and they all handle a NULL return from this function as an error (and hitting the 'free_buf' label only happens on failure to chan->cb->create_buf_file(), so that looks like a clear error to me).

The patch simply sets 'buf' to NULL after the call to relay_destroy_buf(buf); - as far as I can see that should take care of the problem.

The patch also corrects a reference to a documentation file while I was at it.

Warning: I don't know this code at all and I've only read through it quickly to try and work out what to do here, so I'd really apreciate it if someone who actually knows how this code should behave could ACK or NACK the patch before it gets merged anywhere since I did this pretty blind.

Patch has been compile tested only.

---

kernel/relay.c +2 -2
index a615a8f..c55e399 100644
@@ -1,7 +1,7 @@
 /*
  * Public API and common code for kernel->userspace relay file support.
  *
- * See Documentation/filesystems/relayfs.txt for an overview of relayfs.
+ * See Documentation/filesystems/relay.txt for an overview.
  *
  * Copyright (C) 2002-2005 - Tom Zanussi ([email protected]), IBM Corp
  * Copyright (C) 1999-2005 - Karim Yaghmour ([email protected])
@@ -427,6 +427,7 @@ static struct rchan_buf *relay_open_buf(struct rchan *chan, unsigned int cpu)
 
 free_buf:
  	relay_destroy_buf(buf);
+ 	buf = NULL;
 free_name:
  	kfree(tmpname);
 end:
 
 
-

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#2 lore raw
Quoted 4 lines · David J. Wilder

ACK
Thanks for catching this. Your patch looks fine. I tested for regression, no problems. I also tested the error path and had the expected results.

Ok, thank you for testing. I see that Mathieu Desnoyers also ack'ed the patch (thank you Mathieu). Will you guys take care of getting it merged upstream or should I resubmit it myself to Linus or Andrew with your ACKs?

Quoted 10 lines

Thanks
Dave

Quoted 6 lines · Jesper Juhl

Hi,

Coverity spotted what looks like a real possible case of using a variable after it has been freed.
The problem is in kernel/relay.c::relay_open_buf()

[snip]

Signature · 3 lines
Jesper Juhl <[email protected]>
Don't top-post  http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please      http://www.expita.com/nomime.html
Acked-by on v1 1/1
#3 lore raw
In reply to Jesper Juhl
Quoted 33 lines · * Jesper Juhl ([email protected]) wrote:

Hi,

Coverity spotted what looks like a real possible case of using a variable after it has been freed.
The problem is in kernel/relay.c::relay_open_buf()

If the code hits "goto free_buf;" it ends up in this code :

free_buf:

    	relay_destroy_buf(buf);	<--- calls kfree() on 'buf'.
  free_name:
   	kfree(tmpname);

end:
return buf; <-- use after free of 'buf'.

I read through the callers and they all handle a NULL return from this function as an error (and hitting the 'free_buf' label only happens on failure to chan->cb->create_buf_file(), so that looks like a clear error to me).

The patch simply sets 'buf' to NULL after the call to relay_destroy_buf(buf); - as far as I can see that should take care of the problem.

The patch also corrects a reference to a documentation file while I was at it.

Warning: I don't know this code at all and I've only read through it quickly to try and work out what to do here, so I'd really apreciate it if someone who actually knows how this code should behave could ACK or NACK the patch before it gets merged anywhere since I did this pretty blind.

Quoted 4 lines

Patch has been compile tested only.

Yes, the fix seems good. Thanks.

Quoted 25 lines

---

kernel/relay.c +2 -1
index a615a8f..c55e399 100644
@@ -1,7 +1,7 @@
 /*
  * Public API and common code for kernel->userspace relay file support.
  *
- * See Documentation/filesystems/relayfs.txt for an overview of relayfs.
+ * See Documentation/filesystems/relay.txt for an overview.
  *
  * Copyright (C) 2002-2005 - Tom Zanussi ([email protected]), IBM Corp
  * Copyright (C) 1999-2005 - Karim Yaghmour ([email protected])
@@ -427,6 +427,7 @@ static struct rchan_buf *relay_open_buf(struct rchan *chan, unsigned int cpu)
 
 free_buf:
  	relay_destroy_buf(buf);
+ 	buf = NULL;
 free_name:
  	kfree(tmpname);
 end:
Signature · 3 lines
Mathieu Desnoyers
Computer Engineering Ph.D. Student, Ecole Polytechnique de Montreal
OpenPGP key fingerprint: 8CD5 52C3 8E3C 4140 715F  BA06 3F25 A8FE 3BAE 9A68