]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Add pool_alloconly_create_clean()
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Mon, 13 Feb 2017 21:46:15 +0000 (23:46 +0200)
committerTimo Sirainen <timo.sirainen@dovecot.fi>
Mon, 13 Feb 2017 21:46:35 +0000 (23:46 +0200)
This partially reverts 2a2beae3a4c1e75b3aeff996781503138e6f24bc

src/lib/mempool-alloconly.c
src/lib/mempool.h

index 40594ff30bc660af790b9db37f976ce197cf7899..5b0a5e9f55fe4fa3625d98ba7a3aa07af1b3f697 100644 (file)
@@ -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);
index c796a0a9e0e4f69e7739786d82c8869955c6ecb5..da7583d9ef125ed2d4f55cf2b5c3cc5aa08cfce7 100644 (file)
@@ -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