Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the overrun issue reported by static application security testing #177

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions snappy_test_tool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,12 @@ namespace {
class DataEndingAtUnreadablePage {
public:
explicit DataEndingAtUnreadablePage(const std::string& s) {
const size_t page_size = sysconf(_SC_PAGESIZE);
long page_size = sysconf(_SC_PAGESIZE);
page_size_ = (size_t)(page_size < 0 ? 4096 : page_size);
const size_t size = s.size();
// Round up space for string to a multiple of page_size.
size_t space_for_string = (size + page_size - 1) & ~(page_size - 1);
alloc_size_ = space_for_string + page_size;
// Round up space for string to a multiple of page_size_.
size_t space_for_string = (size + page_size_ - 1) & ~(page_size_ - 1);
alloc_size_ = space_for_string + page_size_;
mem_ = mmap(NULL, alloc_size_,
PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
CHECK_NE(MAP_FAILED, mem_);
Expand All @@ -91,13 +92,12 @@ class DataEndingAtUnreadablePage {
data_ = dst;
size_ = size;
// Make guard page unreadable.
CHECK_EQ(0, mprotect(protected_page_, page_size, PROT_NONE));
CHECK_EQ(0, mprotect(protected_page_, page_size_, PROT_NONE));
}

~DataEndingAtUnreadablePage() {
const size_t page_size = sysconf(_SC_PAGESIZE);
// Undo the mprotect.
CHECK_EQ(0, mprotect(protected_page_, page_size, PROT_READ|PROT_WRITE));
CHECK_EQ(0, mprotect(protected_page_, page_size_, PROT_READ|PROT_WRITE));
CHECK_EQ(0, munmap(mem_, alloc_size_));
}

Expand All @@ -110,6 +110,7 @@ class DataEndingAtUnreadablePage {
char* protected_page_;
const char* data_;
size_t size_;
size_t page_size_;
};

#else // HAVE_FUNC_MMAP && HAVE_FUNC_SYSCONF
Expand Down
15 changes: 8 additions & 7 deletions snappy_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ namespace {
class DataEndingAtUnreadablePage {
public:
explicit DataEndingAtUnreadablePage(const std::string& s) {
const size_t page_size = sysconf(_SC_PAGESIZE);
long page_size = sysconf(_SC_PAGESIZE);
page_size_ = (size_t)(page_size < 0 ? 4096 : page_size);
const size_t size = s.size();
// Round up space for string to a multiple of page_size.
size_t space_for_string = (size + page_size - 1) & ~(page_size - 1);
alloc_size_ = space_for_string + page_size;
// Round up space for string to a multiple of page_size_.
size_t space_for_string = (size + page_size_ - 1) & ~(page_size_ - 1);
alloc_size_ = space_for_string + page_size_;
mem_ = mmap(NULL, alloc_size_,
PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
CHECK_NE(MAP_FAILED, mem_);
Expand All @@ -75,13 +76,12 @@ class DataEndingAtUnreadablePage {
data_ = dst;
size_ = size;
// Make guard page unreadable.
CHECK_EQ(0, mprotect(protected_page_, page_size, PROT_NONE));
CHECK_EQ(0, mprotect(protected_page_, page_size_, PROT_NONE));
}

~DataEndingAtUnreadablePage() {
const size_t page_size = sysconf(_SC_PAGESIZE);
// Undo the mprotect.
CHECK_EQ(0, mprotect(protected_page_, page_size, PROT_READ|PROT_WRITE));
CHECK_EQ(0, mprotect(protected_page_, page_size_, PROT_READ|PROT_WRITE));
CHECK_EQ(0, munmap(mem_, alloc_size_));
}

Expand All @@ -94,6 +94,7 @@ class DataEndingAtUnreadablePage {
char* protected_page_;
const char* data_;
size_t size_;
size_t page_size_;
};

#else // HAVE_FUNC_MMAP) && HAVE_FUNC_SYSCONF
Expand Down