Skip to contentSkip to conversation

keys/trusted_keys: reuse TPM version in option handling

PATCH Superseded Srish Srinivasan 3 participants 4 messages Sep 30 – Oct 2

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 keys/trusted_keys: reuse TPM version in option handling

Determine the TPM version once in the seal and unseal paths and reuse it when allocating and parsing trusted key options. This avoids redundant TPM version checks.

---
 security/keys/trusted-keys/trusted_tpm1.c | 22 ++++++----------------
 1 file changed, 6 insertions(+), 16 deletions(-)
security/keys/trusted-keys/trusted_tpm1.c +6 -16
index 3ec078ca3f97..70de4101923f 100644
@@ -700,7 +700,7 @@ static const match_table_t key_tokens = {
 
 /* can have zero or more token= options */
 static int getoptions(char *c, struct trusted_key_payload *pay,
-		      struct trusted_key_options *opt)
+		      struct trusted_key_options *opt, int tpm2)
 {
 	struct trusted_key_tpm *private = opt->private;
 	substring_t args[MAX_OPT_ARGS];
@@ -712,11 +712,6 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
 	unsigned long token_mask = 0;
 	unsigned int digest_len;
 	int i;
-	int tpm2;
-
-	tpm2 = tpm_is_tpm2(chip);
-	if (tpm2 < 0)
-		return tpm2;
 
 	private->hash = tpm2 ? HASH_ALGO_SHA256 : HASH_ALGO_SHA1;
 
@@ -838,15 +833,10 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
 	return 0;
 }
 
