summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormsizanoen1 <msizanoen@qtmlabs.xyz>2022-10-08 14:37:02 +0700
committermsizanoen1 <msizanoen@qtmlabs.xyz>2023-01-09 22:57:30 +0700
commit1d8d483f59ffa62974772fb58a8ef4abe88550ec (patch)
treebea75b39fcef44dcc2d2dec8472277763650529d
parent790096852738b4961ffd8b10664a67b3f075d612 (diff)
downloadsystemd-1d8d483f59ffa62974772fb58a8ef4abe88550ec.tar.gz
journal: enforce strict consistency for realtime timestamps on write
Ensure all realtime timestamps in a journal file are strictly ordered on write as a defense-in-depth measure. All known callers of journal_file_append_entry and journal_file_copy_entry, which call this function, should be able to handle the error by rotating the journal. This is especially helpful for systems with RTC local time enabled, where all log entries from initramfs might be recorded as several hours later than it actually is, which won't get caught by journald during log flushing. In those cases, the resulting inconsistency can cause libsystemd to loop infinitely through journal files as observed in `abrt-dump-journal-oops`.
-rw-r--r--src/journal/journald-server.c4
-rw-r--r--src/libsystemd/sd-journal/journal-file.c6
2 files changed, 10 insertions, 0 deletions
diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c
index 9e2d239abd..b59f42c4b8 100644
--- a/src/journal/journald-server.c
+++ b/src/journal/journald-server.c
@@ -839,6 +839,10 @@ static bool shall_try_append_again(JournalFile *f, int r) {
log_ratelimit_warning(JOURNAL_LOG_RATELIMIT, "%s: Journal file is from the future, rotating.", f->path);
return true;
+ case -EREMCHG: /* Time jumped backwards relative to last journal entry */
+ log_ratelimit_warning(JOURNAL_LOG_RATELIMIT, "%s: Time jumped backwards relative to last journal entry, rotating.", f->path);
+ return true;
+
case -EAFNOSUPPORT:
log_ratelimit_warning(JOURNAL_LOG_RATELIMIT,
"%s: underlying file system does not support memory mapping or another required file system feature.",
diff --git a/src/libsystemd/sd-journal/journal-file.c b/src/libsystemd/sd-journal/journal-file.c
index ab518fea80..805773474c 100644
--- a/src/libsystemd/sd-journal/journal-file.c
+++ b/src/libsystemd/sd-journal/journal-file.c
@@ -2092,6 +2092,12 @@ static int journal_file_append_entry_internal(
assert(ts);
assert(items || n_items == 0);
+ if (ts->realtime < le64toh(f->header->tail_entry_realtime))
+ return log_debug_errno(SYNTHETIC_ERRNO(EREMCHG),
+ "Realtime timestamp %" PRIu64 " smaller than previous realtime "
+ "timestamp %" PRIu64 ", refusing entry.",
+ ts->realtime, le64toh(f->header->tail_entry_realtime));
+
osize = offsetof(Object, entry.items) + (n_items * journal_file_entry_item_size(f));
r = journal_file_append_object(f, OBJECT_ENTRY, osize, &o, &np);