Skip to content

Commit 5299266

Browse files
committed
Improve TPM Extend infor in normal and DEBUG mode
cbfs-init: remove temp files, measure direct cbfs output, extend PCR with proper introspection tracing flash.sh: do not die but go to recovery if flashrom fails, cosmetic fix for warning given to user kexec-insert-key: extend PCR with proper introspection tracing kexec-select-boot: extend PCR with proper introspection tracing kexec-measure-luks: extend PCR with proper introspection tracing tpmr: Add missing TRACE_FUNC, fix comments, extend give hash that was extended to tpm call in DEBUG, fix TPM startsession unsuppressed output still present ash_functions: extend PCR with proper introspection tracing insmod: DEBUG info more pertinent, extend PCR with proper introspection tracing Signed-off-by: Thierry Laurion <[email protected]>
1 parent eb88b18 commit 5299266

File tree

8 files changed

+32
-28
lines changed

8 files changed

+32
-28
lines changed

initrd/bin/cbfs-init

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,14 @@ cbfsfiles=`cbfs -t 50 -l 2>/dev/null | grep "^heads/initrd/"`
1515
for cbfsname in `echo $cbfsfiles`; do
1616
filename=${cbfsname:12}
1717
if [ ! -z "$filename" ]; then
18-
echo "Loading $filename from CBFS"
1918
mkdir -p `dirname $filename` \
2019
|| die "$filename: mkdir failed"
21-
cbfs -t 50 -r $cbfsname > "$filename" \
20+
echo "Extracting CBFS file $cbfsname into $filename"
21+
cbfs -t 50 $CBFS_ARG -r $cbfsname > "$filename" \
2222
|| die "$filename: cbfs file read failed"
2323
if [ "$CONFIG_TPM" = "y" ]; then
24-
TMPFILE=/tmp/cbfs.$$
25-
echo "$filename" > $TMPFILE
26-
cat $filename >> $TMPFILE
27-
DEBUG "Extending TPM PCR $CONFIG_PCR with $filename"
28-
tpmr extend -ix "$CONFIG_PCR" -if $TMPFILE \
24+
echo "TPM: Extending PCR[$CONFIG_PCR with] with $filename"
25+
tpmr extend -ix "$CONFIG_PCR" -if $filename \
2926
|| die "$filename: tpm extend failed"
3027
fi
3128
fi

initrd/bin/flash.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ flash_rom() {
4848
dd if=/tmp/pchstrp9.bin bs=1 count=4 seek=292 of=/tmp/${CONFIG_BOARD}.rom conv=notrunc >/dev/null 2>&1
4949
fi
5050

51-
warn "Do not power off computer. Updating firmware, this will take a few minutes..."
51+
warn "Do not power off computer. Updating firmware, this will take a few minutes"
5252
flashrom $CONFIG_FLASHROM_OPTIONS -w /tmp/${CONFIG_BOARD}.rom 2>&1 \
53-
|| die "$ROM: Flash failed"
53+
|| recovery "$ROM: Flash failed"
5454
fi
5555
}
5656

initrd/bin/kexec-insert-key

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ if ! kexec-unseal-key "$INITRD_DIR/secret.key"; then
6565
fi
6666

6767
# Override PCR 4 so that user can't read the key
68-
DEBUG "Extending TPM PCR 4 to prevent further secret unsealing"
68+
echo "TPM: Extending PCR[4] to prevent any future secret unsealing"
6969
tpmr extend -ix 4 -ic generic ||
7070
die 'Unable to scramble PCR'
7171

initrd/bin/kexec-select-boot

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,9 +384,9 @@ while true; do
384384
if [ "$CONFIG_TPM" = "y" ]; then
385385
if [ ! -r "$TMP_KEY_DEVICES" ]; then
386386
# Extend PCR4 as soon as possible
387-
DEBUG "Extending TPM PCR 4 to prevent further secret unsealing"
387+
DEBUG "TPM: Extending PCR[4] to prevent further secret unsealing"
388388
tpmr extend -ix 4 -ic generic ||
389-
die "Failed to extend PCR 4"
389+
die "Failed to extend TPM PCR[4]"
390390
fi
391391
fi
392392

