Skip to contentSkip to conversation

libdm: Fix memory corruption if dm_asprintf fails.

PATCH New Mikulas Patocka 2 participants 2 messages Oct 18, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libdm: Fix memory corruption if dm_asprintf fails.

I found this bug when reading through my lvm changes.

We must set the variable dmt->geometry to NULL after freeing it.

dm_asprintf may fail, on failure it may not set dmt->geometry, thus dmt->geometry would be pointing to a free string.

---
 libdm/ioctl/libdm-iface.c |    1 +
 1 file changed, 1 insertion(+)
libdm/ioctl/libdm-iface.c +1 -0
Index: lvm2-copy/libdm/ioctl/libdm-iface.c
===================================================================
@@ -817,6 +817,7 @@ int dm_task_set_geometry(struct dm_task
 			 const char *sectors, const char *start)
 {
 	dm_free(dmt->geometry);
+	dmt->geometry = NULL;
 	if (dm_asprintf(&(dmt->geometry), "%s %s %s %s",
 			cylinders, heads, sectors, start) < 0) {
 		log_error("dm_task_set_geometry: sprintf failed");
#1 lore raw

PATCH 1/1 libdm: Fix memory corruption if dm_asprintf fails.

Dne 18.10.2013 02:31, Mikulas Patocka napsal(a):

Quoted 18 lines

I found this bug when reading through my lvm changes.

We must set the variable dmt->geometry to NULL after freeing it.

dm_asprintf may fail, on failure it may not set dmt->geometry, thus dmt->geometry would be pointing to a free string.

---
  libdm/ioctl/libdm-iface.c |    1 +
  1 file changed, 1 insertion(+)

Index: lvm2-copy/libdm/ioctl/libdm-iface.c
===================================================================

libdm/ioctl/libdm-iface.c int dm_task_set_geometry(struct dm_task · line 822

Quoted 6 lines
libdm/ioctl/libdm-iface.c +1 -0
@@ -817,6 +817,7 @@ int dm_task_set_geometry(struct dm_task
  			 const char *sectors, const char *start)
  {
  	dm_free(dmt->geometry);
+	dmt->geometry = NULL;
  	if (dm_asprintf(&(dmt->geometry), "%s %s %s %s",
  			cylinders, heads, sectors, start) < 0) {

Hi

dm_asprintf() -> dm_vasprintf() -> first thing it does is: *result = 0;

So it should never return failure and leave &dmt->geometry in undefined state. On error path it should always be set to NULL - unless there is a bug in dm_vasprintf() which would need to be fixed.

Zdenek