Skip to content

Commit

Permalink
BUG/MEDIUM: hlua: properly handle sample func errors in hlua_run_samp…
Browse files Browse the repository at this point in the history
…le_{fetch,conv}()

To execute sample fetches and converters from lua. hlua API leverages the
sample API. Prior to executing the sample func, the arg checker is called
from hlua_run_sample_{fetch,conv}() to detect potential errors.

However, hlua_run_sample_{fetch,conv}() both pass NULL as <err> argument,
but it is wrong for two reasons. First we miss an opportunity to report
precise error messages to help the user know what went wrong during the
check.. and more importantly, some val check functions consider that the
<err> pointer is never NULL. This is the case for example with
check_crypto_hmac(). Because of this, when such val check functions
encounter an error, they will crash the process because they will try
to de-reference NULL.

This bug was discovered and reported by GH user @JB0925 on haproxy#2745.

Perhaps val check functions should make sure that the provided <err>
pointer is != NULL prior to de-referencing it. But since there are
multiple occurences found in the code and the API isn't clear about that,
it is easier to fix the hlua part (caller) for now.

To fix the issue, let's always provide a valid <err> pointer when
leveraging val_arg() check function pointer, and make use of it in case
or error to report relevant message to the user before freeing it.

It should be backported to all stable versions.
  • Loading branch information
Darlelet committed Oct 8, 2024
1 parent d0e0105 commit f88f162
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/hlua.c
Original file line number Diff line number Diff line change
Expand Up @@ -4816,6 +4816,7 @@ __LJMP static int hlua_run_sample_fetch(lua_State *L)
{
struct hlua_smp *hsmp;
struct sample_fetch *f;
char *errmsg = NULL;
struct arg args[ARGM_NBARGS + 1] = {{0}};
int i;
struct sample smp;
Expand Down Expand Up @@ -4847,8 +4848,9 @@ __LJMP static int hlua_run_sample_fetch(lua_State *L)
MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));

/* Run the special args checker. */
if (f->val_args && !f->val_args(args, NULL)) {
hlua_pushfstring_safe(L, "error in arguments");
if (f->val_args && !f->val_args(args, &errmsg)) {
hlua_pushfstring_safe(L, "error in arguments: %s", errmsg);
ha_free(&errmsg);
goto error;
}

Expand Down Expand Up @@ -4938,6 +4940,7 @@ __LJMP static int hlua_run_sample_conv(lua_State *L)
{
struct hlua_smp *hsmp;
struct sample_conv *conv;
char *errmsg = NULL;
struct arg args[ARGM_NBARGS + 1] = {{0}};
int i;
struct sample smp;
Expand All @@ -4961,8 +4964,9 @@ __LJMP static int hlua_run_sample_conv(lua_State *L)
MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));

/* Run the special args checker. */
if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
hlua_pusherror(L, "error in arguments");
if (conv->val_args && !conv->val_args(args, conv, "", 0, &errmsg)) {
hlua_pushfstring_safe(L, "error in arguments: %s", errmsg);
ha_free(&errmsg);
goto error;
}

Expand Down

0 comments on commit f88f162

Please sign in to comment.