From: Ian Jackson Subject: [PATCH RESEND] libxl: Avoid realloc(, 0) when libxl__xs_directory returns empty list Date: Tue, 12 Nov 2013 17:23:59 +0000 Message-ID: <21122.25647.129932.90020@mariner.uk.xensource.com> References: <516E0DCF.9090001@invisiblethingslab.com> <1366188196.8399.187.camel@zakaz.uk.xensource.com> <20848.6355.859738.100856@mariner.uk.xensource.com> Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Return-path: In-Reply-To: <20848.6355.859738.100856@mariner.uk.xensource.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Ian Campbell , Marek Marczykowski , "xen-devel@lists.xen.org" Cc: Roger Pau Monne List-Id: xen-devel@lists.xenproject.org Ian Jackson writes ("Re: [Xen-devel] xl network-attach SEGV in 4.2 and 4.1"= ): > 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. >From ea275c0e29299a2399aa7814116d68bfdaeb21c1 Mon Sep 17 00:00:00 2001 From: Ian Jackson 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=3DUTF-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. Signed-off-by: Ian Jackson Acked-by: Roger Pau Monn=E9 --- tools/libxl/libxl.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c index 0f0f56c..887e0ca 100644 --- a/tools/libxl/libxl.c +++ b/tools/libxl/libxl.c @@ -1864,7 +1864,7 @@ libxl_device_vtpm *libxl_device_vtpm_list(libxl_ctx *= ctx, uint32_t domid, int *n = fe_path =3D libxl__sprintf(gc, "%s/device/vtpm", libxl__xs_get_dompath= (gc, domid)); dir =3D libxl__xs_directory(gc, XBT_NULL, fe_path, &ndirs); - if(dir) { + if (dir && ndirs) { vtpms =3D malloc(sizeof(*vtpms) * ndirs); libxl_device_vtpm* vtpm; libxl_device_vtpm* end =3D vtpms + ndirs; @@ -2371,7 +2371,7 @@ static int libxl__append_disk_list_of_type(libxl__gc = *gc, be_path =3D libxl__sprintf(gc, "%s/backend/%s/%d", libxl__xs_get_dompath(gc, 0), type, domid); dir =3D libxl__xs_directory(gc, XBT_NULL, be_path, &n); - if (dir) { + if (dir && n) { libxl_device_disk *tmp; tmp =3D realloc(*disks, sizeof (libxl_device_disk) * (*ndisks + n)= ); if (tmp =3D=3D NULL) @@ -3060,7 +3060,7 @@ static int libxl__append_nic_list_of_type(libxl__gc *= gc, be_path =3D libxl__sprintf(gc, "%s/backend/%s/%d", libxl__xs_get_dompath(gc, 0), type, domid); dir =3D libxl__xs_directory(gc, XBT_NULL, be_path, &n); - if (dir) { + if (dir && n) { libxl_device_nic *tmp; tmp =3D realloc(*nics, sizeof (libxl_device_nic) * (*nnics + n)); if (tmp =3D=3D NULL) -- = 1.7.10.4