Commit 120fb0fd authored by Francis Ricci's avatar Francis Ricci Committed by Facebook GitHub Bot

Add mmap safety check to the elf file parser

Summary: mmap() calls are a bit dangerous, in that the backing files can be corrupted or unreadable. Ideally, we would just migrate this whole symbolizer to use ::pread(), but in the meantime, just making sure that the last byte of the mmap'd file is readable seems to get rid of the crashes we've been seeing.

Reviewed By: ccdavid

Differential Revision: D24505608

fbshipit-source-id: e7bb3ea4d34f7b79f5a1cbb593a195f836d85ec7
parent cda620cb
......@@ -216,6 +216,15 @@ ElfFile::OpenResult ElfFile::init() noexcept {
if (std::strncmp(elfMagBuf.data(), ELFMAG, sizeof(ELFMAG)) != 0) {
return {kInvalidElfFile, "invalid ELF magic"};
}
char c;
if (::pread(fd_, &c, 1, length_ - 1) != 1) {
auto msg =
"The last bit of the mmaped memory is no longer valid. This may be "
"caused by the original file being resized, "
"deleted or otherwise modified.";
return {kInvalidElfFile, msg};
}
if (::lseek(fd_, 0, SEEK_SET) != 0) {
return {kInvalidElfFile,
"unable to reset file descriptor after reading ELF magic number"};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment