1From 81a07263f1e522a376d3a30f96f51df3f2879f8a Mon Sep 17 00:00:00 2001
2From: Laszlo Varady <laszlo.varady@protonmail.com>
3Date: Sat, 20 Aug 2022 12:22:44 +0200
4Subject: [PATCH 2/8] syslogformat: add bug reproducer test for non-zero terminated
5 input
6MIME-Version: 1.0
7Content-Type: text/plain; charset=UTF-8
8Content-Transfer-Encoding: 8bit
9
10CVE: CVE-2022-38725
11
12Upstream-Status: Backport
13[https://github.com/syslog-ng/syslog-ng/commit/81a07263f1e522a376d3a30f96f51df3f2879f8a]
14
15Signed-off-by: László Várady <laszlo.varady@protonmail.com>
16
17Signed-off-by: Yogita Urade <yogita.urade@windriver.com>
18---
19 modules/syslogformat/CMakeLists.txt           |  1 +
20 modules/syslogformat/Makefile.am              |  2 +
21 modules/syslogformat/tests/CMakeLists.txt     |  1 +
22 modules/syslogformat/tests/Makefile.am        |  9 +++
23 .../syslogformat/tests/test_syslog_format.c   | 72 +++++++++++++++++++
24 5 files changed, 85 insertions(+)
25 create mode 100644 modules/syslogformat/tests/CMakeLists.txt
26 create mode 100644 modules/syslogformat/tests/Makefile.am
27 create mode 100644 modules/syslogformat/tests/test_syslog_format.c
28
29diff --git a/modules/syslogformat/CMakeLists.txt b/modules/syslogformat/CMakeLists.txt
30index 94ee01aa2..64848efee 100644
31--- a/modules/syslogformat/CMakeLists.txt
32+++ b/modules/syslogformat/CMakeLists.txt
33@@ -14,3 +14,4 @@ add_module(
34   SOURCES ${SYSLOGFORMAT_SOURCES}
35 )
36
37+add_test_subdirectory(tests)
38diff --git a/modules/syslogformat/Makefile.am b/modules/syslogformat/Makefile.am
39index f13f88c1b..14cdf589d 100644
40--- a/modules/syslogformat/Makefile.am
41+++ b/modules/syslogformat/Makefile.am
42@@ -31,3 +31,5 @@ modules_syslogformat_libsyslogformat_la_DEPENDENCIES =	\
43 modules/syslogformat modules/syslogformat/ mod-syslogformat: \
44 	modules/syslogformat/libsyslogformat.la
45 .PHONY: modules/syslogformat/ mod-syslogformat
46+
47+include modules/syslogformat/tests/Makefile.am
48diff --git a/modules/syslogformat/tests/CMakeLists.txt b/modules/syslogformat/tests/CMakeLists.txt
49new file mode 100644
50index 000000000..2e45b7194
51--- /dev/null
52+++ b/modules/syslogformat/tests/CMakeLists.txt
53@@ -0,0 +1 @@
54+add_unit_test(CRITERION TARGET test_syslog_format DEPENDS syslogformat)
55diff --git a/modules/syslogformat/tests/Makefile.am b/modules/syslogformat/tests/Makefile.am
56new file mode 100644
57index 000000000..7ee66a59c
58--- /dev/null
59+++ b/modules/syslogformat/tests/Makefile.am
60@@ -0,0 +1,9 @@
61+modules_syslogformat_tests_TESTS = \
62+    modules/syslogformat/tests/test_syslog_format
63+
64+check_PROGRAMS += ${modules_syslogformat_tests_TESTS}
65+
66+EXTRA_DIST += modules/syslogformat/tests/CMakeLists.txt
67+
68+modules_syslogformat_tests_test_syslog_format_CFLAGS = $(TEST_CFLAGS) -I$(top_srcdir)/modules/syslogformat
69+modules_syslogformat_tests_test_syslog_format_LDADD = $(TEST_LDADD) $(PREOPEN_SYSLOGFORMAT)
70diff --git a/modules/syslogformat/tests/test_syslog_format.c b/modules/syslogformat/tests/test_syslog_format.c
71new file mode 100644
72index 000000000..b247fe3c5
73--- /dev/null
74+++ b/modules/syslogformat/tests/test_syslog_format.c
75@@ -0,0 +1,72 @@
76+/*
77+ * Copyright (c) 2022 One Identity
78+ * Copyright (c) 2022 László Várady
79+ *
80+ * This program is free software; you can redistribute it and/or modify it
81+ * under the terms of the GNU General Public License version 2 as published
82+ * by the Free Software Foundation, or (at your option) any later version.
83+ *
84+ * This program is distributed in the hope that it will be useful,
85+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
86+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
87+ * GNU General Public License for more details.
88+ *
89+ * You should have received a copy of the GNU General Public License
90+ * along with this program; if not, write to the Free Software
91+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
92+ *
93+ * As an additional exemption you are allowed to compile & link against the
94+ * OpenSSL libraries as published by the OpenSSL project. See the file
95+ * COPYING for details.
96+ *
97+ */
98+
99+#include <criterion/criterion.h>
100+
101+#include "apphook.h"
102+#include "cfg.h"
103+#include "syslog-format.h"
104+#include "logmsg/logmsg.h"
105+#include "msg-format.h"
106+#include "scratch-buffers.h"
107+
108+#include <string.h>
109+
110+GlobalConfig *cfg;
111+MsgFormatOptions parse_options;
112+
113+static void
114+setup(void)
115+{
116+  app_startup();
117+  syslog_format_init();
118+
119+  cfg = cfg_new_snippet();
120+  msg_format_options_defaults(&parse_options);
121+}
122+
123+static void
124+teardown(void)
125+{
126+  scratch_buffers_explicit_gc();
127+  app_shutdown();
128+  cfg_free(cfg);
129+}
130+
131+TestSuite(syslog_format, .init = setup, .fini = teardown);
132+
133+Test(syslog_format, parser_should_not_spin_on_non_zero_terminated_input, .timeout = 10)
134+{
135+  const gchar *data = "<182>2022-08-17T05:02:28.217 mymachine su: 'su root' failed for lonvick on /dev/pts/8";
136+  /* chosen carefully to reproduce a bug */
137+  gsize data_length = 27;
138+
139+  msg_format_options_init(&parse_options, cfg);
140+  LogMessage *msg = msg_format_construct_message(&parse_options, (const guchar *) data, data_length);
141+
142+  gsize problem_position;
143+  cr_assert(syslog_format_handler(&parse_options, msg, (const guchar *) data, data_length, &problem_position));
144+
145+  msg_format_options_destroy(&parse_options);
146+  log_msg_unref(msg);
147+}
148--
1492.34.1
150
151