Skip to content

Commit 028bd4a

Browse files
committed
Merge tag 'tpmdd-next-6.19-rc1-v4' of git://git.kernel.org/pub/scm/linux/kernel/git/jarkko/linux-tpmdd
Pull tpm updates from Jarkko Sakkinen: "This contains changes to unify TPM return code translation between trusted_tpm2 and TPM driver itself. Other than that the changes are either bug fixes or minor imrovements" * tag 'tpmdd-next-6.19-rc1-v4' of git://git.kernel.org/pub/scm/linux/kernel/git/jarkko/linux-tpmdd: KEYS: trusted: Use tpm_ret_to_err() in trusted_tpm2 tpm: Use -EPERM as fallback error code in tpm_ret_to_err tpm: Cap the number of PCR banks tpm: Remove tpm_find_get_ops tpm: add WQ_PERCPU to alloc_workqueue users tpm_crb: add missing loc parameter to kerneldoc tpm_crb: Fix a spelling mistake selftests: tpm2: Fix ill defined assertions
2 parents 16460bf + 09b71a5 commit 028bd4a

11 files changed

Lines changed: 42 additions & 81 deletions

File tree

drivers/char/tpm/tpm-chip.c

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -230,42 +230,6 @@ struct tpm_chip *tpm_default_chip(void)
230230
}
231231
EXPORT_SYMBOL_GPL(tpm_default_chip);
232232

