/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 | 1.31M | { |
29 | 1.31M | T* allocation = static_cast<T*>(LockedPoolManager::Instance().alloc(sizeof(T) * n)); |
30 | 1.31M | if (!allocation) { Branch (30:13): [True: 0, False: 654k]
Branch (30:13): [True: 0, False: 0]
Branch (30:13): [True: 0, False: 643k]
Branch (30:13): [True: 0, False: 11.0k]
Branch (30:13): [True: 0, False: 11.0k]
|
31 | 0 | throw std::bad_alloc(); |
32 | 0 | } |
33 | 1.31M | return allocation; |
34 | 1.31M | } secure_allocator<std::array<unsigned char, 32ul> >::allocate(unsigned long) Line | Count | Source | 28 | 654k | { | 29 | 654k | T* allocation = static_cast<T*>(LockedPoolManager::Instance().alloc(sizeof(T) * n)); | 30 | 654k | if (!allocation) { Branch (30:13): [True: 0, False: 654k]
| 31 | 0 | throw std::bad_alloc(); | 32 | 0 | } | 33 | 654k | return allocation; | 34 | 654k | } |
Unexecuted instantiation: secure_allocator<std::array<unsigned char, 96ul> >::allocate(unsigned long) secure_allocator<unsigned char>::allocate(unsigned long) Line | Count | Source | 28 | 643k | { | 29 | 643k | T* allocation = static_cast<T*>(LockedPoolManager::Instance().alloc(sizeof(T) * n)); | 30 | 643k | if (!allocation) { Branch (30:13): [True: 0, False: 643k]
| 31 | 0 | throw std::bad_alloc(); | 32 | 0 | } | 33 | 643k | return allocation; | 34 | 643k | } |
secure_allocator<char>::allocate(unsigned long) Line | Count | Source | 28 | 11.0k | { | 29 | 11.0k | T* allocation = static_cast<T*>(LockedPoolManager::Instance().alloc(sizeof(T) * n)); | 30 | 11.0k | if (!allocation) { Branch (30:13): [True: 0, False: 11.0k]
| 31 | 0 | throw std::bad_alloc(); | 32 | 0 | } | 33 | 11.0k | return allocation; | 34 | 11.0k | } |
random.cpp:secure_allocator<(anonymous namespace)::RNGState>::allocate(unsigned long) Line | Count | Source | 28 | 11.0k | { | 29 | 11.0k | T* allocation = static_cast<T*>(LockedPoolManager::Instance().alloc(sizeof(T) * n)); | 30 | 11.0k | if (!allocation) { Branch (30:13): [True: 0, False: 11.0k]
| 31 | 0 | throw std::bad_alloc(); | 32 | 0 | } | 33 | 11.0k | return allocation; | 34 | 11.0k | } |
|
35 | | |
36 | | void deallocate(T* p, std::size_t n) |
37 | 1.31M | { |
38 | 1.31M | if (p != nullptr) { Branch (38:13): [True: 654k, False: 0]
Branch (38:13): [True: 0, False: 0]
Branch (38:13): [True: 643k, False: 0]
Branch (38:13): [True: 11.0k, False: 0]
Branch (38:13): [True: 11.0k, False: 0]
|
39 | 1.31M | memory_cleanse(p, sizeof(T) * n); |
40 | 1.31M | } |
41 | 1.31M | LockedPoolManager::Instance().free(p); |
42 | 1.31M | } secure_allocator<std::array<unsigned char, 32ul> >::deallocate(std::array<unsigned char, 32ul>*, unsigned long) Line | Count | Source | 37 | 654k | { | 38 | 654k | if (p != nullptr) { Branch (38:13): [True: 654k, False: 0]
| 39 | 654k | memory_cleanse(p, sizeof(T) * n); | 40 | 654k | } | 41 | 654k | LockedPoolManager::Instance().free(p); | 42 | 654k | } |
Unexecuted instantiation: secure_allocator<std::array<unsigned char, 96ul> >::deallocate(std::array<unsigned char, 96ul>*, unsigned long) secure_allocator<unsigned char>::deallocate(unsigned char*, unsigned long) Line | Count | Source | 37 | 643k | { | 38 | 643k | if (p != nullptr) { Branch (38:13): [True: 643k, False: 0]
| 39 | 643k | memory_cleanse(p, sizeof(T) * n); | 40 | 643k | } | 41 | 643k | LockedPoolManager::Instance().free(p); | 42 | 643k | } |
secure_allocator<char>::deallocate(char*, unsigned long) Line | Count | Source | 37 | 11.0k | { | 38 | 11.0k | if (p != nullptr) { Branch (38:13): [True: 11.0k, False: 0]
| 39 | 11.0k | memory_cleanse(p, sizeof(T) * n); | 40 | 11.0k | } | 41 | 11.0k | LockedPoolManager::Instance().free(p); | 42 | 11.0k | } |
random.cpp:secure_allocator<(anonymous namespace)::RNGState>::deallocate((anonymous namespace)::RNGState*, unsigned long) Line | Count | Source | 37 | 11.0k | { | 38 | 11.0k | if (p != nullptr) { Branch (38:13): [True: 11.0k, False: 0]
| 39 | 11.0k | memory_cleanse(p, sizeof(T) * n); | 40 | 11.0k | } | 41 | 11.0k | LockedPoolManager::Instance().free(p); | 42 | 11.0k | } |
|
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: bool operator!=<char>(secure_allocator<char> const&, secure_allocator<char> const&) Unexecuted instantiation: bool operator!=<unsigned char>(secure_allocator<unsigned char> const&, secure_allocator<unsigned char> const&) |
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 | 654k | void operator()(T* t) noexcept { |
63 | 654k | secure_allocator<T>().deallocate(t, 1); |
64 | 654k | } SecureUniqueDeleter<std::array<unsigned char, 32ul> >::operator()(std::array<unsigned char, 32ul>*) Line | Count | Source | 62 | 654k | void operator()(T* t) noexcept { | 63 | 654k | secure_allocator<T>().deallocate(t, 1); | 64 | 654k | } |
Unexecuted instantiation: SecureUniqueDeleter<std::array<unsigned char, 96ul> >::operator()(std::array<unsigned char, 96ul>*) |
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 | 654k | { |
73 | 654k | T* p = secure_allocator<T>().allocate(1); |
74 | | |
75 | | // initialize in place, and return as secure_unique_ptr |
76 | 654k | try { |
77 | 654k | return secure_unique_ptr<T>(new (p) T(std::forward<Args>(as)...)); |
78 | 654k | } catch (...) { |
79 | 0 | secure_allocator<T>().deallocate(p, 1); |
80 | 0 | throw; |
81 | 0 | } |
82 | 654k | } std::unique_ptr<std::array<unsigned char, 32ul>, SecureUniqueDeleter<std::array<unsigned char, 32ul> > > make_secure_unique<std::array<unsigned char, 32ul>>() Line | Count | Source | 72 | 654k | { | 73 | 654k | T* p = secure_allocator<T>().allocate(1); | 74 | | | 75 | | // initialize in place, and return as secure_unique_ptr | 76 | 654k | try { | 77 | 654k | return secure_unique_ptr<T>(new (p) T(std::forward<Args>(as)...)); | 78 | 654k | } catch (...) { | 79 | 0 | secure_allocator<T>().deallocate(p, 1); | 80 | 0 | throw; | 81 | 0 | } | 82 | 654k | } |
Unexecuted instantiation: std::unique_ptr<std::array<unsigned char, 96ul>, SecureUniqueDeleter<std::array<unsigned char, 96ul> > > make_secure_unique<std::array<unsigned char, 96ul>>() |
83 | | |
84 | | #endif // BITCOIN_SUPPORT_ALLOCATORS_SECURE_H |