basic/journal-importer: escape & ellipsize bad data in log entries
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 17 May 2018 09:09:07 +0000 (11:09 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 31 May 2018 12:30:08 +0000 (14:30 +0200)
We shouldn't just log arbitrary stuff, in particular newlines and control chars
Now:
Unknown dunder line __CURSORFACILITY=6\nSYSLOG_IDENTIFIER=/USR/SBIN/CRON\nMES…, ignoring.
Unknown dunder line __REALTIME_TIME[TAMP=1404101101501874\n__MONOTONIC_TIMEST…, ignoring.

src/basic/journal-importer.c
src/basic/string-util.h

index 81044b8..1d5f84a 100644 (file)
@@ -247,6 +247,7 @@ static int get_data_newline(JournalImporter *imp) {
 
 static int process_dunder(JournalImporter *imp, char *line, size_t n) {
         const char *timestamp;
+        char buf[CELLESCAPE_DEFAULT_LENGTH];
         int r;
 
         assert(line);
@@ -264,10 +265,12 @@ static int process_dunder(JournalImporter *imp, char *line, size_t n) {
         timestamp = startswith(line, "__REALTIME_TIMESTAMP=");
         if (timestamp) {
                 uint64_t x;
+
                 line[n-1] = '\0';
                 r = safe_atou64(timestamp, &x);
                 if (r < 0)
-                        return log_warning_errno(r, "Failed to parse __REALTIME_TIMESTAMP '%s': %m", timestamp);
+                        return log_warning_errno(r, "Failed to parse __REALTIME_TIMESTAMP '%s': %m",
+                                                 cellescape(buf, sizeof buf, timestamp));
                 else if (!VALID_REALTIME(x)) {
                         log_warning("__REALTIME_TIMESTAMP out of range, ignoring: %"PRIu64, x);
                         return -ERANGE;
@@ -280,10 +283,12 @@ static int process_dunder(JournalImporter *imp, char *line, size_t n) {
         timestamp = startswith(line, "__MONOTONIC_TIMESTAMP=");
         if (timestamp) {
                 uint64_t x;
+
                 line[n-1] = '\0';
                 r = safe_atou64(timestamp, &x);
                 if (r < 0)
-                        return log_warning_errno(r, "Failed to parse __MONOTONIC_TIMESTAMP '%s': %m", timestamp);
+                        return log_warning_errno(r, "Failed to parse __MONOTONIC_TIMESTAMP '%s': %m",
+                                                 cellescape(buf, sizeof buf, timestamp));
                 else if (!VALID_MONOTONIC(x)) {
                         log_warning("__MONOTONIC_TIMESTAMP out of range, ignoring: %"PRIu64, x);
                         return -ERANGE;
@@ -295,7 +300,7 @@ static int process_dunder(JournalImporter *imp, char *line, size_t n) {
 
         timestamp = startswith(line, "__");
         if (timestamp) {
-                log_notice("Unknown dunder line %s, ignoring.", line);
+                log_notice("Unknown dunder line __%s, ignoring.", cellescape(buf, sizeof buf, timestamp));
                 return 1;
         }
 
index 3c5e2a9..25980e7 100644 (file)
@@ -159,6 +159,9 @@ char *ellipsize_mem(const char *s, size_t old_length_bytes, size_t new_length_co
 char *ellipsize(const char *s, size_t length, unsigned percent);
 char *cellescape(char *buf, size_t len, const char *s);
 
+/* This limit is arbitrary, enough to give some idea what the string contains */
+#define CELLESCAPE_DEFAULT_LENGTH 64
+
 bool nulstr_contains(const char *nulstr, const char *needle);
 
 char* strshorten(char *s, size_t l);