Debian Codegraph @

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

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <stdio.h>
5 #include <sys/stat.h>
6 #include <unistd.h>
7
8 #include "ansi-color.h"
9 #include "chattr-util.h"
10 #include "fd-util.h"
11 #include "iovec-util.h"
12 #include "journal-file-util.h"
13 #include "journal-verify.h"
14 #include "log.h"
15 #include "mmap-cache.h"
16 #include "rm-rf.h"
17 #include "tests.h"
18 #include "time-util.h"
19
20 #define N_ENTRIES 6000
21 #define RANDOM_RANGE 77
22
23 static void bit_toggle(const char *fn, uint64_t p) {
24 uint8_t b;
25 int fd;
26
27 ASSERT_OK_ERRNO(fd = open(fn, O_RDWR|O_CLOEXEC));
28
29 ASSERT_EQ(pread(fd, &b, 1, p/8), 1);
30
31 b ^= 1 << (p % 8);
32
33 ASSERT_EQ(pwrite(fd, &b, 1, p/8), 1);
34
35 safe_close(fd);
36 }
37
38 static int raw_verify(const char *fn, const char *verification_key) {
39 _cleanup_(mmap_cache_unrefp) MMapCache *m = NULL;
40 JournalFile *f;
41 int r;
42
43 ASSERT_NOT_NULL(m = mmap_cache_new());
44
45 r = journal_file_open(
46 /* fd= */ -EBADF,
47 fn,
48 O_RDONLY,
49 JOURNAL_COMPRESS|(verification_key ? JOURNAL_SEAL : 0),
50 0666,
51 /* compress_threshold_bytes= */ UINT64_MAX,
52 /* metrics= */ NULL,
53 m,
54 /* template= */ NULL,
55 &f);
56 if (r < 0)
57 return r;
58
59 r = journal_file_verify(f, verification_key, NULL, NULL, NULL, false);
60 (void) journal_file_close(f);
61
62 return r;
63 }
64
65 static int run_test(const char *verification_key, ssize_t max_iterations) {
66 _cleanup_(mmap_cache_unrefp) MMapCache *m = NULL;
67 char t[] = "/var/tmp/journal-XXXXXX";
68 struct stat st;
69 JournalFile *f;
70 JournalFile *df;
71 usec_t from = 0, to = 0, total = 0;
72 uint64_t start, end;
73 int r;
74
75 ASSERT_NOT_NULL(m = mmap_cache_new());
76
77 /* journal_file_open() requires a valid machine id */
78 if (sd_id128_get_machine(NULL) < 0)
79 return log_tests_skipped("No valid machine ID found");
80
81 test_setup_logging(LOG_DEBUG);
82
83 ASSERT_NOT_NULL(mkdtemp(t));
84 ASSERT_OK_ERRNO(chdir(t));
85 (void) chattr_path(t, FS_NOCOW_FL, FS_NOCOW_FL);
86
87 log_info("Generating a test journal");
88
89 ASSERT_OK_ZERO(journal_file_open(
90 /* fd= */ -EBADF,
91 "test.journal",
92 O_RDWR|O_CREAT,
93 JOURNAL_COMPRESS|(verification_key ? JOURNAL_SEAL : 0),
94 0666,
95 /* compress_threshold_bytes= */ UINT64_MAX,
96 /* metrics= */ NULL,
97 m,
98 /* template= */ NULL,
99 &df));
100
101 for (size_t n = 0; n < N_ENTRIES; n++) {
102 _cleanup_free_ char *test = NULL;
103 struct iovec iovec;
104 struct dual_timestamp ts;
105
106 dual_timestamp_now(&ts);
107 ASSERT_OK_ERRNO(asprintf(&test, "RANDOM=%li", random() % RANDOM_RANGE));
108 iovec = IOVEC_MAKE_STRING(test);
109 ASSERT_OK_ZERO(journal_file_append_entry(
110 df,
111 &ts,
112 /* boot_id= */ NULL,
113 &iovec,
114 /* n_iovec= */ 1,
115 /* seqnum= */ NULL,
116 /* seqnum_id= */ NULL,
117 /* ret_object= */ NULL,
118 /* ret_offset= */ NULL));
119 }
120
121 (void) journal_file_offline_close(df);
122
123 log_info("Verifying with key: %s", strna(verification_key));
124
125 ASSERT_OK_ZERO(journal_file_open(
126 /* fd= */ -EBADF,
127 "test.journal",
128 O_RDONLY,
129 JOURNAL_COMPRESS|(verification_key ? JOURNAL_SEAL : 0),
130 0666,
131 /* compress_threshold_bytes= */ UINT64_MAX,
132 /* metrics= */ NULL,
133 m,
134 /* template= */ NULL,
135 &f));
136 journal_file_print_header(f);
137 journal_file_dump(f);
138
139 ASSERT_OK(journal_file_verify(f, verification_key, &from, &to, &total, true));
140
141 if (verification_key && JOURNAL_HEADER_SEALED(f->header))
142 log_info("=> Validated from %s to %s, %s missing",
143 FORMAT_TIMESTAMP(from),
144 FORMAT_TIMESTAMP(to),
145 FORMAT_TIMESPAN(total > to ? total - to : 0, 0));
146
147 (void) journal_file_close(f);
148 ASSERT_OK_ERRNO(stat("test.journal", &st));
149
150 start = 38448 * 8 + 0;
151 end = max_iterations < 0 ? (uint64_t)st.st_size * 8 : start + max_iterations;
152 log_info("Toggling bits %"PRIu64 " to %"PRIu64, start, end);
153
154 for (uint64_t p = start; p < end; p++) {
155 bit_toggle("test.journal", p);
156
157 if (max_iterations < 0)
158 log_info("[ %"PRIu64"+%"PRIu64"]", p / 8, p % 8);
159
160 r = raw_verify("test.journal", verification_key);
161 /* Suppress the notice when running in the limited (CI) mode */
162 if (verification_key && max_iterations < 0 && r >= 0)
163 log_notice(ANSI_HIGHLIGHT_RED ">>>> %"PRIu64" (bit %"PRIu64") can be toggled without detection." ANSI_NORMAL, p / 8, p % 8);
164
165 bit_toggle("test.journal", p);
166 }
167
168 ASSERT_OK(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL));
169
170 return 0;
171 }
172
173 int main(int argc, char *argv[]) {
174 const char *verification_key = NULL;
175 int max_iterations = 512;
176
177 if (argc > 1) {
178 /* Don't limit the number of iterations when the verification key
179 * is provided on the command line, we want to do that only in CIs */
180 verification_key = argv[1];
181 max_iterations = -1;
182 }
183
184 ASSERT_OK_ERRNO(setenv("SYSTEMD_JOURNAL_COMPACT", "0", 1));
185 run_test(verification_key, max_iterations);
186
187 ASSERT_OK_ERRNO(setenv("SYSTEMD_JOURNAL_COMPACT", "1", 1));
188 run_test(verification_key, max_iterations);
189
190 #if HAVE_GCRYPT
191 /* If we're running without any arguments and we're compiled with gcrypt
192 * check the journal verification stuff with a valid key as well */
193 if (argc <= 1) {
194 verification_key = "c262bd-85187f-0b1b04-877cc5/1c7af8-35a4e900";
195
196 ASSERT_OK_ERRNO(setenv("SYSTEMD_JOURNAL_COMPACT", "0", 1));
197 run_test(verification_key, max_iterations);
198
199 ASSERT_OK_ERRNO(setenv("SYSTEMD_JOURNAL_COMPACT", "1", 1));
200 run_test(verification_key, max_iterations);
201 }
202 #endif
203
204 return 0;
205 }