/root/bitcoin/src/support/allocators/secure.h
| Line | Count | Source | 
| 1 |  | // Copyright (c) 2009-2010 Satoshi Nakamoto | 
| 2 |  | // Copyright (c) 2009-present The Bitcoin Core developers | 
| 3 |  | // Distributed under the MIT software license, see the accompanying | 
| 4 |  | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | 
| 5 |  |  | 
| 6 |  | #ifndef BITCOIN_SUPPORT_ALLOCATORS_SECURE_H | 
| 7 |  | #define BITCOIN_SUPPORT_ALLOCATORS_SECURE_H | 
| 8 |  |  | 
| 9 |  | #include <support/lockedpool.h> | 
| 10 |  | #include <support/cleanse.h> | 
| 11 |  |  | 
| 12 |  | #include <memory> | 
| 13 |  | #include <string> | 
| 14 |  |  | 
| 15 |  | // | 
| 16 |  | // Allocator that locks its contents from being paged | 
| 17 |  | // out of memory and clears its contents before deletion. | 
| 18 |  | // | 
| 19 |  | template <typename T> | 
| 20 |  | struct secure_allocator { | 
| 21 |  |     using value_type = T; | 
| 22 |  |  | 
| 23 |  |     secure_allocator() = default; | 
| 24 |  |     template <typename U> | 
| 25 |  |     secure_allocator(const secure_allocator<U>&) noexcept {} | 
| 26 |  |  | 
| 27 |  |     T* allocate(std::size_t n) | 
| 28 | 0 |     { | 
| 29 | 0 |         T* allocation = static_cast<T*>(LockedPoolManager::Instance().alloc(sizeof(T) * n)); | 
| 30 | 0 |         if (!allocation) { | 
| 31 | 0 |             throw std::bad_alloc(); | 
| 32 | 0 |         } | 
| 33 | 0 |         return allocation; | 
| 34 | 0 |     } Unexecuted instantiation: _ZN16secure_allocatorISt5arrayIhLm32EEE8allocateEmUnexecuted instantiation: _ZN16secure_allocatorISt5arrayIhLm96EEE8allocateEmUnexecuted instantiation: _ZN16secure_allocatorIhE8allocateEmUnexecuted instantiation: _ZN16secure_allocatorIcE8allocateEmUnexecuted instantiation: random.cpp:_ZN16secure_allocatorIN12_GLOBAL__N_18RNGStateEE8allocateEm | 
| 35 |  |  | 
| 36 |  |     void deallocate(T* p, std::size_t n) | 
| 37 | 0 |     { | 
| 38 | 0 |         if (p != nullptr) { | 
| 39 | 0 |             memory_cleanse(p, sizeof(T) * n); | 
| 40 | 0 |         } | 
| 41 | 0 |         LockedPoolManager::Instance().free(p); | 
| 42 | 0 |     } Unexecuted instantiation: _ZN16secure_allocatorISt5arrayIhLm32EEE10deallocateEPS1_mUnexecuted instantiation: _ZN16secure_allocatorISt5arrayIhLm96EEE10deallocateEPS1_mUnexecuted instantiation: _ZN16secure_allocatorIhE10deallocateEPhmUnexecuted instantiation: _ZN16secure_allocatorIcE10deallocateEPcmUnexecuted instantiation: random.cpp:_ZN16secure_allocatorIN12_GLOBAL__N_18RNGStateEE10deallocateEPS1_m | 
| 43 |  |  | 
| 44 |  |     template <typename U> | 
| 45 |  |     friend bool operator==(const secure_allocator&, const secure_allocator<U>&) noexcept | 
| 46 |  |     { | 
| 47 |  |         return true; | 
| 48 |  |     } | 
| 49 |  |     template <typename U> | 
| 50 |  |     friend bool operator!=(const secure_allocator&, const secure_allocator<U>&) noexcept | 
| 51 | 0 |     { | 
| 52 | 0 |         return false; | 
| 53 | 0 |     } Unexecuted instantiation: _ZneIhEbRK16secure_allocatorIhERKS0_IT_EUnexecuted instantiation: _ZneIcEbRK16secure_allocatorIcERKS0_IT_E | 
| 54 |  | }; | 
| 55 |  |  | 
| 56 |  | // This is exactly like std::string, but with a custom allocator. | 
| 57 |  | // TODO: Consider finding a way to make incoming RPC request.params[i] mlock()ed as well | 
| 58 |  | typedef std::basic_string<char, std::char_traits<char>, secure_allocator<char> > SecureString; | 
| 59 |  |  | 
| 60 |  | template<typename T> | 
| 61 |  | struct SecureUniqueDeleter { | 
| 62 | 0 |     void operator()(T* t) noexcept { | 
| 63 | 0 |         secure_allocator<T>().deallocate(t, 1); | 
| 64 | 0 |     } Unexecuted instantiation: _ZN19SecureUniqueDeleterISt5arrayIhLm32EEEclEPS1_Unexecuted instantiation: _ZN19SecureUniqueDeleterISt5arrayIhLm96EEEclEPS1_ | 
| 65 |  | }; | 
| 66 |  |  | 
| 67 |  | template<typename T> | 
| 68 |  | using secure_unique_ptr = std::unique_ptr<T, SecureUniqueDeleter<T>>; | 
| 69 |  |  | 
| 70 |  | template<typename T, typename... Args> | 
| 71 |  | secure_unique_ptr<T> make_secure_unique(Args&&... as) | 
| 72 | 0 | { | 
| 73 | 0 |     T* p = secure_allocator<T>().allocate(1); | 
| 74 |  |  | 
| 75 |  |     // initialize in place, and return as secure_unique_ptr | 
| 76 | 0 |     try { | 
| 77 | 0 |         return secure_unique_ptr<T>(new (p) T(std::forward<Args>(as)...)); | 
| 78 | 0 |     } catch (...) { | 
| 79 | 0 |         secure_allocator<T>().deallocate(p, 1); | 
| 80 | 0 |         throw; | 
| 81 | 0 |     } | 
| 82 | 0 | } Unexecuted instantiation: _Z18make_secure_uniqueISt5arrayIhLm32EEJEESt10unique_ptrIT_19SecureUniqueDeleterIS3_EEDpOT0_Unexecuted instantiation: _Z18make_secure_uniqueISt5arrayIhLm96EEJEESt10unique_ptrIT_19SecureUniqueDeleterIS3_EEDpOT0_ | 
| 83 |  |  | 
| 84 |  | #endif // BITCOIN_SUPPORT_ALLOCATORS_SECURE_H |