diff options
author | Yabin Cui <yabinc@google.com> | 2015-12-08 18:43:00 -0800 |
---|---|---|
committer | Yabin Cui <yabinc@google.com> | 2015-12-08 19:22:29 -0800 |
commit | ce7eba001b8c9b72fa642e6b6a13e66c8d6daf33 (patch) | |
tree | 79dc84f1c3ae5b4c489db793489d826ce8ed2df9 | |
parent | 39be56b77007b1c8269c2e7a9ad1f938ae06f5a9 (diff) | |
download | unwinding-ce7eba001b8c9b72fa642e6b6a13e66c8d6daf33.tar.gz |
libbacktrace: check if elf file paths are valid before reading them.brillo-m9-releasebrillo-m9-devbrillo-m8-releasebrillo-m8-dev
Bug: 25194400
Change-Id: If6e60585673226392d38fab9ad14bd7b94261316
-rw-r--r-- | libbacktrace/BacktraceOffline.cpp | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/libbacktrace/BacktraceOffline.cpp b/libbacktrace/BacktraceOffline.cpp index 27dfb83..abc186b 100644 --- a/libbacktrace/BacktraceOffline.cpp +++ b/libbacktrace/BacktraceOffline.cpp @@ -22,7 +22,9 @@ extern "C" { } #include <stdint.h> +#include <stdio.h> #include <string.h> +#include <sys/stat.h> #include <sys/types.h> #include <ucontext.h> #include <unistd.h> @@ -616,7 +618,30 @@ DebugFrameInfo* ReadDebugFrameFromELFFile(const llvm::object::ELFFile<ELFT>* elf return debug_frame; } +static bool IsValidElfPath(const std::string& filename) { + static const char elf_magic[] = {0x7f, 'E', 'L', 'F'}; + + struct stat st; + if (stat(filename.c_str(), &st) != 0 || !S_ISREG(st.st_mode)) { + return false; + } + FILE* fp = fopen(filename.c_str(), "reb"); + if (fp == nullptr) { + return false; + } + char buf[4]; + if (fread(buf, 4, 1, fp) != 1) { + fclose(fp); + return false; + } + fclose(fp); + return memcmp(buf, elf_magic, 4) == 0; +} + static DebugFrameInfo* ReadDebugFrameFromFile(const std::string& filename) { + if (!IsValidElfPath(filename)) { + return nullptr; + } auto owning_binary = llvm::object::createBinary(llvm::StringRef(filename)); if (owning_binary.getError()) { return nullptr; |