Skip to contentSkip to conversation

fix segfaults triggered by failed vnc handshakes

PATCH In review Tim Hardeck 4 participants 10 messages Oct 14, 2012 – Oct 18, 2012

lore
Share & tools
lore
#1 lore raw

PATCH 1/2 vnc: fix segfault due to failed handshake

In reply to Tim Hardeck

When the VNC server disconnects due to a failed handshake we don't have vs->bh allocated yet.

Check for this case and don't delete it.

---
 ui/vnc.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
ui/vnc.c +3 -1
index 01b2daf..656895a 100644
@@ -1055,7 +1055,9 @@ static void vnc_disconnect_finish(VncState *vs)
     vnc_unlock_output(vs);
 
     qemu_mutex_destroy(&vs->output_mutex);
-    qemu_bh_delete(vs->bh);
+    if (vs->bh != NULL) {
+        qemu_bh_delete(vs->bh);
+    }
     buffer_free(&vs->jobs_buffer);
 
     for (i = 0; i < VNC_STAT_ROWS; ++i) {
Signature · 1 line
1.7.10.4
#3 lore raw

PATCH 2/2 qemu queue: fix uninitialized removals

In reply to Tim Hardeck

When calling QTAILQ_REMOVE or QLIST_REMOVE on an unitialized list QEMU segfaults.

Check for this case specifically on item removal.

---
 qemu-queue.h |    8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
qemu-queue.h +6 -2
index 9288cd8..47ed239 100644
@@ -141,7 +141,9 @@ struct {                                                                \
         if ((elm)->field.le_next != NULL)                               \
                 (elm)->field.le_next->field.le_prev =                   \
                     (elm)->field.le_prev;                               \
-        *(elm)->field.le_prev = (elm)->field.le_next;                   \
+        if ((elm)->field.le_prev != NULL) {                             \
+            *(elm)->field.le_prev = (elm)->field.le_next;               \
+        }                                                               \
 } while (/*CONSTCOND*/0)
 
 #define QLIST_FOREACH(var, head, field)                                 \
@@ -381,7 +383,9 @@ struct {                                                                \
                     (elm)->field.tqe_prev;                              \
         else                                                            \
                 (head)->tqh_last = (elm)->field.tqe_prev;               \
-        *(elm)->field.tqe_prev = (elm)->field.tqe_next;                 \
+        if ((elm)->field.tqe_prev != NULL) {                            \
+            *(elm)->field.tqe_prev = (elm)->field.tqe_next;             \
+        }                                                               \
 } while (/*CONSTCOND*/0)
 
 #define QTAILQ_FOREACH(var, head, field)                                \
Signature · 1 line
1.7.10.4
#0 lore raw

cover fix segfaults triggered by failed vnc handshakes

While trying to adapt Anthony Liguori's websockets patches to the current standard I ran into some segfaults.

They were triggered during disconnects due to failed VNC handshakes.

I have added checks to prevent them.

Tim Hardeck (2):
vnc: fix segfault due to failed handshake
qemu queue: fix uninitialized removals

 qemu-queue.h |    8 ++++++--
 ui/vnc.c     |    4 +++-
 2 files changed, 9 insertions(+), 3 deletions(-)
Signature · 1 line
1.7.10.4
Acked-by on v1 1/2
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] vnc: fix segfault due to failed handshake

In reply to Tim Hardeck

Am 14.10.2012 15:08, schrieb Tim Hardeck:

Quoted 15 lines

When the VNC server disconnects due to a failed handshake we don't have vs->bh allocated yet.

Check for this case and don't delete it.

---
 ui/vnc.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
ui/vnc.c +0 -0
index 01b2daf..656895a 100644

ui/vnc.c static void vnc_disconnect_finish(VncState *vs) · line 1063

Quoted 10 lines
ui/vnc.c +3 -1
@@ -1055,7 +1055,9 @@ static void vnc_disconnect_finish(VncState *vs)
     vnc_unlock_output(vs);
 
     qemu_mutex_destroy(&vs->output_mutex);
