static int verify_entry( JournalFile *f, Object *o, uint64_t p, int data_fd, uint64_t n_data) { uint64_t i, n; int r; assert(f); assert(o); assert(data_fd >= 0); n = journal_file_entry_n_items(o); for (i = 0; i < n; i++) { uint64_t q, h; Object *u; q = le64toh(o->entry.items[i].object_offset); h = le64toh(o->entry.items[i].hash); if (!contains_uint64(f->mmap, data_fd, n_data, q)) { log_error("Invalid data object at entry %llu", (unsigned long long) p); return -EBADMSG; } r = journal_file_move_to_object(f, OBJECT_DATA, q, &u); if (r < 0) return r; if (le64toh(u->data.hash) != h) { log_error("Hash mismatch for data object at entry %llu", (unsigned long long) p); return -EBADMSG; } r = data_object_in_hash_table(f, h, q); if (r < 0) return r; if (r == 0) { log_error("Data object missing from hash at entry %llu", (unsigned long long) p); return -EBADMSG; } } return 0; }
static int verify_entry_array( JournalFile *f, int data_fd, uint64_t n_data, int entry_fd, uint64_t n_entries, int entry_array_fd, uint64_t n_entry_arrays, usec_t *last_usec, bool show_progress) { uint64_t i = 0, a, n, last = 0; int r; assert(f); assert(data_fd >= 0); assert(entry_fd >= 0); assert(entry_array_fd >= 0); assert(last_usec); n = le64toh(f->header->n_entries); a = le64toh(f->header->entry_array_offset); while (i < n) { uint64_t next, m, j; Object *o; if (show_progress) draw_progress(0x8000 + scale_progress(0x3FFF, i, n), last_usec); if (a == 0) { error(a, "Array chain too short at %"PRIu64" of %"PRIu64, i, n); return -EBADMSG; } if (!contains_uint64(f->mmap, entry_array_fd, n_entry_arrays, a)) { error(a, "Invalid array %"PRIu64" of %"PRIu64, i, n); return -EBADMSG; } r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o); if (r < 0) return r; next = le64toh(o->entry_array.next_entry_array_offset); if (next != 0 && next <= a) { error(a, "Array chain has cycle at %"PRIu64" of %"PRIu64" (jumps back from to "OFSfmt")", i, n, next); return -EBADMSG; } m = journal_file_entry_array_n_items(o); for (j = 0; i < n && j < m; i++, j++) { uint64_t p; p = le64toh(o->entry_array.items[j]); if (p <= last) { error(a, "Entry array not sorted at %"PRIu64" of %"PRIu64, i, n); return -EBADMSG; } last = p; if (!contains_uint64(f->mmap, entry_fd, n_entries, p)) { error(a, "Invalid array entry at %"PRIu64" of %"PRIu64, i, n); return -EBADMSG; } r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o); if (r < 0) return r; r = verify_entry(f, o, p, data_fd, n_data); if (r < 0) return r; /* Pointer might have moved, reposition */ r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o); if (r < 0) return r; } a = next; } return 0; }
static int verify_hash_table( JournalFile *f, int data_fd, uint64_t n_data, int entry_fd, uint64_t n_entries, int entry_array_fd, uint64_t n_entry_arrays, usec_t *last_usec, bool show_progress) { uint64_t i, n; int r; assert(f); assert(data_fd >= 0); assert(entry_fd >= 0); assert(entry_array_fd >= 0); assert(last_usec); n = le64toh(f->header->data_hash_table_size) / sizeof(HashItem); if (n <= 0) return 0; r = journal_file_map_data_hash_table(f); if (r < 0) return log_error_errno(r, "Failed to map data hash table: %m"); for (i = 0; i < n; i++) { uint64_t last = 0, p; if (show_progress) draw_progress(0xC000 + scale_progress(0x3FFF, i, n), last_usec); p = le64toh(f->data_hash_table[i].head_hash_offset); while (p != 0) { Object *o; uint64_t next; if (!contains_uint64(f->mmap, data_fd, n_data, p)) { error(p, "Invalid data object at hash entry %"PRIu64" of %"PRIu64, i, n); return -EBADMSG; } r = journal_file_move_to_object(f, OBJECT_DATA, p, &o); if (r < 0) return r; next = le64toh(o->data.next_hash_offset); if (next != 0 && next <= p) { error(p, "Hash chain has a cycle in hash entry %"PRIu64" of %"PRIu64, i, n); return -EBADMSG; } if (le64toh(o->data.hash) % n != i) { error(p, "Hash value mismatch in hash entry %"PRIu64" of %"PRIu64, i, n); return -EBADMSG; } r = verify_data(f, o, p, entry_fd, n_entries, entry_array_fd, n_entry_arrays); if (r < 0) return r; last = p; p = next; } if (last != le64toh(f->data_hash_table[i].tail_hash_offset)) { error(p, "Tail hash pointer mismatch in hash table"); return -EBADMSG; } } return 0; }
static int verify_data( JournalFile *f, Object *o, uint64_t p, int entry_fd, uint64_t n_entries, int entry_array_fd, uint64_t n_entry_arrays) { uint64_t i, n, a, last, q; int r; assert(f); assert(o); assert(entry_fd >= 0); assert(entry_array_fd >= 0); n = le64toh(o->data.n_entries); a = le64toh(o->data.entry_array_offset); /* Entry array means at least two objects */ if (a && n < 2) { error(p, "Entry array present (entry_array_offset="OFSfmt", but n_entries=%"PRIu64")", a, n); return -EBADMSG; } if (n == 0) return 0; /* We already checked that earlier */ assert(o->data.entry_offset); last = q = le64toh(o->data.entry_offset); r = entry_points_to_data(f, entry_fd, n_entries, q, p); if (r < 0) return r; i = 1; while (i < n) { uint64_t next, m, j; if (a == 0) { error(p, "Array chain too short"); return -EBADMSG; } if (!contains_uint64(f->mmap, entry_array_fd, n_entry_arrays, a)) { error(p, "Invalid array offset "OFSfmt, a); return -EBADMSG; } r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o); if (r < 0) return r; next = le64toh(o->entry_array.next_entry_array_offset); if (next != 0 && next <= a) { error(p, "Array chain has cycle (jumps back from "OFSfmt" to "OFSfmt")", a, next); return -EBADMSG; } m = journal_file_entry_array_n_items(o); for (j = 0; i < n && j < m; i++, j++) { q = le64toh(o->entry_array.items[j]); if (q <= last) { error(p, "Data object's entry array not sorted"); return -EBADMSG; } last = q; r = entry_points_to_data(f, entry_fd, n_entries, q, p); if (r < 0) return r; /* Pointer might have moved, reposition */ r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o); if (r < 0) return r; } a = next; } return 0; }
static int entry_points_to_data( JournalFile *f, int entry_fd, uint64_t n_entries, uint64_t entry_p, uint64_t data_p) { int r; uint64_t i, n, a; Object *o; bool found = false; assert(f); assert(entry_fd >= 0); if (!contains_uint64(f->mmap, entry_fd, n_entries, entry_p)) { error(data_p, "Data object references invalid entry at "OFSfmt, entry_p); return -EBADMSG; } r = journal_file_move_to_object(f, OBJECT_ENTRY, entry_p, &o); if (r < 0) return r; n = journal_file_entry_n_items(o); for (i = 0; i < n; i++) if (le64toh(o->entry.items[i].object_offset) == data_p) { found = true; break; } if (!found) { error(entry_p, "Data object at "OFSfmt" not referenced by linked entry", data_p); return -EBADMSG; } /* Check if this entry is also in main entry array. Since the * main entry array has already been verified we can rely on * its consistency. */ i = 0; n = le64toh(f->header->n_entries); a = le64toh(f->header->entry_array_offset); while (i < n) { uint64_t m, u; r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o); if (r < 0) return r; m = journal_file_entry_array_n_items(o); u = MIN(n - i, m); if (entry_p <= le64toh(o->entry_array.items[u-1])) { uint64_t x, y, z; x = 0; y = u; while (x < y) { z = (x + y) / 2; if (le64toh(o->entry_array.items[z]) == entry_p) return 0; if (x + 1 >= y) break; if (entry_p < le64toh(o->entry_array.items[z])) y = z; else x = z; } error(entry_p, "Entry object doesn't exist in main entry array"); return -EBADMSG; } i += u; a = le64toh(o->entry_array.next_entry_array_offset); } return 0; }
static int verify_data( JournalFile *f, Object *o, uint64_t p, int entry_fd, uint64_t n_entries, int entry_array_fd, uint64_t n_entry_arrays) { uint64_t i, n, a, last, q; int r; assert(f); assert(o); assert(entry_fd >= 0); assert(entry_array_fd >= 0); n = le64toh(o->data.n_entries); a = le64toh(o->data.entry_array_offset); /* We already checked this earlier */ assert(n > 0); last = q = le64toh(o->data.entry_offset); r = entry_points_to_data(f, entry_fd, n_entries, q, p); if (r < 0) return r; i = 1; while (i < n) { uint64_t next, m, j; if (a == 0) { log_error("Array chain too short at %llu", (unsigned long long) p); return -EBADMSG; } if (!contains_uint64(f->mmap, entry_array_fd, n_entry_arrays, a)) { log_error("Invalid array at %llu", (unsigned long long) p); return -EBADMSG; } r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o); if (r < 0) return r; next = le64toh(o->entry_array.next_entry_array_offset); if (next != 0 && next <= a) { log_error("Array chain has cycle at %llu", (unsigned long long) p); return -EBADMSG; } m = journal_file_entry_array_n_items(o); for (j = 0; i < n && j < m; i++, j++) { q = le64toh(o->entry_array.items[j]); if (q <= last) { log_error("Data object's entry array not sorted at %llu", (unsigned long long) p); return -EBADMSG; } last = q; r = entry_points_to_data(f, entry_fd, n_entries, q, p); if (r < 0) return r; /* Pointer might have moved, reposition */ r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o); if (r < 0) return r; } a = next; } return 0; }