daandemeyer / rpms / systemd

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