ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
c62b8e
From 78c1a16731f8bec0e854ed570292afc8bf3d8030 Mon Sep 17 00:00:00 2001
23b3cf
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
23b3cf
Date: Thu, 3 Jan 2019 11:35:22 +0100
23b3cf
Subject: [PATCH] journald: do not store the iovec entry for process
23b3cf
 commandline on stack
23b3cf
23b3cf
This fixes a crash where we would read the commandline, whose length is under
23b3cf
control of the sending program, and then crash when trying to create a stack
23b3cf
allocation for it.
23b3cf
23b3cf
CVE-2018-16864
23b3cf
https://bugzilla.redhat.com/show_bug.cgi?id=1653855
23b3cf
23b3cf
The message actually doesn't get written to disk, because
23b3cf
journal_file_append_entry() returns -E2BIG.
23b3cf
23b3cf
Resolves: #1657788
23b3cf
---
23b3cf
 src/journal/coredump.c        | 189 +++++++++++++---------------------
23b3cf
 src/journal/journald-server.c |  13 +--
23b3cf
 src/shared/util.c             |  17 +++
23b3cf
 src/shared/util.h             |   7 ++
23b3cf
 4 files changed, 98 insertions(+), 128 deletions(-)
23b3cf
23b3cf
diff --git a/src/journal/coredump.c b/src/journal/coredump.c
c62b8e
index 59ccd46bb0..40de86f050 100644
23b3cf
--- a/src/journal/coredump.c
23b3cf
+++ b/src/journal/coredump.c
23b3cf
@@ -526,14 +526,6 @@ static int compose_open_fds(pid_t pid, char **open_fds) {
23b3cf
 }
23b3cf
 
