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