Skip to content

Commit b06cf01

Browse files
committed
kpartx: fix segfault when operating on regular files
The following problem has been introduced in multipath-tools 0.14.0: > truncate -s1G /tmp/img > kpartx -a /tmp/img double free or corruption (out) Aborted (core dumped) kpartx -a /tmp/img Fix it by always allocating "uuid" on the heap, rather than using a static char array. Fixes: 8c39e60 ("kpartx: fix some memory leaks") Fixes: #139 Signed-off-by: Martin Wilck <mwilck@suse.com> Reviewed-by: Benjamin Marzinski <bmarzins@redhat.com>
1 parent c916c89 commit b06cf01

2 files changed

Lines changed: 12 additions & 9 deletions

File tree

kpartx/devmapper.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
* Copyright (c) 2004, 2005 Christophe Varoqui
33
*/
4+
#define _GNU_SOURCE
45
#include <stdio.h>
56
#include <stdlib.h>
67
#include <string.h>
@@ -699,14 +700,13 @@ int dm_find_part(const char *parent, const char *delim, int part,
699700

700701
char *nondm_create_uuid(dev_t devt)
701702
{
702-
#define NONDM_UUID_BUFLEN (34 + sizeof(NONDM_UUID_PREFIX) + \
703-
sizeof(NONDM_UUID_SUFFIX))
704-
static char uuid_buf[NONDM_UUID_BUFLEN];
705-
snprintf(uuid_buf, sizeof(uuid_buf), "%s_%u:%u_%s",
706-
NONDM_UUID_PREFIX, major(devt), minor(devt),
707-
NONDM_UUID_SUFFIX);
708-
uuid_buf[NONDM_UUID_BUFLEN-1] = '\0';
709-
return uuid_buf;
703+
char *uuid;
704+
705+
if (asprintf(&uuid, "%s_%u:%u_%s", NONDM_UUID_PREFIX, major(devt),
706+
minor(devt), NONDM_UUID_SUFFIX) >= 0)
707+
return uuid;
708+
else
709+
return NULL;
710710
}
711711

712712
int nondm_parse_uuid(const char *uuid, unsigned int *major, unsigned int *minor)

kpartx/kpartx.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,11 @@ main(int argc, char **argv){
404404
* This allows deletion of partitions created with older kpartx
405405
* versions which didn't use the fake UUID during creation.
406406
*/
407-
if (!uuid && !(what == DELETE && force_devmap))
407+
if (!uuid && !(what == DELETE && force_devmap)) {
408408
uuid = nondm_create_uuid(buf.st_rdev);
409+
if (!uuid)
410+
exit(1);
411+
}
409412

410413
if (delim == NULL) {
411414
delim = xmalloc(DELIM_SIZE);

0 commit comments

Comments
 (0)