tpm2-util: refuse NvPCR extend when the NV index is gone

tpm2_index_to_handle() returns 0 with a NULL handle when the NV index is
not present on the TPM. tpm2_nvpcr_extend_bytes() only checked for r < 0,
so a tombstoned NvPCR (anchor file present, NV slot cleared out from under
us) passed the NULL handle to tpm2_extend_nvpcr_nv_index() and aborted the
process via its assert(). Handle r == 0 explicitly, as the other
tpm2_index_to_handle() callers already do.

The newly introduced -ENODEV is mapped together with -ENOENT to the
io.systemd.PCRExtend.NoSuchNvPCR varlink error.

Signed-off-by: Paul Meyer <katexochen0@gmail.com>
This commit is contained in:
Paul Meyer
2026-06-23 07:38:32 +02:00
parent de2ec842d1
commit 662e02a5f8
2 changed files with 5 additions and 1 deletions
+1 -1
View File
@@ -465,7 +465,7 @@ static int vl_method_extend(sd_varlink *link, sd_json_variant *parameters, sd_va
if (p.nvpcr) {
r = extend_nvpcr_now(p.nvpcr, extend_iovec->iov_base, extend_iovec->iov_len, p.event_type);
if (r == -ENOENT)
if (IN_SET(r, -ENOENT, -ENODEV))
return sd_varlink_error(link, "io.systemd.PCRExtend.NoSuchNvPCR", NULL);
} else
r = extend_pcr_now(INDEX_TO_MASK(uint32_t, p.pcr), extend_iovec->iov_base, extend_iovec->iov_len, p.event_type);
+4
View File
@@ -7451,6 +7451,10 @@ int tpm2_nvpcr_extend_bytes(
&nv_handle);
if (r < 0)
return log_debug_errno(r, "Failed to acquire handle to NV index 0x%" PRIu32 ".", p.nv_index);
if (r == 0)
return log_debug_errno(SYNTHETIC_ERRNO(ENODEV),
"NvPCR '%s' is anchored but its NV index 0x%" PRIx32 " is no longer present on the TPM, refusing extend.",
name, p.nv_index);
log_debug("Successfully acquired handle to existing NV index 0x%" PRIx32 ".", p.nv_index);