BUG/MINOR: pools: fix a possible memory leak in the lockless pool_flush()
authorWilly Tarreau <w@1wt.eu>
Thu, 10 Jun 2021 04:54:22 +0000 (06:54 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 16 Jun 2021 09:31:19 +0000 (11:31 +0200)
The lockless version of pool_flush() had a leftover of the original
version causing the pool's first entry to be set to NULL at the end.
The problem is that it does this outside of any lock and in a non-
atomic way, so that any concurrent alloc+free would result in a lost
object.

The risk is low and the consequence even lower, given that pool_flush()
is only used in pool_destroy() (hence single-threaded) or by stream_free()
during a soft-stop (not the place where most allocations happen), so in
the worst case it could result in valgrind complaining on soft-stop.

The bug was introduced with the first version of the code, in 1.9, so
the fix can be backported to all stable versions.

(cherry picked from commit c239cde26fbf0b07abdf590b77c31154bd65c566)
Signed-off-by: Willy Tarreau <w@1wt.eu>
(cherry picked from commit caf6555f418767a1298aa7e273609fc1510184db)
[wt: adjusted context: pool->allocated still updated last]
Signed-off-by: Willy Tarreau <w@1wt.eu>

src/pool.c

index d9be4b7..183b0e7 100644 (file)
@@ -324,9 +324,8 @@ void pool_flush(struct pool_head *pool)
                removed++;
                pool_free_area(temp, pool->size + POOL_EXTRA);
        }
-       pool->free_list = next;
        _HA_ATOMIC_SUB(&pool->allocated, removed);
-       /* here, we should have pool->allocate == pool->used */
+       /* here, we should have pool->allocated == pool->used */
 }
 
 /*