naccyde / rpms / systemd

Forked from rpms/systemd a year ago
Clone
594167
From 03bf37877f1c7045724cba12d69e93c8c411646e Mon Sep 17 00:00:00 2001
594167
From: Anita Zhang <the.anitazha@gmail.com>
594167
Date: Wed, 26 Jan 2022 10:53:40 -0800
594167
Subject: [PATCH] tree-wide: don't use strjoina() on getenv() values
594167
594167
Avoid doing stack allocations on environment variables.
594167
594167
(cherry picked from commit 1d3b68f6e1538b6a86cbe3650d8b81df2877ef42)
594167
594167
Related: #2017035
594167
---
594167
 src/basic/path-lookup.c  |  8 ++++++--
594167
 src/core/dbus.c          |  8 ++++++--
594167
 src/home/homed-home.c    | 10 +++++++---
594167
 src/home/homed-manager.c | 26 ++++++++++++++++++--------
594167
 src/run/run.c            |  7 +++++--
594167
 src/shared/pager.c       |  9 +++++++--
594167
 6 files changed, 49 insertions(+), 19 deletions(-)
594167
594167
diff --git a/src/basic/path-lookup.c b/src/basic/path-lookup.c
594167
index 6fb8c40e7a..921a30cef7 100644
594167
--- a/src/basic/path-lookup.c
594167
+++ b/src/basic/path-lookup.c
594167
@@ -238,7 +238,7 @@ static int acquire_generator_dirs(
594167
                 char **generator_early,
594167
                 char **generator_late) {
594167
 
594167
-        _cleanup_free_ char *x = NULL, *y = NULL, *z = NULL;
594167
+        _cleanup_free_ char *x = NULL, *y = NULL, *z = NULL, *p = NULL;
594167
         const char *prefix;
594167
 
594167
         assert(generator);
594167
@@ -261,7 +261,11 @@ static int acquire_generator_dirs(
594167
                 if (!e)
594167
                         return -ENXIO;
594167
 
594167
-                prefix = strjoina(e, "/systemd");
594167
+                p = path_join(e, "/systemd");
594167
+                if (!p)
594167
+                        return -ENOMEM;
594167
+
594167
+                prefix = p;
594167
         }
594167
 
594167
         x = path_join(prefix, "generator");
594167
diff --git a/src/core/dbus.c b/src/core/dbus.c
594167
index 2c5bda58f9..073675ceef 100644
594167
--- a/src/core/dbus.c
594167
+++ b/src/core/dbus.c
594167
@@ -925,14 +925,18 @@ int bus_init_private(Manager *m) {
594167
 
594167
                 r = sockaddr_un_set_path(&sa.un, "/run/systemd/private");
594167
         } else {
594167
-                const char *e, *joined;
594167
+                _cleanup_free_ char *joined = NULL;
594167
+                const char *e;
594167
 
594167
                 e = secure_getenv("XDG_RUNTIME_DIR");
594167
                 if (!e)
594167
                         return log_error_errno(SYNTHETIC_ERRNO(EHOSTDOWN),
594167
                                                "XDG_RUNTIME_DIR is not set, refusing.");
594167
 
594167
-                joined = strjoina(e, "/systemd/private");
594167
+                joined = path_join(e, "/systemd/private");
594167
+                if (!joined)
594167
+                        return log_oom();
594167
+
594167
                 r = sockaddr_un_set_path(&sa.un, joined);
594167
         }
594167
         if (r < 0)
594167
diff --git a/src/home/homed-home.c b/src/home/homed-home.c
594167
index 470c7f07f6..1340cf30d3 100644
594167
--- a/src/home/homed-home.c
594167
+++ b/src/home/homed-home.c
594167
@@ -1185,14 +1185,18 @@ static int home_start_work(Home *h, const char *verb, UserRecord *hr, UserRecord
594167
         if (r < 0)
594167
                 return r;
594167
         if (r == 0) {
594167
+                _cleanup_free_ char *joined = NULL;
594167
                 const char *homework, *suffix, *unix_path;
594167
 
594167
                 /* Child */
594167
 
594167
                 suffix = getenv("SYSTEMD_HOME_DEBUG_SUFFIX");
594167
-                if (suffix)
594167
-                        unix_path = strjoina("/run/systemd/home/notify.", suffix);
594167
-                else
594167
+                if (suffix) {
594167
+                        joined = strjoin("/run/systemd/home/notify.", suffix);
594167
+                        if (!joined)
594167
+                                return log_oom();
594167
+                        unix_path = joined;
594167
+                } else
594167
                         unix_path = "/run/systemd/home/notify";
594167
 
594167
                 if (setenv("NOTIFY_SOCKET", unix_path, 1) < 0) {
594167
diff --git a/src/home/homed-manager.c b/src/home/homed-manager.c
594167
index 6c178b8a0e..c1ec555cac 100644
594167
--- a/src/home/homed-manager.c
594167
+++ b/src/home/homed-manager.c
594167
@@ -936,6 +936,7 @@ int manager_enumerate_images(Manager *m) {
594167
 }
594167
 
594167
 static int manager_connect_bus(Manager *m) {
594167
+        _cleanup_free_ char *b = NULL;
594167
         const char *suffix, *busname;
594167
         int r;
594167
 
594167
@@ -955,9 +956,12 @@ static int manager_connect_bus(Manager *m) {
594167
                 return r;
594167
 
594167
         suffix = getenv("SYSTEMD_HOME_DEBUG_SUFFIX");
594167
-        if (suffix)
594167
-                busname = strjoina("org.freedesktop.home1.", suffix);
594167
-        else
594167
+        if (suffix) {
594167
+                b = strjoin("org.freedesktop.home1.", suffix);
594167
+                if (!b)
594167
+                        return log_oom();
594167
+                busname = b;
594167
+        } else
594167
                 busname = "org.freedesktop.home1";
594167
 
594167
         r = sd_bus_request_name_async(m->bus, NULL, busname, 0, NULL, NULL);
594167
@@ -974,6 +978,7 @@ static int manager_connect_bus(Manager *m) {
594167
 }
594167
 
594167
 static int manager_bind_varlink(Manager *m) {
594167
+        _cleanup_free_ char *p = NULL;
594167
         const char *suffix, *socket_path;
594167
         int r;
594167
 
594167
@@ -999,9 +1004,12 @@ static int manager_bind_varlink(Manager *m) {
594167
         /* To make things easier to debug, when working from a homed managed home directory, let's optionally
594167
          * use a different varlink socket name */
594167
         suffix = getenv("SYSTEMD_HOME_DEBUG_SUFFIX");
594167
-        if (suffix)
594167
-                socket_path = strjoina("/run/systemd/userdb/io.systemd.Home.", suffix);
594167
-        else
594167
+        if (suffix) {
594167
+                p = strjoin("/run/systemd/userdb/io.systemd.Home.", suffix);
594167
+                if (!p)
594167
+                        return log_oom();
594167
+                socket_path = p;
594167
+        } else
594167
                 socket_path = "/run/systemd/userdb/io.systemd.Home";
594167
 
594167
         r = varlink_server_listen_address(m->varlink_server, socket_path, 0666);
594167
@@ -1159,9 +1167,11 @@ static int manager_listen_notify(Manager *m) {
594167
 
594167
         suffix = getenv("SYSTEMD_HOME_DEBUG_SUFFIX");
594167
         if (suffix) {
594167
-                const char *unix_path;
594167
+                _cleanup_free_ char *unix_path = NULL;
594167
 
594167
-                unix_path = strjoina("/run/systemd/home/notify.", suffix);
594167
+                unix_path = strjoin("/run/systemd/home/notify.", suffix);
594167
+                if (!unix_path)
594167
+                        return log_oom();
594167
                 r = sockaddr_un_set_path(&sa.un, unix_path);
594167
                 if (r < 0)
594167
                         return log_error_errno(r, "Socket path %s does not fit in sockaddr_un: %m", unix_path);
594167
diff --git a/src/run/run.c b/src/run/run.c
594167
index ff24373847..e75b027542 100644
594167
--- a/src/run/run.c
594167
+++ b/src/run/run.c
594167
@@ -794,9 +794,12 @@ static int transient_service_set_properties(sd_bus_message *m, const char *pty_p
594167
 
594167
                 e = getenv("TERM");
594167
                 if (e) {
594167
-                        char *n;
594167
+                        _cleanup_free_ char *n = NULL;
594167
+
594167
+                        n = strjoin("TERM=", e);
594167
+                        if (!n)
594167
+                                return log_oom();
594167
 
594167
-                        n = strjoina("TERM=", e);
594167
                         r = sd_bus_message_append(m,
594167
                                                   "(sv)",
594167
                                                   "Environment", "as", 1, n);
594167
diff --git a/src/shared/pager.c b/src/shared/pager.c
594167
index f75ef62d2d..9426d3ef98 100644
594167
--- a/src/shared/pager.c
594167
+++ b/src/shared/pager.c
594167
@@ -86,6 +86,7 @@ static int no_quit_on_interrupt(int exe_name_fd, const char *less_opts) {
594167
 void pager_open(PagerFlags flags) {
594167
         _cleanup_close_pair_ int fd[2] = { -1, -1 }, exe_name_pipe[2] = { -1, -1 };
594167
         _cleanup_strv_free_ char **pager_args = NULL;
594167
+        _cleanup_free_ char *l = NULL;
594167
         const char *pager, *less_opts;
594167
         int r;
594167
 
594167
@@ -131,8 +132,12 @@ void pager_open(PagerFlags flags) {
594167
         less_opts = getenv("SYSTEMD_LESS");
594167
         if (!less_opts)
594167
                 less_opts = "FRSXMK";
594167
-        if (flags & PAGER_JUMP_TO_END)
594167
-                less_opts = strjoina(less_opts, " +G");
594167
+        if (flags & PAGER_JUMP_TO_END) {
594167
+                l = strjoin(less_opts, " +G");
594167
+                if (!l)
594167
+                        return (void) log_oom();
594167
+                less_opts = l;
594167
+        }
594167
 
594167
         /* We set SIGINT as PR_DEATHSIG signal here, to match the "K" parameter we set in $LESS, which enables SIGINT behaviour. */
594167
         r = safe_fork("(pager)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGINT|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pager_pid);