-static struct trusted_key_options *trusted_options_alloc(void)
+static struct trusted_key_options *trusted_options_alloc(int tpm2)
 {
 	struct trusted_key_tpm *private;
 	struct trusted_key_options *options;
-	int tpm2;
-
-	tpm2 = tpm_is_tpm2(chip);
-	if (tpm2 < 0)
-		return NULL;
 
 	options = kzalloc_obj(*options);
 	if (options) {
@@ -877,13 +867,13 @@ static int trusted_tpm_seal(struct trusted_key_payload *p, char *datablob)
 	if (tpm2 < 0)
 		return tpm2;
 
-	options = trusted_options_alloc();
+	options = trusted_options_alloc(tpm2);
 	if (!options)
 		return -ENOMEM;
 
 	private = options->private;
 
-	ret = getoptions(datablob, p, options);
+	ret = getoptions(datablob, p, options, tpm2);
 	if (ret < 0)
 		return ret;
 	dump_options(options);
@@ -922,12 +912,12 @@ static int trusted_tpm_unseal(struct trusted_key_payload *p, char *datablob)
 	if (tpm2 < 0)
 		return tpm2;
 
-	options = trusted_options_alloc();
+	options = trusted_options_alloc(tpm2);
 	if (!options)
 		return -ENOMEM;
 	private = options->private;
 
-	ret = getoptions(datablob, p, options);
+	ret = getoptions(datablob, p, options, tpm2);
 	if (ret < 0)
 		return ret;
 	dump_options(options);
Signature · 1 line
2.53.0
#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 5 lines

Determine the TPM version once in the seal and unseal paths and reuse it when allocating and parsing trusted key options. This avoids redundant TPM version checks.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Srish Srinivasan
Quoted 36 lines · Srish Srinivasan

Determine the TPM version once in the seal and unseal paths and reuse it when allocating and parsing trusted key options. This avoids redundant TPM version checks.

---
 security/keys/trusted-keys/trusted_tpm1.c | 22 ++++++----------------
 1 file changed, 6 insertions(+), 16 deletions(-)
security/keys/trusted-keys/trusted_tpm1.c +1 -6
index 3ec078ca3f97..70de4101923f 100644
@@ -700,7 +700,7 @@ static const match_table_t key_tokens = {
 
 /* can have zero or more token= options */
 static int getoptions(char *c, struct trusted_key_payload *pay,
-		      struct trusted_key_options *opt)
+		      struct trusted_key_options *opt, int tpm2)
 {
 	struct trusted_key_tpm *private = opt->private;
 	substring_t args[MAX_OPT_ARGS];
@@ -712,11 +712,6 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
 	unsigned long token_mask = 0;
 	unsigned int digest_len;
 	int i;
-	int tpm2;
-
-	tpm2 = tpm_is_tpm2(chip);
-	if (tpm2 < 0)
-		return tpm2;
 
 	private->hash = tpm2 ? HASH_ALGO_SHA256 : HASH_ALGO_SHA1;
 

security/keys/trusted-keys/trusted_tpm1.c static int getoptions(char *c, struct trusted_key_payload *pay, · line 836

Quoted 5 lines
security/keys/trusted-keys/trusted_tpm1.c +1 -1
@@ -838,15 +833,10 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
 	return 0;
 }
 
-static struct trusted_key_options *trusted_options_alloc(void)
+static struct trusted_key_options *trusted_options_alloc(int tpm2)

Probably "bool is_tpm2" would be better here. It does not need to be tightly coupled with the tristate acquired from tpm_is_tpm2.

Quoted 45 lines
security/keys/trusted-keys/trusted_tpm1.c +4 -9
 {
 	struct trusted_key_tpm *private;
 	struct trusted_key_options *options;
-	int tpm2;
-
-	tpm2 = tpm_is_tpm2(chip);
-	if (tpm2 < 0)
-		return NULL;
 
 	options = kzalloc_obj(*options);
 	if (options) {
@@ -877,13 +867,13 @@ static int trusted_tpm_seal(struct trusted_key_payload *p, char *datablob)
 	if (tpm2 < 0)
 		return tpm2;
 
-	options = trusted_options_alloc();
+	options = trusted_options_alloc(tpm2);
 	if (!options)
 		return -ENOMEM;
 
 	private = options->private;
 
-	ret = getoptions(datablob, p, options);
+	ret = getoptions(datablob, p, options, tpm2);
 	if (ret < 0)
 		return ret;
 	dump_options(options);
@@ -922,12 +912,12 @@ static int trusted_tpm_unseal(struct trusted_key_payload *p, char *datablob)
 	if (tpm2 < 0)
 		return tpm2;
 
-	options = trusted_options_alloc();
+	options = trusted_options_alloc(tpm2);
 	if (!options)
 		return -ENOMEM;
 	private = options->private;
 
-	ret = getoptions(datablob, p, options);
+	ret = getoptions(datablob, p, options, tpm2);
 	if (ret < 0)
 		return ret;
 	dump_options(options);
Signature · 1 line
2.53.0

Br, Jarkko

#3 lore raw

Hi Jarkko,
Thanks for taking a look.

Quoted 36 lines · Jarkko Sakkinen
Quoted 35 lines · Srish Srinivasan

Determine the TPM version once in the seal and unseal paths and reuse it when allocating and parsing trusted key options. This avoids redundant TPM version checks.

---
  security/keys/trusted-keys/trusted_tpm1.c | 22 ++++++----------------
  1 file changed, 6 insertions(+), 16 deletions(-)
security/keys/trusted-keys/trusted_tpm1.c +1 -6
index 3ec078ca3f97..70de4101923f 100644
@@ -700,7 +700,7 @@ static const match_table_t key_tokens = {
 
  /* can have zero or more token= options */
  static int getoptions(char *c, struct trusted_key_payload *pay,
-		      struct trusted_key_options *opt)
+		      struct trusted_key_options *opt, int tpm2)
  {
  	struct trusted_key_tpm *private = opt->private;
  	substring_t args[MAX_OPT_ARGS];
@@ -712,11 +712,6 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
  	unsigned long token_mask = 0;
  	unsigned int digest_len;
  	int i;
-	int tpm2;
-
-	tpm2 = tpm_is_tpm2(chip);
-	if (tpm2 < 0)
-		return tpm2;
 
  	private->hash = tpm2 ? HASH_ALGO_SHA256 : HASH_ALGO_SHA1;
 
Quoted 8 lines

security/keys/trusted-keys/trusted_tpm1.c static int getoptions(char *c, struct trusted_key_payload *pay, · line 836

Quoted 6 lines
security/keys/trusted-keys/trusted_tpm1.c +1 -1
@@ -838,15 +833,10 @@ static int getoptions(char *c, struct trusted_key_payload *pay,
  	return 0;
  }
 
-static struct trusted_key_options *trusted_options_alloc(void)
+static struct trusted_key_options *trusted_options_alloc(int tpm2)

Probably "bool is_tpm2" would be better here. It does not need to be tightly coupled with the tristate acquired from tpm_is_tpm2.

Makes sense.
Sure, I will make this change and send out v2.

Quoted 38 lines
Quoted 38 lines
security/keys/trusted-keys/trusted_tpm1.c +3 -8
  {
  	struct trusted_key_tpm *private;
  	struct trusted_key_options *options;
-	int tpm2;
-
-	tpm2 = tpm_is_tpm2(chip);
-	if (tpm2 < 0)
-		return NULL;
 
  	options = kzalloc_obj(*options);
  	if (options) {
@@ -877,13 +867,13 @@ static int trusted_tpm_seal(struct trusted_key_payload *p, char *datablob)
  	if (tpm2 < 0)
  		return tpm2;
 
-	options = trusted_options_alloc();
+	options = trusted_options_alloc(tpm2);
  	if (!options)
  		return -ENOMEM;
 
  	private = options->private;
 
-	ret = getoptions(datablob, p, options);
+	ret = getoptions(datablob, p, options, tpm2);
  	if (ret < 0)
  		return ret;
  	dump_options(options);
@@ -922,12 +912,12 @@ static int trusted_tpm_unseal(struct trusted_key_payload *p, char *datablob)
  	if (tpm2 < 0)
  		return tpm2;
 
-	options = trusted_options_alloc();
+	options = trusted_options_alloc(tpm2);
  	if (!options)
  		return -ENOMEM;
  	private = options->private;
 
Quoted 9 lines
Quoted 8 lines
security/keys/trusted-keys/trusted_tpm1.c +1 -1
-	ret = getoptions(datablob, p, options);
+	ret = getoptions(datablob, p, options, tpm2);
  	if (ret < 0)
  		return ret;
  	dump_options(options);
Signature · 1 line
2.53.0

Br, Jarkko

Thanks,
Srish.