Blame SOURCES/0224-syslog-fix-segfault-in-syslog_parse_priority.patch

a3e2b5
From a9e1b3a33f65721e7f77f8d8253512dd16cbafdb Mon Sep 17 00:00:00 2001
a3e2b5
From: Yu Watanabe <watanabe.yu+github@gmail.com>
a3e2b5
Date: Wed, 8 Aug 2018 18:27:15 +0900
a3e2b5
Subject: [PATCH] syslog: fix segfault in syslog_parse_priority()
a3e2b5
a3e2b5
(cherry picked from commit a5ee33b951cfa22db53d0274c9c6c0d9d4dae39d)
a3e2b5
(cherry picked from commit 8bd791fb3a8e85063e297204bdef8004aacd22b1)
a3e2b5
Related: #1781712
a3e2b5
---
a3e2b5
 src/basic/syslog-util.c           | 20 +++++++++++---------
a3e2b5
 src/journal/test-journal-syslog.c | 20 ++++++++++++++++++++
a3e2b5
 2 files changed, 31 insertions(+), 9 deletions(-)
a3e2b5
a3e2b5
diff --git a/src/basic/syslog-util.c b/src/basic/syslog-util.c
a3e2b5
index 21461fa581..fe129482f3 100644
a3e2b5
--- a/src/basic/syslog-util.c
a3e2b5
+++ b/src/basic/syslog-util.c
a3e2b5
@@ -10,7 +10,8 @@
a3e2b5
 
a3e2b5
 int syslog_parse_priority(const char **p, int *priority, bool with_facility) {
a3e2b5
         int a = 0, b = 0, c = 0;
a3e2b5
-        int k;
a3e2b5
+        const char *end;
a3e2b5
+        size_t k;
a3e2b5
 
a3e2b5
         assert(p);
a3e2b5
         assert(*p);
a3e2b5
@@ -19,21 +20,22 @@ int syslog_parse_priority(const char **p, int *priority, bool with_facility) {
a3e2b5
         if ((*p)[0] != '<')
a3e2b5
                 return 0;
a3e2b5
 
a3e2b5
-        if (!strchr(*p, '>'))
a3e2b5
+        end = strchr(*p, '>');
a3e2b5
+        if (!end)
a3e2b5
                 return 0;
a3e2b5
 
a3e2b5
-        if ((*p)[2] == '>') {
a3e2b5
+        k = end - *p;
a3e2b5
+        assert(k > 0);
a3e2b5
+
a3e2b5
+        if (k == 2)
a3e2b5
                 c = undecchar((*p)[1]);
a3e2b5
-                k = 3;
a3e2b5
-        } else if ((*p)[3] == '>') {
a3e2b5
+        else if (k == 3) {
a3e2b5
                 b = undecchar((*p)[1]);
a3e2b5
                 c = undecchar((*p)[2]);
a3e2b5
-                k = 4;
a3e2b5
-        } else if ((*p)[4] == '>') {
a3e2b5
+        } else if (k == 4) {
a3e2b5
                 a = undecchar((*p)[1]);
a3e2b5
                 b = undecchar((*p)[2]);
a3e2b5
                 c = undecchar((*p)[3]);
a3e2b5
-                k = 5;
a3e2b5
         } else
a3e2b5
                 return 0;
a3e2b5
 
a3e2b5
@@ -46,7 +48,7 @@ int syslog_parse_priority(const char **p, int *priority, bool with_facility) {
a3e2b5
         else
a3e2b5
                 *priority = (*priority & LOG_FACMASK) | c;
a3e2b5
 
a3e2b5
-        *p += k;
a3e2b5
+        *p += k + 1;
a3e2b5
         return 1;
a3e2b5
 }
a3e2b5
 
a3e2b5
diff --git a/src/journal/test-journal-syslog.c b/src/journal/test-journal-syslog.c
a3e2b5
index 7294cde032..120477cc9f 100644
a3e2b5
--- a/src/journal/test-journal-syslog.c
a3e2b5
+++ b/src/journal/test-journal-syslog.c
a3e2b5
@@ -4,6 +4,7 @@
a3e2b5
 #include "journald-syslog.h"
a3e2b5
 #include "macro.h"
a3e2b5
 #include "string-util.h"
a3e2b5
+#include "syslog-util.h"
a3e2b5
 
a3e2b5
 static void test_syslog_parse_identifier(const char *str,
a3e2b5
                                          const char *ident, const char *pid, const char *rest, int ret) {
a3e2b5
@@ -19,6 +20,17 @@ static void test_syslog_parse_identifier(const char *str,
a3e2b5
         assert_se(streq(buf, rest));
a3e2b5
 }
a3e2b5
 
a3e2b5
+static void test_syslog_parse_priority(const char *str, int priority, int ret) {
a3e2b5
+        const char *buf = str;
a3e2b5
+        int priority2, ret2;
a3e2b5
+
a3e2b5
+        ret2 = syslog_parse_priority(&buf, &priority2, false);
a3e2b5
+
a3e2b5
+        assert_se(ret == ret2);
a3e2b5
+        if (ret2 == 1)
a3e2b5
+                assert_se(priority == priority2);
a3e2b5
+}
a3e2b5
+
a3e2b5
 int main(void) {
a3e2b5
         test_syslog_parse_identifier("pidu[111]: xxx", "pidu", "111", "xxx", 11);
a3e2b5
         test_syslog_parse_identifier("pidu: xxx", "pidu", NULL, "xxx", 6);
a3e2b5
@@ -33,5 +45,13 @@ int main(void) {
a3e2b5
         test_syslog_parse_identifier("pidu: ", "pidu", NULL, "", 6);
a3e2b5
         test_syslog_parse_identifier("pidu : ", NULL, NULL, "pidu : ", 0);
a3e2b5
 
a3e2b5
+        test_syslog_parse_priority("<>", 0, 0);
a3e2b5
+        test_syslog_parse_priority("<>aaa", 0, 0);
a3e2b5
+        test_syslog_parse_priority("<aaaa>", 0, 0);
a3e2b5
+        test_syslog_parse_priority("<aaaa>aaa", 0, 0);
a3e2b5
+        test_syslog_parse_priority(" <aaaa>", 0, 0);
a3e2b5
+        test_syslog_parse_priority(" <aaaa>aaa", 0, 0);
a3e2b5
+        /* TODO: add test cases of valid priorities */
a3e2b5
+
a3e2b5
         return 0;
a3e2b5
 }