Skip to contentSkip to conversation

qapi: fix null pointer dereference on invalid parameter

PATCH In review Peter Lieven 4 participants 6 messages May 7, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qapi: fix null pointer dereference on invalid parameter

qemu segfaults if it receives an invalid parameter via a qmp command instead of throwing an error.

For example:
{ "execute": "blockdev-add",

    "arguments": { "options" : { "driver": "invalid-driver" } } }
---
 qapi/qapi-dealloc-visitor.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
qapi/qapi-dealloc-visitor.c +3 -1
index d0ea118..dc53545 100644
@@ -131,7 +131,9 @@ static void qapi_dealloc_end_list(Visitor *v, Error **errp)
 static void qapi_dealloc_type_str(Visitor *v, char **obj, const char *name,
                                   Error **errp)
 {
-    g_free(*obj);
+    if (obj) {
+        g_free(*obj);
+    }
 }
 
 static void qapi_dealloc_type_int(Visitor *v, int64_t *obj, const char *name,
Signature · 1 line
1.7.9.5
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Peter Lieven

qemu segfaults if it receives an invalid parameter via a qmp command instead of throwing an error.

For example:
{ "execute": "blockdev-add",

Quoted 7 lines
    "arguments": { "options" : { "driver": "invalid-driver" } } }
---
 qapi/qapi-dealloc-visitor.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Does this overlap with any of Markus' work? It emphasizes how bad it is that our visitor interface callbacks are undocumented on what they can be passed and are expected to return.

https://lists.gnu.org/archive/html/qemu-devel/2014-05/msg00225.html

Quoted 6 lines
qapi/qapi-dealloc-visitor.c +0 -0
index d0ea118..dc53545 100644

qapi/qapi-dealloc-visitor.c static void qapi_dealloc_end_list(Visitor *v, Error **errp) · line 137

Quoted 8 lines
qapi/qapi-dealloc-visitor.c +3 -1
@@ -131,7 +131,9 @@ static void qapi_dealloc_end_list(Visitor *v, Error **errp)
 static void qapi_dealloc_type_str(Visitor *v, char **obj, const char *name,
                                   Error **errp)
 {
-    g_free(*obj);
+    if (obj) {
+        g_free(*obj);
+    }
 }

As for solving a crash,

But if Markus' cleanups solve the problem by guaranteeing that the cleanup visitor is never passed a NULL obj, then this would be a dead check. I'm not familiar enough with the rest of the visitor code to know whether the caller is at fault, or whether other callers or visitor callbacks have the same bug.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#2 lore raw
Quoted 33 lines · Eric Blake
Quoted 5 lines · Peter Lieven

qemu segfaults if it receives an invalid parameter via a qmp command instead of throwing an error.

For example:
{ "execute": "blockdev-add",

Quoted 7 lines
     "arguments": { "options" : { "driver": "invalid-driver" } } }
---
  qapi/qapi-dealloc-visitor.c |    4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)

Does this overlap with any of Markus' work? It emphasizes how bad it is that our visitor interface callbacks are undocumented on what they can be passed and are expected to return.

https://lists.gnu.org/archive/html/qemu-devel/2014-05/msg00225.html

qapi/qapi-dealloc-visitor.c static void qapi_dealloc_end_list(Visitor *v, Error **errp) · line 137

Quoted 13 lines
qapi/qapi-dealloc-visitor.c +3 -1
index d0ea118..dc53545 100644
@@ -131,7 +131,9 @@ static void qapi_dealloc_end_list(Visitor *v, Error **errp)
  static void qapi_dealloc_type_str(Visitor *v, char **obj, const char *name,
                                    Error **errp)
  {
-    g_free(*obj);
+    if (obj) {
+        g_free(*obj);
+    }
  }

As for solving a crash,

Quoted 8 lines

But if Markus' cleanups solve the problem by guaranteeing that the cleanup visitor is never passed a NULL obj, then this would be a dead check. I'm not familiar enough with the rest of the visitor code to know whether the caller is at fault, or whether other callers or visitor callbacks have the same bug.

Markus, can you advise please.

Peter

Reviewed-by Eric Blake Reviewed-by
#3 lore raw
Quoted 44 lines · Peter Lieven
Quoted 33 lines · Eric Blake
Quoted 5 lines · Peter Lieven

qemu segfaults if it receives an invalid parameter via a qmp command instead of throwing an error.

For example:
{ "execute": "blockdev-add",

Quoted 7 lines
     "arguments": { "options" : { "driver": "invalid-driver" } } }
---
  qapi/qapi-dealloc-visitor.c |    4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)

Does this overlap with any of Markus' work? It emphasizes how bad it is that our visitor interface callbacks are undocumented on what they can be passed and are expected to return.

https://lists.gnu.org/archive/html/qemu-devel/2014-05/msg00225.html

qapi/qapi-dealloc-visitor.c static void qapi_dealloc_end_list(Visitor *v, Error **errp) · line 137

Quoted 13 lines
qapi/qapi-dealloc-visitor.c +3 -1
index d0ea118..dc53545 100644
@@ -131,7 +131,9 @@ static void qapi_dealloc_end_list(Visitor *v, Error **errp)
  static void qapi_dealloc_type_str(Visitor *v, char **obj, const char *name,
                                    Error **errp)
  {
-    g_free(*obj);
+    if (obj) {
+        g_free(*obj);
+    }
  }

