Skip to content

Commit ca126d4

Browse files
Fix out-of-bounds write in case of failing mmap(...) in PosixLockedPageAllocator::AllocateLocked
1 parent 9c71998 commit ca126d4

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

src/support/allocators/secure.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ struct secure_allocator : public std::allocator<T> {
4040

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

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

src/support/lockedpool.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,9 @@ void *PosixLockedPageAllocator::AllocateLocked(size_t len, bool *lockingSuccess)
248248
void *addr;
249249
len = align_up(len, page_size);
250250
addr = mmap(nullptr, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
251+
if (addr == MAP_FAILED) {
252+
return nullptr;
253+
}
251254
if (addr) {
252255
*lockingSuccess = mlock(addr, len) == 0;
253256
}

src/support/lockedpool.h

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

0 commit comments

Comments
 (0)