journal-authenticate.c revision 56f64d95763a799ba4475daf44d8e9f72a1bd474
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek This file is part of systemd.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Copyright 2012 Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek under the terms of the GNU Lesser General Public License as published by
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2.1 of the License, or
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek (at your option) any later version.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Lesser General Public License for more details.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek You should have received a copy of the GNU Lesser General Public License
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poetteringstatic uint64_t journal_file_tag_seqnum(JournalFile *f) {
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poetteringint journal_file_append_tag(JournalFile *f) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = journal_file_append_object(f, OBJECT_TAG, sizeof(struct TagObject), &o, &p);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering o->tag.seqnum = htole64(journal_file_tag_seqnum(f));
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering o->tag.epoch = htole64(FSPRG_GetEpoch(f->fsprg_state));
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering log_debug("Writing tag %"PRIu64" for epoch %"PRIu64"",
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering /* Add the tag object itself, so that we can protect its
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering * header. This will exclude the actual hash value in it */
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering r = journal_file_hmac_put_object(f, OBJECT_TAG, o, p);
4a0b58c4a30ecaa61202f845ed86f75b36370cd0Lennart Poettering /* Get the HMAC tag and store it in the object */
8a03c9ef744e13dc700a7e7ca6cae8afdcf0d71cZbigniew Jędrzejewski-Szmek memcpy(o->tag.tag, gcry_md_read(f->hmac, 0), TAG_LENGTH);
7f1ad696a273703789b624fe0b209fb63e953016Lennart Poetteringint journal_file_hmac_start(JournalFile *f) {
e150e82097211f09b911c7784a89ef9efed713caMichał Bartoszkiewicz uint8_t key[256 / 8]; /* Let's pass 256 bit from FSPRG to HMAC */
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering /* Prepare HMAC for next cycle */
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering FSPRG_GetKey(f->fsprg_state, key, sizeof(key), 0);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering gcry_md_setkey(f->hmac, key, sizeof(key));
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringstatic int journal_file_get_epoch(JournalFile *f, uint64_t realtime, uint64_t *epoch) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (realtime < f->fss_start_usec)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int journal_file_fsprg_need_evolve(JournalFile *f, uint64_t realtime) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = journal_file_get_epoch(f, realtime, &goal);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek epoch = FSPRG_GetEpoch(f->fsprg_state);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringint journal_file_fsprg_evolve(JournalFile *f, uint64_t realtime) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = journal_file_get_epoch(f, realtime, &goal);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering log_debug("Evolving FSPRG key from epoch %"PRIu64" to %"PRIu64".", epoch, goal);
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmekint journal_file_fsprg_seek(JournalFile *f, uint64_t goal) {
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek /* Cheaper... */
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek epoch = FSPRG_GetEpoch(f->fsprg_state);
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek f->fsprg_state_size = FSPRG_stateinbytes(FSPRG_RECOMMENDED_SECPAR);
282c5c4e422cb6e6685c870946d8b9bdf0879ad1Zbigniew Jędrzejewski-Szmek f->fsprg_state = malloc(f->fsprg_state_size);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering log_debug("Seeking FSPRG key to %"PRIu64".", goal);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering msk = alloca(FSPRG_mskinbytes(FSPRG_RECOMMENDED_SECPAR));
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering FSPRG_GenMK(msk, NULL, f->fsprg_seed, f->fsprg_seed_size, FSPRG_RECOMMENDED_SECPAR);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering FSPRG_Seek(f->fsprg_state, goal, msk, f->fsprg_seed, f->fsprg_seed_size);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringint journal_file_maybe_append_tag(JournalFile *f, uint64_t realtime) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering r = journal_file_fsprg_need_evolve(f, realtime);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = journal_file_fsprg_evolve(f, realtime);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekint journal_file_hmac_put_object(JournalFile *f, int type, Object *o, uint64_t p) {
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo r = journal_file_move_to_object(f, type, p, &o);
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo gcry_md_write(f->hmac, o, offsetof(ObjectHeader, payload));
089ed40bf4b1df0408c9123f7dfcaa23768668f5Vito Caputo /* All but hash and payload are mutable */
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo gcry_md_write(f->hmac, &o->data.hash, sizeof(o->data.hash));
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo gcry_md_write(f->hmac, o->data.payload, le64toh(o->object.size) - offsetof(DataObject, payload));
e167d7fd8d5fe918b6d675e16aeca2c43398a5b2Lennart Poettering /* Same here */
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo gcry_md_write(f->hmac, &o->field.hash, sizeof(o->field.hash));
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo gcry_md_write(f->hmac, o->field.payload, le64toh(o->object.size) - offsetof(FieldObject, payload));
7a24f3bf2fb181243a1957a0cdd54cd919396793Vito Caputo gcry_md_write(f->hmac, &o->entry.seqnum, le64toh(o->object.size) - offsetof(EntryObject, seqnum));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Nothing: everything is mutable */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* All but the tag itself */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek gcry_md_write(f->hmac, &o->tag.seqnum, sizeof(o->tag.seqnum));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek gcry_md_write(f->hmac, &o->tag.epoch, sizeof(o->tag.epoch));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekint journal_file_hmac_put_header(JournalFile *f) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* All but state+reserved, boot_id, arena_size,
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt * tail_object_offset, n_objects, n_entries,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * tail_entry_seqnum, head_entry_seqnum, entry_array_offset,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * head_entry_realtime, tail_entry_realtime,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * tail_entry_monotonic, n_data, n_fields, n_tags,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * n_entry_arrays. */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek gcry_md_write(f->hmac, f->header->signature, offsetof(Header, state) - offsetof(Header, signature));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek gcry_md_write(f->hmac, &f->header->file_id, offsetof(Header, boot_id) - offsetof(Header, file_id));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek gcry_md_write(f->hmac, &f->header->seqnum_id, offsetof(Header, arena_size) - offsetof(Header, seqnum_id));
5c3bde3fa8613e09e694198862ea9038566af422Zbigniew Jędrzejewski-Szmek gcry_md_write(f->hmac, &f->header->data_hash_table_offset, offsetof(Header, tail_object_offset) - offsetof(Header, data_hash_table_offset));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekint journal_file_fss_load(JournalFile *f) {
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (asprintf(&p, "/var/log/journal/" SD_ID128_FORMAT_STR "/fss",
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek SD_ID128_FORMAT_VAL(machine)) < 0)
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek fd = open(p, O_RDWR|O_CLOEXEC|O_NOCTTY, 0600);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek log_error_errno(errno, "Failed to open %s: %m", p);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (st.st_size < (off_t) sizeof(FSSHeader)) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering m = mmap(NULL, PAGE_ALIGN(sizeof(FSSHeader)), PROT_READ, MAP_SHARED, fd, 0);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (memcmp(m->signature, FSS_HEADER_SIGNATURE, 8) != 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (m->incompatible_flags != 0) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (le64toh(m->header_size) < sizeof(FSSHeader)) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (le64toh(m->fsprg_state_size) != FSPRG_stateinbytes(le16toh(m->fsprg_secpar))) {
65089b82401cd395786a7987c470056ff3f01151Lennart Poettering f->fss_file_size = le64toh(m->header_size) + le64toh(m->fsprg_state_size);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if ((uint64_t) st.st_size < f->fss_file_size) {
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt if (!sd_id128_equal(machine, m->machine_id)) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering f->fss_file = mmap(NULL, PAGE_ALIGN(f->fss_file_size), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek f->fss_start_usec = le64toh(f->fss_file->start_usec);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering f->fss_interval_usec = le64toh(f->fss_file->interval_usec);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering f->fsprg_state = (uint8_t*) f->fss_file + le64toh(f->fss_file->header_size);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering f->fsprg_state_size = le64toh(f->fss_file->fsprg_state_size);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering munmap(m, PAGE_ALIGN(sizeof(FSSHeader)));
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmekstatic void initialize_libgcrypt(void) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering const char *p;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (gcry_control(GCRYCTL_INITIALIZATION_FINISHED_P))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek p = gcry_check_version("1.4.5");
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringint journal_file_hmac_setup(JournalFile *f) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering e = gcry_md_open(&f->hmac, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poetteringint journal_file_append_first_tag(JournalFile *f) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering p = le64toh(f->header->field_hash_table_offset);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (p < offsetof(Object, hash_table.items))
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering r = journal_file_hmac_put_object(f, OBJECT_FIELD_HASH_TABLE, NULL, p);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering p = le64toh(f->header->data_hash_table_offset);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (p < offsetof(Object, hash_table.items))
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering r = journal_file_hmac_put_object(f, OBJECT_DATA_HASH_TABLE, NULL, p);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekint journal_file_parse_verification_key(JournalFile *f, const char *key) {
fa6ac76083b8ffc1309876459f54f9f0e2843731Lennart Poettering const char *k;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek unsigned long long start, interval;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek seed_size = FSPRG_RECOMMENDED_SEEDLEN;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek for (c = 0; c < seed_size; c++) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek while (*k == '-')
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (*k != '/') {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sscanf(k, "%llx-%llx", &start, &interval);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek f->fss_start_usec = start * interval;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek f->fss_interval_usec = interval;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekbool journal_file_next_evolve_usec(JournalFile *f, usec_t *u) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek epoch = FSPRG_GetEpoch(f->fsprg_state);