Skip to contentSkip to conversation

initramfs-crypt-hook: emit TPM DA lockout metric

PATCH New Sercan Sari 1 participant 1 message Sep 30

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 initramfs-crypt-hook: emit TPM DA lockout metric

The TPM dictionary-attack (DA) lockout counter increments on unclean shutdowns and failed unlock attempts, and eventually locks the TPM. Its trajectory cannot be observed from the running system without querying the TPM again.

After each unlock attempt, when the lockout counter is non-zero, emit the DA counters (lockoutCounter, maxAuthFail, lockoutInterval, inLockout) as a single 'tpm-da:' key=value line to /dev/kmsg, so a rootfs service can track them via journald without touching the TPM. The metric is emitted on both the success and the failure path, so a lockout is still reported before the hook panics.

---
Changes since v1:
- Emit only when the lockout counter is non-zero, to keep the healthy case silent.
- Emit at most once per TPM device instead of once per unlocked partition.
- Pass the capability dump to tpm2_da_prop as an argument instead of a global, and fold the former tpm2_read_da helper into the caller.

 .../files/encrypt_partition.clevis.script     |  2 ++
 .../files/encrypt_partition.systemd.script    |  2 ++
 .../initramfs-crypt-hook/files/tpm2_functions | 21 +++++++++++++++++++
 .../files/tpm2_functions.buster               |  5 +++++
 4 files changed, 30 insertions(+)
recipes-initramfs/initramfs-crypt-hook/files/encrypt_partition.clevis.script +2 -0
index ceeacd1..e473dfb 100644
@@ -16,8 +16,10 @@ open_tpm2_partition() {
 	tpm_device="$3"
 	if ! TPM2TOOLS_TCTI="device:$tpm_device" usr/bin/clevis luks unlock -n "$crypt_mount_name" \
 		 -d "$partition_device"; then
+		tpm2_report_da_metric
 		panic "Can't decrypt '$partition_device' !"
 	fi
+	tpm2_report_da_metric
 }
 
 enroll_tpm2_token() {
recipes-initramfs/initramfs-crypt-hook/files/encrypt_partition.systemd.script +2 -0
index 94346a8..40eeb4d 100644
@@ -15,8 +15,10 @@ open_tpm2_partition() {
 	tpm_device="$3"
 	if ! /usr/lib/systemd/systemd-cryptsetup attach "$crypt_mount_name" \
 		 "$partition_device" - tpm2-device="$tpm_device"; then
+		tpm2_report_da_metric
 		panic "Can't decrypt '$partition_device' !"
 	fi
+	tpm2_report_da_metric
 }
 
 enroll_tpm2_token() {
recipes-initramfs/initramfs-crypt-hook/files/tpm2_functions +21 -0
index 1c6bbff..d88da5b 100644
@@ -65,3 +65,24 @@ tpm2_key_get() {
 tpm2_key_del() {
 	tpm2_evictcontrol -C o -c "$TPM2_HANDLE"
 }
+
+# Extract a TPM2_PT_<name> property value from the capability dump in $2
+tpm2_da_prop() {
+	echo "$2" | awk -v k="TPM2_PT_$1:" '
+		$1 == k { if (NF > 1) { print $NF; exit } f = 1; next }
+		f && /0x/ { print $NF; exit }'
+}
+
+# Emit the DA counters to the kernel log when the lockout counter is non-zero, once per TPM device.
+tpm2_report_da_metric() {
+	case " $tpm2_da_reported " in *" $tpm_device "*) return ;; esac
+	da="$(tpm2_getcap -T "device:${tpm_device}" properties-variable 2>/dev/null)"
+	da_counter="$(tpm2_da_prop LOCKOUT_COUNTER "$da")"
+	case "$da_counter" in ""|0x0|0) return ;; esac
+	tpm2_da_reported="$tpm2_da_reported $tpm_device"
+	da_maxfail="$(tpm2_da_prop MAX_AUTH_FAIL "$da")"
+	da_interval="$(tpm2_da_prop LOCKOUT_INTERVAL "$da")"
+	da_inlockout="$(echo "$da" | sed -n 's/.*inLockout:[[:space:]]*\([0-9]\).*/\1/p')"
+	echo "tpm-da: counter=${da_counter} maxfail=${da_maxfail:-?} interval=${da_interval:-?} inlockout=${da_inlockout:-?}" \
+		> /dev/kmsg 2>/dev/null || true
+}
recipes-initramfs/initramfs-crypt-hook/files/tpm2_functions.buster +5 -0
index 453ba97..09338ef 100644
@@ -22,3 +22,8 @@ tpm2_key_get() {
 
 tpm2_key_del() {
 }
+
+# tpm2_getcap is not in the buster initramfs, so no DA metric is emitted.
+tpm2_report_da_metric() {
+	:
+}
Signature · 1 line
2.39.5