journal-verify.c revision fd5dc3204d350142a9105d3e9c83bf29d3a900ee
/*-*- 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"
/* FIXME:
*
* - follow all chains
* - check for unreferenced objects
* - verify FSPRG
* - Allow building without libgcrypt
*
* */
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;
break;
}
case OBJECT_FIELD:
return -EBADMSG;
break;
case OBJECT_ENTRY:
return -EBADMSG;
return -EBADMSG;
return -EBADMSG;
break;
case OBJECT_DATA_HASH_TABLE:
case OBJECT_FIELD_HASH_TABLE:
return -EBADMSG;
break;
case OBJECT_ENTRY_ARRAY:
return -EBADMSG;
break;
case OBJECT_TAG:
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;
if (r < 0)
return r;
if (*z == p)
return 1;
if (p < *z)
b = c;
else
a = c;
}
return 0;
}
int r;
Object *o;
uint64_t p = 0;
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;
char data_path[] = "/var/tmp/journal-data-XXXXXX",
entry_path[] = "/var/tmp/journal-entry-XXXXXX",
assert(f);
if (data_fd < 0) {
log_error("Failed to create data file: %m");
goto fail;
}
if (entry_fd < 0) {
log_error("Failed to create entry file: %m");
goto fail;
}
if (entry_array_fd < 0) {
log_error("Failed to create entry array file: %m");
goto fail;
}
/* First iteration: we go through all objects, verify the
* superficial structure, headers, hashes. */
r = journal_file_hmac_put_header(f);
if (r < 0) {
log_error("Failed to calculate HMAC of header.");
goto fail;
}
while (p != 0) {
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;
}
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 without compression at %llu", (unsigned long long) p);
r = -EBADMSG;
goto fail;
}
r = journal_file_hmac_put_object(f, -1, p);
if (r < 0) {
log_error("Failed to calculate HMAC at %llu", (unsigned long long) p);
goto fail;
}
log_error("Tag object without authentication 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;
}
r = write_uint64(entry_fd, p);
if (r < 0)
goto fail;
if (!entry_seqnum_set &&
log_error("Head entry sequence number incorrect");
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 ++;
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;
}
r = write_uint64(data_fd, p);
if (r < 0)
goto fail;
n_data++;
n_fields++;
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;
}
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;
}
n_weird ++;
p = 0;
else
}
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;
}
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 go through all objects again, this
* time verify all pointers. */
while (p != 0) {
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;
}
uint64_t i = 0, n;
!contains_uint64(f->mmap, entry_array_fd, n_entry_arrays, le64toh(o->entry_array.next_entry_array_offset))) {
log_error("Entry array chains up to invalid next array at %llu", (unsigned long long) p);
r = -EBADMSG;
goto fail;
}
n = journal_file_entry_array_n_items(o);
for (i = 0; i < n; i++) {
log_error("Entry array points to invalid next array at %llu", (unsigned long long) p);
r = -EBADMSG;
goto fail;
}
}
}
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;
}
p = 0;
else
}
return 0;
fail:
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;
}