233-
/**
234-
* tpm_find_get_ops() - find and reserve a TPM chip
235-
* @chip: a &struct tpm_chip instance, %NULL for the default chip
236-
*
237-
* Finds a TPM chip and reserves its class device and operations. The chip must
238-
* be released with tpm_put_ops() after use.
239-
* This function is for internal use only. It supports existing TPM callers
240-
* by accepting NULL, but those callers should be converted to pass in a chip
241-
* directly.
242-
*
243-
* Return:
244-
* A reserved &struct tpm_chip instance.
245-
* %NULL if a chip is not found.
246-
* %NULL if the chip is not available.
247-
*/
248-
struct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip)
249-
{
250-
int rc;
251-
252-
if (chip) {
253-
if (!tpm_try_get_ops(chip))
254-
return chip;
255-
return NULL;
256-
}
257-
258-
chip = tpm_default_chip();
259-
if (!chip)
260-
return NULL;
261-
rc = tpm_try_get_ops(chip);
262-
/* release additional reference we got from tpm_default_chip() */
263-
put_device(&chip->dev);
264-
if (rc)
265-
return NULL;
266-
return chip;
267-
}
268-
269233
/**
270234
* tpm_dev_release() - free chip memory and the device number
271235
* @dev: the character device for the TPM chip
@@ -282,7 +246,6 @@ static void tpm_dev_release(struct device *dev)
282246

283247
kfree(chip->work_space.context_buf);
284248
kfree(chip->work_space.session_buf);
285-
kfree(chip->allocated_banks);
286249
#ifdef CONFIG_TCG_TPM2_HMAC
287250
kfree(chip->auth);
288251
#endif

drivers/char/tpm/tpm-dev-common.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ void tpm_common_release(struct file *file, struct file_priv *priv)
275275

276276
int __init tpm_dev_common_init(void)
277277
{
278-
tpm_dev_wq = alloc_workqueue("tpm_dev_wq", WQ_MEM_RECLAIM, 0);
278+
tpm_dev_wq = alloc_workqueue("tpm_dev_wq", WQ_MEM_RECLAIM | WQ_PERCPU,
279+
0);
279280

280281
return !tpm_dev_wq ? -ENOMEM : 0;
281282
}

drivers/char/tpm/tpm-interface.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,13 @@ int tpm_is_tpm2(struct tpm_chip *chip)
313313
{
314314
int rc;
315315

316-
chip = tpm_find_get_ops(chip);
317316
if (!chip)
318317
return -ENODEV;
319318

319+
rc = tpm_try_get_ops(chip);
320+
if (rc)
321+
return rc;
322+
320323
rc = (chip->flags & TPM_CHIP_FLAG_TPM2) != 0;
321324

322325
tpm_put_ops(chip);
@@ -338,10 +341,13 @@ int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
338341
{
339342
int rc;
340343

341-
chip = tpm_find_get_ops(chip);
342344
if (!chip)
343345
return -ENODEV;
344346

347+
rc = tpm_try_get_ops(chip);
348+
if (rc)
349+
return rc;
350+
345351
if (chip->flags & TPM_CHIP_FLAG_TPM2)
346352
rc = tpm2_pcr_read(chip, pcr_idx, digest, NULL);
347353
else
@@ -369,10 +375,13 @@ int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
369375
int rc;
370376
int i;
371377

372-
chip = tpm_find_get_ops(chip);
373378
if (!chip)
374379
return -ENODEV;
375380

381+
rc = tpm_try_get_ops(chip);
382+
if (rc)
383+
return rc;
384+
376385
for (i = 0; i < chip->nr_allocated_banks; i++) {
377386
if (digests[i].alg_id != chip->allocated_banks[i].alg_id) {
378387
rc = -EINVAL;
@@ -492,10 +501,13 @@ int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)
492501
if (!out || max > TPM_MAX_RNG_DATA)
493502
return -EINVAL;
494503

495-
chip = tpm_find_get_ops(chip);
496504
if (!chip)
497505
return -ENODEV;
498506

507+
rc = tpm_try_get_ops(chip);
508+
if (rc)
509+
return rc;
510+
499511
if (chip->flags & TPM_CHIP_FLAG_TPM2)
500512
rc = tpm2_get_random(chip, out, max);
501513
else

drivers/char/tpm/tpm.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,6 @@ static inline void tpm_msleep(unsigned int delay_msec)
267267
int tpm_chip_bootstrap(struct tpm_chip *chip);
268268
int tpm_chip_start(struct tpm_chip *chip);
269269
void tpm_chip_stop(struct tpm_chip *chip);
270-
struct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip);
271270

272271
struct tpm_chip *tpm_chip_alloc(struct device *dev,
273272
const struct tpm_class_ops *ops);

drivers/char/tpm/tpm1-cmd.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -799,11 +799,6 @@ int tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr)
799799
*/
800800
int tpm1_get_pcr_allocation(struct tpm_chip *chip)
801801
{
802-
chip->allocated_banks = kcalloc(1, sizeof(*chip->allocated_banks),
803-
GFP_KERNEL);
804-
if (!chip->allocated_banks)
805-
return -ENOMEM;
806-
807802
chip->allocated_banks[0].alg_id = TPM_ALG_SHA1;
808803
chip->allocated_banks[0].digest_size = hash_digest_size[HASH_ALGO_SHA1];
809804
chip->allocated_banks[0].crypto_id = HASH_ALGO_SHA1;

drivers/char/tpm/tpm2-cmd.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -550,11 +550,9 @@ ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
550550

551551
nr_possible_banks = be32_to_cpup(
552552
(__be32 *)&buf.data[TPM_HEADER_SIZE + 5]);
553-
554-
chip->allocated_banks = kcalloc(nr_possible_banks,
555-
sizeof(*chip->allocated_banks),
556-
GFP_KERNEL);
557-
if (!chip->allocated_banks) {
553+
if (nr_possible_banks > TPM2_MAX_PCR_BANKS) {
554+
pr_err("tpm: out of bank capacity: %u > %u\n",
555+
nr_possible_banks, TPM2_MAX_PCR_BANKS);
558556
rc = -ENOMEM;
559557
goto out;
560558
}

drivers/char/tpm/tpm_crb.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ static int crb_try_pluton_doorbell(struct crb_priv *priv, bool wait_for_complete
179179
*
180180
* @dev: crb device
181181
* @priv: crb private data
182+
* @loc: locality
182183
*
183184
* Write CRB_CTRL_REQ_GO_IDLE to TPM_CRB_CTRL_REQ
184185
* The device should respond within TIMEOUT_C by clearing the bit.
@@ -233,6 +234,7 @@ static int crb_go_idle(struct tpm_chip *chip)
233234
*
234235
* @dev: crb device
235236
* @priv: crb private data
237+
* @loc: locality
236238
*
237239
* Write CRB_CTRL_REQ_CMD_READY to TPM_CRB_CTRL_REQ
238240
* and poll till the device acknowledge it by clearing the bit.
@@ -412,7 +414,7 @@ static int crb_do_acpi_start(struct tpm_chip *chip)
412414
#ifdef CONFIG_ARM64
413415
/*
414416
* This is a TPM Command Response Buffer start method that invokes a
415-
* Secure Monitor Call to requrest the firmware to execute or cancel
417+
* Secure Monitor Call to request the firmware to execute or cancel
416418
* a TPM 2.0 command.
417419
*/
418420
static int tpm_crb_smc_start(struct device *dev, unsigned long func_id)

drivers/char/tpm/tpm_tis_core.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,7 @@ static u8 tpm_tis_status(struct tpm_chip *chip)
265265

266266
/*
267267
* Dump stack for forensics, as invalid TPM_STS.x could be
268-
* potentially triggered by impaired tpm_try_get_ops() or
269-
* tpm_find_get_ops().
268+
* potentially triggered by impaired tpm_try_get_ops().
270269
*/
271270
dump_stack();
272271
}

include/linux/tpm.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
#include <crypto/aes.h>
2727

2828
#define TPM_DIGEST_SIZE 20 /* Max TPM v1.2 PCR size */
29-
#define TPM_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE
29+
30+
#define TPM2_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE
31+
#define TPM2_MAX_PCR_BANKS 8
3032

3133
struct tpm_chip;
3234
struct trusted_key_payload;
@@ -68,7 +70,7 @@ enum tpm2_curves {
6870

6971
struct tpm_digest {
7072
u16 alg_id;
71-
u8 digest[TPM_MAX_DIGEST_SIZE];
73+
u8 digest[TPM2_MAX_DIGEST_SIZE];
7274
} __packed;
7375

7476
struct tpm_bank_info {
@@ -189,7 +191,7 @@ struct tpm_chip {
189191
unsigned int groups_cnt;
190192

191193
u32 nr_allocated_banks;
192-
struct tpm_bank_info *allocated_banks;
194+
struct tpm_bank_info allocated_banks[TPM2_MAX_PCR_BANKS];
193195
#ifdef CONFIG_ACPI
194196
acpi_handle acpi_dev_handle;
195197
char ppi_version[TPM_PPI_VERSION_LEN + 1];
@@ -454,8 +456,10 @@ static inline ssize_t tpm_ret_to_err(ssize_t ret)
454456
return 0;
455457
case TPM2_RC_SESSION_MEMORY:
456458
return -ENOMEM;
459+
case TPM2_RC_HASH:
460+
return -EINVAL;
457461
default:
458-
return -EFAULT;
462+
return -EPERM;
459463
}
460464
}
461465

security/keys/trusted-keys/trusted_tpm2.c

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -333,25 +333,19 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
333333
}
334334

335335
blob_len = tpm2_key_encode(payload, options, &buf.data[offset], blob_len);
336+
if (blob_len < 0)
337+
rc = blob_len;
336338

337339
out:
338340
tpm_buf_destroy(&sized);
339341
tpm_buf_destroy(&buf);
340342

341-
if (rc > 0) {
342-
if (tpm2_rc_value(rc) == TPM2_RC_HASH)
343-
rc = -EINVAL;
344-
else
345-
rc = -EPERM;
346-
}
347-
if (blob_len < 0)
348-
rc = blob_len;
349-
else
343+
if (!rc)
350344
payload->blob_len = blob_len;
351345

352346
out_put:
353347
tpm_put_ops(chip);
354-
return rc;
348+
return tpm_ret_to_err(rc);
355349
}
356350

357351
/**
@@ -455,10 +449,7 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
455449
out:
456450
tpm_buf_destroy(&buf);
457451

458-
if (rc > 0)
459-
rc = -EPERM;
460-
461-
return rc;
452+
return tpm_ret_to_err(rc);
462453
}
463454

464455
/**
@@ -521,8 +512,6 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
521512
tpm_buf_fill_hmac_session(chip, &buf);
522513
rc = tpm_transmit_cmd(chip, &buf, 6, "unsealing");
523514
rc = tpm_buf_check_hmac_response(chip, &buf, rc);
524-
if (rc > 0)
525-
rc = -EPERM;
526515

527516
if (!rc) {
528517
data_len = be16_to_cpup(
@@ -555,7 +544,7 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,
555544

556545
out:
557546
tpm_buf_destroy(&buf);
558-
return rc;
547+
return tpm_ret_to_err(rc);
559548
}
560549

561550
/**
@@ -587,6 +576,5 @@ int tpm2_unseal_trusted(struct tpm_chip *chip,
587576

588577
out:
589578
tpm_put_ops(chip);
590-
591-
return rc;
579+
return tpm_ret_to_err(rc);
592580
}

0 commit comments

Comments
 (0)