2011-06-12 20:55:02 +00:00
|
|
|
/* -*- 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
|
2012-10-27 09:58:13 +00:00
|
|
|
|
|
|
|
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,
|
2014-01-23 12:00:05 +00:00
|
|
|
see <http://www.gnu.org/licenses/>.
|
2012-10-27 09:58:13 +00:00
|
|
|
|
|
|
|
Author: Stef Walter <stef@memberwebs.com>
|
|
|
|
*/
|
2011-06-12 20:55:02 +00:00
|
|
|
|
|
|
|
/*
|
2012-10-27 09:58:13 +00:00
|
|
|
* IMPORTANT: This is pure vanila standard C, no glib. We need this
|
|
|
|
* because certain consumers of this protocol need to be built
|
2011-06-12 20:55:02 +00:00
|
|
|
* without linking in any special libraries. ie: the PKCS#11 module.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "egg-secure-memory.h"
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#ifdef WITH_VALGRIND
|
|
|
|
#include <valgrind/valgrind.h>
|
|
|
|
#include <valgrind/memcheck.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define DEBUG_SECURE_MEMORY 0
|
|
|
|
|
2012-10-27 09:58:13 +00:00
|
|
|
#if DEBUG_SECURE_MEMORY
|
|
|
|
#define DEBUG_ALLOC(msg, n) fprintf(stderr, "%s %lu bytes\n", msg, n);
|
2011-06-12 20:55:02 +00:00
|
|
|
#else
|
|
|
|
#define DEBUG_ALLOC(msg, n)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define DEFAULT_BLOCK_SIZE 16384
|
|
|
|
|
|
|
|
/* Use our own assert to guarantee no glib allocations */
|
|
|
|
#ifndef ASSERT
|
|
|
|
#ifdef G_DISABLE_ASSERT
|
2012-10-27 09:58:13 +00:00
|
|
|
#define ASSERT(x)
|
|
|
|
#else
|
2011-06-12 20:55:02 +00:00
|
|
|
#define ASSERT(x) assert(x)
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define DO_LOCK() \
|
2012-10-27 09:58:13 +00:00
|
|
|
EGG_SECURE_GLOBALS.lock ();
|
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
#define DO_UNLOCK() \
|
2012-10-27 09:58:13 +00:00
|
|
|
EGG_SECURE_GLOBALS.unlock ();
|
2011-06-12 20:55:02 +00:00
|
|
|
|
2012-10-27 09:58:13 +00:00
|
|
|
static int show_warning = 1;
|
2011-06-12 20:55:02 +00:00
|
|
|
int egg_secure_warnings = 1;
|
|
|
|
|
2012-10-27 09:58:13 +00:00
|
|
|
/*
|
|
|
|
* We allocate all memory in units of sizeof(void*). This
|
2011-06-12 20:55:02 +00:00
|
|
|
* is our definition of 'word'.
|
|
|
|
*/
|
|
|
|
typedef void* word_t;
|
|
|
|
|
2012-10-27 09:58:13 +00:00
|
|
|
/* The amount of extra words we can allocate */
|
2011-06-12 20:55:02 +00:00
|
|
|
#define WASTE 4
|
|
|
|
|
2012-10-27 09:58:13 +00:00
|
|
|
/*
|
|
|
|
* Track allocated memory or a free block. This structure is not stored
|
|
|
|
* in the secure memory area. It is allocated from a pool of other
|
2011-06-12 20:55:02 +00:00
|
|
|
* memory. See meta_pool_xxx ().
|
|
|
|
*/
|
|
|
|
typedef struct _Cell {
|
|
|
|
word_t *words; /* Pointer to secure memory */
|
|
|
|
size_t n_words; /* Amount of secure memory in words */
|
2011-09-25 06:22:36 +00:00
|
|
|
size_t requested; /* Amount actually requested by app, in bytes, 0 if unused */
|
|
|
|
const char *tag; /* Tag which describes the allocation */
|
|
|
|
struct _Cell *next; /* Next in memory ring */
|
|
|
|
struct _Cell *prev; /* Previous in memory ring */
|
2011-06-12 20:55:02 +00:00
|
|
|
} Cell;
|
|
|
|
|
2012-10-27 09:58:13 +00:00
|
|
|
/*
|
2011-06-12 20:55:02 +00:00
|
|
|
* A block of secure memory. This structure is the header in that block.
|
|
|
|
*/
|
|
|
|
typedef struct _Block {
|
2011-09-25 06:22:36 +00:00
|
|
|
word_t *words; /* Actual memory hangs off here */
|
|
|
|
size_t n_words; /* Number of words in block */
|
|
|
|
size_t n_used; /* Number of used allocations */
|
|
|
|
struct _Cell* used_cells; /* Ring of used allocations */
|
|
|
|
struct _Cell* unused_cells; /* Ring of unused allocations */
|
|
|
|
struct _Block *next; /* Next block in list */
|
2011-06-12 20:55:02 +00:00
|
|
|
} Block;
|
|
|
|
|
|
|
|
/* -----------------------------------------------------------------------------
|
|
|
|
* UNUSED STACK
|
|
|
|
*/
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
unused_push (void **stack, void *ptr)
|
|
|
|
{
|
|
|
|
ASSERT (ptr);
|
|
|
|
ASSERT (stack);
|
|
|
|
*((void**)ptr) = *stack;
|
|
|
|
*stack = ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void*
|
|
|
|
unused_pop (void **stack)
|
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
ASSERT (stack);
|
|
|
|
ptr = *stack;
|
|
|
|
*stack = *(void**)ptr;
|
|
|
|
return ptr;
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void*
|
|
|
|
unused_peek (void **stack)
|
|
|
|
{
|
|
|
|
ASSERT (stack);
|
2012-10-27 09:58:13 +00:00
|
|
|
return *stack;
|
2011-06-12 20:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -----------------------------------------------------------------------------
|
|
|
|
* POOL META DATA ALLOCATION
|
2012-10-27 09:58:13 +00:00
|
|
|
*
|
|
|
|
* A pool for memory meta data. We allocate fixed size blocks. There are actually
|
2011-06-12 20:55:02 +00:00
|
|
|
* two different structures stored in this pool: Cell and Block. Cell is allocated
|
|
|
|
* way more often, and is bigger so we just allocate that size for both.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Pool allocates this data type */
|
|
|
|
typedef union _Item {
|
|
|
|
Cell cell;
|
|
|
|
Block block;
|
|
|
|
} Item;
|
|
|
|
|
|
|
|
typedef struct _Pool {
|
|
|
|
struct _Pool *next; /* Next pool in list */
|
|
|
|
size_t length; /* Length in bytes of the pool */
|
|
|
|
size_t used; /* Number of cells used in pool */
|
|
|
|
void *unused; /* Unused stack of unused stuff */
|
|
|
|
size_t n_items; /* Total number of items in pool */
|
|
|
|
Item items[1]; /* Actual items hang off here */
|
|
|
|
} Pool;
|
|
|
|
|
2012-10-27 09:58:13 +00:00
|
|
|
static void *
|
2011-06-12 20:55:02 +00:00
|
|
|
pool_alloc (void)
|
|
|
|
{
|
|
|
|
Pool *pool;
|
|
|
|
void *pages, *item;
|
|
|
|
size_t len, i;
|
2012-10-27 09:58:13 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
/* A pool with an available item */
|
2012-10-27 09:58:13 +00:00
|
|
|
for (pool = EGG_SECURE_GLOBALS.pool_data; pool; pool = pool->next) {
|
2011-06-12 20:55:02 +00:00
|
|
|
if (unused_peek (&pool->unused))
|
|
|
|
break;
|
|
|
|
}
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
/* Create a new pool */
|
|
|
|
if (pool == NULL) {
|
|
|
|
len = getpagesize () * 2;
|
|
|
|
pages = mmap (0, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
|
|
|
|
if (pages == MAP_FAILED)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Fill in the block header, and inlude in block list */
|
|
|
|
pool = pages;
|
2012-10-27 09:58:13 +00:00
|
|
|
pool->next = EGG_SECURE_GLOBALS.pool_data;
|
|
|
|
EGG_SECURE_GLOBALS.pool_data = pool;
|
2011-06-12 20:55:02 +00:00
|
|
|
pool->length = len;
|
|
|
|
pool->used = 0;
|
|
|
|
pool->unused = NULL;
|
|
|
|
|
|
|
|
/* Fill block with unused items */
|
|
|
|
pool->n_items = (len - sizeof (Pool)) / sizeof (Item);
|
|
|
|
for (i = 0; i < pool->n_items; ++i)
|
|
|
|
unused_push (&pool->unused, pool->items + i);
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
#ifdef WITH_VALGRIND
|
|
|
|
VALGRIND_CREATE_MEMPOOL(pool, 0, 0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
++pool->used;
|
|
|
|
ASSERT (unused_peek (&pool->unused));
|
|
|
|
item = unused_pop (&pool->unused);
|
2011-11-06 12:41:21 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
#ifdef WITH_VALGRIND
|
|
|
|
VALGRIND_MEMPOOL_ALLOC (pool, item, sizeof (Item));
|
|
|
|
#endif
|
2011-11-06 12:41:21 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
return memset (item, 0, sizeof (Item));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pool_free (void* item)
|
|
|
|
{
|
|
|
|
Pool *pool, **at;
|
|
|
|
char *ptr, *beg, *end;
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
ptr = item;
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
/* Find which block this one belongs to */
|
2012-10-27 09:58:13 +00:00
|
|
|
for (at = (Pool **)&EGG_SECURE_GLOBALS.pool_data, pool = *at;
|
|
|
|
pool != NULL; at = &pool->next, pool = *at) {
|
2011-06-12 20:55:02 +00:00
|
|
|
beg = (char*)pool->items;
|
|
|
|
end = (char*)pool + pool->length - sizeof (Item);
|
|
|
|
if (ptr >= beg && ptr <= end) {
|
|
|
|
ASSERT ((ptr - beg) % sizeof (Item) == 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise invalid meta */
|
|
|
|
ASSERT (at);
|
|
|
|
ASSERT (pool);
|
|
|
|
ASSERT (pool->used > 0);
|
|
|
|
|
|
|
|
/* No more meta cells used in this block, remove from list, destroy */
|
|
|
|
if (pool->used == 1) {
|
|
|
|
*at = pool->next;
|
2011-11-06 12:41:21 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
#ifdef WITH_VALGRIND
|
|
|
|
VALGRIND_DESTROY_MEMPOOL (pool);
|
|
|
|
#endif
|
2011-11-06 12:41:21 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
munmap (pool, pool->length);
|
|
|
|
return;
|
|
|
|
}
|
2011-11-06 12:41:21 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
#ifdef WITH_VALGRIND
|
|
|
|
VALGRIND_MEMPOOL_FREE (pool, item);
|
|
|
|
VALGRIND_MAKE_MEM_UNDEFINED (item, sizeof (Item));
|
|
|
|
#endif
|
2011-11-06 12:41:21 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
--pool->used;
|
|
|
|
memset (item, 0xCD, sizeof (Item));
|
|
|
|
unused_push (&pool->unused, item);
|
|
|
|
}
|
|
|
|
|
2011-09-25 06:22:36 +00:00
|
|
|
#ifndef G_DISABLE_ASSERT
|
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
static int
|
|
|
|
pool_valid (void* item)
|
|
|
|
{
|
|
|
|
Pool *pool;
|
|
|
|
char *ptr, *beg, *end;
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
ptr = item;
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
/* Find which block this one belongs to */
|
2012-10-27 09:58:13 +00:00
|
|
|
for (pool = EGG_SECURE_GLOBALS.pool_data; pool; pool = pool->next) {
|
2011-06-12 20:55:02 +00:00
|
|
|
beg = (char*)pool->items;
|
|
|
|
end = (char*)pool + pool->length - sizeof (Item);
|
2012-10-27 09:58:13 +00:00
|
|
|
if (ptr >= beg && ptr <= end)
|
2011-06-12 20:55:02 +00:00
|
|
|
return (pool->used && (ptr - beg) % sizeof (Item) == 0);
|
|
|
|
}
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-25 06:22:36 +00:00
|
|
|
#endif /* G_DISABLE_ASSERT */
|
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
/* -----------------------------------------------------------------------------
|
|
|
|
* SEC ALLOCATION
|
2012-10-27 09:58:13 +00:00
|
|
|
*
|
2011-06-12 20:55:02 +00:00
|
|
|
* Each memory cell begins and ends with a pointer to its metadata. These are also
|
2012-10-27 09:58:13 +00:00
|
|
|
* used as guards or red zones. Since they're treated as redzones by valgrind we
|
2011-06-12 20:55:02 +00:00
|
|
|
* have to jump through a few hoops before reading and/or writing them.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static inline size_t
|
|
|
|
sec_size_to_words (size_t length)
|
|
|
|
{
|
|
|
|
return (length % sizeof (void*) ? 1 : 0) + (length / sizeof (void*));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
sec_write_guards (Cell *cell)
|
|
|
|
{
|
|
|
|
#ifdef WITH_VALGRIND
|
|
|
|
VALGRIND_MAKE_MEM_UNDEFINED (cell->words, sizeof (word_t));
|
|
|
|
VALGRIND_MAKE_MEM_UNDEFINED (cell->words + cell->n_words - 1, sizeof (word_t));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
((void**)cell->words)[0] = (void*)cell;
|
|
|
|
((void**)cell->words)[cell->n_words - 1] = (void*)cell;
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
#ifdef WITH_VALGRIND
|
|
|
|
VALGRIND_MAKE_MEM_NOACCESS (cell->words, sizeof (word_t));
|
|
|
|
VALGRIND_MAKE_MEM_NOACCESS (cell->words + cell->n_words - 1, sizeof (word_t));
|
2012-10-27 09:58:13 +00:00
|
|
|
#endif
|
2011-06-12 20:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
sec_check_guards (Cell *cell)
|
|
|
|
{
|
|
|
|
#ifdef WITH_VALGRIND
|
|
|
|
VALGRIND_MAKE_MEM_DEFINED (cell->words, sizeof (word_t));
|
2012-10-27 09:58:13 +00:00
|
|
|
VALGRIND_MAKE_MEM_DEFINED (cell->words + cell->n_words - 1, sizeof (word_t));
|
|
|
|
#endif
|
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
ASSERT(((void**)cell->words)[0] == (void*)cell);
|
|
|
|
ASSERT(((void**)cell->words)[cell->n_words - 1] == (void*)cell);
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
#ifdef WITH_VALGRIND
|
|
|
|
VALGRIND_MAKE_MEM_NOACCESS (cell->words, sizeof (word_t));
|
|
|
|
VALGRIND_MAKE_MEM_NOACCESS (cell->words + cell->n_words - 1, sizeof (word_t));
|
2012-10-27 09:58:13 +00:00
|
|
|
#endif
|
2011-06-12 20:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sec_insert_cell_ring (Cell **ring, Cell *cell)
|
|
|
|
{
|
|
|
|
ASSERT (ring);
|
|
|
|
ASSERT (cell);
|
|
|
|
ASSERT (cell != *ring);
|
|
|
|
ASSERT (cell->next == NULL);
|
|
|
|
ASSERT (cell->prev == NULL);
|
2012-10-27 09:58:13 +00:00
|
|
|
|
|
|
|
/* Insert back into the mix of available memory */
|
|
|
|
if (*ring) {
|
2011-06-12 20:55:02 +00:00
|
|
|
cell->next = (*ring)->next;
|
|
|
|
cell->prev = *ring;
|
|
|
|
cell->next->prev = cell;
|
|
|
|
cell->prev->next = cell;
|
|
|
|
} else {
|
|
|
|
cell->next = cell;
|
|
|
|
cell->prev = cell;
|
|
|
|
}
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
*ring = cell;
|
|
|
|
ASSERT (cell->next->prev == cell);
|
|
|
|
ASSERT (cell->prev->next == cell);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sec_remove_cell_ring (Cell **ring, Cell *cell)
|
|
|
|
{
|
|
|
|
ASSERT (ring);
|
|
|
|
ASSERT (*ring);
|
|
|
|
ASSERT (cell->next);
|
|
|
|
ASSERT (cell->prev);
|
|
|
|
|
|
|
|
ASSERT (cell->next->prev == cell);
|
|
|
|
ASSERT (cell->prev->next == cell);
|
|
|
|
|
|
|
|
if (cell == *ring) {
|
|
|
|
/* The last meta? */
|
|
|
|
if (cell->next == cell) {
|
|
|
|
ASSERT (cell->prev == cell);
|
|
|
|
*ring = NULL;
|
|
|
|
|
|
|
|
/* Just pointing to this meta */
|
|
|
|
} else {
|
|
|
|
ASSERT (cell->prev != cell);
|
|
|
|
*ring = cell->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cell->next->prev = cell->prev;
|
|
|
|
cell->prev->next = cell->next;
|
|
|
|
cell->next = cell->prev = NULL;
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
ASSERT (*ring != cell);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void*
|
|
|
|
sec_cell_to_memory (Cell *cell)
|
|
|
|
{
|
|
|
|
return cell->words + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
sec_is_valid_word (Block *block, word_t *word)
|
|
|
|
{
|
|
|
|
return (word >= block->words && word < block->words + block->n_words);
|
|
|
|
}
|
|
|
|
|
2011-11-06 12:41:21 +00:00
|
|
|
static inline void
|
|
|
|
sec_clear_undefined (void *memory,
|
|
|
|
size_t from,
|
|
|
|
size_t to)
|
2011-06-12 20:55:02 +00:00
|
|
|
{
|
2011-11-06 12:41:21 +00:00
|
|
|
char *ptr = memory;
|
2011-06-12 20:55:02 +00:00
|
|
|
ASSERT (from <= to);
|
2011-11-06 12:41:21 +00:00
|
|
|
#ifdef WITH_VALGRIND
|
|
|
|
VALGRIND_MAKE_MEM_UNDEFINED (ptr + from, to - from);
|
|
|
|
#endif
|
|
|
|
memset (ptr + from, 0, to - from);
|
|
|
|
#ifdef WITH_VALGRIND
|
|
|
|
VALGRIND_MAKE_MEM_UNDEFINED (ptr + from, to - from);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
static inline void
|
|
|
|
sec_clear_noaccess (void *memory, size_t from, size_t to)
|
|
|
|
{
|
|
|
|
char *ptr = memory;
|
|
|
|
ASSERT (from <= to);
|
|
|
|
#ifdef WITH_VALGRIND
|
|
|
|
VALGRIND_MAKE_MEM_UNDEFINED (ptr + from, to - from);
|
|
|
|
#endif
|
|
|
|
memset (ptr + from, 0, to - from);
|
|
|
|
#ifdef WITH_VALGRIND
|
|
|
|
VALGRIND_MAKE_MEM_NOACCESS (ptr + from, to - from);
|
|
|
|
#endif
|
2011-06-12 20:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static Cell*
|
|
|
|
sec_neighbor_before (Block *block, Cell *cell)
|
|
|
|
{
|
|
|
|
word_t *word;
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
ASSERT (cell);
|
|
|
|
ASSERT (block);
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
word = cell->words - 1;
|
|
|
|
if (!sec_is_valid_word (block, word))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
#ifdef WITH_VALGRIND
|
|
|
|
VALGRIND_MAKE_MEM_DEFINED (word, sizeof (word_t));
|
|
|
|
#endif
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
cell = *word;
|
|
|
|
sec_check_guards (cell);
|
|
|
|
|
|
|
|
#ifdef WITH_VALGRIND
|
|
|
|
VALGRIND_MAKE_MEM_NOACCESS (word, sizeof (word_t));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
2012-10-27 09:58:13 +00:00
|
|
|
static Cell*
|
2011-06-12 20:55:02 +00:00
|
|
|
sec_neighbor_after (Block *block, Cell *cell)
|
|
|
|
{
|
|
|
|
word_t *word;
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
ASSERT (cell);
|
|
|
|
ASSERT (block);
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
word = cell->words + cell->n_words;
|
|
|
|
if (!sec_is_valid_word (block, word))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
#ifdef WITH_VALGRIND
|
|
|
|
VALGRIND_MAKE_MEM_DEFINED (word, sizeof (word_t));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
cell = *word;
|
|
|
|
sec_check_guards (cell);
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
#ifdef WITH_VALGRIND
|
|
|
|
VALGRIND_MAKE_MEM_NOACCESS (word, sizeof (word_t));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void*
|
2011-09-25 06:22:36 +00:00
|
|
|
sec_alloc (Block *block,
|
|
|
|
const char *tag,
|
|
|
|
size_t length)
|
2011-06-12 20:55:02 +00:00
|
|
|
{
|
|
|
|
Cell *cell, *other;
|
|
|
|
size_t n_words;
|
|
|
|
void *memory;
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
ASSERT (block);
|
|
|
|
ASSERT (length);
|
2011-09-25 06:22:36 +00:00
|
|
|
ASSERT (tag);
|
2011-06-12 20:55:02 +00:00
|
|
|
|
2011-09-25 06:22:36 +00:00
|
|
|
if (!block->unused_cells)
|
2011-06-12 20:55:02 +00:00
|
|
|
return NULL;
|
|
|
|
|
2012-10-27 09:58:13 +00:00
|
|
|
/*
|
|
|
|
* Each memory allocation is aligned to a pointer size, and
|
2011-06-12 20:55:02 +00:00
|
|
|
* then, sandwidched between two pointers to its meta data.
|
|
|
|
* These pointers also act as guards.
|
|
|
|
*
|
2012-10-27 09:58:13 +00:00
|
|
|
* We allocate memory in units of sizeof (void*)
|
2011-06-12 20:55:02 +00:00
|
|
|
*/
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
n_words = sec_size_to_words (length) + 2;
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
/* Look for a cell of at least our required size */
|
2011-09-25 06:22:36 +00:00
|
|
|
cell = block->unused_cells;
|
2011-06-12 20:55:02 +00:00
|
|
|
while (cell->n_words < n_words) {
|
|
|
|
cell = cell->next;
|
2011-09-25 06:22:36 +00:00
|
|
|
if (cell == block->unused_cells) {
|
2011-06-12 20:55:02 +00:00
|
|
|
cell = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
if (!cell)
|
|
|
|
return NULL;
|
2011-09-25 06:22:36 +00:00
|
|
|
|
|
|
|
ASSERT (cell->tag == NULL);
|
|
|
|
ASSERT (cell->requested == 0);
|
2011-06-12 20:55:02 +00:00
|
|
|
ASSERT (cell->prev);
|
|
|
|
ASSERT (cell->words);
|
|
|
|
sec_check_guards (cell);
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
/* Steal from the cell if it's too long */
|
|
|
|
if (cell->n_words > n_words + WASTE) {
|
|
|
|
other = pool_alloc ();
|
|
|
|
if (!other)
|
|
|
|
return NULL;
|
|
|
|
other->n_words = n_words;
|
|
|
|
other->words = cell->words;
|
|
|
|
cell->n_words -= n_words;
|
|
|
|
cell->words += n_words;
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
sec_write_guards (other);
|
|
|
|
sec_write_guards (cell);
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
cell = other;
|
|
|
|
}
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
if (cell->next)
|
2011-09-25 06:22:36 +00:00
|
|
|
sec_remove_cell_ring (&block->unused_cells, cell);
|
|
|
|
|
|
|
|
++block->n_used;
|
|
|
|
cell->tag = tag;
|
|
|
|
cell->requested = length;
|
|
|
|
sec_insert_cell_ring (&block->used_cells, cell);
|
2011-06-12 20:55:02 +00:00
|
|
|
memory = sec_cell_to_memory (cell);
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
#ifdef WITH_VALGRIND
|
|
|
|
VALGRIND_MAKE_MEM_UNDEFINED (memory, length);
|
|
|
|
#endif
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
return memset (memory, 0, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void*
|
|
|
|
sec_free (Block *block, void *memory)
|
|
|
|
{
|
|
|
|
Cell *cell, *other;
|
|
|
|
word_t *word;
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
ASSERT (block);
|
|
|
|
ASSERT (memory);
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
word = memory;
|
|
|
|
--word;
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
#ifdef WITH_VALGRIND
|
|
|
|
VALGRIND_MAKE_MEM_DEFINED (word, sizeof (word_t));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Lookup the meta for this memory block (using guard pointer) */
|
|
|
|
ASSERT (sec_is_valid_word (block, word));
|
|
|
|
ASSERT (pool_valid (*word));
|
|
|
|
cell = *word;
|
|
|
|
|
|
|
|
#ifdef WITH_VALGRIND
|
|
|
|
VALGRIND_MAKE_MEM_DEFINED (cell->words, cell->n_words * sizeof (word_t));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
sec_check_guards (cell);
|
2011-11-06 12:41:21 +00:00
|
|
|
sec_clear_noaccess (memory, 0, cell->requested);
|
2011-06-12 20:55:02 +00:00
|
|
|
|
|
|
|
sec_check_guards (cell);
|
2011-09-25 06:22:36 +00:00
|
|
|
ASSERT (cell->requested > 0);
|
|
|
|
ASSERT (cell->tag != NULL);
|
|
|
|
|
|
|
|
/* Remove from the used cell ring */
|
|
|
|
sec_remove_cell_ring (&block->used_cells, cell);
|
2011-06-12 20:55:02 +00:00
|
|
|
|
2012-10-27 09:58:13 +00:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
2011-06-12 20:55:02 +00:00
|
|
|
}
|
|
|
|
|
2011-11-06 12:41:21 +00:00
|
|
|
static void
|
|
|
|
memcpy_with_vbits (void *dest,
|
|
|
|
void *src,
|
|
|
|
size_t length)
|
|
|
|
{
|
|
|
|
#ifdef WITH_VALGRIND
|
|
|
|
int vbits_setup = 0;
|
|
|
|
void *vbits = NULL;
|
|
|
|
|
|
|
|
if (RUNNING_ON_VALGRIND) {
|
|
|
|
vbits = malloc (length);
|
|
|
|
if (vbits != NULL)
|
|
|
|
vbits_setup = VALGRIND_GET_VBITS (src, vbits, length);
|
|
|
|
VALGRIND_MAKE_MEM_DEFINED (src, length);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
memcpy (dest, src, length);
|
|
|
|
|
|
|
|
#ifdef WITH_VALGRIND
|
|
|
|
if (vbits_setup == 1) {
|
|
|
|
VALGRIND_SET_VBITS (dest, vbits, length);
|
|
|
|
VALGRIND_SET_VBITS (src, vbits, length);
|
|
|
|
}
|
|
|
|
free (vbits);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
static void*
|
2011-09-25 06:22:36 +00:00
|
|
|
sec_realloc (Block *block,
|
|
|
|
const char *tag,
|
|
|
|
void *memory,
|
|
|
|
size_t length)
|
2011-06-12 20:55:02 +00:00
|
|
|
{
|
|
|
|
Cell *cell, *other;
|
|
|
|
word_t *word;
|
|
|
|
size_t n_words;
|
|
|
|
size_t valid;
|
|
|
|
void *alloc;
|
2011-09-25 06:22:36 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
/* Standard realloc behavior, should have been handled elsewhere */
|
|
|
|
ASSERT (memory != NULL);
|
|
|
|
ASSERT (length > 0);
|
2011-09-25 06:22:36 +00:00
|
|
|
ASSERT (tag != NULL);
|
2011-06-12 20:55:02 +00:00
|
|
|
|
|
|
|
/* Dig out where the meta should be */
|
|
|
|
word = memory;
|
|
|
|
--word;
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
#ifdef WITH_VALGRIND
|
|
|
|
VALGRIND_MAKE_MEM_DEFINED (word, sizeof (word_t));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ASSERT (sec_is_valid_word (block, word));
|
|
|
|
ASSERT (pool_valid (*word));
|
|
|
|
cell = *word;
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
/* Validate that it's actually for real */
|
|
|
|
sec_check_guards (cell);
|
2011-09-25 06:22:36 +00:00
|
|
|
ASSERT (cell->requested > 0);
|
|
|
|
ASSERT (cell->tag != NULL);
|
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
/* The amount of valid data */
|
2011-09-25 06:22:36 +00:00
|
|
|
valid = cell->requested;
|
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
/* How many words we actually want */
|
|
|
|
n_words = sec_size_to_words (length) + 2;
|
|
|
|
|
|
|
|
/* Less memory is required than is in the cell */
|
|
|
|
if (n_words <= cell->n_words) {
|
|
|
|
|
|
|
|
/* TODO: No shrinking behavior yet */
|
2011-09-25 06:22:36 +00:00
|
|
|
cell->requested = length;
|
2011-06-12 20:55:02 +00:00
|
|
|
alloc = sec_cell_to_memory (cell);
|
|
|
|
|
2012-10-27 09:58:13 +00:00
|
|
|
/*
|
2011-06-12 20:55:02 +00:00
|
|
|
* Even though we may be reusing the same cell, that doesn't
|
|
|
|
* mean that the allocation is shrinking. It could have shrunk
|
2012-10-27 09:58:13 +00:00
|
|
|
* and is now expanding back some.
|
|
|
|
*/
|
2011-06-12 20:55:02 +00:00
|
|
|
if (length < valid)
|
2011-11-06 12:41:21 +00:00
|
|
|
sec_clear_undefined (alloc, length, valid);
|
|
|
|
|
|
|
|
return alloc;
|
2011-06-12 20:55:02 +00:00
|
|
|
}
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
/* Need braaaaaiiiiiinsss... */
|
|
|
|
while (cell->n_words < n_words) {
|
|
|
|
|
|
|
|
/* See if we have a neighbor who can give us some memory */
|
|
|
|
other = sec_neighbor_after (block, cell);
|
2011-09-25 06:22:36 +00:00
|
|
|
if (!other || other->requested != 0)
|
2011-06-12 20:55:02 +00:00
|
|
|
break;
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
/* Eat the whole neighbor if not too big */
|
|
|
|
if (n_words - cell->n_words + WASTE >= other->n_words) {
|
|
|
|
cell->n_words += other->n_words;
|
|
|
|
sec_write_guards (cell);
|
2011-09-25 06:22:36 +00:00
|
|
|
sec_remove_cell_ring (&block->unused_cells, other);
|
2011-06-12 20:55:02 +00:00
|
|
|
pool_free (other);
|
|
|
|
|
|
|
|
/* Steal from the neighbor */
|
|
|
|
} else {
|
|
|
|
other->words += n_words - cell->n_words;
|
|
|
|
other->n_words -= n_words - cell->n_words;
|
|
|
|
sec_write_guards (other);
|
|
|
|
cell->n_words = n_words;
|
|
|
|
sec_write_guards (cell);
|
|
|
|
}
|
|
|
|
}
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
if (cell->n_words >= n_words) {
|
2011-09-25 06:22:36 +00:00
|
|
|
cell->requested = length;
|
|
|
|
cell->tag = tag;
|
2011-06-12 20:55:02 +00:00
|
|
|
alloc = sec_cell_to_memory (cell);
|
2011-11-06 12:41:21 +00:00
|
|
|
sec_clear_undefined (alloc, valid, length);
|
|
|
|
return alloc;
|
2011-06-12 20:55:02 +00:00
|
|
|
}
|
2011-09-25 06:22:36 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
/* That didn't work, try alloc/free */
|
2011-09-25 06:22:36 +00:00
|
|
|
alloc = sec_alloc (block, tag, length);
|
2011-06-12 20:55:02 +00:00
|
|
|
if (alloc) {
|
2011-11-06 12:41:21 +00:00
|
|
|
memcpy_with_vbits (alloc, memory, valid);
|
2011-06-12 20:55:02 +00:00
|
|
|
sec_free (block, memory);
|
|
|
|
}
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
return alloc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
sec_allocated (Block *block, void *memory)
|
|
|
|
{
|
|
|
|
Cell *cell;
|
|
|
|
word_t *word;
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
ASSERT (block);
|
|
|
|
ASSERT (memory);
|
|
|
|
|
|
|
|
word = memory;
|
|
|
|
--word;
|
|
|
|
|
|
|
|
#ifdef WITH_VALGRIND
|
|
|
|
VALGRIND_MAKE_MEM_DEFINED (word, sizeof (word_t));
|
|
|
|
#endif
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
/* Lookup the meta for this memory block (using guard pointer) */
|
|
|
|
ASSERT (sec_is_valid_word (block, word));
|
|
|
|
ASSERT (pool_valid (*word));
|
|
|
|
cell = *word;
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
sec_check_guards (cell);
|
2011-09-25 06:22:36 +00:00
|
|
|
ASSERT (cell->requested > 0);
|
|
|
|
ASSERT (cell->tag != NULL);
|
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
#ifdef WITH_VALGRIND
|
|
|
|
VALGRIND_MAKE_MEM_NOACCESS (word, sizeof (word_t));
|
|
|
|
#endif
|
2011-09-25 06:22:36 +00:00
|
|
|
|
|
|
|
return cell->requested;
|
2011-06-12 20:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sec_validate (Block *block)
|
|
|
|
{
|
|
|
|
Cell *cell;
|
|
|
|
word_t *word, *last;
|
2011-11-06 12:41:21 +00:00
|
|
|
|
2011-11-19 07:04:28 +00:00
|
|
|
#ifdef WITH_VALGRIND
|
2011-11-06 12:41:21 +00:00
|
|
|
if (RUNNING_ON_VALGRIND)
|
|
|
|
return;
|
2011-11-19 07:04:28 +00:00
|
|
|
#endif
|
2011-11-06 12:41:21 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
word = block->words;
|
|
|
|
last = word + block->n_words;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
ASSERT (word < last);
|
|
|
|
|
|
|
|
ASSERT (sec_is_valid_word (block, word));
|
|
|
|
ASSERT (pool_valid (*word));
|
|
|
|
cell = *word;
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
/* Validate that it's actually for real */
|
|
|
|
sec_check_guards (cell);
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
/* Is it an allocated block? */
|
2011-09-25 06:22:36 +00:00
|
|
|
if (cell->requested > 0) {
|
|
|
|
ASSERT (cell->tag != NULL);
|
|
|
|
ASSERT (cell->next != NULL);
|
|
|
|
ASSERT (cell->prev != NULL);
|
|
|
|
ASSERT (cell->next->prev == cell);
|
|
|
|
ASSERT (cell->prev->next == cell);
|
|
|
|
ASSERT (cell->requested <= (cell->n_words - 2) * sizeof (word_t));
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-11-06 12:41:21 +00:00
|
|
|
/* An unused block */
|
2011-06-12 20:55:02 +00:00
|
|
|
} else {
|
2011-09-25 06:22:36 +00:00
|
|
|
ASSERT (cell->tag == NULL);
|
|
|
|
ASSERT (cell->next != NULL);
|
|
|
|
ASSERT (cell->prev != NULL);
|
2011-06-12 20:55:02 +00:00
|
|
|
ASSERT (cell->next->prev == cell);
|
|
|
|
ASSERT (cell->prev->next == cell);
|
|
|
|
}
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
word += cell->n_words;
|
|
|
|
if (word == last)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -----------------------------------------------------------------------------
|
|
|
|
* LOCKED MEMORY
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void*
|
2011-09-25 06:22:36 +00:00
|
|
|
sec_acquire_pages (size_t *sz,
|
|
|
|
const char *during_tag)
|
2011-06-12 20:55:02 +00:00
|
|
|
{
|
|
|
|
void *pages;
|
|
|
|
unsigned long pgsize;
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
ASSERT (sz);
|
|
|
|
ASSERT (*sz);
|
2011-09-25 06:22:36 +00:00
|
|
|
ASSERT (during_tag);
|
2011-06-12 20:55:02 +00:00
|
|
|
|
|
|
|
/* Make sure sz is a multiple of the page size */
|
|
|
|
pgsize = getpagesize ();
|
|
|
|
*sz = (*sz + pgsize -1) & ~(pgsize - 1);
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
#if defined(HAVE_MLOCK)
|
|
|
|
pages = mmap (0, *sz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
|
|
|
|
if (pages == MAP_FAILED) {
|
2012-10-27 09:58:13 +00:00
|
|
|
if (show_warning && egg_secure_warnings)
|
2011-09-25 06:22:36 +00:00
|
|
|
fprintf (stderr, "couldn't map %lu bytes of memory (%s): %s\n",
|
|
|
|
(unsigned long)*sz, during_tag, strerror (errno));
|
2012-10-27 09:58:13 +00:00
|
|
|
show_warning = 0;
|
2011-06-12 20:55:02 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
if (mlock (pages, *sz) < 0) {
|
2012-10-27 09:58:13 +00:00
|
|
|
if (show_warning && egg_secure_warnings && errno != EPERM) {
|
2011-09-25 06:22:36 +00:00
|
|
|
fprintf (stderr, "couldn't lock %lu bytes of memory (%s): %s\n",
|
|
|
|
(unsigned long)*sz, during_tag, strerror (errno));
|
2012-10-27 09:58:13 +00:00
|
|
|
show_warning = 0;
|
2011-06-12 20:55:02 +00:00
|
|
|
}
|
|
|
|
munmap (pages, *sz);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
DEBUG_ALLOC ("gkr-secure-memory: new block ", *sz);
|
2012-10-27 09:58:13 +00:00
|
|
|
|
|
|
|
show_warning = 1;
|
2011-06-12 20:55:02 +00:00
|
|
|
return pages;
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
#else
|
2012-10-27 09:58:13 +00:00
|
|
|
if (show_warning && egg_secure_warnings)
|
2011-06-12 20:55:02 +00:00
|
|
|
fprintf (stderr, "your system does not support private memory");
|
2012-10-27 09:58:13 +00:00
|
|
|
show_warning = 0;
|
2011-06-12 20:55:02 +00:00
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-10-27 09:58:13 +00:00
|
|
|
static void
|
2011-06-12 20:55:02 +00:00
|
|
|
sec_release_pages (void *pages, size_t sz)
|
|
|
|
{
|
|
|
|
ASSERT (pages);
|
|
|
|
ASSERT (sz % getpagesize () == 0);
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
#if defined(HAVE_MLOCK)
|
|
|
|
if (munlock (pages, sz) < 0 && egg_secure_warnings)
|
|
|
|
fprintf (stderr, "couldn't unlock private memory: %s\n", strerror (errno));
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
if (munmap (pages, sz) < 0 && egg_secure_warnings)
|
|
|
|
fprintf (stderr, "couldn't unmap private anonymous memory: %s\n", strerror (errno));
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
DEBUG_ALLOC ("gkr-secure-memory: freed block ", sz);
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
#else
|
|
|
|
ASSERT (FALSE);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -----------------------------------------------------------------------------
|
|
|
|
* MANAGE DIFFERENT BLOCKS
|
|
|
|
*/
|
|
|
|
|
|
|
|
static Block *all_blocks = NULL;
|
|
|
|
|
2012-10-27 09:58:13 +00:00
|
|
|
static Block*
|
2011-09-25 06:22:36 +00:00
|
|
|
sec_block_create (size_t size,
|
|
|
|
const char *during_tag)
|
2011-06-12 20:55:02 +00:00
|
|
|
{
|
|
|
|
Block *block;
|
|
|
|
Cell *cell;
|
|
|
|
|
2011-09-25 06:22:36 +00:00
|
|
|
ASSERT (during_tag);
|
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
/* We can force all all memory to be malloced */
|
2011-11-06 12:41:21 +00:00
|
|
|
if (getenv ("SECMEM_FORCE_FALLBACK"))
|
|
|
|
return NULL;
|
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
block = pool_alloc ();
|
|
|
|
if (!block)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
cell = pool_alloc ();
|
|
|
|
if (!cell) {
|
|
|
|
pool_free (block);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The size above is a minimum, we're free to go bigger */
|
|
|
|
if (size < DEFAULT_BLOCK_SIZE)
|
|
|
|
size = DEFAULT_BLOCK_SIZE;
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-09-25 06:22:36 +00:00
|
|
|
block->words = sec_acquire_pages (&size, during_tag);
|
2011-06-12 20:55:02 +00:00
|
|
|
block->n_words = size / sizeof (word_t);
|
|
|
|
if (!block->words) {
|
|
|
|
pool_free (block);
|
|
|
|
pool_free (cell);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
#ifdef WITH_VALGRIND
|
|
|
|
VALGRIND_MAKE_MEM_DEFINED (block->words, size);
|
|
|
|
#endif
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
/* The first cell to allocate from */
|
|
|
|
cell->words = block->words;
|
|
|
|
cell->n_words = block->n_words;
|
2011-09-25 06:22:36 +00:00
|
|
|
cell->requested = 0;
|
2011-06-12 20:55:02 +00:00
|
|
|
sec_write_guards (cell);
|
2011-09-25 06:22:36 +00:00
|
|
|
sec_insert_cell_ring (&block->unused_cells, cell);
|
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
block->next = all_blocks;
|
|
|
|
all_blocks = block;
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sec_block_destroy (Block *block)
|
|
|
|
{
|
|
|
|
Block *bl, **at;
|
|
|
|
Cell *cell;
|
|
|
|
|
|
|
|
ASSERT (block);
|
|
|
|
ASSERT (block->words);
|
2011-09-25 06:22:36 +00:00
|
|
|
ASSERT (block->n_used == 0);
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
/* Remove from the list */
|
|
|
|
for (at = &all_blocks, bl = *at; bl; at = &bl->next, bl = *at) {
|
|
|
|
if (bl == block) {
|
|
|
|
*at = block->next;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
/* Must have been found */
|
|
|
|
ASSERT (bl == block);
|
2011-09-25 06:22:36 +00:00
|
|
|
ASSERT (block->used_cells == NULL);
|
2011-06-12 20:55:02 +00:00
|
|
|
|
|
|
|
/* Release all the meta data cells */
|
2011-09-25 06:22:36 +00:00
|
|
|
while (block->unused_cells) {
|
|
|
|
cell = block->unused_cells;
|
|
|
|
sec_remove_cell_ring (&block->unused_cells, cell);
|
2011-06-12 20:55:02 +00:00
|
|
|
pool_free (cell);
|
|
|
|
}
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
/* Release all pages of secure memory */
|
|
|
|
sec_release_pages (block->words, block->n_words * sizeof (word_t));
|
|
|
|
|
|
|
|
pool_free (block);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------
|
|
|
|
* PUBLIC FUNCTIONALITY
|
|
|
|
*/
|
|
|
|
|
|
|
|
void*
|
2011-09-25 06:22:36 +00:00
|
|
|
egg_secure_alloc_full (const char *tag,
|
|
|
|
size_t length,
|
|
|
|
int flags)
|
2011-06-12 20:55:02 +00:00
|
|
|
{
|
|
|
|
Block *block;
|
|
|
|
void *memory = NULL;
|
2011-09-25 06:22:36 +00:00
|
|
|
|
|
|
|
if (tag == NULL)
|
|
|
|
tag = "?";
|
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
if (length > 0xFFFFFFFF / 2) {
|
|
|
|
if (egg_secure_warnings)
|
2012-10-27 09:58:13 +00:00
|
|
|
fprintf (stderr, "tried to allocate an insane amount of memory: %lu\n",
|
|
|
|
(unsigned long)length);
|
2011-06-12 20:55:02 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Can't allocate zero bytes */
|
|
|
|
if (length == 0)
|
|
|
|
return NULL;
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
DO_LOCK ();
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
for (block = all_blocks; block; block = block->next) {
|
2011-09-25 06:22:36 +00:00
|
|
|
memory = sec_alloc (block, tag, length);
|
2011-06-12 20:55:02 +00:00
|
|
|
if (memory)
|
2012-10-27 09:58:13 +00:00
|
|
|
break;
|
2011-06-12 20:55:02 +00:00
|
|
|
}
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
/* None of the current blocks have space, allocate new */
|
|
|
|
if (!memory) {
|
2011-09-25 06:22:36 +00:00
|
|
|
block = sec_block_create (length, tag);
|
2011-06-12 20:55:02 +00:00
|
|
|
if (block)
|
2011-09-25 06:22:36 +00:00
|
|
|
memory = sec_alloc (block, tag, length);
|
2011-06-12 20:55:02 +00:00
|
|
|
}
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
#ifdef WITH_VALGRIND
|
|
|
|
if (memory != NULL)
|
|
|
|
VALGRIND_MALLOCLIKE_BLOCK (memory, length, sizeof (void*), 1);
|
|
|
|
#endif
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
DO_UNLOCK ();
|
2011-09-25 06:22:36 +00:00
|
|
|
|
2012-10-27 09:58:13 +00:00
|
|
|
if (!memory && (flags & EGG_SECURE_USE_FALLBACK) && EGG_SECURE_GLOBALS.fallback != NULL) {
|
|
|
|
memory = EGG_SECURE_GLOBALS.fallback (NULL, length);
|
2011-06-12 20:55:02 +00:00
|
|
|
if (memory) /* Our returned memory is always zeroed */
|
|
|
|
memset (memory, 0, length);
|
|
|
|
}
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
if (!memory)
|
|
|
|
errno = ENOMEM;
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
return memory;
|
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
2011-09-25 06:22:36 +00:00
|
|
|
egg_secure_realloc_full (const char *tag,
|
|
|
|
void *memory,
|
|
|
|
size_t length,
|
|
|
|
int flags)
|
2011-06-12 20:55:02 +00:00
|
|
|
{
|
|
|
|
Block *block = NULL;
|
|
|
|
size_t previous = 0;
|
|
|
|
int donew = 0;
|
|
|
|
void *alloc = NULL;
|
2011-09-25 06:22:36 +00:00
|
|
|
|
|
|
|
if (tag == NULL)
|
|
|
|
tag = "?";
|
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
if (length > 0xFFFFFFFF / 2) {
|
|
|
|
if (egg_secure_warnings)
|
2012-10-27 09:58:13 +00:00
|
|
|
fprintf (stderr, "tried to allocate an insane amount of memory: %lu\n",
|
2011-06-12 20:55:02 +00:00
|
|
|
(unsigned long)length);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
if (memory == NULL)
|
2011-09-25 06:22:36 +00:00
|
|
|
return egg_secure_alloc_full (tag, length, flags);
|
2011-06-12 20:55:02 +00:00
|
|
|
if (!length) {
|
|
|
|
egg_secure_free_full (memory, flags);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
DO_LOCK ();
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
/* Find out where it belongs to */
|
|
|
|
for (block = all_blocks; block; block = block->next) {
|
|
|
|
if (sec_is_valid_word (block, memory)) {
|
|
|
|
previous = sec_allocated (block, memory);
|
|
|
|
|
|
|
|
#ifdef WITH_VALGRIND
|
|
|
|
/* Let valgrind think we are unallocating so that it'll validate */
|
|
|
|
VALGRIND_FREELIKE_BLOCK (memory, sizeof (word_t));
|
|
|
|
#endif
|
|
|
|
|
2011-09-25 06:22:36 +00:00
|
|
|
alloc = sec_realloc (block, tag, memory, length);
|
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
#ifdef WITH_VALGRIND
|
|
|
|
/* Now tell valgrind about either the new block or old one */
|
2012-10-27 09:58:13 +00:00
|
|
|
VALGRIND_MALLOCLIKE_BLOCK (alloc ? alloc : memory,
|
|
|
|
alloc ? length : previous,
|
2011-06-12 20:55:02 +00:00
|
|
|
sizeof (word_t), 1);
|
2012-10-27 09:58:13 +00:00
|
|
|
#endif
|
2011-06-12 20:55:02 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If it didn't work we may need to allocate a new block */
|
|
|
|
if (block && !alloc)
|
|
|
|
donew = 1;
|
|
|
|
|
2011-09-25 06:22:36 +00:00
|
|
|
if (block && block->n_used == 0)
|
2011-06-12 20:55:02 +00:00
|
|
|
sec_block_destroy (block);
|
2012-10-27 09:58:13 +00:00
|
|
|
|
|
|
|
DO_UNLOCK ();
|
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
if (!block) {
|
2012-10-27 09:58:13 +00:00
|
|
|
if ((flags & EGG_SECURE_USE_FALLBACK) && EGG_SECURE_GLOBALS.fallback) {
|
|
|
|
/*
|
|
|
|
* In this case we can't zero the returned memory,
|
2011-06-12 20:55:02 +00:00
|
|
|
* because we don't know what the block size was.
|
|
|
|
*/
|
2012-10-27 09:58:13 +00:00
|
|
|
return EGG_SECURE_GLOBALS.fallback (memory, length);
|
2011-06-12 20:55:02 +00:00
|
|
|
} else {
|
|
|
|
if (egg_secure_warnings)
|
2012-10-27 09:58:13 +00:00
|
|
|
fprintf (stderr, "memory does not belong to secure memory pool: 0x%08lx\n",
|
2011-06-12 20:55:02 +00:00
|
|
|
(unsigned long)memory);
|
2012-10-27 09:58:13 +00:00
|
|
|
ASSERT (0 && "memory does does not belong to secure memory pool");
|
2011-06-12 20:55:02 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2011-09-25 06:22:36 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
if (donew) {
|
2011-09-25 06:22:36 +00:00
|
|
|
alloc = egg_secure_alloc_full (tag, length, flags);
|
2011-06-12 20:55:02 +00:00
|
|
|
if (alloc) {
|
2011-11-06 12:41:21 +00:00
|
|
|
memcpy_with_vbits (alloc, memory, previous);
|
2011-06-12 20:55:02 +00:00
|
|
|
egg_secure_free_full (memory, flags);
|
|
|
|
}
|
|
|
|
}
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
if (!alloc)
|
|
|
|
errno = ENOMEM;
|
|
|
|
|
|
|
|
return alloc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
egg_secure_free (void *memory)
|
|
|
|
{
|
2011-09-25 06:22:36 +00:00
|
|
|
egg_secure_free_full (memory, EGG_SECURE_USE_FALLBACK);
|
2011-06-12 20:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
egg_secure_free_full (void *memory, int flags)
|
|
|
|
{
|
|
|
|
Block *block = NULL;
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
if (memory == NULL)
|
|
|
|
return;
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
DO_LOCK ();
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
/* Find out where it belongs to */
|
|
|
|
for (block = all_blocks; block; block = block->next) {
|
|
|
|
if (sec_is_valid_word (block, memory))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef WITH_VALGRIND
|
|
|
|
/* We like valgrind's warnings, so give it a first whack at checking for errors */
|
2011-11-06 12:41:21 +00:00
|
|
|
if (block != NULL || !(flags & EGG_SECURE_USE_FALLBACK))
|
2011-06-12 20:55:02 +00:00
|
|
|
VALGRIND_FREELIKE_BLOCK (memory, sizeof (word_t));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (block != NULL) {
|
|
|
|
sec_free (block, memory);
|
2011-09-25 06:22:36 +00:00
|
|
|
if (block->n_used == 0)
|
2011-06-12 20:55:02 +00:00
|
|
|
sec_block_destroy (block);
|
|
|
|
}
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
DO_UNLOCK ();
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
if (!block) {
|
2012-10-27 09:58:13 +00:00
|
|
|
if ((flags & EGG_SECURE_USE_FALLBACK) && EGG_SECURE_GLOBALS.fallback) {
|
|
|
|
EGG_SECURE_GLOBALS.fallback (memory, 0);
|
2011-06-12 20:55:02 +00:00
|
|
|
} else {
|
|
|
|
if (egg_secure_warnings)
|
2012-10-27 09:58:13 +00:00
|
|
|
fprintf (stderr, "memory does not belong to secure memory pool: 0x%08lx\n",
|
2011-06-12 20:55:02 +00:00
|
|
|
(unsigned long)memory);
|
2012-10-27 09:58:13 +00:00
|
|
|
ASSERT (0 && "memory does does not belong to secure memory pool");
|
2011-06-12 20:55:02 +00:00
|
|
|
}
|
|
|
|
}
|
2012-10-27 09:58:13 +00:00
|
|
|
}
|
2011-06-12 20:55:02 +00:00
|
|
|
|
2012-10-27 09:58:13 +00:00
|
|
|
int
|
2011-06-12 20:55:02 +00:00
|
|
|
egg_secure_check (const void *memory)
|
|
|
|
{
|
|
|
|
Block *block = NULL;
|
|
|
|
|
|
|
|
DO_LOCK ();
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
/* Find out where it belongs to */
|
|
|
|
for (block = all_blocks; block; block = block->next) {
|
|
|
|
if (sec_is_valid_word (block, (word_t*)memory))
|
|
|
|
break;
|
|
|
|
}
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
DO_UNLOCK ();
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
return block == NULL ? 0 : 1;
|
2012-10-27 09:58:13 +00:00
|
|
|
}
|
2011-06-12 20:55:02 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
egg_secure_validate (void)
|
|
|
|
{
|
|
|
|
Block *block = NULL;
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
DO_LOCK ();
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
for (block = all_blocks; block; block = block->next)
|
|
|
|
sec_validate (block);
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
DO_UNLOCK ();
|
|
|
|
}
|
|
|
|
|
2011-09-25 06:22:36 +00:00
|
|
|
|
|
|
|
static egg_secure_rec *
|
|
|
|
records_for_ring (Cell *cell_ring,
|
|
|
|
egg_secure_rec *records,
|
|
|
|
unsigned int *count,
|
|
|
|
unsigned int *total)
|
|
|
|
{
|
|
|
|
egg_secure_rec *new_rec;
|
|
|
|
unsigned int allocated = *count;
|
|
|
|
Cell *cell;
|
|
|
|
|
|
|
|
cell = cell_ring;
|
|
|
|
do {
|
|
|
|
if (*count >= allocated) {
|
|
|
|
new_rec = realloc (records, sizeof (egg_secure_rec) * (allocated + 32));
|
|
|
|
if (new_rec == NULL) {
|
|
|
|
*count = 0;
|
|
|
|
free (records);
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
records = new_rec;
|
|
|
|
allocated += 32;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cell != NULL) {
|
|
|
|
records[*count].request_length = cell->requested;
|
|
|
|
records[*count].block_length = cell->n_words * sizeof (word_t);
|
|
|
|
records[*count].tag = cell->tag;
|
|
|
|
(*count)++;
|
|
|
|
(*total) += cell->n_words;
|
|
|
|
cell = cell->next;
|
|
|
|
}
|
|
|
|
} while (cell != NULL && cell != cell_ring);
|
|
|
|
|
|
|
|
return records;
|
|
|
|
}
|
|
|
|
|
|
|
|
egg_secure_rec *
|
|
|
|
egg_secure_records (unsigned int *count)
|
2011-06-12 20:55:02 +00:00
|
|
|
{
|
2011-09-25 06:22:36 +00:00
|
|
|
egg_secure_rec *records = NULL;
|
2011-06-12 20:55:02 +00:00
|
|
|
Block *block = NULL;
|
2011-09-25 06:22:36 +00:00
|
|
|
unsigned int total;
|
|
|
|
|
|
|
|
*count = 0;
|
2011-06-12 20:55:02 +00:00
|
|
|
|
|
|
|
DO_LOCK ();
|
2011-09-25 06:22:36 +00:00
|
|
|
|
|
|
|
for (block = all_blocks; block != NULL; block = block->next) {
|
|
|
|
total = 0;
|
|
|
|
|
|
|
|
records = records_for_ring (block->unused_cells, records, count, &total);
|
|
|
|
if (records == NULL)
|
|
|
|
break;
|
|
|
|
records = records_for_ring (block->used_cells, records, count, &total);
|
|
|
|
if (records == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Make sure this actualy accounts for all memory */
|
|
|
|
ASSERT (total == block->n_words);
|
2011-06-12 20:55:02 +00:00
|
|
|
}
|
2011-09-25 06:22:36 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
DO_UNLOCK ();
|
2011-09-25 06:22:36 +00:00
|
|
|
|
|
|
|
return records;
|
2011-06-12 20:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
2011-09-25 06:22:36 +00:00
|
|
|
egg_secure_strdup_full (const char *tag,
|
|
|
|
const char *str,
|
|
|
|
int options)
|
2011-06-12 20:55:02 +00:00
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
char *res;
|
2011-09-25 06:22:36 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
if (!str)
|
|
|
|
return NULL;
|
2011-09-25 06:22:36 +00:00
|
|
|
|
2012-10-27 09:58:13 +00:00
|
|
|
len = strlen (str) + 1;
|
2011-09-25 06:22:36 +00:00
|
|
|
res = (char *)egg_secure_alloc_full (tag, len, options);
|
2011-06-12 20:55:02 +00:00
|
|
|
strcpy (res, str);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-03-25 10:17:03 +00:00
|
|
|
char *
|
|
|
|
egg_secure_strndup_full (const char *tag,
|
|
|
|
const char *str,
|
|
|
|
size_t length,
|
|
|
|
int options)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
char *res;
|
|
|
|
const char *end;
|
|
|
|
|
|
|
|
if (!str)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
end = memchr (str, '\0', length);
|
|
|
|
if (end != NULL)
|
|
|
|
length = (end - str);
|
|
|
|
len = length + 1;
|
|
|
|
res = (char *)egg_secure_alloc_full (tag, len, options);
|
|
|
|
memcpy (res, str, len);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
void
|
|
|
|
egg_secure_clear (void *p, size_t length)
|
|
|
|
{
|
|
|
|
volatile char *vp;
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
if (p == NULL)
|
|
|
|
return;
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
vp = (volatile char*)p;
|
|
|
|
while (length) {
|
|
|
|
*vp = 0xAA;
|
|
|
|
vp++;
|
|
|
|
length--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
egg_secure_strclear (char *str)
|
|
|
|
{
|
|
|
|
if (!str)
|
|
|
|
return;
|
|
|
|
egg_secure_clear ((unsigned char*)str, strlen (str));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
egg_secure_strfree (char *str)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If we're using unpageable 'secure' memory, then the free call
|
2012-10-27 09:58:13 +00:00
|
|
|
* should zero out the memory, but because on certain platforms
|
2011-06-12 20:55:02 +00:00
|
|
|
* we may be using normal memory, zero it out here just in case.
|
|
|
|
*/
|
2012-10-27 09:58:13 +00:00
|
|
|
|
2011-06-12 20:55:02 +00:00
|
|
|
egg_secure_strclear (str);
|
2011-09-25 06:22:36 +00:00
|
|
|
egg_secure_free_full (str, EGG_SECURE_USE_FALLBACK);
|
2011-06-12 20:55:02 +00:00
|
|
|
}
|