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