summaryrefslogtreecommitdiffstats
path: root/crypto/mem_sec.c
diff options
context:
space:
mode:
authorTodd Short <tshort@akamai.com>2016-04-11 16:03:42 -0400
committerRich Salz <rsalz@openssl.org>2016-05-02 12:58:03 -0400
commite8408681b3fff91b794a1a5c65fd190019d6e9ee (patch)
tree04f3c0e8dc43687b4f323e3e33c8a52861aeab64 /crypto/mem_sec.c
parentfbaf30d087a2db2b4e22279e819d481fca21ac5c (diff)
Secure memory fixes
Fix some of the variables to be (s)size_t, so that more than 1GB of secure memory can be allocated. The arena has to be a power of 2, and 2GB fails because it ends up being a negative 32-bit signed number. The |too_late| flag is not strictly necessary; it is easy to figure out if something is secure memory by looking at the arena. As before, secure memory allocations will not fail, but now they can be freed correctly. Once initialized, secure memory can still be used, even if allocations occured before initialization. Reviewed-by: Richard Levitte <levitte@openssl.org> Reviewed-by: Rich Salz <rsalz@openssl.org>
Diffstat (limited to 'crypto/mem_sec.c')
-rw-r--r--crypto/mem_sec.c70
1 files changed, 35 insertions, 35 deletions
diff --git a/crypto/mem_sec.c b/crypto/mem_sec.c
index 31c052512d..d61d945d63 100644
--- a/crypto/mem_sec.c
+++ b/crypto/mem_sec.c
@@ -37,7 +37,6 @@
static size_t secure_mem_used;
static int secure_mem_initialized;
-static int too_late;
static CRYPTO_RWLOCK *sec_malloc_lock = NULL;
@@ -48,7 +47,7 @@ static int sh_init(size_t size, int minsize);
static char *sh_malloc(size_t size);
static void sh_free(char *ptr);
static void sh_done(void);
-static int sh_actual_size(char *ptr);
+static size_t sh_actual_size(char *ptr);
static int sh_allocated(const char *ptr);
#endif
@@ -57,10 +56,6 @@ int CRYPTO_secure_malloc_init(size_t size, int minsize)
#ifdef IMPLEMENTED
int ret = 0;
- if (too_late)
- return ret;
-
- OPENSSL_assert(!secure_mem_initialized);
if (!secure_mem_initialized) {
sec_malloc_lock = CRYPTO_THREAD_lock_new();
if (sec_malloc_lock == NULL)
@@ -75,13 +70,17 @@ int CRYPTO_secure_malloc_init(size_t size, int minsize)
#endif /* IMPLEMENTED */
}
-void CRYPTO_secure_malloc_done()
+int CRYPTO_secure_malloc_done()
{
#ifdef IMPLEMENTED
- sh_done();
- secure_mem_initialized = 0;
- CRYPTO_THREAD_lock_free(sec_malloc_lock);
+ if (secure_mem_used == 0) {
+ sh_done();
+ secure_mem_initialized = 0;
+ CRYPTO_THREAD_lock_free(sec_malloc_lock);
+ return 1;
+ }
#endif /* IMPLEMENTED */
+ return 0;
}
int CRYPTO_secure_malloc_initialized()
@@ -100,7 +99,6 @@ void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
size_t actual_size;
if (!secure_mem_initialized) {
- too_late = 1;
return CRYPTO_malloc(num, file, line);
}
CRYPTO_THREAD_write_lock(sec_malloc_lock);
@@ -130,7 +128,7 @@ void CRYPTO_secure_free(void *ptr, const char *file, int line)
if (ptr == NULL)
return;
- if (!secure_mem_initialized) {
+ if (!CRYPTO_secure_allocated(ptr)) {
CRYPTO_free(ptr, file, line);
return;
}
@@ -208,9 +206,11 @@ size_t CRYPTO_secure_actual_size(void *ptr)
* place.
*/
-# define TESTBIT(t, b) (t[(b) >> 3] & (1 << ((b) & 7)))
-# define SETBIT(t, b) (t[(b) >> 3] |= (1 << ((b) & 7)))
-# define CLEARBIT(t, b) (t[(b) >> 3] &= (0xFF & ~(1 << ((b) & 7))))
+#define ONE ((size_t)1)
+
+# define TESTBIT(t, b) (t[(b) >> 3] & (ONE << ((b) & 7)))
+# define SETBIT(t, b) (t[(b) >> 3] |= (ONE << ((b) & 7)))
+# define CLEARBIT(t, b) (t[(b) >> 3] &= (0xFF & ~(ONE << ((b) & 7))))
#define WITHIN_ARENA(p) \
((char*)(p) >= sh.arena && (char*)(p) < &sh.arena[sh.arena_size])
@@ -229,21 +229,21 @@ typedef struct sh_st
char* map_result;
size_t map_size;
char *arena;
- int arena_size;
+ size_t arena_size;
char **freelist;
- int freelist_size;
- int minsize;
+ ossl_ssize_t freelist_size;
+ size_t minsize;
unsigned char *bittable;
unsigned char *bitmalloc;
- int bittable_size; /* size in bits */
+ size_t bittable_size; /* size in bits */
} SH;
static SH sh;
-static int sh_getlist(char *ptr)
+static size_t sh_getlist(char *ptr)
{
- int list = sh.freelist_size - 1;
- int bit = (sh.arena_size + ptr - sh.arena) / sh.minsize;
+ ossl_ssize_t list = sh.freelist_size - 1;
+ size_t bit = (sh.arena_size + ptr - sh.arena) / sh.minsize;
for (; bit; bit >>= 1, list--) {
if (TESTBIT(sh.bittable, bit))
@@ -257,22 +257,22 @@ static int sh_getlist(char *ptr)
static int sh_testbit(char *ptr, int list, unsigned char *table)
{
- int bit;
+ size_t bit;
OPENSSL_assert(list >= 0 && list < sh.freelist_size);
OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
- bit = (1 << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
+ bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
return TESTBIT(table, bit);
}
static void sh_clearbit(char *ptr, int list, unsigned char *table)
{
- int bit;
+ size_t bit;
OPENSSL_assert(list >= 0 && list < sh.freelist_size);
OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
- bit = (1 << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
+ bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
OPENSSL_assert(TESTBIT(table, bit));
CLEARBIT(table, bit);
@@ -280,11 +280,11 @@ static void sh_clearbit(char *ptr, int list, unsigned char *table)
static void sh_setbit(char *ptr, int list, unsigned char *table)
{
- int bit;
+ size_t bit;
OPENSSL_assert(list >= 0 && list < sh.freelist_size);
OPENSSL_assert(((ptr - sh.arena) & ((sh.arena_size >> list) - 1)) == 0);
- bit = (1 << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
+ bit = (ONE << list) + ((ptr - sh.arena) / (sh.arena_size >> list));
OPENSSL_assert(bit > 0 && bit < sh.bittable_size);
OPENSSL_assert(!TESTBIT(table, bit));
SETBIT(table, bit);
@@ -449,21 +449,21 @@ static int sh_allocated(const char *ptr)
static char *sh_find_my_buddy(char *ptr, int list)
{
- int bit;
+ size_t bit;
char *chunk = NULL;
- bit = (1 << list) + (ptr - sh.arena) / (sh.arena_size >> list);
+ bit = (ONE << list) + (ptr - sh.arena) / (sh.arena_size >> list);
bit ^= 1;
if (TESTBIT(sh.bittable, bit) && !TESTBIT(sh.bitmalloc, bit))
- chunk = sh.arena + ((bit & ((1 << list) - 1)) * (sh.arena_size >> list));
+ chunk = sh.arena + ((bit & ((ONE << list) - 1)) * (sh.arena_size >> list));
return chunk;
}
static char *sh_malloc(size_t size)
{
- int list, slist;
+ ossl_ssize_t list, slist;
size_t i;
char *chunk;
@@ -522,7 +522,7 @@ static char *sh_malloc(size_t size)
static void sh_free(char *ptr)
{
- int list;
+ size_t list;
char *buddy;
if (ptr == NULL)
@@ -559,7 +559,7 @@ static void sh_free(char *ptr)
}
}
-static int sh_actual_size(char *ptr)
+static size_t sh_actual_size(char *ptr)
{
int list;
@@ -568,6 +568,6 @@ static int sh_actual_size(char *ptr)
return 0;
list = sh_getlist(ptr);
OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
- return sh.arena_size / (1 << list);
+ return sh.arena_size / (ONE << list);
}
#endif /* IMPLEMENTED */