Bitcoin Core
31.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
support
allocators
secure.h
Go to the documentation of this file.
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
{
29
T
* allocation =
static_cast<
T
*
>
(
LockedPoolManager::Instance
().
alloc
(
sizeof
(
T
) * n));
30
if
(!allocation) {
31
throw
std::bad_alloc();
32
}
33
return
allocation;
34
}
35
36
void
deallocate
(
T
* p, std::size_t n)
37
{
38
if
(p !=
nullptr
) {
39
memory_cleanse
(p,
sizeof
(
T
) * n);
40
}
41
LockedPoolManager::Instance
().
free
(p);
42
}
43
44
template
<
typename
U>
45
friend
bool
operator==
(
const
secure_allocator
&,
const
secure_allocator<U>
&)
noexcept
46
{
47
return
true
;
48
}
49
};
50
51
// This is exactly like std::string, but with a custom allocator.
52
// TODO: Consider finding a way to make incoming RPC request.params[i] mlock()ed as well
53
typedef
std::basic_string<char, std::char_traits<char>,
secure_allocator<char>
>
SecureString
;
54
55
template
<
typename
T>
56
struct
SecureUniqueDeleter
{
57
void
operator()
(
T
*
t
)
noexcept
{
58
secure_allocator<T>
().
deallocate
(
t
, 1);
59
}
60
};
61
62
template
<
typename
T>
63
using
secure_unique_ptr
= std::unique_ptr<T, SecureUniqueDeleter<T>>;
64
65
template
<
typename
T
,
typename
... Args>
66
secure_unique_ptr<T>
make_secure_unique
(Args&&... as)
67
{
68
T
* p =
secure_allocator<T>
().
allocate
(1);
69
70
// initialize in place, and return as secure_unique_ptr
71
try
{
72
return
secure_unique_ptr<T>
(
new
(p)
T
(std::forward<Args>(as)...));
73
}
catch
(...) {
74
secure_allocator<T>
().
deallocate
(p, 1);
75
throw
;
76
}
77
}
78
79
#endif
// BITCOIN_SUPPORT_ALLOCATORS_SECURE_H
LockedPool::free
void free(void *ptr)
Free a previously allocated chunk of memory.
Definition
lockedpool.cpp:306
LockedPool::alloc
void * alloc(size_t size)
Allocate size bytes from this arena.
Definition
lockedpool.cpp:284
LockedPoolManager::Instance
static LockedPoolManager & Instance()
Return the current instance, or create it once.
Definition
lockedpool.cpp:403
memory_cleanse
void memory_cleanse(void *ptr, size_t len)
Secure overwrite a buffer (possibly containing secret data) with zero-bytes.
Definition
cleanse.cpp:14
cleanse.h
T
#define T(expected, seed, data)
lockedpool.h
SecureString
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition
secure.h:53
make_secure_unique
secure_unique_ptr< T > make_secure_unique(Args &&... as)
Definition
secure.h:66
secure_unique_ptr
std::unique_ptr< T, SecureUniqueDeleter< T > > secure_unique_ptr
Definition
secure.h:63
ByteUnit::t
@ t
Definition
strencodings.h:52
SecureUniqueDeleter
Definition
secure.h:56
SecureUniqueDeleter::operator()
void operator()(T *t) noexcept
Definition
secure.h:57
secure_allocator
Definition
secure.h:20
secure_allocator::operator==
friend bool operator==(const secure_allocator &, const secure_allocator< U > &) noexcept
Definition
secure.h:45
secure_allocator::secure_allocator
secure_allocator(const secure_allocator< U > &) noexcept
Definition
secure.h:25
secure_allocator::secure_allocator
secure_allocator()=default
secure_allocator::allocate
T * allocate(std::size_t n)
Definition
secure.h:27
secure_allocator::value_type
T value_type
Definition
secure.h:21
secure_allocator::deallocate
void deallocate(T *p, std::size_t n)
Definition
secure.h:36
Generated on
for Bitcoin Core by
1.17.0