anitazha / rpms / systemd

Forked from rpms/systemd 3 years ago
Clone

Blame SOURCES/0070-journald-do-not-store-the-iovec-entry-for-process-co.patch

ff6046
From b6c10945e68949edc6418f48ca7b1b748fefabe1 Mon Sep 17 00:00:00 2001
ff6046
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
ff6046
Date: Wed, 5 Dec 2018 18:38:39 +0100
ff6046
Subject: [PATCH] journald: do not store the iovec entry for process
ff6046
 commandline on stack
ff6046
ff6046
This fixes a crash where we would read the commandline, whose length is under
ff6046
control of the sending program, and then crash when trying to create a stack
ff6046
allocation for it.
ff6046
ff6046
CVE-2018-16864
ff6046
https://bugzilla.redhat.com/show_bug.cgi?id=1653855
ff6046
ff6046
The message actually doesn't get written to disk, because
ff6046
journal_file_append_entry() returns -E2BIG.
ff6046
ff6046
(cherry-picked from commit 084eeb865ca63887098e0945fb4e93c852b91b0f)
ff6046
ff6046
Resolves: #1664976
ff6046
---
ff6046
 src/basic/io-util.c           | 10 ++++++++++
ff6046
 src/basic/io-util.h           |  2 ++
ff6046
 src/coredump/coredump.c       | 31 +++++++++++--------------------
ff6046
 src/journal/journald-server.c | 25 +++++++++++++++----------
ff6046
 4 files changed, 38 insertions(+), 30 deletions(-)
ff6046
ff6046
diff --git a/src/basic/io-util.c b/src/basic/io-util.c
ff6046
index 1f64cc933b..575398fbe6 100644
ff6046
--- a/src/basic/io-util.c
ff6046
+++ b/src/basic/io-util.c
ff6046
@@ -8,6 +8,7 @@
ff6046
 #include <unistd.h>
ff6046
 
ff6046
 #include "io-util.h"
ff6046
+#include "string-util.h"
ff6046
 #include "time-util.h"
ff6046
 
ff6046
 int flush_fd(int fd) {
ff6046
@@ -252,3 +253,12 @@ ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length) {
ff6046
 
ff6046
         return q - (const uint8_t*) p;
ff6046
 }
ff6046
+
ff6046
+char* set_iovec_string_field(struct iovec *iovec, size_t *n_iovec, const char *field, const char *value) {
ff6046
+        char *x;
ff6046
+
ff6046
+        x = strappend(field, value);
ff6046
+        if (x)
ff6046
+                iovec[(*n_iovec)++] = IOVEC_MAKE_STRING(x);
ff6046
+        return x;
ff6046
+}
ff6046
diff --git a/src/basic/io-util.h b/src/basic/io-util.h
ff6046
index ed189b5820..792a64ad5e 100644
ff6046
--- a/src/basic/io-util.h
ff6046
+++ b/src/basic/io-util.h
ff6046
@@ -71,3 +71,5 @@ static inline bool FILE_SIZE_VALID_OR_INFINITY(uint64_t l) {
ff6046
 #define IOVEC_MAKE(base, len) (struct iovec) IOVEC_INIT(base, len)
ff6046
 #define IOVEC_INIT_STRING(string) IOVEC_INIT((char*) string, strlen(string))
ff6046
 #define IOVEC_MAKE_STRING(string) (struct iovec) IOVEC_INIT_STRING(string)
ff6046
+
ff6046
+char* set_iovec_string_field(struct iovec *iovec, size_t *n_iovec, const char *field, const char *value);
ff6046
diff --git a/src/coredump/coredump.c b/src/coredump/coredump.c
ff6046
index d7dd81c1b4..ffa88f612d 100644
ff6046
--- a/src/coredump/coredump.c
ff6046
+++ b/src/coredump/coredump.c
ff6046
@@ -1054,19 +1054,10 @@ static int send_iovec(const struct iovec iovec[], size_t n_iovec, int input_fd)
ff6046
         return 0;
ff6046
 }
