2 This file is part of systemd.
4 Copyright 2012 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include "alloc-util.h"
30 #include "journal-authenticate.h"
31 #include "journal-def.h"
32 #include "journal-file.h"
33 #include "journal-verify.h"
36 #include "terminal-util.h"
39 static void draw_progress(uint64_t p
, usec_t
*last_usec
) {
46 z
= now(CLOCK_MONOTONIC
);
49 if (x
!= 0 && x
+ 40 * USEC_PER_MSEC
> z
)
54 n
= (3 * columns()) / 4;
55 j
= (n
* (unsigned) p
) / 65535ULL;
60 fputs("\x1B[?25l" ANSI_HIGHLIGHT_GREEN
, stdout
);
62 for (i
= 0; i
< j
; i
++)
63 fputs("\xe2\x96\x88", stdout
);
65 fputs(ANSI_NORMAL
, stdout
);
67 for (i
= 0; i
< k
; i
++)
68 fputs("\xe2\x96\x91", stdout
);
70 printf(" %3"PRIu64
"%%", 100U * p
/ 65535U);
74 fputs("\x1B[?25h", stdout
);
79 static uint64_t scale_progress(uint64_t scale
, uint64_t p
, uint64_t m
) {
81 /* Calculates scale * p / m, but handles m == 0 safely, and saturates */
89 static void flush_progress(void) {
95 n
= (3 * columns()) / 4;
99 for (i
= 0; i
< n
+ 5; i
++)
106 #define debug(_offset, _fmt, ...) do { \
108 log_debug(OFSfmt": " _fmt, _offset, ##__VA_ARGS__); \
111 #define warning(_offset, _fmt, ...) do { \
113 log_warning(OFSfmt": " _fmt, _offset, ##__VA_ARGS__); \
116 #define error(_offset, _fmt, ...) do { \
118 log_error(OFSfmt": " _fmt, (uint64_t)_offset, ##__VA_ARGS__); \
121 #define error_errno(_offset, error, _fmt, ...) do { \
123 log_error_errno(error, OFSfmt": " _fmt, (uint64_t)_offset, ##__VA_ARGS__); \
126 static int journal_file_object_verify(JournalFile
*f
, uint64_t offset
, Object
*o
) {
133 /* This does various superficial tests about the length an
134 * possible field values. It does not follow any references to
137 if ((o
->object
.flags
& OBJECT_COMPRESSED_XZ
) &&
138 o
->object
.type
!= OBJECT_DATA
) {
139 error(offset
, "Found compressed object that isn't of type DATA, which is not allowed.");
143 switch (o
->object
.type
) {
149 if (le64toh(o
->data
.entry_offset
) == 0)
150 warning(offset
, "Unused data (entry_offset==0)");
152 if ((le64toh(o
->data
.entry_offset
) == 0) ^ (le64toh(o
->data
.n_entries
) == 0)) {
153 error(offset
, "Bad n_entries: %"PRIu64
, le64toh(o
->data
.n_entries
));
157 if (le64toh(o
->object
.size
) - offsetof(DataObject
, payload
) <= 0) {
158 error(offset
, "Bad object size (<= %zu): %"PRIu64
,
159 offsetof(DataObject
, payload
),
160 le64toh(o
->object
.size
));
164 h1
= le64toh(o
->data
.hash
);
166 compression
= o
->object
.flags
& OBJECT_COMPRESSION_MASK
;
168 _cleanup_free_
void *b
= NULL
;
169 size_t alloc
= 0, b_size
;
171 r
= decompress_blob(compression
,
173 le64toh(o
->object
.size
) - offsetof(Object
, data
.payload
),
174 &b
, &alloc
, &b_size
, 0);
176 error_errno(offset
, r
, "%s decompression failed: %m",
177 object_compressed_to_string(compression
));
181 h2
= hash64(b
, b_size
);
183 h2
= hash64(o
->data
.payload
, le64toh(o
->object
.size
) - offsetof(Object
, data
.payload
));
186 error(offset
, "Invalid hash (%08"PRIx64
" vs. %08"PRIx64
, h1
, h2
);
190 if (!VALID64(le64toh(o
->data
.next_hash_offset
)) ||
191 !VALID64(le64toh(o
->data
.next_field_offset
)) ||
192 !VALID64(le64toh(o
->data
.entry_offset
)) ||
193 !VALID64(le64toh(o
->data
.entry_array_offset
))) {
194 error(offset
, "Invalid offset (next_hash_offset="OFSfmt
", next_field_offset="OFSfmt
", entry_offset="OFSfmt
", entry_array_offset="OFSfmt
,
195 le64toh(o
->data
.next_hash_offset
),
196 le64toh(o
->data
.next_field_offset
),
197 le64toh(o
->data
.entry_offset
),
198 le64toh(o
->data
.entry_array_offset
));
206 if (le64toh(o
->object
.size
) - offsetof(FieldObject
, payload
) <= 0) {
208 "Bad field size (<= %zu): %"PRIu64
,
209 offsetof(FieldObject
, payload
),
210 le64toh(o
->object
.size
));
214 if (!VALID64(le64toh(o
->field
.next_hash_offset
)) ||
215 !VALID64(le64toh(o
->field
.head_data_offset
))) {
217 "Invalid offset (next_hash_offset="OFSfmt
", head_data_offset="OFSfmt
,
218 le64toh(o
->field
.next_hash_offset
),
219 le64toh(o
->field
.head_data_offset
));
225 if ((le64toh(o
->object
.size
) - offsetof(EntryObject
, items
)) % sizeof(EntryItem
) != 0) {
227 "Bad entry size (<= %zu): %"PRIu64
,
228 offsetof(EntryObject
, items
),
229 le64toh(o
->object
.size
));
233 if ((le64toh(o
->object
.size
) - offsetof(EntryObject
, items
)) / sizeof(EntryItem
) <= 0) {
235 "Invalid number items in entry: %"PRIu64
,
236 (le64toh(o
->object
.size
) - offsetof(EntryObject
, items
)) / sizeof(EntryItem
));
240 if (le64toh(o
->entry
.seqnum
) <= 0) {
242 "Invalid entry seqnum: %"PRIx64
,
243 le64toh(o
->entry
.seqnum
));
247 if (!VALID_REALTIME(le64toh(o
->entry
.realtime
))) {
249 "Invalid entry realtime timestamp: %"PRIu64
,
250 le64toh(o
->entry
.realtime
));
254 if (!VALID_MONOTONIC(le64toh(o
->entry
.monotonic
))) {
256 "Invalid entry monotonic timestamp: %"PRIu64
,
257 le64toh(o
->entry
.monotonic
));
261 for (i
= 0; i
< journal_file_entry_n_items(o
); i
++) {
262 if (le64toh(o
->entry
.items
[i
].object_offset
) == 0 ||
263 !VALID64(le64toh(o
->entry
.items
[i
].object_offset
))) {
265 "Invalid entry item (%"PRIu64
"/%"PRIu64
" offset: "OFSfmt
,
266 i
, journal_file_entry_n_items(o
),
267 le64toh(o
->entry
.items
[i
].object_offset
));
274 case OBJECT_DATA_HASH_TABLE
:
275 case OBJECT_FIELD_HASH_TABLE
:
276 if ((le64toh(o
->object
.size
) - offsetof(HashTableObject
, items
)) % sizeof(HashItem
) != 0 ||
277 (le64toh(o
->object
.size
) - offsetof(HashTableObject
, items
)) / sizeof(HashItem
) <= 0) {
279 "Invalid %s hash table size: %"PRIu64
,
280 o
->object
.type
== OBJECT_DATA_HASH_TABLE
? "data" : "field",
281 le64toh(o
->object
.size
));
285 for (i
= 0; i
< journal_file_hash_table_n_items(o
); i
++) {
286 if (o
->hash_table
.items
[i
].head_hash_offset
!= 0 &&
287 !VALID64(le64toh(o
->hash_table
.items
[i
].head_hash_offset
))) {
289 "Invalid %s hash table item (%"PRIu64
"/%"PRIu64
") head_hash_offset: "OFSfmt
,
290 o
->object
.type
== OBJECT_DATA_HASH_TABLE
? "data" : "field",
291 i
, journal_file_hash_table_n_items(o
),
292 le64toh(o
->hash_table
.items
[i
].head_hash_offset
));
295 if (o
->hash_table
.items
[i
].tail_hash_offset
!= 0 &&
296 !VALID64(le64toh(o
->hash_table
.items
[i
].tail_hash_offset
))) {
298 "Invalid %s hash table item (%"PRIu64
"/%"PRIu64
") tail_hash_offset: "OFSfmt
,
299 o
->object
.type
== OBJECT_DATA_HASH_TABLE
? "data" : "field",
300 i
, journal_file_hash_table_n_items(o
),
301 le64toh(o
->hash_table
.items
[i
].tail_hash_offset
));
305 if ((o
->hash_table
.items
[i
].head_hash_offset
!= 0) !=
306 (o
->hash_table
.items
[i
].tail_hash_offset
!= 0)) {
308 "Invalid %s hash table item (%"PRIu64
"/%"PRIu64
"): head_hash_offset="OFSfmt
" tail_hash_offset="OFSfmt
,
309 o
->object
.type
== OBJECT_DATA_HASH_TABLE
? "data" : "field",
310 i
, journal_file_hash_table_n_items(o
),
311 le64toh(o
->hash_table
.items
[i
].head_hash_offset
),
312 le64toh(o
->hash_table
.items
[i
].tail_hash_offset
));
319 case OBJECT_ENTRY_ARRAY
:
320 if ((le64toh(o
->object
.size
) - offsetof(EntryArrayObject
, items
)) % sizeof(le64_t
) != 0 ||
321 (le64toh(o
->object
.size
) - offsetof(EntryArrayObject
, items
)) / sizeof(le64_t
) <= 0) {
323 "Invalid object entry array size: %"PRIu64
,
324 le64toh(o
->object
.size
));
328 if (!VALID64(le64toh(o
->entry_array
.next_entry_array_offset
))) {
330 "Invalid object entry array next_entry_array_offset: "OFSfmt
,
331 le64toh(o
->entry_array
.next_entry_array_offset
));
335 for (i
= 0; i
< journal_file_entry_array_n_items(o
); i
++)
336 if (le64toh(o
->entry_array
.items
[i
]) != 0 &&
337 !VALID64(le64toh(o
->entry_array
.items
[i
]))) {
339 "Invalid object entry array item (%"PRIu64
"/%"PRIu64
"): "OFSfmt
,
340 i
, journal_file_entry_array_n_items(o
),
341 le64toh(o
->entry_array
.items
[i
]));
348 if (le64toh(o
->object
.size
) != sizeof(TagObject
)) {
350 "Invalid object tag size: %"PRIu64
,
351 le64toh(o
->object
.size
));
355 if (!VALID_EPOCH(le64toh(o
->tag
.epoch
))) {
357 "Invalid object tag epoch: %"PRIu64
,
358 le64toh(o
->tag
.epoch
));
368 static int write_uint64(int fd
, uint64_t p
) {
371 k
= write(fd
, &p
, sizeof(p
));
380 static int contains_uint64(MMapCache
*m
, MMapFileDescriptor
*f
, uint64_t n
, uint64_t p
) {
395 r
= mmap_cache_get(m
, f
, PROT_READ
|PROT_WRITE
, 0, false, c
* sizeof(uint64_t), sizeof(uint64_t), NULL
, (void **) &z
, NULL
);
414 static int entry_points_to_data(
416 MMapFileDescriptor
*cache_entry_fd
,
427 assert(cache_entry_fd
);
429 if (!contains_uint64(f
->mmap
, cache_entry_fd
, n_entries
, entry_p
)) {
430 error(data_p
, "Data object references invalid entry at "OFSfmt
, entry_p
);
434 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, entry_p
, &o
);
438 n
= journal_file_entry_n_items(o
);
439 for (i
= 0; i
< n
; i
++)
440 if (le64toh(o
->entry
.items
[i
].object_offset
) == data_p
) {
446 error(entry_p
, "Data object at "OFSfmt
" not referenced by linked entry", data_p
);
450 /* Check if this entry is also in main entry array. Since the
451 * main entry array has already been verified we can rely on
452 * its consistency. */
455 n
= le64toh(f
->header
->n_entries
);
456 a
= le64toh(f
->header
->entry_array_offset
);
461 r
= journal_file_move_to_object(f
, OBJECT_ENTRY_ARRAY
, a
, &o
);
465 m
= journal_file_entry_array_n_items(o
);
468 if (entry_p
<= le64toh(o
->entry_array
.items
[u
-1])) {
477 if (le64toh(o
->entry_array
.items
[z
]) == entry_p
)
483 if (entry_p
< le64toh(o
->entry_array
.items
[z
]))
489 error(entry_p
, "Entry object doesn't exist in main entry array");
494 a
= le64toh(o
->entry_array
.next_entry_array_offset
);
500 static int verify_data(
502 Object
*o
, uint64_t p
,
503 MMapFileDescriptor
*cache_entry_fd
, uint64_t n_entries
,
504 MMapFileDescriptor
*cache_entry_array_fd
, uint64_t n_entry_arrays
) {
506 uint64_t i
, n
, a
, last
, q
;
511 assert(cache_entry_fd
);
512 assert(cache_entry_array_fd
);
514 n
= le64toh(o
->data
.n_entries
);
515 a
= le64toh(o
->data
.entry_array_offset
);
517 /* Entry array means at least two objects */
519 error(p
, "Entry array present (entry_array_offset="OFSfmt
", but n_entries=%"PRIu64
")", a
, n
);
526 /* We already checked that earlier */
527 assert(o
->data
.entry_offset
);
529 last
= q
= le64toh(o
->data
.entry_offset
);
530 r
= entry_points_to_data(f
, cache_entry_fd
, n_entries
, q
, p
);
539 error(p
, "Array chain too short");
543 if (!contains_uint64(f
->mmap
, cache_entry_array_fd
, n_entry_arrays
, a
)) {
544 error(p
, "Invalid array offset "OFSfmt
, a
);
548 r
= journal_file_move_to_object(f
, OBJECT_ENTRY_ARRAY
, a
, &o
);
552 next
= le64toh(o
->entry_array
.next_entry_array_offset
);
553 if (next
!= 0 && next
<= a
) {
554 error(p
, "Array chain has cycle (jumps back from "OFSfmt
" to "OFSfmt
")", a
, next
);
558 m
= journal_file_entry_array_n_items(o
);
559 for (j
= 0; i
< n
&& j
< m
; i
++, j
++) {
561 q
= le64toh(o
->entry_array
.items
[j
]);
563 error(p
, "Data object's entry array not sorted");
568 r
= entry_points_to_data(f
, cache_entry_fd
, n_entries
, q
, p
);
572 /* Pointer might have moved, reposition */
573 r
= journal_file_move_to_object(f
, OBJECT_ENTRY_ARRAY
, a
, &o
);
584 static int verify_hash_table(
586 MMapFileDescriptor
*cache_data_fd
, uint64_t n_data
,
587 MMapFileDescriptor
*cache_entry_fd
, uint64_t n_entries
,
588 MMapFileDescriptor
*cache_entry_array_fd
, uint64_t n_entry_arrays
,
590 bool show_progress
) {
596 assert(cache_data_fd
);
597 assert(cache_entry_fd
);
598 assert(cache_entry_array_fd
);
601 n
= le64toh(f
->header
->data_hash_table_size
) / sizeof(HashItem
);
605 r
= journal_file_map_data_hash_table(f
);
607 return log_error_errno(r
, "Failed to map data hash table: %m");
609 for (i
= 0; i
< n
; i
++) {
610 uint64_t last
= 0, p
;
613 draw_progress(0xC000 + scale_progress(0x3FFF, i
, n
), last_usec
);
615 p
= le64toh(f
->data_hash_table
[i
].head_hash_offset
);
620 if (!contains_uint64(f
->mmap
, cache_data_fd
, n_data
, p
)) {
621 error(p
, "Invalid data object at hash entry %"PRIu64
" of %"PRIu64
, i
, n
);
625 r
= journal_file_move_to_object(f
, OBJECT_DATA
, p
, &o
);
629 next
= le64toh(o
->data
.next_hash_offset
);
630 if (next
!= 0 && next
<= p
) {
631 error(p
, "Hash chain has a cycle in hash entry %"PRIu64
" of %"PRIu64
, i
, n
);
635 if (le64toh(o
->data
.hash
) % n
!= i
) {
636 error(p
, "Hash value mismatch in hash entry %"PRIu64
" of %"PRIu64
, i
, n
);
640 r
= verify_data(f
, o
, p
, cache_entry_fd
, n_entries
, cache_entry_array_fd
, n_entry_arrays
);
648 if (last
!= le64toh(f
->data_hash_table
[i
].tail_hash_offset
)) {
649 error(p
, "Tail hash pointer mismatch in hash table");
657 static int data_object_in_hash_table(JournalFile
*f
, uint64_t hash
, uint64_t p
) {
662 n
= le64toh(f
->header
->data_hash_table_size
) / sizeof(HashItem
);
666 r
= journal_file_map_data_hash_table(f
);
668 return log_error_errno(r
, "Failed to map data hash table: %m");
672 q
= le64toh(f
->data_hash_table
[h
].head_hash_offset
);
679 r
= journal_file_move_to_object(f
, OBJECT_DATA
, q
, &o
);
683 q
= le64toh(o
->data
.next_hash_offset
);
689 static int verify_entry(
691 Object
*o
, uint64_t p
,
692 MMapFileDescriptor
*cache_data_fd
, uint64_t n_data
) {
699 assert(cache_data_fd
);
701 n
= journal_file_entry_n_items(o
);
702 for (i
= 0; i
< n
; i
++) {
706 q
= le64toh(o
->entry
.items
[i
].object_offset
);
707 h
= le64toh(o
->entry
.items
[i
].hash
);
709 if (!contains_uint64(f
->mmap
, cache_data_fd
, n_data
, q
)) {
710 error(p
, "Invalid data object of entry");
714 r
= journal_file_move_to_object(f
, OBJECT_DATA
, q
, &u
);
718 if (le64toh(u
->data
.hash
) != h
) {
719 error(p
, "Hash mismatch for data object of entry");
723 r
= data_object_in_hash_table(f
, h
, q
);
727 error(p
, "Data object missing from hash table");
735 static int verify_entry_array(
737 MMapFileDescriptor
*cache_data_fd
, uint64_t n_data
,
738 MMapFileDescriptor
*cache_entry_fd
, uint64_t n_entries
,
739 MMapFileDescriptor
*cache_entry_array_fd
, uint64_t n_entry_arrays
,
741 bool show_progress
) {
743 uint64_t i
= 0, a
, n
, last
= 0;
747 assert(cache_data_fd
);
748 assert(cache_entry_fd
);
749 assert(cache_entry_array_fd
);
752 n
= le64toh(f
->header
->n_entries
);
753 a
= le64toh(f
->header
->entry_array_offset
);
759 draw_progress(0x8000 + scale_progress(0x3FFF, i
, n
), last_usec
);
762 error(a
, "Array chain too short at %"PRIu64
" of %"PRIu64
, i
, n
);
766 if (!contains_uint64(f
->mmap
, cache_entry_array_fd
, n_entry_arrays
, a
)) {
767 error(a
, "Invalid array %"PRIu64
" of %"PRIu64
, i
, n
);
771 r
= journal_file_move_to_object(f
, OBJECT_ENTRY_ARRAY
, a
, &o
);
775 next
= le64toh(o
->entry_array
.next_entry_array_offset
);
776 if (next
!= 0 && next
<= a
) {
777 error(a
, "Array chain has cycle at %"PRIu64
" of %"PRIu64
" (jumps back from to "OFSfmt
")", i
, n
, next
);
781 m
= journal_file_entry_array_n_items(o
);
782 for (j
= 0; i
< n
&& j
< m
; i
++, j
++) {
785 p
= le64toh(o
->entry_array
.items
[j
]);
787 error(a
, "Entry array not sorted at %"PRIu64
" of %"PRIu64
, i
, n
);
792 if (!contains_uint64(f
->mmap
, cache_entry_fd
, n_entries
, p
)) {
793 error(a
, "Invalid array entry at %"PRIu64
" of %"PRIu64
, i
, n
);
797 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, p
, &o
);
801 r
= verify_entry(f
, o
, p
, cache_data_fd
, n_data
);
805 /* Pointer might have moved, reposition */
806 r
= journal_file_move_to_object(f
, OBJECT_ENTRY_ARRAY
, a
, &o
);
817 int journal_file_verify(
820 usec_t
*first_contained
, usec_t
*last_validated
, usec_t
*last_contained
,
821 bool show_progress
) {
824 uint64_t p
= 0, last_epoch
= 0, last_tag_realtime
= 0, last_sealed_realtime
= 0;
826 uint64_t entry_seqnum
= 0, entry_monotonic
= 0, entry_realtime
= 0;
827 sd_id128_t entry_boot_id
;
828 bool entry_seqnum_set
= false, entry_monotonic_set
= false, entry_realtime_set
= false, found_main_entry_array
= false;
829 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;
830 usec_t last_usec
= 0;
831 int data_fd
= -1, entry_fd
= -1, entry_array_fd
= -1;
832 MMapFileDescriptor
*cache_data_fd
= NULL
, *cache_entry_fd
= NULL
, *cache_entry_array_fd
= NULL
;
834 bool found_last
= false;
835 const char *tmp_dir
= NULL
;
838 uint64_t last_tag
= 0;
844 r
= journal_file_parse_verification_key(f
, key
);
846 log_error("Failed to parse seed.");
855 r
= var_tmp_dir(&tmp_dir
);
857 log_error_errno(r
, "Failed to determine temporary directory: %m");
861 data_fd
= open_tmpfile_unlinkable(tmp_dir
, O_RDWR
| O_CLOEXEC
);
863 r
= log_error_errno(data_fd
, "Failed to create data file: %m");
867 entry_fd
= open_tmpfile_unlinkable(tmp_dir
, O_RDWR
| O_CLOEXEC
);
869 r
= log_error_errno(entry_fd
, "Failed to create entry file: %m");
873 entry_array_fd
= open_tmpfile_unlinkable(tmp_dir
, O_RDWR
| O_CLOEXEC
);
874 if (entry_array_fd
< 0) {
875 r
= log_error_errno(entry_array_fd
,
876 "Failed to create entry array file: %m");
880 cache_data_fd
= mmap_cache_add_fd(f
->mmap
, data_fd
);
881 if (!cache_data_fd
) {
886 cache_entry_fd
= mmap_cache_add_fd(f
->mmap
, entry_fd
);
887 if (!cache_entry_fd
) {
892 cache_entry_array_fd
= mmap_cache_add_fd(f
->mmap
, entry_array_fd
);
893 if (!cache_entry_array_fd
) {
898 if (le32toh(f
->header
->compatible_flags
) & ~HEADER_COMPATIBLE_SUPPORTED
) {
899 log_error("Cannot verify file with unknown extensions.");
904 for (i
= 0; i
< sizeof(f
->header
->reserved
); i
++)
905 if (f
->header
->reserved
[i
] != 0) {
906 error(offsetof(Header
, reserved
[i
]), "Reserved field is non-zero");
911 /* First iteration: we go through all objects, verify the
912 * superficial structure, headers, hashes. */
914 p
= le64toh(f
->header
->header_size
);
916 /* Early exit if there are no objects in the file, at all */
917 if (le64toh(f
->header
->tail_object_offset
) == 0)
921 draw_progress(scale_progress(0x7FFF, p
, le64toh(f
->header
->tail_object_offset
)), &last_usec
);
923 r
= journal_file_move_to_object(f
, OBJECT_UNUSED
, p
, &o
);
925 error(p
, "Invalid object");
929 if (p
> le64toh(f
->header
->tail_object_offset
)) {
930 error(offsetof(Header
, tail_object_offset
), "Invalid tail object pointer");
937 r
= journal_file_object_verify(f
, p
, o
);
939 error_errno(p
, r
, "Invalid object contents: %m");
943 if ((o
->object
.flags
& OBJECT_COMPRESSED_XZ
) &&
944 (o
->object
.flags
& OBJECT_COMPRESSED_LZ4
)) {
945 error(p
, "Objected with double compression");
950 if ((o
->object
.flags
& OBJECT_COMPRESSED_XZ
) && !JOURNAL_HEADER_COMPRESSED_XZ(f
->header
)) {
951 error(p
, "XZ compressed object in file without XZ compression");
956 if ((o
->object
.flags
& OBJECT_COMPRESSED_LZ4
) && !JOURNAL_HEADER_COMPRESSED_LZ4(f
->header
)) {
957 error(p
, "LZ4 compressed object in file without LZ4 compression");
962 switch (o
->object
.type
) {
965 r
= write_uint64(data_fd
, p
);
977 if (JOURNAL_HEADER_SEALED(f
->header
) && n_tags
<= 0) {
978 error(p
, "First entry before first tag");
983 r
= write_uint64(entry_fd
, p
);
987 if (le64toh(o
->entry
.realtime
) < last_tag_realtime
) {
988 error(p
, "Older entry after newer tag");
993 if (!entry_seqnum_set
&&
994 le64toh(o
->entry
.seqnum
) != le64toh(f
->header
->head_entry_seqnum
)) {
995 error(p
, "Head entry sequence number incorrect");
1000 if (entry_seqnum_set
&&
1001 entry_seqnum
>= le64toh(o
->entry
.seqnum
)) {
1002 error(p
, "Entry sequence number out of synchronization");
1007 entry_seqnum
= le64toh(o
->entry
.seqnum
);
1008 entry_seqnum_set
= true;
1010 if (entry_monotonic_set
&&
1011 sd_id128_equal(entry_boot_id
, o
->entry
.boot_id
) &&
1012 entry_monotonic
> le64toh(o
->entry
.monotonic
)) {
1013 error(p
, "Entry timestamp out of synchronization");
1018 entry_monotonic
= le64toh(o
->entry
.monotonic
);
1019 entry_boot_id
= o
->entry
.boot_id
;
1020 entry_monotonic_set
= true;
1022 if (!entry_realtime_set
&&
1023 le64toh(o
->entry
.realtime
) != le64toh(f
->header
->head_entry_realtime
)) {
1024 error(p
, "Head entry realtime timestamp incorrect");
1029 entry_realtime
= le64toh(o
->entry
.realtime
);
1030 entry_realtime_set
= true;
1035 case OBJECT_DATA_HASH_TABLE
:
1036 if (n_data_hash_tables
> 1) {
1037 error(p
, "More than one data hash table");
1042 if (le64toh(f
->header
->data_hash_table_offset
) != p
+ offsetof(HashTableObject
, items
) ||
1043 le64toh(f
->header
->data_hash_table_size
) != le64toh(o
->object
.size
) - offsetof(HashTableObject
, items
)) {
1044 error(p
, "header fields for data hash table invalid");
1049 n_data_hash_tables
++;
1052 case OBJECT_FIELD_HASH_TABLE
:
1053 if (n_field_hash_tables
> 1) {
1054 error(p
, "More than one field hash table");
1059 if (le64toh(f
->header
->field_hash_table_offset
) != p
+ offsetof(HashTableObject
, items
) ||
1060 le64toh(f
->header
->field_hash_table_size
) != le64toh(o
->object
.size
) - offsetof(HashTableObject
, items
)) {
1061 error(p
, "Header fields for field hash table invalid");
1066 n_field_hash_tables
++;
1069 case OBJECT_ENTRY_ARRAY
:
1070 r
= write_uint64(entry_array_fd
, p
);
1074 if (p
== le64toh(f
->header
->entry_array_offset
)) {
1075 if (found_main_entry_array
) {
1076 error(p
, "More than one main entry array");
1081 found_main_entry_array
= true;
1088 if (!JOURNAL_HEADER_SEALED(f
->header
)) {
1089 error(p
, "Tag object in file without sealing");
1094 if (le64toh(o
->tag
.seqnum
) != n_tags
+ 1) {
1095 error(p
, "Tag sequence number out of synchronization");
1100 if (le64toh(o
->tag
.epoch
) < last_epoch
) {
1101 error(p
, "Epoch sequence out of synchronization");
1110 debug(p
, "Checking tag %"PRIu64
"...", le64toh(o
->tag
.seqnum
));
1112 rt
= f
->fss_start_usec
+ le64toh(o
->tag
.epoch
) * f
->fss_interval_usec
;
1113 if (entry_realtime_set
&& entry_realtime
>= rt
+ f
->fss_interval_usec
) {
1114 error(p
, "tag/entry realtime timestamp out of synchronization");
1119 /* OK, now we know the epoch. So let's now set
1120 * it, and calculate the HMAC for everything
1121 * since the last tag. */
1122 r
= journal_file_fsprg_seek(f
, le64toh(o
->tag
.epoch
));
1126 r
= journal_file_hmac_start(f
);
1130 if (last_tag
== 0) {
1131 r
= journal_file_hmac_put_header(f
);
1135 q
= le64toh(f
->header
->header_size
);
1140 r
= journal_file_move_to_object(f
, OBJECT_UNUSED
, q
, &o
);
1144 r
= journal_file_hmac_put_object(f
, OBJECT_UNUSED
, o
, q
);
1148 q
= q
+ ALIGN64(le64toh(o
->object
.size
));
1151 /* Position might have changed, let's reposition things */
1152 r
= journal_file_move_to_object(f
, OBJECT_UNUSED
, p
, &o
);
1156 if (memcmp(o
->tag
.tag
, gcry_md_read(f
->hmac
, 0), TAG_LENGTH
) != 0) {
1157 error(p
, "Tag failed verification");
1162 f
->hmac_running
= false;
1163 last_tag_realtime
= rt
;
1164 last_sealed_realtime
= entry_realtime
;
1167 last_tag
= p
+ ALIGN64(le64toh(o
->object
.size
));
1170 last_epoch
= le64toh(o
->tag
.epoch
);
1179 if (p
== le64toh(f
->header
->tail_object_offset
)) {
1184 p
= p
+ ALIGN64(le64toh(o
->object
.size
));
1187 if (!found_last
&& le64toh(f
->header
->tail_object_offset
) != 0) {
1188 error(le64toh(f
->header
->tail_object_offset
), "Tail object pointer dead");
1193 if (n_objects
!= le64toh(f
->header
->n_objects
)) {
1194 error(offsetof(Header
, n_objects
), "Object number mismatch");
1199 if (n_entries
!= le64toh(f
->header
->n_entries
)) {
1200 error(offsetof(Header
, n_entries
), "Entry number mismatch");
1205 if (JOURNAL_HEADER_CONTAINS(f
->header
, n_data
) &&
1206 n_data
!= le64toh(f
->header
->n_data
)) {
1207 error(offsetof(Header
, n_data
), "Data number mismatch");
1212 if (JOURNAL_HEADER_CONTAINS(f
->header
, n_fields
) &&
1213 n_fields
!= le64toh(f
->header
->n_fields
)) {
1214 error(offsetof(Header
, n_fields
), "Field number mismatch");
1219 if (JOURNAL_HEADER_CONTAINS(f
->header
, n_tags
) &&
1220 n_tags
!= le64toh(f
->header
->n_tags
)) {
1221 error(offsetof(Header
, n_tags
), "Tag number mismatch");
1226 if (JOURNAL_HEADER_CONTAINS(f
->header
, n_entry_arrays
) &&
1227 n_entry_arrays
!= le64toh(f
->header
->n_entry_arrays
)) {
1228 error(offsetof(Header
, n_entry_arrays
), "Entry array number mismatch");
1233 if (!found_main_entry_array
&& le64toh(f
->header
->entry_array_offset
) != 0) {
1234 error(0, "Missing entry array");
1239 if (entry_seqnum_set
&&
1240 entry_seqnum
!= le64toh(f
->header
->tail_entry_seqnum
)) {
1241 error(offsetof(Header
, tail_entry_seqnum
), "Invalid tail seqnum");
1246 if (entry_monotonic_set
&&
1247 (!sd_id128_equal(entry_boot_id
, f
->header
->boot_id
) ||
1248 entry_monotonic
!= le64toh(f
->header
->tail_entry_monotonic
))) {
1249 error(0, "Invalid tail monotonic timestamp");
1254 if (entry_realtime_set
&& entry_realtime
!= le64toh(f
->header
->tail_entry_realtime
)) {
1255 error(0, "Invalid tail realtime timestamp");
1260 /* Second iteration: we follow all objects referenced from the
1261 * two entry points: the object hash table and the entry
1262 * array. We also check that everything referenced (directly
1263 * or indirectly) in the data hash table also exists in the
1264 * entry array, and vice versa. Note that we do not care for
1265 * unreferenced objects. We only care that everything that is
1266 * referenced is consistent. */
1268 r
= verify_entry_array(f
,
1269 cache_data_fd
, n_data
,
1270 cache_entry_fd
, n_entries
,
1271 cache_entry_array_fd
, n_entry_arrays
,
1277 r
= verify_hash_table(f
,
1278 cache_data_fd
, n_data
,
1279 cache_entry_fd
, n_entries
,
1280 cache_entry_array_fd
, n_entry_arrays
,
1289 mmap_cache_free_fd(f
->mmap
, cache_data_fd
);
1290 mmap_cache_free_fd(f
->mmap
, cache_entry_fd
);
1291 mmap_cache_free_fd(f
->mmap
, cache_entry_array_fd
);
1293 safe_close(data_fd
);
1294 safe_close(entry_fd
);
1295 safe_close(entry_array_fd
);
1297 if (first_contained
)
1298 *first_contained
= le64toh(f
->header
->head_entry_realtime
);
1300 *last_validated
= last_sealed_realtime
;
1302 *last_contained
= le64toh(f
->header
->tail_entry_realtime
);
1310 log_error("File corruption detected at %s:"OFSfmt
" (of %llu bytes, %"PRIu64
"%%).",
1313 (unsigned long long) f
->last_stat
.st_size
,
1314 100 * p
/ f
->last_stat
.st_size
);
1317 safe_close(data_fd
);
1320 safe_close(entry_fd
);
1322 if (entry_array_fd
>= 0)
1323 safe_close(entry_array_fd
);
1326 mmap_cache_free_fd(f
->mmap
, cache_data_fd
);
1329 mmap_cache_free_fd(f
->mmap
, cache_entry_fd
);
1331 if (cache_entry_array_fd
)
1332 mmap_cache_free_fd(f
->mmap
, cache_entry_array_fd
);