initrd/bin/qubes-measure-luks

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ sha256sum /tmp/lukshdr-* >/tmp/luksDump.txt || die "Unable to hash LUKS headers"
1919
DEBUG "Removing /tmp/lukshdr-*"
2020
rm /tmp/lukshdr-*
2121

22-
DEBUG "Extending TPM PCR 6 with hash of LUKS headers from /tmp/luksDump.txt"
22+
echo "TPM: Extending PCR[6] with hash of LUKS headers from /tmp/luksDump.txt"
2323
tpmr extend -ix 6 -if /tmp/luksDump.txt ||
2424
die "Unable to extend PCR"

initrd/bin/tpmr

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ else
2929
. /etc/config
3030
fi
3131

32-
TRACE_FUNC
3332

3433
# Busybox xxd lacks -r, and we get hex dumps from TPM1 commands. This converts
3534
# a hex dump to binary data using sed and printf
3635
hex2bin() {
36+
TRACE_FUNC
3737
sed 's/\([0-9A-F]\{2\}\)/\\\\\\x\1/gI' | xargs printf
3838
}
3939

@@ -43,6 +43,7 @@ hex2bin() {
4343
# as a file still chokes if the password begins with 'hex:', oddly tpm2-tools
4444
# accepts 'hex:' in the file content.)
4545
tpm2_password_hex() {
46+
TRACE_FUNC
4647
echo "hex:$(echo -n "$1" | xxd -p | tr -d ' \n')"
4748
}
4849

@@ -61,7 +62,7 @@ tpm2_pcrread() {
6162

6263
if [ -z "$APPEND" ]; then
6364
# Don't append - truncate file now so real command always
64-
# appends
65+
# overwrites
6566
true >"$file"
6667
fi
6768

@@ -79,7 +80,7 @@ tpm1_pcrread() {
7980

8081
if [ -z "$APPEND" ]; then
8182
# Don't append - truncate file now so real command always
82-
# appends
83+
# overwrites
8384
true >"$file"
8485
fi
8586

@@ -102,9 +103,10 @@ is_hash() {
102103
# usage:
103104
# extend_pcr_state <alg> <initial_state> <files/hashes...>
104105
# alg - either 'sha1' or 'sha256' to specify algorithm
105-
# initial_state - a hash value setting the initial state
106+
# state - a hash value setting the initial state
106107
# files/hashes... - any number of files or hashes, state is extended once for each item
107108
extend_pcr_state() {
109+
TRACE_FUNC
108110
local alg="$1"
109111
local state="$2"
110112
local next extend
@@ -250,7 +252,8 @@ tpm2_extend() {
250252
esac
251253
done
252254
tpm2 pcrextend "$index:sha256=$hash"
253-
DO_WITH_DEBUG tpm2 pcrread "sha256:$index"
255+
tpm2 pcrread "sha256:$index"
256+
DEBUG "TPM: Extended PCR[$index] with $hash"
254257
}
255258

256259
tpm2_counter_read() {
@@ -348,9 +351,9 @@ tpm2_startsession() {
348351
die "tpm2_flushcontext: unable to flush saved session"
349352
tpm2 readpublic -Q -c "$PRIMARY_HANDLE" -t "$PRIMARY_HANDLE_FILE"
350353
#TODO: do the right thing to not have to suppress "WARN: check public portion the tpmkey manually" see https://github.com/linuxboot/heads/pull/1630#issuecomment-2075120429
351-
tpm2 startauthsession -Q -c "$PRIMARY_HANDLE_FILE" --hmac-session -S "$ENC_SESSION_FILE" 2>&1 > /dev/null
354+
tpm2 startauthsession -Q -c "$PRIMARY_HANDLE_FILE" --hmac-session -S "$ENC_SESSION_FILE" > /dev/null 2>&1
352355
#TODO: do the right thing to not have to suppress "WARN: check public portion the tpmkey manually" see https://github.com/linuxboot/heads/pull/1630#issuecomment-2075120429
353-
tpm2 startauthsession -Q -c "$PRIMARY_HANDLE_FILE" --hmac-session -S "$DEC_SESSION_FILE" 2>&1 > /dev/null
356+
tpm2 startauthsession -Q -c "$PRIMARY_HANDLE_FILE" --hmac-session -S "$DEC_SESSION_FILE" > /dev/null 2>&1
354357
tpm2 sessionconfig -Q --disable-encrypt "$DEC_SESSION_FILE"
355358
}
356359

@@ -381,6 +384,7 @@ cleanup_shred() {
381384
# tpm2_destroy: Destroy a sealed file in the TPM. The mechanism differs by
382385
# TPM version - TPM2 evicts the file object, so it no longer exists.
383386
tpm2_destroy() {
387+
TRACE_FUNC
384388
index="$1" # Index of the sealed file
385389
size="$2" # Size of zeroes to overwrite for TPM1 (unused in TPM2)
386390

@@ -396,6 +400,7 @@ tpm2_destroy() {
396400
# TPM version - TPM1 overwrites the file with zeroes, since this can be done
397401
# without authorization. (Deletion requires authorization.)
398402
tpm1_destroy() {
403+
TRACE_FUNC
399404
index="$1" # Index of the sealed file
400405
size="$2" # Size of zeroes to overwrite for TPM1
401406

@@ -761,6 +766,10 @@ if [ "$CONFIG_TPM2_TOOLS" != "y" ]; then
761766
shift
762767
tpm1_destroy "$@"
763768
;;
769+
extend)
770+
DEBUG "TPM: Extending PCR[$3] with $5"
771+
DO_WITH_DEBUG exec tpm "$@"
772+
;;
764773
seal)
765774
shift
766775
tpm1_seal "$@"
@@ -799,6 +808,7 @@ calcfuturepcr)
799808
replay_pcr "sha256" "$@"
800809
;;
801810
extend)
811+
DEBUG "TPM: Extending PCR[$2] with $4"
802812
tpm2_extend "$@"
803813
;;
804814
counter_read)

initrd/etc/ash_functions

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ recovery() {
241241
DEBUG "Board $CONFIG_BOARD - version $(fw_version)"
242242

243243
if [ "$CONFIG_TPM" = "y" ]; then
244-
DEBUG "Extending TPM PCR 4 for recovery shell access"
244+
echo "TPM: Extending PCR[4] to prevent any further secret unsealing"
245245
tpmr extend -ix 4 -ic recovery
246246
fi
247247

initrd/sbin/insmod

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fi
2828
# Unify lsmod output to use - instead of _ for comparison
2929
module_name=$(basename "$MODULE" | sed 's/_/-/g' | sed 's/\.ko$//')
3030
if lsmod | sed 's/_/-/g' | grep -q "^$module_name\\b"; then
31-
DEBUG "$MODULE: already loaded"
31+
DEBUG "$MODULE: already loaded, skipping"
3232
exit 0
3333
fi
3434

@@ -39,17 +39,14 @@ if [ ! -r /sys/class/tpm/tpm0/pcrs -o ! -x /bin/tpm ]; then
3939
fi
4040

4141
if [ -z "$tpm_missing" ]; then
42-
DEBUG "Extending TPM PCR $MODULE_PCR with $MODULE prior of usage"
42+
echo "TPM: Extending PCR[$MODULE_PCR] with $MODULE prior of loading into kernel"
4343
tpmr extend -ix "$MODULE_PCR" -if "$MODULE" \
4444
|| die "$MODULE: tpm extend failed"
4545
fi
4646

4747
if [ ! -z "$*" -a -z "$tpm_missing" ]; then
48-
DEBUG "Extending TPM PCR $MODULE_PCR with $*"
49-
TMPFILE=/tmp/insmod.$$
50-
echo "$@" > $TMPFILE
51-
DEBUG "Extending TPM PCR $MODULE_PCR with $MODULE prior of usage"
52-
tpmr extend -ix "$MODULE_PCR" -if $TMPFILE \
48+
echo "TPM: Extending PCR[$MODULE_PCR] with $MODULE prior of loading into kernel"
49+
tpmr extend -ix "$MODULE_PCR" -if "$MODULE" \
5350
|| die "$MODULE: tpm extend on arguments failed"
5451
fi
5552

0 commit comments

Comments
 (0)