From 2f61a914591489a20eaf72d351e540b61cba1f2c Mon Sep 17 00:00:00 2001 From: Bigfoot Date: Sat, 12 Feb 2022 18:05:19 -0600 Subject: [PATCH] Prevent crash when opening a file w/o read access (#437) --- plugins/builtin/source/content/providers/file_provider.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/plugins/builtin/source/content/providers/file_provider.cpp b/plugins/builtin/source/content/providers/file_provider.cpp index 1f2547ac9..13e1b86e0 100644 --- a/plugins/builtin/source/content/providers/file_provider.cpp +++ b/plugins/builtin/source/content/providers/file_provider.cpp @@ -238,11 +238,13 @@ namespace hex::plugin::builtin::prv { #else const auto &path = this->m_path.native(); + int mmapprot = PROT_READ | PROT_WRITE; this->m_file = ::open(path.c_str(), O_RDWR); if (this->m_file == -1) { this->m_file = ::open(path.c_str(), O_RDONLY); this->m_writable = false; + mmapprot &= ~(PROT_WRITE); } if (this->m_file == -1) { @@ -252,8 +254,8 @@ namespace hex::plugin::builtin::prv { this->m_fileSize = this->m_fileStats.st_size; - this->m_mappedFile = ::mmap(nullptr, this->m_fileSize, PROT_READ | PROT_WRITE, MAP_SHARED, this->m_file, 0); - if (this->m_mappedFile == nullptr) { + this->m_mappedFile = ::mmap(nullptr, this->m_fileSize, mmapprot, MAP_SHARED, this->m_file, 0); + if (this->m_mappedFile == MAP_FAILED) { ::close(this->m_file); this->m_file = -1;