From 6e0a256d7f71e46fcedd4c01588526d3855cee5c Mon Sep 17 00:00:00 2001 From: z3phyr Date: Sun, 21 Apr 2024 00:14:06 -0400 Subject: [PATCH] [TO BE REVERTED] Fix memory leaks in linux heap parser (#4426) * Fix memory leaks in debug path * Fix memory leaks in mach and mdmp format * Update the fix for mach * Add support for aarch64 tcache parsing * Test case fix * Comment update --------- Co-authored-by: Giridhar Prasath R --- librz/bin/bin.c | 9 ++------- librz/bin/format/mdmp/mdmp_pe.c | 2 +- librz/bin/p/bin_mach0.c | 6 ++++-- librz/bin/p/bin_pe.inc | 2 +- librz/debug/p/native/linux/linux_debug.c | 3 ++- test/db/formats/pe/imports_tinyW7 | 2 +- 6 files changed, 11 insertions(+), 13 deletions(-) diff --git a/librz/bin/bin.c b/librz/bin/bin.c index fcd44957574..2e19540ff37 100644 --- a/librz/bin/bin.c +++ b/librz/bin/bin.c @@ -203,13 +203,8 @@ RZ_API void rz_bin_reloc_free(RZ_NULLABLE RzBinReloc *reloc) { if (!reloc) { return; } - /** - * TODO: leak in bin_elf, but it will cause double free in bin_pe if free here, - * Because in the bin_elf implementation RzBinObject->imports and RzBinObject->relocs->imports - * are two pieces of data, but they are linked to each other in bin_pe - */ - // rz_bin_import_free(reloc->import); - // rz_bin_symbol_free(reloc->symbol); + rz_bin_import_free(reloc->import); + rz_bin_symbol_free(reloc->symbol); free(reloc); } diff --git a/librz/bin/format/mdmp/mdmp_pe.c b/librz/bin/format/mdmp/mdmp_pe.c index 8fcb7bee276..9450cb3ca9a 100644 --- a/librz/bin/format/mdmp/mdmp_pe.c +++ b/librz/bin/format/mdmp/mdmp_pe.c @@ -134,7 +134,7 @@ RzPVector /**/ *PE_(rz_bin_mdmp_pe_get_imports)(struct PE_(rz_bin offset -= pe_bin->vaddr; } rel->additive = 0; - rel->import = ptr; + rel->import = rz_bin_import_clone(ptr); rel->addend = 0; rel->vaddr = offset + pe_bin->vaddr; rel->paddr = imports[i].paddr + pe_bin->paddr; diff --git a/librz/bin/p/bin_mach0.c b/librz/bin/p/bin_mach0.c index a8d4a3b5d6a..d3f0ec69e9c 100644 --- a/librz/bin/p/bin_mach0.c +++ b/librz/bin/p/bin_mach0.c @@ -408,9 +408,11 @@ static RzPVector /**/ *relocs(RzBinFile *bf) { free(ptr); break; } - ptr->import = imp; + ptr->import = rz_bin_import_clone(imp); } else if (reloc->ord >= 0 && reloc->ord < rz_pvector_len(&bin->imports_by_ord)) { - ptr->import = rz_pvector_at(&bin->imports_by_ord, reloc->ord); + RzBinImport *imp = NULL; + imp = rz_pvector_at(&bin->imports_by_ord, reloc->ord); + ptr->import = rz_bin_import_clone(imp); } ptr->addend = reloc->addend; ptr->vaddr = reloc->addr; diff --git a/librz/bin/p/bin_pe.inc b/librz/bin/p/bin_pe.inc index bbc00c69a8c..84bb526e591 100644 --- a/librz/bin/p/bin_pe.inc +++ b/librz/bin/p/bin_pe.inc @@ -474,7 +474,7 @@ static RzPVector /**/ *imports(RzBinFile *bf) { rel->type = RZ_BIN_RELOC_32; #endif rel->additive = 0; - rel->import = ptr; + rel->import = rz_bin_import_clone(ptr); rel->addend = 0; { ut8 addr[4]; diff --git a/librz/debug/p/native/linux/linux_debug.c b/librz/debug/p/native/linux/linux_debug.c index 2d58c104902..ed34ab999b4 100644 --- a/librz/debug/p/native/linux/linux_debug.c +++ b/librz/debug/p/native/linux/linux_debug.c @@ -903,7 +903,8 @@ RZ_API ut64 get_linux_tls_val(RZ_NONNULL RzDebug *dbg, int tid) { } else { tls = rz_reg_get_value(dbg->reg, ri); } -#elif __aarch64__ +#endif +#if __aarch64__ struct iovec iovec = { 0 }; ut64 reg; diff --git a/test/db/formats/pe/imports_tinyW7 b/test/db/formats/pe/imports_tinyW7 index c4a12af454d..b18932fb4a9 100644 --- a/test/db/formats/pe/imports_tinyW7 +++ b/test/db/formats/pe/imports_tinyW7 @@ -18,7 +18,7 @@ vaddr paddr type name 0x800004f4 0x00000234 SET_32 msvcrt_Ordinal_1268 nth vaddr bind type lib name ------------------------------------------------ -284 ---------- NONE FUNC kernel32 FindAtomW +284 0x00401048 NONE FUNC kernel32 Ordinal_284 1268 0x00401034 NONE FUNC msvcrt Ordinal_1268 EOF RUN