journal-verify.c revision 1b8951e5bd9b2bf1722098a861055cae0bb52088
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2012 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <unistd.h>
#include <fcntl.h>
#include <stddef.h>
#include "util.h"
#include "macro.h"
#include "journal-def.h"
#include "journal-file.h"
#include "journal-authenticate.h"
#include "journal-verify.h"
#include "lookup3.h"
#include "compress.h"
#include "fsprg.h"
unsigned n, i, j, k;
usec_t z, x;
if (!on_tty())
return;
z = now(CLOCK_MONOTONIC);
x = *last_usec;
if (x != 0 && x + 40 * USEC_PER_MSEC > z)
return;
*last_usec = z;
j = (n * (unsigned) p) / 65535ULL;
k = n - j;
for (i = 0; i < j; i++)
for (i = 0; i < k; i++)
}
static void flush_progress(void) {
unsigned n, i;
if (!on_tty())
return;
putchar('\r');
for (i = 0; i < n + 5; i++)
putchar(' ');
putchar('\r');
}
flush_progress(); \
} while(0)
flush_progress(); \
} while(0)
flush_progress(); \
} while(0)
uint64_t i;
assert(f);
assert(o);
/* This does various superficial tests about the length an
* possible field values. It does not follow any references to
* other objects. */
return -EBADMSG;
case OBJECT_DATA: {
int compression, r;
return -EBADMSG;
}
return -EBADMSG;
}
if (compression) {
_cleanup_free_ void *b = NULL;
if (r < 0) {
return r;
}
} else
return -EBADMSG;
}
error(offset, "invalid offset (next_hash_offset="OFSfmt", next_field_offset="OFSfmt", entry_offset="OFSfmt", entry_array_offset="OFSfmt,
o->data.next_hash_offset,
o->data.entry_offset,
o->data.entry_array_offset);
return -EBADMSG;
}
break;
}
case OBJECT_FIELD:
"bad field size (<= %zu): %"PRIu64,
return -EBADMSG;
}
o->field.head_data_offset);
return -EBADMSG;
}
break;
case OBJECT_ENTRY:
"bad entry size (<= %zu): %"PRIu64,
return -EBADMSG;
}
"invalid number items in entry: %"PRIu64,
return -EBADMSG;
}
"invalid entry seqnum: %"PRIx64,
return -EBADMSG;
}
"invalid entry realtime timestamp: %"PRIu64,
return -EBADMSG;
}
"invalid entry monotonic timestamp: %"PRIu64,
return -EBADMSG;
}
for (i = 0; i < journal_file_entry_n_items(o); i++) {
i, journal_file_entry_n_items(o),
return -EBADMSG;
}
}
break;
case OBJECT_DATA_HASH_TABLE:
case OBJECT_FIELD_HASH_TABLE:
"invalid %s hash table size: %"PRIu64,
return -EBADMSG;
}
for (i = 0; i < journal_file_hash_table_n_items(o); i++) {
return -EBADMSG;
}
return -EBADMSG;
}
"invalid %s hash table item (%"PRIu64"/%"PRIu64"): head_hash_offset="OFSfmt" tail_hash_offset="OFSfmt,
return -EBADMSG;
}
}
break;
case OBJECT_ENTRY_ARRAY:
"invalid object entry array size: %"PRIu64,
return -EBADMSG;
}
"invalid object entry array next_entry_array_offset: "OFSfmt,
return -EBADMSG;
}
for (i = 0; i < journal_file_entry_array_n_items(o); i++)
return -EBADMSG;
}
break;
case OBJECT_TAG:
"invalid object tag size: %"PRIu64,
return -EBADMSG;
}
"invalid object tag epoch: %"PRIu64,
return -EBADMSG;
}
break;
}
return 0;
}
ssize_t k;
if (k < 0)
return -errno;
if (k != sizeof(p))
return -EIO;
return 0;
}
uint64_t a, b;
int r;
assert(m);
/* Bisection ... */
a = 0; b = n;
while (a < b) {
uint64_t c, *z;
c = (a + b) / 2;
r = mmap_cache_get(m, fd, PROT_READ|PROT_WRITE, 0, false, c * sizeof(uint64_t), sizeof(uint64_t), NULL, (void **) &z);
if (r < 0)
return r;
if (*z == p)
return 1;
if (a + 1 >= b)
return 0;
if (p < *z)
b = c;
else
a = c;
}
return 0;
}
static int entry_points_to_data(
JournalFile *f,
int entry_fd,
int r;
uint64_t i, n, a;
Object *o;
bool found = false;
assert(f);
return -EBADMSG;
}
if (r < 0)
return r;
n = journal_file_entry_n_items(o);
for (i = 0; i < n; i++)
found = true;
break;
}
if (!found) {
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;
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);
uint64_t x, y, z;
x = 0;
y = u;
while (x < y) {
z = (x + y) / 2;
return 0;
if (x + 1 >= y)
break;
y = z;
else
x = z;
}
return -EBADMSG;
}
i += u;
}
return 0;
}
static int verify_data(
JournalFile *f,
int r;
assert(f);
assert(o);
assert(entry_array_fd >= 0);
/* Entry array means at least two objects */
if (a && n < 2) {
error(p,
a, n);
return -EBADMSG;
}
if (n == 0)
return 0;
/* We already checked that earlier */
if (r < 0)
return r;
i = 1;
while (i < n) {
if (a == 0) {
error(p, "array chain too short");
return -EBADMSG;
}
return -EBADMSG;
}
r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o);
if (r < 0)
return r;
a, next);
return -EBADMSG;
}
m = journal_file_entry_array_n_items(o);
for (j = 0; i < n && j < m; i++, j++) {
if (q <= last) {
error(p, "data object's entry array not sorted");
return -EBADMSG;
}
last = q;
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,
bool show_progress) {
uint64_t i, n;
int r;
assert(f);
assert(entry_array_fd >= 0);
for (i = 0; i < n; i++) {
if (show_progress)
while (p != 0) {
Object *o;
i, n);
return -EBADMSG;
}
r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
if (r < 0)
return r;
i, n);
return -EBADMSG;
}
i, n);
return -EBADMSG;
}
if (r < 0)
return r;
last = p;
p = next;
}
error(p, "tail hash pointer mismatch in hash table");
return -EBADMSG;
}
}
return 0;
}
uint64_t n, h, q;
int r;
assert(f);
h = hash % n;
while (q != 0) {
Object *o;
if (p == q)
return 1;
r = journal_file_move_to_object(f, OBJECT_DATA, q, &o);
if (r < 0)
return r;
}
return 0;
}
static int verify_entry(
JournalFile *f,
uint64_t i, n;
int r;
assert(f);
assert(o);
n = journal_file_entry_n_items(o);
for (i = 0; i < n; i++) {
uint64_t q, h;
Object *u;
error(p, "invalid data object of entry");
return -EBADMSG;
}
r = journal_file_move_to_object(f, OBJECT_DATA, q, &u);
if (r < 0)
return r;
error(p, "hash mismatch for data object of entry");
return -EBADMSG;
}
r = data_object_in_hash_table(f, h, q);
if (r < 0)
return r;
if (r == 0) {
error(p, "data object missing from hash table");
return -EBADMSG;
}
}
return 0;
}
static int verify_entry_array(
JournalFile *f,
bool show_progress) {
int r;
assert(f);
assert(entry_array_fd >= 0);
while (i < n) {
Object *o;
if (show_progress)
if (a == 0) {
return -EBADMSG;
}
return -EBADMSG;
}
r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o);
if (r < 0)
return r;
error(a,
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;
if (p <= last) {
i, n);
return -EBADMSG;
}
last = p;
i, n);
return -EBADMSG;
}
r = journal_file_move_to_object(f, OBJECT_ENTRY, p, &o);
if (r < 0)
return r;
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;
}
int journal_file_verify(
JournalFile *f,
const char *key,
bool show_progress) {
int r;
Object *o;
bool entry_seqnum_set = false, entry_monotonic_set = false, entry_realtime_set = false, found_main_entry_array = false;
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;
unsigned i;
bool found_last = false;
#ifdef HAVE_GCRYPT
#endif
assert(f);
if (key) {
#ifdef HAVE_GCRYPT
r = journal_file_parse_verification_key(f, key);
if (r < 0) {
log_error("Failed to parse seed.");
return r;
}
#else
return -ENOTSUP;
#endif
} else if (f->seal)
return -ENOKEY;
if (data_fd < 0) {
r = -errno;
goto fail;
}
if (entry_fd < 0) {
r = -errno;
goto fail;
}
if (entry_array_fd < 0) {
r = -errno;
goto fail;
}
log_error("Cannot verify file with unknown extensions.");
r = -ENOTSUP;
goto fail;
}
r = -EBADMSG;
goto fail;
}
/* First iteration: we go through all objects, verify the
* superficial structure, headers, hashes. */
while (p != 0) {
if (show_progress)
r = journal_file_move_to_object(f, -1, p, &o);
if (r < 0) {
error(p, "invalid object");
goto fail;
}
r = -EBADMSG;
goto fail;
}
found_last = true;
n_objects ++;
r = journal_file_object_verify(f, p, o);
if (r < 0) {
goto fail;
}
error(p, "objected with double compression");
r = -EINVAL;
goto fail;
}
error(p, "XZ compressed object in file without XZ compression");
r = -EBADMSG;
goto fail;
}
error(p, "LZ4 compressed object in file without LZ4 compression");
r = -EBADMSG;
goto fail;
}
case OBJECT_DATA:
r = write_uint64(data_fd, p);
if (r < 0)
goto fail;
n_data++;
break;
case OBJECT_FIELD:
n_fields++;
break;
case OBJECT_ENTRY:
error(p, "first entry before first tag");
r = -EBADMSG;
goto fail;
}
r = write_uint64(entry_fd, p);
if (r < 0)
goto fail;
error(p, "older entry after newer tag");
r = -EBADMSG;
goto fail;
}
if (!entry_seqnum_set &&
error(p, "head entry sequence number incorrect");
r = -EBADMSG;
goto fail;
}
if (entry_seqnum_set &&
error(p, "entry sequence number out of synchronization");
r = -EBADMSG;
goto fail;
}
entry_seqnum_set = true;
if (entry_monotonic_set &&
error(p, "entry timestamp out of synchronization");
r = -EBADMSG;
goto fail;
}
entry_monotonic_set = true;
if (!entry_realtime_set &&
error(p, "head entry realtime timestamp incorrect");
r = -EBADMSG;
goto fail;
}
entry_realtime_set = true;
n_entries ++;
break;
case OBJECT_DATA_HASH_TABLE:
if (n_data_hash_tables > 1) {
error(p, "more than one data hash table");
r = -EBADMSG;
goto fail;
}
le64toh(f->header->data_hash_table_size) != le64toh(o->object.size) - offsetof(HashTableObject, items)) {
error(p, "header fields for data hash table invalid");
r = -EBADMSG;
goto fail;
}
break;
case OBJECT_FIELD_HASH_TABLE:
if (n_field_hash_tables > 1) {
error(p, "more than one field hash table");
r = -EBADMSG;
goto fail;
}
le64toh(f->header->field_hash_table_size) != le64toh(o->object.size) - offsetof(HashTableObject, items)) {
error(p, "header fields for field hash table invalid");
r = -EBADMSG;
goto fail;
}
break;
case OBJECT_ENTRY_ARRAY:
r = write_uint64(entry_array_fd, p);
if (r < 0)
goto fail;
if (found_main_entry_array) {
error(p, "more than one main entry array");
r = -EBADMSG;
goto fail;
}
found_main_entry_array = true;
}
break;
case OBJECT_TAG:
if (!JOURNAL_HEADER_SEALED(f->header)) {
error(p, "tag object in file without sealing");
r = -EBADMSG;
goto fail;
}
error(p, "tag sequence number out of synchronization");
r = -EBADMSG;
goto fail;
}
error(p, "epoch sequence out of synchronization");
r = -EBADMSG;
goto fail;
}
#ifdef HAVE_GCRYPT
if (f->seal) {
r = -EBADMSG;
goto fail;
}
/* OK, now we know the epoch. So let's now set
* it, and calculate the HMAC for everything
* since the last tag. */
if (r < 0)
goto fail;
r = journal_file_hmac_start(f);
if (r < 0)
goto fail;
if (last_tag == 0) {
r = journal_file_hmac_put_header(f);
if (r < 0)
goto fail;
} else
q = last_tag;
while (q <= p) {
r = journal_file_move_to_object(f, -1, q, &o);
if (r < 0)
goto fail;
r = journal_file_hmac_put_object(f, -1, o, q);
if (r < 0)
goto fail;
}
/* Position might have changed, let's reposition things */
r = journal_file_move_to_object(f, -1, p, &o);
if (r < 0)
goto fail;
error(p, "tag failed verification");
r = -EBADMSG;
goto fail;
}
f->hmac_running = false;
}
#endif
n_tags ++;
break;
default:
n_weird ++;
}
p = 0;
else
}
if (!found_last) {
r = -EBADMSG;
goto fail;
}
r = -EBADMSG;
goto fail;
}
r = -EBADMSG;
goto fail;
}
r = -EBADMSG;
goto fail;
}
r = -EBADMSG;
goto fail;
}
r = -EBADMSG;
goto fail;
}
r = -EBADMSG;
goto fail;
}
if (n_data_hash_tables != 1) {
error(0, "missing data hash table");
r = -EBADMSG;
goto fail;
}
if (n_field_hash_tables != 1) {
error(0, "missing field hash table");
r = -EBADMSG;
goto fail;
}
if (!found_main_entry_array) {
error(0, "missing entry array");
r = -EBADMSG;
goto fail;
}
if (entry_seqnum_set &&
r = -EBADMSG;
goto fail;
}
if (entry_monotonic_set &&
error(0, "invalid tail monotonic timestamp");
r = -EBADMSG;
goto fail;
}
error(0, "invalid tail realtime timestamp");
r = -EBADMSG;
goto fail;
}
/* Second iteration: we follow all objects referenced from the
* two entry points: the object hash table and the entry
* array. We also check that everything referenced (directly
* or indirectly) in the data hash table also exists in the
* entry array, and vice versa. Note that we do not care for
* unreferenced objects. We only care that everything that is
* referenced is consistent. */
r = verify_entry_array(f,
if (r < 0)
goto fail;
r = verify_hash_table(f,
if (r < 0)
goto fail;
if (show_progress)
if (first_contained)
if (last_validated)
if (last_contained)
return 0;
fail:
if (show_progress)
f->path,
p,
if (data_fd >= 0) {
}
if (entry_fd >= 0) {
}
if (entry_array_fd >= 0) {
}
return r;
}