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