Skip to contentSkip to conversation

xl network-attach SEGV in 4.2 and 4.1

PATCH RESEND In review Marek Marczykowski 5 participants 11 messages Apr 17, 2013 – Nov 25, 2013

lore
Share & tools
lore
#0 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi all,

4.2 and 4.1 suffers from SEGV during xl network-attach in libxl__device_nic_add. In 4.3-unstable it is fixed by:
5420f2650 libxl: Set vfb and vkb devid if not done so by the caller

So either the patch need to be backported to 4.1 and 4.2, or fixed by this one:
- ------
libxl: Fix SEGV in network-attach

When "device/vif" directory exists but is empty l!=NULL, but nb==0, so l[nb-1] is invalid. Add missing check.

tools/libxl/libxl.c +0 -0
index 5783cd2..9e06a7d 100644

- --- a/tools/libxl/libxl.c
+++ b/tools/libxl/libxl.c
@@ -2569,7 +2569,8 @@ void libxl__device_nic_add(libxl__egc *egc, uint32_t domid,

             goto out_free;
         }
         if (!(l = libxl__xs_directory(gc, XBT_NULL,

- - libxl__sprintf(gc, "%s/device/vif",
dompath), &nb))) {
+ libxl__sprintf(gc, "%s/device/vif",
dompath), &nb)) ||

+                nb == 0) {
             nic->devid = 0;
         } else {
             nic->devid = strtoul(l[nb - 1], NULL, 10) + 1;

- --
Best Regards / Pozdrawiam,
Marek Marczykowski
Invisible Things Lab
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.13 (GNU/Linux)
Comment: Using GnuPG with undefined - http://www.enigmail.net/

iQEcBAEBAgAGBQJRbg3PAAoJENuP0xzK19cs0qQH/2mR6lUwj75tKCckFJfM7HPJ 6YEqDvdunmCXr7Wcf9p+Z6RRsnjie9ihDwxbaP4b5XYMLW59YbvZEb3tyrIADYJy gGwE+8xn3Jx0NjiRCJF5O2nF3w9I5W9o5sUMbXporo/Tu4X0e3PJ6wb1JseZWsLL mVl3bjwIChos6eKsAKOuHIPqFCASsKIGJibrkWva9LqQ4k/xgjTshCgmbAgEITEo 8fAZbTRL/SM91qX5a+2ynUf/0h5Sb/IvLyLwm86n1wsqLoJ261VMgAOsXxYGUTBM Pr5Gvc+s5VdhA0iY4IYE8GuQ99MnaXO6T+JSenpfJ3L26LSMvHkImFLqI3ljhzE= =FWNX
-----END PGP SIGNATURE-----

#1 lore raw

Copying the stable tools maintainer. Please always do so for backport requests (see MAINTAINERS in the relevant tree to see who to copy).

Quoted 7 lines · Marek Marczykowski

Hi all,

4.2 and 4.1 suffers from SEGV during xl network-attach in libxl__device_nic_add. In 4.3-unstable it is fixed by:
5420f2650 libxl: Set vfb and vkb devid if not done so by the caller

So either the patch need to be backported to 4.1 and 4.2,

On the face of it I can't see any problem with backporting that commit

Quoted 31 lines

or fixed by this one:
------
libxl: Fix SEGV in network-attach

When "device/vif" directory exists but is empty l!=NULL, but nb==0, so l[nb-1] is invalid. Add missing check.

tools/libxl/libxl.c +1 -1
index 5783cd2..9e06a7d 100644
@@ -2569,7 +2569,8 @@ void libxl__device_nic_add(libxl__egc *egc, uint32_t
 domid,
             goto out_free;
         }
         if (!(l = libxl__xs_directory(gc, XBT_NULL,
-                                     libxl__sprintf(gc, "%s/device/vif",
 dompath), &nb))) {
+                                     libxl__sprintf(gc, "%s/device/vif",

dompath), &nb)) ||

