journal-verify.c revision 8088cbd3cfcf539c984d8042cd2b92ebbfda6d82
/*-*- 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 "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"
/* FIXME:
*
* - evolve key even if nothing happened in regular intervals
*
* - Allow building without libgcrypt
* - check with sparse
* - 64bit conversions
*
* */
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: {
return -EBADMSG;
return -EBADMSG;
void *b = NULL;
return -EBADMSG;
free(b);
} else
return -EBADMSG;
return -EBADMSG;
break;
}
case OBJECT_FIELD:
return -EBADMSG;
return -EBADMSG;
break;
case OBJECT_ENTRY:
return -EBADMSG;
return -EBADMSG;
return -EBADMSG;
for (i = 0; i < journal_file_entry_n_items(o); i++) {
return -EBADMSG;
}
break;
case OBJECT_DATA_HASH_TABLE:
case OBJECT_FIELD_HASH_TABLE:
return -EBADMSG;
return -EBADMSG;
for (i = 0; i < journal_file_hash_table_n_items(o); i++) {
return -EBADMSG;
return -EBADMSG;
return -EBADMSG;
}
break;
case OBJECT_ENTRY_ARRAY:
return -EBADMSG;
return -EBADMSG;
return -EBADMSG;
for (i = 0; i < journal_file_entry_array_n_items(o); i++)
if (o->entry_array.items[i] != 0 &&
return -EBADMSG;
break;
case OBJECT_TAG:
return -EBADMSG;
return -EBADMSG;
break;
}
return 0;
}
unsigned n, i, j, k;
usec_t z, x;
if (!isatty(STDOUT_FILENO))
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 (!isatty(STDOUT_FILENO))
return;
putchar('\r');
for (i = 0; i < n + 5; i++)
putchar(' ');
putchar('\r');
}
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, c * sizeof(uint64_t), sizeof(uint64_t), (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, j;
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);
for (j = 0; i < n && j < m; i++, j++)
return 0;
}
return 0;
}
static int verify_data(
JournalFile *f,
int r;
assert(f);
assert(o);
assert(entry_array_fd >= 0);
/* We already checked this earlier */
assert(n > 0);
if (r < 0)
return r;
i = 1;
while (i < n) {
if (a == 0) {
log_error("Array chain too short at %llu", (unsigned long long) p);
return -EBADMSG;
}
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;
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++) {
if (q <= last) {
log_error("Data object's entry array not sorted at %llu", (unsigned long long) p);
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;
log_error("Invalid data object at hash entry %llu of %llu",
(unsigned long long) i, (unsigned long long) n);
return -EBADMSG;
}
r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
if (r < 0)
return r;
log_error("Hash chain has a cycle in hash entry %llu of %llu",
(unsigned long long) i, (unsigned long long) n);
return -EBADMSG;
}
log_error("Hash value mismatch in hash entry %llu of %llu",
(unsigned long long) i, (unsigned long long) n);
return -EBADMSG;
}
if (r < 0)
return r;
last = p;
p = next;
}
log_error("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;
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;
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,
bool show_progress) {
int r;
assert(f);
assert(entry_array_fd >= 0);
while (i < n) {
Object *o;
if (show_progress)
if (a == 0) {
log_error("Array chain too short at %llu of %llu",
(unsigned long long) i, (unsigned long long) n);
return -EBADMSG;
}
log_error("Invalid array at %llu of %llu",
(unsigned long long) i, (unsigned long long) n);
return -EBADMSG;
}
r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, a, &o);
if (r < 0)
return r;
log_error("Array chain has cycle at %llu of %llu",
(unsigned long long) i, (unsigned long long) n);
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) {
log_error("Entry array not sorted at %llu of %llu",
(unsigned long long) i, (unsigned long long) n);
return -EBADMSG;
}
last = p;
log_error("Invalid array entry at %llu of %llu",
(unsigned long long) i, (unsigned long long) 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;
}
const char *k;
int r;
if (!seed)
return -ENOMEM;
k = key;
for (c = 0; c < seed_size; c++) {
int x, y;
while (*k == '-')
k++;
x = unhexchar(*k);
if (x < 0) {
return -EINVAL;
}
k++;
y = unhexchar(*k);
if (y < 0) {
return -EINVAL;
}
k++;
}
if (*k != '/') {
return -EINVAL;
}
k++;
if (r != 2) {
return -EINVAL;
}
f->fsprg_seed = seed;
f->fsprg_seed_size = seed_size;
f->fss_interval_usec = interval;
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;
char data_path[] = "/var/tmp/journal-data-XXXXXX",
entry_path[] = "/var/tmp/journal-entry-XXXXXX",
unsigned i;
bool found_last;
assert(f);
if (key) {
r = journal_file_parse_verification_key(f, key);
if (r < 0) {
log_error("Failed to parse seed.");
return r;
}
} else if (f->seal)
return -ENOKEY;
if (data_fd < 0) {
log_error("Failed to create data file: %m");
r = -errno;
goto fail;
}
if (entry_fd < 0) {
log_error("Failed to create entry file: %m");
r = -errno;
goto fail;
}
if (entry_array_fd < 0) {
log_error("Failed to create entry array file: %m");
r = -errno;
goto fail;
}
#ifdef HAVE_GCRYPT
#else
if (f->header->compatible_flags != 0)
#endif
{
log_error("Cannot verify file with unknown extensions.");
r = -ENOTSUP;
goto fail;
}
log_error("Reserved field in non-zero.");
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) {
log_error("Invalid object at %llu", (unsigned long long) p);
goto fail;
}
log_error("Invalid tail object pointer");
r = -EBADMSG;
goto fail;
}
found_last = true;
n_objects ++;
r = journal_file_object_verify(f, o);
if (r < 0) {
log_error("Invalid object contents at %llu", (unsigned long long) p);
goto fail;
}
log_error("Compressed object in file without compression at %llu", (unsigned long long) p);
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:
log_error("First entry before first tag at %llu", (unsigned long long) p);
r = -EBADMSG;
goto fail;
}
r = write_uint64(entry_fd, p);
if (r < 0)
goto fail;
log_error("Older entry after newer tag at %llu", (unsigned long long) p);
r = -EBADMSG;
goto fail;
}
if (!entry_seqnum_set &&
log_error("Head entry sequence number incorrect at %llu", (unsigned long long) p);
r = -EBADMSG;
goto fail;
}
if (entry_seqnum_set &&
log_error("Entry sequence number out of synchronization at %llu", (unsigned long long) p);
r = -EBADMSG;
goto fail;
}
entry_seqnum_set = true;
if (entry_monotonic_set &&
log_error("Entry timestamp out of synchronization at %llu", (unsigned long long) p);
r = -EBADMSG;
goto fail;
}
entry_monotonic_set = true;
if (!entry_realtime_set &&
log_error("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) {
log_error("More than one data hash table at %llu", (unsigned long long) p);
r = -EBADMSG;
goto fail;
}
le64toh(f->header->data_hash_table_size) != le64toh(o->object.size) - offsetof(HashTableObject, items)) {
log_error("Header fields for data hash table invalid");
r = -EBADMSG;
goto fail;
}
break;
case OBJECT_FIELD_HASH_TABLE:
if (n_field_hash_tables > 1) {
log_error("More than one field hash table at %llu", (unsigned long long) p);
r = -EBADMSG;
goto fail;
}
le64toh(f->header->field_hash_table_size) != le64toh(o->object.size) - offsetof(HashTableObject, items)) {
log_error("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) {
log_error("More than one main entry array at %llu", (unsigned long long) p);
r = -EBADMSG;
goto fail;
}
found_main_entry_array = true;
}
break;
case OBJECT_TAG: {
if (!JOURNAL_HEADER_SEALED(f->header)) {
log_error("Tag object in file without sealing at %llu", (unsigned long long) p);
r = -EBADMSG;
goto fail;
}
log_error("Tag sequence number out of synchronization at %llu", (unsigned long long) p);
r = -EBADMSG;
goto fail;
}
log_error("Epoch sequence out of synchronization at %llu", (unsigned long long) p);
r = -EBADMSG;
goto fail;
}
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, 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;
log_error("Tag failed verification at %llu", (unsigned long long) p);
r = -EBADMSG;
goto fail;
}
f->hmac_running = false;
}
n_tags ++;
break;
}
default:
n_weird ++;
}
p = 0;
else
}
if (!found_last) {
log_error("Tail object pointer dead");
r = -EBADMSG;
goto fail;
}
log_error("Object number mismatch");
r = -EBADMSG;
goto fail;
}
log_error("Entry number mismatch");
r = -EBADMSG;
goto fail;
}
log_error("Data number mismatch");
r = -EBADMSG;
goto fail;
}
log_error("Field number mismatch");
r = -EBADMSG;
goto fail;
}
log_error("Tag number mismatch");
r = -EBADMSG;
goto fail;
}
log_error("Entry array number mismatch");
r = -EBADMSG;
goto fail;
}
if (n_data_hash_tables != 1) {
log_error("Missing data hash table");
r = -EBADMSG;
goto fail;
}
if (n_field_hash_tables != 1) {
log_error("Missing field hash table");
r = -EBADMSG;
goto fail;
}
if (!found_main_entry_array) {
log_error("Missing entry array");
r = -EBADMSG;
goto fail;
}
if (entry_seqnum_set &&
log_error("Invalid tail seqnum");
r = -EBADMSG;
goto fail;
}
if (entry_monotonic_set &&
log_error("Invalid tail monotonic timestamp");
r = -EBADMSG;
goto fail;
}
log_error("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_validated)
if (last_validated)
if (last_contained)
return 0;
fail:
if (show_progress)
log_error("File corruption detected at %s:%llu (of %llu, %llu%%).",
f->path,
(unsigned long long) p,
if (data_fd >= 0) {
}
if (entry_fd >= 0) {
}
if (entry_array_fd >= 0) {
}
return r;
}