From: Timo Sirainen Date: Mon, 13 Feb 2017 21:46:15 +0000 (+0200) Subject: lib: Add pool_alloconly_create_clean() X-Git-Tag: 2.3.0.rc1~2125 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c2664306fcea8bcaafd19a6d1b683ef0ad0ece91;p=thirdparty%2Fdovecot%2Fcore.git lib: Add pool_alloconly_create_clean() This partially reverts 2a2beae3a4c1e75b3aeff996781503138e6f24bc --- diff --git a/src/lib/mempool-alloconly.c b/src/lib/mempool-alloconly.c index 40594ff30b..5b0a5e9f55 100644 --- a/src/lib/mempool-alloconly.c +++ b/src/lib/mempool-alloconly.c @@ -24,6 +24,7 @@ struct alloconly_pool { size_t base_size; bool disable_warning; #endif + bool clean_frees; }; struct pool_block { @@ -159,6 +160,17 @@ pool_t pool_alloconly_create(const char *name ATTR_UNUSED, size_t size) return &new_apool->pool; } +pool_t pool_alloconly_create_clean(const char *name, size_t size) +{ + struct alloconly_pool *apool; + pool_t pool; + + pool = pool_alloconly_create(name, size); + apool = (struct alloconly_pool *)pool; + apool->clean_frees = TRUE; + return pool; +} + static void pool_alloconly_destroy(struct alloconly_pool *apool) { void *block; @@ -170,7 +182,13 @@ static void pool_alloconly_destroy(struct alloconly_pool *apool) block = apool->block; #ifdef DEBUG safe_memset(block, CLEAR_CHR, SIZEOF_POOLBLOCK + apool->block->size); +#else + if (apool->clean_frees) { + safe_memset(block, CLEAR_CHR, + SIZEOF_POOLBLOCK + apool->block->size); + } #endif + #ifndef USE_GC free(block); #endif @@ -362,6 +380,11 @@ static void pool_alloconly_clear(pool_t pool) #ifdef DEBUG safe_memset(block, CLEAR_CHR, SIZEOF_POOLBLOCK + block->size); +#else + if (apool->clean_frees) { + safe_memset(block, CLEAR_CHR, + SIZEOF_POOLBLOCK + block->size); + } #endif #ifndef USE_GC free(block); diff --git a/src/lib/mempool.h b/src/lib/mempool.h index c796a0a9e0..da7583d9ef 100644 --- a/src/lib/mempool.h +++ b/src/lib/mempool.h @@ -57,6 +57,11 @@ extern pool_t unsafe_data_stack_pool; /* Create a new alloc-only pool. Note that `size' specifies the initial malloc()ed block size, part of it is used internally. */ pool_t pool_alloconly_create(const char *name, size_t size); +/* Like alloconly pool, but clear the memory before freeing it. The idea is + that you could allocate memory for storing sensitive information from this + pool, and be sure that it gets cleared from the memory when it's no longer + needed. */ +pool_t pool_alloconly_create_clean(const char *name, size_t size); /* When allocating memory from returned pool, the data stack frame must be the same as it was when calling this function. pool_unref() also checks