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