From: Yu Watanabe Date: Tue, 2 Jan 2024 19:30:29 +0000 (+0900) Subject: sd-journal: do not read unnecessary object X-Git-Tag: v256-rc1~908^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6c4ea35962fe8d9715101f99f4ac44b8c5eaf51b;p=thirdparty%2Fsystemd.git sd-journal: do not read unnecessary object In journal_file_next_entry(), if the passed offset matches an entry object, then generic_array_bisect() returns the object, but the object we requested is the next (or previous) object. Hence, we should not validate the object returned by generic_array_bisect(), otherwise it may fail when the journal is corrupted. Note the validity of the entry object that should be returned by journal_file_next_entry() will be checked in the following generic_array_get(). So, when journal_file_next_entry() succeeds, the returned object is always validated. --- diff --git a/src/libsystemd/sd-journal/journal-file.c b/src/libsystemd/sd-journal/journal-file.c index 24a02c74c2c..64bf8ef9afe 100644 --- a/src/libsystemd/sd-journal/journal-file.c +++ b/src/libsystemd/sd-journal/journal-file.c @@ -3539,7 +3539,8 @@ int journal_file_next_entry( p, test_object_offset, direction, - ret_object ? &o : NULL, &q, &i); + NULL, &q, &i); /* Here, do not read entry object, as the result object + * may not be the one we want, and it may be broken. */ if (r <= 0) return r; @@ -3549,12 +3550,11 @@ int journal_file_next_entry( * the same offset, and the index needs to be shifted. Otherwise, use the found object as is, * as it is the nearest entry object from the input offset 'p'. */ - if (p != q) - goto found; - - r = bump_array_index(&i, direction, n); - if (r <= 0) - return r; + if (p == q) { + r = bump_array_index(&i, direction, n); + if (r <= 0) + return r; + } /* And jump to it */ r = generic_array_get(f, le64toh(f->header->entry_array_offset), i, direction, ret_object ? &o : NULL, &q); @@ -3566,7 +3566,7 @@ int journal_file_next_entry( return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), "%s: entry array not properly ordered at entry index %" PRIu64, f->path, i); -found: + if (ret_object) *ret_object = o; if (ret_offset)