Skip to content

Commit 93f949f

Browse files
committed
tpm2: ensure auth session contexts are flushed after use
The TPM is capable of storing a limited number of auth session handles. Ensure auth sessions are flushed after use, to prevent tpm2_startauthsession from failing with 'out of session handles'. Change-type: patch Signed-off-by: Joseph Kogut <[email protected]>
1 parent 1f1cb00 commit 93f949f

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

meta-balena-common/recipes-core/initrdscripts/files/cryptsetup-efi-tpm

+4
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ cryptsetup_run() {
106106
tpm2_startauthsession --policy-session -S "${SESSION_CTX}"
107107
tpm2_policypcr -S "${SESSION_CTX}" -l "${PCRS}"
108108

109+
trap 'tpm2_flushcontext "${SESSION_CTX}"' EXIT
110+
109111
# combined multiple policies with tpm2_policyor
110112
POLICIES="$(find "${POLICY_PATH}" -type f | sort | xargs)"
111113
if [ "$(echo "${POLICIES}" | wc -w)" -gt 1 ]; then
@@ -121,6 +123,8 @@ cryptsetup_run() {
121123
fail "Failed to unlock LUKS passphrase using the TPM"
122124
fi
123125

126+
tpm2_flushcontext "${SESSION_CTX}" >/dev/null 2>&1
127+
124128
BOOT_DEVICE=$(lsblk -nlo pkname "${EFI_DEV}")
125129

126130
# Check that we have the expected amount of encrypted partitions on the boot device

meta-balena-common/recipes-support/hostapp-update-hooks/files/0-signed-update

+4-2
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ updateKeys() {
124124
CURRENT_POLICY_PATH="$(find /mnt/efi -name "policies.*")"
125125
for UNLOCK_PCRS in 0,2,3,7 0,1,2,3; do
126126
{
127-
[ -f "${SESSION_CTX}" ] && tpm2_flushcontext "${SESSION_CTX}" 2>&1 || true
127+
tpm2_flushcontext "${SESSION_CTX}" 2>&1 || true
128128
tpm2_startauthsession --policy-session -S "${SESSION_CTX}"
129129
tpm2_policypcr -S "${SESSION_CTX}" -l "sha256:${UNLOCK_PCRS}"
130130
POLICIES="$(find "${CURRENT_POLICY_PATH}" -type f | sort | xargs)"
@@ -140,6 +140,8 @@ updateKeys() {
140140
fi
141141
done
142142

143+
tpm2_flushcontext "${SESSION_CTX}" >/dev/null 2>&1
144+
143145
POLICY_UPDATED="${POLICY_PATH}/policy.updated"
144146
POLICY_EFIBIN="${POLICY_PATH}/policy.efibin"
145147
POLICY_COMBINED="$(mktemp -t)"
@@ -186,7 +188,7 @@ updateKeys() {
186188
esac
187189

188190
{
189-
tpm2_flushcontext "${SESSION_CTX}"
191+
tpm2_flushcontext "${SESSION_CTX}" 2>&1
190192

191193
hw_encrypt_passphrase "$PASSPHRASE_FILE" "$POLICY" "$RESULT_DIR"
192194
rm -rf "${CURRENT_POLICY_PATH}"

meta-balena-common/recipes-support/hostapp-update-hooks/files/95-secureboot/2-fwd_commit_update-policy

+4
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ if [ "$(echo "${POLICIES}" | wc -w)" -gt 1 ]; then
4646
update_reason="Combined policy in use"
4747
fi
4848

49+
trap 'tpm2_flushcontext "${SESSION_CTX}"' EXIT
50+
4951
if hw_decrypt_passphrase "${EFI_MOUNT_DIR}" "session:${SESSION_CTX}" "${PASSPHRASE_FILE}"; then
5052
echo "Unlocked passphrase using pcr:sha256:0,2,3,7"
5153
elif hw_decrypt_passphrase "${EFI_MOUNT_DIR}" "pcr:sha256:0,1,2,3" "${PASSPHRASE_FILE}"; then
@@ -56,6 +58,8 @@ else
5658
exit 1
5759
fi
5860

61+
tpm2_flushcontext "${SESSION_CTX}" >/dev/null 2>&1
62+
5963
POLICY="$(mktemp -t)"
6064
PCRS="0,2,3,7"
6165
PCR_VAL_BIN="$(mktemp -t)"

0 commit comments

Comments
 (0)