1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* gnome-keyring-memory.c - library for allocating memory that is non-pageable
Copyright (C) 2007 Stefan Walter
The Gnome Keyring Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Gnome Keyring Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the Gnome Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Author: Stef Walter <stef@memberwebs.com>
*/
#include "config.h"
#include "gnome-keyring-memory.h"
#include "gnome-keyring-private.h"
#include "egg/egg-secure-memory.h"
#include <glib.h>
#include <string.h>
static GStaticMutex memory_mutex = G_STATIC_MUTEX_INIT;
#define WARNING "couldn't allocate secure memory to keep passwords " \
"and or keys from being written to the disk"
#define ABORTMSG "The GNOME_KEYRING_PARANOID environment variable was set. " \
"Exiting..."
/*
* These are called from gkr-secure-memory.c to provide appropriate
* locking for memory between threads
*/
void
egg_memory_lock (void)
{
g_static_mutex_lock (&memory_mutex);
}
void
egg_memory_unlock (void)
{
g_static_mutex_unlock (&memory_mutex);
}
void*
egg_memory_fallback (void *p, size_t sz)
{
const gchar *env;
/* We were asked to free memory */
if (!sz) {
g_free (p);
return NULL;
}
/* We were asked to allocate */
if (!p) {
env = g_getenv ("GNOME_KEYRING_PARANOID");
if (env && *env) {
g_message (WARNING);
g_error (ABORTMSG);
}
return g_malloc0 (sz);
}
/*
* Reallocation is a bit of a gray area, as we can be asked
* by external libraries (like libgcrypt) to reallocate a
* non-secure block into secure memory. We cannot satisfy
* this request (as we don't know the size of the original
* block) so we just try our best here.
*/
return g_realloc (p, sz);
}
/* -----------------------------------------------------------------------------
* PUBLIC FUNCTIONS
*/
/**
* gnome_keyring_memory_alloc:
* @sz: The new desired size of the memory block.
*
* Allocate a block of gnome-keyring non-pageable memory.
*
* If non-pageable memory cannot be allocated then normal memory will be
* returned.
*
* Return value: The new memory block which should be freed with
* gnome_keyring_memory_free()
**/
gpointer
gnome_keyring_memory_alloc (gulong sz)
{
gpointer p;
/* Try to allocate secure memory */
p = egg_secure_alloc_full (sz, GKR_SECURE_USE_FALLBACK);
/* Our fallback will always allocate */
g_assert (p);
return p;
}
/**
* gnome_keyring_memory_try_alloc:
* @sz: The new desired size of the memory block.
*
* Allocate a block of gnome-keyring non-pageable memory.
*
* If non-pageable memory cannot be allocated, then NULL is returned.
*
* Return value: The new block, or NULL if memory cannot be allocated.
* The memory block should be freed with gnome_keyring_memory_free()
*/
gpointer
gnome_keyring_memory_try_alloc (gulong sz)
{
return egg_secure_alloc_full (sz, 0);
}
/**
* gnome_keyring_memory_realloc:
* @p: The pointer to reallocate or NULL to allocate a new block.
* @sz: The new desired size of the memory block, or 0 to free the memory.
*
* Reallocate a block of gnome-keyring non-pageable memory.
*
* Glib memory is also reallocated correctly. If called with a null pointer,
* then a new block of memory is allocated. If called with a zero size,
* then the block of memory is freed.
*
* If non-pageable memory cannot be allocated then normal memory will be
* returned.
*
* Return value: The new block, or NULL if the block was freed.
* The memory block should be freed with gnome_keyring_memory_free()
*/
gpointer
gnome_keyring_memory_realloc (gpointer p, gulong sz)
{
gpointer n;
if (!p) {
return gnome_keyring_memory_alloc (sz);
} else if (!sz) {
gnome_keyring_memory_free (p);
return NULL;
} else if (!egg_secure_check (p)) {
return g_realloc (p, sz);
}
/* First try and ask secure memory to reallocate */
n = egg_secure_realloc_full (p, sz, GKR_SECURE_USE_FALLBACK);
g_assert (n);
return n;
}
/**
* gnome_keyring_memory_try_realloc:
* @p: The pointer to reallocate or NULL to allocate a new block.
* @sz: The new desired size of the memory block.
*
* Reallocate a block of gnome-keyring non-pageable memory.
*
* Glib memory is also reallocated correctly when passed to this function.
* If called with a null pointer, then a new block of memory is allocated.
* If called with a zero size, then the block of memory is freed.
*
* If memory cannot be allocated, NULL is returned and the original block
* of memory remains intact.
*
* Return value: The new block, or NULL if memory cannot be allocated.
* The memory block should be freed with gnome_keyring_memory_free()
*/
gpointer
gnome_keyring_memory_try_realloc (gpointer p, gulong sz)
{
gpointer n;
if (!p) {
return gnome_keyring_memory_try_alloc (sz);
} else if (!sz) {
gnome_keyring_memory_free (p);
return NULL;
} else if (!egg_secure_check (p)) {
return g_try_realloc (p, sz);
}
/* First try and ask secure memory to reallocate */
n = egg_secure_realloc_full (p, sz, 0);
g_assert (n);
return n;
}
/**
* gnome_keyring_memory_free:
* @p: The pointer to the beginning of the block of memory to free.
*
* Free a block of gnome-keyring non-pageable memory.
*
* Glib memory is also freed correctly when passed to this function. If called
* with a null pointer then no action is taken.
*/
void
gnome_keyring_memory_free (gpointer p)
{
if (!p)
return;
egg_secure_free_full (p, GKR_SECURE_USE_FALLBACK);
}
/**
* gnome_keyring_memory_is_secure:
* @p: The pointer to check
*
* Check if a pointer is in non-pageable memory allocated by gnome-keyring.
*
* Return value: Whether the memory is non-pageable or not
*/
gboolean
gnome_keyring_memory_is_secure (gpointer p)
{
return egg_secure_check (p) ? TRUE : FALSE;
}
/**
* gnome_keyring_memory_strdup:
* @str: The null terminated string to copy
*
* Copy a string into non-pageable memory. If the input string is %NULL, then
* %NULL will be returned.
*
* Return value: The copied string, should be freed with gnome_keyring_memory_free()
*/
gchar*
gnome_keyring_memory_strdup (const gchar* str)
{
return egg_secure_strdup (str);
}
|