Skip to content

Commit 1a8d685

Browse files
committed
initrd/etc/functions: fix TPM counter newline presence/stripping
Tested under QEMU - wipe of /boot/kexec_* - TPM reset + boot default + define default + TPM DUK - remove qemu *.rom files (so keyring injected is unique and triggers TPM unseal error on boot) - Reseal TPMTOTP+HOTP succeeds giving debug output of TPM counter increment succeeding - comparing hashes under /boot/kexec_rollback.txt validates TPM increment works and is validated (rollback is to prevent copying old kexec*.txt + kexec.sig under /boot) Signed-off-by: Thierry Laurion <insurgo@riseup.net>
1 parent 5dcad9e commit 1a8d685

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

initrd/etc/functions

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -828,44 +828,60 @@ prompt_new_owner_password() {
828828
}
829829

830830
check_tpm_counter() {
831+
# $1: rollback file path
831832
TRACE_FUNC
832833

833834
LABEL=${2:-3135106223}
834835
tpm_password="$3"
835836
# if the /boot.hashes file already exists, read the TPM counter ID
836837
# from it.
837838
if [ -r "$1" ]; then
838-
TPM_COUNTER=$(grep counter- "$1" | cut -d- -f2)
839+
# Robustly extract the first hex string after 'counter-' on any line
840+
TPM_COUNTER=$(grep -Eo 'counter-[0-9a-fA-F]+' "$1" | sed -n 's/counter-//p' | head -n1 | tr -d '\n')
841+
DEBUG "Extracted TPM_COUNTER: '$TPM_COUNTER' from $1"
842+
if [ -z "$TPM_COUNTER" ]; then
843+
INFO "$1 exists but no valid TPM counter found; creating new TPM counter"
844+
tpmr counter_create \
845+
-pwdc '' \
846+
-la $LABEL |
847+
tee /tmp/counter >/dev/null 2>&1 ||
848+
die "Unable to create TPM counter"
849+
TPM_COUNTER=$(cut -d: -f1 </tmp/counter | tr -d '\n')
850+
DEBUG "Created new TPM_COUNTER: '$TPM_COUNTER'"
851+
fi
839852
else
840853
INFO "$1 does not exist; creating new TPM counter"
841854
tpmr counter_create \
842855
-pwdc '' \
843856
-la $LABEL |
844857
tee /tmp/counter >/dev/null 2>&1 ||
845858
die "Unable to create TPM counter"
846-
TPM_COUNTER=$(cut -d: -f1 </tmp/counter)
859+
TPM_COUNTER=$(cut -d: -f1 </tmp/counter | tr -d '\n')
860+
DEBUG "Created new TPM_COUNTER: '$TPM_COUNTER'"
847861
fi
848862

849863
if [ -z "$TPM_COUNTER" ]; then
850-
die "$1: TPM Counter not found?"
864+
die "No TPM counter could be found or created."
851865
fi
852866
}
853867

854868
# Read the TPM counter value from the TPM.
855869
read_tpm_counter() {
856870
TRACE_FUNC
857-
if [ ! -e /tmp/counter-"$1" ]; then
858-
DEBUG "Counter file /tmp/counter-$1 not found. Attempting to read from TPM."
859-
DO_WITH_DEBUG tpmr counter_read -ix "$1" | tee /tmp/counter-"$1" >/dev/null 2>&1 ||
860-
die "Counter read failed for index $1"
871+
local counter_id
872+
counter_id="$(echo "$1" | tr -d '\n')"
873+
if [ ! -e /tmp/counter-"$counter_id" ]; then
874+
DEBUG "Counter file /tmp/counter-$counter_id not found. Attempting to read from TPM."
875+
DO_WITH_DEBUG tpmr counter_read -ix "$counter_id" | tee /tmp/counter-"$counter_id" >/dev/null 2>&1 ||
876+
die "Counter read failed for index $counter_id"
861877
fi
862-
DEBUG "Counter file /tmp/counter-$1 read successfully."
878+
DEBUG "Counter file /tmp/counter-$counter_id read successfully."
863879
}
864880

865-
# Increment the TPM counter value in the TPM.
866881
increment_tpm_counter() {
867882
TRACE_FUNC
868-
local counter_id="$1"
883+
local counter_id
884+
counter_id="$(echo "$1" | tr -d '\n')"
869885

870886
# Check if counter exists by reading it first
871887
if ! DO_WITH_DEBUG tpmr counter_read -ix "$counter_id" >/tmp/counter-check 2>/dev/null; then
@@ -883,7 +899,7 @@ increment_tpm_counter() {
883899
DEBUG "TPM counter increment failed. Attempting to create a new counter..."
884900

885901
if DO_WITH_DEBUG tpmr counter_create -pwdc '' -la 3135106223 >/tmp/new-counter 2>/dev/null; then
886-
NEW_COUNTER=$(cut -d: -f1 </tmp/new-counter)
902+
NEW_COUNTER=$(cut -d: -f1 </tmp/new-counter | tr -d '\n')
887903
DEBUG "Created new TPM counter: $NEW_COUNTER. Update kexec_rollback.txt to use this counter."
888904
fi
889905

0 commit comments

Comments
 (0)