naccyde / rpms / systemd

Forked from rpms/systemd a year ago
Clone
4fbe94
From bef599d1a0e41afe4b6f1d6dfb6fbc86896ab8c5 Mon Sep 17 00:00:00 2001
4fbe94
From: Evgeny Vereshchagin <evvers@ya.ru>
4fbe94
Date: Fri, 16 Nov 2018 23:32:31 +0100
4fbe94
Subject: [PATCH] journald: check whether sscanf has changed the value
4fbe94
 corresponding to %n
4fbe94
4fbe94
It's possible for sscanf to receive strings containing all three fields
4fbe94
and not matching the template at the same time. When this happens the
4fbe94
value of k doesn't change, which basically means that process_audit_string
4fbe94
tries to access memory randomly. Sometimes it works and sometimes it doesn't :-)
4fbe94
4fbe94
See also https://bugzilla.redhat.com/show_bug.cgi?id=1059314.
4fbe94
4fbe94
(cherry picked from commit 1dab14aba749b9c5ab8176c5730107b70834240b)
4fbe94
4fbe94
Resolves: #1764560
4fbe94
---
4fbe94
 src/journal/journald-audit.c        | 3 ++-
4fbe94
 test/fuzz/fuzz-journald-audit/crash | 1 +
4fbe94
 2 files changed, 3 insertions(+), 1 deletion(-)
4fbe94
 create mode 100644 test/fuzz/fuzz-journald-audit/crash
4fbe94
4fbe94
diff --git a/src/journal/journald-audit.c b/src/journal/journald-audit.c
4fbe94
index 7810a0139a..0fd6ab2a84 100644
4fbe94
--- a/src/journal/journald-audit.c
4fbe94
+++ b/src/journal/journald-audit.c
4fbe94
@@ -341,11 +341,12 @@ void process_audit_string(Server *s, int type, const char *data, size_t size) {
4fbe94
         if (!p)
4fbe94
                 return;
4fbe94
 
4fbe94
+        k = 0;
4fbe94
         if (sscanf(p, "(%" PRIu64 ".%" PRIu64 ":%" PRIu64 "):%n",
4fbe94
                    &seconds,
4fbe94
                    &msec,
4fbe94
                    &id,
4fbe94
-                   &k) != 3)
4fbe94
+                   &k) != 3 || k == 0)
4fbe94
                 return;
4fbe94
 
4fbe94
         p += k;
4fbe94
diff --git a/test/fuzz/fuzz-journald-audit/crash b/test/fuzz/fuzz-journald-audit/crash
4fbe94
new file mode 100644
4fbe94
index 0000000000..91bd85ca6e
4fbe94
--- /dev/null
4fbe94
+++ b/test/fuzz/fuzz-journald-audit/crash
4fbe94
@@ -0,0 +1 @@
4fbe94
+audit(1542398162.211:744) pid=7376 uid=1000 auid=1000 ses=6 subj=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 msg='op=PAM:accounting grantors=pam_unix,pam_localuser acct="vagrant" exe="/usr/bin/sudo" hostname=? addr=? terminal=/dev/pts/1 res=success'