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