]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
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 |
21 | static 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 | ||
40 | static int raw_verify(const char *fn, const char *verification_key) { | |
41 | JournalFile *f; | |
42 | int r; | |
43 | ||
f5fbe71d | 44 | r = journal_file_open(-1, fn, O_RDONLY, 0666, true, UINT64_MAX, !!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 | 54 | int 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; |
b72631e5 LP |
60 | struct stat st; |
61 | uint64_t p; | |
0284adc6 | 62 | |
143bfdaf | 63 | /* journal_file_open requires a valid machine id */ |
317bb217 ZJS |
64 | if (access("/etc/machine-id", F_OK) != 0) |
65 | return log_tests_skipped("/etc/machine-id not found"); | |
143bfdaf | 66 | |
6d7c4033 | 67 | test_setup_logging(LOG_DEBUG); |
0284adc6 LP |
68 | |
69 | assert_se(mkdtemp(t)); | |
70 | assert_se(chdir(t) >= 0); | |
949082ac | 71 | (void) chattr_path(t, FS_NOCOW_FL, FS_NOCOW_FL, NULL); |
0284adc6 LP |
72 | |
73 | log_info("Generating..."); | |
74 | ||
f5fbe71d | 75 | assert_se(journal_file_open(-1, "test.journal", O_RDWR|O_CREAT, 0666, true, UINT64_MAX, !!verification_key, NULL, NULL, NULL, NULL, &f) == 0); |
0284adc6 LP |
76 | |
77 | for (n = 0; n < N_ENTRIES; n++) { | |
78 | struct iovec iovec; | |
79 | struct dual_timestamp ts; | |
80 | char *test; | |
81 | ||
82 | dual_timestamp_get(&ts); | |
83 | ||
84 | assert_se(asprintf(&test, "RANDOM=%lu", random() % RANDOM_RANGE)); | |
85 | ||
5cfa2c3d | 86 | iovec = IOVEC_MAKE_STRING(test); |
0284adc6 | 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 | ||
f5fbe71d | 97 | assert_se(journal_file_open(-1, "test.journal", O_RDONLY, 0666, true, UINT64_MAX, !!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 | 104 | log_info("=> Validated from %s to %s, %s missing", |
04f5c018 ZJS |
105 | FORMAT_TIMESTAMP(from), |
106 | FORMAT_TIMESTAMP(to), | |
5291f26d | 107 | FORMAT_TIMESPAN(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 | } |