changeset 21534:576ae10cd6cc

lib: Add pool_alloconly_create_clean() This partially reverts 2a2beae3a4c1e75b3aeff996781503138e6f24bc
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Mon, 13 Feb 2017 23:46:15 +0200
parents eb9ee9ea67eb
children fc183b141006
files src/lib/mempool-alloconly.c src/lib/mempool.h
diffstat 2 files changed, 28 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib/mempool-alloconly.c	Mon Feb 13 20:02:24 2017 +0200
+++ b/src/lib/mempool-alloconly.c	Mon Feb 13 23:46:15 2017 +0200
@@ -24,6 +24,7 @@
 	size_t base_size;
 	bool disable_warning;
 #endif
+	bool clean_frees;
 };
 
 struct pool_block {
@@ -159,6 +160,17 @@
 	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 @@
 	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 @@
 
 #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);
--- a/src/lib/mempool.h	Mon Feb 13 20:02:24 2017 +0200
+++ b/src/lib/mempool.h	Mon Feb 13 23:46:15 2017 +0200
@@ -57,6 +57,11 @@
 /* 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