Skip to contentSkip to conversation

filter-mirror: segfault when specifying non existent device

PATCH Applied Eduardo Otubo 3 participants 5 messages Aug 21, 2017 – Sep 29, 2017 Networking

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 filter-mirror: segfault when specifying non existent device

When using filter-mirror like the example below where the interface 'ndev0' does not exist on the host, QEMU crashes into segmentation fault.

 $ qemu-system-x86_64 -S -machine pc -netdev user,id=ndev0 -object filter-mirror,id=test-object,netdev=ndev0

This happens because the function filter_mirror_setup() does not checks if the device actually exists and still keep on processing calling qemu_chr_find(). This patch fixes this issue.

---
 net/filter-mirror.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
net/filter-mirror.c +11 -3
index 90e2c92337..e18a4b16a0 100644
@@ -213,14 +213,22 @@ static void filter_mirror_setup(NetFilterState *nf, Error **errp)
     MirrorState *s = FILTER_MIRROR(nf);
     Chardev *chr;
 
+    if (s->outdev == NULL) {
+        goto err;
+    }
+
     chr = qemu_chr_find(s->outdev);
+
     if (chr == NULL) {
-        error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
-                  "Device '%s' not found", s->outdev);
-        return;
+        goto err;
     }
 
     qemu_chr_fe_init(&s->chr_out, chr, errp);
+
+err:
+    error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, "Device '%s' not found",
+              nf->netdev_id);
+    return;
 }
 
 static void redirector_rs_finalize(SocketReadState *rs)
Signature · 1 line
2.13.5
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Eduardo Otubo

When using filter-mirror like the example below where the interface 'ndev0' does not exist on the host, QEMU crashes into segmentation fault.

  $ qemu-system-x86_64 -S -machine pc -netdev user,id=ndev0 -object filter-mirror,id=test-object,netdev=ndev0

This happens because the function filter_mirror_setup() does not checks if the device actually exists and still keep on processing calling qemu_chr_find(). This patch fixes this issue.

Looks good for me.

Thanks
Zhang Chen

Quoted 34 lines
---
  net/filter-mirror.c | 14 +++++++++++---
  1 file changed, 11 insertions(+), 3 deletions(-)
net/filter-mirror.c +11 -3
index 90e2c92337..e18a4b16a0 100644
@@ -213,14 +213,22 @@ static void filter_mirror_setup(NetFilterState *nf, Error **errp)
      MirrorState *s = FILTER_MIRROR(nf);
      Chardev *chr;
 
+    if (s->outdev == NULL) {
+        goto err;
+    }
+
      chr = qemu_chr_find(s->outdev);
+
      if (chr == NULL) {
-        error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
-                  "Device '%s' not found", s->outdev);
-        return;
+        goto err;
      }
 
      qemu_chr_fe_init(&s->chr_out, chr, errp);
+
+err:
+    error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, "Device '%s' not found",
+              nf->netdev_id);
+    return;
  }
 
  static void redirector_rs_finalize(SocketReadState *rs)
Signature · 2 lines
Thanks
Zhang Chen
#2 lore raw
Quoted 13 lines · Zhang Chen
Quoted 10 lines · Eduardo Otubo

When using filter-mirror like the example below where the interface 'ndev0' does not exist on the host, QEMU crashes into segmentation fault.

  $ qemu-system-x86_64 -S -machine pc -netdev user,id=ndev0 -object filter-mirror,id=test-object,netdev=ndev0

This happens because the function filter_mirror_setup() does not checks if the device actually exists and still keep on processing calling qemu_chr_find(). This patch fixes this issue.

Quoted 5 lines
Quoted 1 line

Looks good for me.

Ping.

Quoted 43 lines

Thanks
Zhang Chen

Quoted 31 lines
---
  net/filter-mirror.c | 14 +++++++++++---
  1 file changed, 11 insertions(+), 3 deletions(-)
net/filter-mirror.c +11 -3
index 90e2c92337..e18a4b16a0 100644
@@ -213,14 +213,22 @@ static void filter_mirror_setup(NetFilterState *nf, Error **errp)
      MirrorState *s = FILTER_MIRROR(nf);
      Chardev *chr;
