BUG/MINOR: worker: Missing calloc return value check in mworker_env_to_proc_list
authorRemi Tricot-Le Breton <rlebreton@haproxy.com>
Wed, 19 May 2021 08:45:12 +0000 (10:45 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Mon, 31 May 2021 13:31:35 +0000 (15:31 +0200)
A memory allocation failure happening in mworker_env_to_proc_list when
trying to allocate a mworker_proc would have resulted in a crash. This
function is only called during init.

It was raised in GitHub issue #1233.
It could be backported to all stable branches.

(cherry picked from commit 1f4fa906c73e61dba74f9e8b762da12df3052f57)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
(cherry picked from commit d90aa347d2a3a81bae8461c009c9b4d69eb069a5)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>

include/haproxy/mworker.h
src/haproxy.c
src/mworker.c

index edc612e..3de2dd1 100644 (file)
@@ -18,7 +18,7 @@
 #include <haproxy/signal-t.h>
 
 void mworker_proc_list_to_env();
-void mworker_env_to_proc_list();
+int mworker_env_to_proc_list();
 
 
 void mworker_block_signals();
index 62da462..2c24b3b 100644 (file)
@@ -1931,8 +1931,10 @@ static void init(int argc, char **argv)
        if (global.mode & (MODE_MWORKER|MODE_MWORKER_WAIT)) {
                struct wordlist *it, *c;
 
-               mworker_env_to_proc_list(); /* get the info of the children in the env */
-
+               /* get the info of the children in the env */
+               if (mworker_env_to_proc_list() < 0) {
+                       exit(EXIT_FAILURE);
+               }
 
                if (!LIST_ISEMPTY(&mworker_cli_conf)) {
 
index abdc1d3..39e28b2 100644 (file)
@@ -129,13 +129,13 @@ void mworker_proc_list_to_env()
 /*
  * unserialize the proc list from the environment
  */
-void mworker_env_to_proc_list()
+int mworker_env_to_proc_list()
 {
        char *msg, *token = NULL, *s1;
 
        msg = getenv("HAPROXY_PROCESSES");
        if (!msg)
-               return;
+               return 0;
 
        while ((token = strtok_r(msg, "|", &s1))) {
                struct mworker_proc *child;
@@ -145,6 +145,10 @@ void mworker_env_to_proc_list()
                msg = NULL;
 
                child = calloc(1, sizeof(*child));
+               if (!child) {
+                       ha_alert("Out of memory while trying to allocate a worker process structure.");
+                       return -1;
+               }
 
                while ((subtoken = strtok_r(token, ";", &s2))) {
 
@@ -191,6 +195,8 @@ void mworker_env_to_proc_list()
        }
 
        unsetenv("HAPROXY_PROCESSES");
+
+       return 0;
 }
 
 /* Signal blocking and unblocking */