Skip to content

Commit 1ccd62e

Browse files
practicalswiftfurszy
authored andcommitted
Fix out-of-bounds write in case of failing mmap(...) in PosixLockedPageAllocator::AllocateLocked
1 parent 7ad7157 commit 1ccd62e

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

src/support/allocators/secure.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ struct secure_allocator : public std::allocator<T> {
4141

4242
T* allocate(std::size_t n, const void* hint = 0)
4343
{
44-
return static_cast<T*>(LockedPoolManager::Instance().alloc(sizeof(T) * n));
44+
T* allocation = static_cast<T*>(LockedPoolManager::Instance().alloc(sizeof(T) * n));
45+
if (!allocation) {
46+
throw std::bad_alloc();
47+
}
48+
return allocation;
4549
}
4650

4751
void deallocate(T* p, std::size_t n)

src/support/lockedpool.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ void *PosixLockedPageAllocator::AllocateLocked(size_t len, bool *lockingSuccess)
228228
void *addr;
229229
len = align_up(len, page_size);
230230
addr = mmap(nullptr, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
231+
if (addr == MAP_FAILED) {
232+
return nullptr;
233+
}
231234
if (addr) {
232235
*lockingSuccess = mlock(addr, len) == 0;
233236
}

src/support/lockedpool.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class LockedPageAllocator
2121
virtual ~LockedPageAllocator() {}
2222
/** Allocate and lock memory pages.
2323
* If len is not a multiple of the system page size, it is rounded up.
24-
* Returns 0 in case of allocation failure.
24+
* Returns nullptr in case of allocation failure.
2525
*
2626
* If locking the memory pages could not be accomplished it will still
2727
* return the memory, however the lockingSuccess flag will be false.

0 commit comments

Comments
 (0)