HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/libsystemd/sd-journal/journal-verify.c

57,566 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 79061940b399066119d84eb6de1fc3d4a503b117

Lines 1001–1441 of 1,441

                        break;
 
                case OBJECT_FIELD:
                        n_fields++;
                        break;
 
                case OBJECT_ENTRY:
                        if (JOURNAL_HEADER_SEALED(f->header) && n_tags <= 0) {
                                error(p, "First entry before first tag");
                                r = -EBADMSG;
                                goto fail;
                        }
 
                        r = write_uint64(entry_fp, p);
                        if (r < 0)
                                goto fail;
 
                        if (le64toh(o->entry.realtime) < last_tag_realtime) {
                                error(p,
                                      "Older entry after newer tag (%"PRIu64" < %"PRIu64")",
                                      le64toh(o->entry.realtime),
                                      last_tag_realtime);
                                r = -EBADMSG;
                                goto fail;
                        }
 
                        if (!entry_seqnum_set &&
                            le64toh(o->entry.seqnum) != le64toh(f->header->head_entry_seqnum)) {
                                error(p,
                                      "Head entry sequence number incorrect (%"PRIu64" != %"PRIu64")",
                                      le64toh(o->entry.seqnum),
                                      le64toh(f->header->head_entry_seqnum));
                                r = -EBADMSG;
                                goto fail;
                        }
 
                        if (entry_seqnum_set &&
                            entry_seqnum >= le64toh(o->entry.seqnum)) {
                                error(p,
                                      "Entry sequence number out of synchronization (%"PRIu64" >= %"PRIu64")",
                                      entry_seqnum,
                                      le64toh(o->entry.seqnum));
                                r = -EBADMSG;
                                goto fail;
                        }
 
                        entry_seqnum = le64toh(o->entry.seqnum);
                        entry_seqnum_set = true;
 
                        if (entry_monotonic_set &&
                            sd_id128_equal(entry_boot_id, o->entry.boot_id) &&
                            entry_monotonic > le64toh(o->entry.monotonic)) {
                                error(p,
                                      "Entry timestamp out of synchronization (%"PRIu64" > %"PRIu64")",
                                      entry_monotonic,
                                      le64toh(o->entry.monotonic));
                                r = -EBADMSG;
                                goto fail;
                        }
 
                        entry_monotonic = le64toh(o->entry.monotonic);
                        entry_boot_id = o->entry.boot_id;
                        entry_monotonic_set = true;
 
                        if (!entry_realtime_set &&
                            le64toh(o->entry.realtime) != le64toh(f->header->head_entry_realtime)) {
                                error(p,
                                      "Head entry realtime timestamp incorrect (%"PRIu64" != %"PRIu64")",
                                      le64toh(o->entry.realtime),
                                      le64toh(f->header->head_entry_realtime));
                                r = -EBADMSG;
                                goto fail;
                        }
 
                        entry_realtime = le64toh(o->entry.realtime);
                        entry_realtime_set = true;
 
                        max_entry_realtime = MAX(max_entry_realtime, le64toh(o->entry.realtime));
                        min_entry_realtime = MIN(min_entry_realtime, le64toh(o->entry.realtime));
 
                        n_entries++;
                        break;
 
                case OBJECT_DATA_HASH_TABLE:
                        r = verify_hash_table(o, p, &n_data_hash_tables,
                                              le64toh(f->header->data_hash_table_offset),
                                              le64toh(f->header->data_hash_table_size));
                        if (r < 0)
                                goto fail;
                        break;
 
                case OBJECT_FIELD_HASH_TABLE:
                        r = verify_hash_table(o, p, &n_field_hash_tables,
                                              le64toh(f->header->field_hash_table_offset),
                                              le64toh(f->header->field_hash_table_size));
                        if (r < 0)
                                goto fail;
 
                        break;
 
                case OBJECT_ENTRY_ARRAY:
                        r = write_uint64(entry_array_fp, p);
                        if (r < 0)
                                goto fail;
 
                        if (p == le64toh(f->header->entry_array_offset)) {
                                if (found_main_entry_array) {
                                        error(p, "More than one main entry array");
                                        r = -EBADMSG;
                                        goto fail;
                                }
 
                                found_main_entry_array = true;
                        }
 
                        n_entry_arrays++;
                        break;
 
                case OBJECT_TAG:
                        if (!JOURNAL_HEADER_SEALED(f->header)) {
                                error(p, "Tag object in file without sealing");
                                r = -EBADMSG;
                                goto fail;
                        }
 
                        if (le64toh(o->tag.seqnum) != n_tags + 1) {
                                error(p,
                                      "Tag sequence number out of synchronization (%"PRIu64" != %"PRIu64")",
                                      le64toh(o->tag.seqnum),
                                      n_tags + 1);
                                r = -EBADMSG;
                                goto fail;
                        }
 
                        if (JOURNAL_HEADER_SEALED_CONTINUOUS(f->header)) {
                                if (!(n_tags == 0 || (n_tags == 1 && le64toh(o->tag.epoch) == last_epoch)
                                      || le64toh(o->tag.epoch) == last_epoch + 1)) {
                                        error(p,
                                              "Epoch sequence not continuous (%"PRIu64" vs %"PRIu64")",
                                              le64toh(o->tag.epoch),
                                              last_epoch);
                                        r = -EBADMSG;
                                        goto fail;
                                }
                        } else {
                                if (le64toh(o->tag.epoch) < last_epoch) {
                                        error(p,
                                              "Epoch sequence out of synchronization (%"PRIu64" < %"PRIu64")",
                                              le64toh(o->tag.epoch),
                                              last_epoch);
                                        r = -EBADMSG;
                                        goto fail;
                                }
                        }
 
                        if (JOURNAL_HEADER_SEALED(f->header) && journal_auth_supported()) {
                                uint64_t q, rt, rt_end;
 
                                debug(p, "Checking tag %"PRIu64"...", le64toh(o->tag.seqnum));
 
                                r = journal_file_auth_epoch_to_realtime_usec(f, le64toh(o->tag.epoch), &rt, &rt_end);
                                if (r < 0)
                                        goto fail;
 
                                if (entry_realtime_set && entry_realtime >= rt_end) {
                                        error(p,
                                              "tag/entry realtime timestamp out of synchronization (%"PRIu64" >= %"PRIu64")",
                                              entry_realtime,
                                              rt_end);
                                        r = -EBADMSG;
                                        goto fail;
                                }
                                if (max_entry_realtime >= rt_end) {
                                        error(p,
                                              "Entry realtime (%"PRIu64", %s) is too late with respect to tag (%"PRIu64", %s)",
                                              max_entry_realtime, FORMAT_TIMESTAMP(max_entry_realtime),
                                              rt_end, FORMAT_TIMESTAMP(rt_end));
                                        r = -EBADMSG;
                                        goto fail;
                                }
                                if (min_entry_realtime < rt) {
                                        error(p,
                                              "Entry realtime (%"PRIu64", %s) is too early with respect to tag (%"PRIu64", %s)",
                                              min_entry_realtime, FORMAT_TIMESTAMP(min_entry_realtime),
                                              rt, FORMAT_TIMESTAMP(rt));
                                        r = -EBADMSG;
                                        goto fail;
                                }
                                min_entry_realtime = USEC_INFINITY;
 
                                /* OK, now we know the epoch. So let's now set
                                 * it, and calculate the HMAC for everything
                                 * since the last tag. */
                                r = journal_file_auth_seek(f, le64toh(o->tag.epoch));
                                if (r < 0)
                                        goto fail;
 
                                r = journal_file_auth_start(f);
                                if (r < 0)
                                        goto fail;
 
                                if (last_tag == 0) {
                                        r = journal_file_auth_put_header(f);
                                        if (r < 0)
                                                goto fail;
 
                                        q = le64toh(f->header->header_size);
                                } else
                                        q = last_tag;
 
                                while (q <= p) {
                                        r = journal_file_move_to_object(f, OBJECT_UNUSED, q, &o);
                                        if (r < 0)
                                                goto fail;
 
                                        r = journal_file_auth_put_object(f, OBJECT_UNUSED, o, q);
                                        if (r < 0)
                                                goto fail;
 
                                        q = q + ALIGN64(le64toh(o->object.size));
                                }
 
                                /* Position might have changed, let's reposition things */
                                r = journal_file_move_to_object(f, OBJECT_UNUSED, p, &o);
                                if (r < 0)
                                        goto fail;
 
                                uint8_t tag[TAG_LENGTH];
                                CLEANUP_ERASE(tag);
 
                                r = journal_file_auth_end(f, tag);
                                if (r < 0)
                                        goto fail;
 
                                if (memcmp(o->tag.tag, tag, TAG_LENGTH) != 0) {
                                        error(p, "Tag failed verification");
                                        r = -EBADMSG;
                                        goto fail;
                                }
 
                                last_tag_realtime = rt;
                                last_tag_realtime_end = rt_end;
                        }
 
                        last_tag = p + ALIGN64(le64toh(o->object.size));
                        last_epoch = le64toh(o->tag.epoch);
 
                        n_tags++;
                        break;
                }
 
                if (p == le64toh(f->header->tail_object_offset)) {
                        found_last = true;
                        break;
                }
 
                p = p + ALIGN64(le64toh(o->object.size));
        };
 
        if (!found_last && le64toh(f->header->tail_object_offset) != 0) {
                error(le64toh(f->header->tail_object_offset),
                      "Tail object pointer dead (%"PRIu64" != 0)",
                      le64toh(f->header->tail_object_offset));
                r = -EBADMSG;
                goto fail;
        }
 
        if (n_objects != le64toh(f->header->n_objects)) {
                error(offsetof(Header, n_objects),
                      "Object number mismatch (%"PRIu64" != %"PRIu64")",
                      n_objects,
                      le64toh(f->header->n_objects));
                r = -EBADMSG;
                goto fail;
        }
 
        if (n_entries != le64toh(f->header->n_entries)) {
                error(offsetof(Header, n_entries),
                      "Entry number mismatch (%"PRIu64" != %"PRIu64")",
                      n_entries,
                      le64toh(f->header->n_entries));
                r = -EBADMSG;
                goto fail;
        }
 
        if (JOURNAL_HEADER_CONTAINS(f->header, n_data) &&
            n_data != le64toh(f->header->n_data)) {
                error(offsetof(Header, n_data),
                      "Data number mismatch (%"PRIu64" != %"PRIu64")",
                      n_data,
                      le64toh(f->header->n_data));
                r = -EBADMSG;
                goto fail;
        }
 
        if (JOURNAL_HEADER_CONTAINS(f->header, n_fields) &&
            n_fields != le64toh(f->header->n_fields)) {
                error(offsetof(Header, n_fields),
                      "Field number mismatch (%"PRIu64" != %"PRIu64")",
                      n_fields,
                      le64toh(f->header->n_fields));
                r = -EBADMSG;
                goto fail;
        }
 
        if (JOURNAL_HEADER_CONTAINS(f->header, n_tags) &&
            n_tags != le64toh(f->header->n_tags)) {
                error(offsetof(Header, n_tags),
                      "Tag number mismatch (%"PRIu64" != %"PRIu64")",
                      n_tags,
                      le64toh(f->header->n_tags));
                r = -EBADMSG;
                goto fail;
        }
 
        if (JOURNAL_HEADER_CONTAINS(f->header, n_entry_arrays) &&
            n_entry_arrays != le64toh(f->header->n_entry_arrays)) {
                error(offsetof(Header, n_entry_arrays),
                      "Entry array number mismatch (%"PRIu64" != %"PRIu64")",
                      n_entry_arrays,
                      le64toh(f->header->n_entry_arrays));
                r = -EBADMSG;
                goto fail;
        }
 
        if (!found_main_entry_array && le64toh(f->header->entry_array_offset) != 0) {
                error(0, "Missing main entry array");
                r = -EBADMSG;
                goto fail;
        }
 
        if (entry_seqnum_set &&
            entry_seqnum != le64toh(f->header->tail_entry_seqnum)) {
                error(offsetof(Header, tail_entry_seqnum),
                      "Tail entry sequence number incorrect (%"PRIu64" != %"PRIu64")",
                      entry_seqnum,
                      le64toh(f->header->tail_entry_seqnum));
                r = -EBADMSG;
                goto fail;
        }
 
        if (entry_monotonic_set &&
            (sd_id128_equal(entry_boot_id, f->header->tail_entry_boot_id) &&
             JOURNAL_HEADER_TAIL_ENTRY_BOOT_ID(f->header) &&
             entry_monotonic != le64toh(f->header->tail_entry_monotonic))) {
                error(0,
                      "Invalid tail monotonic timestamp (%"PRIu64" != %"PRIu64")",
                      entry_monotonic,
                      le64toh(f->header->tail_entry_monotonic));
                r = -EBADMSG;
                goto fail;
        }
 
        if (entry_realtime_set && entry_realtime != le64toh(f->header->tail_entry_realtime)) {
                error(0,
                      "Invalid tail realtime timestamp (%"PRIu64" != %"PRIu64")",
                      entry_realtime,
                      le64toh(f->header->tail_entry_realtime));
                r = -EBADMSG;
                goto fail;
        }
 
        if (fflush(data_fp) != 0) {
                r = log_error_errno(errno, "Failed to flush data file stream: %m");
                goto fail;
        }
 
        if (fflush(entry_fp) != 0) {
                r = log_error_errno(errno, "Failed to flush entry file stream: %m");
                goto fail;
        }
 
        if (fflush(entry_array_fp) != 0) {
                r = log_error_errno(errno, "Failed to flush entry array file stream: %m");
                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,
                               cache_data_fd, n_data,
                               cache_entry_fd, n_entries,
                               cache_entry_array_fd, n_entry_arrays,
                               &last_usec,
                               show_progress);
        if (r < 0)
                goto fail;
 
        r = verify_data_hash_table(f,
                                   cache_data_fd, n_data,
                                   cache_entry_fd, n_entries,
                                   cache_entry_array_fd, n_entry_arrays,
                                   &last_usec,
                                   show_progress);
        if (r < 0)
                goto fail;
 
        if (show_progress)
                flush_progress();
 
        mmap_cache_fd_free(cache_data_fd);
        mmap_cache_fd_free(cache_entry_fd);
        mmap_cache_fd_free(cache_entry_array_fd);
 
        if (ret_first_contained)
                *ret_first_contained = le64toh(f->header->head_entry_realtime);
        if (ret_last_validated)
                *ret_last_validated = last_tag_realtime_end;
        if (ret_last_contained)
                *ret_last_contained = le64toh(f->header->tail_entry_realtime);
 
        return 0;
 
fail:
        if (show_progress)
                flush_progress();
 
        log_error("File corruption detected at %s:%"PRIu64" (of %"PRIu64" bytes, %"PRIu64"%%).",
                  f->path,
                  p,
                  (uint64_t) f->last_stat.st_size,
                  100U * p / (uint64_t) f->last_stat.st_size);
 
        if (cache_data_fd)
                mmap_cache_fd_free(cache_data_fd);
 
        if (cache_entry_fd)
                mmap_cache_fd_free(cache_entry_fd);
 
        if (cache_entry_array_fd)
                mmap_cache_fd_free(cache_entry_array_fd);
 
        return r;
}