Skip to content

Commit 879b589

Browse files
robertosassuJarkko Sakkinen
authored andcommitted
tpm: retrieve digest size of unknown algorithms with PCR read
Currently, the TPM driver retrieves the digest size from a table mapping TPM algorithms identifiers to identifiers defined by the crypto subsystem. If the algorithm is not defined by the latter, the digest size can be retrieved from the output of the PCR read command. The patch modifies the definition of tpm_pcr_read() and tpm2_pcr_read() to pass the desired hash algorithm and obtain the digest size at TPM startup. Algorithms and corresponding digest sizes are stored in the new structure tpm_bank_info, member of tpm_chip, so that the information can be used by other kernel subsystems. tpm_bank_info contains: the TPM algorithm identifier, necessary to generate the event log as defined by Trusted Computing Group (TCG); the digest size, to pad/truncate a digest calculated with a different algorithm; the crypto subsystem identifier, to calculate the digest of event data. This patch also protects against data corruption that could happen in the bus, by checking that the digest size returned by the TPM during a PCR read matches the size of the algorithm passed to tpm2_pcr_read(). For the initial PCR read, when digest sizes are not yet available, this patch ensures that the amount of data copied from the output returned by the TPM does not exceed the size of the array data are copied to. Signed-off-by: Roberto Sassu <[email protected]> Reviewed-by: Jarkko Sakkinen <[email protected]> Tested-by: Jarkko Sakkinen <[email protected]> Acked-by: Mimi Zohar <[email protected]> Signed-off-by: Jarkko Sakkinen <[email protected]>
1 parent aa04247 commit 879b589

File tree

6 files changed

+96
-36
lines changed

6 files changed

+96
-36
lines changed

drivers/char/tpm/tpm-interface.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,12 @@ EXPORT_SYMBOL_GPL(tpm_is_tpm2);
281281
* tpm_pcr_read - read a PCR value from SHA1 bank
282282
* @chip: a &struct tpm_chip instance, %NULL for the default chip
283283
* @pcr_idx: the PCR to be retrieved
284-
* @res_buf: the value of the PCR
284+
* @digest: the PCR bank and buffer current PCR value is written to
285285
*
286286
* Return: same as with tpm_transmit_cmd()
287287
*/
288-
int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf)
288+
int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
289+
struct tpm_digest *digest)
289290
{
290291
int rc;
291292

@@ -294,9 +295,9 @@ int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf)
294295
return -ENODEV;
295296

296297
if (chip->flags & TPM_CHIP_FLAG_TPM2)
297-
rc = tpm2_pcr_read(chip, pcr_idx, res_buf);
298+
rc = tpm2_pcr_read(chip, pcr_idx, digest, NULL);
298299
else
299-
rc = tpm1_pcr_read(chip, pcr_idx, res_buf);
300+
rc = tpm1_pcr_read(chip, pcr_idx, digest->digest);
300301

301302
tpm_put_ops(chip);
302303
return rc;
@@ -309,9 +310,8 @@ EXPORT_SYMBOL_GPL(tpm_pcr_read);
309310
* @pcr_idx: the PCR to be retrieved
310311
* @hash: the hash value used to extend the PCR value
311312
*
312-
* Note: with TPM 2.0 extends also those banks with a known digest size to the
313-
* cryto subsystem in order to prevent malicious use of those PCR banks. In the
314-
* future we should dynamically determine digest sizes.
313+
* Note: with TPM 2.0 extends also those banks for which no digest was
314+
* specified in order to prevent malicious use of those PCR banks.
315315
*
316316
* Return: same as with tpm_transmit_cmd()
317317
*/
@@ -332,7 +332,7 @@ int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash)
332332
return -ENOMEM;
333333

334334
for (i = 0; i < chip->nr_allocated_banks; i++) {
335-
digest_list[i].alg_id = chip->allocated_banks[i];
335+
digest_list[i].alg_id = chip->allocated_banks[i].alg_id;
336336
memcpy(digest_list[i].digest, hash, TPM_DIGEST_SIZE);
337337
}
338338

drivers/char/tpm/tpm.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ struct tpm_chip {
247247
unsigned int groups_cnt;
248248

249249
u32 nr_allocated_banks;
250-
u16 *allocated_banks;
250+
struct tpm_bank_info *allocated_banks;
251251
#ifdef CONFIG_ACPI
252252
acpi_handle acpi_dev_handle;
253253
char ppi_version[TPM_PPI_VERSION_LEN + 1];
@@ -532,7 +532,8 @@ static inline u32 tpm2_rc_value(u32 rc)
532532
}
533533

534534
int tpm2_get_timeouts(struct tpm_chip *chip);
535-
int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf);
535+
int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
536+
struct tpm_digest *digest, u16 *digest_size_ptr);
536537
int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, u32 count,
537538
struct tpm_digest *digests);
538539
int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max);

drivers/char/tpm/tpm1-cmd.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,9 @@ int tpm1_auto_startup(struct tpm_chip *chip)
703703
goto out;
704704
}
705705

