84b277
From 559cbd8367f3eb2c7ce49f923a688e7f83c5b439 Mon Sep 17 00:00:00 2001
84b277
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
84b277
Date: Thu, 27 Feb 2014 00:07:29 -0500
84b277
Subject: [PATCH] journal: assume that next entry is after previous entry
84b277
84b277
With a corrupted file, we can get in a situation where two entries
84b277
in the entry array point to the same object. Then journal_file_next_entry
84b277
will find the first one using generic_arrray_bisect, and try to move to
84b277
the second one, but since the address is the same, generic_array_get will
84b277
return the first one. journal_file_next_entry ends up in an infinite loop.
84b277
84b277
https://bugzilla.redhat.com/show_bug.cgi?id=1047039
84b277
84b277
(cherry-picked from fb099c8d2af6620db2709e826a258089d10cdfe8)
84b277
84b277
Resolves: #1147524
84b277
---
84b277
 src/journal/journal-file.c | 26 ++++++++++++++++++++------
84b277
 1 file changed, 20 insertions(+), 6 deletions(-)
84b277
84b277
diff --git a/src/journal/journal-file.c b/src/journal/journal-file.c
84b277
index 9dbd674..f101a1f 100644
84b277
--- a/src/journal/journal-file.c
84b277
+++ b/src/journal/journal-file.c
84b277
@@ -1359,7 +1359,7 @@ int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const st
84b277
 }
84b277
 
84b277
 typedef struct ChainCacheItem {
84b277
-        uint64_t first; /* the array at the begin of the chain */
84b277
+        uint64_t first; /* the array at the beginning of the chain */
84b277
         uint64_t array; /* the cached array */
84b277
         uint64_t begin; /* the first item in the cached array */
84b277
         uint64_t total; /* the total number of items in all arrays before this one in the chain */
84b277
@@ -1945,7 +1945,7 @@ int journal_file_next_entry(
84b277
                 direction_t direction,
84b277
                 Object **ret, uint64_t *offset) {
84b277
 
84b277
-        uint64_t i, n;
84b277
+        uint64_t i, n, ofs;
84b277
         int r;
84b277
 
84b277
         assert(f);
84b277
@@ -1986,10 +1986,24 @@ int journal_file_next_entry(
84b277
         }
84b277
 
84b277
         /* And jump to it */
84b277
-        return generic_array_get(f,
84b277
-                                 le64toh(f->header->entry_array_offset),
84b277
-                                 i,
84b277
-                                 ret, offset);
84b277
+        r = generic_array_get(f,
84b277
+                              le64toh(f->header->entry_array_offset),
84b277
+                              i,
84b277
+                              ret, &ofs;;
84b277
+        if (r <= 0)
84b277
+                return r;
84b277
+
84b277
+        if (p > 0 &&
84b277
+            (direction == DIRECTION_DOWN ? ofs <= p : ofs >= p)) {
84b277
+                log_debug("%s: entry array corrupted at entry %"PRIu64,
84b277
+                          f->path, i);
84b277
+                return -EBADMSG;
84b277
+        }
84b277
+
84b277
+        if (offset)
84b277
+                *offset = ofs;
84b277
+
84b277
+        return 1;
84b277
 }
84b277
 
84b277
 int journal_file_skip_entry(