Skip to content

Commit

Permalink
Fix out-of-bounds write in case of failing mmap(...) in PosixLockedPa…
Browse files Browse the repository at this point in the history
…geAllocator::AllocateLocked
  • Loading branch information
practicalswift committed Jan 6, 2019
1 parent 9c71998 commit ca126d4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/support/allocators/secure.h
Expand Up @@ -40,7 +40,11 @@ struct secure_allocator : public std::allocator<T> {

T* allocate(std::size_t n, const void* hint = 0)
{
return static_cast<T*>(LockedPoolManager::Instance().alloc(sizeof(T) * n));
T* allocation = static_cast<T*>(LockedPoolManager::Instance().alloc(sizeof(T) * n));
if (!allocation) {
throw std::bad_alloc();
}
return allocation;
}

void deallocate(T* p, std::size_t n)
Expand Down
3 changes: 3 additions & 0 deletions src/support/lockedpool.cpp
Expand Up @@ -248,6 +248,9 @@ void *PosixLockedPageAllocator::AllocateLocked(size_t len, bool *lockingSuccess)
void *addr;
len = align_up(len, page_size);
addr = mmap(nullptr, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
if (addr == MAP_FAILED) {
return nullptr;
}
if (addr) {
*lockingSuccess = mlock(addr, len) == 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/support/lockedpool.h
Expand Up @@ -22,7 +22,7 @@ class LockedPageAllocator
virtual ~LockedPageAllocator() {}
/** Allocate and lock memory pages.
* If len is not a multiple of the system page size, it is rounded up.
* Returns 0 in case of allocation failure.
* Returns nullptr in case of allocation failure.
*
* If locking the memory pages could not be accomplished it will still
* return the memory, however the lockingSuccess flag will be false.
Expand Down

0 comments on commit ca126d4

Please sign in to comment.