Skip to contentSkip to conversation

chardev: fix pty_chr_timer

PATCH In review Gerd Hoffmann 2 participants 3 messages Aug 22, 2013 – Aug 30, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 chardev: fix pty_chr_timer

pty_chr_timer first calls pty_chr_update_read_handler(), then clears
timer_tag (because it is a one-shot timer). This is the wrong order
though. pty_chr_update_read_handler might re-arm time timer, and the new timer_tag gets overwitten in that case.

This leads to crashes when unplugging a pty chardev: pty_chr_close thinks no timer is running -> timer isn't canceled -> pty_chr_timer gets called with stale CharDevState -> BOOM.

This patch fixes the ordering.
Kill the pointless goto while being at it.

https://bugzilla.redhat.com/show_bug.cgi?id=994414

---
 qemu-char.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
qemu-char.c +4 -8
index 1be1cf6..1621fbd 100644
@@ -1026,15 +1026,11 @@ static gboolean pty_chr_timer(gpointer opaque)
     struct CharDriverState *chr = opaque;
     PtyCharDriver *s = chr->opaque;
 
-    if (s->connected) {
-        goto out;
-    }
-
-    /* Next poll ... */
-    pty_chr_update_read_handler(chr);
-
-out:
     s->timer_tag = 0;
+    if (!s->connected) {
+        /* Next poll ... */
+        pty_chr_update_read_handler(chr);
+    }
     return FALSE;
 }
 
Signature · 1 line
1.8.3.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 32 lines · Gerd Hoffmann

pty_chr_timer first calls pty_chr_update_read_handler(), then clears
timer_tag (because it is a one-shot timer). This is the wrong order
though. pty_chr_update_read_handler might re-arm time timer, and the new timer_tag gets overwitten in that case.

This leads to crashes when unplugging a pty chardev: pty_chr_close thinks no timer is running -> timer isn't canceled -> pty_chr_timer gets called with stale CharDevState -> BOOM.

This patch fixes the ordering.
Kill the pointless goto while being at it.

https://bugzilla.redhat.com/show_bug.cgi?id=994414

---
 qemu-char.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
qemu-char.c +0 -5
index 1be1cf6..1621fbd 100644
@@ -1026,15 +1026,11 @@ static gboolean pty_chr_timer(gpointer opaque)
     struct CharDriverState *chr = opaque;
     PtyCharDriver *s = chr->opaque;
 
-    if (s->connected) {
-        goto out;
-    }
-
-    /* Next poll ... */

qemu-char.c static gboolean pty_chr_timer(gpointer opaque) · line 1035

Quoted 10 lines
qemu-char.c +4 -3
-    pty_chr_update_read_handler(chr);
-
-out:
     s->timer_tag = 0;
+    if (!s->connected) {
+        /* Next poll ... */
+        pty_chr_update_read_handler(chr);
+    }
     return FALSE;
 }

pty_chr_timer()
s->timer_tag = 0; pty_chr_update_read_handler() // s->connected == 0

    pty_chr_state(..., 1)       // G_IO_HUP is clear
      not calling: g_source_remove(s->timer_tag)
      s->connected = 1;
      qemu_chr_be_generic_open()
      s->fd_tag = io_add_watch_poll()

So, in this order, s->timer_tag is not removed in pty_chr_state().

But that shouldn't be necessary anyway, since pty_chr_timer() returns FALSE, and its associated tag (s->timer_tag, see pty_chr_rearm_timer()) is removed anyway.

Seems OK to me.

#2 lore raw
In reply to Gerd Hoffmann
Quoted 11 lines · Gerd Hoffmann

pty_chr_timer first calls pty_chr_update_read_handler(), then clears
timer_tag (because it is a one-shot timer). This is the wrong order
though. pty_chr_update_read_handler might re-arm time timer, and the new timer_tag gets overwitten in that case.

This leads to crashes when unplugging a pty chardev: pty_chr_close thinks no timer is running -> timer isn't canceled -> pty_chr_timer gets called with stale CharDevState -> BOOM.

This patch fixes the ordering.
Kill the pointless goto while being at it.

Ping?

Quoted 32 lines

https://bugzilla.redhat.com/show_bug.cgi?id=994414

---
 qemu-char.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
qemu-char.c +4 -8
index 1be1cf6..1621fbd 100644
@@ -1026,15 +1026,11 @@ static gboolean pty_chr_timer(gpointer opaque)
     struct CharDriverState *chr = opaque;
     PtyCharDriver *s = chr->opaque;
 
-    if (s->connected) {
-        goto out;
-    }
-
-    /* Next poll ... */
-    pty_chr_update_read_handler(chr);
-
-out:
     s->timer_tag = 0;
+    if (!s->connected) {
+        /* Next poll ... */
+        pty_chr_update_read_handler(chr);
+    }
     return FALSE;
 }