]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MINOR: lua: Properly catch alloc errors when parsing lua filter directives
authorChristopher Faulet <cfaulet@haproxy.com>
Fri, 13 Aug 2021 06:33:57 +0000 (08:33 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Fri, 13 Aug 2021 06:42:00 +0000 (08:42 +0200)
When a lua filter declaration is parsed, some allocation errors were not
properly handled. In addition, we must be sure the filter identifier is defined
in lua to duplicate it when the filter configuration is filled.

This patch fix a defect reported in the issue #1347. It only concerns
2.5-dev. No backport needed.

src/hlua.c

index 717380c6bca5f3e263ded3f8e706745f55b564b9..7e68d0b33bc6b0f6f922d6dbc62289ec15ddc3e2 100644 (file)
@@ -10350,10 +10350,8 @@ static int hlua_filter_parse_fct(char **args, int *cur_arg, struct proxy *px,
 
        /* Initialize the filter ops with default callbacks */
        hlua_flt_ops = calloc(1, sizeof(*hlua_flt_ops));
-       if (!hlua_flt_ops) {
-               memprintf(err, "Lua filter '%s' : Lua out of memory error", reg_flt->name);
-               return -1;
-       }
+       if (!hlua_flt_ops)
+               goto error;
        hlua_flt_ops->init              = hlua_filter_init;
        hlua_flt_ops->deinit            = hlua_filter_deinit;
        if (state_id) {
@@ -10396,21 +10394,28 @@ static int hlua_filter_parse_fct(char **args, int *cur_arg, struct proxy *px,
 
        /* Create the filter config */
        conf = calloc(1, sizeof(*conf));
-       if (!conf) {
-               memprintf(err, "Lua filter '%s' : Lua out of memory error", reg_flt->name);
+       if (!conf)
                goto error;
-       }
        conf->reg = reg_flt;
 
        /* duplicate args */
        for (pos = 0; *args[*cur_arg + 1 + pos]; pos++);
        conf->args = calloc(pos + 1, sizeof(*conf->args));
-       for (pos = 0; *args[*cur_arg + 1 + pos]; pos++)
+       if (!conf->args)
+               goto error;
+       for (pos = 0; *args[*cur_arg + 1 + pos]; pos++) {
                conf->args[pos] = strdup(args[*cur_arg + 1 + pos]);
+               if (!conf->args[pos])
+                       goto error;
+       }
        conf->args[pos] = NULL;
        *cur_arg += pos + 1;
 
-       fconf->id    = strdup(flt_id);
+       if (flt_id) {
+               fconf->id    = strdup(flt_id);
+               if (!fconf->id)
+                       goto error;
+       }
        fconf->flags = flt_flags;
        fconf->conf  = conf;
        fconf->ops   = hlua_flt_ops;
@@ -10419,8 +10424,15 @@ static int hlua_filter_parse_fct(char **args, int *cur_arg, struct proxy *px,
        return 0;
 
   error:
+       memprintf(err, "Lua filter '%s' : Lua out of memory error", reg_flt->name);
        free(hlua_flt_ops);
+       if (conf && conf->args) {
+               for (pos = 0; conf->args[pos]; pos++)
+                       free(conf->args[pos]);
+               free(conf->args);
+       }
        free(conf);
+       free((char *)fconf->id);
        lua_settop(L, 0);
        return -1;
 }