From: Zbigniew Jędrzejewski-Szmek Date: Thu, 17 May 2018 09:09:07 +0000 (+0200) Subject: basic/journal-importer: escape & ellipsize bad data in log entries X-Git-Tag: v239~172^2~15 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=cca24fc3e60680861b1783dd54e80da7d16db347;p=thirdparty%2Fsystemd.git basic/journal-importer: escape & ellipsize bad data in log entries 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. --- diff --git a/src/basic/journal-importer.c b/src/basic/journal-importer.c index 81044b85fbc..1d5f84af4d4 100644 --- a/src/basic/journal-importer.c +++ b/src/basic/journal-importer.c @@ -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; } diff --git a/src/basic/string-util.h b/src/basic/string-util.h index 3c5e2a9ef69..25980e7cc88 100644 --- a/src/basic/string-util.h +++ b/src/basic/string-util.h @@ -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);