daandemeyer / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
ff6046
From 55a1c766445750aaefe28bd7bea454f5f1cff9bb Mon Sep 17 00:00:00 2001
ff6046
From: Lennart Poettering <lennart@poettering.net>
ff6046
Date: Wed, 17 Oct 2018 18:36:24 +0200
ff6046
Subject: [PATCH] =?UTF-8?q?core:=20when=20deserializing=20state=20always?=
ff6046
 =?UTF-8?q?=20use=20read=5Fline(=E2=80=A6,=20LONG=5FLINE=5FMAX,=20?=
ff6046
 =?UTF-8?q?=E2=80=A6)?=
ff6046
MIME-Version: 1.0
ff6046
Content-Type: text/plain; charset=UTF-8
ff6046
Content-Transfer-Encoding: 8bit
ff6046
ff6046
This should be much better than fgets(), as we can read substantially
ff6046
longer lines and overly long lines result in proper errors.
ff6046
ff6046
Fixes a vulnerability discovered by Jann Horn at Google.
ff6046
ff6046
CVE-2018-15686
ff6046
LP: #1796402
ff6046
https://bugzilla.redhat.com/show_bug.cgi?id=1639071
ff6046
ff6046
(cherry picked from commit 8948b3415d762245ebf5e19d80b97d4d8cc208c1)
ff6046
ff6046
Resolves: CVE-2018-15686
ff6046
---
ff6046
 src/core/job.c     | 19 +++++++++++--------
ff6046
 src/core/manager.c | 47 ++++++++++++++++++++--------------------------
ff6046
 src/core/unit.c    | 34 +++++++++++++++++----------------
ff6046
 src/core/unit.h    |  2 +-
ff6046
 4 files changed, 50 insertions(+), 52 deletions(-)
ff6046
ff6046
diff --git a/src/core/job.c b/src/core/job.c
ff6046
index 734756b666..8552ffb704 100644
ff6046
--- a/src/core/job.c
ff6046
+++ b/src/core/job.c
ff6046
@@ -10,6 +10,7 @@
ff6046
 #include "dbus-job.h"
ff6046
 #include "dbus.h"
ff6046
 #include "escape.h"
ff6046
+#include "fileio.h"
ff6046
 #include "job.h"
ff6046
 #include "log.h"
ff6046
 #include "macro.h"
ff6046
@@ -1091,24 +1092,26 @@ int job_serialize(Job *j, FILE *f) {
ff6046
 }
ff6046
 