ff6046
 
ff6046
-static char* set_iovec_field(struct iovec *iovec, size_t *n_iovec, const char *field, const char *value) {
ff6046
-        char *x;
ff6046
-
ff6046
-        x = strappend(field, value);
ff6046
-        if (x)
ff6046
-                iovec[(*n_iovec)++] = IOVEC_MAKE_STRING(x);
ff6046
-        return x;
ff6046
-}
ff6046
-
ff6046
 static char* set_iovec_field_free(struct iovec *iovec, size_t *n_iovec, const char *field, char *value) {
ff6046
         char *x;
ff6046
 
ff6046
-        x = set_iovec_field(iovec, n_iovec, field, value);
ff6046
+        x = set_iovec_string_field(iovec, n_iovec, field, value);
ff6046
         free(value);
ff6046
         return x;
ff6046
 }
ff6046
@@ -1116,36 +1107,36 @@ static int gather_pid_metadata(
ff6046
                         disable_coredumps();
ff6046
                 }
ff6046
 
ff6046
-                set_iovec_field(iovec, n_iovec, "COREDUMP_UNIT=", context[CONTEXT_UNIT]);
ff6046
+                set_iovec_string_field(iovec, n_iovec, "COREDUMP_UNIT=", context[CONTEXT_UNIT]);
ff6046
         }
ff6046
 
ff6046
         if (cg_pid_get_user_unit(pid, &t) >= 0)
ff6046
                 set_iovec_field_free(iovec, n_iovec, "COREDUMP_USER_UNIT=", t);
ff6046
 
ff6046
         /* The next few are mandatory */
ff6046
-        if (!set_iovec_field(iovec, n_iovec, "COREDUMP_PID=", context[CONTEXT_PID]))
ff6046
+        if (!set_iovec_string_field(iovec, n_iovec, "COREDUMP_PID=", context[CONTEXT_PID]))
ff6046
                 return log_oom();
ff6046
 
ff6046
-        if (!set_iovec_field(iovec, n_iovec, "COREDUMP_UID=", context[CONTEXT_UID]))
ff6046
+        if (!set_iovec_string_field(iovec, n_iovec, "COREDUMP_UID=", context[CONTEXT_UID]))
ff6046
                 return log_oom();
ff6046
 
ff6046
-        if (!set_iovec_field(iovec, n_iovec, "COREDUMP_GID=", context[CONTEXT_GID]))
ff6046
+        if (!set_iovec_string_field(iovec, n_iovec, "COREDUMP_GID=", context[CONTEXT_GID]))
ff6046
                 return log_oom();
ff6046
 
ff6046
-        if (!set_iovec_field(iovec, n_iovec, "COREDUMP_SIGNAL=", context[CONTEXT_SIGNAL]))
ff6046
+        if (!set_iovec_string_field(iovec, n_iovec, "COREDUMP_SIGNAL=", context[CONTEXT_SIGNAL]))
ff6046
                 return log_oom();
ff6046
 
ff6046
-        if (!set_iovec_field(iovec, n_iovec, "COREDUMP_RLIMIT=", context[CONTEXT_RLIMIT]))
ff6046
+        if (!set_iovec_string_field(iovec, n_iovec, "COREDUMP_RLIMIT=", context[CONTEXT_RLIMIT]))
ff6046
                 return log_oom();
ff6046
 
ff6046
-        if (!set_iovec_field(iovec, n_iovec, "COREDUMP_HOSTNAME=", context[CONTEXT_HOSTNAME]))
ff6046
+        if (!set_iovec_string_field(iovec, n_iovec, "COREDUMP_HOSTNAME=", context[CONTEXT_HOSTNAME]))
ff6046
                 return log_oom();
ff6046
 
ff6046
-        if (!set_iovec_field(iovec, n_iovec, "COREDUMP_COMM=", context[CONTEXT_COMM]))
ff6046
+        if (!set_iovec_string_field(iovec, n_iovec, "COREDUMP_COMM=", context[CONTEXT_COMM]))
ff6046
                 return log_oom();