706-
chip->allocated_banks[0] = TPM_ALG_SHA1;
706+
chip->allocated_banks[0].alg_id = TPM_ALG_SHA1;
707+
chip->allocated_banks[0].digest_size = hash_digest_size[HASH_ALGO_SHA1];
708+
chip->allocated_banks[0].crypto_id = HASH_ALGO_SHA1;
707709
chip->nr_allocated_banks = 1;
708710

709711
return rc;

drivers/char/tpm/tpm2-cmd.c

Lines changed: 67 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -171,39 +171,65 @@ struct tpm2_pcr_read_out {
171171
* tpm2_pcr_read() - read a PCR value
172172
* @chip: TPM chip to use.
173173
* @pcr_idx: index of the PCR to read.
174-
* @res_buf: buffer to store the resulting hash.
174+
* @digest: PCR bank and buffer current PCR value is written to.
175+
* @digest_size_ptr: pointer to variable that stores the digest size.
175176
*
176177
* Return: Same as with tpm_transmit_cmd.
177178
*/
178-
int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf)
179+
int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
180+
struct tpm_digest *digest, u16 *digest_size_ptr)
179181
{
182+
int i;
180183
int rc;
181184
struct tpm_buf buf;
182185
struct tpm2_pcr_read_out *out;
183186
u8 pcr_select[TPM2_PCR_SELECT_MIN] = {0};
187+
u16 digest_size;
188+
u16 expected_digest_size = 0;
184189

185190
if (pcr_idx >= TPM2_PLATFORM_PCR)
186191
return -EINVAL;
187192

193+
if (!digest_size_ptr) {
194+
for (i = 0; i < chip->nr_allocated_banks &&
195+
chip->allocated_banks[i].alg_id != digest->alg_id; i++)
196+
;
197+
198+
if (i == chip->nr_allocated_banks)
199+
return -EINVAL;
200+
201+
expected_digest_size = chip->allocated_banks[i].digest_size;
202+
}
203+
188204
rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_PCR_READ);
189205
if (rc)
190206
return rc;
191207

192208
pcr_select[pcr_idx >> 3] = 1 << (pcr_idx & 0x7);
193209

194210
tpm_buf_append_u32(&buf, 1);
195-
tpm_buf_append_u16(&buf, TPM_ALG_SHA1);
211+
tpm_buf_append_u16(&buf, digest->alg_id);
196212
tpm_buf_append_u8(&buf, TPM2_PCR_SELECT_MIN);
197213
tpm_buf_append(&buf, (const unsigned char *)pcr_select,
198214
sizeof(pcr_select));
199215

200-
rc = tpm_transmit_cmd(chip, &buf, 0, res_buf ?
201-
"attempting to read a pcr value" : NULL);
202-
if (rc == 0 && res_buf) {
203-
out = (struct tpm2_pcr_read_out *)&buf.data[TPM_HEADER_SIZE];
204-
memcpy(res_buf, out->digest, SHA1_DIGEST_SIZE);
216+
rc = tpm_transmit_cmd(chip, &buf, 0, "attempting to read a pcr value");
217+
if (rc)
218+
goto out;
219+
220+
out = (struct tpm2_pcr_read_out *)&buf.data[TPM_HEADER_SIZE];
221+
digest_size = be16_to_cpu(out->digest_size);
222+
if (digest_size > sizeof(digest->digest) ||
223+
(!digest_size_ptr && digest_size != expected_digest_size)) {
224+
rc = -EINVAL;
225+
goto out;
205226
}
206227

228+
if (digest_size_ptr)
229+
*digest_size_ptr = digest_size;
230+
231+
memcpy(digest->digest, out->digest, digest_size);
232+
out:
207233
tpm_buf_destroy(&buf);
208234
return rc;
209235
}
@@ -232,7 +258,6 @@ int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, u32 count,
232258
struct tpm2_null_auth_area auth_area;
233259
int rc;
234260
int i;
235-
int j;
236261

237262
if (count > chip->nr_allocated_banks)
238263
return -EINVAL;
@@ -254,14 +279,9 @@ int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, u32 count,
254279
tpm_buf_append_u32(&buf, count);
255280

256281
for (i = 0; i < count; i++) {
257-
for (j = 0; j < ARRAY_SIZE(tpm2_hash_map); j++) {
258-
if (digests[i].alg_id != tpm2_hash_map[j].tpm_id)
259-
continue;
260-
tpm_buf_append_u16(&buf, digests[i].alg_id);
261-
tpm_buf_append(&buf, (const unsigned char
262-
*)&digests[i].digest,
263-
hash_digest_size[tpm2_hash_map[j].crypto_id]);
264-
}
282+
tpm_buf_append_u16(&buf, digests[i].alg_id);
283+
tpm_buf_append(&buf, (const unsigned char *)&digests[i].digest,
284+
chip->allocated_banks[i].digest_size);
265285
}
266286

267287
rc = tpm_transmit_cmd(chip, &buf, 0, "attempting extend a PCR value");
@@ -795,6 +815,30 @@ int tpm2_probe(struct tpm_chip *chip)
795815
}
796816
EXPORT_SYMBOL_GPL(tpm2_probe);
797817

