| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-device.h" |
| 4 |
#include "sd-id128.h" |
| 5 |
#include "sd-json.h" |
| 6 |
#include "sd-varlink.h" |
| 7 |
|
| 8 |
#include "alloc-util.h" |
| 9 |
#include "blkid-util.h" |
| 10 |
#include "blockdev-util.h" |
| 11 |
#include "chase.h" |
| 12 |
#include "errno-util.h" |
| 13 |
#include "escape.h" |
| 14 |
#include "fd-util.h" |
| 15 |
#include "hexdecoct.h" |
| 16 |
#include "id128-util.h" |
| 17 |
#include "iovec-util.h" |
| 18 |
#include "log.h" |
| 19 |
#include "mountpoint-util.h" |
| 20 |
#include "pcrextend-util.h" |
| 21 |
#include "pkcs7-util.h" |
| 22 |
#include "sha256.h" |
| 23 |
#include "string-util.h" |
| 24 |
#include "strv.h" |
| 25 |
#include "tpm2-pcr.h" |
| 26 |
#include "user-record.h" |
| 27 |
|
| 28 |
static int device_get_file_system_word( |
| 29 |
sd_device *d, |
| 30 |
const char *prefix, |
| 31 |
char **ret) { |
| 32 |
|
| 33 |
#if HAVE_BLKID |
| 34 |
int r; |
| 35 |
#endif |
| 36 |
|
| 37 |
assert(d); |
| 38 |
assert(prefix); |
| 39 |
assert(ret); |
| 40 |
|
| 41 |
#if HAVE_BLKID |
| 42 |
r = dlopen_libblkid(LOG_DEBUG); |
| 43 |
if (r < 0) |
| 44 |
return r; |
| 45 |
|
| 46 |
_cleanup_close_ int block_fd = sd_device_open(d, O_RDONLY|O_CLOEXEC|O_NONBLOCK); |
| 47 |
if (block_fd < 0) |
| 48 |
return block_fd; |
| 49 |
|
| 50 |
_cleanup_(blkid_free_probep) blkid_probe b = sym_blkid_new_probe(); |
| 51 |
if (!b) |
| 52 |
return -ENOMEM; |
| 53 |
|
| 54 |
errno = 0; |
| 55 |
r = sym_blkid_probe_set_device(b, block_fd, 0, 0); |
| 56 |
if (r != 0) |
| 57 |
return errno_or_else(ENOMEM); |
| 58 |
|
| 59 |
(void) sym_blkid_probe_enable_superblocks(b, 1); |
| 60 |
(void) sym_blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE|BLKID_SUBLKS_UUID|BLKID_SUBLKS_LABEL); |
| 61 |
(void) sym_blkid_probe_enable_partitions(b, 1); |
| 62 |
(void) sym_blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS); |
| 63 |
|
| 64 |
errno = 0; |
| 65 |
r = sym_blkid_do_safeprobe(b); |
| 66 |
if (r == _BLKID_SAFEPROBE_ERROR) |
| 67 |
return errno_or_else(EIO); |
| 68 |
if (IN_SET(r, _BLKID_SAFEPROBE_AMBIGUOUS, _BLKID_SAFEPROBE_NOT_FOUND)) |
| 69 |
return -ENOPKG; |
| 70 |
|
| 71 |
assert(r == _BLKID_SAFEPROBE_FOUND); |
| 72 |
|
| 73 |
_cleanup_strv_free_ char **l = strv_new(prefix); |
| 74 |
if (!l) |
| 75 |
return -ENOMEM; |
| 76 |
|
| 77 |
FOREACH_STRING(field, "TYPE", "UUID", "LABEL", "PART_ENTRY_UUID", "PART_ENTRY_TYPE", "PART_ENTRY_NAME") { |
| 78 |
const char *v = NULL; |
| 79 |
|
| 80 |
(void) sym_blkid_probe_lookup_value(b, field, &v, NULL); |
| 81 |
|
| 82 |
_cleanup_free_ char *escaped = xescape(strempty(v), ":"); /* Avoid ambiguity around ":" */ |
| 83 |
if (!escaped) |
| 84 |
return -ENOMEM; |
| 85 |
|
| 86 |
r = strv_consume(&l, TAKE_PTR(escaped)); |
| 87 |
if (r < 0) |
| 88 |
return r; |
| 89 |
} |
| 90 |
|
| 91 |
assert(strv_length(l) == 7); /* We always want 7 components, to avoid ambiguous strings */ |
| 92 |
|
| 93 |
_cleanup_free_ char *word = strv_join(l, ":"); |
| 94 |
if (!word) |
| 95 |
return -ENOMEM; |
| 96 |
|
| 97 |
*ret = TAKE_PTR(word); |
| 98 |
return 0; |
| 99 |
#else |
| 100 |
return -EOPNOTSUPP; |
| 101 |
#endif |
| 102 |
} |
| 103 |
|
| 104 |
int pcrextend_file_system_word(const char *path, char **ret_word, char **ret_normalized_path) { |
| 105 |
_cleanup_free_ char *normalized_path = NULL, *normalized_escaped = NULL, *prefix = NULL, *word = NULL; |
| 106 |
_cleanup_(sd_device_unrefp) sd_device *d = NULL; |
| 107 |
_cleanup_close_ int dfd = -EBADF; |
| 108 |
int r; |
| 109 |
|
| 110 |
assert(path); |
| 111 |
assert(ret_word); |
| 112 |
|
| 113 |
dfd = chase_and_open(path, NULL, 0, O_DIRECTORY|O_CLOEXEC, &normalized_path); |
| 114 |
if (dfd < 0) |
| 115 |
return log_error_errno(dfd, "Failed to open path '%s': %m", path); |
| 116 |
|
| 117 |
r = is_mount_point_at(dfd, NULL, 0); |
| 118 |
if (r < 0) |
| 119 |
return log_error_errno(r, "Failed to determine if path '%s' is mount point: %m", normalized_path); |
| 120 |
if (r == 0) |
| 121 |
return log_error_errno(SYNTHETIC_ERRNO(ENOTDIR), "Specified path '%s' is not a mount point, refusing.", normalized_path); |
| 122 |
|
| 123 |
normalized_escaped = xescape(normalized_path, ":"); /* Avoid ambiguity around ":" */ |
| 124 |
if (!normalized_escaped) |
| 125 |
return log_oom(); |
| 126 |
|
| 127 |
prefix = strjoin("file-system:", normalized_escaped); |
| 128 |
if (!prefix) |
| 129 |
return log_oom(); |
| 130 |
|
| 131 |
r = block_device_new_from_fd(dfd, BLOCK_DEVICE_LOOKUP_BACKING, &d); |
| 132 |
if (r < 0) { |
| 133 |
log_notice_errno(r, "Unable to determine backing block device of '%s', using generic fallback file system identity string: %m", path); |
| 134 |
|
| 135 |
word = strjoin(prefix, "::::::"); |
| 136 |
if (!word) |
| 137 |
return log_oom(); |
| 138 |
} else { |
| 139 |
r = device_get_file_system_word(d, prefix, &word); |
| 140 |
if (r < 0) |
| 141 |
return log_error_errno(r, "Failed to get file system identifier string for '%s': %m", path); |
| 142 |
} |
| 143 |
|
| 144 |
*ret_word = TAKE_PTR(word); |
| 145 |
|
| 146 |
if (ret_normalized_path) |
| 147 |
*ret_normalized_path = TAKE_PTR(normalized_path); |
| 148 |
|
| 149 |
return 0; |
| 150 |
} |
| 151 |
|
| 152 |
int pcrextend_machine_id_word(char **ret) { |
| 153 |
_cleanup_free_ char *word = NULL; |
| 154 |
sd_id128_t mid; |
| 155 |
int r; |
| 156 |
|
| 157 |
assert(ret); |
| 158 |
|
| 159 |
r = sd_id128_get_machine(&mid); |
| 160 |
if (r < 0) |
| 161 |
return log_error_errno(r, "Failed to acquire machine ID: %m"); |
| 162 |
|
| 163 |
word = strjoin("machine-id:", SD_ID128_TO_STRING(mid)); |
| 164 |
if (!word) |
| 165 |
return log_oom(); |
| 166 |
|
| 167 |
*ret = TAKE_PTR(word); |
| 168 |
return 0; |
| 169 |
} |
| 170 |
|
| 171 |
int pcrextend_product_id_word(char **ret) { |
| 172 |
_cleanup_free_ char *word = NULL; |
| 173 |
sd_id128_t pid; |
| 174 |
int r; |
| 175 |
|
| 176 |
assert(ret); |
| 177 |
|
| 178 |
r = id128_get_product(&pid); |
| 179 |
if (IN_SET(r, -ENOENT, -EADDRNOTAVAIL)) /* No product UUID field, or an all-zero or all-0xFF UUID */ |
| 180 |
word = strdup("product-id:missing"); |
| 181 |
else if (r < 0) |
| 182 |
return log_error_errno(r, "Failed to acquire product ID: %m"); |
| 183 |
else |
| 184 |
word = strjoin("product-id:", SD_ID128_TO_STRING(pid)); |
| 185 |
if (!word) |
| 186 |
return log_oom(); |
| 187 |
|
| 188 |
*ret = TAKE_PTR(word); |
| 189 |
return 0; |
| 190 |
} |
| 191 |
|
| 192 |
int pcrextend_login_word(UserRecord *ur, char **ret) { |
| 193 |
int r; |
| 194 |
|
| 195 |
assert(ur); |
| 196 |
assert(ret); |
| 197 |
|
| 198 |
/* Reduce the user record to the sections that make up its stable, host-specific identity, and turn |
| 199 |
* that into a word to measure into the 'login' NvPCR. We deliberately keep the 'regular', |
| 200 |
* 'perMachine' and 'binding' sections (the portable identity plus how it is realized on *this* |
| 201 |
* host), but drop 'privileged' (so password hash churn doesn't perturb the NvPCR), 'secret' and |
| 202 |
* 'status' (transient) and 'signature' (so the scheme is identical for signed and unsigned records). |
| 203 |
* Only 'regular' is required, the others are merely allowed, so plain NSS users reduce cleanly. */ |
| 204 |
UserRecordLoadFlags mask = |
| 205 |
USER_RECORD_REQUIRE_REGULAR | |
| 206 |
USER_RECORD_ALLOW_PER_MACHINE | |
| 207 |
USER_RECORD_ALLOW_BINDING | |
| 208 |
USER_RECORD_STRIP_PRIVILEGED | |
| 209 |
USER_RECORD_STRIP_SECRET | |
| 210 |
USER_RECORD_STRIP_STATUS | |
| 211 |
USER_RECORD_STRIP_SIGNATURE | |
| 212 |
USER_RECORD_PERMISSIVE; |
| 213 |
|
| 214 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 215 |
r = user_group_record_mangle(ur->json, mask, &v, /* ret_mask= */ NULL); |
| 216 |
if (r < 0) |
| 217 |
return log_error_errno(r, "Failed to reduce user record of '%s': %m", ur->user_name); |
| 218 |
|
| 219 |
/* Normalize so the serialization is canonical (stable key order) regardless of how the record was |
| 220 |
* assembled by the various userdb backends. */ |
| 221 |
r = sd_json_variant_normalize(&v); |
| 222 |
if (r < 0) |
| 223 |
return log_error_errno(r, "Failed to normalize user record of '%s': %m", ur->user_name); |
| 224 |
|
| 225 |
/* Format to compact, single-line JSON (no SD_JSON_FORMAT_NEWLINE), so the measured word stays on one |
| 226 |
* line and the colon-separated word structure is unambiguous. */ |
| 227 |
_cleanup_free_ char *text = NULL; |
| 228 |
r = sd_json_variant_format(v, /* flags= */ 0, &text); |
| 229 |
if (r < 0) |
| 230 |
return log_error_errno(r, "Failed to format user record of '%s': %m", ur->user_name); |
| 231 |
|
| 232 |
_cleanup_free_ char *name_escaped = xescape(ur->user_name, ":"); /* Avoid ambiguity around ":" */ |
| 233 |
if (!name_escaped) |
| 234 |
return log_oom(); |
| 235 |
|
| 236 |
_cleanup_free_ char *word = strjoin("login:", name_escaped, ":", text); |
| 237 |
if (!word) |
| 238 |
return log_oom(); |
| 239 |
|
| 240 |
*ret = TAKE_PTR(word); |
| 241 |
return 0; |
| 242 |
} |
| 243 |
|
| 244 |
int pcrextend_verity_word( |
| 245 |
const char *name, |
| 246 |
const struct iovec *root_hash, |
| 247 |
const struct iovec *root_hash_sig, |
| 248 |
char **ret) { |
| 249 |
|
| 250 |
int r; |
| 251 |
|
| 252 |
assert(name); |
| 253 |
assert(iovec_is_set(root_hash)); |
| 254 |
assert(ret); |
| 255 |
|
| 256 |
_cleanup_free_ char *name_escaped = xescape(name, ":"); /* Avoid ambiguity around ":" */ |
| 257 |
if (!name_escaped) |
| 258 |
return log_oom(); |
| 259 |
|
| 260 |
_cleanup_free_ char *h = hexmem(root_hash->iov_base, root_hash->iov_len); |
| 261 |
if (!h) |
| 262 |
return log_oom(); |
| 263 |
|
| 264 |
_cleanup_free_ char *sigs = NULL; |
| 265 |
if (iovec_is_set(root_hash_sig)) { |
| 266 |
size_t n_signers = 0; |
| 267 |
Signer *signers = NULL; |
| 268 |
|
| 269 |
/* Let's extract the X.509 issuer + serial number from the PKCS#7 signature and include that |
| 270 |
* in the measurement record. This is useful since it allows us to have different signing |
| 271 |
* keys for confext + sysext + other types of DDIs, and by means of this information we can |
| 272 |
* discern which kind it was. Ideally, we'd measure the fingerprint of the X.509 certificate, |
| 273 |
* but typically that's not available in a PKCS#7 signature. */ |
| 274 |
|
| 275 |
CLEANUP_ARRAY(signers, n_signers, signer_free_many); |
| 276 |
|
| 277 |
r = pkcs7_extract_signers(root_hash_sig, &signers, &n_signers); |
| 278 |
if (r < 0) |
| 279 |
return r; |
| 280 |
|
| 281 |
FOREACH_ARRAY(i, signers, n_signers) { |
| 282 |
_cleanup_free_ char *serial = hexmem(i->serial.iov_base, i->serial.iov_len); |
| 283 |
if (!serial) |
| 284 |
return log_oom(); |
| 285 |
|
| 286 |
_cleanup_free_ char *issuer = NULL; |
| 287 |
if (base64mem(i->issuer.iov_base, i->issuer.iov_len, &issuer) < 0) |
| 288 |
return log_oom(); |
| 289 |
|
| 290 |
if (strextendf_with_separator(&sigs, ",", "%s/%s", serial, issuer) < 0) |
| 291 |
return log_oom(); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
_cleanup_free_ char *word = strjoin("verity:", name_escaped, ":", h, ":", strempty(sigs)); |
| 296 |
if (!word) |
| 297 |
return log_oom(); |
| 298 |
|
| 299 |
*ret = TAKE_PTR(word); |
| 300 |
return 0; |
| 301 |
} |
| 302 |
|
| 303 |
int pcrextend_verity_now( |
| 304 |
const char *name, |
| 305 |
const struct iovec *root_hash, |
| 306 |
const struct iovec *root_hash_sig) { |
| 307 |
|
| 308 |
#if HAVE_TPM2 |
| 309 |
int r; |
| 310 |
|
| 311 |
_cleanup_free_ char *word = NULL; |
| 312 |
r = pcrextend_verity_word( |
| 313 |
name, |
| 314 |
root_hash, |
| 315 |
root_hash_sig, |
| 316 |
&word); |
| 317 |
if (r < 0) |
| 318 |
return r; |
| 319 |
|
| 320 |
_cleanup_(sd_varlink_unrefp) sd_varlink *vl = NULL; |
| 321 |
r = sd_varlink_connect_address(&vl, "/run/systemd/io.systemd.PCRExtend"); |
| 322 |
if (r < 0) |
| 323 |
return r; |
| 324 |
|
| 325 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *reply = NULL; |
| 326 |
const char *error_id = NULL; |
| 327 |
r = sd_varlink_callbo( |
| 328 |
vl, |
| 329 |
"io.systemd.PCRExtend.Extend", |
| 330 |
/* ret_reply= */ NULL, |
| 331 |
&error_id, |
| 332 |
SD_JSON_BUILD_PAIR_STRING("nvpcr", "verity"), |
| 333 |
SD_JSON_BUILD_PAIR_STRING("text", word), |
| 334 |
SD_JSON_BUILD_PAIR_STRING("eventType", "dm_verity")); |
| 335 |
if (r < 0) |
| 336 |
return log_debug_errno(r, "Failed to issue io.systemd.PCRExtend.Extend() varlink call: %m"); |
| 337 |
if (error_id) { |
| 338 |
r = sd_varlink_error_to_errno(error_id, reply); |
| 339 |
if (r != -EBADR) |
| 340 |
return log_debug_errno(r, "Failed to issue io.systemd.PCRExtend.Extend() varlink call: %m"); |
| 341 |
|
| 342 |
return log_debug_errno(r, "Failed to issue io.systemd.PCRExtend.Extend() varlink call: %s", error_id); |
| 343 |
} |
| 344 |
|
| 345 |
log_debug("Measurement of '%s' into 'verity' NvPCR completed.", word); |
| 346 |
return 1; |
| 347 |
#else |
| 348 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "TPM2 support disabled, not measuring Verity root hashes and signatures."); |
| 349 |
#endif |
| 350 |
} |
| 351 |
|
| 352 |
#define IMDS_USERDATA_TRUNCATED_MAX 256U |
| 353 |
|
| 354 |
int pcrextend_imds_userdata_word(const struct iovec *data, char **ret) { |
| 355 |
assert(iovec_is_set(data)); |
| 356 |
assert(ret); |
| 357 |
|
| 358 |
/* We include both a hash of the complete user data, and a truncated version of the data in the word |
| 359 |
* we measure. The former protects the actual data, the latter is useful for debugging. */ |
| 360 |
|
| 361 |
_cleanup_free_ char *hash = hexmem(SHA256_DIRECT(data->iov_base, data->iov_len), SHA256_DIGEST_SIZE); |
| 362 |
if (!hash) |
| 363 |
return log_oom(); |
| 364 |
|
| 365 |
_cleanup_free_ char *data_encoded = NULL; |
| 366 |
if (base64mem_full(data->iov_base, MIN(data->iov_len, IMDS_USERDATA_TRUNCATED_MAX), /* line_break= */ SIZE_MAX, &data_encoded) < 0) |
| 367 |
return log_oom(); |
| 368 |
|
| 369 |
_cleanup_free_ char *word = strjoin("imds-userdata:", hash, ":", data_encoded); |
| 370 |
if (!word) |
| 371 |
return log_oom(); |
| 372 |
|
| 373 |
*ret = TAKE_PTR(word); |
| 374 |
return 0; |
| 375 |
} |
| 376 |
|
| 377 |
int pcrextend_imds_userdata_now(const struct iovec *data) { |
| 378 |
|
| 379 |
#if HAVE_TPM2 |
| 380 |
int r; |
| 381 |
|
| 382 |
_cleanup_free_ char *word = NULL; |
| 383 |
r = pcrextend_imds_userdata_word(data, &word); |
| 384 |
if (r < 0) |
| 385 |
return r; |
| 386 |
|
| 387 |
_cleanup_(sd_varlink_unrefp) sd_varlink *vl = NULL; |
| 388 |
r = sd_varlink_connect_address(&vl, "/run/systemd/io.systemd.PCRExtend"); |
| 389 |
if (r < 0) |
| 390 |
return r; |
| 391 |
|
| 392 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *reply = NULL; |
| 393 |
const char *error_id = NULL; |
| 394 |
r = sd_varlink_callbo( |
| 395 |
vl, |
| 396 |
"io.systemd.PCRExtend.Extend", |
| 397 |
/* ret_reply= */ NULL, |
| 398 |
&error_id, |
| 399 |
SD_JSON_BUILD_PAIR_INTEGER("pcr", TPM2_PCR_KERNEL_CONFIG), |
| 400 |
SD_JSON_BUILD_PAIR_STRING("text", word), |
| 401 |
SD_JSON_BUILD_PAIR_STRING("eventType", "imds_userdata")); |
| 402 |
if (r < 0) |
| 403 |
return log_debug_errno(r, "Failed to issue io.systemd.PCRExtend.Extend() varlink call: %m"); |
| 404 |
if (error_id) { |
| 405 |
r = sd_varlink_error_to_errno(error_id, reply); |
| 406 |
if (r != -EBADR) |
| 407 |
return log_debug_errno(r, "Failed to issue io.systemd.PCRExtend.Extend() varlink call: %m"); |
| 408 |
|
| 409 |
return log_debug_errno(r, "Failed to issue io.systemd.PCRExtend.Extend() varlink call: %s", error_id); |
| 410 |
} |
| 411 |
|
| 412 |
log_debug("Measurement of '%s' into PCR 12 completed.", word); |
| 413 |
return 1; |
| 414 |
#else |
| 415 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "TPM2 support disabled, not measuring IMDS userdata."); |
| 416 |
#endif |
| 417 |
} |