Skip to content

Commit 07e3bf7

Browse files
committed
WiP tpmr: add calc_pcr to simulate PCR content from reset state, measuring files/strings to arrive to same cbmem -L (TCPA/Event log content from real measured stuff)
Traces: When looking at TCPA log/TPM Event log: ~ # cbmem -L coreboot TPM log: PCR-2 5622416ea417186aa1ac32b32c527ac09009fb5e SHA1 [FMAP: FMAP] PCR-2 8bbaeca78eb7e169df69d3687258318b58c8671e SHA1 [CBFS: bootblock] PCR-2 73ccefadc0a1be8184be89800e69186a260ebe40 SHA1 [CBFS: fallback/romstage] PCR-2 d697f8c98ef6f1b4aca397821e176bb48a227212 SHA1 [CBFS: fallback/postcar] PCR-2 b88302e3a46fb7fb11b92730d05c41b5f1f11bcf SHA1 [CBFS: fallback/ramstage] PCR-2 b688d567b0dfe1e1c6e4584289619a525b85cbd6 SHA1 [CBFS: bootsplash.jpg] PCR-2 9130eeb4cfe031edeabc56e6b84812d49a5a6bda SHA1 [CBFS: fallback/payload] We see that PCR2 is extended from reset state (40*0) with: FMAP (only one not under cbfs), bootblock, fallback/romstage, fallback/postcar, fallback/ramstage, bootsplash.jpg, fallback/payload cbmem permits to extract FMAP from cbmem: ~ # cbmem --rawdump $(cbmem -l | grep FMAP | awk -F " " {'print $3'}) | xxd 00000000: 5f5f 464d 4150 5f5f 0101 0000 60ff 0000 __FMAP__....`... 00000010: 0000 0000 a000 464c 4153 4800 0000 0000 ......FLASH..... 00000020: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 00000030: 0000 0000 0000 0300 0000 0800 0000 9800 ................ 00000040: 4249 4f53 0000 0000 0000 0000 0000 0000 BIOS............ 00000050: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 00000060: 0000 0000 0800 0002 0000 464d 4150 0000 ..........FMAP.. 00000070: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 00000080: 0000 0000 0000 0000 0000 0000 0002 0800 ................ 00000090: 00fe 9700 434f 5245 424f 4f54 0000 0000 ....COREBOOT.... 000000a0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 000000b0: 0000 0000 0000 ...... ~ # xxd -r cbmem.txt | sha1sum 6ecd73787c001bbc1215bd8787361e1a63b580cb - This sha1sum doesn't match reported TCPA/Event log for FMAP entry: 6ecd73787c001bbc1215bd8787361e1a63b580cb != 5622416ea417186aa1ac32b32c527ac09009fb5e FMAP extracted from cbfsutil externally from ROM image is padded with ff up to 512 bytes (this is dynamic and cannot be hardcoded magic): user@heads-tests-deb12:~/heads$ sudo cbfstool ~/heads/build/x86/qemu-coreboot-whiptail-tpm1/heads-qemu-coreboot-whiptail-tpm1-v0.2.0-1955-gfff99df-dirty.rom read -r FMAP -f xxx.bin user@heads-tests-deb12:~/heads$ xxd xxx.bin > hex.txt user@heads-tests-deb12:~/heads$ cat hex.txt 00000000: 5f5f 464d 4150 5f5f 0101 0000 60ff 0000 __FMAP__....`... 00000010: 0000 0000 a000 464c 4153 4800 0000 0000 ......FLASH..... 00000020: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 00000030: 0000 0000 0000 0300 0000 0800 0000 9800 ................ 00000040: 4249 4f53 0000 0000 0000 0000 0000 0000 BIOS............ 00000050: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 00000060: 0000 0000 0800 0002 0000 464d 4150 0000 ..........FMAP.. 00000070: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 00000080: 0000 0000 0000 0000 0000 0000 0002 0800 ................ 00000090: 00fe 9700 434f 5245 424f 4f54 0000 0000 ....COREBOOT.... 000000a0: 0000 0000 0000 0000 0000 0000 0000 0000 ................ 000000b0: 0000 0000 0000 ffff ffff ffff ffff ffff ................ 000000c0: ffff ffff ffff ffff ffff ffff ffff ffff ................ 000000d0: ffff ffff ffff ffff ffff ffff ffff ffff ................ 000000e0: ffff ffff ffff ffff ffff ffff ffff ffff ................ 000000f0: ffff ffff ffff ffff ffff ffff ffff ffff ................ 00000100: ffff ffff ffff ffff ffff ffff ffff ffff ................ 00000110: ffff ffff ffff ffff ffff ffff ffff ffff ................ 00000120: ffff ffff ffff ffff ffff ffff ffff ffff ................ 00000130: ffff ffff ffff ffff ffff ffff ffff ffff ................ 00000140: ffff ffff ffff ffff ffff ffff ffff ffff ................ 00000150: ffff ffff ffff ffff ffff ffff ffff ffff ................ 00000160: ffff ffff ffff ffff ffff ffff ffff ffff ................ 00000170: ffff ffff ffff ffff ffff ffff ffff ffff ................ 00000180: ffff ffff ffff ffff ffff ffff ffff ffff ................ 00000190: ffff ffff ffff ffff ffff ffff ffff ffff ................ 000001a0: ffff ffff ffff ffff ffff ffff ffff ffff ................ 000001b0: ffff ffff ffff ffff ffff ffff ffff ffff ................ 000001c0: ffff ffff ffff ffff ffff ffff ffff ffff ................ 000001d0: ffff ffff ffff ffff ffff ffff ffff ffff ................ 000001e0: ffff ffff ffff ffff ffff ffff ffff ffff ................ 000001f0: ffff ffff ffff ffff ffff ffff ffff ffff ................ user@heads-tests-deb12:~/heads$ xxd -r hex.txt | sha1sum 5622416ea417186aa1ac32b32c527ac09009fb5e - This matches FMAP region measured in TCPA/TPM event log for FMAP trace above: 6ecd73787c001bbc1215bd8787361e1a63b580cb = 6ecd73787c001bbc1215bd8787361e1a63b580cb It seems that coreboot measured boot code does the same as cbfstool (padding prior of measuring) instead of taking only the raw output we could replicate with cbfs.... Signed-off-by: Thierry Laurion <insurgo@riseup.net>
1 parent 5bcd6cd commit 07e3bf7

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

