aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/utils
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2018-12-28 12:10:53 -0500
committerJack Lloyd <[email protected]>2018-12-28 12:17:46 -0500
commit752dcf335d06c75605313630f87beaa78db5e50d (patch)
tree6987d2768ea391c286b3b02e6842865851cfb8d4 /src/lib/utils
parent1d1f9a91a4f4805abda9590ee552ef6bb000b259 (diff)
Use posix_memalign instead of mmap for creating the locking pool
As described in #602, using mmap with fork causes problems because the mmap remains shared in the child instead of being copy-on-write, then the parent and child stomp on each others memory. However we really do not need mmap semantics, we just want a block of memory that is page-aligned, which can be done with posix_memalign instead. This was added in POSIX.1-2001 and seems to be implemented by all modern systems. Closes #602
Diffstat (limited to 'src/lib/utils')
-rw-r--r--src/lib/utils/os_utils.cpp26
1 files changed, 9 insertions, 17 deletions
diff --git a/src/lib/utils/os_utils.cpp b/src/lib/utils/os_utils.cpp
index 558bd71e6..265d4aac2 100644
--- a/src/lib/utils/os_utils.cpp
+++ b/src/lib/utils/os_utils.cpp
@@ -23,6 +23,7 @@
#include <sys/resource.h>
#include <sys/mman.h>
#include <signal.h>
+ #include <stdlib.h>
#include <setjmp.h>
#include <unistd.h>
#include <errno.h>
@@ -331,25 +332,16 @@ void* OS::allocate_locked_pages(size_t length)
{
#if defined(BOTAN_TARGET_OS_HAS_POSIX1) && defined(BOTAN_TARGET_OS_HAS_POSIX_MLOCK)
-#if !defined(MAP_NOCORE)
- #define MAP_NOCORE 0
-#endif
+ const size_t page_size = OS::system_page_size();
-#if !defined(MAP_ANONYMOUS)
- #define MAP_ANONYMOUS MAP_ANON
-#endif
+ if(length % page_size != 0)
+ return nullptr;
- void* ptr = ::mmap(nullptr,
- length,
- PROT_READ | PROT_WRITE,
- MAP_ANONYMOUS | MAP_SHARED | MAP_NOCORE,
- /*fd*/-1,
- /*offset*/0);
+ void* ptr = nullptr;
+ int rc = ::posix_memalign(&ptr, page_size, length);
- if(ptr == MAP_FAILED)
- {
+ if(rc != 0 || ptr == nullptr)
return nullptr;
- }
#if defined(MADV_DONTDUMP)
::madvise(ptr, length, MADV_DONTDUMP);
@@ -357,7 +349,7 @@ void* OS::allocate_locked_pages(size_t length)
if(::mlock(ptr, length) != 0)
{
- ::munmap(ptr, length);
+ std::free(ptr);
return nullptr; // failed to lock
}
@@ -392,7 +384,7 @@ void OS::free_locked_pages(void* ptr, size_t length)
#if defined(BOTAN_TARGET_OS_HAS_POSIX1) && defined(BOTAN_TARGET_OS_HAS_POSIX_MLOCK)
secure_scrub_memory(ptr, length);
::munlock(ptr, length);
- ::munmap(ptr, length);
+ std::free(ptr);
#elif defined(BOTAN_TARGET_OS_HAS_VIRTUAL_LOCK)
secure_scrub_memory(ptr, length);