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