+                nb == 0) {
             nic->devid = 0;
         } else {
             nic->devid = strtoul(l[nb - 1], NULL, 10) + 1;

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#2 lore raw

Ian Campbell writes ("Re: [Xen-devel] xl network-attach SEGV in 4.2 and 4.1"):

Quoted 4 lines · Marek Marczykowski
Quoted 3 lines

Hi all,
When "device/vif" directory exists but is empty l!=NULL, but nb==0, so l[nb-1] is invalid. Add missing check.

Thanks for pointing this out. The root cause is the expectation that libxl__xs_directory won't return a non-null list but set *nb to 0.

I found some occurrences of this bug in (xen-unstable) staging. See patch below, which should probably go into 4.1 and 4.2.

Quoted 16 lines
Quoted 14 lines
tools/libxl/libxl.c +1 -1
index 5783cd2..9e06a7d 100644
@@ -2569,7 +2569,8 @@ void libxl__device_nic_add(libxl__egc *egc, uint32_t
 domid,
             goto out_free;
         }
         if (!(l = libxl__xs_directory(gc, XBT_NULL,
-                                     libxl__sprintf(gc, "%s/device/vif",
 dompath), &nb))) {
+                                     libxl__sprintf(gc, "%s/device/vif",
Quoted 2 lines

dompath), &nb)) ||
+ nb == 0) {

I think this is the right way to fix this in the stable trees. I looked at the backport of "5420f2650 libxl: Set vfb and vkb devid if not done so by the caller" and it's nontrivial, so although it's a bugfix I'd rather leave it.

Ian.

commit d1f52920ee96c91665813fb875c0c76e5eb7a312

Author: Ian Jackson <[email protected]>
Date:   Thu Apr 18 16:27:46 2013 +0100

libxl: Avoid realloc(,0) when libxl__xs_directory returns empty list

If the named path is a leaf node, libxl__xs_directory can succeed, returning non-null, but set *nb to 0.

In three places in libxl this may result in a zero size argument being passed to malloc() or realloc(), which is not adviseable.

tools/libxl/libxl.c +3 -3
index 0f936c0..1c04d37 100644
@@ -1840,7 +1840,7 @@ libxl_device_vtpm *libxl_device_vtpm_list(libxl_ctx *ctx, uint32_t domid, int *n
 
     fe_path = libxl__sprintf(gc, "%s/device/vtpm", libxl__xs_get_dompath(gc, domid));
     dir = libxl__xs_directory(gc, XBT_NULL, fe_path, &ndirs);
-    if(dir) {
+    if (dir && ndirs) {
        vtpms = malloc(sizeof(*vtpms) * ndirs);
        libxl_device_vtpm* vtpm;
        libxl_device_vtpm* end = vtpms + ndirs;
@@ -2314,7 +2314,7 @@ static int libxl__append_disk_list_of_type(libxl__gc *gc,
     be_path = libxl__sprintf(gc, "%s/backend/%s/%d",
                              libxl__xs_get_dompath(gc, 0), type, domid);
     dir = libxl__xs_directory(gc, XBT_NULL, be_path, &n);
-    if (dir) {
+    if (dir && n) {
         libxl_device_disk *tmp;
         tmp = realloc(*disks, sizeof (libxl_device_disk) * (*ndisks + n));
         if (tmp == NULL)
@@ -3006,7 +3006,7 @@ static int libxl__append_nic_list_of_type(libxl__gc *gc,
     be_path = libxl__sprintf(gc, "%s/backend/%s/%d",
                              libxl__xs_get_dompath(gc, 0), type, domid);
     dir = libxl__xs_directory(gc, XBT_NULL, be_path, &n);
-    if (dir) {
+    if (dir && n) {
         libxl_device_nic *tmp;
         tmp = realloc(*nics, sizeof (libxl_device_nic) * (*nnics + n));
         if (tmp == NULL)
#3 lore raw

Re: xl network-attach SEGV in 4.2 and 4.1 [and 1 more messages]

Marek Marczykowski writes ("[Xen-devel] xl network-attach SEGV in 4.2 and 4.1"):

Quoted 7 lines

4.2 and 4.1 suffers from SEGV during xl network-attach in libxl__device_nic_add. In 4.3-unstable it is fixed by:
5420f2650 libxl: Set vfb and vkb devid if not done so by the caller

So either the patch need to be backported to 4.1 and 4.2, or fixed by this one:
- ------
libxl: Fix SEGV in network-attach

I have applied this to 4.2 and 4.1. I had to fix up a conflict in 4.1 and also I fixed a whitespace bug. The patches as committed are below.

Quoted 2 lines

When "device/vif" directory exists but is empty l!=NULL, but nb==0, so l[nb-1] is invalid. Add missing check.

...

Quoted 6 lines
         if (!(l = libxl__xs_directory(gc, XBT_NULL,

- - libxl__sprintf(gc, "%s/device/vif",
dompath), &nb))) {
+ libxl__sprintf(gc, "%s/device/vif",
dompath), &nb)) ||
+ nb == 0) {

Something in the arrangements you used to prepare this patch linewrapped it _before you signed it_.

Ian Jackson writes ("Re: [Xen-devel] xl network-attach SEGV in 4.2 and 4.1"):

Quoted 4 lines
Author: Ian Jackson <[email protected]>
Date:   Thu Apr 18 16:27:46 2013 +0100

libxl: Avoid realloc(,0) when libxl__xs_directory returns empty list

IWBNI someone would review this and ack it; I'd like it to go into 4.1, 4.2 and unstable.

Ian.

#6 lore raw
In reply to Ian Jackson
Quoted 43 lines · Ian Jackson

Ian Campbell writes ("Re: [Xen-devel] xl network-attach SEGV in 4.2 and 4.1"):

Quoted 4 lines · Marek Marczykowski
Quoted 3 lines

Hi all,
When "device/vif" directory exists but is empty l!=NULL, but nb==0, so l[nb-1] is invalid. Add missing check.

Thanks for pointing this out. The root cause is the expectation that libxl__xs_directory won't return a non-null list but set *nb to 0.

I found some occurrences of this bug in (xen-unstable) staging. See patch below, which should probably go into 4.1 and 4.2.

Quoted 16 lines
Quoted 14 lines
tools/libxl/libxl.c +1 -1
index 5783cd2..9e06a7d 100644
@@ -2569,7 +2569,8 @@ void libxl__device_nic_add(libxl__egc *egc, uint32_t
 domid,
             goto out_free;
         }
         if (!(l = libxl__xs_directory(gc, XBT_NULL,
-                                     libxl__sprintf(gc, "%s/device/vif",
 dompath), &nb))) {
+                                     libxl__sprintf(gc, "%s/device/vif",
Quoted 2 lines

dompath), &nb)) ||
+ nb == 0) {

I think this is the right way to fix this in the stable trees. I looked at the backport of "5420f2650 libxl: Set vfb and vkb devid if not done so by the caller" and it's nontrivial, so although it's a bugfix I'd rather leave it.

Ian.

commit d1f52920ee96c91665813fb875c0c76e5eb7a312

Author: Ian Jackson <[email protected]>
Date:   Thu Apr 18 16:27:46 2013 +0100

libxl: Avoid realloc(,0) when libxl__xs_directory returns empty list

Quoted 7 lines

If the named path is a leaf node, libxl__xs_directory can succeed, returning non-null, but set *nb to 0.

In three places in libxl this may result in a zero size argument being passed to malloc() or realloc(), which is not adviseable.

Quoted 38 lines
tools/libxl/libxl.c +3 -3
index 0f936c0..1c04d37 100644
@@ -1840,7 +1840,7 @@ libxl_device_vtpm *libxl_device_vtpm_list(libxl_ctx *ctx, uint32_t domid, int *n
 
     fe_path = libxl__sprintf(gc, "%s/device/vtpm", libxl__xs_get_dompath(gc, domid));
     dir = libxl__xs_directory(gc, XBT_NULL, fe_path, &ndirs);
-    if(dir) {
+    if (dir && ndirs) {
        vtpms = malloc(sizeof(*vtpms) * ndirs);
        libxl_device_vtpm* vtpm;
        libxl_device_vtpm* end = vtpms + ndirs;
@@ -2314,7 +2314,7 @@ static int libxl__append_disk_list_of_type(libxl__gc *gc,
     be_path = libxl__sprintf(gc, "%s/backend/%s/%d",
                              libxl__xs_get_dompath(gc, 0), type, domid);
     dir = libxl__xs_directory(gc, XBT_NULL, be_path, &n);
-    if (dir) {
+    if (dir && n) {
         libxl_device_disk *tmp;
         tmp = realloc(*disks, sizeof (libxl_device_disk) * (*ndisks + n));
         if (tmp == NULL)
@@ -3006,7 +3006,7 @@ static int libxl__append_nic_list_of_type(libxl__gc *gc,
     be_path = libxl__sprintf(gc, "%s/backend/%s/%d",
                              libxl__xs_get_dompath(gc, 0), type, domid);
     dir = libxl__xs_directory(gc, XBT_NULL, be_path, &n);
-    if (dir) {
+    if (dir && n) {
         libxl_device_nic *tmp;
         tmp = realloc(*nics, sizeof (libxl_device_nic) * (*nnics + n));
         if (tmp == NULL)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#4 lore raw

Re: xl network-attach SEGV in 4.2 and 4.1 [and 1 more messages]

In reply to Ian Jackson
Quoted 7 lines
Quoted 1 line
Quoted 1 line · Ian Jackson

Marek Marczykowski writes ("[Xen-devel] xl network-attach SEGV in 4.2 and 4.1"):

Quoted 4 lines

4.2 and 4.1 suffers from SEGV during xl network-attach in libxl__device_nic_add. In 4.3-unstable it is fixed by:
5420f2650 libxl: Set vfb and vkb devid if not done so by the caller

Quoted 7 lines
Quoted 1 line

So either the patch need to be backported to 4.1 and 4.2, or fixed by this

one:

Quoted 2 lines

- ------
libxl: Fix SEGV in network-attach

I have applied this to 4.2 and 4.1. I had to fix up a conflict in 4.1 and also I fixed a whitespace bug. The patches as committed are below.

This fixup on 4.1 went wrong - you dropped an & that's necessary. I'll take the liberty to fix this right away.

Jan

Quoted 23 lines
Quoted 2 lines

When "device/vif" directory exists but is empty l!=NULL, but nb==0, so l[nb-1] is invalid. Add missing check.

...

Quoted 6 lines
         if (!(l = libxl__xs_directory(gc, XBT_NULL,

- - libxl__sprintf(gc, "%s/device/vif",
dompath), &nb))) {
+ libxl__sprintf(gc, "%s/device/vif",
dompath), &nb)) ||
+ nb == 0) {

Something in the arrangements you used to prepare this patch linewrapped it _before you signed it_.

Ian Jackson writes ("Re: [Xen-devel] xl network-attach SEGV in 4.2 and 4.1"):

Quoted 4 lines
Author: Ian Jackson <[email protected]>
Date:   Thu Apr 18 16:27:46 2013 +0100

libxl: Avoid realloc(,0) when libxl__xs_directory returns empty list

IWBNI someone would review this and ack it; I'd like it to go into 4.1, 4.2 and unstable.

Ian.

#5 lore raw

Re: xl network-attach SEGV in 4.2 and 4.1 [and 1 more messages]

Jan Beulich writes ("Re: [Xen-devel] xl network-attach SEGV in 4.2 and 4.1 [and 1 more messages]"):

Quoted 2 lines

This fixup on 4.1 went wrong - you dropped an & that's necessary. I'll take the liberty to fix this right away.

Oops, thanks.

Ian.

#7 lore raw

PATCH RESEND 1/1 libxl: Avoid realloc(, 0) when libxl__xs_directory returns empty list

In reply to Ian Jackson

Ian Jackson writes ("Re: [Xen-devel] xl network-attach SEGV in 4.2 and 4.1"):

Quoted 5 lines

Thanks for pointing this out. The root cause is the expectation that libxl__xs_directory won't return a non-null list but set *nb to 0.

I found some occurrences of this bug in (xen-unstable) staging. See patch below, which should probably go into 4.1 and 4.2.

Ping.

Ian.

Quoted 1 line

From ea275c0e29299a2399aa7814116d68bfdaeb21c1 Mon Sep 17 00:00:00 2001

From: Ian Jackson <[email protected]>
Date: Thu, 18 Apr 2013 16:27:46 +0100
Subject: [PATCH] libxl: Avoid realloc(,0) when libxl__xs_directory returns
empty list
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

If the named path is a leaf node, libxl__xs_directory can succeed, returning non-null, but set *nb to 0.

In three places in libxl this may result in a zero size argument being passed to malloc() or realloc(), which is not adviseable.

---
 tools/libxl/libxl.c |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/libxl/libxl.c +3 -3
index 0f0f56c..887e0ca 100644
@@ -1864,7 +1864,7 @@ libxl_device_vtpm *libxl_device_vtpm_list(libxl_ctx *ctx, uint32_t domid, int *n
 
     fe_path = libxl__sprintf(gc, "%s/device/vtpm", libxl__xs_get_dompath(gc, domid));
     dir = libxl__xs_directory(gc, XBT_NULL, fe_path, &ndirs);
-    if(dir) {
+    if (dir && ndirs) {
        vtpms = malloc(sizeof(*vtpms) * ndirs);
        libxl_device_vtpm* vtpm;
        libxl_device_vtpm* end = vtpms + ndirs;
@@ -2371,7 +2371,7 @@ static int libxl__append_disk_list_of_type(libxl__gc *gc,
     be_path = libxl__sprintf(gc, "%s/backend/%s/%d",
                              libxl__xs_get_dompath(gc, 0), type, domid);
     dir = libxl__xs_directory(gc, XBT_NULL, be_path, &n);
-    if (dir) {
+    if (dir && n) {
         libxl_device_disk *tmp;
         tmp = realloc(*disks, sizeof (libxl_device_disk) * (*ndisks + n));
         if (tmp == NULL)
@@ -3060,7 +3060,7 @@ static int libxl__append_nic_list_of_type(libxl__gc *gc,
     be_path = libxl__sprintf(gc, "%s/backend/%s/%d",
                              libxl__xs_get_dompath(gc, 0), type, domid);
     dir = libxl__xs_directory(gc, XBT_NULL, be_path, &n);
-    if (dir) {
+    if (dir && n) {
         libxl_device_nic *tmp;
         tmp = realloc(*nics, sizeof (libxl_device_nic) * (*nnics + n));
         if (tmp == NULL)
Signature · 1 line
1.7.10.4
Acked-by on v1 1/1
#8 lore raw

Re: [PATCH RESEND] libxl: Avoid realloc(, 0) when libxl__xs_directory returns empty list

Quoted 8 lines · Ian Jackson

Ian Jackson writes ("Re: [Xen-devel] xl network-attach SEGV in 4.2 and 4.1"):

Quoted 5 lines

Thanks for pointing this out. The root cause is the expectation that libxl__xs_directory won't return a non-null list but set *nb to 0.

I found some occurrences of this bug in (xen-unstable) staging. See patch below, which should probably go into 4.1 and 4.2.

Ping.

I could have sworn this went in...

Quoted 12 lines

Ian.

From ea275c0e29299a2399aa7814116d68bfdaeb21c1 Mon Sep 17 00:00:00 2001
From: Ian Jackson <[email protected]>
Date: Thu, 18 Apr 2013 16:27:46 +0100
Subject: [PATCH] libxl: Avoid realloc(,0) when libxl__xs_directory returns
empty list
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Quoted 5 lines

If the named path is a leaf node, libxl__xs_directory can succeed, returning non-null, but set *nb to 0.

In three places in libxl this may result in a zero size argument being passed to malloc() or realloc(), which is not adviseable.

I thought it was strictly speaking fine, but I can see why it would be best to avoid.

Quoted 2 lines
Quoted 27 lines
---
 tools/libxl/libxl.c |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/libxl/libxl.c +2 -2
index 0f0f56c..887e0ca 100644
@@ -1864,7 +1864,7 @@ libxl_device_vtpm *libxl_device_vtpm_list(libxl_ctx *ctx, uint32_t domid, int *n
 
     fe_path = libxl__sprintf(gc, "%s/device/vtpm", libxl__xs_get_dompath(gc, domid));
     dir = libxl__xs_directory(gc, XBT_NULL, fe_path, &ndirs);
-    if(dir) {
+    if (dir && ndirs) {
        vtpms = malloc(sizeof(*vtpms) * ndirs);
        libxl_device_vtpm* vtpm;
        libxl_device_vtpm* end = vtpms + ndirs;
@@ -2371,7 +2371,7 @@ static int libxl__append_disk_list_of_type(libxl__gc *gc,
     be_path = libxl__sprintf(gc, "%s/backend/%s/%d",
                              libxl__xs_get_dompath(gc, 0), type, domid);
     dir = libxl__xs_directory(gc, XBT_NULL, be_path, &n);
-    if (dir) {
+    if (dir && n) {
         libxl_device_disk *tmp;
         tmp = realloc(*disks, sizeof (libxl_device_disk) * (*ndisks + n));
         if (tmp == NULL)

tools/libxl/libxl.c static int libxl__append_nic_list_of_type(libxl__gc *gc, · line 3066

Quoted 8 lines
tools/libxl/libxl.c +1 -1
@@ -3060,7 +3060,7 @@ static int libxl__append_nic_list_of_type(libxl__gc *gc,
     be_path = libxl__sprintf(gc, "%s/backend/%s/%d",
                              libxl__xs_get_dompath(gc, 0), type, domid);
     dir = libxl__xs_directory(gc, XBT_NULL, be_path, &n);
-    if (dir) {
+    if (dir && n) {
         libxl_device_nic *tmp;
         tmp = realloc(*nics, sizeof (libxl_device_nic) * (*nnics + n));
         if (tmp == NULL)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#9 lore raw

Re: [PATCH RESEND] libxl: Avoid realloc(, 0) when libxl__xs_directory returns empty list

Ian Campbell writes ("Re: [PATCH RESEND] libxl: Avoid realloc(,0) when libxl__xs_directory returns empty list"):

Quoted 4 lines · Ian Jackson
Quoted 1 line

Ping.

I could have sworn this went in...

Yes. I have found a bunch of these lying around in branches in my main tree.

Quoted 5 lines
Quoted 2 lines

In three places in libxl this may result in a zero size argument being passed to malloc() or realloc(), which is not adviseable.

I thought it was strictly speaking fine, but I can see why it would be best to avoid.

Calling malloc(0) merely produces a fiddly-to-deal-with corner case, where it may succeed by returning 0.

realloc(something,0) is actually wrong. If it returns 0 you don't know whether that means "I tried to allocate you a new 0-length block, but failed, so your original block is still there" or "I have successfully freed your old block; have this null pointer as an answer".

Quoted 4 lines
Quoted 2 lines

And put on the backport list.

Thanks,
Ian.

#10 lore raw

Re: [PATCH RESEND] libxl: Avoid realloc(, 0) when libxl__xs_directory returns empty list

Ian Jackson writes ("Re: [PATCH RESEND] libxl: Avoid realloc(,0) when libxl__xs_directory returns empty list"):

Quoted 3 lines

And put on the backport list.

Now applied to 4.3. Was not applicable to 4.2, 4.1.

Ian.