BUG/MEDIUM: ring: invert the length check to avoid an int overflow
authorWilly Tarreau <w@1wt.eu>
Wed, 17 Sep 2025 16:45:13 +0000 (18:45 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Wed, 1 Oct 2025 14:48:35 +0000 (16:48 +0200)
commit12fa138575874f911e1dddc0f6d46f6fee990952
tree876233f7d64a217f99c8737cd1331a15e914cb9b
parente8600734c2c75b2de1e37b37e03c4b3e04ae1ad7
BUG/MEDIUM: ring: invert the length check to avoid an int overflow

Vincent Gramer reported in GH issue #3125 a case of crash on a BUG_ON()
condition in the rings. What happens is that a message that is one byte
less than the maximum ring size is emitted, and it passes all the checks,
but once inflated by the extra +1 for the refcount, it can no longer. But
the check was made based on message size compared to space left, except
that this space left can now be negative, which is a high positive for
size_t, so the check remained valid and triggered a BUG_ON() later.

Let's compute the size the other way around instead (i.e. current +
needed) since we can't have rings as large as half of the memory space
anyway, thus we have no risk of overflow on this one.

This needs to be backported to all versions supporting multi-threaded
rings (3.0 and above).

Thanks to Vincent for the easy and working reproducer.

(cherry picked from commit d53ad49ad1713ee874097fff1b4a777141ca2a49)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
(cherry picked from commit 800da9eb7f13029b2f31cbafab13e4ddbbe5a2de)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
(cherry picked from commit 26a640832f0bdc4ccb50d7933bce262d92a49a08)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
src/ring.c