]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-journal: also verify tail_entry_boot_id and friends in journal_file_verify_header()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 22 Sep 2023 18:14:40 +0000 (03:14 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 26 Sep 2023 04:26:22 +0000 (13:26 +0900)
Then, we can drop the redundant check in journal_file_read_tail_timestamp().

src/libsystemd/sd-journal/journal-file.c
src/libsystemd/sd-journal/sd-journal.c

index ef860d228b9d548b7d53f542142322729061a5d1..638a6e1f9d619b00e470d978e188b81da417c62c 100644 (file)
@@ -623,10 +623,36 @@ static int journal_file_verify_header(JournalFile *f) {
                         return -ENODATA;
         }
 
-        if (JOURNAL_HEADER_CONTAINS(f->header, tail_entry_offset))
-                if (!offset_is_valid(le64toh(f->header->tail_entry_offset), header_size, tail_object_offset))
+        if (JOURNAL_HEADER_CONTAINS(f->header, tail_entry_offset)) {
+                uint64_t offset = le64toh(f->header->tail_entry_offset);
+
+                if (!offset_is_valid(offset, header_size, tail_object_offset))
                         return -ENODATA;
 
+                if (offset > 0) {
+                        /* When there is an entry object, then these fields must be filled. */
+                        if (sd_id128_is_null(f->header->tail_entry_boot_id))
+                                return -ENODATA;
+                        if (!VALID_REALTIME(le64toh(f->header->head_entry_realtime)))
+                                return -ENODATA;
+                        if (!VALID_REALTIME(le64toh(f->header->tail_entry_realtime)))
+                                return -ENODATA;
+                        if (!VALID_MONOTONIC(le64toh(f->header->tail_entry_realtime)))
+                                return -ENODATA;
+                } else {
+                        /* Otherwise, the fields must be zero. */
+                        if (JOURNAL_HEADER_TAIL_ENTRY_BOOT_ID(f->header) &&
+                            !sd_id128_is_null(f->header->tail_entry_boot_id))
+                                return -ENODATA;
+                        if (f->header->head_entry_realtime != 0)
+                                return -ENODATA;
+                        if (f->header->tail_entry_realtime != 0)
+                                return -ENODATA;
+                        if (f->header->tail_entry_realtime != 0)
+                                return -ENODATA;
+                }
+        }
+
         /* Verify number of objects */
         uint64_t n_objects = le64toh(f->header->n_objects);
         if (n_objects > arena_size / sizeof(ObjectHeader))
index 5e49311327af201c0430dba94c647bf16442546e..718d45af0efffa01865cdcc577cd04b437abeea9 100644 (file)
@@ -2445,14 +2445,6 @@ static int journal_file_read_tail_timestamp(sd_journal *j, JournalFile *f) {
                         mo = le64toh(f->header->tail_entry_monotonic);
                         rt = le64toh(f->header->tail_entry_realtime);
                         id = f->header->tail_entry_boot_id;
-
-                        /* Some superficial checking if what we read makes sense. Note that we only do this
-                         * when reading the timestamps from the Header object, but not when reading them from
-                         * the most recent entry object, because in that case journal_file_move_to_object()
-                         * already validated them. */
-                        if (!VALID_MONOTONIC(mo) || !VALID_REALTIME(rt))
-                                return -ENODATA;
-
                 } else {
                         /* Otherwise let's find the last entry manually (this possibly means traversing the
                          * chain of entry arrays, till the end */