Skip to content

Commit 5df16ca

Browse files
committed
KEYS: trusted: Fix incorrect handling of tpm_get_random()
When tpm_get_random() was introduced, it defined the following API for the return value: 1. A positive value tells how many bytes of random data was generated. 2. A negative value on error. However, in the call sites the API was used incorrectly, i.e. as it would only return negative values and otherwise zero. Returning he positive read counts to the user space does not make any possible sense. Fix this by returning -EIO when tpm_get_random() returns a positive value. Fixes: 41ab999 ("tpm: Move tpm_get_random api into the TPM device driver") Cc: stable@vger.kernel.org Cc: Mimi Zohar <zohar@linux.ibm.com> Cc: "James E.J. Bottomley" <James.Bottomley@HansenPartnership.com> Cc: David Howells <dhowells@redhat.com> Cc: Kent Yoder <key@linux.vnet.ibm.com> Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org> Reviewed-by: Mimi Zohar <zohar@linux.ibm.com>
1 parent 90cba8d commit 5df16ca

1 file changed

Lines changed: 17 additions & 3 deletions

File tree

‎security/keys/trusted-keys/trusted_tpm1.c‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,9 +403,12 @@ static int osap(struct tpm_buf *tb, struct osapsess *s,
403403
int ret;
404404

405405
ret = tpm_get_random(chip, ononce, TPM_NONCE_SIZE);
406-
if (ret != TPM_NONCE_SIZE)
406+
if (ret < 0)
407407
return ret;
408408

409+
if (ret != TPM_NONCE_SIZE)
410+
return -EIO;
411+
409412
tpm_buf_reset(tb, TPM_TAG_RQU_COMMAND, TPM_ORD_OSAP);
410413
tpm_buf_append_u16(tb, type);
411414
tpm_buf_append_u32(tb, handle);
@@ -496,8 +499,12 @@ static int tpm_seal(struct tpm_buf *tb, uint16_t keytype,
496499
goto out;
497500

498501
ret = tpm_get_random(chip, td->nonceodd, TPM_NONCE_SIZE);
502+
if (ret < 0)
503+
return ret;
504+
499505
if (ret != TPM_NONCE_SIZE)
500-
goto out;
506+
return -EIO;
507+
501508
ordinal = htonl(TPM_ORD_SEAL);
502509
datsize = htonl(datalen);
503510
pcrsize = htonl(pcrinfosize);
@@ -601,9 +608,12 @@ static int tpm_unseal(struct tpm_buf *tb,
601608

602609
ordinal = htonl(TPM_ORD_UNSEAL);
603610
ret = tpm_get_random(chip, nonceodd, TPM_NONCE_SIZE);
611+
if (ret < 0)
612+
return ret;
613+
604614
if (ret != TPM_NONCE_SIZE) {
605615
pr_info("trusted_key: tpm_get_random failed (%d)\n", ret);
606-
return ret;
616+
return -EIO;
607617
}
608618
ret = TSS_authhmac(authdata1, keyauth, TPM_NONCE_SIZE,
609619
enonce1, nonceodd, cont, sizeof(uint32_t),
@@ -1013,8 +1023,12 @@ static int trusted_instantiate(struct key *key,
10131023
case Opt_new:
10141024
key_len = payload->key_len;
10151025
ret = tpm_get_random(chip, payload->key, key_len);
1026+
if (ret < 0)
1027+
goto out;
1028+
10161029
if (ret != key_len) {
10171030
pr_info("trusted_key: key_create failed (%d)\n", ret);
1031+
ret = -EIO;
10181032
goto out;
10191033
}
10201034
if (tpm2)

0 commit comments

Comments
 (0)