use after free in serport

4 messages, 4 authors, 2010-10-31 · open the first message on its own page

use after free in serport

From: Sebastian Andrzej Siewior <hidden>
Date: 2010-10-30 11:16:23

There is a userland tool called inputattach which binds a serial port to
a specific device lets say a touch screen. serport
(CONFIG_SERIO_SERPORT) is the couterpart in the kernel. So the userland
does the following:

- 1. open("/dev/ttySx", O_RDWR | O_NOCTTY | O_NONBLOCK);
- 2. setline()
- 3. ioctl(fd, TIOCSETD, &ldisc); with ldisc = N_MOUSE
  if the ldisc class is not set (yet), it calls serport_ldisc_open()
  which allocates a little bit of memory.
- 4. ioctl(fd, SPIOCSTYPE, &devt) with devt beeing the device the user
  wants to attach
- 5. read(fd, NULL, 0);
  this attaches the device, calls its ->connect function and the program
  stays here for as long as the driver has to remain attached and
  working.
- 6. ioctl(fd, TIOCSETD, &ldisc); with ldisc = 0.
  this calls serport_ldisc_close() and the extra memory is gone.
- 7. close()

Now the program remains in step 5. If the user starts it again then it
behaves a little different:
- step 3 does nothing because the correct ldisc is allready set.
  tty_set_ldisc() discovers that (tty->ldisc->ops->num == ldisc) is true
  (Check the no-op case) so nothing happens.
- in step 5 serport_ldisc_read() discovers that it allready performing a
  read() and returns with -EBUSY.
- step 6 deallocates memory which was allocated by the other process.

Now we have the struct serport freed but the first process is still
using it.

Any idea how to fix it? I've seen that it is possible to manually bind
devices via /sys so maybe we could remove serport. However I'm not sure
if this kind of bug also affects other ldisc classes.

Sebastian

Re: use after free in serport

From: Jiri Slaby <hidden>
Date: 2010-10-31 09:16:33

On 10/30/2010 01:16 PM, Sebastian Andrzej Siewior wrote:
Now we have the struct serport freed but the first process is still
using it.

Any idea how to fix it?
I think so:
http://lkml.org/lkml/2010/10/21/223

(the updated patch attached)

regards,
-- 
js
suse labs

Re: use after free in serport

From: Sebastian Andrzej Siewior <hidden>
Date: 2010-10-31 10:41:41

* Jiri Slaby | 2010-10-31 10:16:22 [+0100]:
I think so:
http://lkml.org/lkml/2010/10/21/223

(the updated patch attached)
Thank you very much the patch, it fixes my problem. The commit that is
causing the bug (65b770468e98, ("tty-ldisc: turn ldisc user count into a
proper refcount")) was merged v2.6.31-rc6~66^2~1. Any chance that you
push it stable once it hits mainline?
regards,
Sebastian

Re: use after free in serport

From: Greg KH <gregkh@suse.de>
Date: 2010-10-31 14:23:10

On Sun, Oct 31, 2010 at 11:41:36AM +0100, Sebastian Andrzej Siewior wrote:
* Jiri Slaby | 2010-10-31 10:16:22 [+0100]:
quoted
I think so:
http://lkml.org/lkml/2010/10/21/223

(the updated patch attached)
Thank you very much the patch, it fixes my problem. The commit that is
causing the bug (65b770468e98, ("tty-ldisc: turn ldisc user count into a
proper refcount")) was merged v2.6.31-rc6~66^2~1. Any chance that you
push it stable once it hits mainline?
Yes, I will do so.

thanks,

greg k-h
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help