BUG/MINOR: pattern: pat_ref_set: fix UAF reported by coverity
authorValentine Krasnobaeva <vkrasnobaeva@haproxy.com>
Mon, 12 Aug 2024 13:32:00 +0000 (15:32 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Tue, 3 Sep 2024 16:49:07 +0000 (18:49 +0200)
commitbf82f46ef01db0fc89a05b14721ca05d186f193b
tree1b2f980e4135730d4544637356d365e1320dd3b9
parentb8523ffaf8c34a29e38120051355115564667c46
BUG/MINOR: pattern: pat_ref_set: fix UAF reported by coverity

memprintf() performs realloc and updates then the pointer to an output buffer,
where it has written the data. So free() is called on the previous buffer
address, if it was provided.

pat_ref_set_elt() uses memprintf() to write its error message as well as
pat_ref_set(). So, when we re-enter into the while loop the second time and
pat_ref_set_elt() has returned, the *err ptr (previous value of *merr) is
already freed by memprintf() from pat_ref_set_el().

'if (!found)' condition is false at this point, because we've found a node at
the first loop. So, the second memprintf(), in order to write error messages,
does again free(*err).

This should be backported in all stable versions.

(cherry picked from commit 4f2493f3551f8c344485cccf075c3b15f9825181)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
src/pattern.c