mirror of
https://gitlab.gnome.org/GNOME/libsecret.git
synced 2024-12-22 04:38:55 +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
File diff suppressed because it is too large
Load Diff
@ -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
|
||||
@ -27,49 +27,51 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
/* -------------------------------------------------------------------
|
||||
* Low Level Secure Memory
|
||||
*
|
||||
* IMPORTANT: This is pure vanila standard C, no glib. We need this
|
||||
* because certain consumers of this protocol need to be built
|
||||
* Low Level Secure Memory
|
||||
*
|
||||
* IMPORTANT: This is pure vanila standard C, no glib. We need this
|
||||
* because certain consumers of this protocol need to be built
|
||||
* without linking in any special libraries. ie: the PKCS#11 module.
|
||||
*
|
||||
*
|
||||
* Thread locking
|
||||
*
|
||||
*
|
||||
* In order to use these functions in a module the following functions
|
||||
* must be defined somewhere, and provide appropriate locking for
|
||||
* must be defined somewhere, and provide appropriate locking for
|
||||
* secure memory between threads:
|
||||
*/
|
||||
|
||||
extern void egg_memory_lock (void);
|
||||
|
||||
extern void egg_memory_unlock (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;
|
||||
|
||||
#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) \
|
||||
egg_secure_glob EGG_SECURE_GLOBALS = { \
|
||||
lock, unlock, fallback, NULL, EGG_SECURE_POOL_VER_STR };
|
||||
|
||||
#define EGG_SECURE_DEFINE_GLIB_GLOBALS() \
|
||||
static GStaticMutex memory_mutex = G_STATIC_MUTEX_INIT; \
|
||||
static void egg_memory_lock (void) \
|
||||
{ g_static_mutex_lock (&memory_mutex); } \
|
||||
static void egg_memory_unlock (void) \
|
||||
{ g_static_mutex_unlock (&memory_mutex); } \
|
||||
EGG_SECURE_DEFINE_GLOBALS (egg_memory_lock, egg_memory_unlock, g_realloc);
|
||||
|
||||
extern egg_secure_glob EGG_SECURE_GLOBALS;
|
||||
|
||||
/*
|
||||
* 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_GLIB_DEFINITIONS() \
|
||||
static GStaticMutex memory_mutex = G_STATIC_MUTEX_INIT; \
|
||||
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) \
|
||||
{ return g_realloc (p, sz); } \
|
||||
|
||||
/*
|
||||
* Main functionality
|
||||
*
|
||||
*
|
||||
* Allocations return NULL on failure.
|
||||
*/
|
||||
|
||||
*/
|
||||
|
||||
#define EGG_SECURE_USE_FALLBACK 0x0001
|
||||
|
||||
#define EGG_SECURE_DECLARE(tag) \
|
||||
@ -90,13 +92,13 @@ void* egg_secure_alloc_full (const char *tag, size_t length, int options);
|
||||
|
||||
void* egg_secure_realloc_full (const char *tag, void *p, size_t length, int options);
|
||||
|
||||
void egg_secure_free (void* p);
|
||||
void egg_secure_free (void* p);
|
||||
|
||||
void egg_secure_free_full (void* p, int fallback);
|
||||
void egg_secure_free_full (void* p, int fallback);
|
||||
|
||||
void egg_secure_clear (void *p, size_t length);
|
||||
|
||||
int egg_secure_check (const void* p);
|
||||
int egg_secure_check (const void* p);
|
||||
|
||||
void egg_secure_validate (void);
|
||||
|
||||
|
@ -34,7 +34,7 @@
|
||||
#include <glib.h>
|
||||
#include <gcrypt.h>
|
||||
|
||||
EGG_SECURE_GLIB_DEFINITIONS ();
|
||||
EGG_SECURE_DEFINE_GLIB_GLOBALS ();
|
||||
|
||||
static void
|
||||
test_perform (void)
|
||||
|
@ -33,7 +33,7 @@
|
||||
|
||||
#include <gcrypt.h>
|
||||
|
||||
EGG_SECURE_GLIB_DEFINITIONS ();
|
||||
EGG_SECURE_DEFINE_GLIB_GLOBALS ();
|
||||
|
||||
static void
|
||||
test_hkdf_test_case_1 (void)
|
||||
|
@ -32,14 +32,14 @@
|
||||
#include <string.h>
|
||||
|
||||
|
||||
EGG_SECURE_GLIB_DEFINITIONS ();
|
||||
EGG_SECURE_DEFINE_GLIB_GLOBALS ();
|
||||
|
||||
/* Declared in egg-secure-memory.c */
|
||||
extern int egg_secure_warnings;
|
||||
|
||||
EGG_SECURE_DECLARE (tests);
|
||||
|
||||
/*
|
||||
/*
|
||||
* Each test looks like (on one line):
|
||||
* void unit_test_xxxxx (CuTest* cu)
|
||||
*
|
||||
@ -93,7 +93,7 @@ test_realloc_across (void)
|
||||
g_assert (p != NULL);
|
||||
g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p, 1088));
|
||||
|
||||
/* Reallocate to a large one, will have to have changed blocks */
|
||||
/* Reallocate to a large one, will have to have changed blocks */
|
||||
p2 = egg_secure_realloc_full ("tests", p, 16200, 0);
|
||||
g_assert (p2 != NULL);
|
||||
g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p2, 16200));
|
||||
@ -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;
|
||||
|
@ -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;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user