ff6046
 
ff6046
         if (context[CONTEXT_EXE] &&
ff6046
-            !set_iovec_field(iovec, n_iovec, "COREDUMP_EXE=", context[CONTEXT_EXE]))
ff6046
+            !set_iovec_string_field(iovec, n_iovec, "COREDUMP_EXE=", context[CONTEXT_EXE]))
ff6046
                 return log_oom();
ff6046
 
ff6046
         if (sd_pid_get_session(pid, &t) >= 0)
ff6046
@@ -1213,7 +1204,7 @@ static int gather_pid_metadata(
ff6046
                 iovec[(*n_iovec)++] = IOVEC_MAKE_STRING(t);
ff6046
 
ff6046
         if (safe_atoi(context[CONTEXT_SIGNAL], &signo) >= 0 && SIGNAL_VALID(signo))
ff6046
-                set_iovec_field(iovec, n_iovec, "COREDUMP_SIGNAL_NAME=SIG", signal_to_string(signo));
ff6046
+                set_iovec_string_field(iovec, n_iovec, "COREDUMP_SIGNAL_NAME=SIG", signal_to_string(signo));
ff6046
 
ff6046
         return 0; /* we successfully acquired all metadata */
ff6046
 }
ff6046
diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c
ff6046
index ce2446a2f1..8de45552f6 100644
ff6046
--- a/src/journal/journald-server.c
ff6046
+++ b/src/journal/journald-server.c
ff6046
@@ -753,6 +753,7 @@ static void dispatch_message_real(
ff6046
                 pid_t object_pid) {
ff6046
 
ff6046
         char source_time[sizeof("_SOURCE_REALTIME_TIMESTAMP=") + DECIMAL_STR_MAX(usec_t)];
ff6046
+        _cleanup_free_ char *cmdline1 = NULL, *cmdline2 = NULL;
ff6046
         uid_t journal_uid;
ff6046
         ClientContext *o;
ff6046
 
ff6046
@@ -769,20 +770,23 @@ static void dispatch_message_real(
ff6046
                 IOVEC_ADD_NUMERIC_FIELD(iovec, n, c->uid, uid_t, uid_is_valid, UID_FMT, "_UID");
ff6046
                 IOVEC_ADD_NUMERIC_FIELD(iovec, n, c->gid, gid_t, gid_is_valid, GID_FMT, "_GID");
ff6046
 
ff6046
-                IOVEC_ADD_STRING_FIELD(iovec, n, c->comm, "_COMM");
ff6046
-                IOVEC_ADD_STRING_FIELD(iovec, n, c->exe, "_EXE");
ff6046
-                IOVEC_ADD_STRING_FIELD(iovec, n, c->cmdline, "_CMDLINE");
ff6046
-                IOVEC_ADD_STRING_FIELD(iovec, n, c->capeff, "_CAP_EFFECTIVE");
ff6046
+                IOVEC_ADD_STRING_FIELD(iovec, n, c->comm, "_COMM"); /* At most TASK_COMM_LENGTH (16 bytes) */
ff6046
+                IOVEC_ADD_STRING_FIELD(iovec, n, c->exe, "_EXE"); /* A path, so at most PATH_MAX (4096 bytes) */
ff6046
 
ff6046
-                IOVEC_ADD_SIZED_FIELD(iovec, n, c->label, c->label_size, "_SELINUX_CONTEXT");
ff6046
+                if (c->cmdline)
ff6046
+                        /* At most _SC_ARG_MAX (2MB usually), which is too much to put on stack.
ff6046
+                         * Let's use a heap allocation for this one. */
ff6046
+                        cmdline1 = set_iovec_string_field(iovec, &n, "_CMDLINE=", c->cmdline);
ff6046
 
ff6046
+                IOVEC_ADD_STRING_FIELD(iovec, n, c->capeff, "_CAP_EFFECTIVE"); /* Read from /proc/.../status */
ff6046
+                IOVEC_ADD_SIZED_FIELD(iovec, n, c->label, c->label_size, "_SELINUX_CONTEXT");
ff6046
                 IOVEC_ADD_NUMERIC_FIELD(iovec, n, c->auditid, uint32_t, audit_session_is_valid, "%" PRIu32, "_AUDIT_SESSION");
ff6046
                 IOVEC_ADD_NUMERIC_FIELD(iovec, n, c->loginuid, uid_t, uid_is_valid, UID_FMT, "_AUDIT_LOGINUID");
ff6046
 
ff6046
-                IOVEC_ADD_STRING_FIELD(iovec, n, c->cgroup, "_SYSTEMD_CGROUP");
ff6046
+                IOVEC_ADD_STRING_FIELD(iovec, n, c->cgroup, "_SYSTEMD_CGROUP"); /* A path */
ff6046
                 IOVEC_ADD_STRING_FIELD(iovec, n, c->session, "_SYSTEMD_SESSION");
ff6046
                 IOVEC_ADD_NUMERIC_FIELD(iovec, n, c->owner_uid, uid_t, uid_is_valid, UID_FMT, "_SYSTEMD_OWNER_UID");
ff6046
-                IOVEC_ADD_STRING_FIELD(iovec, n, c->unit, "_SYSTEMD_UNIT");
ff6046
+                IOVEC_ADD_STRING_FIELD(iovec, n, c->unit, "_SYSTEMD_UNIT"); /* Unit names are bounded by UNIT_NAME_MAX */
ff6046
                 IOVEC_ADD_STRING_FIELD(iovec, n, c->user_unit, "_SYSTEMD_USER_UNIT");
ff6046
                 IOVEC_ADD_STRING_FIELD(iovec, n, c->slice, "_SYSTEMD_SLICE");
ff6046
                 IOVEC_ADD_STRING_FIELD(iovec, n, c->user_slice, "_SYSTEMD_USER_SLICE");
ff6046
@@ -803,13 +807,14 @@ static void dispatch_message_real(
ff6046
                 IOVEC_ADD_NUMERIC_FIELD(iovec, n, o->uid, uid_t, uid_is_valid, UID_FMT, "OBJECT_UID");
ff6046
                 IOVEC_ADD_NUMERIC_FIELD(iovec, n, o->gid, gid_t, gid_is_valid, GID_FMT, "OBJECT_GID");
ff6046
 
ff6046
+                /* See above for size limits, only ->cmdline may be large, so use a heap allocation for it. */
ff6046
                 IOVEC_ADD_STRING_FIELD(iovec, n, o->comm, "OBJECT_COMM");
ff6046
                 IOVEC_ADD_STRING_FIELD(iovec, n, o->exe, "OBJECT_EXE");
ff6046
-                IOVEC_ADD_STRING_FIELD(iovec, n, o->cmdline, "OBJECT_CMDLINE");
ff6046
-                IOVEC_ADD_STRING_FIELD(iovec, n, o->capeff, "OBJECT_CAP_EFFECTIVE");
ff6046
+                if (o->cmdline)
ff6046
+                        cmdline2 = set_iovec_string_field(iovec, &n, "OBJECT_CMDLINE=", o->cmdline);
ff6046
 
ff6046
+                IOVEC_ADD_STRING_FIELD(iovec, n, o->capeff, "OBJECT_CAP_EFFECTIVE");
ff6046
                 IOVEC_ADD_SIZED_FIELD(iovec, n, o->label, o->label_size, "OBJECT_SELINUX_CONTEXT");
ff6046
-
ff6046
                 IOVEC_ADD_NUMERIC_FIELD(iovec, n, o->auditid, uint32_t, audit_session_is_valid, "%" PRIu32, "OBJECT_AUDIT_SESSION");
ff6046
                 IOVEC_ADD_NUMERIC_FIELD(iovec, n, o->loginuid, uid_t, uid_is_valid, UID_FMT, "OBJECT_AUDIT_LOGINUID");
ff6046