Debian Codegraph @

src/analyze/analyze-nvpcrs.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "analyze-nvpcrs.h"
5 #include "analyze.h"
6 #include "conf-files.h"
7 #include "constants.h"
8 #include "format-table.h"
9 #include "hexdecoct.h"
10 #include "strv.h"
11 #include "tpm2-util.h"
12
13 #if HAVE_TPM2
14 static int add_nvpcr_to_table(Tpm2Context **c, Table *t, const char *name) {
15 int r;
16
17 _cleanup_free_ char *h = NULL;
18 uint32_t nv_index = 0;
19 uint64_t priority = 0;
20 if (c) {
21 if (!*c) {
22 r = tpm2_context_new_or_warn(/* device= */ NULL, c);
23 if (r < 0)
24 return r;
25 }
26
27 _cleanup_(iovec_done) struct iovec digest = {};
28 r = tpm2_nvpcr_read(*c, /* session= */ NULL, name, &digest, &nv_index, &priority);
29 if (r < 0)
30 return log_error_errno(r, "Failed to read NvPCR '%s': %m", name);
31 if (r > 0) { /* set? */
32 h = hexmem(digest.iov_base, digest.iov_len);
33 if (!h)
34 return log_oom();
35 }
36 } else {
37 r = tpm2_nvpcr_get_index(name, &nv_index, &priority);
38 if (r < 0)
39 return log_error_errno(r, "Failed to get NV index of NvPCR '%s': %m", name);
40 }
41
42 r = table_add_many(
43 t,
44 TABLE_STRING, name,
45 TABLE_UINT32_HEX_0x, nv_index,
46 TABLE_UINT64, priority,
47 TABLE_STRING, h);
48 if (r < 0)
49 return table_log_add_error(r);
50
51 return 0;
52 }
53 #endif
54
55 int verb_nvpcrs(int argc, char *argv[], uintptr_t _data, void *userdata) {
56 #if HAVE_TPM2
57 _cleanup_(tpm2_context_unrefp) Tpm2Context *c = NULL;
58 _cleanup_(table_unrefp) Table *table = NULL;
59 int r;
60
61 bool have_tpm2 = tpm2_is_mostly_supported();
62
63 if (!have_tpm2)
64 log_notice("System lacks full TPM2 support, not showing NvPCR state.");
65
66 table = table_new("name", "nvindex", "priority", "value");
67 if (!table)
68 return log_oom();
69
70 (void) table_set_align_percent(table, table_get_cell(table, 0, 1), 100);
71 (void) table_set_align_percent(table, table_get_cell(table, 0, 2), 100);
72 table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
73 (void) table_set_sort(table, (size_t) 2, (size_t) 0);
74
75 if (!have_tpm2)
76 (void) table_hide_column_from_display(table, (size_t) 3);
77
78 if (strv_isempty(strv_skip(argv, 1))) {
79 _cleanup_strv_free_ char **l = NULL;
80 r = conf_files_list_nulstr(
81 &l,
82 ".nvpcr",
83 /* root= */ NULL,
84 CONF_FILES_REGULAR|CONF_FILES_BASENAME|CONF_FILES_FILTER_MASKED|CONF_FILES_TRUNCATE_SUFFIX|CONF_FILES_WARN,
85 CONF_PATHS_NULSTR("nvpcr"));
86 if (r < 0)
87 return log_error_errno(r, "Failed to find .nvpcr files: %m");
88
89 STRV_FOREACH(i, l) {
90 r = add_nvpcr_to_table(have_tpm2 ? &c : NULL, table, *i);
91 if (r < 0)
92 return r;
93 }
94 } else
95 for (int i = 1; i < argc; i++) {
96 r = add_nvpcr_to_table(have_tpm2 ? &c : NULL, table, argv[i]);
97 if (r < 0)
98 return r;
99 }
100
101 if (table_isempty(table) && FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF))
102 log_notice("No NvPCRs defined.");
103 else {
104 r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, /* show_header= */ true);
105 if (r < 0)
106 return log_error_errno(r, "Failed to output table: %m");
107 }
108
109 return EXIT_SUCCESS;
110 #else
111 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "TPM2 support not enabled at build time.");
112 #endif
113 }