923a60
From abb072100e72c07c5cf98b02f5b1a15a48f4b496 Mon Sep 17 00:00:00 2001
923a60
From: Yu Watanabe <watanabe.yu+github@gmail.com>
923a60
Date: Fri, 4 Jan 2019 15:33:43 +0100
923a60
Subject: [PATCH] journal: fix syslog_parse_identifier()
923a60
923a60
Fixes #9829.
923a60
923a60
(cherry-picked from commit a6aadf4ae0bae185dc4c414d492a4a781c80ffe5)
923a60
923a60
Resolves: #1657794
923a60
923a60
[msekleta: Fix for CVE-2018-16866]
923a60
---
923a60
 src/journal/journald-syslog.c     | 6 +++---
923a60
 src/journal/test-journal-syslog.c | 8 +++++++-
923a60
 2 files changed, 10 insertions(+), 4 deletions(-)
923a60
923a60
diff --git a/src/journal/journald-syslog.c b/src/journal/journald-syslog.c
923a60
index 01d2bf69f4..3ea5d79c3a 100644
923a60
--- a/src/journal/journald-syslog.c
923a60
+++ b/src/journal/journald-syslog.c
923a60
@@ -209,7 +209,7 @@ size_t syslog_parse_identifier(const char **buf, char **identifier, char **pid)
923a60
         e = l;
923a60
         l--;
923a60
 
923a60
-        if (p[l-1] == ']') {
923a60
+        if (l > 0 && p[l-1] == ']') {
923a60
                 size_t k = l-1;
923a60
 
923a60
                 for (;;) {
923a60
@@ -234,8 +234,8 @@ size_t syslog_parse_identifier(const char **buf, char **identifier, char **pid)
923a60
         if (t)
923a60
                 *identifier = t;
923a60
 
923a60
-        if (strchr(WHITESPACE, p[e]))
923a60
-                e++;
923a60
+        e += strspn(p + e, WHITESPACE);
923a60
+
923a60
         *buf = p + e;
923a60
         return e;
923a60
 }
923a60
diff --git a/src/journal/test-journal-syslog.c b/src/journal/test-journal-syslog.c
923a60
index c99ca0654b..f56a813774 100644
923a60
--- a/src/journal/test-journal-syslog.c
923a60
+++ b/src/journal/test-journal-syslog.c
923a60
@@ -22,7 +22,7 @@
923a60
 #include "journald-syslog.h"
923a60
 #include "macro.h"
923a60
 
923a60
-static void test_syslog_parse_identifier(const char* str,
923a60
+static void test_syslog_parse_identifier(const char *str,
923a60
                                          const char *ident, const char*pid, int ret) {
923a60
         const char *buf = str;
923a60
         _cleanup_free_ char *ident2 = NULL, *pid2 = NULL;
923a60
@@ -38,7 +38,13 @@ static void test_syslog_parse_identifier(const char* str,
923a60
 int main(void) {
923a60
         test_syslog_parse_identifier("pidu[111]: xxx", "pidu", "111", 11);
923a60
         test_syslog_parse_identifier("pidu: xxx", "pidu", NULL, 6);
923a60
+        test_syslog_parse_identifier("pidu:  xxx", "pidu", NULL, 7);
923a60
         test_syslog_parse_identifier("pidu xxx", NULL, NULL, 0);
923a60
+        test_syslog_parse_identifier(":", "", NULL, 1);
923a60
+        test_syslog_parse_identifier(":  ", "", NULL, 3);
923a60
+        test_syslog_parse_identifier("pidu:", "pidu", NULL, 5);
923a60
+        test_syslog_parse_identifier("pidu: ", "pidu", NULL, 6);
923a60
+        test_syslog_parse_identifier("pidu : ", NULL, NULL, 0);
923a60
 
923a60
         return 0;
923a60
 }