Debian Codegraph @

src/lib/data_mgr/SecureAllocator.h

raw
1 /*
2 * Copyright (c) 2010 SURFnet bv
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*****************************************************************************
28 SecureAllocator.h
29
30 Implements a template class for a secure C++ allocator. The allocator will
31 zero all the memory it allocates before releasing it to ensure that the
32 data stored in the memory is destroyed properly to minimise the risk of
33 obtaining sensitive data from memory
34 *****************************************************************************/
35
36 #ifndef _SOFTHSM_V2_SECUREALLOCATOR_H
37 #define _SOFTHSM_V2_SECUREALLOCATOR_H
38
39 #include <limits>
40 #include <stdlib.h>
41 #include <string.h>
42 #if defined(SENSITIVE_NON_PAGED) && !defined(_WIN32)
43 #include <sys/mman.h>
44 #endif // SENSITIVE_NON_PAGED
45 #include "config.h"
46 #include "log.h"
47 #include "SecureMemoryRegistry.h"
48
49 template<class T> class SecureAllocator
50 {
51 public:
52 // Member types
53 typedef T value_type;
54 typedef T* pointer;
55 typedef T& reference;
56 typedef const T* const_pointer;
57 typedef const T& const_reference;
58 typedef size_t size_type;
59 typedef ptrdiff_t difference_type;
60
61 // Rebind to another type
62 template<class U> struct rebind
63 {
64 typedef SecureAllocator<U> other;
65 };
66
67 // Constructor
68 inline SecureAllocator() { }
69
70 inline SecureAllocator(const SecureAllocator&) { }
71
72 template<class U> SecureAllocator(const SecureAllocator<U>&) { }
73
74 // Destructor
75 inline virtual ~SecureAllocator() { }
76
77 // Return the maximum allocation size
78 size_type max_size() const
79 {
80 return std::numeric_limits<std::size_t>::max() / sizeof(T);
81 }
82
83 // Return the address of values
84 inline pointer address(reference value) const
85 {
86 return &value;
87 }
88
89 inline const_pointer address(const_reference value) const
90 {
91 return &value;
92 }
93
94 // Allocate n elements of type T
95 inline pointer allocate(size_type n, const void* = NULL)
96 {
97 #ifdef SENSITIVE_NON_PAGED
98 // Allocate memory on a page boundary
99 #ifndef _WIN32
100 pointer r = (pointer) valloc(n * sizeof(T));
101 #else
102 pointer r = (pointer) VirtualAlloc(NULL, n * sizeof(T), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
103 #endif
104
105 if (r == NULL)
106 {
107 ERROR_MSG("Out of memory");
108
109 return NULL;
110 }
111
112 // Lock the memory so it doesn't get swapped out
113 #ifndef _WIN32
114 if (mlock((const void*) r, n * sizeof(T)) != 0)
115 #else
116 if (VirtualLock((const void*) r, n * sizeof(T)) == 0)
117 #endif
118 {
119 ERROR_MSG("Could not allocate non-paged memory for secure storage");
120
121 // Hmmm... best to not return any allocated space in this case
122 #ifndef _WIN32
123 free(r);
124 #else
125 VirtualFree((const void*) r, MEM_RELEASE);
126 #endif
127
128 return NULL;
129 }
130
131 // Register the memory in the secure memory registry
132 SecureMemoryRegistry::i()->add(r, n * sizeof(T));
133
134 return r;
135 #else
136 pointer r = (pointer)(::operator new(n * sizeof(T)));
137
138 if (r == NULL)
139 {
140 ERROR_MSG("Out of memory");
141
142 return NULL;
143 }
144
145 // Register the memory in the secure memory registry
146 SecureMemoryRegistry::i()->add(r, n * sizeof(T));
147
148 return r;
149 #endif // SENSITIVE_NON_PAGED
150 }
151
152 // Deallocate n elements of type T
153 inline void deallocate(pointer p, size_type n)
154 {
155 #ifdef PARANOID
156 // First toggle all bits on
157 memset(p, 0xFF, n * sizeof(T));
158 #endif // PARANOID
159
160 // Toggle all bits off
161 memset(p, 0x00, n * sizeof(T));
162
163 // Unregister the memory from the secure memory registry
164 SecureMemoryRegistry::i()->remove(p);
165
166 #ifdef SENSITIVE_NON_PAGED
167 #ifndef _WIN32
168 munlock((const void*) p, n * sizeof(T));
169 #else
170 VirtualUnlock((const void*) p, n * sizeof(T));
171 #endif
172
173 #ifndef _WIN32
174 free(p);
175 #else
176 VirtualFree((const void*) r, MEM_RELEASE);
177 #endif
178 #else
179 // Release the memory
180 ::operator delete((void*) p);
181 #endif // SENSITIVE_NON_PAGED
182 }
183
184 // Initialise allocate storage with a value
185 void construct(pointer p, const T& value)
186 {
187 new((void*) p)T(value);
188 }
189
190 // Destroy elements of initialised storage
191 void destroy(pointer p)
192 {
193 // Call destructor
194 p->~T();
195 }
196
197 // Comparison operators
198 inline bool operator==(SecureAllocator const&) const { return true; }
199 inline bool operator!=(SecureAllocator const&) const { return false; }
200 };
201
202 #endif // !_SOFTHSM_V2_SECUREALLOCATOR_H
203