BUG/MINOR: log: set proper smp size for balance log-hash
authorAurelien DARRAGON <adarragon@haproxy.com>
Wed, 5 Mar 2025 11:01:34 +0000 (12:01 +0100)
committerAmaury Denoyelle <adenoyelle@haproxy.com>
Tue, 18 Mar 2025 15:26:12 +0000 (16:26 +0100)
result.data.u.str.size was set to size+1 to take into account terminating
NULL byte as per the comment. But this is wrong because the caller is free
to set size to just the right amount of bytes (without terminating NULL
byte). In fact all smp API functions will not read past str.data so there
is not risk about uninitialized reads, but this leaves an ambiguity for
converters that may use all the smp size to perform transformations, and
since we don't know about the "message" memory origin, we cannot assume
that its size may be greater than size. So we max it out to size just to
be safe.

This bug was not known to cause any issue, it was spotted during code
review. It should be backported in 2.9 with b30bd7a ("MEDIUM: log/balance:
support for the "hash" lb algorithm")

(cherry picked from commit 94a9b0f5deabd49020c8ff535a3404494345b399)
Signed-off-by: Willy Tarreau <w@1wt.eu>
(cherry picked from commit 7e5d59e8bf5f44c57e2cd13427aef5288777fbd2)
[ad: ctx adjustement]
Signed-off-by: Amaury Denoyelle <adenoyelle@haproxy.com>

src/log.c

index c19f6d0..9b4167e 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -3059,8 +3059,7 @@ static inline void __do_send_log_backend(struct proxy *be, struct log_header hdr
                result.data.type = SMP_T_STR;
                result.flags = SMP_F_CONST;
                result.data.u.str.area = message;
-               result.data.u.str.data = size;
-               result.data.u.str.size = size + 1; /* with terminating NULL byte */
+               result.data.u.str.data = result.data.u.str.size = size;
                if (sample_process_cnv(be->lbprm.expr, &result)) {
                        /* gen_hash takes binary input, ensure that we provide such value to it */
                        if (result.data.type == SMP_T_BIN || sample_casts[result.data.type][SMP_T_BIN]) {