1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "alloc-util.h"
13 #include "journal-authenticate.h"
14 #include "journal-def.h"
15 #include "journal-file.h"
16 #include "journal-verify.h"
19 #include "terminal-util.h"
22 static void draw_progress(uint64_t p
, usec_t
*last_usec
) {
29 z
= now(CLOCK_MONOTONIC
);
32 if (x
!= 0 && x
+ 40 * USEC_PER_MSEC
> z
)
37 n
= (3 * columns()) / 4;
38 j
= (n
* (unsigned) p
) / 65535ULL;
43 fputs("\x1B[?25l" ANSI_HIGHLIGHT_GREEN
, stdout
);
45 for (i
= 0; i
< j
; i
++)
46 fputs("\xe2\x96\x88", stdout
);
48 fputs(ANSI_NORMAL
, stdout
);
50 for (i
= 0; i
< k
; i
++)
51 fputs("\xe2\x96\x91", stdout
);
53 printf(" %3"PRIu64
"%%", 100U * p
/ 65535U);
57 fputs("\x1B[?25h", stdout
);
62 static uint64_t scale_progress(uint64_t scale
, uint64_t p
, uint64_t m
) {
63 /* Calculates scale * p / m, but handles m == 0 safely, and saturates.
64 * Currently all callers use m >= 1, but we keep the check to be defensive.
67 if (p
>= m
|| m
== 0) /* lgtm [cpp/constant-comparison] */
73 static void flush_progress(void) {
79 n
= (3 * columns()) / 4;
83 for (i
= 0; i
< n
+ 5; i
++)
90 #define debug(_offset, _fmt, ...) do { \
92 log_debug(OFSfmt": " _fmt, _offset, ##__VA_ARGS__); \
95 #define warning(_offset, _fmt, ...) do { \
97 log_warning(OFSfmt": " _fmt, _offset, ##__VA_ARGS__); \
100 #define error(_offset, _fmt, ...) do { \
102 log_error(OFSfmt": " _fmt, (uint64_t)_offset, ##__VA_ARGS__); \
105 #define error_errno(_offset, error, _fmt, ...) do { \
107 log_error_errno(error, OFSfmt": " _fmt, (uint64_t)_offset, ##__VA_ARGS__); \
110 static int journal_file_object_verify(JournalFile
*f
, uint64_t offset
, Object
*o
) {
117 /* This does various superficial tests about the length an
118 * possible field values. It does not follow any references to
121 if ((o
->object
.flags
& OBJECT_COMPRESSED_XZ
) &&
122 o
->object
.type
!= OBJECT_DATA
) {
123 error(offset
, "Found compressed object that isn't of type DATA, which is not allowed.");
127 switch (o
->object
.type
) {
133 if (le64toh(o
->data
.entry_offset
) == 0)
134 warning(offset
, "Unused data (entry_offset==0)");
136 if ((le64toh(o
->data
.entry_offset
) == 0) ^ (le64toh(o
->data
.n_entries
) == 0)) {
137 error(offset
, "Bad n_entries: %"PRIu64
, le64toh(o
->data
.n_entries
));
141 if (le64toh(o
->object
.size
) - offsetof(DataObject
, payload
) <= 0) {
142 error(offset
, "Bad object size (<= %zu): %"PRIu64
,
143 offsetof(DataObject
, payload
),
144 le64toh(o
->object
.size
));
148 h1
= le64toh(o
->data
.hash
);
150 compression
= o
->object
.flags
& OBJECT_COMPRESSION_MASK
;
152 _cleanup_free_
void *b
= NULL
;
153 size_t alloc
= 0, b_size
;
155 r
= decompress_blob(compression
,
157 le64toh(o
->object
.size
) - offsetof(Object
, data
.payload
),
158 &b
, &alloc
, &b_size
, 0);
160 error_errno(offset
, r
, "%s decompression failed: %m",
161 object_compressed_to_string(compression
));
165 h2
= hash64(b
, b_size
);
167 h2
= hash64(o
->data
.payload
, le64toh(o
->object
.size
) - offsetof(Object
, data
.payload
));
170 error(offset
, "Invalid hash (%08"PRIx64
" vs. %08"PRIx64
, h1
, h2
);
174 if (!VALID64(le64toh(o
->data
.next_hash_offset
)) ||
175 !VALID64(le64toh(o
->data
.next_field_offset
)) ||
176 !VALID64(le64toh(o
->data
.entry_offset
)) ||
177 !VALID64(le64toh(o
->data
.entry_array_offset
))) {
178 error(offset
, "Invalid offset (next_hash_offset="OFSfmt
", next_field_offset="OFSfmt
", entry_offset="OFSfmt
", entry_array_offset="OFSfmt
,
179 le64toh(o
->data
.next_hash_offset
),
180 le64toh(o
->data
.next_field_offset
),
181 le64toh(o
->data
.entry_offset
),
182 le64toh(o
->data
.entry_array_offset
));
190 if (le64toh(o
->object
.size
) - offsetof(FieldObject
, payload
) <= 0) {
192 "Bad field size (<= %zu): %"PRIu64
,
193 offsetof(FieldObject
, payload
),
194 le64toh(o
->object
.size
));
198 if (!VALID64(le64toh(o
->field
.next_hash_offset
)) ||
199 !VALID64(le64toh(o
->field
.head_data_offset
))) {
201 "Invalid offset (next_hash_offset="OFSfmt
", head_data_offset="OFSfmt
,
202 le64toh(o
->field
.next_hash_offset
),
203 le64toh(o
->field
.head_data_offset
));
209 if ((le64toh(o
->object
.size
) - offsetof(EntryObject
, items
)) % sizeof(EntryItem
) != 0) {
211 "Bad entry size (<= %zu): %"PRIu64
,
212 offsetof(EntryObject
, items
),
213 le64toh(o
->object
.size
));
217 if ((le64toh(o
->object
.size
) - offsetof(EntryObject
, items
)) / sizeof(EntryItem
) <= 0) {
219 "Invalid number items in entry: %"PRIu64
,
220 (le64toh(o
->object
.size
) - offsetof(EntryObject
, items
)) / sizeof(EntryItem
));
224 if (le64toh(o
->entry
.seqnum
) <= 0) {
226 "Invalid entry seqnum: %"PRIx64
,
227 le64toh(o
->entry
.seqnum
));
231 if (!VALID_REALTIME(le64toh(o
->entry
.realtime
))) {
233 "Invalid entry realtime timestamp: %"PRIu64
,
234 le64toh(o
->entry
.realtime
));
238 if (!VALID_MONOTONIC(le64toh(o
->entry
.monotonic
))) {
240 "Invalid entry monotonic timestamp: %"PRIu64
,
241 le64toh(o
->entry
.monotonic
));
245 for (i
= 0; i
< journal_file_entry_n_items(o
); i
++) {
246 if (le64toh(o
->entry
.items
[i
].object_offset
) == 0 ||
247 !VALID64(le64toh(o
->entry
.items
[i
].object_offset
))) {
249 "Invalid entry item (%"PRIu64
"/%"PRIu64
" offset: "OFSfmt
,
250 i
, journal_file_entry_n_items(o
),
251 le64toh(o
->entry
.items
[i
].object_offset
));
258 case OBJECT_DATA_HASH_TABLE
:
259 case OBJECT_FIELD_HASH_TABLE
:
260 if ((le64toh(o
->object
.size
) - offsetof(HashTableObject
, items
)) % sizeof(HashItem
) != 0 ||
261 (le64toh(o
->object
.size
) - offsetof(HashTableObject
, items
)) / sizeof(HashItem
) <= 0) {
263 "Invalid %s hash table size: %"PRIu64
,
264 o
->object
.type
== OBJECT_DATA_HASH_TABLE
? "data" : "field",
265 le64toh(o
->object
.size
));
269 for (i
= 0; i
< journal_file_hash_table_n_items(o
); i
++) {
270 if (o
->hash_table
.items
[i
].head_hash_offset
!= 0 &&
271 !VALID64(le64toh(o
->hash_table
.items
[i
].head_hash_offset
))) {
273 "Invalid %s hash table item (%"PRIu64
"/%"PRIu64
") head_hash_offset: "OFSfmt
,
274 o
->object
.type
== OBJECT_DATA_HASH_TABLE
? "data" : "field",
275 i
, journal_file_hash_table_n_items(o
),
276 le64toh(o
->hash_table
.items
[i
].head_hash_offset
));
279 if (o
->hash_table
.items
[i
].tail_hash_offset
!= 0 &&
280 !VALID64(le64toh(o
->hash_table
.items
[i
].tail_hash_offset
))) {
282 "Invalid %s hash table item (%"PRIu64
"/%"PRIu64
") tail_hash_offset: "OFSfmt
,
283 o
->object
.type
== OBJECT_DATA_HASH_TABLE
? "data" : "field",
284 i
, journal_file_hash_table_n_items(o
),
285 le64toh(o
->hash_table
.items
[i
].tail_hash_offset
));
289 if ((o
->hash_table
.items
[i
].head_hash_offset
!= 0) !=
290 (o
->hash_table
.items
[i
].tail_hash_offset
!= 0)) {
292 "Invalid %s hash table item (%"PRIu64
"/%"PRIu64
"): head_hash_offset="OFSfmt
" tail_hash_offset="OFSfmt
,
293 o
->object
.type
== OBJECT_DATA_HASH_TABLE
? "data" : "field",
294 i
, journal_file_hash_table_n_items(o
),
295 le64toh(o
->hash_table
.items
[i
].head_hash_offset
),
296 le64toh(o
->hash_table
.items
[i
].tail_hash_offset
));
303 case OBJECT_ENTRY_ARRAY
:
304 if ((le64toh(o
->object
.size
) - offsetof(EntryArrayObject
, items
)) % sizeof(le64_t
) != 0 ||
305 (le64toh(o
->object
.size
) - offsetof(EntryArrayObject
, items
)) / sizeof(le64_t
) <= 0) {
307 "Invalid object entry array size: %"PRIu64
,
308 le64toh(o
->object
.size
));
312 if (!VALID64(le64toh(o
->entry_array
.next_entry_array_offset
))) {
314 "Invalid object entry array next_entry_array_offset: "OFSfmt
,
315 le64toh(o
->entry_array
.next_entry_array_offset
));
319 for (i
= 0; i
< journal_file_entry_array_n_items(o
); i
++)
320 if (le64toh(o
->entry_array
.items
[i
]) != 0 &&
321 !VALID64(le64toh(o
->entry_array
.items
[i
]))) {
323 "Invalid object entry array item (%"PRIu64
"/%"PRIu64
"): "OFSfmt
,
324 i
, journal_file_entry_array_n_items(o
),
325 le64toh(o
->entry_array
.items
[i
]));
332 if (le64toh(o
->object
.size
) != sizeof(TagObject
)) {
334 "Invalid object tag size: %"PRIu64
,
335 le64toh(o
->object
.size
));
339 if (!VALID_EPOCH(le64toh(o
->tag
.epoch
))) {
341 "Invalid object tag epoch: %"PRIu64
,
342 le64toh(o
->tag
.epoch
));
352 static int write_uint64(int fd
, uint64_t p
) {
355 k
= write(fd
, &p
, sizeof(p
));
364 static int contains_uint64(MMapCache
*m
, MMapFileDescriptor
*f
, uint64_t n
, uint64_t p
) {
379 r
= mmap_cache_get(m
, f
, PROT_READ
|PROT_WRITE
, 0, false, c
* sizeof(uint64_t), sizeof(uint64_t), NULL
, (void **) &z
, NULL
);
398 static int entry_points_to_data(
400 MMapFileDescriptor
*cache_entry_fd
,
411 assert(cache_entry_fd
);
413 if (!contains_uint64(f
->mmap
, cache_entry_fd
, n_entries
, entry_p
)) {
414 error(data_p
, "Data object references invalid entry at "OFSfmt
, entry_p
);
418 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, entry_p
, &o
);
422 n
= journal_file_entry_n_items(o
);
423 for (i
= 0; i
< n
; i
++)
424 if (le64toh(o
->entry
.items
[i
].object_offset
) == data_p
) {
430 error(entry_p
, "Data object at "OFSfmt
" not referenced by linked entry", data_p
);
434 /* Check if this entry is also in main entry array. Since the
435 * main entry array has already been verified we can rely on
436 * its consistency. */
439 n
= le64toh(f
->header
->n_entries
);
440 a
= le64toh(f
->header
->entry_array_offset
);
445 r
= journal_file_move_to_object(f
, OBJECT_ENTRY_ARRAY
, a
, &o
);
449 m
= journal_file_entry_array_n_items(o
);
452 if (entry_p
<= le64toh(o
->entry_array
.items
[u
-1])) {
461 if (le64toh(o
->entry_array
.items
[z
]) == entry_p
)
467 if (entry_p
< le64toh(o
->entry_array
.items
[z
]))
473 error(entry_p
, "Entry object doesn't exist in main entry array");
478 a
= le64toh(o
->entry_array
.next_entry_array_offset
);
484 static int verify_data(
486 Object
*o
, uint64_t p
,
487 MMapFileDescriptor
*cache_entry_fd
, uint64_t n_entries
,
488 MMapFileDescriptor
*cache_entry_array_fd
, uint64_t n_entry_arrays
) {
490 uint64_t i
, n
, a
, last
, q
;
495 assert(cache_entry_fd
);
496 assert(cache_entry_array_fd
);
498 n
= le64toh(o
->data
.n_entries
);
499 a
= le64toh(o
->data
.entry_array_offset
);
501 /* Entry array means at least two objects */
503 error(p
, "Entry array present (entry_array_offset="OFSfmt
", but n_entries=%"PRIu64
")", a
, n
);
510 /* We already checked that earlier */
511 assert(o
->data
.entry_offset
);
513 last
= q
= le64toh(o
->data
.entry_offset
);
514 r
= entry_points_to_data(f
, cache_entry_fd
, n_entries
, q
, p
);
523 error(p
, "Array chain too short");
527 if (!contains_uint64(f
->mmap
, cache_entry_array_fd
, n_entry_arrays
, a
)) {
528 error(p
, "Invalid array offset "OFSfmt
, a
);
532 r
= journal_file_move_to_object(f
, OBJECT_ENTRY_ARRAY
, a
, &o
);
536 next
= le64toh(o
->entry_array
.next_entry_array_offset
);
537 if (next
!= 0 && next
<= a
) {
538 error(p
, "Array chain has cycle (jumps back from "OFSfmt
" to "OFSfmt
")", a
, next
);
542 m
= journal_file_entry_array_n_items(o
);
543 for (j
= 0; i
< n
&& j
< m
; i
++, j
++) {
545 q
= le64toh(o
->entry_array
.items
[j
]);
547 error(p
, "Data object's entry array not sorted");
552 r
= entry_points_to_data(f
, cache_entry_fd
, n_entries
, q
, p
);
556 /* Pointer might have moved, reposition */
557 r
= journal_file_move_to_object(f
, OBJECT_ENTRY_ARRAY
, a
, &o
);
568 static int verify_hash_table(
570 MMapFileDescriptor
*cache_data_fd
, uint64_t n_data
,
571 MMapFileDescriptor
*cache_entry_fd
, uint64_t n_entries
,
572 MMapFileDescriptor
*cache_entry_array_fd
, uint64_t n_entry_arrays
,
574 bool show_progress
) {
580 assert(cache_data_fd
);
581 assert(cache_entry_fd
);
582 assert(cache_entry_array_fd
);
585 n
= le64toh(f
->header
->data_hash_table_size
) / sizeof(HashItem
);
589 r
= journal_file_map_data_hash_table(f
);
591 return log_error_errno(r
, "Failed to map data hash table: %m");
593 for (i
= 0; i
< n
; i
++) {
594 uint64_t last
= 0, p
;
597 draw_progress(0xC000 + scale_progress(0x3FFF, i
, n
), last_usec
);
599 p
= le64toh(f
->data_hash_table
[i
].head_hash_offset
);
604 if (!contains_uint64(f
->mmap
, cache_data_fd
, n_data
, p
)) {
605 error(p
, "Invalid data object at hash entry %"PRIu64
" of %"PRIu64
, i
, n
);
609 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
613 next
= le64toh(o
->data
.next_hash_offset
);
614 if (next
!= 0 && next
<= p
) {
615 error(p
, "Hash chain has a cycle in hash entry %"PRIu64
" of %"PRIu64
, i
, n
);
619 if (le64toh(o
->data
.hash
) % n
!= i
) {
620 error(p
, "Hash value mismatch in hash entry %"PRIu64
" of %"PRIu64
, i
, n
);
624 r
= verify_data(f
, o
, p
, cache_entry_fd
, n_entries
, cache_entry_array_fd
, n_entry_arrays
);
632 if (last
!= le64toh(f
->data_hash_table
[i
].tail_hash_offset
)) {
633 error(p
, "Tail hash pointer mismatch in hash table");
641 static int data_object_in_hash_table(JournalFile
*f
, uint64_t hash
, uint64_t p
) {
646 n
= le64toh(f
->header
->data_hash_table_size
) / sizeof(HashItem
);
650 r
= journal_file_map_data_hash_table(f
);
652 return log_error_errno(r
, "Failed to map data hash table: %m");
656 q
= le64toh(f
->data_hash_table
[h
].head_hash_offset
);
663 r
= journal_file_move_to_object(f
, OBJECT_DATA
, q
, &o
);
667 q
= le64toh(o
->data
.next_hash_offset
);
673 static int verify_entry(
675 Object
*o
, uint64_t p
,
676 MMapFileDescriptor
*cache_data_fd
, uint64_t n_data
) {
683 assert(cache_data_fd
);
685 n
= journal_file_entry_n_items(o
);
686 for (i
= 0; i
< n
; i
++) {
690 q
= le64toh(o
->entry
.items
[i
].object_offset
);
691 h
= le64toh(o
->entry
.items
[i
].hash
);
693 if (!contains_uint64(f
->mmap
, cache_data_fd
, n_data
, q
)) {
694 error(p
, "Invalid data object of entry");
698 r
= journal_file_move_to_object(f
, OBJECT_DATA
, q
, &u
);
702 if (le64toh(u
->data
.hash
) != h
) {
703 error(p
, "Hash mismatch for data object of entry");
707 r
= data_object_in_hash_table(f
, h
, q
);
711 error(p
, "Data object missing from hash table");
719 static int verify_entry_array(
721 MMapFileDescriptor
*cache_data_fd
, uint64_t n_data
,
722 MMapFileDescriptor
*cache_entry_fd
, uint64_t n_entries
,
723 MMapFileDescriptor
*cache_entry_array_fd
, uint64_t n_entry_arrays
,
725 bool show_progress
) {
727 uint64_t i
= 0, a
, n
, last
= 0;
731 assert(cache_data_fd
);
732 assert(cache_entry_fd
);
733 assert(cache_entry_array_fd
);
736 n
= le64toh(f
->header
->n_entries
);
737 a
= le64toh(f
->header
->entry_array_offset
);
743 draw_progress(0x8000 + scale_progress(0x3FFF, i
, n
), last_usec
);
746 error(a
, "Array chain too short at %"PRIu64
" of %"PRIu64
, i
, n
);
750 if (!contains_uint64(f
->mmap
, cache_entry_array_fd
, n_entry_arrays
, a
)) {
751 error(a
, "Invalid array %"PRIu64
" of %"PRIu64
, i
, n
);
755 r
= journal_file_move_to_object(f
, OBJECT_ENTRY_ARRAY
, a
, &o
);
759 next
= le64toh(o
->entry_array
.next_entry_array_offset
);
760 if (next
!= 0 && next
<= a
) {
761 error(a
, "Array chain has cycle at %"PRIu64
" of %"PRIu64
" (jumps back from to "OFSfmt
")", i
, n
, next
);
765 m
= journal_file_entry_array_n_items(o
);
766 for (j
= 0; i
< n
&& j
< m
; i
++, j
++) {
769 p
= le64toh(o
->entry_array
.items
[j
]);
771 error(a
, "Entry array not sorted at %"PRIu64
" of %"PRIu64
, i
, n
);
776 if (!contains_uint64(f
->mmap
, cache_entry_fd
, n_entries
, p
)) {
777 error(a
, "Invalid array entry at %"PRIu64
" of %"PRIu64
, i
, n
);
781 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, p
, &o
);
785 r
= verify_entry(f
, o
, p
, cache_data_fd
, n_data
);
789 /* Pointer might have moved, reposition */
790 r
= journal_file_move_to_object(f
, OBJECT_ENTRY_ARRAY
, a
, &o
);
801 int journal_file_verify(
804 usec_t
*first_contained
, usec_t
*last_validated
, usec_t
*last_contained
,
805 bool show_progress
) {
808 uint64_t p
= 0, last_epoch
= 0, last_tag_realtime
= 0, last_sealed_realtime
= 0;
810 uint64_t entry_seqnum
= 0, entry_monotonic
= 0, entry_realtime
= 0;
811 sd_id128_t entry_boot_id
;
812 bool entry_seqnum_set
= false, entry_monotonic_set
= false, entry_realtime_set
= false, found_main_entry_array
= false;
813 uint64_t n_weird
= 0, n_objects
= 0, n_entries
= 0, n_data
= 0, n_fields
= 0, n_data_hash_tables
= 0, n_field_hash_tables
= 0, n_entry_arrays
= 0, n_tags
= 0;
814 usec_t last_usec
= 0;
815 int data_fd
= -1, entry_fd
= -1, entry_array_fd
= -1;
816 MMapFileDescriptor
*cache_data_fd
= NULL
, *cache_entry_fd
= NULL
, *cache_entry_array_fd
= NULL
;
818 bool found_last
= false;
819 const char *tmp_dir
= NULL
;
822 uint64_t last_tag
= 0;
828 r
= journal_file_parse_verification_key(f
, key
);
830 log_error("Failed to parse seed.");
839 r
= var_tmp_dir(&tmp_dir
);
841 log_error_errno(r
, "Failed to determine temporary directory: %m");
845 data_fd
= open_tmpfile_unlinkable(tmp_dir
, O_RDWR
| O_CLOEXEC
);
847 r
= log_error_errno(data_fd
, "Failed to create data file: %m");
851 entry_fd
= open_tmpfile_unlinkable(tmp_dir
, O_RDWR
| O_CLOEXEC
);
853 r
= log_error_errno(entry_fd
, "Failed to create entry file: %m");
857 entry_array_fd
= open_tmpfile_unlinkable(tmp_dir
, O_RDWR
| O_CLOEXEC
);
858 if (entry_array_fd
< 0) {
859 r
= log_error_errno(entry_array_fd
,
860 "Failed to create entry array file: %m");
864 cache_data_fd
= mmap_cache_add_fd(f
->mmap
, data_fd
);
865 if (!cache_data_fd
) {
870 cache_entry_fd
= mmap_cache_add_fd(f
->mmap
, entry_fd
);
871 if (!cache_entry_fd
) {
876 cache_entry_array_fd
= mmap_cache_add_fd(f
->mmap
, entry_array_fd
);
877 if (!cache_entry_array_fd
) {
882 if (le32toh(f
->header
->compatible_flags
) & ~HEADER_COMPATIBLE_SUPPORTED
) {
883 log_error("Cannot verify file with unknown extensions.");
888 for (i
= 0; i
< sizeof(f
->header
->reserved
); i
++)
889 if (f
->header
->reserved
[i
] != 0) {
890 error(offsetof(Header
, reserved
[i
]), "Reserved field is non-zero");
895 /* First iteration: we go through all objects, verify the
896 * superficial structure, headers, hashes. */
898 p
= le64toh(f
->header
->header_size
);
900 /* Early exit if there are no objects in the file, at all */
901 if (le64toh(f
->header
->tail_object_offset
) == 0)
905 draw_progress(scale_progress(0x7FFF, p
, le64toh(f
->header
->tail_object_offset
)), &last_usec
);
907 r
= journal_file_move_to_object(f
, OBJECT_UNUSED
, p
, &o
);
909 error(p
, "Invalid object");
913 if (p
> le64toh(f
->header
->tail_object_offset
)) {
914 error(offsetof(Header
, tail_object_offset
), "Invalid tail object pointer");
921 r
= journal_file_object_verify(f
, p
, o
);
923 error_errno(p
, r
, "Invalid object contents: %m");
927 if ((o
->object
.flags
& OBJECT_COMPRESSED_XZ
) &&
928 (o
->object
.flags
& OBJECT_COMPRESSED_LZ4
)) {
929 error(p
, "Objected with double compression");
934 if ((o
->object
.flags
& OBJECT_COMPRESSED_XZ
) && !JOURNAL_HEADER_COMPRESSED_XZ(f
->header
)) {
935 error(p
, "XZ compressed object in file without XZ compression");
940 if ((o
->object
.flags
& OBJECT_COMPRESSED_LZ4
) && !JOURNAL_HEADER_COMPRESSED_LZ4(f
->header
)) {
941 error(p
, "LZ4 compressed object in file without LZ4 compression");
946 switch (o
->object
.type
) {
949 r
= write_uint64(data_fd
, p
);
961 if (JOURNAL_HEADER_SEALED(f
->header
) && n_tags
<= 0) {
962 error(p
, "First entry before first tag");
967 r
= write_uint64(entry_fd
, p
);
971 if (le64toh(o
->entry
.realtime
) < last_tag_realtime
) {
972 error(p
, "Older entry after newer tag");
977 if (!entry_seqnum_set
&&
978 le64toh(o
->entry
.seqnum
) != le64toh(f
->header
->head_entry_seqnum
)) {
979 error(p
, "Head entry sequence number incorrect");
984 if (entry_seqnum_set
&&
985 entry_seqnum
>= le64toh(o
->entry
.seqnum
)) {
986 error(p
, "Entry sequence number out of synchronization");
991 entry_seqnum
= le64toh(o
->entry
.seqnum
);
992 entry_seqnum_set
= true;
994 if (entry_monotonic_set
&&
995 sd_id128_equal(entry_boot_id
, o
->entry
.boot_id
) &&
996 entry_monotonic
> le64toh(o
->entry
.monotonic
)) {
997 error(p
, "Entry timestamp out of synchronization");
1002 entry_monotonic
= le64toh(o
->entry
.monotonic
);
1003 entry_boot_id
= o
->entry
.boot_id
;
1004 entry_monotonic_set
= true;
1006 if (!entry_realtime_set
&&
1007 le64toh(o
->entry
.realtime
) != le64toh(f
->header
->head_entry_realtime
)) {
1008 error(p
, "Head entry realtime timestamp incorrect");
1013 entry_realtime
= le64toh(o
->entry
.realtime
);
1014 entry_realtime_set
= true;
1019 case OBJECT_DATA_HASH_TABLE
:
1020 if (n_data_hash_tables
> 1) {
1021 error(p
, "More than one data hash table");
1026 if (le64toh(f
->header
->data_hash_table_offset
) != p
+ offsetof(HashTableObject
, items
) ||
1027 le64toh(f
->header
->data_hash_table_size
) != le64toh(o
->object
.size
) - offsetof(HashTableObject
, items
)) {
1028 error(p
, "header fields for data hash table invalid");
1033 n_data_hash_tables
++;
1036 case OBJECT_FIELD_HASH_TABLE
:
1037 if (n_field_hash_tables
> 1) {
1038 error(p
, "More than one field hash table");
1043 if (le64toh(f
->header
->field_hash_table_offset
) != p
+ offsetof(HashTableObject
, items
) ||
1044 le64toh(f
->header
->field_hash_table_size
) != le64toh(o
->object
.size
) - offsetof(HashTableObject
, items
)) {
1045 error(p
, "Header fields for field hash table invalid");
1050 n_field_hash_tables
++;
1053 case OBJECT_ENTRY_ARRAY
:
1054 r
= write_uint64(entry_array_fd
, p
);
1058 if (p
== le64toh(f
->header
->entry_array_offset
)) {
1059 if (found_main_entry_array
) {
1060 error(p
, "More than one main entry array");
1065 found_main_entry_array
= true;
1072 if (!JOURNAL_HEADER_SEALED(f
->header
)) {
1073 error(p
, "Tag object in file without sealing");
1078 if (le64toh(o
->tag
.seqnum
) != n_tags
+ 1) {
1079 error(p
, "Tag sequence number out of synchronization");
1084 if (le64toh(o
->tag
.epoch
) < last_epoch
) {
1085 error(p
, "Epoch sequence out of synchronization");
1094 debug(p
, "Checking tag %"PRIu64
"...", le64toh(o
->tag
.seqnum
));
1096 rt
= f
->fss_start_usec
+ le64toh(o
->tag
.epoch
) * f
->fss_interval_usec
;
1097 if (entry_realtime_set
&& entry_realtime
>= rt
+ f
->fss_interval_usec
) {
1098 error(p
, "tag/entry realtime timestamp out of synchronization");
1103 /* OK, now we know the epoch. So let's now set
1104 * it, and calculate the HMAC for everything
1105 * since the last tag. */
1106 r
= journal_file_fsprg_seek(f
, le64toh(o
->tag
.epoch
));
1110 r
= journal_file_hmac_start(f
);
1114 if (last_tag
== 0) {
1115 r
= journal_file_hmac_put_header(f
);
1119 q
= le64toh(f
->header
->header_size
);
1124 r
= journal_file_move_to_object(f
, OBJECT_UNUSED
, q
, &o
);
1128 r
= journal_file_hmac_put_object(f
, OBJECT_UNUSED
, o
, q
);
1132 q
= q
+ ALIGN64(le64toh(o
->object
.size
));
1135 /* Position might have changed, let's reposition things */
1136 r
= journal_file_move_to_object(f
, OBJECT_UNUSED
, p
, &o
);
1140 if (memcmp(o
->tag
.tag
, gcry_md_read(f
->hmac
, 0), TAG_LENGTH
) != 0) {
1141 error(p
, "Tag failed verification");
1146 f
->hmac_running
= false;
1147 last_tag_realtime
= rt
;
1148 last_sealed_realtime
= entry_realtime
;
1151 last_tag
= p
+ ALIGN64(le64toh(o
->object
.size
));
1154 last_epoch
= le64toh(o
->tag
.epoch
);
1163 if (p
== le64toh(f
->header
->tail_object_offset
)) {
1168 p
= p
+ ALIGN64(le64toh(o
->object
.size
));
1171 if (!found_last
&& le64toh(f
->header
->tail_object_offset
) != 0) {
1172 error(le64toh(f
->header
->tail_object_offset
), "Tail object pointer dead");
1177 if (n_objects
!= le64toh(f
->header
->n_objects
)) {
1178 error(offsetof(Header
, n_objects
), "Object number mismatch");
1183 if (n_entries
!= le64toh(f
->header
->n_entries
)) {
1184 error(offsetof(Header
, n_entries
), "Entry number mismatch");
1189 if (JOURNAL_HEADER_CONTAINS(f
->header
, n_data
) &&
1190 n_data
!= le64toh(f
->header
->n_data
)) {
1191 error(offsetof(Header
, n_data
), "Data number mismatch");
1196 if (JOURNAL_HEADER_CONTAINS(f
->header
, n_fields
) &&
1197 n_fields
!= le64toh(f
->header
->n_fields
)) {
1198 error(offsetof(Header
, n_fields
), "Field number mismatch");
1203 if (JOURNAL_HEADER_CONTAINS(f
->header
, n_tags
) &&
1204 n_tags
!= le64toh(f
->header
->n_tags
)) {
1205 error(offsetof(Header
, n_tags
), "Tag number mismatch");
1210 if (JOURNAL_HEADER_CONTAINS(f
->header
, n_entry_arrays
) &&
1211 n_entry_arrays
!= le64toh(f
->header
->n_entry_arrays
)) {
1212 error(offsetof(Header
, n_entry_arrays
), "Entry array number mismatch");
1217 if (!found_main_entry_array
&& le64toh(f
->header
->entry_array_offset
) != 0) {
1218 error(0, "Missing entry array");
1223 if (entry_seqnum_set
&&
1224 entry_seqnum
!= le64toh(f
->header
->tail_entry_seqnum
)) {
1225 error(offsetof(Header
, tail_entry_seqnum
), "Invalid tail seqnum");
1230 if (entry_monotonic_set
&&
1231 (sd_id128_equal(entry_boot_id
, f
->header
->boot_id
) &&
1232 entry_monotonic
!= le64toh(f
->header
->tail_entry_monotonic
))) {
1233 error(0, "Invalid tail monotonic timestamp");
1238 if (entry_realtime_set
&& entry_realtime
!= le64toh(f
->header
->tail_entry_realtime
)) {
1239 error(0, "Invalid tail realtime timestamp");
1244 /* Second iteration: we follow all objects referenced from the
1245 * two entry points: the object hash table and the entry
1246 * array. We also check that everything referenced (directly
1247 * or indirectly) in the data hash table also exists in the
1248 * entry array, and vice versa. Note that we do not care for
1249 * unreferenced objects. We only care that everything that is
1250 * referenced is consistent. */
1252 r
= verify_entry_array(f
,
1253 cache_data_fd
, n_data
,
1254 cache_entry_fd
, n_entries
,
1255 cache_entry_array_fd
, n_entry_arrays
,
1261 r
= verify_hash_table(f
,
1262 cache_data_fd
, n_data
,
1263 cache_entry_fd
, n_entries
,
1264 cache_entry_array_fd
, n_entry_arrays
,
1273 mmap_cache_free_fd(f
->mmap
, cache_data_fd
);
1274 mmap_cache_free_fd(f
->mmap
, cache_entry_fd
);
1275 mmap_cache_free_fd(f
->mmap
, cache_entry_array_fd
);
1277 safe_close(data_fd
);
1278 safe_close(entry_fd
);
1279 safe_close(entry_array_fd
);
1281 if (first_contained
)
1282 *first_contained
= le64toh(f
->header
->head_entry_realtime
);
1284 *last_validated
= last_sealed_realtime
;
1286 *last_contained
= le64toh(f
->header
->tail_entry_realtime
);
1294 log_error("File corruption detected at %s:"OFSfmt
" (of %llu bytes, %"PRIu64
"%%).",
1297 (unsigned long long) f
->last_stat
.st_size
,
1298 100 * p
/ f
->last_stat
.st_size
);
1301 safe_close(data_fd
);
1304 safe_close(entry_fd
);
1306 if (entry_array_fd
>= 0)
1307 safe_close(entry_array_fd
);
1310 mmap_cache_free_fd(f
->mmap
, cache_data_fd
);
1313 mmap_cache_free_fd(f
->mmap
, cache_entry_fd
);
1315 if (cache_entry_array_fd
)
1316 mmap_cache_free_fd(f
->mmap
, cache_entry_array_fd
);