818+
static int tpm2_init_bank_info(struct tpm_chip *chip, u32 bank_index)
819+
{
820+
struct tpm_bank_info *bank = chip->allocated_banks + bank_index;
821+
struct tpm_digest digest = { .alg_id = bank->alg_id };
822+
int i;
823+
824+
/*
825+
* Avoid unnecessary PCR read operations to reduce overhead
826+
* and obtain identifiers of the crypto subsystem.
827+
*/
828+
for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++) {
829+
enum hash_algo crypto_algo = tpm2_hash_map[i].crypto_id;
830+
831+
if (bank->alg_id != tpm2_hash_map[i].tpm_id)
832+
continue;
833+
834+
bank->digest_size = hash_digest_size[crypto_algo];
835+
bank->crypto_id = crypto_algo;
836+
return 0;
837+
}
838+
839+
return tpm2_pcr_read(chip, 0, &digest, &bank->digest_size);
840+
}
841+
798842
struct tpm2_pcr_selection {
799843
__be16 hash_alg;
800844
u8 size_of_select;
@@ -858,7 +902,12 @@ static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
858902
pcr_select_offset = memchr_inv(pcr_selection.pcr_select, 0,
859903
pcr_selection.size_of_select);
860904
if (pcr_select_offset) {
861-
chip->allocated_banks[nr_alloc_banks] = hash_alg;
905+
chip->allocated_banks[nr_alloc_banks].alg_id = hash_alg;
906+
907+
rc = tpm2_init_bank_info(chip, nr_alloc_banks);
908+
if (rc < 0)
909+
break;
910+
862911
nr_alloc_banks++;
863912
}
864913

include/linux/tpm.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ struct tpm_digest {
4747
u8 digest[TPM_MAX_DIGEST_SIZE];
4848
} __packed;
4949

50+
struct tpm_bank_info {
51+
u16 alg_id;
52+
u16 digest_size;
53+
u16 crypto_id;
54+
};
55+
5056
enum TPM_OPS_FLAGS {
5157
TPM_OPS_AUTO_STARTUP = BIT(0),
5258
};
@@ -72,7 +78,8 @@ struct tpm_class_ops {
7278
#if defined(CONFIG_TCG_TPM) || defined(CONFIG_TCG_TPM_MODULE)
7379

7480
extern int tpm_is_tpm2(struct tpm_chip *chip);
75-
extern int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf);
81+
extern int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
82+
struct tpm_digest *digest);
7683
extern int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash);
7784
extern int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen);
7885
extern int tpm_get_random(struct tpm_chip *chip, u8 *data, size_t max);
@@ -89,7 +96,8 @@ static inline int tpm_is_tpm2(struct tpm_chip *chip)
8996
return -ENODEV;
9097
}
9198

92-
static inline int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf)
99+
static inline int tpm_pcr_read(struct tpm_chip *chip, int pcr_idx,
100+
struct tpm_digest *digest)
93101
{
94102
return -ENODEV;
95103
}

security/integrity/ima/ima_crypto.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -643,12 +643,12 @@ int ima_calc_buffer_hash(const void *buf, loff_t len,
643643
return calc_buffer_shash(buf, len, hash);
644644
}
645645

646-
static void __init ima_pcrread(u32 idx, u8 *pcr)
646+
static void __init ima_pcrread(u32 idx, struct tpm_digest *d)
647647
{
648648
if (!ima_tpm_chip)
649649
return;
650650

651-
if (tpm_pcr_read(ima_tpm_chip, idx, pcr) != 0)
651+
if (tpm_pcr_read(ima_tpm_chip, idx, d) != 0)
652652
pr_err("Error Communicating to TPM chip\n");
653653
}
654654

@@ -658,7 +658,7 @@ static void __init ima_pcrread(u32 idx, u8 *pcr)
658658
static int __init ima_calc_boot_aggregate_tfm(char *digest,
659659
struct crypto_shash *tfm)
660660
{
661-
u8 pcr_i[TPM_DIGEST_SIZE];
661+
struct tpm_digest d = { .alg_id = TPM_ALG_SHA1, .digest = {0} };
662662
int rc;
663663
u32 i;
664664
SHASH_DESC_ON_STACK(shash, tfm);
@@ -672,9 +672,9 @@ static int __init ima_calc_boot_aggregate_tfm(char *digest,
672672

673673
/* cumulative sha1 over tpm registers 0-7 */
674674
for (i = TPM_PCR0; i < TPM_PCR8; i++) {
675-
ima_pcrread(i, pcr_i);
675+
ima_pcrread(i, &d);
676676
/* now accumulate with current aggregate */
677-
rc = crypto_shash_update(shash, pcr_i, TPM_DIGEST_SIZE);
677+
rc = crypto_shash_update(shash, d.digest, TPM_DIGEST_SIZE);
678678
}
679679
if (!rc)
680680
crypto_shash_final(shash, digest);

0 commit comments

Comments
 (0)