tpm2-sessions: Fix out of range indexing in name_size
Subsystems (5)
Jarkko Sakkinen
maintainerPATCH v2 1/1 tpm2-sessions: Fix out of range indexing in name_size
[ Upstream commit 6e9722e9a7bfe1bbad649937c811076acf86e1fd ]
'name_size' does not have any range checks, and it just directly indexes with TPM_ALG_ID, which could lead into memory corruption at worst.
Address the issue by only processing known values and returning -EINVAL for unrecognized values.
Make also 'tpm_buf_append_name' and 'tpm_buf_fill_hmac_session' fallible so that errors are detected before causing any spurious TPM traffic.
End also the authorization session on failure in both of the functions, as the session state would be then by definition corrupted.
Cc [email protected] # v6.10+
Fixes 1085b8276bb4 ("tpm: Add the rest of the session HMAC API")
Reviewed-by Jonathan McDowell <[email protected]>
Signed-off-by Jarkko Sakkinen <[email protected]>
---
Backport for v6.12.
--- drivers/char/tpm/tpm2-cmd.c | 23 ++++- drivers/char/tpm/tpm2-sessions.c | 114 ++++++++++++++-------- include/linux/tpm.h | 13 ++- security/keys/trusted-keys/trusted_tpm2.c | 29 ++++-- 4 files changed, 126 insertions(+), 53 deletions(-)
drivers/char/tpm/tpm2-cmd.c +19 -4
@@ -253,7 +253,11 @@ int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, }if (!disable_pcr_integrity) { -tpm_buf_append_name (chip, &buf, pcr_idx,NULL ); + rc =tpm_buf_append_name (chip, &buf, pcr_idx,NULL ); +if (rc) { +tpm_buf_destroy (&buf); +return rc; + }tpm_buf_append_hmac_session (chip, &buf,0 ,NULL ,0 ); }else {tpm_buf_append_handle (chip, &buf, pcr_idx);
@@ -268,8 +272,14 @@ int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, chip->allocated_banks[i].digest_size); } -if (!disable_pcr_integrity) -tpm_buf_fill_hmac_session (chip, &buf); +if (!disable_pcr_integrity) { + rc =tpm_buf_fill_hmac_session (chip, &buf); +if (rc) { +tpm_buf_destroy (&buf); +return rc; + } + } + rc =tpm_transmit_cmd (chip, &buf,0 ,"attempting extend a PCR value" );if (!disable_pcr_integrity) rc =tpm_buf_check_hmac_response (chip, &buf, rc);
@@ -327,7 +337,12 @@ int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max) | TPM2_SA_CONTINUE_SESSION, NULL, 0);tpm_buf_append_u16 (&buf, num_bytes); -tpm_buf_fill_hmac_session (chip, &buf); + err =tpm_buf_fill_hmac_session (chip, &buf); +if (err) { +tpm_buf_destroy (&buf); +return err; + } + err = tpm_transmit_cmd(chip, &buf, offsetof(struct tpm2_get_random_out, buffer),
drivers/char/tpm/tpm2-sessions.c +76 -38
200 lines not on this page · raw message
include/linux/tpm.h +8 -5
@@ -523,8 +523,8 @@ static inline struct tpm2_auth *tpm2_chip_auth(struct tpm_chip *chip)#endif } -void tpm_buf_append_name (struct tpm_chip *chip,struct tpm_buf *buf, -u32 handle,u8 *name); +int tpm_buf_append_name (struct tpm_chip *chip,struct tpm_buf *buf, +u32 handle,u8 *name);void tpm_buf_append_hmac_session (struct tpm_chip *chip,struct tpm_buf *buf,u8 attributes,u8 *passphrase,int passphraselen);
@@ -557,7 +557,7 @@ static inline void tpm_buf_append_hmac_session_opt(struct tpm_chip *chip,#ifdef CONFIG_TCG_TPM2_HMAC int tpm2_start_auth_session (struct tpm_chip *chip); -void tpm_buf_fill_hmac_session (struct tpm_chip *chip,struct tpm_buf *buf); +int tpm_buf_fill_hmac_session (struct tpm_chip *chip,struct tpm_buf *buf);int tpm_buf_check_hmac_response (struct tpm_chip *chip,struct tpm_buf *buf,int rc);void tpm2_end_auth_session (struct tpm_chip *chip);
@@ -571,10 +571,13 @@ static inline int tpm2_start_auth_session(struct tpm_chip *chip)static inline void tpm2_end_auth_session (struct tpm_chip *chip) { } -static inline void tpm_buf_fill_hmac_session (struct tpm_chip *chip, -struct tpm_buf *buf) + +static inline int tpm_buf_fill_hmac_session (struct tpm_chip *chip, +struct tpm_buf *buf) { +return 0 ; } +static inline int tpm_buf_check_hmac_response (struct tpm_chip *chip,struct tpm_buf *buf,int rc)
security/keys/trusted-keys/trusted_tpm2.c +23 -6
@@ -283,7 +283,10 @@ int tpm2_seal_trusted(struct tpm_chip *chip, goto out_put; } -tpm_buf_append_name (chip, &buf, options->keyhandle,NULL ); + rc =tpm_buf_append_name (chip, &buf, options->keyhandle,NULL ); +if (rc) + goto out; +tpm_buf_append_hmac_session (chip, &buf,TPM2_SA_DECRYPT , options->keyauth,TPM_DIGEST_SIZE );
@@ -331,7 +334,10 @@ int tpm2_seal_trusted(struct tpm_chip *chip, goto out; } -tpm_buf_fill_hmac_session (chip, &buf); + rc =tpm_buf_fill_hmac_session (chip, &buf); +if (rc) + goto out; + rc =tpm_transmit_cmd (chip, &buf,4 ,"sealing data" ); rc =tpm_buf_check_hmac_response (chip, &buf, rc);if (rc)
@@ -448,7 +454,10 @@ static int tpm2_load_cmd(struct tpm_chip *chip,return rc; } -tpm_buf_append_name (chip, &buf, options->keyhandle,NULL ); + rc =tpm_buf_append_name (chip, &buf, options->keyhandle,NULL ); +if (rc) + goto out; +tpm_buf_append_hmac_session (chip, &buf,0 , options->keyauth,TPM_DIGEST_SIZE );
@@ -460,7 +469,10 @@ static int tpm2_load_cmd(struct tpm_chip *chip, goto out; } -tpm_buf_fill_hmac_session (chip, &buf); + rc =tpm_buf_fill_hmac_session (chip, &buf); +if (rc) + goto out; + rc =tpm_transmit_cmd (chip, &buf,4 ,"loading blob" ); rc =tpm_buf_check_hmac_response (chip, &buf, rc);if (!rc)
@@ -508,7 +520,9 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,return rc; } -tpm_buf_append_name (chip, &buf, blob_handle,NULL ); + rc =tpm_buf_append_name (chip, &buf, options->keyhandle,NULL ); +if (rc) + goto out;if (!options->policyhandle) { tpm_buf_append_hmac_session(chip, &buf, TPM2_SA_ENCRYPT,
@@ -533,7 +547,10 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip, NULL, 0); } -tpm_buf_fill_hmac_session (chip, &buf); + rc =tpm_buf_fill_hmac_session (chip, &buf); +if (rc) + goto out; + rc =tpm_transmit_cmd (chip, &buf,6 ,"unsealing" ); rc =tpm_buf_check_hmac_response (chip, &buf, rc);if (rc >0 )
Signature · 1 line
2.52.0