]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MINOR: mworker: stop doing strtok directly from the env
authorWilliam Lallemand <wlallemand@haproxy.org>
Tue, 21 Feb 2023 11:44:56 +0000 (12:44 +0100)
committerWilliam Lallemand <wlallemand@haproxy.org>
Tue, 21 Feb 2023 12:53:33 +0000 (13:53 +0100)
When parsing the HAPROXY_PROCESSES environement variable, strtok was
done directly from the ptr resulting from getenv(), which replaces the ;
by \0, showing confusing environment variables when debugging in /proc
or in a corefile.

Example:

(gdb) x/39s *environ
[...]
0x7fff6935af64: "HAPROXY_PROCESSES=|type=w"
0x7fff6935af7e: "fd=3"
0x7fff6935af83: "pid=4444"
0x7fff6935af8d: "rpid=1"
0x7fff6935af94: "reloads=0"
0x7fff6935af9e: "timestamp=1676338060"
0x7fff6935afb3: "id="
0x7fff6935afb7: "version=2.4.0-8076da-1010+11"

This patch fixes the issue by doing a strdup on the variable.

Could be backported in previous versions (mworker_proc_to_env_list
exists since 1.9)

src/mworker.c

index 4bc224ee734f37338f62231f84f2ae07da57ddbb..0d788ca3d1072dc08832eebea576a1bc1b872b60 100644 (file)
@@ -166,17 +166,26 @@ struct mworker_proc *mworker_proc_new()
 
 /*
  * unserialize the proc list from the environment
+ * Return < 0 upon error.
  */
 int mworker_env_to_proc_list()
 {
-       char *msg, *token = NULL, *s1;
+       char *env, *msg, *omsg = NULL, *token = NULL, *s1;
        struct mworker_proc *child;
        int minreloads = INT_MAX; /* minimum number of reloads to chose which processes are "current" ones */
+       int err = 0;
 
-       msg = getenv("HAPROXY_PROCESSES");
-       if (!msg)
+       env = getenv("HAPROXY_PROCESSES");
+       if (!env)
                return 0;
 
+       omsg = msg = strdup(env);
+       if (!msg) {
+               ha_alert("Out of memory while trying to allocate a worker process structure.");
+               err = -1;
+               goto out;
+       }
+
        while ((token = strtok_r(msg, "|", &s1))) {
                char *subtoken = NULL;
                char *s2;
@@ -185,8 +194,9 @@ int mworker_env_to_proc_list()
 
                child = mworker_proc_new();
                if (!child) {
-                       ha_alert("Out of memory while trying to allocate a worker process structure.");
-                       return -1;
+                       ha_alert("out of memory while trying to allocate a worker process structure.");
+                       err = -1;
+                       goto out;
                }
 
                while ((subtoken = strtok_r(token, ";", &s2))) {
@@ -244,7 +254,9 @@ int mworker_env_to_proc_list()
 
        unsetenv("HAPROXY_PROCESSES");
 
-       return 0;
+out:
+       free(omsg);
+       return err;
 }
 
 /* Signal blocking and unblocking */