Fix: using putenv() and free()-ing the value is invalid
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 30 Nov 2017 23:18:03 +0000 (00:18 +0100)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 30 Nov 2017 23:18:03 +0000 (00:18 +0100)
putenv() does not copy the string passed as the parameter. Hence,
free()-ing the string results in an invalid environment. In the
"good" case, we don't care since we execl().

However, on error, our process now has an invalid environment
which can cause breakage further down the line.

Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
src/bin/lttng-sessiond/main.c

index 968fec84a1d6bc80299cbbcd0aa2241de84f5012..297db20ce062201630bd96ab91f268b79897916b 100644 (file)
@@ -2450,20 +2450,18 @@ static pid_t spawn_consumerd(struct consumer_data *consumer_data)
                                if (!tmp) {
                                        tmp = "";
                                }
-                               tmplen = strlen("LD_LIBRARY_PATH=")
-                                       + strlen(config.consumerd64_lib_dir.value) + 1 /* : */ + strlen(tmp);
+                               tmplen = strlen(config.consumerd64_lib_dir.value) + 1 /* : */ + strlen(tmp);
                                tmpnew = zmalloc(tmplen + 1 /* \0 */);
                                if (!tmpnew) {
                                        ret = -ENOMEM;
                                        goto error;
                                }
-                               strcpy(tmpnew, "LD_LIBRARY_PATH=");
                                strcat(tmpnew, config.consumerd64_lib_dir.value);
                                if (tmp[0] != '\0') {
                                        strcat(tmpnew, ":");
                                        strcat(tmpnew, tmp);
                                }
-                               ret = putenv(tmpnew);
+                               ret = setenv("LD_LIBRARY_PATH", tmpnew, 1);
                                if (ret) {
                                        ret = -errno;
                                        free(tmpnew);
@@ -2491,20 +2489,18 @@ static pid_t spawn_consumerd(struct consumer_data *consumer_data)
                                if (!tmp) {
                                        tmp = "";
                                }
-                               tmplen = strlen("LD_LIBRARY_PATH=")
-                                       + strlen(config.consumerd32_lib_dir.value) + 1 /* : */ + strlen(tmp);
+                               tmplen = strlen(config.consumerd32_lib_dir.value) + 1 /* : */ + strlen(tmp);
                                tmpnew = zmalloc(tmplen + 1 /* \0 */);
                                if (!tmpnew) {
                                        ret = -ENOMEM;
                                        goto error;
                                }
-                               strcpy(tmpnew, "LD_LIBRARY_PATH=");
                                strcat(tmpnew, config.consumerd32_lib_dir.value);
                                if (tmp[0] != '\0') {
                                        strcat(tmpnew, ":");
                                        strcat(tmpnew, tmp);
                                }
-                               ret = putenv(tmpnew);
+                               ret = setenv("LD_LIBRARY_PATH", tmpnew, 1);
                                if (ret) {
                                        ret = -errno;
                                        free(tmpnew);
This page took 0.028398 seconds and 4 git commands to generate.