Skip to contentSkip to conversation

Fix memory corruption when using realloc_memory_ranges()

PATCH Applied Bernhard Walle 2 participants 2 messages Jan 16, 2009 – Jan 19, 2009

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix memory corruption when using realloc_memory_ranges()

Because realloc_memory_ranges() makes the old memory invalid, and we return a pointer to memory_range in get_memory_ranges(), we need to copy the contents in get_memory_ranges().

Some code that calls realloc_memory_ranges() may be triggered by get_base_ranges() which is called after get_memory_ranges().

Yes, the memory needs to be deleted somewhere, but I don't know currently where it's the best, and since it's not in a loop and memory is deleted anyway after program termination I don't want to introduce unneccessary complexity. The problem is that get_base_ranges() gets called from architecture independent code and that allocation is PPC64-specific here.

index b0d8acd..ad8a31c 100644

---
 kexec/arch/ppc64/kexec-ppc64.c |   11 ++++++++++-
 1 files changed, 10 insertions(+), 1 deletions(-)
kexec/arch/ppc64/kexec-ppc64.c +10 -1
index b0d8acd..ad8a31c 100644
@@ -715,7 +715,16 @@ int get_memory_ranges(struct memory_range **range, int *ranges,
 	if (setup_memory_ranges(kexec_flags))
 		return -1;
 
-	*range = memory_range;
+	/*
+	 * copy the memory here, another realloc_memory_ranges might
+	 * corrupt the old memory
+	 */
+	*range = calloc(sizeof(struct memory_range), nr_memory_ranges);
+	if (*range == NULL)
+		return -1;
+	memmove(*range, memory_range,
+		sizeof(struct memory_range) * nr_memory_ranges);
+
 	*ranges = nr_memory_ranges;
 	fprintf(stderr, "get memory ranges:%d\n", nr_memory_ranges);
 	return 0;
Signature · 7 lines
1.6.0.2


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Applied
#1 lore raw
Quoted 4 lines · Bernhard Walle

Because realloc_memory_ranges() makes the old memory invalid, and we return a pointer to memory_range in get_memory_ranges(), we need to copy the contents in get_memory_ranges().

Quoted 8 lines

Some code that calls realloc_memory_ranges() may be triggered by get_base_ranges() which is called after get_memory_ranges().

Yes, the memory needs to be deleted somewhere, but I don't know currently where it's the best, and since it's not in a loop and memory is deleted anyway after program termination I don't want to introduce unneccessary complexity. The problem is that get_base_ranges() gets called from architecture independent code and that allocation is PPC64-specific here.

Thanks, this sounds like a bit of a mess, but I think that your patch does improve the situation.

Applied.

Signature · 9 lines
Simon Horman
  VA Linux Systems Japan K.K., Sydney, Australia Satellite Office
  H: www.vergenet.net/~horms/             W: www.valinux.co.jp/en


_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec