mirror of
https://gitlab.gnome.org/GNOME/libsecret.git
synced 2024-12-22 12:48:51 +00:00
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:
parent
275d314d57
commit
7cea18071b
@ -1,25 +1,25 @@
|
|||||||
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
|
/* -*- 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
|
/* egg-secure-memory.h - library for allocating memory that is non-pageable
|
||||||
*
|
|
||||||
* Copyright (C) 2007 Stefan Walter
|
Copyright (C) 2007 Stefan Walter
|
||||||
*
|
|
||||||
* This program is free software; you can redistribute it and/or modify
|
The Gnome Keyring Library is free software; you can redistribute it and/or
|
||||||
* it under the terms of the GNU Lesser General Public License as
|
modify it under the terms of the GNU Library General Public License as
|
||||||
* published by the Free Software Foundation; either version 2.1 of
|
published by the Free Software Foundation; either version 2 of the
|
||||||
* the License, or (at your option) any later version.
|
License, or (at your option) any later version.
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful, but
|
The Gnome Keyring Library is distributed in the hope that it will be useful,
|
||||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
* Lesser General Public License for more details.
|
Library General Public License for more details.
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU Lesser General Public
|
You should have received a copy of the GNU Library General Public
|
||||||
* License along with this program; if not, write to the Free Software
|
License along with the Gnome Library; see the file COPYING.LIB. If not,
|
||||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||||
* MA 02110-1301 USA
|
Boston, MA 02111-1307, USA.
|
||||||
*
|
|
||||||
* Author: Stef Walter <stef@thewalter.net>
|
Author: Stef Walter <stef@memberwebs.com>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* IMPORTANT: This is pure vanila standard C, no glib. We need this
|
* IMPORTANT: This is pure vanila standard C, no glib. We need this
|
||||||
@ -66,12 +66,12 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define DO_LOCK() \
|
#define DO_LOCK() \
|
||||||
egg_memory_lock ();
|
EGG_SECURE_GLOBALS.lock ();
|
||||||
|
|
||||||
#define DO_UNLOCK() \
|
#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;
|
int egg_secure_warnings = 1;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -163,17 +163,25 @@ typedef struct _Pool {
|
|||||||
Item items[1]; /* Actual items hang off here */
|
Item items[1]; /* Actual items hang off here */
|
||||||
} Pool;
|
} Pool;
|
||||||
|
|
||||||
static Pool *all_pools = NULL;
|
static void *
|
||||||
|
|
||||||
static void*
|
|
||||||
pool_alloc (void)
|
pool_alloc (void)
|
||||||
{
|
{
|
||||||
Pool *pool;
|
Pool *pool;
|
||||||
void *pages, *item;
|
void *pages, *item;
|
||||||
size_t len, i;
|
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 */
|
/* 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))
|
if (unused_peek (&pool->unused))
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -187,8 +195,8 @@ pool_alloc (void)
|
|||||||
|
|
||||||
/* Fill in the block header, and inlude in block list */
|
/* Fill in the block header, and inlude in block list */
|
||||||
pool = pages;
|
pool = pages;
|
||||||
pool->next = all_pools;
|
pool->next = EGG_SECURE_GLOBALS.pool_data;
|
||||||
all_pools = pool;
|
EGG_SECURE_GLOBALS.pool_data = pool;
|
||||||
pool->length = len;
|
pool->length = len;
|
||||||
pool->used = 0;
|
pool->used = 0;
|
||||||
pool->unused = NULL;
|
pool->unused = NULL;
|
||||||
@ -223,7 +231,8 @@ pool_free (void* item)
|
|||||||
ptr = item;
|
ptr = item;
|
||||||
|
|
||||||
/* Find which block this one belongs to */
|
/* 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;
|
beg = (char*)pool->items;
|
||||||
end = (char*)pool + pool->length - sizeof (Item);
|
end = (char*)pool + pool->length - sizeof (Item);
|
||||||
if (ptr >= beg && ptr <= end) {
|
if (ptr >= beg && ptr <= end) {
|
||||||
@ -270,7 +279,7 @@ pool_valid (void* item)
|
|||||||
ptr = item;
|
ptr = item;
|
||||||
|
|
||||||
/* Find which block this one belongs to */
|
/* 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;
|
beg = (char*)pool->items;
|
||||||
end = (char*)pool + pool->length - sizeof (Item);
|
end = (char*)pool + pool->length - sizeof (Item);
|
||||||
if (ptr >= beg && ptr <= end)
|
if (ptr >= beg && ptr <= end)
|
||||||
@ -858,18 +867,18 @@ sec_acquire_pages (size_t *sz,
|
|||||||
#if defined(HAVE_MLOCK)
|
#if defined(HAVE_MLOCK)
|
||||||
pages = mmap (0, *sz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
|
pages = mmap (0, *sz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
|
||||||
if (pages == MAP_FAILED) {
|
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",
|
fprintf (stderr, "couldn't map %lu bytes of memory (%s): %s\n",
|
||||||
(unsigned long)*sz, during_tag, strerror (errno));
|
(unsigned long)*sz, during_tag, strerror (errno));
|
||||||
lock_warning = 0;
|
show_warning = 0;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mlock (pages, *sz) < 0) {
|
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",
|
fprintf (stderr, "couldn't lock %lu bytes of memory (%s): %s\n",
|
||||||
(unsigned long)*sz, during_tag, strerror (errno));
|
(unsigned long)*sz, during_tag, strerror (errno));
|
||||||
lock_warning = 0;
|
show_warning = 0;
|
||||||
}
|
}
|
||||||
munmap (pages, *sz);
|
munmap (pages, *sz);
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -877,13 +886,13 @@ sec_acquire_pages (size_t *sz,
|
|||||||
|
|
||||||
DEBUG_ALLOC ("gkr-secure-memory: new block ", *sz);
|
DEBUG_ALLOC ("gkr-secure-memory: new block ", *sz);
|
||||||
|
|
||||||
lock_warning = 1;
|
show_warning = 1;
|
||||||
return pages;
|
return pages;
|
||||||
|
|
||||||
#else
|
#else
|
||||||
if (lock_warning && egg_secure_warnings)
|
if (show_warning && egg_secure_warnings)
|
||||||
fprintf (stderr, "your system does not support private memory");
|
fprintf (stderr, "your system does not support private memory");
|
||||||
lock_warning = 0;
|
show_warning = 0;
|
||||||
return NULL;
|
return NULL;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -1050,8 +1059,8 @@ egg_secure_alloc_full (const char *tag,
|
|||||||
|
|
||||||
DO_UNLOCK ();
|
DO_UNLOCK ();
|
||||||
|
|
||||||
if (!memory && (flags & EGG_SECURE_USE_FALLBACK)) {
|
if (!memory && (flags & EGG_SECURE_USE_FALLBACK) && EGG_SECURE_GLOBALS.fallback != NULL) {
|
||||||
memory = egg_memory_fallback (NULL, length);
|
memory = EGG_SECURE_GLOBALS.fallback (NULL, length);
|
||||||
if (memory) /* Our returned memory is always zeroed */
|
if (memory) /* Our returned memory is always zeroed */
|
||||||
memset (memory, 0, length);
|
memset (memory, 0, length);
|
||||||
}
|
}
|
||||||
@ -1124,17 +1133,17 @@ egg_secure_realloc_full (const char *tag,
|
|||||||
DO_UNLOCK ();
|
DO_UNLOCK ();
|
||||||
|
|
||||||
if (!block) {
|
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,
|
* In this case we can't zero the returned memory,
|
||||||
* because we don't know what the block size was.
|
* because we don't know what the block size was.
|
||||||
*/
|
*/
|
||||||
return egg_memory_fallback (memory, length);
|
return EGG_SECURE_GLOBALS.fallback (memory, length);
|
||||||
} else {
|
} else {
|
||||||
if (egg_secure_warnings)
|
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);
|
(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;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1190,13 +1199,13 @@ egg_secure_free_full (void *memory, int flags)
|
|||||||
DO_UNLOCK ();
|
DO_UNLOCK ();
|
||||||
|
|
||||||
if (!block) {
|
if (!block) {
|
||||||
if ((flags & EGG_SECURE_USE_FALLBACK)) {
|
if ((flags & EGG_SECURE_USE_FALLBACK) && EGG_SECURE_GLOBALS.fallback) {
|
||||||
egg_memory_fallback (memory, 0);
|
EGG_SECURE_GLOBALS.fallback (memory, 0);
|
||||||
} else {
|
} else {
|
||||||
if (egg_secure_warnings)
|
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);
|
(unsigned long)memory);
|
||||||
ASSERT (0 && "memory does does not belong to gnome-keyring");
|
ASSERT (0 && "memory does does not belong to secure memory pool");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,25 +1,25 @@
|
|||||||
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
|
/* -*- 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
|
/* egg-secure-memory.h - library for allocating memory that is non-pageable
|
||||||
*
|
|
||||||
* Copyright (C) 2007 Stefan Walter
|
Copyright (C) 2007 Stefan Walter
|
||||||
*
|
|
||||||
* This program is free software; you can redistribute it and/or modify
|
The Gnome Keyring Library is free software; you can redistribute it and/or
|
||||||
* it under the terms of the GNU Lesser General Public License as
|
modify it under the terms of the GNU Library General Public License as
|
||||||
* published by the Free Software Foundation; either version 2.1 of
|
published by the Free Software Foundation; either version 2 of the
|
||||||
* the License, or (at your option) any later version.
|
License, or (at your option) any later version.
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful, but
|
The Gnome Keyring Library is distributed in the hope that it will be useful,
|
||||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
* Lesser General Public License for more details.
|
Library General Public License for more details.
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU Lesser General Public
|
You should have received a copy of the GNU Library General Public
|
||||||
* License along with this program; if not, write to the Free Software
|
License along with the Gnome Library; see the file COPYING.LIB. If not,
|
||||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||||
* MA 02110-1301 USA
|
Boston, MA 02111-1307, USA.
|
||||||
*
|
|
||||||
* Author: Stef Walter <stef@thewalter.net>
|
Author: Stef Walter <stef@memberwebs.com>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef EGG_SECURE_MEMORY_H
|
#ifndef EGG_SECURE_MEMORY_H
|
||||||
#define EGG_SECURE_MEMORY_H
|
#define EGG_SECURE_MEMORY_H
|
||||||
@ -40,29 +40,31 @@
|
|||||||
* secure memory between threads:
|
* 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
|
||||||
|
|
||||||
/*
|
#define EGG_SECURE_DEFINE_GLOBALS(lock, unlock, fallback) \
|
||||||
* Allocation Fallbacks
|
egg_secure_glob EGG_SECURE_GLOBALS = { \
|
||||||
*
|
lock, unlock, fallback, NULL, EGG_SECURE_POOL_VER_STR };
|
||||||
* 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_GLIB_DEFINITIONS() \
|
#define EGG_SECURE_DEFINE_GLIB_GLOBALS() \
|
||||||
static GStaticMutex memory_mutex = G_STATIC_MUTEX_INIT; \
|
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); } \
|
{ g_static_mutex_lock (&memory_mutex); } \
|
||||||
void egg_memory_unlock (void) \
|
static void egg_memory_unlock (void) \
|
||||||
{ g_static_mutex_unlock (&memory_mutex); } \
|
{ g_static_mutex_unlock (&memory_mutex); } \
|
||||||
void* egg_memory_fallback (void *p, size_t sz) \
|
EGG_SECURE_DEFINE_GLOBALS (egg_memory_lock, egg_memory_unlock, g_realloc);
|
||||||
{ return g_realloc (p, sz); } \
|
|
||||||
|
extern egg_secure_glob EGG_SECURE_GLOBALS;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Main functionality
|
* Main functionality
|
||||||
|
@ -34,7 +34,7 @@
|
|||||||
#include <glib.h>
|
#include <glib.h>
|
||||||
#include <gcrypt.h>
|
#include <gcrypt.h>
|
||||||
|
|
||||||
EGG_SECURE_GLIB_DEFINITIONS ();
|
EGG_SECURE_DEFINE_GLIB_GLOBALS ();
|
||||||
|
|
||||||
static void
|
static void
|
||||||
test_perform (void)
|
test_perform (void)
|
||||||
|
@ -33,7 +33,7 @@
|
|||||||
|
|
||||||
#include <gcrypt.h>
|
#include <gcrypt.h>
|
||||||
|
|
||||||
EGG_SECURE_GLIB_DEFINITIONS ();
|
EGG_SECURE_DEFINE_GLIB_GLOBALS ();
|
||||||
|
|
||||||
static void
|
static void
|
||||||
test_hkdf_test_case_1 (void)
|
test_hkdf_test_case_1 (void)
|
||||||
|
@ -32,7 +32,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
|
||||||
EGG_SECURE_GLIB_DEFINITIONS ();
|
EGG_SECURE_DEFINE_GLIB_GLOBALS ();
|
||||||
|
|
||||||
/* Declared in egg-secure-memory.c */
|
/* Declared in egg-secure-memory.c */
|
||||||
extern int egg_secure_warnings;
|
extern int egg_secure_warnings;
|
||||||
@ -182,26 +182,25 @@ test_multialloc (void)
|
|||||||
case 0: /* Allocate some memory */
|
case 0: /* Allocate some memory */
|
||||||
size = g_random_int_range (1, 16384);
|
size = g_random_int_range (1, 16384);
|
||||||
data = egg_secure_alloc (size);
|
data = egg_secure_alloc (size);
|
||||||
g_assert (data);
|
g_assert (data != NULL);
|
||||||
memset (data, 0xCAFEBABE, size);
|
memset (data, 0xCAFEBABE, size);
|
||||||
g_ptr_array_add (memory, data);
|
g_ptr_array_add (memory, data);
|
||||||
break;
|
break;
|
||||||
case 1: /* Reallocate some memory */
|
case 1: /* Reallocate some memory */
|
||||||
index = g_random_int_range (0, memory->len);
|
index = g_random_int_range (0, memory->len);
|
||||||
data = g_ptr_array_index (memory, index);
|
data = g_ptr_array_index (memory, index);
|
||||||
g_assert (data);
|
g_assert (data != NULL);
|
||||||
size = g_random_int_range (1, 16384);
|
size = g_random_int_range (1, 16384);
|
||||||
data = egg_secure_realloc (data, size);
|
data = egg_secure_realloc (data, size);
|
||||||
g_assert (data);
|
g_assert (data != NULL);
|
||||||
memset (data, 0xCAFEBABE, size);
|
memset (data, 0xCAFEBABE, size);
|
||||||
g_ptr_array_index (memory, index) = data;
|
g_ptr_array_index (memory, index) = data;
|
||||||
break;
|
break;
|
||||||
case 2: /* Free some memory */
|
case 2: /* Free some memory */
|
||||||
index = g_random_int_range (0, memory->len);
|
index = g_random_int_range (0, memory->len);
|
||||||
data = g_ptr_array_index (memory, index);
|
data = g_ptr_array_remove_index_fast (memory, index);
|
||||||
g_assert (data);
|
g_assert (data != NULL);
|
||||||
egg_secure_free (data);
|
egg_secure_free (data);
|
||||||
g_ptr_array_remove_index_fast (memory, index);
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
g_assert_not_reached ();
|
g_assert_not_reached ();
|
||||||
@ -214,7 +213,8 @@ test_multialloc (void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
g_assert (memory->len == 0);
|
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);
|
g_ptr_array_free (memory, TRUE);
|
||||||
|
|
||||||
egg_secure_warnings = 1;
|
egg_secure_warnings = 1;
|
||||||
|
@ -104,7 +104,7 @@
|
|||||||
* during a secret_service_get() or secret_service_new() operation.
|
* during a secret_service_get() or secret_service_new() operation.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
EGG_SECURE_GLIB_DEFINITIONS ();
|
EGG_SECURE_DEFINE_GLIB_GLOBALS ();
|
||||||
|
|
||||||
GQuark _secret_error_quark = 0;
|
GQuark _secret_error_quark = 0;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user