tpmr: Move last TPM owner password prompt/shred into tpmr

Prompt for TPM owner password internally within tpm2_counter_create.
Add tpm1_counter_create to prompt for password internally.  Wipe the
cache in either if the operation fails, in case the password was
incorrect.

Signed-off-by: Jonathon Hall <jonathon.hall@puri.sm>
This commit is contained in:
Jonathon Hall 2023-11-06 10:31:50 -05:00 committed by Thierry Laurion
parent 9e0491e9db
commit fd6a947cb3
No known key found for this signature in database
GPG Key ID: E7B4A71658E36A93
3 changed files with 24 additions and 26 deletions

View File

@ -603,7 +603,6 @@ generate_checksums() {
if [ "$CONFIG_TPM" = "y" ]; then
if [ "$CONFIG_IGNORE_ROLLBACK" != "y" ]; then
tpmr counter_create \
-pwdo "$TPM_PASS" \
-pwdc '' \
-la -3135106223 |
tee /tmp/counter ||

View File

@ -290,18 +290,23 @@ tpm2_counter_inc() {
echo "$index: $(tpm2 nvread 0x$index | xxd -pc8)"
}
tpm2_counter_cre() {
TRACE "Under /bin/tpmr:tpm2_counter_cre"
tpm1_counter_create() {
TRACE "Under /bin/tpmr:tpm1_counter_create"
# tpmr handles the TPM owner password (from cache or prompt), but all
# other parameters for TPM1 are passed directly, and TPM2 mimics the
# TPM1 interface.
prompt_tpm_owner_password
if ! tpm counter_create -pwdo "$(cat "/tmp/secret/tpm_owner_password")" "$@"; then
DEBUG "Failed to create counter from tpm1_counter_create. Wiping /tmp/secret/tpm_owner_password"
shred -n 10 -z -u /tmp/secret/tpm_owner_password
die "Unable to create counter from tpm1_counter_create"
fi
}
tpm2_counter_create() {
TRACE "Under /bin/tpmr:tpm2_counter_create"
while true; do
case "$1" in
-pwdo)
pwdo="$2"
shift 2
;;
-pwdof)
pwdo="file:$2"
shift 2
;;
-pwdc)
pwd="$2"
shift 2
@ -315,13 +320,14 @@ tpm2_counter_cre() {
;;
esac
done
prompt_tpm_owner_password
rand_index="1$(dd if=/dev/urandom bs=1 count=3 | xxd -pc3)"
tpm2 nvdefine -C o -s 8 -a "ownerread|authread|authwrite|nt=1" \
-P "$(tpm2_password_hex "$pwdo")" "0x$rand_index" >/dev/console ||
-P "$(tpm2_password_hex "$(cat "/tmp/secret/tpm_owner_password")")" "0x$rand_index" >/dev/console ||
{
DEBUG "Failed to create counter from tpm2_counter_cre. Wiping /tmp/secret/tpm_owner_password"
DEBUG "Failed to create counter from tpm2_counter_create. Wiping /tmp/secret/tpm_owner_password"
shred -n 10 -z -u /tmp/secret/tpm_owner_password
die "Unable to create counter from tpm2_counter_cre"
die "Unable to create counter from tpm2_counter_create"
}
echo "$rand_index: (valid after an increment)"
}
@ -742,6 +748,10 @@ if [ "$CONFIG_TPM2_TOOLS" != "y" ]; then
shift
replay_pcr "sha1" "$@"
;;
counter_create)
shift
tpm1_counter_create "$@"
;;
destroy)
shift
tpm1_destroy "$@"
@ -793,7 +803,7 @@ counter_increment)
tpm2_counter_inc "$@"
;;
counter_create)
tpm2_counter_cre "$@"
tpm2_counter_create "$@"
;;
destroy)
tpm2_destroy "$@"

View File

@ -250,22 +250,11 @@ check_tpm_counter() {
TPM_COUNTER=$(grep counter- "$1" | cut -d- -f2)
else
warn "$1 does not exist; creating new TPM counter"
prompt_tpm_owner_password
tpmr counter_create \
-pwdo "$tpm_password" \
-pwdc '' \
-la $LABEL |
tee /tmp/counter ||
{
DEBUG "Failed to create TPM counter. Shredding TPM Owner Password"
#tpm2 counter_cre wipes the cached tpm owner password if invalid.
#tpm1 calls counter_create directly and does not wipe the cached password.
# This is a workaround to make the two calls behave the same in case we need to create a new counter.
# I am not able to get here in my tests: the cached password should be able to create a counter at this point.
#TODO: unify the two calls further and delete the next shred call.
shred -n 10 -z -u /tmp/secret/tpm_owner_password
die "Unable to create TPM counter"
}
TPM_COUNTER=$(cut -d: -f1 </tmp/counter)
fi