| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "analyze.h" |
| 7 |
#include "analyze-pcrs.h" |
| 8 |
#include "ansi-color.h" |
| 9 |
#include "fileio.h" |
| 10 |
#include "format-table.h" |
| 11 |
#include "hexdecoct.h" |
| 12 |
#include "log.h" |
| 13 |
#include "strv.h" |
| 14 |
#include "tpm2-util.h" |
| 15 |
|
| 16 |
static int get_pcr_alg(const char **ret) { |
| 17 |
assert(ret); |
| 18 |
|
| 19 |
FOREACH_STRING(alg, "sha256", "sha384", "sha1") { |
| 20 |
_cleanup_free_ char *p = NULL; |
| 21 |
|
| 22 |
if (asprintf(&p, "/sys/class/tpm/tpm0/pcr-%s/0", alg) < 0) |
| 23 |
return log_oom(); |
| 24 |
|
| 25 |
if (access(p, F_OK) < 0) { |
| 26 |
if (errno != ENOENT) |
| 27 |
return log_error_errno(errno, "Failed to determine whether %s exists: %m", p); |
| 28 |
} else { |
| 29 |
*ret = alg; |
| 30 |
return 1; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
log_notice("Kernel does not support reading PCR values."); |
| 35 |
*ret = NULL; |
| 36 |
return 0; |
| 37 |
} |
| 38 |
|
| 39 |
static int get_current_pcr(const char *alg, uint32_t pcr, void **ret, size_t *ret_size) { |
| 40 |
_cleanup_free_ char *p = NULL, *s = NULL; |
| 41 |
_cleanup_free_ void *buf = NULL; |
| 42 |
size_t ss = 0, bufsize = 0; |
| 43 |
int r; |
| 44 |
|
| 45 |
assert(alg); |
| 46 |
assert(ret); |
| 47 |
assert(ret_size); |
| 48 |
|
| 49 |
if (asprintf(&p, "/sys/class/tpm/tpm0/pcr-%s/%" PRIu32, alg, pcr) < 0) |
| 50 |
return log_oom(); |
| 51 |
|
| 52 |
r = read_virtual_file(p, 4096, &s, &ss); |
| 53 |
if (r < 0) |
| 54 |
return log_error_errno(r, "Failed to read '%s': %m", p); |
| 55 |
|
| 56 |
r = unhexmem_full(s, ss, /* secure= */ false, &buf, &bufsize); |
| 57 |
if (r < 0) |
| 58 |
return log_error_errno(r, "Failed to decode hex PCR data '%s': %m", s); |
| 59 |
|
| 60 |
*ret = TAKE_PTR(buf); |
| 61 |
*ret_size = bufsize; |
| 62 |
return 0; |
| 63 |
} |
| 64 |
|
| 65 |
static int add_pcr_to_table(Table *table, const char *alg, uint32_t pcr) { |
| 66 |
_cleanup_free_ char *h = NULL; |
| 67 |
const char *color = NULL; |
| 68 |
int r; |
| 69 |
|
| 70 |
if (alg) { |
| 71 |
_cleanup_free_ void *buf = NULL; |
| 72 |
size_t bufsize = 0; |
| 73 |
|
| 74 |
r = get_current_pcr(alg, pcr, &buf, &bufsize); |
| 75 |
if (r < 0) |
| 76 |
return r; |
| 77 |
|
| 78 |
h = hexmem(buf, bufsize); |
| 79 |
if (!h) |
| 80 |
return log_oom(); |
| 81 |
|
| 82 |
/* Grey out PCRs that are not sensibly initialized */ |
| 83 |
if (memeqbyte(0, buf, bufsize) || |
| 84 |
memeqbyte(0xFFU, buf, bufsize)) |
| 85 |
color = ansi_grey(); |
| 86 |
} |
| 87 |
|
| 88 |
r = table_add_many(table, |
| 89 |
TABLE_UINT32, pcr, |
| 90 |
TABLE_STRING, tpm2_pcr_index_to_string(pcr), |
| 91 |
TABLE_STRING, h, |
| 92 |
TABLE_SET_COLOR, color); |
| 93 |
if (r < 0) |
| 94 |
return table_log_add_error(r); |
| 95 |
|
| 96 |
return 0; |
| 97 |
} |
| 98 |
|
| 99 |
int verb_pcrs(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 100 |
_cleanup_(table_unrefp) Table *table = NULL; |
| 101 |
const char *alg = NULL; |
| 102 |
int r; |
| 103 |
|
| 104 |
if (!tpm2_is_mostly_supported()) |
| 105 |
log_notice("System lacks sufficient TPM2 support, not showing PCR state."); |
| 106 |
else { |
| 107 |
r = get_pcr_alg(&alg); |
| 108 |
if (r < 0) |
| 109 |
return r; |
| 110 |
} |
| 111 |
|
| 112 |
table = table_new("nr", "name", alg ?: "-"); |
| 113 |
if (!table) |
| 114 |
return log_oom(); |
| 115 |
|
| 116 |
(void) table_set_align_percent(table, table_get_cell(table, 0, 0), 100); |
| 117 |
table_set_ersatz_string(table, TABLE_ERSATZ_DASH); |
| 118 |
|
| 119 |
if (!alg) /* hide hash column if we couldn't acquire it */ |
| 120 |
(void) table_set_display(table, 0, 1); |
| 121 |
|
| 122 |
char **args = strv_skip(argv, 1); |
| 123 |
if (args) { |
| 124 |
STRV_FOREACH(arg, args) { |
| 125 |
int pi; |
| 126 |
|
| 127 |
pi = tpm2_pcr_index_from_string(*arg); |
| 128 |
if (pi < 0) |
| 129 |
return log_error_errno(pi, "PCR index \"%s\" not known.", *arg); |
| 130 |
|
| 131 |
r = add_pcr_to_table(table, alg, pi); |
| 132 |
if (r < 0) |
| 133 |
return r; |
| 134 |
} |
| 135 |
|
| 136 |
(void) table_set_sort(table, (size_t) 0); |
| 137 |
} else |
| 138 |
for (uint32_t pi = 0; pi < _TPM2_PCR_INDEX_MAX_DEFINED; pi++) { |
| 139 |
r = add_pcr_to_table(table, alg, pi); |
| 140 |
if (r < 0) |
| 141 |
return r; |
| 142 |
} |
| 143 |
|
| 144 |
r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, /* show_header= */ true); |
| 145 |
if (r < 0) |
| 146 |
return r; |
| 147 |
|
| 148 |
return EXIT_SUCCESS; |
| 149 |
} |