DEBUG: pools: replace the link pointer with the caller's address on pool_free()
authorWilly Tarreau <w@1wt.eu>
Wed, 9 Feb 2022 15:49:16 +0000 (16:49 +0100)
committerWilly Tarreau <w@1wt.eu>
Tue, 15 Feb 2022 06:47:01 +0000 (07:47 +0100)
commit8421b0c59f87f6b66683991896aae1be776f992b
treef0122bcfc51655ce5217f1f40837be749cf83d0d
parent889da687a09579bc78d9978928ebb96cc02cff01
DEBUG: pools: replace the link pointer with the caller's address on pool_free()

Along recent evolutions of the pools, we've lost the ability to reliably
detect double-frees because while in the past the same pointer was being
used to chain the objects in the cache and to store the pool's address,
since 2.0 they're different so the pool's address is never overwritten on
free() and a double-free will rarely be detected.

This patch sets the caller's return address there. It can never be equal
to a pool's address and will help guess what was the previous call path.
It will not work on exotic architectures nor with very old compilers but
these are not the environments where we're trying to get detailed bug
reports, and this is not done by default anyway so we don't care about
this limitation. Note that depending on the inlining status of the
function, the result may differ but that's no big deal either.

A test by placing a double free of an appctx inside the release handler
itself successfully reported the trouble during appctx_free() and showed
that the return address was in stream_int_shutw_applet() (this one calls
the release handler).

(cherry picked from commit 27c8da1fd5e4f906fd3a9f96a4284c886b559305)
[wt: simplified for 2.5 and older, just use POOL_LINK()]
Signed-off-by: Willy Tarreau <w@1wt.eu>
include/haproxy/pool.h