]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/journal-verify.c
do not filter out deprecated USER audit messages
[thirdparty/systemd.git] / src / journal / journal-verify.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
0284adc6 2
0284adc6 3#include <fcntl.h>
feb12d3e 4#include <stddef.h>
cf0fbc49
TA
5#include <sys/mman.h>
6#include <unistd.h>
0284adc6 7
b5efdb8a 8#include "alloc-util.h"
3ffd4af2
LP
9#include "compress.h"
10#include "fd-util.h"
0d39fa9c 11#include "fileio.h"
34a8f081 12#include "fs-util.h"
3ffd4af2 13#include "journal-authenticate.h"
0284adc6
LP
14#include "journal-def.h"
15#include "journal-file.h"
0284adc6 16#include "journal-verify.h"
f59a5f6b 17#include "lookup3.h"
3ffd4af2 18#include "macro.h"
288a74cc 19#include "terminal-util.h"
3ffd4af2 20#include "util.h"
f59a5f6b 21
54f3ff07
ZJS
22static void draw_progress(uint64_t p, usec_t *last_usec) {
23 unsigned n, i, j, k;
24 usec_t z, x;
25
26 if (!on_tty())
27 return;
28
29 z = now(CLOCK_MONOTONIC);
30 x = *last_usec;
31
32 if (x != 0 && x + 40 * USEC_PER_MSEC > z)
33 return;
34
35 *last_usec = z;
36
37 n = (3 * columns()) / 4;
38 j = (n * (unsigned) p) / 65535ULL;
39 k = n - j;
40
7565bb98
LP
41 fputs("\r", stdout);
42 if (colors_enabled())
43 fputs("\x1B[?25l" ANSI_HIGHLIGHT_GREEN, stdout);
54f3ff07
ZJS
44
45 for (i = 0; i < j; i++)
46 fputs("\xe2\x96\x88", stdout);
47
1fc464f6 48 fputs(ANSI_NORMAL, stdout);
54f3ff07
ZJS
49
50 for (i = 0; i < k; i++)
51 fputs("\xe2\x96\x91", stdout);
52
53 printf(" %3"PRIu64"%%", 100U * p / 65535U);
54
7565bb98
LP
55 fputs("\r", stdout);
56 if (colors_enabled())
57 fputs("\x1B[?25h", stdout);
58
54f3ff07
ZJS
59 fflush(stdout);
60}
61
45c047b2
LP
62static uint64_t scale_progress(uint64_t scale, uint64_t p, uint64_t m) {
63
64 /* Calculates scale * p / m, but handles m == 0 safely, and saturates */
65
66 if (p >= m || m == 0)
67 return scale;
68
69 return scale * p / m;
70}
71
54f3ff07
ZJS
72static void flush_progress(void) {
73 unsigned n, i;
74
75 if (!on_tty())
76 return;
77
78 n = (3 * columns()) / 4;
79
80 putchar('\r');
81
82 for (i = 0; i < n + 5; i++)
83 putchar(' ');
84
85 putchar('\r');
86 fflush(stdout);
87}
88
9ed794a3 89#define debug(_offset, _fmt, ...) do { \
54f3ff07
ZJS
90 flush_progress(); \
91 log_debug(OFSfmt": " _fmt, _offset, ##__VA_ARGS__); \
9ed794a3 92 } while (0)
54f3ff07 93
9ed794a3 94#define warning(_offset, _fmt, ...) do { \
54f3ff07
ZJS
95 flush_progress(); \
96 log_warning(OFSfmt": " _fmt, _offset, ##__VA_ARGS__); \
9ed794a3 97 } while (0)
54f3ff07 98
9ed794a3 99#define error(_offset, _fmt, ...) do { \
54f3ff07
ZJS
100 flush_progress(); \
101 log_error(OFSfmt": " _fmt, (uint64_t)_offset, ##__VA_ARGS__); \
9ed794a3 102 } while (0)
54f3ff07 103
581fc868
ZJS
104#define error_errno(_offset, error, _fmt, ...) do { \
105 flush_progress(); \
106 log_error_errno(error, OFSfmt": " _fmt, (uint64_t)_offset, ##__VA_ARGS__); \
107 } while (0)
108
92fba83e 109static int journal_file_object_verify(JournalFile *f, uint64_t offset, Object *o) {
db11ac1a
LP
110 uint64_t i;
111
0284adc6 112 assert(f);
92fba83e 113 assert(offset);
0284adc6
LP
114 assert(o);
115
116 /* This does various superficial tests about the length an
117 * possible field values. It does not follow any references to
118 * other objects. */
119
d89c8fdf 120 if ((o->object.flags & OBJECT_COMPRESSED_XZ) &&
bca9e39d
LP
121 o->object.type != OBJECT_DATA) {
122 error(offset, "Found compressed object that isn't of type DATA, which is not allowed.");
f59a5f6b 123 return -EBADMSG;
bca9e39d 124 }
f59a5f6b 125
0284adc6 126 switch (o->object.type) {
f59a5f6b 127
fd5dc320
LP
128 case OBJECT_DATA: {
129 uint64_t h1, h2;
1ec7120e 130 int compression, r;
fd5dc320 131
92fba83e 132 if (le64toh(o->data.entry_offset) == 0)
e80acc51 133 warning(offset, "Unused data (entry_offset==0)");
92fba83e
ZJS
134
135 if ((le64toh(o->data.entry_offset) == 0) ^ (le64toh(o->data.n_entries) == 0)) {
f39c13e0 136 error(offset, "Bad n_entries: %"PRIu64, le64toh(o->data.n_entries));
0284adc6 137 return -EBADMSG;
92fba83e 138 }
0284adc6 139
92fba83e 140 if (le64toh(o->object.size) - offsetof(DataObject, payload) <= 0) {
e80acc51 141 error(offset, "Bad object size (<= %zu): %"PRIu64,
54f3ff07
ZJS
142 offsetof(DataObject, payload),
143 le64toh(o->object.size));
0284adc6 144 return -EBADMSG;
92fba83e 145 }
f59a5f6b 146
fd5dc320 147 h1 = le64toh(o->data.hash);
f59a5f6b 148
1ec7120e
ZJS
149 compression = o->object.flags & OBJECT_COMPRESSION_MASK;
150 if (compression) {
d89c8fdf 151 _cleanup_free_ void *b = NULL;
fa1c4b51 152 size_t alloc = 0, b_size;
f59a5f6b 153
1ec7120e
ZJS
154 r = decompress_blob(compression,
155 o->data.payload,
156 le64toh(o->object.size) - offsetof(Object, data.payload),
157 &b, &alloc, &b_size, 0);
158 if (r < 0) {
581fc868
ZJS
159 error_errno(offset, r, "%s decompression failed: %m",
160 object_compressed_to_string(compression));
1ec7120e 161 return r;
92fba83e 162 }
fd5dc320
LP
163
164 h2 = hash64(b, b_size);
fd5dc320
LP
165 } else
166 h2 = hash64(o->data.payload, le64toh(o->object.size) - offsetof(Object, data.payload));
167
92fba83e 168 if (h1 != h2) {
e80acc51 169 error(offset, "Invalid hash (%08"PRIx64" vs. %08"PRIx64, h1, h2);
fd5dc320 170 return -EBADMSG;
92fba83e 171 }
f59a5f6b 172
f39c13e0
LP
173 if (!VALID64(le64toh(o->data.next_hash_offset)) ||
174 !VALID64(le64toh(o->data.next_field_offset)) ||
175 !VALID64(le64toh(o->data.entry_offset)) ||
176 !VALID64(le64toh(o->data.entry_array_offset))) {
e80acc51 177 error(offset, "Invalid offset (next_hash_offset="OFSfmt", next_field_offset="OFSfmt", entry_offset="OFSfmt", entry_array_offset="OFSfmt,
f39c13e0
LP
178 le64toh(o->data.next_hash_offset),
179 le64toh(o->data.next_field_offset),
180 le64toh(o->data.entry_offset),
181 le64toh(o->data.entry_array_offset));
db11ac1a 182 return -EBADMSG;
92fba83e 183 }
db11ac1a 184
0284adc6 185 break;
fd5dc320 186 }
0284adc6
LP
187
188 case OBJECT_FIELD:
92fba83e 189 if (le64toh(o->object.size) - offsetof(FieldObject, payload) <= 0) {
54f3ff07 190 error(offset,
e80acc51 191 "Bad field size (<= %zu): %"PRIu64,
54f3ff07
ZJS
192 offsetof(FieldObject, payload),
193 le64toh(o->object.size));
0284adc6 194 return -EBADMSG;
92fba83e 195 }
db11ac1a 196
f39c13e0
LP
197 if (!VALID64(le64toh(o->field.next_hash_offset)) ||
198 !VALID64(le64toh(o->field.head_data_offset))) {
54f3ff07 199 error(offset,
e80acc51 200 "Invalid offset (next_hash_offset="OFSfmt", head_data_offset="OFSfmt,
f39c13e0
LP
201 le64toh(o->field.next_hash_offset),
202 le64toh(o->field.head_data_offset));
db11ac1a 203 return -EBADMSG;
92fba83e 204 }
0284adc6
LP
205 break;
206
207 case OBJECT_ENTRY:
92fba83e 208 if ((le64toh(o->object.size) - offsetof(EntryObject, items)) % sizeof(EntryItem) != 0) {
54f3ff07 209 error(offset,
e80acc51 210 "Bad entry size (<= %zu): %"PRIu64,
54f3ff07
ZJS
211 offsetof(EntryObject, items),
212 le64toh(o->object.size));
0284adc6 213 return -EBADMSG;
92fba83e 214 }
0284adc6 215
92fba83e 216 if ((le64toh(o->object.size) - offsetof(EntryObject, items)) / sizeof(EntryItem) <= 0) {
54f3ff07 217 error(offset,
e80acc51 218 "Invalid number items in entry: %"PRIu64,
54f3ff07 219 (le64toh(o->object.size) - offsetof(EntryObject, items)) / sizeof(EntryItem));
0284adc6 220 return -EBADMSG;
92fba83e
ZJS
221 }
222
223 if (le64toh(o->entry.seqnum) <= 0) {
54f3ff07 224 error(offset,
e80acc51 225 "Invalid entry seqnum: %"PRIx64,
54f3ff07 226 le64toh(o->entry.seqnum));
92fba83e
ZJS
227 return -EBADMSG;
228 }
0284adc6 229
92fba83e 230 if (!VALID_REALTIME(le64toh(o->entry.realtime))) {
54f3ff07 231 error(offset,
e80acc51 232 "Invalid entry realtime timestamp: %"PRIu64,
54f3ff07 233 le64toh(o->entry.realtime));
0284adc6 234 return -EBADMSG;
92fba83e
ZJS
235 }
236
237 if (!VALID_MONOTONIC(le64toh(o->entry.monotonic))) {
54f3ff07 238 error(offset,
e80acc51 239 "Invalid entry monotonic timestamp: %"PRIu64,
54f3ff07 240 le64toh(o->entry.monotonic));
92fba83e
ZJS
241 return -EBADMSG;
242 }
0284adc6 243
db11ac1a 244 for (i = 0; i < journal_file_entry_n_items(o); i++) {
f39c13e0
LP
245 if (le64toh(o->entry.items[i].object_offset) == 0 ||
246 !VALID64(le64toh(o->entry.items[i].object_offset))) {
54f3ff07 247 error(offset,
e80acc51 248 "Invalid entry item (%"PRIu64"/%"PRIu64" offset: "OFSfmt,
54f3ff07 249 i, journal_file_entry_n_items(o),
f39c13e0 250 le64toh(o->entry.items[i].object_offset));
db11ac1a 251 return -EBADMSG;
92fba83e 252 }
db11ac1a
LP
253 }
254
0284adc6
LP
255 break;
256
257 case OBJECT_DATA_HASH_TABLE:
258 case OBJECT_FIELD_HASH_TABLE:
92fba83e
ZJS
259 if ((le64toh(o->object.size) - offsetof(HashTableObject, items)) % sizeof(HashItem) != 0 ||
260 (le64toh(o->object.size) - offsetof(HashTableObject, items)) / sizeof(HashItem) <= 0) {
54f3ff07 261 error(offset,
e80acc51 262 "Invalid %s hash table size: %"PRIu64,
54f3ff07
ZJS
263 o->object.type == OBJECT_DATA_HASH_TABLE ? "data" : "field",
264 le64toh(o->object.size));
86adf873 265 return -EBADMSG;
92fba83e 266 }
86adf873 267
fb9a24b6
LP
268 for (i = 0; i < journal_file_hash_table_n_items(o); i++) {
269 if (o->hash_table.items[i].head_hash_offset != 0 &&
92fba83e 270 !VALID64(le64toh(o->hash_table.items[i].head_hash_offset))) {
54f3ff07 271 error(offset,
e80acc51 272 "Invalid %s hash table item (%"PRIu64"/%"PRIu64") head_hash_offset: "OFSfmt,
54f3ff07
ZJS
273 o->object.type == OBJECT_DATA_HASH_TABLE ? "data" : "field",
274 i, journal_file_hash_table_n_items(o),
275 le64toh(o->hash_table.items[i].head_hash_offset));
fb9a24b6 276 return -EBADMSG;
92fba83e 277 }
fb9a24b6 278 if (o->hash_table.items[i].tail_hash_offset != 0 &&
92fba83e 279 !VALID64(le64toh(o->hash_table.items[i].tail_hash_offset))) {
54f3ff07 280 error(offset,
e80acc51 281 "Invalid %s hash table item (%"PRIu64"/%"PRIu64") tail_hash_offset: "OFSfmt,
54f3ff07
ZJS
282 o->object.type == OBJECT_DATA_HASH_TABLE ? "data" : "field",
283 i, journal_file_hash_table_n_items(o),
284 le64toh(o->hash_table.items[i].tail_hash_offset));
fb9a24b6 285 return -EBADMSG;
92fba83e 286 }
fb9a24b6
LP
287
288 if ((o->hash_table.items[i].head_hash_offset != 0) !=
92fba83e 289 (o->hash_table.items[i].tail_hash_offset != 0)) {
54f3ff07 290 error(offset,
e80acc51 291 "Invalid %s hash table item (%"PRIu64"/%"PRIu64"): head_hash_offset="OFSfmt" tail_hash_offset="OFSfmt,
54f3ff07
ZJS
292 o->object.type == OBJECT_DATA_HASH_TABLE ? "data" : "field",
293 i, journal_file_hash_table_n_items(o),
294 le64toh(o->hash_table.items[i].head_hash_offset),
295 le64toh(o->hash_table.items[i].tail_hash_offset));
fb9a24b6 296 return -EBADMSG;
92fba83e 297 }
fb9a24b6
LP
298 }
299
0284adc6
LP
300 break;
301
302 case OBJECT_ENTRY_ARRAY:
92fba83e
ZJS
303 if ((le64toh(o->object.size) - offsetof(EntryArrayObject, items)) % sizeof(le64_t) != 0 ||
304 (le64toh(o->object.size) - offsetof(EntryArrayObject, items)) / sizeof(le64_t) <= 0) {
54f3ff07 305 error(offset,
e80acc51 306 "Invalid object entry array size: %"PRIu64,
54f3ff07 307 le64toh(o->object.size));
86adf873 308 return -EBADMSG;
92fba83e 309 }
86adf873 310
f39c13e0 311 if (!VALID64(le64toh(o->entry_array.next_entry_array_offset))) {
54f3ff07 312 error(offset,
e80acc51 313 "Invalid object entry array next_entry_array_offset: "OFSfmt,
f39c13e0 314 le64toh(o->entry_array.next_entry_array_offset));
db11ac1a 315 return -EBADMSG;
92fba83e 316 }
db11ac1a 317
fb9a24b6 318 for (i = 0; i < journal_file_entry_array_n_items(o); i++)
af13a6b0
GM
319 if (le64toh(o->entry_array.items[i]) != 0 &&
320 !VALID64(le64toh(o->entry_array.items[i]))) {
54f3ff07 321 error(offset,
e80acc51 322 "Invalid object entry array item (%"PRIu64"/%"PRIu64"): "OFSfmt,
54f3ff07
ZJS
323 i, journal_file_entry_array_n_items(o),
324 le64toh(o->entry_array.items[i]));
fb9a24b6 325 return -EBADMSG;
92fba83e 326 }
fb9a24b6 327
0284adc6
LP
328 break;
329
330 case OBJECT_TAG:
92fba83e 331 if (le64toh(o->object.size) != sizeof(TagObject)) {
54f3ff07 332 error(offset,
e80acc51 333 "Invalid object tag size: %"PRIu64,
54f3ff07 334 le64toh(o->object.size));
0284adc6 335 return -EBADMSG;
92fba83e 336 }
fc89a139 337
f39c13e0 338 if (!VALID_EPOCH(le64toh(o->tag.epoch))) {
54f3ff07 339 error(offset,
e80acc51 340 "Invalid object tag epoch: %"PRIu64,
f39c13e0 341 le64toh(o->tag.epoch));
fc89a139 342 return -EBADMSG;
92fba83e 343 }
fc89a139 344
0284adc6
LP
345 break;
346 }
347
348 return 0;
349}
350
0284adc6
LP
351static int write_uint64(int fd, uint64_t p) {
352 ssize_t k;
353
354 k = write(fd, &p, sizeof(p));
355 if (k < 0)
356 return -errno;
357 if (k != sizeof(p))
358 return -EIO;
359
360 return 0;
361}
362
be7cdd8e 363static int contains_uint64(MMapCache *m, MMapFileDescriptor *f, uint64_t n, uint64_t p) {
0284adc6
LP
364 uint64_t a, b;
365 int r;
366
367 assert(m);
be7cdd8e 368 assert(f);
0284adc6
LP
369
370 /* Bisection ... */
371
372 a = 0; b = n;
373 while (a < b) {
374 uint64_t c, *z;
375
376 c = (a + b) / 2;
377
b42549ad 378 r = mmap_cache_get(m, f, PROT_READ|PROT_WRITE, 0, false, c * sizeof(uint64_t), sizeof(uint64_t), NULL, (void **) &z, NULL);
0284adc6
LP
379 if (r < 0)
380 return r;
381
382 if (*z == p)
383 return 1;
384
a2e99cdf
LP
385 if (a + 1 >= b)
386 return 0;
387
0284adc6
LP
388 if (p < *z)
389 b = c;
fcde2389 390 else
0284adc6
LP
391 a = c;
392 }
393
394 return 0;
395}
396
86adf873
LP
397static int entry_points_to_data(
398 JournalFile *f,
be7cdd8e 399 MMapFileDescriptor *cache_entry_fd,
86adf873
LP
400 uint64_t n_entries,
401 uint64_t entry_p,
402 uint64_t data_p) {
403
404 int r;
405 uint64_t i, n, a;
406 Object *o;
407 bool found = false;
408
409 assert(f);
be7cdd8e 410 assert(cache_entry_fd);
86adf873 411
be7cdd8e 412 if (!contains_uint64(f->mmap, cache_entry_fd, n_entries, entry_p)) {
e80acc51 413 error(data_p, "Data object references invalid entry at "OFSfmt, entry_p);
86adf873
LP
414 return -EBADMSG;
415 }
416
417 r = journal_file_move_to_object(f, OBJECT_ENTRY, entry_p, &o);
418 if (r < 0)
419 return r;
420
421 n = journal_file_entry_n_items(o);
422 for (i = 0; i < n; i++)
423 if (le64toh(o->entry.items[i].object_offset) == data_p) {
424 found = true;
425 break;
426 }
427
428 if (!found) {
e80acc51 429 error(entry_p, "Data object at "OFSfmt" not referenced by linked entry", data_p);
86adf873
LP
430 return -EBADMSG;
431 }
432
433 /* Check if this entry is also in main entry array. Since the
434 * main entry array has already been verified we can rely on
7517e174 435 * its consistency. */
86adf873 436
369f0589 437 i = 0;
86adf873
LP
438 n = le64toh(f->header->n_entries);
439 a = le64toh(f->header->entry_array_offset);
86adf873
LP
440
441 while (i < n) {
369f0589 442 uint64_t m, u;
86adf873
LP
443
444 r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o);
445 if (r < 0)
446 return r;
447
448 m = journal_file_entry_array_n_items(o);
369f0589
LP
449 u = MIN(n - i, m);
450
451 if (entry_p <= le64toh(o->entry_array.items[u-1])) {
452 uint64_t x, y, z;
453
454 x = 0;
455 y = u;
456
457 while (x < y) {
458 z = (x + y) / 2;
459
460 if (le64toh(o->entry_array.items[z]) == entry_p)
461 return 0;
462
463 if (x + 1 >= y)
464 break;
465
466 if (entry_p < le64toh(o->entry_array.items[z]))
467 y = z;
468 else
469 x = z;
470 }
471
e80acc51 472 error(entry_p, "Entry object doesn't exist in main entry array");
369f0589
LP
473 return -EBADMSG;
474 }
86adf873 475
369f0589 476 i += u;
356fe3e6 477 a = le64toh(o->entry_array.next_entry_array_offset);
86adf873
LP
478 }
479
480 return 0;
481}
482
483static int verify_data(
484 JournalFile *f,
485 Object *o, uint64_t p,
be7cdd8e
VC
486 MMapFileDescriptor *cache_entry_fd, uint64_t n_entries,
487 MMapFileDescriptor *cache_entry_array_fd, uint64_t n_entry_arrays) {
86adf873
LP
488
489 uint64_t i, n, a, last, q;
490 int r;
491
492 assert(f);
493 assert(o);
be7cdd8e
VC
494 assert(cache_entry_fd);
495 assert(cache_entry_array_fd);
86adf873
LP
496
497 n = le64toh(o->data.n_entries);
498 a = le64toh(o->data.entry_array_offset);
499
92fba83e
ZJS
500 /* Entry array means at least two objects */
501 if (a && n < 2) {
e80acc51 502 error(p, "Entry array present (entry_array_offset="OFSfmt", but n_entries=%"PRIu64")", a, n);
92fba83e
ZJS
503 return -EBADMSG;
504 }
505
506 if (n == 0)
507 return 0;
508
509 /* We already checked that earlier */
510 assert(o->data.entry_offset);
86adf873
LP
511
512 last = q = le64toh(o->data.entry_offset);
be7cdd8e 513 r = entry_points_to_data(f, cache_entry_fd, n_entries, q, p);
86adf873
LP
514 if (r < 0)
515 return r;
516
2a7273ef 517 i = 1;
86adf873
LP
518 while (i < n) {
519 uint64_t next, m, j;
520
521 if (a == 0) {
e80acc51 522 error(p, "Array chain too short");
86adf873
LP
523 return -EBADMSG;
524 }
525
be7cdd8e 526 if (!contains_uint64(f->mmap, cache_entry_array_fd, n_entry_arrays, a)) {
e80acc51 527 error(p, "Invalid array offset "OFSfmt, a);
86adf873
LP
528 return -EBADMSG;
529 }
530
531 r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o);
532 if (r < 0)
533 return r;
534
535 next = le64toh(o->entry_array.next_entry_array_offset);
536 if (next != 0 && next <= a) {
e80acc51 537 error(p, "Array chain has cycle (jumps back from "OFSfmt" to "OFSfmt")", a, next);
86adf873
LP
538 return -EBADMSG;
539 }
540
541 m = journal_file_entry_array_n_items(o);
542 for (j = 0; i < n && j < m; i++, j++) {
543
544 q = le64toh(o->entry_array.items[j]);
545 if (q <= last) {
e80acc51 546 error(p, "Data object's entry array not sorted");
86adf873
LP
547 return -EBADMSG;
548 }
549 last = q;
550
be7cdd8e 551 r = entry_points_to_data(f, cache_entry_fd, n_entries, q, p);
86adf873
LP
552 if (r < 0)
553 return r;
554
555 /* Pointer might have moved, reposition */
556 r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o);
557 if (r < 0)
558 return r;
559 }
560
561 a = next;
562 }
563
564 return 0;
565}
566
567static int verify_hash_table(
568 JournalFile *f,
be7cdd8e
VC
569 MMapFileDescriptor *cache_data_fd, uint64_t n_data,
570 MMapFileDescriptor *cache_entry_fd, uint64_t n_entries,
571 MMapFileDescriptor *cache_entry_array_fd, uint64_t n_entry_arrays,
b72631e5
LP
572 usec_t *last_usec,
573 bool show_progress) {
86adf873
LP
574
575 uint64_t i, n;
576 int r;
577
578 assert(f);
be7cdd8e
VC
579 assert(cache_data_fd);
580 assert(cache_entry_fd);
581 assert(cache_entry_array_fd);
86adf873
LP
582 assert(last_usec);
583
584 n = le64toh(f->header->data_hash_table_size) / sizeof(HashItem);
dade37d4
LP
585 if (n <= 0)
586 return 0;
587
588 r = journal_file_map_data_hash_table(f);
589 if (r < 0)
590 return log_error_errno(r, "Failed to map data hash table: %m");
591
86adf873
LP
592 for (i = 0; i < n; i++) {
593 uint64_t last = 0, p;
594
b72631e5 595 if (show_progress)
45c047b2 596 draw_progress(0xC000 + scale_progress(0x3FFF, i, n), last_usec);
86adf873
LP
597
598 p = le64toh(f->data_hash_table[i].head_hash_offset);
599 while (p != 0) {
600 Object *o;
601 uint64_t next;
602
be7cdd8e 603 if (!contains_uint64(f->mmap, cache_data_fd, n_data, p)) {
e80acc51 604 error(p, "Invalid data object at hash entry %"PRIu64" of %"PRIu64, i, n);
86adf873
LP
605 return -EBADMSG;
606 }
607
608 r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
609 if (r < 0)
610 return r;
611
612 next = le64toh(o->data.next_hash_offset);
613 if (next != 0 && next <= p) {
e80acc51 614 error(p, "Hash chain has a cycle in hash entry %"PRIu64" of %"PRIu64, i, n);
86adf873
LP
615 return -EBADMSG;
616 }
617
618 if (le64toh(o->data.hash) % n != i) {
e80acc51 619 error(p, "Hash value mismatch in hash entry %"PRIu64" of %"PRIu64, i, n);
86adf873
LP
620 return -EBADMSG;
621 }
622
be7cdd8e 623 r = verify_data(f, o, p, cache_entry_fd, n_entries, cache_entry_array_fd, n_entry_arrays);
86adf873
LP
624 if (r < 0)
625 return r;
626
627 last = p;
628 p = next;
629 }
630
631 if (last != le64toh(f->data_hash_table[i].tail_hash_offset)) {
e80acc51 632 error(p, "Tail hash pointer mismatch in hash table");
86adf873
LP
633 return -EBADMSG;
634 }
635 }
636
637 return 0;
638}
639
640static int data_object_in_hash_table(JournalFile *f, uint64_t hash, uint64_t p) {
641 uint64_t n, h, q;
642 int r;
643 assert(f);
644
645 n = le64toh(f->header->data_hash_table_size) / sizeof(HashItem);
dade37d4
LP
646 if (n <= 0)
647 return 0;
648
649 r = journal_file_map_data_hash_table(f);
650 if (r < 0)
651 return log_error_errno(r, "Failed to map data hash table: %m");
652
86adf873
LP
653 h = hash % n;
654
655 q = le64toh(f->data_hash_table[h].head_hash_offset);
656 while (q != 0) {
657 Object *o;
658
659 if (p == q)
660 return 1;
661
662 r = journal_file_move_to_object(f, OBJECT_DATA, q, &o);
663 if (r < 0)
664 return r;
665
666 q = le64toh(o->data.next_hash_offset);
667 }
668
669 return 0;
670}
671
672static int verify_entry(
673 JournalFile *f,
674 Object *o, uint64_t p,
be7cdd8e 675 MMapFileDescriptor *cache_data_fd, uint64_t n_data) {
86adf873
LP
676
677 uint64_t i, n;
678 int r;
679
680 assert(f);
681 assert(o);
be7cdd8e 682 assert(cache_data_fd);
86adf873
LP
683
684 n = journal_file_entry_n_items(o);
685 for (i = 0; i < n; i++) {
686 uint64_t q, h;
687 Object *u;
688
689 q = le64toh(o->entry.items[i].object_offset);
690 h = le64toh(o->entry.items[i].hash);
691
be7cdd8e 692 if (!contains_uint64(f->mmap, cache_data_fd, n_data, q)) {
e80acc51
LP
693 error(p, "Invalid data object of entry");
694 return -EBADMSG;
695 }
86adf873
LP
696
697 r = journal_file_move_to_object(f, OBJECT_DATA, q, &u);
698 if (r < 0)
699 return r;
700
701 if (le64toh(u->data.hash) != h) {
e80acc51 702 error(p, "Hash mismatch for data object of entry");
86adf873
LP
703 return -EBADMSG;
704 }
705
706 r = data_object_in_hash_table(f, h, q);
707 if (r < 0)
708 return r;
709 if (r == 0) {
e80acc51 710 error(p, "Data object missing from hash table");
86adf873
LP
711 return -EBADMSG;
712 }
713 }
714
715 return 0;
716}
717
718static int verify_entry_array(
719 JournalFile *f,
be7cdd8e
VC
720 MMapFileDescriptor *cache_data_fd, uint64_t n_data,
721 MMapFileDescriptor *cache_entry_fd, uint64_t n_entries,
722 MMapFileDescriptor *cache_entry_array_fd, uint64_t n_entry_arrays,
b72631e5
LP
723 usec_t *last_usec,
724 bool show_progress) {
86adf873
LP
725
726 uint64_t i = 0, a, n, last = 0;
727 int r;
728
729 assert(f);
be7cdd8e
VC
730 assert(cache_data_fd);
731 assert(cache_entry_fd);
732 assert(cache_entry_array_fd);
86adf873
LP
733 assert(last_usec);
734
735 n = le64toh(f->header->n_entries);
736 a = le64toh(f->header->entry_array_offset);
737 while (i < n) {
738 uint64_t next, m, j;
739 Object *o;
740
b72631e5 741 if (show_progress)
45c047b2 742 draw_progress(0x8000 + scale_progress(0x3FFF, i, n), last_usec);
86adf873
LP
743
744 if (a == 0) {
e80acc51 745 error(a, "Array chain too short at %"PRIu64" of %"PRIu64, i, n);
86adf873
LP
746 return -EBADMSG;
747 }
748
be7cdd8e 749 if (!contains_uint64(f->mmap, cache_entry_array_fd, n_entry_arrays, a)) {
e80acc51 750 error(a, "Invalid array %"PRIu64" of %"PRIu64, i, n);
86adf873
LP
751 return -EBADMSG;
752 }
753
754 r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o);
755 if (r < 0)
756 return r;
757
758 next = le64toh(o->entry_array.next_entry_array_offset);
759 if (next != 0 && next <= a) {
e80acc51 760 error(a, "Array chain has cycle at %"PRIu64" of %"PRIu64" (jumps back from to "OFSfmt")", i, n, next);
86adf873
LP
761 return -EBADMSG;
762 }
763
764 m = journal_file_entry_array_n_items(o);
765 for (j = 0; i < n && j < m; i++, j++) {
766 uint64_t p;
767
768 p = le64toh(o->entry_array.items[j]);
769 if (p <= last) {
e80acc51 770 error(a, "Entry array not sorted at %"PRIu64" of %"PRIu64, i, n);
86adf873
LP
771 return -EBADMSG;
772 }
773 last = p;
774
be7cdd8e 775 if (!contains_uint64(f->mmap, cache_entry_fd, n_entries, p)) {
e80acc51 776 error(a, "Invalid array entry at %"PRIu64" of %"PRIu64, i, n);
86adf873
LP
777 return -EBADMSG;
778 }
779
780 r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
781 if (r < 0)
782 return r;
783
be7cdd8e 784 r = verify_entry(f, o, p, cache_data_fd, n_data);
86adf873
LP
785 if (r < 0)
786 return r;
787
788 /* Pointer might have moved, reposition */
789 r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o);
790 if (r < 0)
791 return r;
792 }
793
794 a = next;
795 }
796
797 return 0;
798}
799
6c7be122
LP
800int journal_file_verify(
801 JournalFile *f,
802 const char *key,
2a7b539a 803 usec_t *first_contained, usec_t *last_validated, usec_t *last_contained,
b72631e5 804 bool show_progress) {
0284adc6
LP
805 int r;
806 Object *o;
0ab5c3ed
ZJS
807 uint64_t p = 0, last_epoch = 0, last_tag_realtime = 0, last_sealed_realtime = 0;
808
14d10188 809 uint64_t entry_seqnum = 0, entry_monotonic = 0, entry_realtime = 0;
0284adc6
LP
810 sd_id128_t entry_boot_id;
811 bool entry_seqnum_set = false, entry_monotonic_set = false, entry_realtime_set = false, found_main_entry_array = false;
14d10188 812 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;
0284adc6
LP
813 usec_t last_usec = 0;
814 int data_fd = -1, entry_fd = -1, entry_array_fd = -1;
be7cdd8e 815 MMapFileDescriptor *cache_data_fd = NULL, *cache_entry_fd = NULL, *cache_entry_array_fd = NULL;
97147f8c 816 unsigned i;
e8c108ca 817 bool found_last = false;
992e8f22 818 const char *tmp_dir = NULL;
34a8f081 819
349cc4a5 820#if HAVE_GCRYPT
0ab5c3ed
ZJS
821 uint64_t last_tag = 0;
822#endif
0284adc6
LP
823 assert(f);
824
baed47c3 825 if (key) {
349cc4a5 826#if HAVE_GCRYPT
baed47c3 827 r = journal_file_parse_verification_key(f, key);
b7c9ae91
LP
828 if (r < 0) {
829 log_error("Failed to parse seed.");
56e81f7c 830 return r;
b7c9ae91 831 }
feb12d3e 832#else
15411c0c 833 return -EOPNOTSUPP;
feb12d3e 834#endif
c586dbf1
LP
835 } else if (f->seal)
836 return -ENOKEY;
b7c9ae91 837
992e8f22 838 r = var_tmp_dir(&tmp_dir);
34a8f081
OW
839 if (r < 0) {
840 log_error_errno(r, "Failed to determine temporary directory: %m");
841 goto fail;
842 }
843
844 data_fd = open_tmpfile_unlinkable(tmp_dir, O_RDWR | O_CLOEXEC);
0284adc6 845 if (data_fd < 0) {
709f6e46 846 r = log_error_errno(data_fd, "Failed to create data file: %m");
0284adc6
LP
847 goto fail;
848 }
0284adc6 849
34a8f081 850 entry_fd = open_tmpfile_unlinkable(tmp_dir, O_RDWR | O_CLOEXEC);
0284adc6 851 if (entry_fd < 0) {
709f6e46 852 r = log_error_errno(entry_fd, "Failed to create entry file: %m");
0284adc6
LP
853 goto fail;
854 }
0284adc6 855
34a8f081 856 entry_array_fd = open_tmpfile_unlinkable(tmp_dir, O_RDWR | O_CLOEXEC);
0284adc6 857 if (entry_array_fd < 0) {
709f6e46 858 r = log_error_errno(entry_array_fd,
76ef789d 859 "Failed to create entry array file: %m");
0284adc6
LP
860 goto fail;
861 }
0284adc6 862
be7cdd8e
VC
863 cache_data_fd = mmap_cache_add_fd(f->mmap, data_fd);
864 if (!cache_data_fd) {
865 r = log_oom();
866 goto fail;
867 }
868
869 cache_entry_fd = mmap_cache_add_fd(f->mmap, entry_fd);
870 if (!cache_entry_fd) {
871 r = log_oom();
872 goto fail;
873 }
874
875 cache_entry_array_fd = mmap_cache_add_fd(f->mmap, entry_array_fd);
876 if (!cache_entry_array_fd) {
877 r = log_oom();
878 goto fail;
879 }
880
54f3ff07 881 if (le32toh(f->header->compatible_flags) & ~HEADER_COMPATIBLE_SUPPORTED) {
97147f8c 882 log_error("Cannot verify file with unknown extensions.");
15411c0c 883 r = -EOPNOTSUPP;
97147f8c
LP
884 goto fail;
885 }
886
887 for (i = 0; i < sizeof(f->header->reserved); i++)
888 if (f->header->reserved[i] != 0) {
e80acc51 889 error(offsetof(Header, reserved[i]), "Reserved field is non-zero");
97147f8c
LP
890 r = -EBADMSG;
891 goto fail;
892 }
893
0284adc6
LP
894 /* First iteration: we go through all objects, verify the
895 * superficial structure, headers, hashes. */
896
0284adc6 897 p = le64toh(f->header->header_size);
8dc37a85
LP
898 for (;;) {
899 /* Early exit if there are no objects in the file, at all */
900 if (le64toh(f->header->tail_object_offset) == 0)
901 break;
902
b72631e5 903 if (show_progress)
45c047b2 904 draw_progress(scale_progress(0x7FFF, p, le64toh(f->header->tail_object_offset)), &last_usec);
0284adc6 905
d05089d8 906 r = journal_file_move_to_object(f, OBJECT_UNUSED, p, &o);
0284adc6 907 if (r < 0) {
e80acc51 908 error(p, "Invalid object");
0284adc6
LP
909 goto fail;
910 }
911
97147f8c 912 if (p > le64toh(f->header->tail_object_offset)) {
e80acc51 913 error(offsetof(Header, tail_object_offset), "Invalid tail object pointer");
0284adc6
LP
914 r = -EBADMSG;
915 goto fail;
916 }
917
313cefa1 918 n_objects++;
0284adc6 919
92fba83e 920 r = journal_file_object_verify(f, p, o);
0284adc6 921 if (r < 0) {
581fc868 922 error_errno(p, r, "Invalid object contents: %m");
0284adc6
LP
923 goto fail;
924 }
925
d89c8fdf
ZJS
926 if ((o->object.flags & OBJECT_COMPRESSED_XZ) &&
927 (o->object.flags & OBJECT_COMPRESSED_LZ4)) {
e80acc51 928 error(p, "Objected with double compression");
54f3ff07 929 r = -EINVAL;
d89c8fdf
ZJS
930 goto fail;
931 }
932
933 if ((o->object.flags & OBJECT_COMPRESSED_XZ) && !JOURNAL_HEADER_COMPRESSED_XZ(f->header)) {
54f3ff07 934 error(p, "XZ compressed object in file without XZ compression");
d89c8fdf
ZJS
935 r = -EBADMSG;
936 goto fail;
937 }
938
939 if ((o->object.flags & OBJECT_COMPRESSED_LZ4) && !JOURNAL_HEADER_COMPRESSED_LZ4(f->header)) {
54f3ff07 940 error(p, "LZ4 compressed object in file without LZ4 compression");
0284adc6
LP
941 r = -EBADMSG;
942 goto fail;
943 }
944
a8e5f514 945 switch (o->object.type) {
0284adc6 946
a8e5f514
LP
947 case OBJECT_DATA:
948 r = write_uint64(data_fd, p);
949 if (r < 0)
0284adc6 950 goto fail;
0284adc6 951
a8e5f514
LP
952 n_data++;
953 break;
0284adc6 954
a8e5f514
LP
955 case OBJECT_FIELD:
956 n_fields++;
957 break;
0284adc6 958
a8e5f514 959 case OBJECT_ENTRY:
8088cbd3 960 if (JOURNAL_HEADER_SEALED(f->header) && n_tags <= 0) {
e80acc51 961 error(p, "First entry before first tag");
6c7be122
LP
962 r = -EBADMSG;
963 goto fail;
964 }
965
0284adc6
LP
966 r = write_uint64(entry_fd, p);
967 if (r < 0)
968 goto fail;
969
f7fab8a5 970 if (le64toh(o->entry.realtime) < last_tag_realtime) {
e80acc51 971 error(p, "Older entry after newer tag");
7b5fd91c
LP
972 r = -EBADMSG;
973 goto fail;
974 }
975
0284adc6
LP
976 if (!entry_seqnum_set &&
977 le64toh(o->entry.seqnum) != le64toh(f->header->head_entry_seqnum)) {
e80acc51 978 error(p, "Head entry sequence number incorrect");
0284adc6
LP
979 r = -EBADMSG;
980 goto fail;
981 }
982
983 if (entry_seqnum_set &&
984 entry_seqnum >= le64toh(o->entry.seqnum)) {
e80acc51 985 error(p, "Entry sequence number out of synchronization");
0284adc6
LP
986 r = -EBADMSG;
987 goto fail;
988 }
989
990 entry_seqnum = le64toh(o->entry.seqnum);
991 entry_seqnum_set = true;
992
993 if (entry_monotonic_set &&
994 sd_id128_equal(entry_boot_id, o->entry.boot_id) &&
995 entry_monotonic > le64toh(o->entry.monotonic)) {
e80acc51 996 error(p, "Entry timestamp out of synchronization");
0284adc6
LP
997 r = -EBADMSG;
998 goto fail;
999 }
1000
1001 entry_monotonic = le64toh(o->entry.monotonic);
1002 entry_boot_id = o->entry.boot_id;
1003 entry_monotonic_set = true;
1004
1005 if (!entry_realtime_set &&
1006 le64toh(o->entry.realtime) != le64toh(f->header->head_entry_realtime)) {
e80acc51 1007 error(p, "Head entry realtime timestamp incorrect");
0284adc6
LP
1008 r = -EBADMSG;
1009 goto fail;
1010 }
1011
1012 entry_realtime = le64toh(o->entry.realtime);
1013 entry_realtime_set = true;
1014
313cefa1 1015 n_entries++;
a8e5f514 1016 break;
0284adc6 1017
a8e5f514 1018 case OBJECT_DATA_HASH_TABLE:
0284adc6 1019 if (n_data_hash_tables > 1) {
e80acc51 1020 error(p, "More than one data hash table");
0284adc6
LP
1021 r = -EBADMSG;
1022 goto fail;
1023 }
1024
1025 if (le64toh(f->header->data_hash_table_offset) != p + offsetof(HashTableObject, items) ||
1026 le64toh(f->header->data_hash_table_size) != le64toh(o->object.size) - offsetof(HashTableObject, items)) {
54f3ff07 1027 error(p, "header fields for data hash table invalid");
0284adc6
LP
1028 r = -EBADMSG;
1029 goto fail;
1030 }
0284adc6 1031
a8e5f514
LP
1032 n_data_hash_tables++;
1033 break;
1034
1035 case OBJECT_FIELD_HASH_TABLE:
0284adc6 1036 if (n_field_hash_tables > 1) {
e80acc51 1037 error(p, "More than one field hash table");
0284adc6
LP
1038 r = -EBADMSG;
1039 goto fail;
1040 }
1041
1042 if (le64toh(f->header->field_hash_table_offset) != p + offsetof(HashTableObject, items) ||
1043 le64toh(f->header->field_hash_table_size) != le64toh(o->object.size) - offsetof(HashTableObject, items)) {
e80acc51 1044 error(p, "Header fields for field hash table invalid");
0284adc6
LP
1045 r = -EBADMSG;
1046 goto fail;
1047 }
a8e5f514
LP
1048
1049 n_field_hash_tables++;
1050 break;
1051
1052 case OBJECT_ENTRY_ARRAY:
1053 r = write_uint64(entry_array_fd, p);
1054 if (r < 0)
1055 goto fail;
1056
1057 if (p == le64toh(f->header->entry_array_offset)) {
1058 if (found_main_entry_array) {
e80acc51 1059 error(p, "More than one main entry array");
a8e5f514
LP
1060 r = -EBADMSG;
1061 goto fail;
1062 }
1063
1064 found_main_entry_array = true;
1065 }
1066
1067 n_entry_arrays++;
1068 break;
1069
feb12d3e 1070 case OBJECT_TAG:
8088cbd3 1071 if (!JOURNAL_HEADER_SEALED(f->header)) {
e80acc51 1072 error(p, "Tag object in file without sealing");
a8e5f514
LP
1073 r = -EBADMSG;
1074 goto fail;
1075 }
1076
1077 if (le64toh(o->tag.seqnum) != n_tags + 1) {
e80acc51 1078 error(p, "Tag sequence number out of synchronization");
a8e5f514
LP
1079 r = -EBADMSG;
1080 goto fail;
1081 }
1082
14d10188 1083 if (le64toh(o->tag.epoch) < last_epoch) {
e80acc51 1084 error(p, "Epoch sequence out of synchronization");
7b5fd91c
LP
1085 r = -EBADMSG;
1086 goto fail;
1087 }
1088
349cc4a5 1089#if HAVE_GCRYPT
c586dbf1 1090 if (f->seal) {
feb12d3e
LP
1091 uint64_t q, rt;
1092
e80acc51 1093 debug(p, "Checking tag %"PRIu64"...", le64toh(o->tag.seqnum));
14d10188 1094
f39c13e0 1095 rt = f->fss_start_usec + le64toh(o->tag.epoch) * f->fss_interval_usec;
f7fab8a5 1096 if (entry_realtime_set && entry_realtime >= rt + f->fss_interval_usec) {
54f3ff07 1097 error(p, "tag/entry realtime timestamp out of synchronization");
c586dbf1
LP
1098 r = -EBADMSG;
1099 goto fail;
1100 }
14d10188 1101
c586dbf1
LP
1102 /* OK, now we know the epoch. So let's now set
1103 * it, and calculate the HMAC for everything
1104 * since the last tag. */
1105 r = journal_file_fsprg_seek(f, le64toh(o->tag.epoch));
14d10188
LP
1106 if (r < 0)
1107 goto fail;
1108
c586dbf1 1109 r = journal_file_hmac_start(f);
14d10188
LP
1110 if (r < 0)
1111 goto fail;
1112
c586dbf1
LP
1113 if (last_tag == 0) {
1114 r = journal_file_hmac_put_header(f);
1115 if (r < 0)
1116 goto fail;
1117
1118 q = le64toh(f->header->header_size);
1119 } else
1120 q = last_tag;
1121
1122 while (q <= p) {
d05089d8 1123 r = journal_file_move_to_object(f, OBJECT_UNUSED, q, &o);
c586dbf1
LP
1124 if (r < 0)
1125 goto fail;
1126
d05089d8 1127 r = journal_file_hmac_put_object(f, OBJECT_UNUSED, o, q);
c586dbf1
LP
1128 if (r < 0)
1129 goto fail;
1130
1131 q = q + ALIGN64(le64toh(o->object.size));
1132 }
1133
1134 /* Position might have changed, let's reposition things */
d05089d8 1135 r = journal_file_move_to_object(f, OBJECT_UNUSED, p, &o);
14d10188
LP
1136 if (r < 0)
1137 goto fail;
1138
c586dbf1 1139 if (memcmp(o->tag.tag, gcry_md_read(f->hmac, 0), TAG_LENGTH) != 0) {
e80acc51 1140 error(p, "Tag failed verification");
c586dbf1
LP
1141 r = -EBADMSG;
1142 goto fail;
1143 }
14d10188 1144
c586dbf1
LP
1145 f->hmac_running = false;
1146 last_tag_realtime = rt;
f7fab8a5 1147 last_sealed_realtime = entry_realtime;
14d10188
LP
1148 }
1149
14d10188 1150 last_tag = p + ALIGN64(le64toh(o->object.size));
0ab5c3ed
ZJS
1151#endif
1152
c586dbf1 1153 last_epoch = le64toh(o->tag.epoch);
6c7be122 1154
313cefa1 1155 n_tags++;
a8e5f514
LP
1156 break;
1157
1158 default:
313cefa1 1159 n_weird++;
a8e5f514 1160 }
0284adc6 1161
8dc37a85
LP
1162 if (p == le64toh(f->header->tail_object_offset)) {
1163 found_last = true;
1164 break;
1165 }
0284adc6 1166
8dc37a85
LP
1167 p = p + ALIGN64(le64toh(o->object.size));
1168 };
1169
1170 if (!found_last && le64toh(f->header->tail_object_offset) != 0) {
e80acc51 1171 error(le64toh(f->header->tail_object_offset), "Tail object pointer dead");
97147f8c
LP
1172 r = -EBADMSG;
1173 goto fail;
1174 }
1175
0284adc6 1176 if (n_objects != le64toh(f->header->n_objects)) {
e80acc51 1177 error(offsetof(Header, n_objects), "Object number mismatch");
0284adc6
LP
1178 r = -EBADMSG;
1179 goto fail;
1180 }
1181
1182 if (n_entries != le64toh(f->header->n_entries)) {
e80acc51 1183 error(offsetof(Header, n_entries), "Entry number mismatch");
0284adc6
LP
1184 r = -EBADMSG;
1185 goto fail;
1186 }
1187
1188 if (JOURNAL_HEADER_CONTAINS(f->header, n_data) &&
1189 n_data != le64toh(f->header->n_data)) {
e80acc51 1190 error(offsetof(Header, n_data), "Data number mismatch");
0284adc6
LP
1191 r = -EBADMSG;
1192 goto fail;
1193 }
1194
1195 if (JOURNAL_HEADER_CONTAINS(f->header, n_fields) &&
1196 n_fields != le64toh(f->header->n_fields)) {
e80acc51 1197 error(offsetof(Header, n_fields), "Field number mismatch");
0284adc6
LP
1198 r = -EBADMSG;
1199 goto fail;
1200 }
1201
1202 if (JOURNAL_HEADER_CONTAINS(f->header, n_tags) &&
a8e5f514 1203 n_tags != le64toh(f->header->n_tags)) {
e80acc51 1204 error(offsetof(Header, n_tags), "Tag number mismatch");
0284adc6
LP
1205 r = -EBADMSG;
1206 goto fail;
1207 }
1208
2dee23eb
LP
1209 if (JOURNAL_HEADER_CONTAINS(f->header, n_entry_arrays) &&
1210 n_entry_arrays != le64toh(f->header->n_entry_arrays)) {
e80acc51 1211 error(offsetof(Header, n_entry_arrays), "Entry array number mismatch");
2dee23eb
LP
1212 r = -EBADMSG;
1213 goto fail;
1214 }
1215
8dc37a85 1216 if (!found_main_entry_array && le64toh(f->header->entry_array_offset) != 0) {
e80acc51 1217 error(0, "Missing entry array");
0284adc6
LP
1218 r = -EBADMSG;
1219 goto fail;
1220 }
1221
1222 if (entry_seqnum_set &&
1223 entry_seqnum != le64toh(f->header->tail_entry_seqnum)) {
e80acc51 1224 error(offsetof(Header, tail_entry_seqnum), "Invalid tail seqnum");
0284adc6
LP
1225 r = -EBADMSG;
1226 goto fail;
1227 }
1228
1229 if (entry_monotonic_set &&
e71d1f6c 1230 (sd_id128_equal(entry_boot_id, f->header->boot_id) &&
0284adc6 1231 entry_monotonic != le64toh(f->header->tail_entry_monotonic))) {
e80acc51 1232 error(0, "Invalid tail monotonic timestamp");
0284adc6
LP
1233 r = -EBADMSG;
1234 goto fail;
1235 }
1236
1237 if (entry_realtime_set && entry_realtime != le64toh(f->header->tail_entry_realtime)) {
e80acc51 1238 error(0, "Invalid tail realtime timestamp");
0284adc6
LP
1239 r = -EBADMSG;
1240 goto fail;
1241 }
1242
86adf873
LP
1243 /* Second iteration: we follow all objects referenced from the
1244 * two entry points: the object hash table and the entry
1245 * array. We also check that everything referenced (directly
1246 * or indirectly) in the data hash table also exists in the
1247 * entry array, and vice versa. Note that we do not care for
1248 * unreferenced objects. We only care that everything that is
1249 * referenced is consistent. */
1250
1251 r = verify_entry_array(f,
be7cdd8e
VC
1252 cache_data_fd, n_data,
1253 cache_entry_fd, n_entries,
1254 cache_entry_array_fd, n_entry_arrays,
b72631e5
LP
1255 &last_usec,
1256 show_progress);
86adf873
LP
1257 if (r < 0)
1258 goto fail;
0284adc6 1259
86adf873 1260 r = verify_hash_table(f,
be7cdd8e
VC
1261 cache_data_fd, n_data,
1262 cache_entry_fd, n_entries,
1263 cache_entry_array_fd, n_entry_arrays,
b72631e5
LP
1264 &last_usec,
1265 show_progress);
86adf873
LP
1266 if (r < 0)
1267 goto fail;
0284adc6 1268
b72631e5
LP
1269 if (show_progress)
1270 flush_progress();
0284adc6 1271
be7cdd8e
VC
1272 mmap_cache_free_fd(f->mmap, cache_data_fd);
1273 mmap_cache_free_fd(f->mmap, cache_entry_fd);
1274 mmap_cache_free_fd(f->mmap, cache_entry_array_fd);
0284adc6 1275
03e334a1
LP
1276 safe_close(data_fd);
1277 safe_close(entry_fd);
1278 safe_close(entry_array_fd);
0284adc6 1279
2a7b539a
LP
1280 if (first_contained)
1281 *first_contained = le64toh(f->header->head_entry_realtime);
6c7be122 1282 if (last_validated)
f7fab8a5 1283 *last_validated = last_sealed_realtime;
6c7be122
LP
1284 if (last_contained)
1285 *last_contained = le64toh(f->header->tail_entry_realtime);
1286
0284adc6
LP
1287 return 0;
1288
1289fail:
b72631e5
LP
1290 if (show_progress)
1291 flush_progress();
0284adc6 1292
92fba83e 1293 log_error("File corruption detected at %s:"OFSfmt" (of %llu bytes, %"PRIu64"%%).",
0284adc6 1294 f->path,
507f22bd 1295 p,
0284adc6 1296 (unsigned long long) f->last_stat.st_size,
507f22bd 1297 100 * p / f->last_stat.st_size);
0284adc6 1298
be7cdd8e 1299 if (data_fd >= 0)
03e334a1 1300 safe_close(data_fd);
0284adc6 1301
be7cdd8e 1302 if (entry_fd >= 0)
03e334a1 1303 safe_close(entry_fd);
0284adc6 1304
be7cdd8e 1305 if (entry_array_fd >= 0)
03e334a1 1306 safe_close(entry_array_fd);
be7cdd8e
VC
1307
1308 if (cache_data_fd)
1309 mmap_cache_free_fd(f->mmap, cache_data_fd);
1310
1311 if (cache_entry_fd)
1312 mmap_cache_free_fd(f->mmap, cache_entry_fd);
1313
1314 if (cache_entry_array_fd)
1315 mmap_cache_free_fd(f->mmap, cache_entry_array_fd);
0284adc6
LP
1316
1317 return r;
1318}