+    if (s->outdev == NULL) {
+        goto err;
+    }
+
      chr = qemu_chr_find(s->outdev);
+
      if (chr == NULL) {
-        error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
-                  "Device '%s' not found", s->outdev);
-        return;
+        goto err;
      }
      qemu_chr_fe_init(&s->chr_out, chr, errp);
+
+err:
+    error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, "Device '%s' not found",
+              nf->netdev_id);
+    return;
  }
  static void redirector_rs_finalize(SocketReadState *rs)
Signature · 2 lines
Thanks
Zhang Chen
Signature · 2 lines
Eduardo Otubo
Senior Software Engineer @ RedHat
#3 lore raw
In reply to Zhang Chen
Quoted 13 lines · Zhang Chen
Quoted 10 lines · Eduardo Otubo

When using filter-mirror like the example below where the interface 'ndev0' does not exist on the host, QEMU crashes into segmentation fault.

  $ qemu-system-x86_64 -S -machine pc -netdev user,id=ndev0 -object filter-mirror,id=test-object,netdev=ndev0

This happens because the function filter_mirror_setup() does not checks if the device actually exists and still keep on processing calling qemu_chr_find(). This patch fixes this issue.

Quoted 9 lines
Quoted 1 line

Looks good for me.

Thanks
Zhang Chen

So Peter and Michael pointed that this patch didn't pass on make check causing this:

qemu-system-x86_64: -object filter-mirror,id=qtest-f0,netdev=qtest-bn0,queue=tx,outdev=mirror0: Device 'qtest-bn0' not found
Broken pipe
GTester: last random seed: R02S4f1b7fb2da540e3e36e962f19f19ac65

(tests/test-filter-mirror:6059): GLib-CRITICAL **: g_hook_destroy_link:
assertion 'hook != NULL' failed
make: *** [/home/otubo/develop/qemu/otubo/tests/Makefile.include:847:
check-qtest-x86_64] Error 1
otubo@vader ~/develop/qemu/otubo netdev_segfault $ git show

Quoted 16 lines
Quoted 16 lines
---
  net/filter-mirror.c | 14 +++++++++++---
  1 file changed, 11 insertions(+), 3 deletions(-)
net/filter-mirror.c +4 -0
index 90e2c92337..e18a4b16a0 100644
@@ -213,14 +213,22 @@ static void filter_mirror_setup(NetFilterState *nf, Error **errp)
      MirrorState *s = FILTER_MIRROR(nf);
      Chardev *chr;
+    if (s->outdev == NULL) {
+        goto err;
+    }
+
      chr = qemu_chr_find(s->outdev);
Quoted 8 lines

net/filter-mirror.c static void filter_mirror_setup(NetFilterState *nf, Error **errp) · line 226

Quoted 8 lines
net/filter-mirror.c +2 -3
+
      if (chr == NULL) {
-        error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
-                  "Device '%s' not found", s->outdev);
-        return;
+        goto err;
      }
      qemu_chr_fe_init(&s->chr_out, chr, errp);

And the reason was there was no return after qemu_chr_fe_init, making it fatally go to the "err:" label.

Quoted 4 lines

net/filter-mirror.c static void filter_mirror_setup(NetFilterState *nf, Error **errp) · line 230

Quoted 4 lines
net/filter-mirror.c +4 -0
+
+err:
+    error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, "Device '%s' not found",
+              nf->netdev_id);

Also, "nf->netdev_id" wasn't the device not found, but "s->outdev"; which makes sense to have two error messages here one for when Null and one for when it's not found, otherwise we'd fall into not very clear error messages like:

qemu-system-x86_64: -object filter-mirror,id=test-object,netdev=ndev0: Device '(null)' not found

I'm fixing all this and sending a v2 shortly.
Thanks for the review and tests.

Signature · 2 lines
Eduardo Otubo
Senior Software Engineer @ RedHat
#4 lore raw
In reply to Eduardo Otubo
Quoted 3 lines · Eduardo Otubo

When using filter-mirror like the example below where the interface 'ndev0' does not exist on the host, QEMU crashes into segmentation fault.

Applied to -trivial, thanks!

/mjt