]>
Commit | Line | Data |
---|---|---|
0284adc6 LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2012 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
cf0fbc49 | 20 | #include <fcntl.h> |
0284adc6 LP |
21 | #include <stdio.h> |
22 | #include <unistd.h> | |
0284adc6 | 23 | |
3ffd4af2 | 24 | #include "fd-util.h" |
0284adc6 LP |
25 | #include "journal-file.h" |
26 | #include "journal-verify.h" | |
3ffd4af2 LP |
27 | #include "log.h" |
28 | #include "rm-rf.h" | |
288a74cc | 29 | #include "terminal-util.h" |
3ffd4af2 | 30 | #include "util.h" |
0284adc6 LP |
31 | |
32 | #define N_ENTRIES 6000 | |
33 | #define RANDOM_RANGE 77 | |
34 | ||
b72631e5 LP |
35 | static void bit_toggle(const char *fn, uint64_t p) { |
36 | uint8_t b; | |
37 | ssize_t r; | |
38 | int fd; | |
39 | ||
40 | fd = open(fn, O_RDWR|O_CLOEXEC); | |
787784c4 | 41 | assert_se(fd >= 0); |
b72631e5 LP |
42 | |
43 | r = pread(fd, &b, 1, p/8); | |
787784c4 | 44 | assert_se(r == 1); |
b72631e5 LP |
45 | |
46 | b ^= 1 << (p % 8); | |
47 | ||
48 | r = pwrite(fd, &b, 1, p/8); | |
787784c4 | 49 | assert_se(r == 1); |
b72631e5 | 50 | |
03e334a1 | 51 | safe_close(fd); |
b72631e5 LP |
52 | } |
53 | ||
54 | static int raw_verify(const char *fn, const char *verification_key) { | |
55 | JournalFile *f; | |
56 | int r; | |
57 | ||
feb12d3e | 58 | r = journal_file_open(fn, O_RDONLY, 0666, true, !!verification_key, NULL, NULL, NULL, &f); |
b72631e5 LP |
59 | if (r < 0) |
60 | return r; | |
61 | ||
62 | r = journal_file_verify(f, verification_key, NULL, NULL, NULL, false); | |
63 | journal_file_close(f); | |
64 | ||
65 | return r; | |
66 | } | |
67 | ||
0284adc6 LP |
68 | int main(int argc, char *argv[]) { |
69 | char t[] = "/tmp/journal-XXXXXX"; | |
70 | unsigned n; | |
71 | JournalFile *f; | |
14d10188 | 72 | const char *verification_key = argv[1]; |
b72631e5 | 73 | usec_t from = 0, to = 0, total = 0; |
6c7be122 LP |
74 | char a[FORMAT_TIMESTAMP_MAX]; |
75 | char b[FORMAT_TIMESTAMP_MAX]; | |
76 | char c[FORMAT_TIMESPAN_MAX]; | |
b72631e5 LP |
77 | struct stat st; |
78 | uint64_t p; | |
0284adc6 | 79 | |
143bfdaf HHPF |
80 | /* journal_file_open requires a valid machine id */ |
81 | if (access("/etc/machine-id", F_OK) != 0) | |
82 | return EXIT_TEST_SKIP; | |
83 | ||
0284adc6 LP |
84 | log_set_max_level(LOG_DEBUG); |
85 | ||
86 | assert_se(mkdtemp(t)); | |
87 | assert_se(chdir(t) >= 0); | |
88 | ||
89 | log_info("Generating..."); | |
90 | ||
14d10188 | 91 | assert_se(journal_file_open("test.journal", O_RDWR|O_CREAT, 0666, true, !!verification_key, NULL, NULL, NULL, &f) == 0); |
0284adc6 LP |
92 | |
93 | for (n = 0; n < N_ENTRIES; n++) { | |
94 | struct iovec iovec; | |
95 | struct dual_timestamp ts; | |
96 | char *test; | |
97 | ||
98 | dual_timestamp_get(&ts); | |
99 | ||
100 | assert_se(asprintf(&test, "RANDOM=%lu", random() % RANDOM_RANGE)); | |
101 | ||
102 | iovec.iov_base = (void*) test; | |
103 | iovec.iov_len = strlen(test); | |
104 | ||
105 | assert_se(journal_file_append_entry(f, &ts, &iovec, 1, NULL, NULL, NULL) == 0); | |
106 | ||
107 | free(test); | |
108 | } | |
109 | ||
110 | journal_file_close(f); | |
111 | ||
112 | log_info("Verifying..."); | |
113 | ||
feb12d3e | 114 | assert_se(journal_file_open("test.journal", O_RDONLY, 0666, true, !!verification_key, NULL, NULL, NULL, &f) == 0); |
f7fab8a5 LP |
115 | /* journal_file_print_header(f); */ |
116 | journal_file_dump(f); | |
3223f44f | 117 | |
b72631e5 | 118 | assert_se(journal_file_verify(f, verification_key, &from, &to, &total, true) >= 0); |
6c7be122 | 119 | |
ece174c5 | 120 | if (verification_key && JOURNAL_HEADER_SEALED(f->header)) |
b72631e5 LP |
121 | log_info("=> Validated from %s to %s, %s missing", |
122 | format_timestamp(a, sizeof(a), from), | |
123 | format_timestamp(b, sizeof(b), to), | |
2fa4092c | 124 | format_timespan(c, sizeof(c), total > to ? total - to : 0, 0)); |
feb12d3e | 125 | |
0284adc6 LP |
126 | journal_file_close(f); |
127 | ||
f7fab8a5 LP |
128 | if (verification_key) { |
129 | log_info("Toggling bits..."); | |
b72631e5 | 130 | |
f7fab8a5 | 131 | assert_se(stat("test.journal", &st) >= 0); |
b72631e5 | 132 | |
f7fab8a5 LP |
133 | for (p = 38448*8+0; p < ((uint64_t) st.st_size * 8); p ++) { |
134 | bit_toggle("test.journal", p); | |
b72631e5 | 135 | |
507f22bd | 136 | log_info("[ %"PRIu64"+%"PRIu64"]", p / 8, p % 8); |
b72631e5 | 137 | |
f7fab8a5 | 138 | if (raw_verify("test.journal", verification_key) >= 0) |
1fc464f6 | 139 | log_notice(ANSI_HIGHLIGHT_RED ">>>> %"PRIu64" (bit %"PRIu64") can be toggled without detection." ANSI_NORMAL, p / 8, p % 8); |
b72631e5 | 140 | |
f7fab8a5 LP |
141 | bit_toggle("test.journal", p); |
142 | } | |
b72631e5 LP |
143 | } |
144 | ||
0284adc6 LP |
145 | log_info("Exiting..."); |
146 | ||
c6878637 | 147 | assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0); |
0284adc6 LP |
148 | |
149 | return 0; | |
150 | } |