Bring over a new version of the secure memory code from gcr

* This allows libraries to share the pool if they have the same
   version of the secure memory code.
This commit is contained in:
Stef Walter 2012-10-27 11:58:13 +02:00
parent 275d314d57
commit 7cea18071b
6 changed files with 301 additions and 290 deletions

View File

@ -1,25 +1,25 @@
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* egg-secure-memory.h - library for allocating memory that is non-pageable
*
* Copyright (C) 2007 Stefan Walter
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This program 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* Author: Stef Walter <stef@thewalter.net>
*/
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>
*/
/*
* IMPORTANT: This is pure vanila standard C, no glib. We need this
@ -66,12 +66,12 @@
#endif
#define DO_LOCK() \
egg_memory_lock ();
EGG_SECURE_GLOBALS.lock ();
#define DO_UNLOCK() \
egg_memory_unlock ();
EGG_SECURE_GLOBALS.unlock ();
static int lock_warning = 1;
static int show_warning = 1;
int egg_secure_warnings = 1;
/*
@ -163,17 +163,25 @@ typedef struct _Pool {
Item items[1]; /* Actual items hang off here */
} Pool;
static Pool *all_pools = NULL;
static void*
static void *
pool_alloc (void)
{
Pool *pool;
void *pages, *item;
size_t len, i;
if (!EGG_SECURE_GLOBALS.pool_version ||
strcmp (EGG_SECURE_GLOBALS.pool_version, EGG_SECURE_POOL_VER_STR) != 0) {
if (show_warning && egg_secure_warnings)
fprintf (stderr, "the secure memory pool version does not match the code '%s' != '%s'\n",
EGG_SECURE_GLOBALS.pool_version ? EGG_SECURE_GLOBALS.pool_version : "(null)",
EGG_SECURE_POOL_VER_STR);
show_warning = 0;
return NULL;
}
/* A pool with an available item */
for (pool = all_pools; pool; pool = pool->next) {
for (pool = EGG_SECURE_GLOBALS.pool_data; pool; pool = pool->next) {
if (unused_peek (&pool->unused))
break;
}
@ -187,8 +195,8 @@ pool_alloc (void)
/* Fill in the block header, and inlude in block list */
pool = pages;
pool->next = all_pools;
all_pools = pool;
pool->next = EGG_SECURE_GLOBALS.pool_data;
EGG_SECURE_GLOBALS.pool_data = pool;
pool->length = len;
pool->used = 0;
pool->unused = NULL;
@ -223,7 +231,8 @@ pool_free (void* item)
ptr = item;
/* Find which block this one belongs to */
for (at = &all_pools, pool = *at; pool; at = &pool->next, pool = *at) {
for (at = (Pool **)&EGG_SECURE_GLOBALS.pool_data, pool = *at;
pool != NULL; at = &pool->next, pool = *at) {
beg = (char*)pool->items;
end = (char*)pool + pool->length - sizeof (Item);
if (ptr >= beg && ptr <= end) {
@ -270,7 +279,7 @@ pool_valid (void* item)
ptr = item;
/* Find which block this one belongs to */
for (pool = all_pools; pool; pool = pool->next) {
for (pool = EGG_SECURE_GLOBALS.pool_data; pool; pool = pool->next) {
beg = (char*)pool->items;
end = (char*)pool + pool->length - sizeof (Item);
if (ptr >= beg && ptr <= end)
@ -591,39 +600,39 @@ sec_free (Block *block, void *memory)
/* Remove from the used cell ring */
sec_remove_cell_ring (&block->used_cells, cell);
/* Find previous unallocated neighbor, and merge if possible */
other = sec_neighbor_before (block, cell);
if (other && other->requested == 0) {
ASSERT (other->tag == NULL);
ASSERT (other->next && other->prev);
other->n_words += cell->n_words;
sec_write_guards (other);
pool_free (cell);
cell = other;
}
/* Find previous unallocated neighbor, and merge if possible */
other = sec_neighbor_before (block, cell);
if (other && other->requested == 0) {
ASSERT (other->tag == NULL);
ASSERT (other->next && other->prev);
other->n_words += cell->n_words;
sec_write_guards (other);
pool_free (cell);
cell = other;
}
/* Find next unallocated neighbor, and merge if possible */
other = sec_neighbor_after (block, cell);
if (other && other->requested == 0) {
ASSERT (other->tag == NULL);
ASSERT (other->next && other->prev);
other->n_words += cell->n_words;
other->words = cell->words;
if (cell->next)
sec_remove_cell_ring (&block->unused_cells, cell);
sec_write_guards (other);
pool_free (cell);
cell = other;
}
/* Find next unallocated neighbor, and merge if possible */
other = sec_neighbor_after (block, cell);
if (other && other->requested == 0) {
ASSERT (other->tag == NULL);
ASSERT (other->next && other->prev);
other->n_words += cell->n_words;
other->words = cell->words;
if (cell->next)
sec_remove_cell_ring (&block->unused_cells, cell);
sec_write_guards (other);
pool_free (cell);
cell = other;
}
/* Add to the unused list if not already there */
if (!cell->next)
sec_insert_cell_ring (&block->unused_cells, cell);
/* Add to the unused list if not already there */
if (!cell->next)
sec_insert_cell_ring (&block->unused_cells, cell);
cell->tag = NULL;
cell->requested = 0;
--block->n_used;
return NULL;
cell->tag = NULL;
cell->requested = 0;
--block->n_used;
return NULL;
}
static void
@ -858,18 +867,18 @@ sec_acquire_pages (size_t *sz,
#if defined(HAVE_MLOCK)
pages = mmap (0, *sz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
if (pages == MAP_FAILED) {
if (lock_warning && egg_secure_warnings)
if (show_warning && egg_secure_warnings)
fprintf (stderr, "couldn't map %lu bytes of memory (%s): %s\n",
(unsigned long)*sz, during_tag, strerror (errno));
lock_warning = 0;
show_warning = 0;
return NULL;
}
if (mlock (pages, *sz) < 0) {
if (lock_warning && egg_secure_warnings && errno != EPERM) {
if (show_warning && egg_secure_warnings && errno != EPERM) {
fprintf (stderr, "couldn't lock %lu bytes of memory (%s): %s\n",
(unsigned long)*sz, during_tag, strerror (errno));
lock_warning = 0;
show_warning = 0;
}
munmap (pages, *sz);
return NULL;
@ -877,13 +886,13 @@ sec_acquire_pages (size_t *sz,
DEBUG_ALLOC ("gkr-secure-memory: new block ", *sz);
lock_warning = 1;
show_warning = 1;
return pages;
#else
if (lock_warning && egg_secure_warnings)
if (show_warning && egg_secure_warnings)
fprintf (stderr, "your system does not support private memory");
lock_warning = 0;
show_warning = 0;
return NULL;
#endif
@ -1050,8 +1059,8 @@ egg_secure_alloc_full (const char *tag,
DO_UNLOCK ();
if (!memory && (flags & EGG_SECURE_USE_FALLBACK)) {
memory = egg_memory_fallback (NULL, length);
if (!memory && (flags & EGG_SECURE_USE_FALLBACK) && EGG_SECURE_GLOBALS.fallback != NULL) {
memory = EGG_SECURE_GLOBALS.fallback (NULL, length);
if (memory) /* Our returned memory is always zeroed */
memset (memory, 0, length);
}
@ -1124,17 +1133,17 @@ egg_secure_realloc_full (const char *tag,
DO_UNLOCK ();
if (!block) {
if ((flags & EGG_SECURE_USE_FALLBACK)) {
if ((flags & EGG_SECURE_USE_FALLBACK) && EGG_SECURE_GLOBALS.fallback) {
/*
* In this case we can't zero the returned memory,
* because we don't know what the block size was.
*/
return egg_memory_fallback (memory, length);
return EGG_SECURE_GLOBALS.fallback (memory, length);
} else {
if (egg_secure_warnings)
fprintf (stderr, "memory does not belong to gnome-keyring: 0x%08lx\n",
fprintf (stderr, "memory does not belong to secure memory pool: 0x%08lx\n",
(unsigned long)memory);
ASSERT (0 && "memory does does not belong to gnome-keyring");
ASSERT (0 && "memory does does not belong to secure memory pool");
return NULL;
}
}
@ -1190,13 +1199,13 @@ egg_secure_free_full (void *memory, int flags)
DO_UNLOCK ();
if (!block) {
if ((flags & EGG_SECURE_USE_FALLBACK)) {
egg_memory_fallback (memory, 0);
if ((flags & EGG_SECURE_USE_FALLBACK) && EGG_SECURE_GLOBALS.fallback) {
EGG_SECURE_GLOBALS.fallback (memory, 0);
} else {
if (egg_secure_warnings)
fprintf (stderr, "memory does not belong to gnome-keyring: 0x%08lx\n",
fprintf (stderr, "memory does not belong to secure memory pool: 0x%08lx\n",
(unsigned long)memory);
ASSERT (0 && "memory does does not belong to gnome-keyring");
ASSERT (0 && "memory does does not belong to secure memory pool");
}
}
}

View File

@ -1,25 +1,25 @@
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* egg-secure-memory.h - library for allocating memory that is non-pageable
*
* Copyright (C) 2007 Stefan Walter
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This program 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* Author: Stef Walter <stef@thewalter.net>
*/
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>
*/
#ifndef EGG_SECURE_MEMORY_H
#define EGG_SECURE_MEMORY_H
@ -40,29 +40,31 @@
* secure memory between threads:
*/
extern void egg_memory_lock (void);
typedef struct {
void (* lock) (void);
void (* unlock) (void);
void * (* fallback) (void *pointer,
size_t length);
void * pool_data;
const char * pool_version;
} egg_secure_glob;
extern void egg_memory_unlock (void);
#define EGG_SECURE_POOL_VER_STR "1.0"
#define EGG_SECURE_GLOBALS SECMEM_pool_data_v1_0
/*
* Allocation Fallbacks
*
* If we cannot allocate secure memory, then this function
* (defined elsewhere) will be called which has a chance to
* allocate other memory abort or do whatever.
*
* Same call semantics as realloc with regard to NULL and zeros
*/
extern void* egg_memory_fallback (void *p, size_t length);
#define EGG_SECURE_DEFINE_GLOBALS(lock, unlock, fallback) \
egg_secure_glob EGG_SECURE_GLOBALS = { \
lock, unlock, fallback, NULL, EGG_SECURE_POOL_VER_STR };
#define EGG_SECURE_GLIB_DEFINITIONS() \
#define EGG_SECURE_DEFINE_GLIB_GLOBALS() \
static GStaticMutex memory_mutex = G_STATIC_MUTEX_INIT; \
void egg_memory_lock (void) \
static void egg_memory_lock (void) \
{ g_static_mutex_lock (&memory_mutex); } \
void egg_memory_unlock (void) \
static void egg_memory_unlock (void) \
{ g_static_mutex_unlock (&memory_mutex); } \
void* egg_memory_fallback (void *p, size_t sz) \
{ return g_realloc (p, sz); } \
EGG_SECURE_DEFINE_GLOBALS (egg_memory_lock, egg_memory_unlock, g_realloc);
extern egg_secure_glob EGG_SECURE_GLOBALS;
/*
* Main functionality

View File

@ -34,7 +34,7 @@
#include <glib.h>
#include <gcrypt.h>
EGG_SECURE_GLIB_DEFINITIONS ();
EGG_SECURE_DEFINE_GLIB_GLOBALS ();
static void
test_perform (void)

View File

@ -33,7 +33,7 @@
#include <gcrypt.h>
EGG_SECURE_GLIB_DEFINITIONS ();
EGG_SECURE_DEFINE_GLIB_GLOBALS ();
static void
test_hkdf_test_case_1 (void)

View File

@ -32,7 +32,7 @@
#include <string.h>
EGG_SECURE_GLIB_DEFINITIONS ();
EGG_SECURE_DEFINE_GLIB_GLOBALS ();
/* Declared in egg-secure-memory.c */
extern int egg_secure_warnings;
@ -182,26 +182,25 @@ test_multialloc (void)
case 0: /* Allocate some memory */
size = g_random_int_range (1, 16384);
data = egg_secure_alloc (size);
g_assert (data);
g_assert (data != NULL);
memset (data, 0xCAFEBABE, size);
g_ptr_array_add (memory, data);
break;
case 1: /* Reallocate some memory */
index = g_random_int_range (0, memory->len);
data = g_ptr_array_index (memory, index);
g_assert (data);
g_assert (data != NULL);
size = g_random_int_range (1, 16384);
data = egg_secure_realloc (data, size);
g_assert (data);
g_assert (data != NULL);
memset (data, 0xCAFEBABE, size);
g_ptr_array_index (memory, index) = data;
break;
case 2: /* Free some memory */
index = g_random_int_range (0, memory->len);
data = g_ptr_array_index (memory, index);
g_assert (data);
data = g_ptr_array_remove_index_fast (memory, index);
g_assert (data != NULL);
egg_secure_free (data);
g_ptr_array_remove_index_fast (memory, index);
break;
default:
g_assert_not_reached ();
@ -214,7 +213,8 @@ test_multialloc (void)
}
g_assert (memory->len == 0);
g_ptr_array_set_free_func (memory, egg_secure_free);
for (i = 0; i < memory->len; i++)
egg_secure_free (memory->pdata[i]);
g_ptr_array_free (memory, TRUE);
egg_secure_warnings = 1;

View File

@ -104,7 +104,7 @@
* during a secret_service_get() or secret_service_new() operation.
*/
EGG_SECURE_GLIB_DEFINITIONS ();
EGG_SECURE_DEFINE_GLIB_GLOBALS ();
GQuark _secret_error_quark = 0;