initrd/bin/tpmr

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,49 @@ replay_pcr() {
228228
# (6: LUKS header, 7: user related cbfs files loaded from cbfs-init)
229229
}
230230

231+
# usage: calc_pcr <alg> <pcr_num> [ <input_file>|<input_hash> ... ]
232+
# Calculate PCR value to compare with CBMEM event log.
233+
# First argument is PCR number, followed by optional
234+
# hashes and/or files.
235+
# Resulting PCR value is returned in binary form.
236+
calc_pcr() {
237+
TRACE "Under /bin/tpmr:calc_pcr"
238+
if [ -z "$2" ]; then
239+
echo >&2 "No PCR number passed"
240+
return
241+
fi
242+
if [ "$2" -ge 8 ]; then
243+
echo >&2 "Illegal PCR number ($2)"
244+
return
245+
fi
246+
local alg="$1"
247+
local pcr="$2"
248+
local alg_digits=0
249+
# SHA-1 hashes are 40 chars
250+
if [ "$alg" = "sha1" ]; then alg_digits=40; fi
251+
# SHA-256 hashes are 64 chars
252+
if [ "$alg" = "sha256" ]; then alg_digits=64; fi
253+
shift 2
254+
replayed_pcr=$(extend_pcr_state $alg $(printf "%.${alg_digits}d" 0) $@)
255+
echo $replayed_pcr | hex2bin
256+
DEBUG "Replayed cbmem -L clean boot state of PCR=$pcr ALG=$alg : $replayed_pcr"
257+
# To manually introspect calculated to PCR values:
258+
# TODO: fix the following examples with WORKING examples
259+
# PCR-2:
260+
# bash tpmr calc_pcr 2 <(cbmem -r 464d4150) <(cbfs --read bootblock) \
261+
# <(cbfs --read fallback/romstage) <(cbfs --read fallback/postcar) \
262+
# <(cbfs --read fallback/ramstage) <(cbfs --read bootsplash.jpg) \
263+
# <(cbfs --read fallback/payload) | xxd -p
264+
# PCR-4, in case of recovery shell (bash used for process substitution):
265+
# bash -c "tpmr calc_pcr 4 <(echo -n recovery)" | xxd -p
266+
# PCR-4, in case of normal boot passing through kexec-select-boot:
267+
# bash -c "tpmr calc_pcr 4 <(echo -n generic)" | xxd -p
268+
# PCR-5, depending on which modules are loaded for given board:
269+
# tpmr calc_pcr 5 module0.ko module1.ko module2.ko | xxd -p
270+
# PCR-6 and PCR-7: similar to 5, but with different files passed
271+
# (6: LUKS header, 7: user related cbfs files loaded from cbfs-init)
272+
}
273+
231274
tpm2_extend() {
232275
TRACE "Under /bin/tpmr:tpm2_extend"
233276
while true; do
@@ -519,7 +562,7 @@ tpm1_seal() {
519562
-of "$sealed_file" \
520563
-hk 40000000 \
521564
"${POLICY_ARGS[@]}"
522-
565+
523566
# try it without the TPM Owner Password first
524567
if ! tpm nv_writevalue -in "$index" -if "$sealed_file"; then
525568
# to create an nvram space we need the TPM Owner Password
@@ -751,6 +794,10 @@ if [ "$CONFIG_TPM2_TOOLS" != "y" ]; then
751794
shift
752795
replay_pcr "sha1" "$@"
753796
;;
797+
calc_pcr)
798+
shift
799+
calc_pcr "sha1" "$@"
800+
;;
754801
counter_create)
755802
shift
756803
tpm1_counter_create "$@"
@@ -796,6 +843,9 @@ pcrsize)
796843
calcfuturepcr)
797844
replay_pcr "sha256" "$@"
798845
;;
846+
calc_pcr)
847+
calc_pcr "sha256" "$@"
848+
;;
799849
extend)
800850
tpm2_extend "$@"
801851
;;

0 commit comments

Comments
 (0)