As for solving a crash,

Quoted 8 lines

But if Markus' cleanups solve the problem by guaranteeing that the cleanup visitor is never passed a NULL obj, then this would be a dead check. I'm not familiar enough with the rest of the visitor code to know whether the caller is at fault, or whether other callers or visitor callbacks have the same bug.

Quoted 1 line

Markus, can you advise please.

My series doesn't address this problem, and I can in fact reproduce the crash with it applied.

Your fix effectively reverts my commit 25a7017. Let's turn it into a proper revert:

Revert "qapi: Clean up superfluous null check in qapi_dealloc_type_str()"

This reverts commit 25a7017555f1b4aeb543b5d323ff4afb8f9c5437.

Turns out the argument *can* be null: QEMU now segfaults if it receives an invalid parameter via a qmp command instead of throwing an error.

For example:
{ "execute": "blockdev-add",
"arguments": { "options" : { "driver": "invalid-driver" } } }

The deallocation visitor is more special than I (naively) thought...

#4 lore raw
Quoted 46 lines · On Wed, 07 May 2014 18:55:26 +0200 Markus Armbruster <[email protected]> wrote:
Quoted 44 lines · Peter Lieven
Quoted 41 lines · Eric Blake
Quoted 5 lines · Peter Lieven

qemu segfaults if it receives an invalid parameter via a qmp command instead of throwing an error.

For example:
{ "execute": "blockdev-add",

Quoted 7 lines
     "arguments": { "options" : { "driver": "invalid-driver" } } }
---
  qapi/qapi-dealloc-visitor.c |    4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)

Does this overlap with any of Markus' work? It emphasizes how bad it is that our visitor interface callbacks are undocumented on what they can be passed and are expected to return.

https://lists.gnu.org/archive/html/qemu-devel/2014-05/msg00225.html

qapi/qapi-dealloc-visitor.c static void qapi_dealloc_end_list(Visitor *v, Error **errp) · line 137

Quoted 13 lines
qapi/qapi-dealloc-visitor.c +3 -1
index d0ea118..dc53545 100644
@@ -131,7 +131,9 @@ static void qapi_dealloc_end_list(Visitor *v, Error **errp)
  static void qapi_dealloc_type_str(Visitor *v, char **obj, const char *name,
                                    Error **errp)
  {
-    g_free(*obj);
+    if (obj) {
+        g_free(*obj);
+    }
  }

As for solving a crash,

But if Markus' cleanups solve the problem by guaranteeing that the cleanup visitor is never passed a NULL obj, then this would be a dead check. I'm not familiar enough with the rest of the visitor code to know whether the caller is at fault, or whether other callers or visitor callbacks have the same bug.

Quoted 7 lines
Quoted 1 line

Markus, can you advise please.

My series doesn't address this problem, and I can in fact reproduce the crash with it applied.

Your fix effectively reverts my commit 25a7017. Let's turn it into a proper revert:

Which means that Peter has to repost as a real revert, right?

Peter, I'd appreciate if you do that shortly. I'd like to include that fix in my next pull request.

Thanks!

#5 lore raw
Quoted 51 lines · Luiz Capitulino
Quoted 48 lines · On Wed, 07 May 2014 18:55:26 +0200 Markus Armbruster <[email protected]> wrote:
Quoted 45 lines · Peter Lieven
Quoted 33 lines · Eric Blake
Quoted 5 lines · Peter Lieven

qemu segfaults if it receives an invalid parameter via a qmp command instead of throwing an error.

For example:
{ "execute": "blockdev-add",

Quoted 7 lines
     "arguments": { "options" : { "driver": "invalid-driver" } } }
---
  qapi/qapi-dealloc-visitor.c |    4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)

Does this overlap with any of Markus' work? It emphasizes how bad it is that our visitor interface callbacks are undocumented on what they can be passed and are expected to return.

https://lists.gnu.org/archive/html/qemu-devel/2014-05/msg00225.html

qapi/qapi-dealloc-visitor.c static void qapi_dealloc_end_list(Visitor *v, Error **errp) · line 137

Quoted 13 lines
qapi/qapi-dealloc-visitor.c +3 -1
index d0ea118..dc53545 100644
@@ -131,7 +131,9 @@ static void qapi_dealloc_end_list(Visitor *v, Error **errp)
  static void qapi_dealloc_type_str(Visitor *v, char **obj, const char *name,
                                    Error **errp)
  {
-    g_free(*obj);
+    if (obj) {
+        g_free(*obj);
+    }
  }

As for solving a crash,

Quoted 8 lines

But if Markus' cleanups solve the problem by guaranteeing that the cleanup visitor is never passed a NULL obj, then this would be a dead check. I'm not familiar enough with the rest of the visitor code to know whether the caller is at fault, or whether other callers or visitor callbacks have the same bug.

Markus, can you advise please.

Quoted 7 lines
Quoted 5 lines

My series doesn't address this problem, and I can in fact reproduce the crash with it applied.

Your fix effectively reverts my commit 25a7017. Let's turn it into a proper revert:

Which means that Peter has to repost as a real revert, right?

You could also replace Peter's commit message by mine. I ran git-revert, double-checked the patch is the same, worked Peter's message into the commit message, including his S-o-b, and topped it off with R-bys.

Quoted 2 lines

Peter, I'd appreciate if you do that shortly. I'd like to include that fix in my next pull request.

The result should be the same.