]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/test-journal-verify.c
Merge pull request #10902 from poettering/highlight-status
[thirdparty/systemd.git] / src / journal / test-journal-verify.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <fcntl.h>
4 #include <stdio.h>
5 #include <unistd.h>
6
7 #include "fd-util.h"
8 #include "io-util.h"
9 #include "journal-file.h"
10 #include "journal-verify.h"
11 #include "log.h"
12 #include "rm-rf.h"
13 #include "terminal-util.h"
14 #include "tests.h"
15 #include "util.h"
16
17 #define N_ENTRIES 6000
18 #define RANDOM_RANGE 77
19
20 static void bit_toggle(const char *fn, uint64_t p) {
21 uint8_t b;
22 ssize_t r;
23 int fd;
24
25 fd = open(fn, O_RDWR|O_CLOEXEC);
26 assert_se(fd >= 0);
27
28 r = pread(fd, &b, 1, p/8);
29 assert_se(r == 1);
30
31 b ^= 1 << (p % 8);
32
33 r = pwrite(fd, &b, 1, p/8);
34 assert_se(r == 1);
35
36 safe_close(fd);
37 }
38
39 static int raw_verify(const char *fn, const char *verification_key) {
40 JournalFile *f;
41 int r;
42
43 r = journal_file_open(-1, fn, O_RDONLY, 0666, true, (uint64_t) -1, !!verification_key, NULL, NULL, NULL, NULL, &f);
44 if (r < 0)
45 return r;
46
47 r = journal_file_verify(f, verification_key, NULL, NULL, NULL, false);
48 (void) journal_file_close(f);
49
50 return r;
51 }
52
53 int main(int argc, char *argv[]) {
54 char t[] = "/tmp/journal-XXXXXX";
55 unsigned n;
56 JournalFile *f;
57 const char *verification_key = argv[1];
58 usec_t from = 0, to = 0, total = 0;
59 char a[FORMAT_TIMESTAMP_MAX];
60 char b[FORMAT_TIMESTAMP_MAX];
61 char c[FORMAT_TIMESPAN_MAX];
62 struct stat st;
63 uint64_t p;
64
65 /* journal_file_open requires a valid machine id */
66 if (access("/etc/machine-id", F_OK) != 0)
67 return log_tests_skipped("/etc/machine-id not found");
68
69 test_setup_logging(LOG_DEBUG);
70
71 assert_se(mkdtemp(t));
72 assert_se(chdir(t) >= 0);
73
74 log_info("Generating...");
75
76 assert_se(journal_file_open(-1, "test.journal", O_RDWR|O_CREAT, 0666, true, (uint64_t) -1, !!verification_key, NULL, NULL, NULL, NULL, &f) == 0);
77
78 for (n = 0; n < N_ENTRIES; n++) {
79 struct iovec iovec;
80 struct dual_timestamp ts;
81 char *test;
82
83 dual_timestamp_get(&ts);
84
85 assert_se(asprintf(&test, "RANDOM=%lu", random() % RANDOM_RANGE));
86
87 iovec = IOVEC_MAKE_STRING(test);
88
89 assert_se(journal_file_append_entry(f, &ts, NULL, &iovec, 1, NULL, NULL, NULL) == 0);
90
91 free(test);
92 }
93
94 (void) journal_file_close(f);
95
96 log_info("Verifying...");
97
98 assert_se(journal_file_open(-1, "test.journal", O_RDONLY, 0666, true, (uint64_t) -1, !!verification_key, NULL, NULL, NULL, NULL, &f) == 0);
99 /* journal_file_print_header(f); */
100 journal_file_dump(f);
101
102 assert_se(journal_file_verify(f, verification_key, &from, &to, &total, true) >= 0);
103
104 if (verification_key && JOURNAL_HEADER_SEALED(f->header))
105 log_info("=> Validated from %s to %s, %s missing",
106 format_timestamp(a, sizeof(a), from),
107 format_timestamp(b, sizeof(b), to),
108 format_timespan(c, sizeof(c), total > to ? total - to : 0, 0));
109
110 (void) journal_file_close(f);
111
112 if (verification_key) {
113 log_info("Toggling bits...");
114
115 assert_se(stat("test.journal", &st) >= 0);
116
117 for (p = 38448*8+0; p < ((uint64_t) st.st_size * 8); p ++) {
118 bit_toggle("test.journal", p);
119
120 log_info("[ %"PRIu64"+%"PRIu64"]", p / 8, p % 8);
121
122 if (raw_verify("test.journal", verification_key) >= 0)
123 log_notice(ANSI_HIGHLIGHT_RED ">>>> %"PRIu64" (bit %"PRIu64") can be toggled without detection." ANSI_NORMAL, p / 8, p % 8);
124
125 bit_toggle("test.journal", p);
126 }
127 }
128
129 log_info("Exiting...");
130
131 assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
132
133 return 0;
134 }