Debian Codegraph @

src/libsystemd/sd-journal/journal-authenticate.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <sys/mman.h>
5 #include <sys/stat.h>
6
7 #include "alloc-util.h"
8 #include "fd-util.h"
9 #include "fsprg.h"
10 #include "gcrypt-util.h"
11 #include "hexdecoct.h"
12 #include "journal-authenticate.h"
13 #include "journal-def.h"
14 #include "journal-file.h"
15 #include "log.h"
16 #include "memory-util.h"
17 #include "string-util.h"
18 #include "time-util.h"
19
20 static void* fssheader_free(FSSHeader *p) {
21 /* mmap() returns MAP_FAILED on error and sets the errno */
22 if (!p || p == MAP_FAILED)
23 return NULL;
24
25 assert_se(munmap(p, PAGE_ALIGN(sizeof(FSSHeader))) >= 0);
26 return NULL;
27 }
28
29 DEFINE_TRIVIAL_CLEANUP_FUNC(FSSHeader*, fssheader_free);
30
31 #if HAVE_GCRYPT
32 static uint64_t journal_file_tag_seqnum(JournalFile *f) {
33 uint64_t r;
34
35 assert(f);
36
37 r = le64toh(f->header->n_tags) + 1;
38 f->header->n_tags = htole64(r);
39
40 return r;
41 }
42 #endif
43
44 int journal_file_append_tag(JournalFile *f) {
45 #if HAVE_GCRYPT
46 Object *o;
47 uint64_t p;
48 int r;
49
50 assert(f);
51
52 if (!JOURNAL_HEADER_SEALED(f->header))
53 return 0;
54
55 if (!f->hmac_running) {
56 r = journal_file_hmac_start(f);
57 if (r < 0)
58 return r;
59 }
60
61 assert(f->hmac);
62
63 r = journal_file_append_object(f, OBJECT_TAG, sizeof(struct TagObject), &o, &p);
64 if (r < 0)
65 return r;
66
67 o->tag.seqnum = htole64(journal_file_tag_seqnum(f));
68 o->tag.epoch = htole64(FSPRG_GetEpoch(f->fsprg_state));
69
70 log_debug("Writing tag %"PRIu64" for epoch %"PRIu64"",
71 le64toh(o->tag.seqnum),
72 FSPRG_GetEpoch(f->fsprg_state));
73
74 /* Add the tag object itself, so that we can protect its
75 * header. This will exclude the actual hash value in it */
76 r = journal_file_hmac_put_object(f, OBJECT_TAG, o, p);
77 if (r < 0)
78 return r;
79
80 /* Get the HMAC tag and store it in the object */
81 memcpy(o->tag.tag, sym_gcry_md_read(f->hmac, 0), TAG_LENGTH);
82 f->hmac_running = false;
83
84 return 0;
85 #else
86 return -EOPNOTSUPP;
87 #endif
88 }
89
90 int journal_file_hmac_start(JournalFile *f) {
91 #if HAVE_GCRYPT
92 uint8_t key[256 / 8]; /* Let's pass 256 bit from FSPRG to HMAC */
93 gcry_error_t err;
94 int r;
95
96 assert(f);
97
98 if (!JOURNAL_HEADER_SEALED(f->header))
99 return 0;
100
101 if (f->hmac_running)
102 return 0;
103
104 /* Prepare HMAC for next cycle */
105 sym_gcry_md_reset(f->hmac);
106
107 r = FSPRG_GetKey(f->fsprg_state, key, sizeof(key), 0);
108 if (r < 0)
109 return r;
110
111 err = sym_gcry_md_setkey(f->hmac, key, sizeof(key));
112 if (gcry_err_code(err) != GPG_ERR_NO_ERROR)
113 return log_debug_errno(SYNTHETIC_ERRNO(EIO),
114 "sym_gcry_md_setkey() failed with error code: %s",
115 sym_gcry_strerror(err));
116
117 f->hmac_running = true;
118
119 return 0;
120 #else
121 return -EOPNOTSUPP;
122 #endif
123 }
124
125 static int journal_file_get_epoch(JournalFile *f, uint64_t realtime, uint64_t *epoch) {
126 uint64_t t;
127
128 assert(f);
129 assert(epoch);
130 assert(JOURNAL_HEADER_SEALED(f->header));
131
132 if (f->fss_start_usec == 0 || f->fss_interval_usec == 0)
133 return -EOPNOTSUPP;
134
135 if (realtime < f->fss_start_usec)
136 return -ESTALE;
137
138 t = realtime - f->fss_start_usec;
139 t = t / f->fss_interval_usec;
140
141 *epoch = t;
142
143 return 0;
144 }
145
146 static int journal_file_fsprg_need_evolve(JournalFile *f, uint64_t realtime) {
147 uint64_t goal, epoch;
148 int r;
149
150 assert(f);
151
152 if (!JOURNAL_HEADER_SEALED(f->header))
153 return 0;
154
155 r = journal_file_get_epoch(f, realtime, &goal);
156 if (r < 0)
157 return r;
158
159 epoch = FSPRG_GetEpoch(f->fsprg_state);
160 if (epoch > goal)
161 return -ESTALE;
162
163 return epoch != goal;
164 }
165
166 int journal_file_fsprg_evolve(JournalFile *f, uint64_t realtime) {
167 uint64_t goal, epoch;
168 int r;
169
170 assert(f);
171
172 if (!JOURNAL_HEADER_SEALED(f->header))
173 return 0;
174
175 r = journal_file_get_epoch(f, realtime, &goal);
176 if (r < 0)
177 return r;
178
179 epoch = FSPRG_GetEpoch(f->fsprg_state);
180 if (epoch < goal)
181 log_debug("Evolving FSPRG key from epoch %"PRIu64" to %"PRIu64".", epoch, goal);
182
183 for (;;) {
184 if (epoch > goal)
185 return -ESTALE;
186 if (epoch == goal)
187 return 0;
188
189 r = FSPRG_Evolve(f->fsprg_state);
190 if (r < 0)
191 return r;
192
193 epoch = FSPRG_GetEpoch(f->fsprg_state);
194 if (epoch < goal) {
195 r = journal_file_append_tag(f);
196 if (r < 0)
197 return r;
198 }
199 }
200 }
201
202 int journal_file_fsprg_seek(JournalFile *f, uint64_t goal) {
203 void *msk;
204 uint64_t epoch;
205 int r;
206
207 assert(f);
208
209 if (!JOURNAL_HEADER_SEALED(f->header))
210 return 0;
211
212 assert(f->fsprg_seed);
213
214 if (f->fsprg_state) {
215 /* Cheaper... */
216
217 epoch = FSPRG_GetEpoch(f->fsprg_state);
218 if (goal == epoch)
219 return 0;
220
221 if (goal == epoch + 1)
222 return FSPRG_Evolve(f->fsprg_state);
223 } else {
224 f->fsprg_state_size = FSPRG_stateinbytes(FSPRG_RECOMMENDED_SECPAR);
225 f->fsprg_state = malloc(f->fsprg_state_size);
226 if (!f->fsprg_state)
227 return -ENOMEM;
228 }
229
230 log_debug("Seeking FSPRG key to %"PRIu64".", goal);
231
232 msk = alloca_safe(FSPRG_mskinbytes(FSPRG_RECOMMENDED_SECPAR));
233
234 r = FSPRG_GenMK(msk, NULL, f->fsprg_seed, f->fsprg_seed_size, FSPRG_RECOMMENDED_SECPAR);
235 if (r < 0)
236 return r;
237
238 return FSPRG_Seek(f->fsprg_state, goal, msk, f->fsprg_seed, f->fsprg_seed_size);
239 }
240
241 int journal_file_maybe_append_tag(JournalFile *f, uint64_t realtime) {
242 int r;
243
244 assert(f);
245
246 if (!JOURNAL_HEADER_SEALED(f->header))
247 return 0;
248
249 if (realtime <= 0)
250 realtime = now(CLOCK_REALTIME);
251
252 r = journal_file_fsprg_need_evolve(f, realtime);
253 if (r <= 0)
254 return 0;
255
256 r = journal_file_append_tag(f);
257 if (r < 0)
258 return r;
259
260 r = journal_file_fsprg_evolve(f, realtime);
261 if (r < 0)
262 return r;
263
264 return 0;
265 }
266
267 int journal_file_hmac_put_object(JournalFile *f, ObjectType type, Object *o, uint64_t p) {
268 #if HAVE_GCRYPT
269 int r;
270
271 assert(f);
272
273 if (!JOURNAL_HEADER_SEALED(f->header))
274 return 0;
275
276 r = journal_file_hmac_start(f);
277 if (r < 0)
278 return r;
279
280 if (!o) {
281 r = journal_file_move_to_object(f, type, p, &o);
282 if (r < 0)
283 return r;
284 } else if (type > OBJECT_UNUSED && o->object.type != type)
285 return -EBADMSG;
286
287 sym_gcry_md_write(f->hmac, o, offsetof(ObjectHeader, payload));
288
289 switch (o->object.type) {
290
291 case OBJECT_DATA:
292 /* All but hash and payload are mutable */
293 sym_gcry_md_write(f->hmac, &o->data.hash, sizeof(o->data.hash));
294 sym_gcry_md_write(f->hmac, journal_file_data_payload_field(f, o), le64toh(o->object.size) - journal_file_data_payload_offset(f));
295 break;
296
297 case OBJECT_FIELD:
298 /* Same here */
299 sym_gcry_md_write(f->hmac, &o->field.hash, sizeof(o->field.hash));
300 sym_gcry_md_write(f->hmac, o->field.payload, le64toh(o->object.size) - offsetof(Object, field.payload));
301 break;
302
303 case OBJECT_ENTRY:
304 /* All */
305 sym_gcry_md_write(f->hmac, &o->entry.seqnum, le64toh(o->object.size) - offsetof(Object, entry.seqnum));
306 break;
307
308 case OBJECT_FIELD_HASH_TABLE:
309 case OBJECT_DATA_HASH_TABLE:
310 case OBJECT_ENTRY_ARRAY:
311 /* Nothing: everything is mutable */
312 break;
313
314 case OBJECT_TAG:
315 /* All but the tag itself */
316 sym_gcry_md_write(f->hmac, &o->tag.seqnum, sizeof(o->tag.seqnum));
317 sym_gcry_md_write(f->hmac, &o->tag.epoch, sizeof(o->tag.epoch));
318 break;
319 default:
320 return -EINVAL;
321 }
322
323 return 0;
324 #else
325 return -EOPNOTSUPP;
326 #endif
327 }
328
329 int journal_file_hmac_put_header(JournalFile *f) {
330 #if HAVE_GCRYPT
331 int r;
332
333 assert(f);
334
335 if (!JOURNAL_HEADER_SEALED(f->header))
336 return 0;
337
338 r = journal_file_hmac_start(f);
339 if (r < 0)
340 return r;
341
342 /* All but state+reserved, boot_id, arena_size,
343 * tail_object_offset, n_objects, n_entries,
344 * tail_entry_seqnum, head_entry_seqnum, entry_array_offset,
345 * head_entry_realtime, tail_entry_realtime,
346 * tail_entry_monotonic, n_data, n_fields, n_tags,
347 * n_entry_arrays. */
348
349 sym_gcry_md_write(f->hmac, f->header->signature, offsetof(Header, state) - offsetof(Header, signature));
350 sym_gcry_md_write(f->hmac, &f->header->file_id, offsetof(Header, tail_entry_boot_id) - offsetof(Header, file_id));
351 sym_gcry_md_write(f->hmac, &f->header->seqnum_id, offsetof(Header, arena_size) - offsetof(Header, seqnum_id));
352 sym_gcry_md_write(f->hmac, &f->header->data_hash_table_offset, offsetof(Header, tail_object_offset) - offsetof(Header, data_hash_table_offset));
353
354 return 0;
355 #else
356 return -EOPNOTSUPP;
357 #endif
358 }
359
360 int journal_file_fss_load(JournalFile *f) {
361 _cleanup_close_ int fd = -EBADF;
362 _cleanup_free_ char *path = NULL;
363 _cleanup_(fssheader_freep) FSSHeader *header = NULL;
364 struct stat st;
365 sd_id128_t machine;
366 int r;
367
368 assert(f);
369
370 /* This function is used to determine whether sealing should be enabled in the journal header so we
371 * can't check the header to check if sealing is enabled here. */
372
373 r = sd_id128_get_machine(&machine);
374 if (r < 0)
375 return r;
376
377 if (asprintf(&path, "/var/log/journal/" SD_ID128_FORMAT_STR "/fss",
378 SD_ID128_FORMAT_VAL(machine)) < 0)
379 return -ENOMEM;
380
381 fd = open(path, O_RDWR|O_CLOEXEC|O_NOCTTY, 0600);
382 if (fd < 0) {
383 if (errno != ENOENT)
384 log_error_errno(errno, "Failed to open %s: %m", path);
385
386 return -errno;
387 }
388
389 if (fstat(fd, &st) < 0)
390 return -errno;
391
392 if (st.st_size < (off_t) sizeof(FSSHeader))
393 return -ENODATA;
394
395 header = mmap(NULL, PAGE_ALIGN(sizeof(FSSHeader)), PROT_READ, MAP_SHARED, fd, 0);
396 if (header == MAP_FAILED)
397 return -errno;
398
399 if (memcmp(header->signature, FSS_HEADER_SIGNATURE, 8) != 0)
400 return -EBADMSG;
401
402 if (header->incompatible_flags != 0)
403 return -EPROTONOSUPPORT;
404
405 if (le64toh(header->header_size) < sizeof(FSSHeader))
406 return -EBADMSG;
407
408 if (le64toh(header->fsprg_state_size) != FSPRG_stateinbytes(le16toh(header->fsprg_secpar)))
409 return -EBADMSG;
410
411 f->fss_file_size = le64toh(header->header_size) + le64toh(header->fsprg_state_size);
412 if ((uint64_t) st.st_size < f->fss_file_size)
413 return -ENODATA;
414
415 if (!sd_id128_equal(machine, header->machine_id))
416 return -EHOSTDOWN;
417
418 if (le64toh(header->start_usec) <= 0 || le64toh(header->interval_usec) <= 0)
419 return -EBADMSG;
420
421 size_t sz = PAGE_ALIGN(f->fss_file_size);
422 assert(sz < SIZE_MAX);
423 f->fss_file = mmap(NULL, sz, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
424 if (f->fss_file == MAP_FAILED) {
425 f->fss_file = NULL;
426 return -errno;
427 }
428
429 f->fss_start_usec = le64toh(f->fss_file->start_usec);
430 f->fss_interval_usec = le64toh(f->fss_file->interval_usec);
431
432 f->fsprg_state = (uint8_t*) f->fss_file + le64toh(f->fss_file->header_size);
433 f->fsprg_state_size = le64toh(f->fss_file->fsprg_state_size);
434
435 return 0;
436 }
437
438 int journal_file_hmac_setup(JournalFile *f) {
439 #if HAVE_GCRYPT
440 gcry_error_t e;
441 int r;
442
443 if (!JOURNAL_HEADER_SEALED(f->header))
444 return 0;
445
446 r = initialize_libgcrypt(true);
447 if (r < 0)
448 return r;
449
450 e = sym_gcry_md_open(&f->hmac, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
451 if (e != 0)
452 return -EOPNOTSUPP;
453
454 return 0;
455 #else
456 return -EOPNOTSUPP;
457 #endif
458 }
459
460 int journal_file_append_first_tag(JournalFile *f) {
461 uint64_t p;
462 int r;
463
464 if (!JOURNAL_HEADER_SEALED(f->header))
465 return 0;
466
467 log_debug("Calculating first tag...");
468
469 r = journal_file_hmac_put_header(f);
470 if (r < 0)
471 return r;
472
473 p = le64toh(f->header->field_hash_table_offset);
474 if (p < offsetof(Object, hash_table.items))
475 return -EINVAL;
476 p -= offsetof(Object, hash_table.items);
477
478 r = journal_file_hmac_put_object(f, OBJECT_FIELD_HASH_TABLE, NULL, p);
479 if (r < 0)
480 return r;
481
482 p = le64toh(f->header->data_hash_table_offset);
483 if (p < offsetof(Object, hash_table.items))
484 return -EINVAL;
485 p -= offsetof(Object, hash_table.items);
486
487 r = journal_file_hmac_put_object(f, OBJECT_DATA_HASH_TABLE, NULL, p);
488 if (r < 0)
489 return r;
490
491 r = journal_file_append_tag(f);
492 if (r < 0)
493 return r;
494
495 return 0;
496 }
497
498 int journal_file_parse_verification_key(JournalFile *f, const char *key) {
499 _cleanup_free_ uint8_t *seed = NULL;
500 size_t seed_size;
501 const char *k;
502 unsigned long long start, interval;
503 int r;
504
505 assert(f);
506 assert(key);
507
508 seed_size = FSPRG_RECOMMENDED_SEEDLEN;
509 seed = malloc(seed_size);
510 if (!seed)
511 return -ENOMEM;
512
513 k = key;
514 for (size_t c = 0; c < seed_size; c++) {
515 int x, y;
516
517 k = skip_leading_chars(k, "-");
518
519 x = unhexchar(*k);
520 if (x < 0)
521 return -EINVAL;
522 k++;
523
524 y = unhexchar(*k);
525 if (y < 0)
526 return -EINVAL;
527 k++;
528
529 seed[c] = (uint8_t) (x * 16 + y);
530 }
531
532 if (*k != '/')
533 return -EINVAL;
534 k++;
535
536 r = sscanf(k, "%llx-%llx", &start, &interval);
537 if (r != 2)
538 return -EINVAL;
539
540 f->fsprg_seed = TAKE_PTR(seed);
541 f->fsprg_seed_size = seed_size;
542
543 f->fss_start_usec = start * interval;
544 f->fss_interval_usec = interval;
545
546 return 0;
547 }
548
549 bool journal_file_next_evolve_usec(JournalFile *f, usec_t *u) {
550 uint64_t epoch;
551
552 assert(f);
553 assert(u);
554
555 if (!JOURNAL_HEADER_SEALED(f->header))
556 return false;
557
558 epoch = FSPRG_GetEpoch(f->fsprg_state);
559
560 *u = (usec_t) (f->fss_start_usec + f->fss_interval_usec * epoch + f->fss_interval_usec);
561
562 return true;
563 }