From 2edb12e249387a72b24f5f34efae4735d589af9f Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Fri, 29 Sep 2017 19:15:48 -0400 Subject: In secure_allocator, hide mlock/new usage in a function in mem_ops Switch to calloc/free instead of new/delete - shouldn't matter since we are only allocate integral types. This change reduces the size of libbotan-2.so by ~300 Kb on my system. --- .../utils/locking_allocator/locking_allocator.cpp | 2 +- .../utils/locking_allocator/locking_allocator.h | 2 +- src/lib/utils/mem_ops.cpp | 32 ++++++++++++++++++++++ src/lib/utils/mem_ops.h | 18 ++++++++++++ 4 files changed, 52 insertions(+), 2 deletions(-) (limited to 'src/lib/utils') diff --git a/src/lib/utils/locking_allocator/locking_allocator.cpp b/src/lib/utils/locking_allocator/locking_allocator.cpp index 880f3add8..f36fa9130 100644 --- a/src/lib/utils/locking_allocator/locking_allocator.cpp +++ b/src/lib/utils/locking_allocator/locking_allocator.cpp @@ -116,7 +116,7 @@ void* mlock_allocator::allocate(size_t num_elems, size_t elem_size) return nullptr; } -bool mlock_allocator::deallocate(void* p, size_t num_elems, size_t elem_size) +bool mlock_allocator::deallocate(void* p, size_t num_elems, size_t elem_size) BOTAN_NOEXCEPT { if(!m_pool) return false; diff --git a/src/lib/utils/locking_allocator/locking_allocator.h b/src/lib/utils/locking_allocator/locking_allocator.h index 8e38129e2..e9299c120 100644 --- a/src/lib/utils/locking_allocator/locking_allocator.h +++ b/src/lib/utils/locking_allocator/locking_allocator.h @@ -21,7 +21,7 @@ class BOTAN_PUBLIC_API(2,0) mlock_allocator final void* allocate(size_t num_elems, size_t elem_size); - bool deallocate(void* p, size_t num_elems, size_t elem_size); + bool deallocate(void* p, size_t num_elems, size_t elem_size) BOTAN_NOEXCEPT; mlock_allocator(const mlock_allocator&) = delete; diff --git a/src/lib/utils/mem_ops.cpp b/src/lib/utils/mem_ops.cpp index 45de24cb8..b2097e4c7 100644 --- a/src/lib/utils/mem_ops.cpp +++ b/src/lib/utils/mem_ops.cpp @@ -6,8 +6,40 @@ #include +#if defined(BOTAN_HAS_LOCKING_ALLOCATOR) + #include +#endif + namespace Botan { +void* allocate_memory(size_t elems, size_t elem_size) + { +#if defined(BOTAN_HAS_LOCKING_ALLOCATOR) + if(void* p = mlock_allocator::instance().allocate(elems, elem_size)) + return p; +#endif + + void* ptr = std::calloc(elems, elem_size); + if(!ptr) + throw std::bad_alloc(); + return ptr; + } + +void deallocate_memory(void* p, size_t elems, size_t elem_size) + { + if(p == nullptr) + return; + + secure_scrub_memory(p, elems * elem_size); + +#if defined(BOTAN_HAS_LOCKING_ALLOCATOR) + if(mlock_allocator::instance().deallocate(p, elems, elem_size)) + return; +#endif + + std::free(p); + } + bool constant_time_compare(const uint8_t x[], const uint8_t y[], size_t len) diff --git a/src/lib/utils/mem_ops.h b/src/lib/utils/mem_ops.h index 328196000..4b03b23d2 100644 --- a/src/lib/utils/mem_ops.h +++ b/src/lib/utils/mem_ops.h @@ -14,6 +14,24 @@ namespace Botan { +/** +* Allocate a memory buffer by some method. This should only be used for +* primitive types (uint8_t, uint32_t, etc). +* +* @param elems the number of elements +* @param elem_size the size of each element +* @return pointer to allocated and zeroed memory, or throw std::bad_alloc on failure +*/ +BOTAN_PUBLIC_API(2,3) void* allocate_memory(size_t elems, size_t elem_size); + +/** +* Free a pointer returned by allocate_memory +* @param p the pointer returned by allocate_memory +* @param elems the number of elements, as passed to allocate_memory +* @param elem_size the size of each element, as passed to allocate_memory +*/ +BOTAN_PUBLIC_API(2,3) void deallocate_memory(void* p, size_t elems, size_t elem_size); + /** * Scrub memory contents in a way that a compiler should not elide, * using some system specific technique. Note that this function might -- cgit v1.2.3