Have IMA find and use a tpm_chip until system shutdown
Subsystems (5)
v6 cover Have IMA find and use a tpm_chip until system shutdown
This series of patches converts IMA's usage of the tpm_chip to find a TPM chip initially and use it until the machine is shut down. To do this we need to introduce a kref for the tpm_chip that IMA and all other users of a tpm_chip hold onto until they don't need the TPM chip anymore.
Stefan
v5->v6:
- more verbose patch description in patch 2
v4->v5:
- repost with fixed changelog and posted to linux-security-module
v3->v4:
- followed Jason's suggestions:
- extended docs of tpm_find_get_ops()
- calling new function tpm_default_chip()
- new patch 3 that uses tpm_default_chip() in tpm_find_get_ops()
v2->v3:
- renaming tpm_chip_find_get() to tpm_get_ops()
- IMA does not lock access to ima_tpm_chip anymore
- IMA does not have a reboot notifier to release the chip anymore
v1->v2:
- use the kref of the device via get_device()/put_device()
Stefan Berger (5):
tpm: rename tpm_chip_find_get() to tpm_find_get_ops()
tpm: Implement tpm_default_chip() to find a TPM chip
tpm: Convert tpm_find_get_ops() to use tpm_default_chip()
ima: Use tpm_default_chip() and call TPM functions with a tpm_chip
ima: Get rid of ima_used_chip and use ima_tpm_chip != NULL instead
drivers/char/tpm/tpm-chip.c | 68 +++++++++++++++++++---------- drivers/char/tpm/tpm-interface.c | 14 +++--- drivers/char/tpm/tpm.h | 2 +- include/linux/tpm.h | 5 +++ security/integrity/ima/ima.h | 2 +- security/integrity/ima/ima_crypto.c | 4 +- security/integrity/ima/ima_init.c | 16 +++---- security/integrity/ima/ima_queue.c | 4 +- 8 files changed, 69 insertions(+), 46 deletions(-)
Signature · 6 lines
2.17.1 -- To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH v6 1/5 tpm: rename tpm_chip_find_get() to tpm_find_get_ops()
Rename tpm_chip_find_get() to tpm_find_get_ops() to more closely match the tpm_put_ops() counter part.
--- drivers/char/tpm/tpm-chip.c | 9 ++++++--- drivers/char/tpm/tpm-interface.c | 14 +++++++------- drivers/char/tpm/tpm.h | 2 +- 3 files changed, 14 insertions(+), 11 deletions(-)
drivers/char/tpm/tpm-chip.c +6 -3
@@ -81,18 +81,21 @@ void tpm_put_ops(struct tpm_chip *chip)EXPORT_SYMBOL_GPL (tpm_put_ops);/** -* tpm_chip_find_get() - find and reserve a TPM chip +* tpm_find_get_ops() - find and reserve a TPM chip * @chip: a &struct tpm_chip instance, %NULL for the default chip * * Finds a TPM chip and reserves its class device and operations. The chip must -* be released with tpm_chip_put_ops() after use. +* be released with tpm_put_ops() after use. +* This function is for internal use only. It supports existing TPM callers +* by accepting NULL, but those callers should be converted to pass in a chip +* directly. * * Return: * A reserved &struct tpm_chip instance. * %NULL if a chip is not found. * %NULL if the chip is not available. */ -struct tpm_chip *tpm_chip_find_get (struct tpm_chip *chip) +struct tpm_chip *tpm_find_get_ops (struct tpm_chip *chip) {struct tpm_chip *res =NULL ;int chip_num =0 ;
drivers/char/tpm/tpm-interface.c +7 -7
@@ -929,7 +929,7 @@ int tpm_is_tpm2(struct tpm_chip *chip) {int rc; - chip =tpm_chip_find_get (chip); + chip =tpm_find_get_ops (chip);if (!chip)return -ENODEV ;
@@ -953,7 +953,7 @@ int tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf) {int rc; - chip =tpm_chip_find_get (chip); + chip =tpm_find_get_ops (chip);if (!chip)return -ENODEV ;if (chip->flags &TPM_CHIP_FLAG_TPM2 )
@@ -1012,7 +1012,7 @@ int tpm_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash)u32 count =0 ;int i; - chip =tpm_chip_find_get (chip); + chip =tpm_find_get_ops (chip);if (!chip)return -ENODEV ;
@@ -1141,7 +1141,7 @@ int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen) {int rc; - chip =tpm_chip_find_get (chip); + chip =tpm_find_get_ops (chip);if (!chip)return -ENODEV ;
@@ -1261,7 +1261,7 @@ int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)if (!out || !num_bytes || max >TPM_MAX_RNG_DATA )return -EINVAL ; - chip =tpm_chip_find_get (chip); + chip =tpm_find_get_ops (chip);if (!chip)return -ENODEV ;
@@ -1323,7 +1323,7 @@ int tpm_seal_trusted(struct tpm_chip *chip, struct trusted_key_payload *payload, {int rc; - chip =tpm_chip_find_get (chip); + chip =tpm_find_get_ops (chip);if (!chip || !(chip->flags &TPM_CHIP_FLAG_TPM2 ))return -ENODEV ;
@@ -1351,7 +1351,7 @@ int tpm_unseal_trusted(struct tpm_chip *chip, {int rc; - chip =tpm_chip_find_get (chip); + chip =tpm_find_get_ops (chip);if (!chip || !(chip->flags &TPM_CHIP_FLAG_TPM2 ))return -ENODEV ;
drivers/char/tpm/tpm.h +1 -1
@@ -535,7 +535,7 @@ static inline void tpm_msleep(unsigned int delay_msec) delay_msec * 1000); }; -struct tpm_chip *tpm_chip_find_get (struct tpm_chip *chip); +struct tpm_chip *tpm_find_get_ops (struct tpm_chip *chip);__must_check int tpm_try_get_ops (struct tpm_chip *chip);void tpm_put_ops (struct tpm_chip *chip);
Signature · 6 lines
2.17.1 -- To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH v6 2/5 tpm: Implement tpm_default_chip() to find a TPM chip
In reply to Stefan BergerImplement tpm_default_chip() to find the first TPM chip and return it to the caller while increasing the reference count on its device. This function can be used by other subsystems, such as IMA, to find the system's default TPM chip and use it for all subsequent TPM operations.
--- drivers/char/tpm/tpm-chip.c | 27 +++++++++++++++++++++++++++ include/linux/tpm.h | 5 +++++ 2 files changed, 32 insertions(+)
drivers/char/tpm/tpm-chip.c +27 -0
@@ -80,6 +80,33 @@ void tpm_put_ops(struct tpm_chip *chip) }EXPORT_SYMBOL_GPL (tpm_put_ops); +/** +* tpm_default_chip() - find a TPM chip and get a reference to it +*/ +struct tpm_chip *tpm_default_chip (void ) +{ +struct tpm_chip *chip, *res =NULL ; +int chip_num =0 ; +int chip_prev; + +mutex_lock (&idr_lock); + +do { + chip_prev = chip_num; + chip =idr_get_next (&dev_nums_idr, &chip_num); +if (chip) { +get_device (&chip->dev); + res = chip; +break ; + } + }while (chip_prev != chip_num); + +mutex_unlock (&idr_lock); + +return res; +} +EXPORT_SYMBOL_GPL (tpm_default_chip); +/** * tpm_find_get_ops() - find and reserve a TPM chip * @chip: a &struct tpm_chip instance, %NULL for the default chip
include/linux/tpm.h +5 -0
@@ -61,6 +61,7 @@ extern int tpm_seal_trusted(struct tpm_chip *chip,extern int tpm_unseal_trusted (struct tpm_chip *chip,struct trusted_key_payload *payload,struct trusted_key_options *options); +extern struct tpm_chip *tpm_default_chip (void );#else static inline int tpm_is_tpm2 (struct tpm_chip *chip) {
@@ -96,5 +97,9 @@ static inline int tpm_unseal_trusted(struct tpm_chip *chip, {return -ENODEV ; } +static inline struct tpm_chip *tpm_default_chip (void ) +{ +return NULL ; +}#endif #endif
Signature · 6 lines
2.17.1 -- To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH v6 3/5 tpm: Convert tpm_find_get_ops() to use tpm_default_chip()
In reply to Stefan BergerConvert tpm_find_get_ops() to use tpm_default_chip() in case no chip is passed in.
--- drivers/char/tpm/tpm-chip.c | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-)
drivers/char/tpm/tpm-chip.c +13 -19
@@ -124,29 +124,23 @@ EXPORT_SYMBOL_GPL(tpm_default_chip);*/ struct tpm_chip *tpm_find_get_ops (struct tpm_chip *chip) { -struct tpm_chip *res =NULL ; -int chip_num =0 ; -int chip_prev; - -mutex_lock (&idr_lock); +int rc; -if (!chip) { -do { - chip_prev = chip_num; - chip =idr_get_next (&dev_nums_idr, &chip_num); -if (chip && !tpm_try_get_ops (chip)) { - res = chip; -break ; - } - }while (chip_prev != chip_num); - }else { +if (chip) {if (!tpm_try_get_ops (chip)) - res = chip; +return chip; +return NULL ; } -mutex_unlock (&idr_lock); - -return res; + chip =tpm_default_chip (); +if (!chip) +return NULL ; + rc =tpm_try_get_ops (chip); +/* release additional reference we got from tpm_default_chip() */ +put_device (&chip->dev); +if (!rc) +return NULL ; +return chip; }/**
Signature · 6 lines
2.17.1 -- To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH v6 3/5] tpm: Convert tpm_find_get_ops() to use tpm_default_chip()
Quoted 41 lines · Stefan Berger
Convert
tpm_find_get_ops()to usetpm_default_chip()in case no chip is passed in.Signed-off-by Stefan Berger <[email protected]>Reviewed-by Jarkko Sakkinen <[email protected]>--- drivers/char/tpm/tpm-chip.c | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-)drivers/char/tpm/tpm-chip.c +4
-16@@ -124,29 +124,23 @@ EXPORT_SYMBOL_GPL(tpm_default_chip);*/ struct tpm_chip *tpm_find_get_ops (struct tpm_chip *chip) { -struct tpm_chip *res =NULL ; -int chip_num =0 ; -int chip_prev; - -mutex_lock (&idr_lock); +int rc; -if (!chip) { -do { - chip_prev = chip_num; - chip =idr_get_next (&dev_nums_idr, &chip_num); -if (chip && !tpm_try_get_ops (chip)) { - res = chip; -break ; - } - }while (chip_prev != chip_num); - }else { +if (chip) {if (!tpm_try_get_ops (chip)) - res = chip; +return chip; +return NULL ; }
drivers/char/tpm/tpm-chip.c EXPORT_SYMBOL_GPL(tpm_default_chip); · line 141
Quoted 10 lines
drivers/char/tpm/tpm-chip.c +7
-3-mutex_unlock (&idr_lock); - -return res; + chip =tpm_default_chip (); +if (!chip) +return NULL ; + rc =tpm_try_get_ops (chip); +/* release additional reference we got from tpm_default_chip() */ +put_device (&chip->dev); +if (!rc)
This should say 'if (rc)'.
drivers/char/tpm/tpm-chip.c EXPORT_SYMBOL_GPL(tpm_default_chip); · line 146
Quoted 5 lines
drivers/char/tpm/tpm-chip.c +2
-0+return NULL ; +return chip; }/**
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH v6 4/5 ima: Use tpm_default_chip() and call TPM functions with a tpm_chip
In reply to Stefan BergerRather than accessing the TPM functions by passing a NULL pointer for the tpm_chip, which causes a lookup for a suitable chip every time, get a hold of a tpm_chip and access the TPM functions using it.
--- security/integrity/ima/ima.h | 1 + security/integrity/ima/ima_crypto.c | 2 +- security/integrity/ima/ima_init.c | 11 ++++------- security/integrity/ima/ima_queue.c | 2 +- 4 files changed, 7 insertions(+), 9 deletions(-)
security/integrity/ima/ima.h +1 -0
@@ -56,6 +56,7 @@ extern int ima_policy_flag;extern int ima_used_chip;extern int ima_hash_algo;extern int ima_appraise; +extern struct tpm_chip *ima_tpm_chip;/* IMA event related data */ struct ima_event_data {
security/integrity/ima/ima_crypto.c +1 -1
@@ -634,7 +634,7 @@ static void __init ima_pcrread(int idx, u8 *pcr)if (!ima_used_chip)return ; -if (tpm_pcr_read (NULL , idx, pcr) !=0 ) +if (tpm_pcr_read (ima_tpm_chip, idx, pcr) !=0 )pr_err ("Error Communicating to TPM chip\n" ); }
security/integrity/ima/ima_init.c +4 -7
@@ -27,6 +27,7 @@/* name for boot aggregate entry */ static const char *boot_aggregate_name ="boot_aggregate" ;int ima_used_chip; +struct tpm_chip *ima_tpm_chip;/* Add the boot aggregate to the IMA measurement list and extend * the PCR register.
@@ -106,17 +107,13 @@ void __init ima_load_x509(void)int __init ima_init (void ) { -u8 pcr_i[TPM_DIGEST_SIZE ];int rc; - ima_used_chip =0 ; - rc =tpm_pcr_read (NULL ,0 , pcr_i); -if (rc ==0 ) - ima_used_chip =1 ; + ima_tpm_chip =tpm_default_chip (); + ima_used_chip = ima_tpm_chip !=NULL ;if (!ima_used_chip) -pr_info ("No TPM chip found, activating TPM-bypass! (rc=%d)\n" , - rc); +pr_info ("No TPM chip found, activating TPM-bypass!\n" ); rc =integrity_init_keyring (INTEGRITY_KEYRING_IMA );if (rc)
security/integrity/ima/ima_queue.c +1 -1
@@ -145,7 +145,7 @@ static int ima_pcr_extend(const u8 *hash, int pcr)if (!ima_used_chip)return result; - result =tpm_pcr_extend (NULL , pcr, hash); + result =tpm_pcr_extend (ima_tpm_chip, pcr, hash);if (result !=0 )pr_err ("Error Communicating to TPM chip, result: %d\n" , result);return result;
Signature · 6 lines
2.17.1 -- To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH v6 5/5 ima: Get rid of ima_used_chip and use ima_tpm_chip != NULL instead
In reply to Stefan BergerGet rid of ima_used_chip and use ima_tpm_chip variable instead for determining whether to use the TPM chip.
--- security/integrity/ima/ima.h | 1 - security/integrity/ima/ima_crypto.c | 2 +- security/integrity/ima/ima_init.c | 7 ++----- security/integrity/ima/ima_queue.c | 2 +- 4 files changed, 4 insertions(+), 8 deletions(-)
security/integrity/ima/ima.h +0 -1
@@ -53,7 +53,6 @@ enum tpm_pcrs { TPM_PCR0 = 0, TPM_PCR8 = 8 };extern int ima_policy_flag;/* set during initialization */ -extern int ima_used_chip;extern int ima_hash_algo;extern int ima_appraise;extern struct tpm_chip *ima_tpm_chip;
security/integrity/ima/ima_crypto.c +1 -1
@@ -631,7 +631,7 @@ int ima_calc_buffer_hash(const void *buf, loff_t len,static void __init ima_pcrread (int idx,u8 *pcr) { -if (!ima_used_chip) +if (!ima_tpm_chip)return ;if (tpm_pcr_read (ima_tpm_chip, idx, pcr) !=0 )
security/integrity/ima/ima_init.c +2 -5
@@ -26,7 +26,6 @@/* name for boot aggregate entry */ static const char *boot_aggregate_name ="boot_aggregate" ; -int ima_used_chip;struct tpm_chip *ima_tpm_chip;/* Add the boot aggregate to the IMA measurement list and extend
@@ -65,7 +64,7 @@ static int __init ima_add_boot_aggregate(void) iint->ima_hash->algo =HASH_ALGO_SHA1 ; iint->ima_hash->length =SHA1_DIGEST_SIZE ; -if (ima_used_chip) { +if (ima_tpm_chip) { result =ima_calc_boot_aggregate (&hash.hdr);if (result <0 ) { audit_cause ="hashing_error" ;
@@ -110,9 +109,7 @@ int __init ima_init(void)int rc; ima_tpm_chip =tpm_default_chip (); - - ima_used_chip = ima_tpm_chip !=NULL ; -if (!ima_used_chip) +if (!ima_tpm_chip)pr_info ("No TPM chip found, activating TPM-bypass!\n" ); rc =integrity_init_keyring (INTEGRITY_KEYRING_IMA );
security/integrity/ima/ima_queue.c +1 -1
@@ -142,7 +142,7 @@ static int ima_pcr_extend(const u8 *hash, int pcr) {int result =0 ; -if (!ima_used_chip) +if (!ima_tpm_chip)return result; result =tpm_pcr_extend (ima_tpm_chip, pcr, hash);
Signature · 6 lines
2.17.1 -- To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org More majordomo info@ http://vger.kernel.org/majordomo-info.html