-    qemu_bh_delete(vs->bh);
+    if (vs->bh != NULL) {
+        qemu_bh_delete(vs->bh);
+    }
     buffer_free(&vs->jobs_buffer);
 
     for (i = 0; i < VNC_STAT_ROWS; ++i) {

qemu_bh_delete() is not checking for a NULL argument, therefore this fix looks good to me,

Adding some CCs.

As a followup it might be a good idea to either assert or ignore a NULL argument in qemu_bh_delete().

Regards,
Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#4 lore raw

Re: [Qemu-devel] [PATCH 2/2] qemu queue: fix uninitialized removals

In reply to Tim Hardeck

Tim,

Am 14.10.2012 15:08, schrieb Tim Hardeck:

Quoted 2 lines

When calling QTAILQ_REMOVE or QLIST_REMOVE on an unitialized list QEMU segfaults.

Can this be reproduced by a user today? Or is this just fixing the case that a developer forgot to initialize a list?

Regards,
Andreas

Quoted 33 lines

Check for this case specifically on item removal.

---
 qemu-queue.h |    8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
qemu-queue.h +6 -2
index 9288cd8..47ed239 100644
@@ -141,7 +141,9 @@ struct {                                                                \
         if ((elm)->field.le_next != NULL)                               \
                 (elm)->field.le_next->field.le_prev =                   \
                     (elm)->field.le_prev;                               \
-        *(elm)->field.le_prev = (elm)->field.le_next;                   \
+        if ((elm)->field.le_prev != NULL) {                             \
+            *(elm)->field.le_prev = (elm)->field.le_next;               \
+        }                                                               \
 } while (/*CONSTCOND*/0)
 
 #define QLIST_FOREACH(var, head, field)                                 \
@@ -381,7 +383,9 @@ struct {                                                                \
                     (elm)->field.tqe_prev;                              \
         else                                                            \
                 (head)->tqh_last = (elm)->field.tqe_prev;               \
-        *(elm)->field.tqe_prev = (elm)->field.tqe_next;                 \
+        if ((elm)->field.tqe_prev != NULL) {                            \
+            *(elm)->field.tqe_prev = (elm)->field.tqe_next;             \
+        }                                                               \
 } while (/*CONSTCOND*/0)
 
 #define QTAILQ_FOREACH(var, head, field)                                \
Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#5 lore raw

Re: [Qemu-devel] [PATCH 2/2] qemu queue: fix uninitialized removals

Hi Andreas,

Quoted 8 lines · On Wednesday 17 October 2012 17:00:15 Andreas Färber wrote:

Tim,

Am 14.10.2012 15:08, schrieb Tim Hardeck:

Quoted 2 lines

When calling QTAILQ_REMOVE or QLIST_REMOVE on an unitialized list QEMU segfaults.

Can this be reproduced by a user today? Or is this just fixing the case that a developer forgot to initialize a list?

I am not sure but in this case it happened during an early VNC connection state failure which most likely wouldn't happen to regular users. I triggered it while working on the VNC connection part.

The issue could most likely be also fixed in the VNC connection initialization process but if this changes doesn't have a relevant performance impact they might prevent some other/future crashes.

Regards
Tim

Quoted 49 lines

Regards,
Andreas

Quoted 45 lines

Check for this case specifically on item removal.

---

 qemu-queue.h |    8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
qemu-queue.h +6 -2
index 9288cd8..47ed239 100644
@@ -141,7 +141,9 @@ struct {                                              
                 \> 
         if ((elm)->field.le_next != NULL)                               \
 
                 (elm)->field.le_next->field.le_prev =                   \
 
                     (elm)->field.le_prev;                               \
 
-        *(elm)->field.le_prev = (elm)->field.le_next;                   \
+        if ((elm)->field.le_prev != NULL) {                             \
+            *(elm)->field.le_prev = (elm)->field.le_next;               \
+        }                                                               \
 
 } while (/*CONSTCOND*/0)
 
 #define QLIST_FOREACH(var, head, field)                                 \
@@ -381,7 +383,9 @@ struct {                                              
                 \> 
                     (elm)->field.tqe_prev;                              \
 
         else                                                            \
 
                 (head)->tqh_last = (elm)->field.tqe_prev;               \
 
-        *(elm)->field.tqe_prev = (elm)->field.tqe_next;                 \
+        if ((elm)->field.tqe_prev != NULL) {                            \
+            *(elm)->field.tqe_prev = (elm)->field.tqe_next;             \
+        }                                                               \
 
 } while (/*CONSTCOND*/0)
 
 #define QTAILQ_FOREACH(var, head, field)                                \
Signature · 4 lines
SUSE LINUX Products GmbH, GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer, HRB 
16746 (AG Nürnberg)
Maxfeldstr. 5, 90409 Nürnberg, Germany
T: +49 (0) 911 74053-0  F: +49 (0) 911 74053-483 http://www.suse.de/
#6 lore raw

Re: [Qemu-devel] [PATCH 2/2] qemu queue: fix uninitialized removals

Am 17.10.2012 23:24, schrieb Tim Hardeck:

Quoted 11 lines

Hi Andreas,

Quoted 8 lines · On Wednesday 17 October 2012 17:00:15 Andreas Färber wrote:

Tim,

Am 14.10.2012 15:08, schrieb Tim Hardeck:

Quoted 2 lines

When calling QTAILQ_REMOVE or QLIST_REMOVE on an unitialized list QEMU segfaults.

Can this be reproduced by a user today? Or is this just fixing the case that a developer forgot to initialize a list?

Quoted 7 lines

I am not sure but in this case it happened during an early VNC connection state failure which most likely wouldn't happen to regular users. I triggered it while working on the VNC connection part.

The issue could most likely be also fixed in the VNC connection initialization process but if this changes doesn't have a relevant performance impact they might prevent some other/future crashes.

At the same time, it could be hiding real bugs, where ignoring the QLIST_REMOVE() isn't the right fix. I can see your point, but I would be careful with making interfaces less strict.

In any case, I don't think this qualifies for qemu-trivial, Andreas.

Kevin

#7 lore raw

Re: [Qemu-devel] [PATCH 2/2] qemu queue: fix uninitialized removals

Am 18.10.2012 12:43, schrieb Kevin Wolf:

Quoted 12 lines

Am 17.10.2012 23:24, schrieb Tim Hardeck:

Quoted 11 lines · On Wednesday 17 October 2012 17:00:15 Andreas Färber wrote:
Quoted 6 lines

Am 14.10.2012 15:08, schrieb Tim Hardeck:

Quoted 2 lines

When calling QTAILQ_REMOVE or QLIST_REMOVE on an unitialized list QEMU segfaults.

Can this be reproduced by a user today? Or is this just fixing the case that a developer forgot to initialize a list?

I am not sure but in this case it happened during an early VNC connection state failure which most likely wouldn't happen to regular users. I triggered it while working on the VNC connection part.

Quoted 7 lines
Quoted 3 lines

The issue could most likely be also fixed in the VNC connection initialization process but if this changes doesn't have a relevant performance impact they might prevent some other/future crashes.

At the same time, it could be hiding real bugs, where ignoring the QLIST_REMOVE() isn't the right fix. I can see your point, but I would be careful with making interfaces less strict.

What I don't get is, why is avoiding a NULL pointer dereference any better from accessing random memory through an uninitialized pointer? Or am I getting "uninitialized" wrong?

Quoted 1 line

In any case, I don't think this qualifies for qemu-trivial, Andreas.

Maybe not, but we don't have a clear maintainer that I'm aware of, and no one else reviewed it for several days before I did. ;)

