]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/shared/exec-util.c
tree-wide: use ASSERT_PTR more
[thirdparty/systemd.git] / src / shared / exec-util.c
index 61ee3b18d5401bcbfb5c4b6257b009711c57791a..19ddf0498625d72bd973a68ee4af3475b86c8bfe 100644 (file)
 #include "conf-files.h"
 #include "env-file.h"
 #include "env-util.h"
+#include "errno-util.h"
 #include "exec-util.h"
 #include "fd-util.h"
 #include "fileio.h"
 #include "hashmap.h"
 #include "macro.h"
+#include "missing_syscall.h"
 #include "process-util.h"
 #include "rlimit-util.h"
 #include "serialize.h"
@@ -32,8 +34,7 @@
 /* Put this test here for a lack of better place */
 assert_cc(EAGAIN == EWOULDBLOCK);
 
-static int do_spawn(const char *path, char *argv[], int stdout_fd, pid_t *pid) {
-
+static int do_spawn(const char *path, char *argv[], int stdout_fd, pid_t *pid, bool set_systemd_exec_pid) {
         pid_t _pid;
         int r;
 
@@ -49,13 +50,19 @@ static int do_spawn(const char *path, char *argv[], int stdout_fd, pid_t *pid) {
                 char *_argv[2];
 
                 if (stdout_fd >= 0) {
-                        r = rearrange_stdio(STDIN_FILENO, stdout_fd, STDERR_FILENO);
+                        r = rearrange_stdio(STDIN_FILENO, TAKE_FD(stdout_fd), STDERR_FILENO);
                         if (r < 0)
                                 _exit(EXIT_FAILURE);
                 }
 
                 (void) rlimit_nofile_safe();
 
+                if (set_systemd_exec_pid) {
+                        r = setenv_systemd_exec_pid(false);
+                        if (r < 0)
+                                log_warning_errno(r, "Failed to set $SYSTEMD_EXEC_PID, ignoring: %m");
+                }
+
                 if (!argv) {
                         _argv[0] = (char*) path;
                         _argv[1] = NULL;
@@ -84,7 +91,6 @@ static int do_execute(
 
         _cleanup_hashmap_free_free_ Hashmap *pids = NULL;
         _cleanup_strv_free_ char **paths = NULL;
-        char **path, **e;
         int r;
         bool parallel_execution;
 
@@ -131,7 +137,7 @@ static int do_execute(
                                 return log_error_errno(fd, "Failed to open serialization file: %m");
                 }
 
-                r = do_spawn(t, argv, fd, &pid);
+                r = do_spawn(t, argv, fd, &pid, FLAGS_SET(flags, EXEC_DIR_SET_SYSTEMD_EXEC_PID));
                 if (r <= 0)
                         continue;
 
@@ -247,7 +253,7 @@ int execute_directories(
 }
 
 static int gather_environment_generate(int fd, void *arg) {
-        char ***env = arg, **x, **y;
+        char ***env = ASSERT_PTR(arg);
         _cleanup_fclose_ FILE *f = NULL;
         _cleanup_strv_free_ char **new = NULL;
         int r;
@@ -258,8 +264,6 @@ static int gather_environment_generate(int fd, void *arg) {
          * fd is always consumed, even on error.
          */
 
-        assert(env);
-
         f = fdopen(fd, "r");
         if (!f) {
                 safe_close(fd);
@@ -271,18 +275,12 @@ static int gather_environment_generate(int fd, void *arg) {
                 return r;
 
         STRV_FOREACH_PAIR(x, y, new) {
-                char *p;
-
                 if (!env_name_is_valid(*x)) {
                         log_warning("Invalid variable assignment \"%s=...\", ignoring.", *x);
                         continue;
                 }
 
-                p = strjoin(*x, "=", *y);
-                if (!p)
-                        return -ENOMEM;
-
-                r = strv_env_replace(env, p);
+                r = strv_env_assign(env, *x, *y);
                 if (r < 0)
                         return r;
 
@@ -290,18 +288,16 @@ static int gather_environment_generate(int fd, void *arg) {
                         return -errno;
         }
 
-        return r;
+        return 0;
 }
 
 static int gather_environment_collect(int fd, void *arg) {
         _cleanup_fclose_ FILE *f = NULL;
-        char ***env = arg;
+        char ***env = ASSERT_PTR(arg);
         int r;
 
         /* Write out a series of env=cescape(VAR=value) assignments to fd. */
 
-        assert(env);
-
         f = fdopen(fd, "w");
         if (!f) {
                 safe_close(fd);
@@ -321,13 +317,11 @@ static int gather_environment_collect(int fd, void *arg) {
 
 static int gather_environment_consume(int fd, void *arg) {
         _cleanup_fclose_ FILE *f = NULL;
-        char ***env = arg;
+        char ***env = ASSERT_PTR(arg);
         int r = 0;
 
         /* Read a series of env=cescape(VAR=value) assignments from fd into env. */
 
-        assert(env);
-
         f = fdopen(fd, "r");
         if (!f) {
                 safe_close(fd);
@@ -368,16 +362,14 @@ static int gather_environment_consume(int fd, void *arg) {
 
 int exec_command_flags_from_strv(char **ex_opts, ExecCommandFlags *flags) {
         ExecCommandFlags ex_flag, ret_flags = 0;
-        char **opt;
 
         assert(flags);
 
         STRV_FOREACH(opt, ex_opts) {
                 ex_flag = exec_command_flags_from_string(*opt);
-                if (ex_flag >= 0)
-                        ret_flags |= ex_flag;
-                else
-                        return -EINVAL;
+                if (ex_flag < 0)
+                        return ex_flag;
+                ret_flags |= ex_flag;
         }
 
         *flags = ret_flags;
@@ -393,6 +385,9 @@ int exec_command_flags_to_strv(ExecCommandFlags flags, char ***ex_opts) {
 
         assert(ex_opts);
 
+        if (flags < 0)
+                return flags;
+
         for (i = 0; it != 0; it &= ~(1 << i), i++) {
                 if (FLAGS_SET(flags, (1 << i))) {
                         str = exec_command_flags_to_string(1 << i);
@@ -444,3 +439,114 @@ ExecCommandFlags exec_command_flags_from_string(const char *s) {
         else
                 return 1 << idx;
 }
+
+int fexecve_or_execve(int executable_fd, const char *executable, char *const argv[], char *const envp[]) {
+        /* Refuse invalid fds, regardless if fexecve() use is enabled or not */
+        if (executable_fd < 0)
+                return -EBADF;
+
+        /* Block any attempts on exploiting Linux' liberal argv[] handling, i.e. CVE-2021-4034 and suchlike */
+        if (isempty(executable) || strv_isempty(argv))
+                return -EINVAL;
+
+#if ENABLE_FEXECVE
+
+        execveat(executable_fd, "", argv, envp, AT_EMPTY_PATH);
+
+        if (IN_SET(errno, ENOSYS, ENOENT) || ERRNO_IS_PRIVILEGE(errno))
+                /* Old kernel or a script or an overzealous seccomp filter? Let's fall back to execve().
+                 *
+                 * fexecve(3): "If fd refers to a script (i.e., it is an executable text file that names a
+                 * script interpreter with a first line that begins with the characters #!) and the
+                 * close-on-exec flag has been set for fd, then fexecve() fails with the error ENOENT. This
+                 * error occurs because, by the time the script interpreter is executed, fd has already been
+                 * closed because of the close-on-exec flag. Thus, the close-on-exec flag can't be set on fd
+                 * if it refers to a script."
+                 *
+                 * Unfortunately, if we unset close-on-exec, the script will be executed just fine, but (at
+                 * least in case of bash) the script name, $0, will be shown as /dev/fd/nnn, which breaks
+                 * scripts which make use of $0. Thus, let's fall back to execve() in this case.
+                 */
+#endif
+                execve(executable, argv, envp);
+        return -errno;
+}
+
+int fork_agent(const char *name, const int except[], size_t n_except, pid_t *ret_pid, const char *path, ...) {
+        bool stdout_is_tty, stderr_is_tty;
+        size_t n, i;
+        va_list ap;
+        char **l;
+        int r;
+
+        assert(path);
+
+        /* Spawns a temporary TTY agent, making sure it goes away when we go away */
+
+        r = safe_fork_full(name,
+                           except,
+                           n_except,
+                           FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS|FORK_REOPEN_LOG,
+                           ret_pid);
+        if (r < 0)
+                return r;
+        if (r > 0)
+                return 0;
+
+        /* In the child: */
+
+        stdout_is_tty = isatty(STDOUT_FILENO);
+        stderr_is_tty = isatty(STDERR_FILENO);
+
+        if (!stdout_is_tty || !stderr_is_tty) {
+                int fd;
+
+                /* Detach from stdout/stderr and reopen /dev/tty for them. This is important to ensure that
+                 * when systemctl is started via popen() or a similar call that expects to read EOF we
+                 * actually do generate EOF and not delay this indefinitely by keeping an unused copy of
+                 * stdin around. */
+                fd = open("/dev/tty", O_WRONLY);
+                if (fd < 0) {
+                        if (errno != ENXIO) {
+                                log_error_errno(errno, "Failed to open /dev/tty: %m");
+                                _exit(EXIT_FAILURE);
+                        }
+
+                        /* If we get ENXIO here we have no controlling TTY even though stdout/stderr are
+                         * connected to a TTY. That's a weird setup, but let's handle it gracefully: let's
+                         * skip the forking of the agents, given the TTY setup is not in order. */
+                } else {
+                        if (!stdout_is_tty && dup2(fd, STDOUT_FILENO) < 0) {
+                                log_error_errno(errno, "Failed to dup2 /dev/tty: %m");
+                                _exit(EXIT_FAILURE);
+                        }
+
+                        if (!stderr_is_tty && dup2(fd, STDERR_FILENO) < 0) {
+                                log_error_errno(errno, "Failed to dup2 /dev/tty: %m");
+                                _exit(EXIT_FAILURE);
+                        }
+
+                        fd = safe_close_above_stdio(fd);
+                }
+        }
+
+        (void) rlimit_nofile_safe();
+
+        /* Count arguments */
+        va_start(ap, path);
+        for (n = 0; va_arg(ap, char*); n++)
+                ;
+        va_end(ap);
+
+        /* Allocate strv */
+        l = newa(char*, n + 1);
+
+        /* Fill in arguments */
+        va_start(ap, path);
+        for (i = 0; i <= n; i++)
+                l[i] = va_arg(ap, char*);
+        va_end(ap);
+
+        execv(path, l);
+        _exit(EXIT_FAILURE);
+}