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