Andreas

Signature · 2 lines
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 16746 AG Nürnberg
#8 lore raw

Re: [Qemu-devel] [PATCH 2/2] qemu queue: fix uninitialized removals

In reply to Kevin Wolf
Quoted 12 lines · Kevin Wolf

Am 17.10.2012 23:24, schrieb Tim Hardeck:

Quoted 11 lines · On Wednesday 17 October 2012 17:00:15 Andreas Färber wrote:
Quoted 6 lines

Am 14.10.2012 15:08, schrieb Tim Hardeck:

Quoted 2 lines

When calling QTAILQ_REMOVE or QLIST_REMOVE on an unitialized list QEMU segfaults.

Can this be reproduced by a user today? Or is this just fixing the case that a developer forgot to initialize a list?

I am not sure but in this case it happened during an early VNC connection state failure which most likely wouldn't happen to regular users. I triggered it while working on the VNC connection part.

Quoted 7 lines
Quoted 3 lines

The issue could most likely be also fixed in the VNC connection initialization process but if this changes doesn't have a relevant performance impact they might prevent some other/future crashes.

At the same time, it could be hiding real bugs, where ignoring the QLIST_REMOVE() isn't the right fix. I can see your point, but I would be careful with making interfaces less strict.

I agree this patch doesn't seem like the right fix. All lists should be initialised (either via the _INIT macro or statically using the _HEAD_INITIALIZER macros) before use. If we ever try to do one of the other operations on an uninitialised list that's a bug which needs to be tracked down and fixed.

-- PMM

#9 lore raw

Re: [Qemu-devel] [PATCH 2/2] qemu queue: fix uninitialized removals

In reply to Tim Hardeck
Quoted 4 lines · Tim Hardeck

When calling QTAILQ_REMOVE or QLIST_REMOVE on an unitialized list QEMU segfaults.

Check for this case specifically on item removal.

Incidentally, this commit message is inaccurate -- you can't call the _REMOVE macros on a list (uninitialised or otherwise) because they take the list item, not the list itself. The case you are trying to guard against here is attempting to remove an item which never got inserted into the list in the first place.

However this check doesn't catch all cases, because (a) there's no guarantee that the list element pointers get initialised to NULL and (b) removing an item from the list doesn't clear the pointers either, so this check still wouldn't catch "removed the item twice". Better just to accept that the semantics are "you can only use the _REMOVE macro on items that are actually in the list", I think.

-- PMM