23b3cf
 int main(int argc, char* argv[]) {
23b3cf
-
23b3cf
-        /* The small core field we allocate on the stack, to keep things simple */
23b3cf
-        char
23b3cf
-                *core_pid = NULL, *core_uid = NULL, *core_gid = NULL, *core_signal = NULL,
23b3cf
-                *core_session = NULL, *core_exe = NULL, *core_comm = NULL, *core_cmdline = NULL,
23b3cf
-                *core_cgroup = NULL, *core_cwd = NULL, *core_root = NULL, *core_unit = NULL,
23b3cf
-                *core_slice = NULL;
23b3cf
-
23b3cf
         /* The larger ones we allocate on the heap */
23b3cf
         _cleanup_free_ char
23b3cf
                 *core_timestamp = NULL,  *core_message = NULL, *coredump_data = NULL, *core_owner_uid = NULL,
23b3cf
@@ -547,7 +539,8 @@ int main(int argc, char* argv[]) {
23b3cf
 
23b3cf
         struct iovec iovec[26];
23b3cf
         off_t coredump_size;
23b3cf
-        int r, j = 0;
23b3cf
+        int r;
23b3cf
+        unsigned int n_iovec = 0;
23b3cf
         uid_t uid, owner_uid;
23b3cf
         gid_t gid;
23b3cf
         pid_t pid;
23b3cf
@@ -634,151 +627,107 @@ int main(int argc, char* argv[]) {
23b3cf
                         goto finish;
23b3cf
                 }
23b3cf
 
23b3cf
-                core_unit = strjoina("COREDUMP_UNIT=", t);
23b3cf
-                free(t);
23b3cf
-
23b3cf
-        } else if (cg_pid_get_user_unit(pid, &t) >= 0) {
23b3cf
-                core_unit = strjoina("COREDUMP_USER_UNIT=", t);
23b3cf
-                free(t);
23b3cf
+                if (!set_iovec_field_free(iovec, &n_iovec, "COREDUMP_UNIT=", t)) {
23b3cf
+                        r = log_oom();
23b3cf
+                        goto finish;
23b3cf
+                }
23b3cf
         }
23b3cf
 
23b3cf
-        if (core_unit)
23b3cf
-                IOVEC_SET_STRING(iovec[j++], core_unit);
23b3cf
+        if (cg_pid_get_user_unit(pid, &t) >= 0) {
23b3cf
+                if (!set_iovec_field_free(iovec, &n_iovec, "COREDUMP_USER_UNIT=", t)) {
23b3cf
+                        r = log_oom();
23b3cf
+                        goto finish;
23b3cf
+                }
23b3cf
+        }
23b3cf
 
23b3cf
         /* OK, now we know it's not the journal, hence we can make use
23b3cf
          * of it now. */
23b3cf
         log_set_target(LOG_TARGET_JOURNAL_OR_KMSG);
23b3cf
         log_open();
23b3cf
 
23b3cf
-        core_pid = strjoina("COREDUMP_PID=", info[INFO_PID]);
23b3cf
-        IOVEC_SET_STRING(iovec[j++], core_pid);
23b3cf
-
23b3cf
-        core_uid = strjoina("COREDUMP_UID=", info[INFO_UID]);
23b3cf
-        IOVEC_SET_STRING(iovec[j++], core_uid);
23b3cf
-
23b3cf
-        core_gid = strjoina("COREDUMP_GID=", info[INFO_GID]);
23b3cf
-        IOVEC_SET_STRING(iovec[j++], core_gid);
23b3cf
-
23b3cf
-        core_signal = strjoina("COREDUMP_SIGNAL=", info[INFO_SIGNAL]);
23b3cf
-        IOVEC_SET_STRING(iovec[j++], core_signal);
23b3cf
-
23b3cf
-        if (sd_pid_get_session(pid, &t) >= 0) {
23b3cf
-                core_session = strjoina("COREDUMP_SESSION=", t);
23b3cf
-                free(t);
23b3cf
-
23b3cf
-                IOVEC_SET_STRING(iovec[j++], core_session);
23b3cf
+        if (!set_iovec_string_field(iovec, &n_iovec, "COREDUMP_PID=", info[INFO_PID])) {
23b3cf
+                r = log_oom();
23b3cf
+                goto finish;
23b3cf
         }
23b3cf
 
23b3cf
-        if (sd_pid_get_owner_uid(pid, &owner_uid) >= 0) {
23b3cf
-                r = asprintf(&core_owner_uid,
23b3cf
-                             "COREDUMP_OWNER_UID=" UID_FMT, owner_uid);
23b3cf
-                if (r > 0)
23b3cf
-                        IOVEC_SET_STRING(iovec[j++], core_owner_uid);
23b3cf
+        if (!set_iovec_string_field(iovec, &n_iovec, "COREDUMP_UID=", info[INFO_UID])) {
23b3cf
+                r = log_oom();
23b3cf
+                goto finish;
23b3cf
         }
23b3cf
 
23b3cf
-        if (sd_pid_get_slice(pid, &t) >= 0) {
23b3cf
-                core_slice = strjoina("COREDUMP_SLICE=", t);
23b3cf
-                free(t);
23b3cf
+        if (!set_iovec_string_field(iovec, &n_iovec, "COREDUMP_GID=", info[INFO_GID])) {
23b3cf
+                r = log_oom();
23b3cf
+                goto finish;
23b3cf
+        }
23b3cf
 
23b3cf
-                IOVEC_SET_STRING(iovec[j++], core_slice);
23b3cf
+        if (!set_iovec_string_field(iovec, &n_iovec, "COREDUMP_SIGNAL=", info[INFO_SIGNAL])) {
23b3cf
+                r = log_oom();
23b3cf
+                goto finish;
23b3cf
         }
23b3cf
 
23b3cf
-        if (comm) {
23b3cf
-                core_comm = strjoina("COREDUMP_COMM=", comm);
23b3cf
-                IOVEC_SET_STRING(iovec[j++], core_comm);
23b3cf
+        if (comm && !set_iovec_string_field(iovec, &n_iovec, "COREDUMP_COMM=", comm)) {
23b3cf
+                r = log_oom();
23b3cf
+                goto finish;
23b3cf
         }
23b3cf
 
23b3cf
-        if (exe) {
23b3cf
-                core_exe = strjoina("COREDUMP_EXE=", exe);
23b3cf
-                IOVEC_SET_STRING(iovec[j++], core_exe);
23b3cf
+        if (exe && !set_iovec_string_field(iovec, &n_iovec, "COREDUMP_EXE=", exe)) {
23b3cf
+                r = log_oom();
23b3cf
+                goto finish;
23b3cf
         }
23b3cf
 
23b3cf
-        if (get_process_cmdline(pid, 0, false, &t) >= 0) {
23b3cf
-                core_cmdline = strjoina("COREDUMP_CMDLINE=", t);
23b3cf
-                free(t);
23b3cf
+        if (sd_pid_get_session(pid, &t) >= 0)
23b3cf
+                set_iovec_field_free(iovec, &n_iovec, "COREDUMP_SESSION=", t);
23b3cf
 
23b3cf
-                IOVEC_SET_STRING(iovec[j++], core_cmdline);
23b3cf
+        if (sd_pid_get_owner_uid(pid, &owner_uid) >= 0) {
23b3cf
+                r = asprintf(&core_owner_uid,
23b3cf
+                             "COREDUMP_OWNER_UID=" UID_FMT, owner_uid);
23b3cf
+                if (r > 0)
23b3cf
+                        IOVEC_SET_STRING(iovec[n_iovec++], core_owner_uid);
23b3cf
         }
23b3cf
 
23b3cf
-        if (cg_pid_get_path_shifted(pid, NULL, &t) >= 0) {
23b3cf
-                core_cgroup = strjoina("COREDUMP_CGROUP=", t);
23b3cf
-                free(t);
23b3cf
+        if (sd_pid_get_slice(pid, &t) >= 0)
23b3cf
+                set_iovec_field_free(iovec, &n_iovec, "COREDUMP_SLICE=", t);
23b3cf
 
23b3cf
-                IOVEC_SET_STRING(iovec[j++], core_cgroup);
23b3cf
-        }
23b3cf
+        if (get_process_cmdline(pid, 0, false, &t) >= 0)
23b3cf
+                set_iovec_field_free(iovec, &n_iovec, "COREDUMP_CMDLINE=", t);
23b3cf
 
23b3cf
-        if (compose_open_fds(pid, &t) >= 0) {
23b3cf
-                core_open_fds = strappend("COREDUMP_OPEN_FDS=", t);
23b3cf
-                free(t);
23b3cf
+        if (cg_pid_get_path_shifted(pid, NULL, &t) >= 0)
23b3cf
+                set_iovec_field_free(iovec, &n_iovec, "COREDUMP_CGROUP=", t);
23b3cf
 
23b3cf
-                if (core_open_fds)
23b3cf
-                        IOVEC_SET_STRING(iovec[j++], core_open_fds);
23b3cf
-        }
23b3cf
+        if (compose_open_fds(pid, &t) >= 0)
23b3cf
+                set_iovec_field_free(iovec, &n_iovec, "COREDUMP_OPEN_FDS=", t);
23b3cf
 
23b3cf
         p = procfs_file_alloca(pid, "status");
23b3cf
-        if (read_full_file(p, &t, NULL) >= 0) {
23b3cf
-                core_proc_status = strappend("COREDUMP_PROC_STATUS=", t);
23b3cf
-                free(t);
23b3cf
-
23b3cf
-                if (core_proc_status)
23b3cf
-                        IOVEC_SET_STRING(iovec[j++], core_proc_status);
23b3cf
-        }
23b3cf
+        if (read_full_file(p, &t, NULL) >= 0)
23b3cf
+                set_iovec_field_free(iovec, &n_iovec, "COREDUMP_PROC_STATUS=", t);
23b3cf
 
23b3cf
         p = procfs_file_alloca(pid, "maps");
23b3cf
-        if (read_full_file(p, &t, NULL) >= 0) {
23b3cf
-                core_proc_maps = strappend("COREDUMP_PROC_MAPS=", t);
23b3cf
-                free(t);
23b3cf
-
23b3cf
-                if (core_proc_maps)
23b3cf
-                        IOVEC_SET_STRING(iovec[j++], core_proc_maps);
23b3cf
-        }
23b3cf
+        if (read_full_file(p, &t, NULL) >= 0)
23b3cf
+                set_iovec_field_free(iovec, &n_iovec, "COREDUMP_PROC_MAPS=", t);
23b3cf
 
23b3cf
         p = procfs_file_alloca(pid, "limits");
23b3cf
-        if (read_full_file(p, &t, NULL) >= 0) {
23b3cf
-                core_proc_limits = strappend("COREDUMP_PROC_LIMITS=", t);
23b3cf
-                free(t);
23b3cf
-
23b3cf
-                if (core_proc_limits)
23b3cf
-                        IOVEC_SET_STRING(iovec[j++], core_proc_limits);
23b3cf
-        }
23b3cf
+        if (read_full_file(p, &t, NULL) >= 0)
23b3cf
+                set_iovec_field_free(iovec, &n_iovec, "COREDUMP_PROC_LIMITS=", t);
23b3cf
 
23b3cf
         p = procfs_file_alloca(pid, "cgroup");
23b3cf
-        if (read_full_file(p, &t, NULL) >=0) {
23b3cf
-                core_proc_cgroup = strappend("COREDUMP_PROC_CGROUP=", t);
23b3cf
-                free(t);
23b3cf
+        if (read_full_file(p, &t, NULL) >=0)
23b3cf
+                set_iovec_field_free(iovec, &n_iovec, "COREDUMP_PROC_CGROUP=", t);
23b3cf
 
23b3cf
-                if (core_proc_cgroup)
23b3cf
-                        IOVEC_SET_STRING(iovec[j++], core_proc_cgroup);
23b3cf
-        }
23b3cf
-
23b3cf
-        if (get_process_cwd(pid, &t) >= 0) {
23b3cf
-                core_cwd = strjoina("COREDUMP_CWD=", t);
23b3cf
-                free(t);
23b3cf
+        if (get_process_cwd(pid, &t) >= 0)
23b3cf
+                set_iovec_field_free(iovec, &n_iovec, "COREDUMP_CWD=", t);
23b3cf
 
23b3cf
-                IOVEC_SET_STRING(iovec[j++], core_cwd);
23b3cf
-        }
23b3cf
+        if (get_process_root(pid, &t) >= 0)
23b3cf
+                set_iovec_field_free(iovec, &n_iovec, "COREDUMP_ROOT=", t);
23b3cf
 
23b3cf
-        if (get_process_root(pid, &t) >= 0) {
23b3cf
-                core_root = strjoina("COREDUMP_ROOT=", t);
23b3cf
-                free(t);
23b3cf
-
23b3cf
-                IOVEC_SET_STRING(iovec[j++], core_root);
23b3cf
-        }
23b3cf
-
23b3cf
-        if (get_process_environ(pid, &t) >= 0) {
23b3cf
-                core_environ = strappend("COREDUMP_ENVIRON=", t);
23b3cf
-                free(t);
23b3cf
-
23b3cf
-                if (core_environ)
23b3cf
-                        IOVEC_SET_STRING(iovec[j++], core_environ);
23b3cf
-        }
23b3cf
+        if (get_process_environ(pid, &t) >= 0)
23b3cf
+                set_iovec_field_free(iovec, &n_iovec, "COREDUMP_ENVIRON=", t);
23b3cf
 
23b3cf
         core_timestamp = strjoin("COREDUMP_TIMESTAMP=", info[INFO_TIMESTAMP], "000000", NULL);
23b3cf
         if (core_timestamp)
23b3cf
-                IOVEC_SET_STRING(iovec[j++], core_timestamp);
23b3cf
+                IOVEC_SET_STRING(iovec[n_iovec++], core_timestamp);
23b3cf
 
23b3cf
-        IOVEC_SET_STRING(iovec[j++], "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1");
23b3cf
-        IOVEC_SET_STRING(iovec[j++], "PRIORITY=2");
23b3cf
+        IOVEC_SET_STRING(iovec[n_iovec++], "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1");
23b3cf
+        IOVEC_SET_STRING(iovec[n_iovec++], "PRIORITY=2");
23b3cf
 
23b3cf
         /* Vacuum before we write anything again */
23b3cf
         coredump_vacuum(-1, arg_keep_free, arg_max_use);
23b3cf
@@ -800,7 +749,7 @@ int main(int argc, char* argv[]) {
23b3cf
                 const char *coredump_filename;
23b3cf
 
23b3cf
                 coredump_filename = strjoina("COREDUMP_FILENAME=", filename);
23b3cf
-                IOVEC_SET_STRING(iovec[j++], coredump_filename);
23b3cf
+                IOVEC_SET_STRING(iovec[n_iovec++], coredump_filename);
23b3cf
         }
23b3cf
 
23b3cf
         /* Vacuum again, but exclude the coredump we just created */
23b3cf
@@ -838,7 +787,7 @@ int main(int argc, char* argv[]) {
23b3cf
 log:
23b3cf
         core_message = strjoin("MESSAGE=Process ", info[INFO_PID], " (", comm, ") of user ", info[INFO_UID], " dumped core.", NULL);
23b3cf
         if (core_message)
23b3cf
-                IOVEC_SET_STRING(iovec[j++], core_message);
23b3cf
+                IOVEC_SET_STRING(iovec[n_iovec++], core_message);
23b3cf
 
23b3cf
         /* Optionally store the entire coredump in the journal */
23b3cf
         if (IN_SET(arg_storage, COREDUMP_STORAGE_JOURNAL, COREDUMP_STORAGE_BOTH) &&
23b3cf
@@ -849,13 +798,13 @@ log:
23b3cf
 
23b3cf
                 r = allocate_journal_field(coredump_fd, (size_t) coredump_size, &coredump_data, &sz);
23b3cf
                 if (r >= 0) {
23b3cf
-                        iovec[j].iov_base = coredump_data;
23b3cf
-                        iovec[j].iov_len = sz;
23b3cf
-                        j++;
23b3cf
+                        iovec[n_iovec].iov_base = coredump_data;
23b3cf
+                        iovec[n_iovec].iov_len = sz;
23b3cf
+                        n_iovec++;
23b3cf
                 }
23b3cf
         }
23b3cf
 
23b3cf
-        r = sd_journal_sendv(iovec, j);
23b3cf
+        r = sd_journal_sendv(iovec, n_iovec);
23b3cf
         if (r < 0)
23b3cf
                 log_error_errno(r, "Failed to log coredump: %m");
23b3cf
 
23b3cf
diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c
c62b8e
index 7e67e055e3..c35858247b 100644
23b3cf
--- a/src/journal/journald-server.c
23b3cf
+++ b/src/journal/journald-server.c
23b3cf
@@ -788,9 +788,9 @@ static void dispatch_message_real(
23b3cf
 
23b3cf
                 r = get_process_cmdline(ucred->pid, 0, false, &t);
23b3cf
                 if (r >= 0) {
23b3cf
-                        x = strjoina("_CMDLINE=", t);
23b3cf
-                        free(t);
23b3cf
-                        IOVEC_SET_STRING(iovec[n++], x);
23b3cf
+                        /* At most _SC_ARG_MAX (2MB usually), which is too much to put on stack.
23b3cf
+                         * Let's use a heap allocation for this one. */
23b3cf
+                        set_iovec_field_free(iovec, &n, "_CMDLINE=", t);
23b3cf
                 }
23b3cf
 
23b3cf
                 r = get_process_capeff(ucred->pid, &t);
23b3cf
@@ -915,11 +915,8 @@ static void dispatch_message_real(
23b3cf
                 }
23b3cf
 
23b3cf
                 r = get_process_cmdline(object_pid, 0, false, &t);
23b3cf
-                if (r >= 0) {
23b3cf
-                        x = strjoina("OBJECT_CMDLINE=", t);
23b3cf
-                        free(t);
23b3cf
-                        IOVEC_SET_STRING(iovec[n++], x);
23b3cf
-                }
23b3cf
+                if (r >= 0)
23b3cf
+                        set_iovec_field_free(iovec, &n, "OBJECT_CMDLINE=", t);
23b3cf
 
23b3cf
 #ifdef HAVE_AUDIT
23b3cf
                 r = audit_session_from_pid(object_pid, &audit);
23b3cf
diff --git a/src/shared/util.c b/src/shared/util.c
c62b8e
index 78967103a6..c71e021cd7 100644
23b3cf
--- a/src/shared/util.c
23b3cf
+++ b/src/shared/util.c
23b3cf
@@ -2275,6 +2275,23 @@ int flush_fd(int fd) {
23b3cf
         }
23b3cf
 }
23b3cf
 
23b3cf
+char* set_iovec_string_field(struct iovec *iovec, unsigned int *n_iovec, const char *field, const char *value) {
23b3cf
+        char *x;
23b3cf
+
23b3cf
+        x = strappend(field, value);
23b3cf
+        if (x)
23b3cf
+                iovec[(*n_iovec)++] = IOVEC_MAKE_STRING(x);
23b3cf
+        return x;
23b3cf
+}
23b3cf
+
23b3cf
+char* set_iovec_field_free(struct iovec *iovec, unsigned int *n_iovec, const char *field, char *value) {
23b3cf
+        char *x;
23b3cf
+
23b3cf
+        x = set_iovec_string_field(iovec, n_iovec, field, value);
23b3cf
+        free(value);
23b3cf
+        return x;
23b3cf
+}
23b3cf
+
23b3cf
 int acquire_terminal(
23b3cf
                 const char *name,
23b3cf
                 bool fail,
23b3cf
diff --git a/src/shared/util.h b/src/shared/util.h
c62b8e
index cf096aa07b..8fc237495a 100644
23b3cf
--- a/src/shared/util.h
23b3cf
+++ b/src/shared/util.h
23b3cf
@@ -1140,3 +1140,10 @@ static inline void block_signals_reset(sigset_t *ss) {
23b3cf
                 _t;                                                                \
23b3cf
         })
23b3cf
 
23b3cf
+#define IOVEC_INIT(base, len) { .iov_base = (base), .iov_len = (len) }
23b3cf
+#define IOVEC_MAKE(base, len) (struct iovec) IOVEC_INIT(base, len)
23b3cf
+#define IOVEC_INIT_STRING(string) IOVEC_INIT((char*) string, strlen(string))
23b3cf
+#define IOVEC_MAKE_STRING(string) (struct iovec) IOVEC_INIT_STRING(string)
23b3cf
+
23b3cf
+char* set_iovec_string_field(struct iovec *iovec, unsigned int *n_iovec, const char *field, const char *value);
23b3cf
+char* set_iovec_field_free(struct iovec *iovec, unsigned int *n_iovec, const char *field, char *value);