ff6046
 int job_deserialize(Job *j, FILE *f) {
ff6046
+        int r;
ff6046
+
ff6046
         assert(j);
ff6046
         assert(f);
ff6046
 
ff6046
         for (;;) {
ff6046
-                char line[LINE_MAX], *l, *v;
ff6046
+                _cleanup_free_ char *line = NULL;
ff6046
+                char *l, *v;
ff6046
                 size_t k;
ff6046
 
ff6046
-                if (!fgets(line, sizeof(line), f)) {
ff6046
-                        if (feof(f))
ff6046
-                                return 0;
ff6046
-                        return -errno;
ff6046
-                }
ff6046
+                r = read_line(f, LONG_LINE_MAX, &line);
ff6046
+                if (r < 0)
ff6046
+                        return log_error_errno(r, "Failed to read serialization line: %m");
ff6046
+                if (r == 0)
ff6046
+                        return 0;
ff6046
 
ff6046
-                char_array_0(line);
ff6046
                 l = strstrip(line);
ff6046
 
ff6046
                 /* End marker */
ff6046
-                if (l[0] == 0)
ff6046
+                if (isempty(l))
ff6046
                         return 0;
ff6046
 
ff6046
                 k = strcspn(l, "=");
ff6046
diff --git a/src/core/manager.c b/src/core/manager.c
ff6046
index 3b2fe11e87..c83e296cf3 100644
ff6046
--- a/src/core/manager.c
ff6046
+++ b/src/core/manager.c
ff6046
@@ -3144,22 +3144,17 @@ int manager_deserialize(Manager *m, FILE *f, FDSet *fds) {
ff6046
         m->n_reloading++;
ff6046
 
ff6046
         for (;;) {
ff6046
-                char line[LINE_MAX];
ff6046
+                _cleanup_free_ char *line = NULL;
ff6046
                 const char *val, *l;
ff6046
 
ff6046
-                if (!fgets(line, sizeof(line), f)) {
ff6046
-                        if (feof(f))
ff6046
-                                r = 0;
ff6046
-                        else
ff6046
-                                r = -errno;
ff6046
-
ff6046
-                        goto finish;
ff6046
-                }
ff6046
+                r = read_line(f, LONG_LINE_MAX, &line);
ff6046
+                if (r < 0)
ff6046
+                        return log_error_errno(r, "Failed to read serialization line: %m");
ff6046
+                if (r == 0)
ff6046
+                        break;
ff6046
 
ff6046
-                char_array_0(line);
ff6046
                 l = strstrip(line);
ff6046
-
ff6046
-                if (l[0] == 0)
ff6046
+                if (isempty(l)) /* end marker */
ff6046
                         break;
ff6046
 
ff6046
                 if ((val = startswith(l, "current-job-id="))) {
ff6046
@@ -3326,29 +3321,27 @@ int manager_deserialize(Manager *m, FILE *f, FDSet *fds) {
ff6046
         }
ff6046
 
ff6046
         for (;;) {
ff6046
-                Unit *u;
ff6046
-                char name[UNIT_NAME_MAX+2];
ff6046
+                _cleanup_free_ char *line = NULL;
ff6046
                 const char* unit_name;
ff6046
+                Unit *u;
ff6046
 
ff6046
                 /* Start marker */
ff6046
-                if (!fgets(name, sizeof(name), f)) {
ff6046
-                        if (feof(f))
ff6046
-                                r = 0;
ff6046
-                        else
ff6046
-                                r = -errno;
ff6046
-
ff6046
-                        goto finish;
ff6046
-                }
ff6046
+                r = read_line(f, LONG_LINE_MAX, &line);
ff6046
+                if (r < 0)
ff6046
+                        return log_error_errno(r, "Failed to read serialization line: %m");
ff6046
+                if (r == 0)
ff6046
+                        break;
ff6046
 
ff6046
-                char_array_0(name);
ff6046
-                unit_name = strstrip(name);
ff6046
+                unit_name = strstrip(line);
ff6046
 
ff6046
                 r = manager_load_unit(m, unit_name, NULL, NULL, &u);
ff6046
                 if (r < 0) {
ff6046
                         log_notice_errno(r, "Failed to load unit \"%s\", skipping deserialization: %m", unit_name);
ff6046
-                        if (r == -ENOMEM)
ff6046
-                                goto finish;
ff6046
-                        unit_deserialize_skip(f);
ff6046
+
ff6046
+                        r = unit_deserialize_skip(f);
ff6046
+                        if (r < 0)
ff6046
+                                return r;
ff6046
+
ff6046
                         continue;
ff6046
                 }
ff6046
 
ff6046
diff --git a/src/core/unit.c b/src/core/unit.c
ff6046
index 721d8d60a3..cc43ddc4f1 100644
ff6046
--- a/src/core/unit.c
ff6046
+++ b/src/core/unit.c
ff6046
@@ -3368,21 +3368,19 @@ int unit_deserialize(Unit *u, FILE *f, FDSet *fds) {
ff6046
         assert(fds);
ff6046
 
ff6046
         for (;;) {
ff6046
-                char line[LINE_MAX], *l, *v;
ff6046
+                _cleanup_free_ char *line = NULL;
ff6046
                 CGroupIPAccountingMetric m;
ff6046
+                char *l, *v;
ff6046
                 size_t k;
ff6046
 
ff6046
-                if (!fgets(line, sizeof(line), f)) {
ff6046
-                        if (feof(f))
ff6046
-                                return 0;
ff6046
-                        return -errno;
ff6046
-                }
ff6046
+                r = read_line(f, LONG_LINE_MAX, &line);
ff6046
+                if (r < 0)
ff6046
+                        return log_error_errno(r, "Failed to read serialization line: %m");
ff6046
+                if (r == 0) /* eof */
ff6046
+                        break;
ff6046
 
ff6046
-                char_array_0(line);
ff6046
                 l = strstrip(line);
ff6046
-
ff6046
-                /* End marker */
ff6046
-                if (isempty(l))
ff6046
+                if (isempty(l)) /* End marker */
ff6046
                         break;
ff6046
 
ff6046
                 k = strcspn(l, "=");
ff6046
@@ -3657,23 +3655,27 @@ int unit_deserialize(Unit *u, FILE *f, FDSet *fds) {
ff6046
         return 0;
ff6046
 }
ff6046
 
ff6046
-void unit_deserialize_skip(FILE *f) {
ff6046
+int unit_deserialize_skip(FILE *f) {
ff6046
+        int r;
ff6046
         assert(f);
ff6046
 
ff6046
         /* Skip serialized data for this unit. We don't know what it is. */
ff6046
 
ff6046
         for (;;) {
ff6046
-                char line[LINE_MAX], *l;
ff6046
+                _cleanup_free_ char *line = NULL;
ff6046
+                char *l;
ff6046
 
ff6046
-                if (!fgets(line, sizeof line, f))
ff6046
-                        return;
ff6046
+                r = read_line(f, LONG_LINE_MAX, &line);
ff6046
+                if (r < 0)
ff6046
+                        return log_error_errno(r, "Failed to read serialization line: %m");
ff6046
+                if (r == 0)
ff6046
+                        return 0;
ff6046
 
ff6046
-                char_array_0(line);
ff6046
                 l = strstrip(line);
ff6046
 
ff6046
                 /* End marker */
ff6046
                 if (isempty(l))
ff6046
-                        return;
ff6046
+                        return 1;
ff6046
         }
ff6046
 }
ff6046
 
ff6046
diff --git a/src/core/unit.h b/src/core/unit.h
ff6046
index b3131eba1b..e1a60da244 100644
ff6046
--- a/src/core/unit.h
ff6046
+++ b/src/core/unit.h
ff6046
@@ -679,7 +679,7 @@ bool unit_can_serialize(Unit *u) _pure_;
ff6046
 
ff6046
 int unit_serialize(Unit *u, FILE *f, FDSet *fds, bool serialize_jobs);
ff6046
 int unit_deserialize(Unit *u, FILE *f, FDSet *fds);
ff6046
-void unit_deserialize_skip(FILE *f);
ff6046
+int unit_deserialize_skip(FILE *f);
ff6046
 
ff6046
 int unit_serialize_item(Unit *u, FILE *f, const char *key, const char *value);
ff6046
 int unit_serialize_item_escaped(Unit *u, FILE *f, const char *key, const char *value);