Skip to content

Commit 7fc2cd2

Browse files
committed
Merge tag 'keys-trusted-next-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/jarkko/linux-tpmdd
Pull trusted key updates from Jarkko Sakkinen: - Remove duplicate 'tpm2_hash_map' in favor of 'tpm2_find_hash_alg()' - Fix a memory leak on failure paths of 'tpm2_load_cmd' * tag 'keys-trusted-next-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/jarkko/linux-tpmdd: KEYS: trusted: Fix a memory leak in tpm2_load_cmd KEYS: trusted: Replace a redundant instance of tpm2_hash_map
2 parents b082c4b + 62cd5d4 commit 7fc2cd2

3 files changed

Lines changed: 22 additions & 22 deletions

File tree

drivers/char/tpm/tpm2-cmd.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,26 @@ static bool disable_pcr_integrity;
1818
module_param(disable_pcr_integrity, bool, 0444);
1919
MODULE_PARM_DESC(disable_pcr_integrity, "Disable integrity protection of TPM2_PCR_Extend");
2020

21-
static struct tpm2_hash tpm2_hash_map[] = {
21+
struct tpm2_hash tpm2_hash_map[] = {
2222
{HASH_ALGO_SHA1, TPM_ALG_SHA1},
2323
{HASH_ALGO_SHA256, TPM_ALG_SHA256},
2424
{HASH_ALGO_SHA384, TPM_ALG_SHA384},
2525
{HASH_ALGO_SHA512, TPM_ALG_SHA512},
2626
{HASH_ALGO_SM3_256, TPM_ALG_SM3_256},
2727
};
2828

29+
int tpm2_find_hash_alg(unsigned int crypto_id)
30+
{
31+
int i;
32+
33+
for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++)
34+
if (crypto_id == tpm2_hash_map[i].crypto_id)
35+
return tpm2_hash_map[i].tpm_id;
36+
37+
return -EINVAL;
38+
}
39+
EXPORT_SYMBOL_GPL(tpm2_find_hash_alg);
40+
2941
int tpm2_get_timeouts(struct tpm_chip *chip)
3042
{
3143
chip->timeout_a = msecs_to_jiffies(TPM2_TIMEOUT_A);

include/linux/tpm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ extern int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
473473
extern int tpm_get_random(struct tpm_chip *chip, u8 *data, size_t max);
474474
extern struct tpm_chip *tpm_default_chip(void);
475475
void tpm2_flush_context(struct tpm_chip *chip, u32 handle);
476+
int tpm2_find_hash_alg(unsigned int crypto_id);
476477

477478
static inline void tpm_buf_append_empty_auth(struct tpm_buf *buf, u32 handle)
478479
{

security/keys/trusted-keys/trusted_tpm2.c

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,6 @@
1818

1919
#include "tpm2key.asn1.h"
2020

21-
static struct tpm2_hash tpm2_hash_map[] = {
22-
{HASH_ALGO_SHA1, TPM_ALG_SHA1},
23-
{HASH_ALGO_SHA256, TPM_ALG_SHA256},
24-
{HASH_ALGO_SHA384, TPM_ALG_SHA384},
25-
{HASH_ALGO_SHA512, TPM_ALG_SHA512},
26-
{HASH_ALGO_SM3_256, TPM_ALG_SM3_256},
27-
};
28-
2921
static u32 tpm2key_oid[] = { 2, 23, 133, 10, 1, 5 };
3022

3123
static int tpm2_key_encode(struct trusted_key_payload *payload,
@@ -244,20 +236,13 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
244236
off_t offset = TPM_HEADER_SIZE;
245237
struct tpm_buf buf, sized;
246238
int blob_len = 0;
247-
u32 hash;
239+
int hash;
248240
u32 flags;
249-
int i;
250241
int rc;
251242

252-
for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++) {
253-
if (options->hash == tpm2_hash_map[i].crypto_id) {
254-
hash = tpm2_hash_map[i].tpm_id;
255-
break;
256-
}
257-
}
258-
259-
if (i == ARRAY_SIZE(tpm2_hash_map))
260-
return -EINVAL;
243+
hash = tpm2_find_hash_alg(options->hash);
244+
if (hash < 0)
245+
return hash;
261246

262247
if (!options->keyhandle)
263248
return -EINVAL;
@@ -387,6 +372,7 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
387372
struct trusted_key_options *options,
388373
u32 *blob_handle)
389374
{
375+
u8 *blob_ref __free(kfree) = NULL;
390376
struct tpm_buf buf;
391377
unsigned int private_len;
392378
unsigned int public_len;
@@ -400,6 +386,9 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
400386
/* old form */
401387
blob = payload->blob;
402388
payload->old_format = 1;
389+
} else {
390+
/* Bind for cleanup: */
391+
blob_ref = blob;
403392
}
404393

405394
/* new format carries keyhandle but old format doesn't */
@@ -464,8 +453,6 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
464453
(__be32 *) &buf.data[TPM_HEADER_SIZE]);
465454

466455
out:
467-
if (blob != payload->blob)
468-
kfree(blob);
469456
tpm_buf_destroy(&buf);
470457

471458
if (rc > 0)

0 commit comments

Comments
 (0)