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