Debian Codegraph @

src/lib/data_mgr/SecureMemoryRegistry.cpp

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 SecureMemoryRegistry.cpp
29
30 Implements a singleton class that keeps track of all securely allocated
31 memory. This registry can be used to wipe securely allocated memory in case
32 of a fatal exception
33 *****************************************************************************/
34
35 #include <stdlib.h>
36 #include <string.h>
37 #include "log.h"
38 #include "SecureMemoryRegistry.h"
39
40 // Constructor
41 SecureMemoryRegistry::SecureMemoryRegistry()
42 {
43 SecMemRegistryMutex = MutexFactory::i()->getMutex();
44 }
45
46 // Destructor
47 SecureMemoryRegistry::~SecureMemoryRegistry()
48 {
49 if (!registry.empty())
50 {
51 ERROR_MSG("SecureMemoryRegistry is not empty: leak!");
52 }
53 MutexFactory::i()->recycleMutex(SecMemRegistryMutex);
54 }
55
56 // Return the one-and-only instance
57 SecureMemoryRegistry* SecureMemoryRegistry::i()
58 {
59 if (instance.get() == NULL)
60 {
61 instance.reset(new SecureMemoryRegistry());
62
63 if (instance.get() == NULL)
64 {
65 // This is very bad!
66 ERROR_MSG("failed to instantiate SecureMemoryRegistry");
67
68 }
69 }
70
71 return instance.get();
72 }
73
74 // This will destroy the one-and-only instance.
75 void SecureMemoryRegistry::reset()
76 {
77 instance.reset();
78 }
79
80 // Register a block of memory
81 void SecureMemoryRegistry::add(void* pointer, size_t blocksize)
82 {
83 MutexLocker lock(SecMemRegistryMutex);
84
85 registry[pointer] = blocksize;
86
87 //DEBUG_MSG("Registered block of %d bytes at 0x%x", blocksize, pointer);
88 }
89
90 // Unregister a block of memory
91 size_t SecureMemoryRegistry::remove(void* pointer)
92 {
93 //DEBUG_MSG("Unregistered block of %d bytes at 0x%x", registry[pointer], pointer);
94
95 MutexLocker lock(SecMemRegistryMutex);
96
97 size_t rv = registry[pointer];
98
99 registry.erase(pointer);
100
101 return rv;
102 }
103
104 // Wipe all registered blocks of memory
105 void SecureMemoryRegistry::wipe()
106 {
107 MutexLocker lock(SecMemRegistryMutex);
108
109 // Be very careful in this method to catch any weird exceptions that
110 // may occur since if we're in this method it means something has already
111 // gone pear shaped once before and we're exiting on a fatal exception
112 try
113 {
114 for (std::map<void*, size_t>::iterator i = registry.begin(); i != registry.end(); i++)
115 {
116 try
117 {
118 DEBUG_MSG("Wiping block of %d bytes at 0x%x", i->second, i->first);
119 }
120 catch (...)
121 {
122 }
123
124 try
125 {
126 #ifdef PARANOID
127 memset(i->first, 0xFF, i->second);
128 #endif // PARANOID
129 memset(i->first, 0x00, i->second);
130 }
131 catch (...)
132 {
133 ERROR_MSG("Failed to wipe block of %d bytes at 0x%x", i->second, i->first);
134 }
135 }
136 }
137 catch (...)
138 {
139 ERROR_MSG("Failed to enumerate the secure memory registry");
140 }
141 }
142