From f65599e216b0d692b461bbcb9a62d8da4aaf912b Mon Sep 17 00:00:00 2001 From: nathanel23 <46408536+nathanel23@users.noreply.github.com> Date: Sun, 6 Jan 2019 01:06:23 +0100 Subject: [PATCH] Fix for memory corruption/crash related to rhash This patch fixes issues with some calls to the rhash C library that cause subtle memory corruptions. There are two main issues that this patch addresses: 1) The library init function is called multiple times in various places within in the Util class. This is not necessary and may cause issues if the initialization is repeated simultaneously in multiple threads. Therefore the call was moved to the beginning of the main() function. 2) There is not enough space in the output buffers to store the terminating NUL character of the hex-formatted hashes. The print function from the rhash library writes a regular C string to the output buffer and expects enough space to include the end marker. Depending on the memory layout generated by the compiler, this results in one byte of the stack to be overwritten, which might lead to random issues that are hard to find. On AArch64 (ARM) with GCC 6.3 the call to the rhash_print() function happens to destroy the lower byte of the frame pointer and causes a crash due to an invalid free() operation some time after the Util::createXML function returned. --- main.cpp | 2 ++ src/util.cpp | 9 +++------ 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/main.cpp b/main.cpp index bada05a..cd5faa5 100644 --- a/main.cpp +++ b/main.cpp @@ -26,6 +26,8 @@ template void set_vm_value(std::map