Streamline TPM2 HMAC sessions
Subsystems (4)
Jarkko Sakkinen
maintainerv9 cover Streamline TPM2 HMAC sessions
This patch set contains accumulated patches, which gradually improve TPM2 HMAC session management and TPM driver memory management.
RNG test
========
I run this test both TPM1 and TPM2 chips using QEMU and swtpm:
#!/bin/sh
ctrl_c() {
set +e
echo 0 > tracing_on
echo nop > current_tracer
echo BYE
exit
}
trap ctrl_c EXIT INT
mount -t tracefs none /sys/kernel/tracing
set -e
cd /sys/kernel/tracing
echo function > current_tracer
echo p:tpm_get_random tpm_get_random > kprobe_events echo tpm_get_random > set_ftrace_filter
echo 1 > tracing_on
cat /dev/hwrng > /dev/null &
echo > trace
cat trace_pipe &
sleep 10
Change Log
==========
v9:
- Simplified hwrng patches.
- Minor tweaks.
v8:
- Patch was a bit out-of-sync after piling new stuff. Now it is somewhat
sane: RNG patches first, then tpm2-sessions and finally managed tpm_buf allocations.
- I added inline comment on explaining why unconditional sha256_update() call is safe to do when managing only single authorization handle.
v7:
- Updated cover letter to match better the current state of the patch set.
v6:
- OK, so I decided to send one more update with managed allocations moved to the tail so that it does not block reviewing more trivial patches.
- Trimmed some of the patches and improved commit messages. v5:
- I decided to add the managed allocation patch to this and take it from the master branch for the time being, as it needs more eyes despite having already one reviewed-by tag (especially tested-by tags).
Jarkko Sakkinen (11):
KEYS: trusted: Use get_random-fallback for TPM
KEYS: trusted: Use get_random_bytes_wait() instead of tpm_get_random()
tpm: Change tpm_get_random() opportunistic
tpm2-sessions: Define TPM2_NAME_MAX_SIZE
KEYS: trusted: Open code tpm2_buf_append()
KEYS: trusted: Remove dead branch from tpm2_unseal_cmd
KEYS: trusted: Re-orchestrate tpm2_read_public() calls
tpm2-sessions: Remove the support for more than one authorization
tpm-buf: Remove tpm_buf_append_handle
tpm-buf: Merge TPM_BUF_BOUNDARY_ERROR and TPM_BUF_OVERFLOW
tpm-buf: Implement managed allocations
drivers/char/tpm/tpm-buf.c | 156 ++++----- drivers/char/tpm/tpm-interface.c | 20 +- drivers/char/tpm/tpm-sysfs.c | 23 +- drivers/char/tpm/tpm.h | 1 - drivers/char/tpm/tpm1-cmd.c | 201 +++++------ drivers/char/tpm/tpm2-cmd.c | 386 ++++++++++------------ drivers/char/tpm/tpm2-sessions.c | 281 ++++++---------- drivers/char/tpm/tpm2-space.c | 44 +-- drivers/char/tpm/tpm_vtpm_proxy.c | 30 +- include/linux/tpm.h | 77 +++-- security/keys/trusted-keys/trusted_tpm1.c | 70 ++-- security/keys/trusted-keys/trusted_tpm2.c | 327 +++++++++--------- 12 files changed, 735 insertions(+), 881 deletions(-)
Signature · 1 line
2.52.0
Jarkko Sakkinen
maintainerPATCH v9 1/11 KEYS: trusted: Use get_random-fallback for TPM
1. tpm2_get_random() is costly when TCG_TPM2_HMAC is enabled and thus its use should be pooled rather than directly used. This both reduces latency and improves its predictability.
2. Linux is better off overall if every subsystem uses the same source for generating the random numbers required.
Thus, unset '.get_random', which causes fallback to kernel_get_random().
One might argue that TPM RNG should be used for the generated trusted keys, so that they have matching entropy with the TPM internally generated objects.
This argument does have some weight into it but as far cryptography goes, FIPS certification sets the exact bar, not which exact FIPS certified RNG will be used. Thus, the rational choice is obviously to pick the lowest latency path, which is kernel RNG.
Finally, there is an actual defence in depth benefit when using kernel RNG as it helps to mitigate TPM firmware bugs concerning RNG implementation, given the obfuscation by the other entropy sources.
---
v7:
- A new patch. Simplifies follow up patches.
--- security/keys/trusted-keys/trusted_tpm1.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
security/keys/trusted-keys/trusted_tpm1.c +10 -6
@@ -6,6 +6,16 @@* See Documentation/security/keys/trusted-encrypted.rst */ +/** +* DOC: Random Number Generation +* +* tpm_get_random() was previously used here as the RNG in order to have equal +* entropy with the objects fully inside the TPM. However, as far as goes, +* kernel RNG is equally fine, as long as long as it is FIPS certified. Also, +* using kernel RNG has the benefit of mitigating bugs in the TPM firmware +* associated with the RNG. +*/ +#include <crypto/hash_info.h> #include <crypto/sha1.h> #include <crypto/utils.h>
@@ -936,11 +946,6 @@ static int trusted_tpm_unseal(struct trusted_key_payload *p, char *datablob)return ret; } -static int trusted_tpm_get_random (unsigned char *key,size_t key_len) -{ -return tpm_get_random (chip, key, key_len); -} -static int __init init_digests (void ) {int i;
@@ -992,6 +997,5 @@ struct trusted_key_ops trusted_key_tpm_ops = { .init = trusted_tpm_init, .seal = trusted_tpm_seal, .unseal = trusted_tpm_unseal, - .get_random = trusted_tpm_get_random, .exit = trusted_tpm_exit, };
Signature · 1 line
2.52.0
Re: [PATCH v9 01/11] KEYS: trusted: Use get_random-fallback for TPM
Quoted 38 lines · Jarkko Sakkinen
1.
tpm2_get_random()is costly whenTCG_TPM2_HMACis enabled and thus its use should be pooled rather than directly used. This both reduces latency and improves its predictability.2. Linux is better off overall if every subsystem uses the same source for generating the random numbers required.
Thus, unset '.get_random', which causes fallback to
kernel_get_random().One might argue that TPM RNG should be used for the generated trusted keys, so that they have matching entropy with the TPM internally generated objects.
This argument does have some weight into it but as far cryptography goes, FIPS certification sets the exact bar, not which exact FIPS certified RNG will be used. Thus, the rational choice is obviously to pick the lowest latency path, which is kernel RNG.
Finally, there is an actual defence in depth benefit when using kernel RNG as it helps to mitigate TPM firmware bugs concerning RNG implementation, given the obfuscation by the other entropy sources.
Reviewed-by Eric Biggers <[email protected]>Signed-off-by Jarkko Sakkinen <[email protected]>---
v7:
- A new patch. Simplifies follow up patches.--- security/keys/trusted-keys/trusted_tpm1.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)security/keys/trusted-keys/trusted_tpm1.c +0
-0@@ -6,6 +6,16 @@* See Documentation/security/keys/trusted-encrypted.rst */
security/keys/trusted-keys/trusted_tpm1.c line 17
Quoted 10 lines
security/keys/trusted-keys/trusted_tpm1.c +9
-0+/** +* DOC: Random Number Generation +* +* tpm_get_random() was previously used here as the RNG in order to have equal +* entropy with the objects fully inside the TPM. However, as far as goes, +* kernel RNG is equally fine, as long as long as it is FIPS certified. Also, +* using kernel RNG has the benefit of mitigating bugs in the TPM firmware +* associated with the RNG. +*/
If we switch to the kernel RNG that is better, and the TPM one is flawed, I guess we are going to have big problems anyway, since the TPM random number generator is used by the TPM itself internally.
I think it makes sense to leave as it is.
Thanks
Roberto
Quoted 23 lines
security/keys/trusted-keys/trusted_tpm1.c +1
-6+#include <crypto/hash_info.h> #include <crypto/sha1.h> #include <crypto/utils.h> @@ -936,11 +946,6 @@ static int trusted_tpm_unseal(struct trusted_key_payload *p, char *datablob)return ret; } -static int trusted_tpm_get_random (unsigned char *key,size_t key_len) -{ -return tpm_get_random (chip, key, key_len); -} -static int __init init_digests (void ) {int i;@@ -992,6 +997,5 @@ struct trusted_key_ops trusted_key_tpm_ops = { .init = trusted_tpm_init, .seal = trusted_tpm_seal, .unseal = trusted_tpm_unseal, - .get_random = trusted_tpm_get_random, .exit = trusted_tpm_exit, };
Jarkko Sakkinen
maintainerRe: [PATCH v9 01/11] KEYS: trusted: Use get_random-fallback for TPM
Quoted 54 lines · Roberto Sassu
Quoted 38 lines · Jarkko Sakkinen
1.
tpm2_get_random()is costly whenTCG_TPM2_HMACis enabled and thus its use should be pooled rather than directly used. This both reduces latency and improves its predictability.2. Linux is better off overall if every subsystem uses the same source for generating the random numbers required.
Thus, unset '.get_random', which causes fallback to
kernel_get_random().One might argue that TPM RNG should be used for the generated trusted keys, so that they have matching entropy with the TPM internally generated objects.
This argument does have some weight into it but as far cryptography goes, FIPS certification sets the exact bar, not which exact FIPS certified RNG will be used. Thus, the rational choice is obviously to pick the lowest latency path, which is kernel RNG.
Finally, there is an actual defence in depth benefit when using kernel RNG as it helps to mitigate TPM firmware bugs concerning RNG implementation, given the obfuscation by the other entropy sources.
Reviewed-by Eric Biggers <[email protected]>Signed-off-by Jarkko Sakkinen <[email protected]>---
v7:
- A new patch. Simplifies follow up patches.--- security/keys/trusted-keys/trusted_tpm1.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)security/keys/trusted-keys/trusted_tpm1.c +0
-0@@ -6,6 +6,16 @@* See Documentation/security/keys/trusted-encrypted.rst */ security/keys/trusted-keys/trusted_tpm1.c line 17
Quoted 10 lines
security/keys/trusted-keys/trusted_tpm1.c +9
-0+/** +* DOC: Random Number Generation +* +* tpm_get_random() was previously used here as the RNG in order to have equal +* entropy with the objects fully inside the TPM. However, as far as goes, +* kernel RNG is equally fine, as long as long as it is FIPS certified. Also, +* using kernel RNG has the benefit of mitigating bugs in the TPM firmware +* associated with the RNG. +*/ If we switch to the kernel RNG that is better, and the TPM one is flawed, I guess we are going to have big problems anyway, since the TPM random number generator is used by the TPM itself internally.
Quoted 1 line
I think it makes sense to leave as it is.
There's neither really formal case for not doing this unless the random number provided by TPM would be opaque to kernel because as soon as CPU touches it, the "risk" matches kernel RNG generated random number.
These change do have a measurable benefit as they objectively decrease TPM traffic.
And as we probably know, security certifications do not really apply simply by using TPM RNG.
BR, Jarkko
Mimi Zohar
maintainerRe: [PATCH v9 01/11] KEYS: trusted: Use get_random-fallback for TPM
In reply to Jarkko Sakkinen[Cc: Chris Fenner, Jonathan McDowell, Roberto]
Quoted 3 lines · Jarkko Sakkinen
1.
tpm2_get_random()is costly whenTCG_TPM2_HMACis enabled and thus its use should be pooled rather than directly used. This both reduces latency and improves its predictability.
If the concern is the latency of encrypting the bus session, please remember that:
- Not all environments expose the TPM bus to sniffing.
- The current TPM trusted keys design is based on TPM RNG, but already allows it to be replaced with the kernel RNG via the "trusted_rng=kernel" boot command line option.
- The proposed patch removes that possibility for no reason.
Mimi & Elaine
Re: [PATCH v9 01/11] KEYS: trusted: Use get_random-fallback for TPM
My conclusion about TCG_TPM2_HMAC after [1] and [2] was that TPM2_TCG_HMAC doesn't (or didn't at the time) actually solve the threat model it claims to (active interposer adversaries), while dramatically increasing the cost of many kernel TPM activities beyond the amount that would have been required to just solve for passive/bus-sniffer interposer adversaries. The added symmetric crypto required to secure a TPM transaction is almost not noticeable; the big performance problem is the re-bootstrapping of the session with ECDH for every command.
My primary concern at that time was, essentially, that TCG_TPM2_HMAC punts on checking that the key that was used to secure the session was actually resident in a real TPM and not just an interposer adversary. I wrote up my understanding at
https://www.dlp.rip/decorative-cryptography, for anyone who wants a long-form opinionated take :).
Unless I'm wrong, or TCG_TPM2_HMAC has changed dramatically since August, I don't think "TPM2_TCG_HMAC makes this too costly" is a compelling reason to make a security decision. (There could be other reasons to make choices about whether to use the TPM as a source of randomness in the kernel! This just isn't one IMHO.)
The version of TCG_TPM2_HMAC that I'd like to see someday would be one that fully admits that its threat model is only passive interposers, and sets up one session upon startup and ContextSaves/ContextLoads it back into the TPM as needed in order to secure parameter encryption for e.g., GetRandom() and Unseal() calls.
[1]: https://lore.kernel.org/linux-integrity/CAMigqh2nwuRRxaLyOJ+QaTJ+XGmkQj=rMj5K9GP1bCcXp2OsBQ@mail.gmail.com/
[2]: https://lore.kernel.org/linux-integrity/[email protected]/
Thanks
Chris
Quoted 20 lines · Mimi Zohar
[Cc: Chris Fenner, Jonathan McDowell, Roberto]
Quoted 3 lines · Jarkko Sakkinen
1.
tpm2_get_random()is costly whenTCG_TPM2_HMACis enabled and thus its use should be pooled rather than directly used. This both reduces latency and improves its predictability.If the concern is the latency of encrypting the bus session, please remember that:
- Not all environments expose the TPM bus to sniffing.
- The current TPM trusted keys design is based on TPM RNG, but already allows it to be replaced with the kernel RNG via the "trusted_rng=kernel" boot command line option.
- The proposed patch removes that possibility for no reason.Mimi & Elaine
Jarkko Sakkinen
maintainerRe: [PATCH v9 01/11] KEYS: trusted: Use get_random-fallback for TPM
Quoted 23 lines · Chris Fenner
My conclusion about
TCG_TPM2_HMACafter [1] and [2] was thatTPM2_TCG_HMACdoesn't (or didn't at the time) actually solve the threat model it claims to (active interposer adversaries), while dramatically increasing the cost of many kernel TPM activities beyond the amount that would have been required to just solve for passive/bus-sniffer interposer adversaries. The added symmetric crypto required to secure a TPM transaction is almost not noticeable; the big performance problem is the re-bootstrapping of the session with ECDH for every command.My primary concern at that time was, essentially, that
TCG_TPM2_HMACpunts on checking that the key that was used to secure the session was actually resident in a real TPM and not just an interposer adversary. I wrote up my understanding at
https://www.dlp.rip/decorative-cryptography, for anyone who wants a long-form opinionated take :).Unless I'm wrong, or
TCG_TPM2_HMAChas changed dramatically since August, I don't think "TPM2_TCG_HMACmakes this too costly" is a compelling reason to make a security decision. (There could be other reasons to make choices about whether to use the TPM as a source of randomness in the kernel! This just isn't one IMHO.)
Quoted 5 lines
The version of
TCG_TPM2_HMACthat I'd like to see someday would be one that fully admits that its threat model is only passive interposers, and sets up one session upon startup and ContextSaves/ContextLoads it back into the TPM as needed in order to secure parameter encryption for e.g.,GetRandom()andUnseal()calls.
Neither agreeing nor disagreeing but this patch set clearly does not move forward and I spent already enough energy for this. For better ideas the patches are available in queue branch.
High-level takes don't move anything forward (or backward), sorry.
Quoted 28 lines
[1]: https://lore.kernel.org/linux-integrity/CAMigqh2nwuRRxaLyOJ+QaTJ+XGmkQj=rMj5K9GP1bCcXp2OsBQ@mail.gmail.com/
[2]: https://lore.kernel.org/linux-integrity/[email protected]/Thanks
ChrisQuoted 20 lines · Mimi Zohar
[Cc: Chris Fenner, Jonathan McDowell, Roberto]
Quoted 3 lines · Jarkko Sakkinen
1.
tpm2_get_random()is costly whenTCG_TPM2_HMACis enabled and thus its use should be pooled rather than directly used. This both reduces latency and improves its predictability.If the concern is the latency of encrypting the bus session, please remember that:
- Not all environments expose the TPM bus to sniffing.
- The current TPM trusted keys design is based on TPM RNG, but already allows it to be replaced with the kernel RNG via the "trusted_rng=kernel" boot command line option.
- The proposed patch removes that possibility for no reason.Mimi & Elaine
BR, Jarkko
Mimi Zohar
maintainerRe: [PATCH v9 01/11] KEYS: trusted: Use get_random-fallback for TPM
Quoted 30 lines · Jarkko Sakkinen
Quoted 23 lines · Chris Fenner
My conclusion about
TCG_TPM2_HMACafter [1] and [2] was thatTPM2_TCG_HMACdoesn't (or didn't at the time) actually solve the threat model it claims to (active interposer adversaries), while dramatically increasing the cost of many kernel TPM activities beyond the amount that would have been required to just solve for passive/bus-sniffer interposer adversaries. The added symmetric crypto required to secure a TPM transaction is almost not noticeable; the big performance problem is the re-bootstrapping of the session with ECDH for every command.My primary concern at that time was, essentially, that
TCG_TPM2_HMACpunts on checking that the key that was used to secure the session was actually resident in a real TPM and not just an interposer adversary. I wrote up my understanding at
https://www.dlp.rip/decorative-cryptography, for anyone who wants a long-form opinionated take :).Unless I'm wrong, or
TCG_TPM2_HMAChas changed dramatically since August, I don't think "TPM2_TCG_HMACmakes this too costly" is a compelling reason to make a security decision. (There could be other reasons to make choices about whether to use the TPM as a source of randomness in the kernel! This just isn't one IMHO.)Quoted 5 lines
The version of
TCG_TPM2_HMACthat I'd like to see someday would be one that fully admits that its threat model is only passive interposers, and sets up one session upon startup and ContextSaves/ContextLoads it back into the TPM as needed in order to secure parameter encryption for e.g.,GetRandom()andUnseal()calls.
Quoted 3 lines
Neither agreeing nor disagreeing but this patch set clearly does not move forward and I spent already enough energy for this. For better ideas the patches are available in queue branch.
Jarkko, you totally ignored my comments below. I object to your removing the TPM trusted-keys RNG support.
Mimi
Quoted 33 lines
High-level takes don't move anything forward (or backward), sorry.
Quoted 28 lines
[1]: https://lore.kernel.org/linux-integrity/CAMigqh2nwuRRxaLyOJ+QaTJ+XGmkQj=rMj5K9GP1bCcXp2OsBQ@mail.gmail.com/
[2]: https://lore.kernel.org/linux-integrity/[email protected]/Thanks
ChrisQuoted 20 lines · Mimi Zohar
[Cc: Chris Fenner, Jonathan McDowell, Roberto]
Quoted 3 lines · Jarkko Sakkinen
1.
tpm2_get_random()is costly whenTCG_TPM2_HMACis enabled and thus its use should be pooled rather than directly used. This both reduces latency and improves its predictability.If the concern is the latency of encrypting the bus session, please remember that:
- Not all environments expose the TPM bus to sniffing.
- The current TPM trusted keys design is based on TPM RNG, but already allows it to be replaced with the kernel RNG via the "trusted_rng=kernel" boot command line option.
- The proposed patch removes that possibility for no reason.Mimi & Elaine
BR, Jarkko
Re: [PATCH v9 01/11] KEYS: trusted: Use get_random-fallback for TPM
Apologies if my long message derailed this discussion. I meant to support Mimi's concern here and project a future vision where TCG_TPM2_HMAC doesn't conflict with other features.
More concisely, I think that:
Quoted 1 line
tpm2_get_random()is costly whenTCG_TPM2_HMACis enabled
is not a compelling argument for removing TPM as an RNG source, because TCG_TPM2_HMAC is known to have poor performance already anyway.
Thanks
Chris
Quoted 74 lines · Mimi Zohar
Quoted 30 lines · Jarkko Sakkinen
Quoted 23 lines · Chris Fenner
My conclusion about
TCG_TPM2_HMACafter [1] and [2] was thatTPM2_TCG_HMACdoesn't (or didn't at the time) actually solve the threat model it claims to (active interposer adversaries), while dramatically increasing the cost of many kernel TPM activities beyond the amount that would have been required to just solve for passive/bus-sniffer interposer adversaries. The added symmetric crypto required to secure a TPM transaction is almost not noticeable; the big performance problem is the re-bootstrapping of the session with ECDH for every command.My primary concern at that time was, essentially, that
TCG_TPM2_HMACpunts on checking that the key that was used to secure the session was actually resident in a real TPM and not just an interposer adversary. I wrote up my understanding at
https://www.dlp.rip/decorative-cryptography, for anyone who wants a long-form opinionated take :).Unless I'm wrong, or
TCG_TPM2_HMAChas changed dramatically since August, I don't think "TPM2_TCG_HMACmakes this too costly" is a compelling reason to make a security decision. (There could be other reasons to make choices about whether to use the TPM as a source of randomness in the kernel! This just isn't one IMHO.)Quoted 5 lines
The version of
TCG_TPM2_HMACthat I'd like to see someday would be one that fully admits that its threat model is only passive interposers, and sets up one session upon startup and ContextSaves/ContextLoads it back into the TPM as needed in order to secure parameter encryption for e.g.,GetRandom()andUnseal()calls.Quoted 3 lines
Neither agreeing nor disagreeing but this patch set clearly does not move forward and I spent already enough energy for this. For better ideas the patches are available in queue branch.
Jarkko, you totally ignored my comments below. I object to your removing the TPM trusted-keys RNG support.
Mimi
Quoted 33 lines
High-level takes don't move anything forward (or backward), sorry.
Quoted 28 lines
[1]: https://lore.kernel.org/linux-integrity/CAMigqh2nwuRRxaLyOJ+QaTJ+XGmkQj=rMj5K9GP1bCcXp2OsBQ@mail.gmail.com/
[2]: https://lore.kernel.org/linux-integrity/[email protected]/Thanks
ChrisQuoted 20 lines · Mimi Zohar
[Cc: Chris Fenner, Jonathan McDowell, Roberto]
Quoted 3 lines · Jarkko Sakkinen
1.
tpm2_get_random()is costly whenTCG_TPM2_HMACis enabled and thus its use should be pooled rather than directly used. This both reduces latency and improves its predictability.If the concern is the latency of encrypting the bus session, please remember that:
- Not all environments expose the TPM bus to sniffing.
- The current TPM trusted keys design is based on TPM RNG, but already allows it to be replaced with the kernel RNG via the "trusted_rng=kernel" boot command line option.
- The proposed patch removes that possibility for no reason.Mimi & Elaine
BR, Jarkko
Mimi Zohar
maintainerRe: [PATCH v9 01/11] KEYS: trusted: Use get_random-fallback for TPM
Quoted 11 lines · Chris Fenner
Apologies if my long message derailed this discussion. I meant to support Mimi's concern here and project a future vision where
TCG_TPM2_HMACdoesn't conflict with other features.More concisely, I think that:
Quoted 1 line
tpm2_get_random()is costly whenTCG_TPM2_HMACis enabledis not a compelling argument for removing TPM as an RNG source, because
TCG_TPM2_HMACis known to have poor performance already anyway.
Agreed. Thanks, Chris! FYI, we raised concerns about IMA performance with the TPM HMAC and encrypted feature while it was being developed. James had some ideas, at the time, as to how to resolve the performance issue for IMA. Yet it was upstreamed without those changes and with CONFIG_TCG_TPM2_HMAC enabled by default on x86 systems.
Jarkko has queued this patch in the "queue" branch, without indicating whether it will eventually be upstreamed or not.
Mimi
Jarkko Sakkinen
maintainerRe: [PATCH v9 01/11] KEYS: trusted: Use get_random-fallback for TPM
Quoted 13 lines · Mimi Zohar
Quoted 11 lines · Chris Fenner
Apologies if my long message derailed this discussion. I meant to support Mimi's concern here and project a future vision where
TCG_TPM2_HMACdoesn't conflict with other features.More concisely, I think that:
Quoted 1 line
tpm2_get_random()is costly whenTCG_TPM2_HMACis enabledis not a compelling argument for removing TPM as an RNG source, because
TCG_TPM2_HMACis known to have poor performance already anyway.
Quoted 8 lines
Agreed. Thanks, Chris! FYI, we raised concerns about IMA performance with the TPM HMAC and encrypted feature while it was being developed. James had some ideas, at the time, as to how to resolve the performance issue for IMA. Yet it was upstreamed without those changes and with
CONFIG_TCG_TPM2_HMACenabled by default on x86 systems.Jarkko has queued this patch in the "queue" branch, without indicating whether it will eventually be upstreamed or not.
Yes and there's been multiple months of time to comment this and I backed up the patch set there, which is not same as applying it.
Quoted 2 lines
Mimi
BR, Jarkko
Jarkko Sakkinen
maintainerRe: [PATCH v9 01/11] KEYS: trusted: Use get_random-fallback for TPM
Quoted 20 lines · Jarkko Sakkinen
Quoted 19 lines · Mimi Zohar
Quoted 11 lines · Chris Fenner
Apologies if my long message derailed this discussion. I meant to support Mimi's concern here and project a future vision where
TCG_TPM2_HMACdoesn't conflict with other features.More concisely, I think that:
Quoted 1 line
tpm2_get_random()is costly whenTCG_TPM2_HMACis enabledis not a compelling argument for removing TPM as an RNG source, because
TCG_TPM2_HMACis known to have poor performance already anyway.Agreed. Thanks, Chris! FYI, we raised concerns about IMA performance with the TPM HMAC and encrypted feature while it was being developed. James had some ideas, at the time, as to how to resolve the performance issue for IMA. Yet it was upstreamed without those changes and with
CONFIG_TCG_TPM2_HMACenabled by default on x86 systems.
Quoted 5 lines
Quoted 2 lines
Jarkko has queued this patch in the "queue" branch, without indicating whether it will eventually be upstreamed or not.
Yes and there's been multiple months of time to comment this and I backed up the patch set there, which is not same as applying it.
There's quite many other patches in that patch set also in the queue branch. This was largeriy past life for me when these comments came. Really don't understand what is suddenly going on tnh and for one not that interesting patch.
BR, Jarkko
Jarkko Sakkinen
maintainerRe: [PATCH v9 01/11] KEYS: trusted: Use get_random-fallback for TPM
Quoted 24 lines · Jarkko Sakkinen
Quoted 23 lines · Jarkko Sakkinen
Quoted 21 lines · Mimi Zohar
Quoted 11 lines · Chris Fenner
Apologies if my long message derailed this discussion. I meant to support Mimi's concern here and project a future vision where
TCG_TPM2_HMACdoesn't conflict with other features.More concisely, I think that:
Quoted 1 line
tpm2_get_random()is costly whenTCG_TPM2_HMACis enabledis not a compelling argument for removing TPM as an RNG source, because
TCG_TPM2_HMACis known to have poor performance already anyway.Agreed. Thanks, Chris! FYI, we raised concerns about IMA performance with the TPM HMAC and encrypted feature while it was being developed. James had some ideas, at the time, as to how to resolve the performance issue for IMA. Yet it was upstreamed without those changes and with
CONFIG_TCG_TPM2_HMACenabled by default on x86 systems.Jarkko has queued this patch in the "queue" branch, without indicating whether it will eventually be upstreamed or not.
Quoted 7 lines
Quoted 2 lines
Yes and there's been multiple months of time to comment this and I backed up the patch set there, which is not same as applying it.
There's quite many other patches in that patch set also in the queue branch. This was largeriy past life for me when these comments came. Really don't understand what is suddenly going on tnh and for one not that interesting patch.
Underlined: not a queue to anywhere. I can rename it something else, did not really think about the name when I created the branch.
BR, Jarkkko
Jarkko Sakkinen
maintainerRe: [PATCH v9 01/11] KEYS: trusted: Use get_random-fallback for TPM
In reply to Mimi ZoharQuoted 31 lines · Mimi Zohar
Quoted 30 lines · Jarkko Sakkinen
Quoted 28 lines · Chris Fenner
My conclusion about
TCG_TPM2_HMACafter [1] and [2] was thatTPM2_TCG_HMACdoesn't (or didn't at the time) actually solve the threat model it claims to (active interposer adversaries), while dramatically increasing the cost of many kernel TPM activities beyond the amount that would have been required to just solve for passive/bus-sniffer interposer adversaries. The added symmetric crypto required to secure a TPM transaction is almost not noticeable; the big performance problem is the re-bootstrapping of the session with ECDH for every command.My primary concern at that time was, essentially, that
TCG_TPM2_HMACpunts on checking that the key that was used to secure the session was actually resident in a real TPM and not just an interposer adversary. I wrote up my understanding at
https://www.dlp.rip/decorative-cryptography, for anyone who wants a long-form opinionated take :).Unless I'm wrong, or
TCG_TPM2_HMAChas changed dramatically since August, I don't think "TPM2_TCG_HMACmakes this too costly" is a compelling reason to make a security decision. (There could be other reasons to make choices about whether to use the TPM as a source of randomness in the kernel! This just isn't one IMHO.)The version of
TCG_TPM2_HMACthat I'd like to see someday would be one that fully admits that its threat model is only passive interposers, and sets up one session upon startup and ContextSaves/ContextLoads it back into the TPM as needed in order to secure parameter encryption for e.g.,GetRandom()andUnseal()calls.
Quoted 6 lines
Quoted 3 lines
Neither agreeing nor disagreeing but this patch set clearly does not move forward and I spent already enough energy for this. For better ideas the patches are available in queue branch.
Jarkko, you totally ignored my comments below. I object to your removing the TPM trusted-keys RNG support.
It has not been removed but I can keep the patches still backed up in a branch, can't I?
BR, Jarkko
Jarkko Sakkinen
maintainerRe: [PATCH v9 01/11] KEYS: trusted: Use get_random-fallback for TPM
In reply to Mimi ZoharQuoted 10 lines · Mimi Zohar
[Cc: Chris Fenner, Jonathan McDowell, Roberto]
Quoted 3 lines · Jarkko Sakkinen
1.
tpm2_get_random()is costly whenTCG_TPM2_HMACis enabled and thus its use should be pooled rather than directly used. This both reduces latency and improves its predictability.If the concern is the latency of encrypting the bus session, please remember that:
Quoted 7 lines
- Not all environments expose the TPM bus to sniffing.
- The current TPM trusted keys design is based on TPM RNG, but already allows it to be replaced with the kernel RNG via the "trusted_rng=kernel" boot command line option.
- The proposed patch removes that possibility for no reason.Mimi & Elaine
I'm keeping this patch set in queue branch, possibly picking patches to some other patch set or they are available for picking to other patch sets.
BR, Jarkko
Jarkko Sakkinen
maintainerPATCH v9 2/11 KEYS: trusted: Use get_random_bytes_wait() instead of tpm_get_random()
In reply to Jarkko SakkinenSubstitute the remaining tpm_get_random() calls in trusted_tpm1.c with get_random_bytes_wait() thus aligning random number generation for TPM 1.2 with the removal of '.get_random' callback.
--- security/keys/trusted-keys/trusted_tpm1.c | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-)
security/keys/trusted-keys/trusted_tpm1.c +3 -15
@@ -371,13 +371,10 @@ static int osap(struct tpm_buf *tb, struct osapsess *s,unsigned char ononce[TPM_NONCE_SIZE ];int ret; - ret =tpm_get_random (chip, ononce,TPM_NONCE_SIZE ); + ret =get_random_bytes_wait (ononce,TPM_NONCE_SIZE );if (ret <0 )return ret; -if (ret !=TPM_NONCE_SIZE ) -return -EIO ; -tpm_buf_reset (tb,TPM_TAG_RQU_COMMAND ,TPM_ORD_OSAP );tpm_buf_append_u16 (tb, type);tpm_buf_append_u32 (tb, handle);
@@ -464,15 +461,10 @@ static int tpm_seal(struct tpm_buf *tb, uint16_t keytype,memcpy (td->xorwork +SHA1_DIGEST_SIZE , sess.enonce,SHA1_DIGEST_SIZE );sha1 (td->xorwork,SHA1_DIGEST_SIZE *2 , td->xorhash); - ret =tpm_get_random (chip, td->nonceodd,TPM_NONCE_SIZE ); + ret =get_random_bytes_wait (td->nonceodd,TPM_NONCE_SIZE );if (ret <0 ) goto out; -if (ret !=TPM_NONCE_SIZE ) { - ret = -EIO ; - goto out; - } - ordinal =htonl (TPM_ORD_SEAL ); datsize =htonl (datalen); pcrsize =htonl (pcrinfosize);
@@ -575,14 +567,10 @@ static int tpm_unseal(struct tpm_buf *tb, } ordinal =htonl (TPM_ORD_UNSEAL ); - ret =tpm_get_random (chip, nonceodd,TPM_NONCE_SIZE ); + ret =get_random_bytes_wait (nonceodd,TPM_NONCE_SIZE );if (ret <0 )return ret; -if (ret !=TPM_NONCE_SIZE ) { -pr_info ("tpm_get_random failed (%d)\n" , ret); -return -EIO ; - } ret =TSS_authhmac (authdata1, keyauth,TPM_NONCE_SIZE , enonce1, nonceodd, cont,sizeof (uint32_t ), &ordinal, bloblen, blob,0 ,0 );
Signature · 1 line
2.52.0
Jarkko Sakkinen
maintainerPATCH v9 3/11 tpm: Change tpm_get_random() opportunistic
In reply to Jarkko Sakkinenhwrng framework does not have a requirement that the all bytes requested need to be provided. By enforcing such a requirement internally, TPM driver can cause unpredictability in latency, as a single tpm_get_random() call can result multiple TPM commands.
Especially, when TCG_TPM2_HMAC is enabled, extra roundtrips could have significant effect to the system latency.
Thus, send TPM command only once and return bytes received instead of committing to the number of requested bytes.
---
v9:
- Merge orchestration.
v7:
- Given that hwrng is now only caller for tpm_get_random(), remove the wait parameter.
v4:
- Fixed grammar mistakes.
--- drivers/char/tpm/tpm-interface.c | 20 ++++-- drivers/char/tpm/tpm1-cmd.c | 66 ++++++++---------- drivers/char/tpm/tpm2-cmd.c | 115 +++++++++++++------------------ 3 files changed, 88 insertions(+), 113 deletions(-)
drivers/char/tpm/tpm-interface.c +13 -7
@@ -487,18 +487,24 @@ int tpm_pm_resume(struct device *dev)EXPORT_SYMBOL_GPL (tpm_pm_resume);/** -* tpm_get_random() - get random bytes from the TPM's RNG -* @chip: a &struct tpm_chip instance, %NULL for the default chip -* @out: destination buffer for the random bytes -* @max: the max number of bytes to write to @out +* tpm_get_random() - Get random bytes from the TPM's RNG +* @chip: A &tpm_chip instance. When set to %NULL, the default chip is used. +* @out: Destination buffer for the acquired random bytes. +* @max: The maximum number of bytes to write to @out. +* +* Tries to pull bytes from the TPM. At most, @max bytes are returned. * -* Return: number of random bytes read or a negative error value. +* Returns the number of random bytes read on success. +* Returns -EINVAL when @out is NULL, or @max is not between zero and +* %TPM_MAX_RNG_DATA. +* Returns tpm_transmit_cmd() error codes when the TPM command results an +* error. */ int tpm_get_random (struct tpm_chip *chip,u8 *out,size_t max) {int rc; -if (!out || max >TPM_MAX_RNG_DATA ) +if (!out || !max || max >TPM_MAX_RNG_DATA )return -EINVAL ;if (!chip)
@@ -514,7 +520,7 @@ int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max) rc =tpm1_get_random (chip, out, max);tpm_put_ops (chip); -return rc; +return rc !=0 ? rc : -EIO ; }EXPORT_SYMBOL_GPL (tpm_get_random);
drivers/char/tpm/tpm1-cmd.c +27 -39
90 lines not on this page · raw message
drivers/char/tpm/tpm2-cmd.c +48 -67
146 lines not on this page · raw message
Signature · 1 line
2.52.0
Jarkko Sakkinen
maintainerPATCH v9 4/11 tpm2-sessions: Define TPM2_NAME_MAX_SIZE
In reply to Jarkko SakkinenDefine TPM2_NAME_MAX_SIZE, which describes the maximum size for hashes encoded as TPMT_HA, which the prime identifier used for persistent and transient keys in TPM2 protocol.
Set its value to 'SHA512_DIGEST_SIZE + 2', as SHA512 has the largest digest size of the algorithms in TCG algorithm repository.
In additionl, rename TPM2_NAME_SIZE as TPM2_NULL_NAME_SIZE in order to avoid any possible confusion.
---
v6:
- Rewrote the commit message.
v2:
- Rename TPM2_NAME_SIZE as TPM2_NULL_NAME_SIZE.
--- drivers/char/tpm/tpm-sysfs.c | 2 +- drivers/char/tpm/tpm2-sessions.c | 2 +- include/linux/tpm.h | 37 +++++++++++++++++++++----------- 3 files changed, 27 insertions(+), 14 deletions(-)
drivers/char/tpm/tpm-sysfs.c +1 -1
@@ -314,7 +314,7 @@ static ssize_t null_name_show(struct device *dev, struct device_attribute *attr, char *buf) {struct tpm_chip *chip =to_tpm_chip (dev); -int size =TPM2_NAME_SIZE ; +int size =TPM2_NULL_NAME_SIZE ;bin2hex (buf, chip->null_key_name, size); size *=2 ;
drivers/char/tpm/tpm2-sessions.c +1 -1
@@ -137,7 +137,7 @@ struct tpm2_auth {* we must compute and remember */ u32 name_h[AUTH_MAX_NAMES ]; -u8 name[AUTH_MAX_NAMES ][2 +SHA512_DIGEST_SIZE ]; +u8 name[AUTH_MAX_NAMES ][TPM2_MAX_NAME_SIZE ]; };#ifdef CONFIG_TCG_TPM2_HMAC
include/linux/tpm.h +25 -12
@@ -27,9 +27,33 @@#define TPM_DIGEST_SIZE 20/* Max TPM v1.2 PCR size */ +/* +* SHA-512 is, as of today, the largest digest in the TCG algorithm repository. +*/ #define TPM2_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE + +/* +* A TPM name digest i.e., TPMT_HA, is a concatenation of TPM_ALG_ID of the +* name algorithm and hash of TPMT_PUBLIC. +*/ +#define TPM2_MAX_NAME_SIZE (TPM2_MAX_DIGEST_SIZE + 2) + +/* +* The maximum number of PCR banks. +*/ #define TPM2_MAX_PCR_BANKS 8 +/* +* Fixed define for the size of a name. This is actually HASHALG size +* plus 2, so 32 for SHA256 +*/ +#define TPM2_NULL_NAME_SIZE 34 + +/* +* The maximum size for an object context +*/ +#define TPM2_MAX_CONTEXT_SIZE 4096 +struct tpm_chip ;struct trusted_key_payload ;struct trusted_key_options ;
@@ -139,17 +163,6 @@ struct tpm_chip_seqops {/* fixed define for the curve we use which is NIST_P256 */ #define EC_PT_SZ 32 -/* -* fixed define for the size of a name. This is actually HASHALG size -* plus 2, so 32 for SHA256 -*/ -#define TPM2_NAME_SIZE 34 - -/* -* The maximum size for an object context -*/ -#define TPM2_MAX_CONTEXT_SIZE 4096 -struct tpm_chip {struct device dev;struct device devs;
@@ -211,7 +224,7 @@ struct tpm_chip {/* saved context for NULL seed */ u8 null_key_context[TPM2_MAX_CONTEXT_SIZE ];/* name of NULL seed */ -u8 null_key_name[TPM2_NAME_SIZE ]; +u8 null_key_name[TPM2_NULL_NAME_SIZE ];u8 null_ec_key_x[EC_PT_SZ ];u8 null_ec_key_y[EC_PT_SZ ];struct tpm2_auth *auth;
Signature · 1 line
2.52.0
Jarkko Sakkinen
maintainerPATCH v9 5/11 KEYS: trusted: Open code tpm2_buf_append()
In reply to Jarkko SakkinenFrom: Jarkko Sakkinen <[email protected]>
tpm2_buf_append_auth() has a single call site and most of its parameters are redundant. Open code it to the call site so that less cross-referencing is required while browsing the source code.
---
v6:
- Trimmed the patch by removing comment update as it is out of scope.
--- security/keys/trusted-keys/trusted_tpm2.c | 40 ++++------------------- 1 file changed, 7 insertions(+), 33 deletions(-)
security/keys/trusted-keys/trusted_tpm2.c +7 -33
@@ -190,36 +190,6 @@ int tpm2_key_priv(void *context, size_t hdrlen,return 0 ; } -/** -* tpm2_buf_append_auth() - append TPMS_AUTH_COMMAND to the buffer. -* -* @buf: an allocated tpm_buf instance -* @session_handle: session handle -* @nonce: the session nonce, may be NULL if not used -* @nonce_len: the session nonce length, may be 0 if not used -* @attributes: the session attributes -* @hmac: the session HMAC or password, may be NULL if not used -* @hmac_len: the session HMAC or password length, maybe 0 if not used -*/ -static void tpm2_buf_append_auth (struct tpm_buf *buf,u32 session_handle, -const u8 *nonce,u16 nonce_len, -u8 attributes, -const u8 *hmac,u16 hmac_len) -{ -tpm_buf_append_u32 (buf,9 + nonce_len + hmac_len); -tpm_buf_append_u32 (buf, session_handle); -tpm_buf_append_u16 (buf, nonce_len); - -if (nonce && nonce_len) -tpm_buf_append (buf, nonce, nonce_len); - -tpm_buf_append_u8 (buf, attributes); -tpm_buf_append_u16 (buf, hmac_len); - -if (hmac && hmac_len) -tpm_buf_append (buf, hmac, hmac_len); -} -/** * tpm2_seal_trusted() - seal the payload of a trusted key *
@@ -518,9 +488,13 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,* could repeat our actions with the exfiltrated * password. */ -tpm2_buf_append_auth (&buf, options->policyhandle, -NULL /* nonce */ ,0 ,0 , - options->blobauth, options->blobauth_len); +tpm_buf_append_u32 (&buf,9 + options->blobauth_len); +tpm_buf_append_u32 (&buf, options->policyhandle); +tpm_buf_append_u16 (&buf,0 ); +tpm_buf_append_u8 (&buf,0 ); +tpm_buf_append_u16 (&buf, options->blobauth_len); +tpm_buf_append (&buf, options->blobauth, options->blobauth_len); +if (tpm2_chip_auth (chip)) {tpm_buf_append_hmac_session (chip, &buf,TPM2_SA_ENCRYPT ,NULL ,0 ); }else {
Signature · 1 line
2.52.0
Jarkko Sakkinen
maintainerPATCH v9 6/11 KEYS: trusted: Remove dead branch from tpm2_unseal_cmd
In reply to Jarkko SakkinenTPM2_Unseal requires TPM2_ST_SESSIONS, and tpm2_unseal_cmd() always does set up either password or HMAC session.
Remove the branch in tpm2_unseal_cmd() conditionally setting TPM2_ST_NO_SESSIONS. It is faulty but luckily it is never exercised at run-time, and thus does not cause regressions.
--- security/keys/trusted-keys/trusted_tpm2.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-)
security/keys/trusted-keys/trusted_tpm2.c +1 -9
@@ -451,10 +451,8 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip, struct trusted_key_options *options, u32 blob_handle) { -struct tpm_header *head;struct tpm_buf buf;u16 data_len; -int offset;u8 *data;int rc;
@@ -495,14 +493,8 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,tpm_buf_append_u16 (&buf, options->blobauth_len);tpm_buf_append (&buf, options->blobauth, options->blobauth_len); -if (tpm2_chip_auth (chip)) { +if (tpm2_chip_auth (chip))tpm_buf_append_hmac_session (chip, &buf,TPM2_SA_ENCRYPT ,NULL ,0 ); - }else { - offset = buf.handles *4 +TPM_HEADER_SIZE ; - head = (struct tpm_header *)buf.data; -if (tpm_buf_length (&buf) == offset) - head->tag =cpu_to_be16 (TPM2_ST_NO_SESSIONS ); - } } rc =tpm_buf_fill_hmac_session (chip, &buf);
Signature · 1 line
2.52.0
Jarkko Sakkinen
maintainerPATCH v9 7/11 KEYS: trusted: Re-orchestrate tpm2_read_public() calls
In reply to Jarkko Sakkinentpm2_load_cmd() and tpm2_unseal_cmd() use the same parent, and calls to tpm_buf_append_name() cause the exact same TPM2_ReadPublic command to be sent to the chip, causing unnecessary traffic.
1. Export tpm2_read_public in order to make it callable from 'trusted_tpm2'.
2. Re-orchestrate tpm2_seal_trusted() and tpm2_unseal_trusted() in order to halve the name resolutions required:
2a. Move tpm2_read_public() calls into trusted_tpm2.
2b. Pass TPM name to tpm_buf_append_name().
2c. Rework tpm_buf_append_name() to use the pre-resolved name.
--- drivers/char/tpm/tpm2-cmd.c | 3 +- drivers/char/tpm/tpm2-sessions.c | 95 +++++------------ include/linux/tpm.h | 10 +- security/keys/trusted-keys/trusted_tpm2.c | 122 ++++++++++++++-------- 4 files changed, 117 insertions(+), 113 deletions(-)
drivers/char/tpm/tpm2-cmd.c +2 -1
@@ -202,7 +202,8 @@ int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, }if (!disable_pcr_integrity) { - rc =tpm_buf_append_name (chip, &buf, pcr_idx,NULL ); + rc =tpm_buf_append_name (chip, &buf, pcr_idx, (u8 *)&pcr_idx, +sizeof (u32));if (rc) {tpm_buf_destroy (&buf);return rc;
drivers/char/tpm/tpm2-sessions.c +28 -67
153 lines not on this page · raw message
include/linux/tpm.h +9 -1
@@ -543,7 +543,7 @@ static inline struct tpm2_auth *tpm2_chip_auth(struct tpm_chip *chip) }int tpm_buf_append_name (struct tpm_chip *chip,struct tpm_buf *buf, -u32 handle,u8 *name); +u32 handle,u8 *name,u16 name_size);void tpm_buf_append_hmac_session (struct tpm_chip *chip,struct tpm_buf *buf,u8 attributes,u8 *passphrase,int passphraselen);
@@ -557,6 +557,7 @@ int tpm_buf_fill_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf);int tpm_buf_check_hmac_response (struct tpm_chip *chip,struct tpm_buf *buf,int rc);void tpm2_end_auth_session (struct tpm_chip *chip); +int tpm2_read_public (struct tpm_chip *chip,u32 handle,void *name);#else #include <linux/unaligned.h>
@@ -580,6 +581,13 @@ static inline int tpm_buf_check_hmac_response(struct tpm_chip *chip, {return rc; } + +static inline int tpm2_read_public (struct tpm_chip *chip,u32 handle, +void *name) +{ +memcpy (name, &handle,sizeof (u32)); +return sizeof (u32); +}#endif /* CONFIG_TCG_TPM2_HMAC */ #endif
security/keys/trusted-keys/trusted_tpm2.c +78 -44
209 lines not on this page · raw message
Signature · 1 line
2.52.0
Jarkko Sakkinen
maintainerPATCH v9 8/11 tpm2-sessions: Remove the support for more than one authorization
In reply to Jarkko SakkinenKernel uses at most a single HMAC authorization at a time.
From that basis, remove the unused machinery for managing multiple authorizations.
---
v8:
- Rewrote the commit message.
- Added an inline comment explaining why unconditional sha256_update() call for the name is safe.
--- drivers/char/tpm/tpm2-sessions.c | 40 ++++++++++++++------------------ 1 file changed, 18 insertions(+), 22 deletions(-)
drivers/char/tpm/tpm2-sessions.c +18 -22
@@ -72,9 +72,6 @@#include <crypto/sha2.h> #include <crypto/utils.h> -/* maximum number of names the TPM must remember for authorization */ -#define AUTH_MAX_NAMES 3 -#define AES_KEY_BYTES AES_KEYSIZE_128#define AES_KEY_BITS (AES_KEY_BYTES*8)
@@ -136,8 +133,8 @@ struct tpm2_auth {* handle, but they are part of the session by name, which * we must compute and remember */ -u8 name[AUTH_MAX_NAMES ][TPM2_MAX_NAME_SIZE ]; -u16 name_size_tbl[AUTH_MAX_NAMES ]; +u8 name[TPM2_MAX_NAME_SIZE ]; +u16 name_size; };#ifdef CONFIG_TCG_TPM2_HMAC
@@ -261,11 +258,14 @@ EXPORT_SYMBOL_GPL(tpm2_read_public);int tpm_buf_append_name (struct tpm_chip *chip,struct tpm_buf *buf,u32 handle,u8 *name,u16 name_size) { -#ifdef CONFIG_TCG_TPM2_HMAC struct tpm2_auth *auth; -int slot;int ret; -#endif + +if (tpm_buf_length (buf) !=TPM_HEADER_SIZE ) { +dev_err (&chip->dev,"too many handles\n" ); + ret = -EIO ; + goto err; + }if (!tpm2_chip_auth (chip)) {tpm_buf_append_handle (chip, buf, handle);
@@ -273,12 +273,6 @@ int tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf, }#ifdef CONFIG_TCG_TPM2_HMAC - slot = (tpm_buf_length (buf) -TPM_HEADER_SIZE ) /4 ; -if (slot >=AUTH_MAX_NAMES ) { -dev_err (&chip->dev,"too many handles\n" ); - ret = -EIO ; - goto err; - } auth = chip->auth;if (auth->session !=tpm_buf_length (buf)) {dev_err (&chip->dev,"session state malformed" );
@@ -287,16 +281,14 @@ int tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf, }tpm_buf_append_u32 (buf, handle); auth->session +=4 ; -memcpy (auth->name[slot], name, name_size); - auth->name_size_tbl[slot] = name_size; +memcpy (auth->name, name, name_size); + auth->name_size = name_size;#endif return 0 ; -#ifdef CONFIG_TCG_TPM2_HMAC err:tpm2_end_auth_session (chip);return ret; -#endif }EXPORT_SYMBOL_GPL (tpm_buf_append_name);
@@ -662,14 +654,18 @@ int tpm_buf_fill_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf) }sha256_init (&sctx); -/* ordinal is already BE */ sha256_update (&sctx, (u8 *)&head->ordinal,sizeof (head->ordinal)); -/* add the handle names */ -for (i =0 ; i < handles; i++) -sha256_update (&sctx, auth->name[i], auth->name_size_tbl[i]); + +/* +* If tpm2_buf_append_name() has not been called, this is a no-op, as +* auth->name_size is zero. +*/ +sha256_update (&sctx, auth->name, auth->name_size); +if (offset_s !=tpm_buf_length (buf))sha256_update (&sctx, &buf->data[offset_s],tpm_buf_length (buf) - offset_s); +sha256_final (&sctx, cphash);/* now calculate the hmac */
Signature · 1 line
2.52.0
Jarkko Sakkinen
maintainerPATCH v9 9/11 tpm-buf: Remove tpm_buf_append_handle
In reply to Jarkko SakkinenSince the number of handles is fixed to a single handle, eliminate all uses of buf->handles and deduce values during compile-time.
---
v2:
- Streamline the code change and remove dead code.
--- drivers/char/tpm/tpm-buf.c | 23 ----------------------- drivers/char/tpm/tpm2-cmd.c | 2 +- drivers/char/tpm/tpm2-sessions.c | 14 ++------------ include/linux/tpm.h | 2 -- 4 files changed, 3 insertions(+), 38 deletions(-)
drivers/char/tpm/tpm-buf.c +0 -23
@@ -44,7 +44,6 @@ void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal) head->tag =cpu_to_be16 (tag); head->length =cpu_to_be32 (sizeof (*head)); head->ordinal =cpu_to_be32 (ordinal); - buf->handles =0 ; }EXPORT_SYMBOL_GPL (tpm_buf_reset);
@@ -146,26 +145,6 @@ void tpm_buf_append_u32(struct tpm_buf *buf, const u32 value) }EXPORT_SYMBOL_GPL (tpm_buf_append_u32); -/** -* tpm_buf_append_handle() - Add a handle -* @chip: &tpm_chip instance -* @buf: &tpm_buf instance -* @handle: a TPM object handle -* -* Add a handle to the buffer, and increase the count tracking the number of -* handles in the command buffer. Works only for command buffers. -*/ -void tpm_buf_append_handle (struct tpm_chip *chip,struct tpm_buf *buf,u32 handle) -{ -if (buf->flags &TPM_BUF_TPM2B ) { -dev_err (&chip->dev,"Invalid buffer type (TPM2B)\n" ); -return ; - } - -tpm_buf_append_u32 (buf, handle); - buf->handles++; -} -/** * tpm_buf_read() - Read from a TPM buffer * @buf: &tpm_buf instance
@@ -242,5 +221,3 @@ u32 tpm_buf_read_u32(struct tpm_buf *buf, off_t *offset)return be32_to_cpu (value); }EXPORT_SYMBOL_GPL (tpm_buf_read_u32); - -
drivers/char/tpm/tpm2-cmd.c +1 -1
@@ -210,7 +210,7 @@ int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, }tpm_buf_append_hmac_session (chip, &buf,0 ,NULL ,0 ); }else { -tpm_buf_append_handle (chip, &buf, pcr_idx); +tpm_buf_append_u32 (&buf, pcr_idx);tpm_buf_append_auth (chip, &buf,NULL ,0 ); }
drivers/char/tpm/tpm2-sessions.c +2 -12
@@ -268,7 +268,7 @@ int tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf, }if (!tpm2_chip_auth (chip)) { -tpm_buf_append_handle (chip, buf, handle); +tpm_buf_append_u32 (buf, handle);return 0 ; }
@@ -295,17 +295,7 @@ EXPORT_SYMBOL_GPL(tpm_buf_append_name);void tpm_buf_append_auth (struct tpm_chip *chip,struct tpm_buf *buf,u8 *passphrase,int passphrase_len) { -/* offset tells us where the sessions area begins */ -int offset = buf->handles *4 +TPM_HEADER_SIZE ; -u32 len =9 + passphrase_len; - -if (tpm_buf_length (buf) != offset) { -/* not the first session so update the existing length */ - len +=get_unaligned_be32 (&buf->data[offset]); -put_unaligned_be32 (len, &buf->data[offset]); - }else { -tpm_buf_append_u32 (buf, len); - } +tpm_buf_append_u32 (buf,9 + passphrase_len);/* auth handle */ tpm_buf_append_u32 (buf,TPM2_RS_PW );/* nonce */
include/linux/tpm.h +0 -2
@@ -397,7 +397,6 @@ struct tpm_buf {u32 flags;u32 length;u8 *data; -u8 handles; };enum tpm2_object_attributes {
@@ -441,7 +440,6 @@ void tpm_buf_append_u32(struct tpm_buf *buf, const u32 value);u8 tpm_buf_read_u8 (struct tpm_buf *buf,off_t *offset);u16 tpm_buf_read_u16 (struct tpm_buf *buf,off_t *offset);u32 tpm_buf_read_u32 (struct tpm_buf *buf,off_t *offset); -void tpm_buf_append_handle (struct tpm_chip *chip,struct tpm_buf *buf,u32 handle);/* * Check if TPM device is in the firmware upgrade mode.
Signature · 1 line
2.52.0
Jarkko Sakkinen
maintainerPATCH v9 10/11 tpm-buf: Merge TPM_BUF_BOUNDARY_ERROR and TPM_BUF_OVERFLOW
In reply to Jarkko SakkinenFrom: Jarkko Sakkinen <[email protected]>
Merge TPM_BUF_BOUNDARY_ERROR and TPM_BUF_OVERFLOW into TPM_BUF_INVALID, given that they are identical. The only difference are the log messages.
In addition, add a missing TPM_BUF_INVALID check to tpm_buf_append_handle() following the pattern from other functions in tpm-buf.c.
--- drivers/char/tpm/tpm-buf.c | 10 ++++------ include/linux/tpm.h | 8 +++----- security/keys/trusted-keys/trusted_tpm2.c | 6 +++--- 3 files changed, 10 insertions(+), 14 deletions(-)
drivers/char/tpm/tpm-buf.c +4 -6
@@ -103,13 +103,12 @@ EXPORT_SYMBOL_GPL(tpm_buf_length);*/ void tpm_buf_append (struct tpm_buf *buf,const u8 *new_data,u16 new_length) { -/* Return silently if overflow has already happened. */ -if (buf->flags &TPM_BUF_OVERFLOW ) +if (buf->flags &TPM_BUF_INVALID )return ;if ((buf->length + new_length) >PAGE_SIZE ) {WARN (1 ,"tpm_buf: write overflow\n" ); - buf->flags |=TPM_BUF_OVERFLOW ; + buf->flags |=TPM_BUF_INVALID ;return ; }
@@ -156,14 +155,13 @@ static void tpm_buf_read(struct tpm_buf *buf, off_t *offset, size_t count, void {off_t next_offset; -/* Return silently if overflow has already happened. */ -if (buf->flags &TPM_BUF_BOUNDARY_ERROR ) +if (buf->flags &TPM_BUF_INVALID )return ; next_offset = *offset + count;if (next_offset > buf->length) {WARN (1 ,"tpm_buf: read out of boundary\n" ); - buf->flags |=TPM_BUF_BOUNDARY_ERROR ; + buf->flags |=TPM_BUF_INVALID ;return ; }
include/linux/tpm.h +3 -5
@@ -382,12 +382,10 @@ struct tpm_header { } __packed;enum tpm_buf_flags { -/* the capacity exceeded: */ -TPM_BUF_OVERFLOW =BIT (0 ),/* TPM2B format: */ -TPM_BUF_TPM2B =BIT (1 ), -/* read out of boundary: */ -TPM_BUF_BOUNDARY_ERROR =BIT (2 ), +TPM_BUF_TPM2B =BIT (0 ), +/* The buffer is in invalid and unusable state: */ +TPM_BUF_INVALID =BIT (1 ), };/*
security/keys/trusted-keys/trusted_tpm2.c +3 -3
@@ -292,7 +292,7 @@ int tpm2_seal_trusted(struct tpm_chip *chip,/* creation PCR */ tpm_buf_append_u32 (&buf,0 ); -if (buf.flags &TPM_BUF_OVERFLOW ) { +if (buf.flags &TPM_BUF_INVALID ) { rc = -E2BIG ;tpm2_end_auth_session (chip); goto out;
@@ -308,7 +308,7 @@ int tpm2_seal_trusted(struct tpm_chip *chip, goto out; blob_len =tpm_buf_read_u32 (&buf, &offset); -if (blob_len >MAX_BLOB_SIZE || buf.flags &TPM_BUF_BOUNDARY_ERROR ) { +if (blob_len >MAX_BLOB_SIZE || buf.flags &TPM_BUF_INVALID ) { rc = -E2BIG ; goto out; }
@@ -412,7 +412,7 @@ static int tpm2_load_cmd(struct tpm_chip *chip,tpm_buf_append (&buf, blob, blob_len); -if (buf.flags &TPM_BUF_OVERFLOW ) { +if (buf.flags &TPM_BUF_INVALID ) { rc = -E2BIG ;tpm2_end_auth_session (chip); goto out;
Signature · 1 line
2.52.0
Jarkko Sakkinen
maintainerPATCH v9 11/11 tpm-buf: Implement managed allocations
In reply to Jarkko SakkinenFrom: Jarkko Sakkinen <[email protected]>
Decouple kzalloc from buffer creation, so that a managed allocation can be used:
struct tpm_buf *buf __free(kfree) buf = kzalloc(TPM_BUFSIZE, GFP_KERNEL); if (!buf) return -ENOMEM; tpm_buf_init(buf, TPM_BUFSIZE);
Alternatively, stack allocations are also possible:
u8 buf_data[512]; struct tpm_buf *buf = (struct tpm_buf *)buf_data; tpm_buf_init(buf, sizeof(buf_data));
This is achieved by embedding buffer's header inside the allocated blob, instead of having an outer wrapper.
--- drivers/char/tpm/tpm-buf.c | 123 +++++---- drivers/char/tpm/tpm-sysfs.c | 21 +- drivers/char/tpm/tpm.h | 1 - drivers/char/tpm/tpm1-cmd.c | 163 +++++------- drivers/char/tpm/tpm2-cmd.c | 298 ++++++++++------------ drivers/char/tpm/tpm2-sessions.c | 142 +++++------ drivers/char/tpm/tpm2-space.c | 44 ++-- drivers/char/tpm/tpm_vtpm_proxy.c | 30 +-- include/linux/tpm.h | 20 +- security/keys/trusted-keys/trusted_tpm1.c | 36 +-- security/keys/trusted-keys/trusted_tpm2.c | 173 ++++++------- 11 files changed, 499 insertions(+), 552 deletions(-)
drivers/char/tpm/tpm-buf.c +77 -46
@@ -7,81 +7,107 @@#include <linux/module.h> #include <linux/tpm.h> -/** -* tpm_buf_init() - Allocate and initialize a TPM command -* @buf: A &tpm_buf -* @tag: TPM_TAG_RQU_COMMAND, TPM2_ST_NO_SESSIONS or TPM2_ST_SESSIONS -* @ordinal: A command ordinal -* -* Return: 0 or -ENOMEM -*/ -int tpm_buf_init (struct tpm_buf *buf,u16 tag,u32 ordinal) +static void __tpm_buf_size_invariant (struct tpm_buf *buf,u16 buf_size) { - buf->data = (u8 *)__get_free_page (GFP_KERNEL ); -if (!buf->data) -return -ENOMEM ; - -tpm_buf_reset (buf, tag, ordinal); -return 0 ; +u32 buf_size_2 = (u32 )buf->capacity + (u32 )sizeof (*buf); + +if (!buf->capacity) { +if (buf_size >TPM_BUFSIZE ) { +WARN (1 ,"%s: size overflow: %u\n" , __func__, buf_size); + buf->flags |=TPM_BUF_INVALID ; + } + }else { +if (buf_size != buf_size_2) { +WARN (1 ,"%s: size mismatch: %u != %u\n" , __func__, buf_size, + buf_size_2); + buf->flags |=TPM_BUF_INVALID ; + } + } } -EXPORT_SYMBOL_GPL (tpm_buf_init); -/** -* tpm_buf_reset() - Initialize a TPM command -* @buf: A &tpm_buf -* @tag: TPM_TAG_RQU_COMMAND, TPM2_ST_NO_SESSIONS or TPM2_ST_SESSIONS -* @ordinal: A command ordinal -*/ -void tpm_buf_reset (struct tpm_buf *buf,u16 tag,u32 ordinal) +static void __tpm_buf_reset (struct tpm_buf *buf,u16 buf_size,u16 tag,u32 ordinal) {struct tpm_header *head = (struct tpm_header *)buf->data; +__tpm_buf_size_invariant (buf, buf_size); + +if (buf->flags &TPM_BUF_INVALID ) +return ; +WARN_ON (tag !=TPM_TAG_RQU_COMMAND && tag !=TPM2_ST_NO_SESSIONS && tag !=TPM2_ST_SESSIONS && tag !=0 ); buf->flags =0 ; buf->length =sizeof (*head); + buf->capacity = buf_size -sizeof (*buf); head->tag =cpu_to_be16 (tag); head->length =cpu_to_be32 (sizeof (*head)); head->ordinal =cpu_to_be32 (ordinal); } -EXPORT_SYMBOL_GPL (tpm_buf_reset); + +static void __tpm_buf_reset_sized (struct tpm_buf *buf,u16 buf_size) +{ +__tpm_buf_size_invariant (buf, buf_size); + +if (buf->flags &TPM_BUF_INVALID ) +return ; + + buf->flags =TPM_BUF_TPM2B ; + buf->length =2 ; + buf->capacity = buf_size -sizeof (*buf); + buf->data[0 ] =0 ; + buf->data[1 ] =0 ; +}/** -* tpm_buf_init_sized() - Allocate and initialize a sized (TPM2B) buffer -* @buf: A @tpm_buf -* -* Return: 0 or -ENOMEM +* tpm_buf_init() - Initialize a TPM command +* @buf: A &tpm_buf +* @buf_size: Size of the buffer. */ -int tpm_buf_init_sized (struct tpm_buf *buf) +void tpm_buf_init (struct tpm_buf *buf,u16 buf_size) { - buf->data = (u8 *)__get_free_page (GFP_KERNEL ); -if (!buf->data) -return -ENOMEM ; +memset (buf,0 , buf_size); +__tpm_buf_reset (buf, buf_size,TPM_TAG_RQU_COMMAND ,0 ); +} +EXPORT_SYMBOL_GPL (tpm_buf_init); -tpm_buf_reset_sized (buf); -return 0 ; +/** +* tpm_buf_init_sized() - Initialize a sized buffer +* @buf: A &tpm_buf +* @buf_size: Size of the buffer. +*/ +void tpm_buf_init_sized (struct tpm_buf *buf,u16 buf_size) +{ +memset (buf,0 , buf_size); +__tpm_buf_reset_sized (buf, buf_size); }EXPORT_SYMBOL_GPL (tpm_buf_init_sized);/** -* tpm_buf_reset_sized() - Initialize a sized buffer +* tpm_buf_reset() - Re-initialize a TPM command * @buf: A &tpm_buf +* @tag: TPM_TAG_RQU_COMMAND, TPM2_ST_NO_SESSIONS or TPM2_ST_SESSIONS +* @ordinal: A command ordinal */ -void tpm_buf_reset_sized (struct tpm_buf *buf) +void tpm_buf_reset (struct tpm_buf *buf,u16 tag,u32 ordinal) { - buf->flags =TPM_BUF_TPM2B ; - buf->length =2 ; - buf->data[0 ] =0 ; - buf->data[1 ] =0 ; +u16 buf_size = buf->capacity +sizeof (*buf); + +__tpm_buf_reset (buf, buf_size, tag, ordinal); } -EXPORT_SYMBOL_GPL (tpm_buf_reset_sized); +EXPORT_SYMBOL_GPL (tpm_buf_reset); -void tpm_buf_destroy (struct tpm_buf *buf) +/** +* tpm_buf_reset_sized() - Re-initialize a sized buffer +* @buf: A &tpm_buf +*/ +void tpm_buf_reset_sized (struct tpm_buf *buf) { -free_page ((unsigned long )buf->data); +u16 buf_size = buf->capacity +sizeof (*buf); + +__tpm_buf_reset_sized (buf, buf_size); } -EXPORT_SYMBOL_GPL (tpm_buf_destroy); +EXPORT_SYMBOL_GPL (tpm_buf_reset_sized);/** * tpm_buf_length() - Return the number of bytes consumed by the data
@@ -89,8 +115,11 @@ EXPORT_SYMBOL_GPL(tpm_buf_destroy);* * Return: The number of bytes consumed by the buffer */ -u32 tpm_buf_length (struct tpm_buf *buf) +u16 tpm_buf_length (struct tpm_buf *buf) { +if (buf->flags &TPM_BUF_INVALID ) +return 0 ; +return buf->length; }EXPORT_SYMBOL_GPL (tpm_buf_length);
@@ -103,10 +132,12 @@ EXPORT_SYMBOL_GPL(tpm_buf_length);*/ void tpm_buf_append (struct tpm_buf *buf,const u8 *new_data,u16 new_length) { +u32 total_length = (u32 )buf->length + (u32 )new_length; +if (buf->flags &TPM_BUF_INVALID )return ; -if ((buf->length + new_length) >PAGE_SIZE ) { +if (total_length > (u32 )buf->capacity) {WARN (1 ,"tpm_buf: write overflow\n" ); buf->flags |=TPM_BUF_INVALID ;return ;
drivers/char/tpm/tpm-sysfs.c +10 -11
@@ -32,28 +32,29 @@ struct tpm_readpubek_out {static ssize_t pubek_show (struct device *dev,struct device_attribute *attr,char *buf) { -struct tpm_buf tpm_buf;struct tpm_readpubek_out *out;int i;char *str = buf;struct tpm_chip *chip =to_tpm_chip (dev);char anti_replay[20 ]; + struct tpm_buf *tpm_buf __free(kfree) = kzalloc(TPM_BUFSIZE, GFP_KERNEL); +if (!tpm_buf) +return -ENOMEM ; +memset (&anti_replay,0 ,sizeof (anti_replay));if (tpm_try_get_ops (chip))return 0 ; -if (tpm_buf_init (&tpm_buf,TPM_TAG_RQU_COMMAND ,TPM_ORD_READPUBEK )) - goto out_ops; - -tpm_buf_append (&tpm_buf, anti_replay,sizeof (anti_replay)); +tpm_buf_init (tpm_buf,TPM_BUFSIZE ); +tpm_buf_reset (tpm_buf,TPM_TAG_RQU_COMMAND ,TPM_ORD_READPUBEK ); +tpm_buf_append (tpm_buf, anti_replay,sizeof (anti_replay)); -if (tpm_transmit_cmd (chip, &tpm_buf,READ_PUBEK_RESULT_MIN_BODY_SIZE , -"attempting to read the PUBEK" )) - goto out_buf; +if (tpm_transmit_cmd (chip, tpm_buf,READ_PUBEK_RESULT_MIN_BODY_SIZE ,"TPM_ReadPubek" )) + goto out_ops; - out = (struct tpm_readpubek_out *)&tpm_buf.data[10 ]; + out = (struct tpm_readpubek_out *)&tpm_buf->data[10 ]; str +=sprintf (str, "Algorithm: %4ph\n"
@@ -71,8 +72,6 @@ static ssize_t pubek_show(struct device *dev, struct device_attribute *attr,for (i =0 ; i <256 ; i +=16 ) str +=sprintf (str,"%16ph\n" , &out->modulus[i]); -out_buf: -tpm_buf_destroy (&tpm_buf); out_ops:tpm_put_ops (chip);return str - buf;
drivers/char/tpm/tpm.h +0 -1
@@ -32,7 +32,6 @@#endif #define TPM_MINOR 224/* officially assigned */ -#define TPM_BUFSIZE 4096#define TPM_NUM_DEVICES 65536#define TPM_RETRY 50
drivers/char/tpm/tpm1-cmd.c +69 -94
266 lines not on this page · raw message
drivers/char/tpm/tpm2-cmd.c +135 -163
539 lines not on this page · raw message
drivers/char/tpm/tpm2-sessions.c +65 -77
266 lines not on this page · raw message
drivers/char/tpm/tpm2-space.c +18 -26
106 lines not on this page · raw message
drivers/char/tpm/tpm_vtpm_proxy.c +13 -17
@@ -395,40 +395,36 @@ static bool vtpm_proxy_tpm_req_canceled(struct tpm_chip *chip, u8 status)static int vtpm_proxy_request_locality (struct tpm_chip *chip,int locality) { -struct tpm_buf buf;int rc;const struct tpm_header *header;struct proxy_dev *proxy_dev =dev_get_drvdata (&chip->dev); + struct tpm_buf *buf __free(kfree) = kzalloc(TPM_BUFSIZE, GFP_KERNEL); +if (!buf) +return -ENOMEM ; + +tpm_buf_init (buf,TPM_BUFSIZE );if (chip->flags &TPM_CHIP_FLAG_TPM2 ) - rc =tpm_buf_init (&buf,TPM2_ST_SESSIONS , -TPM2_CC_SET_LOCALITY ); +tpm_buf_reset (buf,TPM2_ST_SESSIONS ,TPM2_CC_SET_LOCALITY );else - rc =tpm_buf_init (&buf,TPM_TAG_RQU_COMMAND , -TPM_ORD_SET_LOCALITY ); -if (rc) -return rc; -tpm_buf_append_u8 (&buf, locality); +tpm_buf_reset (buf,TPM_TAG_RQU_COMMAND ,TPM_ORD_SET_LOCALITY ); + +tpm_buf_append_u8 (buf, locality); proxy_dev->state |=STATE_DRIVER_COMMAND ; - rc =tpm_transmit_cmd (chip, &buf,0 ,"attempting to set locality" ); + rc =tpm_transmit_cmd (chip, buf,0 ,"attempting to set locality" ); proxy_dev->state &= ~STATE_DRIVER_COMMAND ; -if (rc <0 ) { - locality = rc; - goto out; - } +if (rc <0 ) +return rc; - header = (const struct tpm_header *)buf.data; + header = (const struct tpm_header *)buf->data; rc =be32_to_cpu (header->return_code);if (rc) locality =-1 ; -out: -tpm_buf_destroy (&buf); -return locality; }
include/linux/tpm.h +11 -9
@@ -25,7 +25,8 @@#include <crypto/hash_info.h> #include <crypto/aes.h> -#define TPM_DIGEST_SIZE 20/* Max TPM v1.2 PCR size */ +#define TPM_DIGEST_SIZE 20/* Max TPM v1.2 PCR size */ +#define TPM_BUFSIZE 4096/* * SHA-512 is, as of today, the largest digest in the TCG algorithm repository.
@@ -389,12 +390,14 @@ enum tpm_buf_flags { };/* -* A string buffer type for constructing TPM commands. +* A buffer for constructing and parsing TPM commands, responses and sized +* (TPM2B) buffers. */ struct tpm_buf { -u32 flags; -u32 length; -u8 *data; +u8 flags; +u16 length; +u16 capacity; +u8 data[]; };enum tpm2_object_attributes {
@@ -425,12 +428,11 @@ struct tpm2_hash {unsigned int tpm_id; }; -int tpm_buf_init (struct tpm_buf *buf,u16 tag,u32 ordinal); +void tpm_buf_init (struct tpm_buf *buf,u16 buf_size); +void tpm_buf_init_sized (struct tpm_buf *buf,u16 buf_size);void tpm_buf_reset (struct tpm_buf *buf,u16 tag,u32 ordinal); -int tpm_buf_init_sized (struct tpm_buf *buf);void tpm_buf_reset_sized (struct tpm_buf *buf); -void tpm_buf_destroy (struct tpm_buf *buf); -u32 tpm_buf_length (struct tpm_buf *buf); +u16 tpm_buf_length (struct tpm_buf *buf);void tpm_buf_append (struct tpm_buf *buf,const u8 *new_data,u16 new_length);void tpm_buf_append_u8 (struct tpm_buf *buf,const u8 value);void tpm_buf_append_u16 (struct tpm_buf *buf,const u16 value);
security/keys/trusted-keys/trusted_tpm1.c +18 -18
@@ -320,9 +320,10 @@ static int TSS_checkhmac2(unsigned char *buffer,* For key specific tpm requests, we will generate and send our * own TPM command packets using the drivers send function. */ -static int trusted_tpm_send (unsigned char *cmd,size_t buflen) +static int trusted_tpm_send (void *cmd,size_t cmd_len) { -struct tpm_buf buf; +u8 buf_data[512 ]; +struct tpm_buf *buf = (struct tpm_buf *)buf_data;int rc;if (!chip)
@@ -332,11 +333,10 @@ static int trusted_tpm_send(unsigned char *cmd, size_t buflen)if (rc)return rc; - buf.flags =0 ; - buf.length = buflen; - buf.data = cmd; +tpm_buf_init (buf,sizeof (buf_data)); +tpm_buf_append (buf, cmd, cmd_len);dump_tpm_buf (cmd); - rc =tpm_transmit_cmd (chip, &buf,4 ,"sending data" ); + rc =tpm_transmit_cmd (chip, buf,4 ,"sending data" );dump_tpm_buf (cmd);if (rc >0 )
@@ -622,23 +622,23 @@ static int tpm_unseal(struct tpm_buf *tb,static int key_seal (struct trusted_key_payload *p,struct trusted_key_options *o) { -struct tpm_buf tb;int ret; - ret =tpm_buf_init (&tb,0 ,0 ); -if (ret) -return ret; + struct tpm_buf *tb __free(kfree) = kzalloc(TPM_BUFSIZE, GFP_KERNEL); +if (!tb) +return -ENOMEM ; + +tpm_buf_init (tb,TPM_BUFSIZE );/* include migratable flag at end of sealed key */ p->key[p->key_len] = p->migratable; - ret =tpm_seal (&tb, o->keytype, o->keyhandle, o->keyauth, + ret =tpm_seal (tb, o->keytype, o->keyhandle, o->keyauth, p->key, p->key_len +1 , p->blob, &p->blob_len, o->blobauth, o->pcrinfo, o->pcrinfo_len);if (ret <0 )pr_info ("srkseal failed (%d)\n" , ret); -tpm_buf_destroy (&tb);return ret; }
@@ -648,14 +648,15 @@ static int key_seal(struct trusted_key_payload *p,static int key_unseal (struct trusted_key_payload *p,struct trusted_key_options *o) { -struct tpm_buf tb;int ret; - ret =tpm_buf_init (&tb,0 ,0 ); -if (ret) -return ret; + struct tpm_buf *tb __free(kfree) = kzalloc(TPM_BUFSIZE, GFP_KERNEL); +if (!tb) +return -ENOMEM ; + +tpm_buf_init (tb,TPM_BUFSIZE ); - ret =tpm_unseal (&tb, o->keyhandle, o->keyauth, p->blob, p->blob_len, + ret =tpm_unseal (tb, o->keyhandle, o->keyauth, p->blob, p->blob_len, o->blobauth, p->key, &p->key_len);if (ret <0 )pr_info ("srkunseal failed (%d)\n" , ret);
@@ -663,7 +664,6 @@ static int key_unseal(struct trusted_key_payload *p,/* pull migratable flag out of sealed key */ p->migratable = p->key[--p->key_len]; -tpm_buf_destroy (&tb);return ret; }
security/keys/trusted-keys/trusted_tpm2.c +83 -90
308 lines not on this page · raw message
Signature · 1 line
2.52.0
Jarkko Sakkinen
maintainerRe: [PATCH v9 11/11] tpm-buf: Implement managed allocations
Quoted 18 lines · Jarkko Sakkinen
From: Jarkko Sakkinen <[email protected]>
Decouple kzalloc from buffer creation, so that a managed allocation can be used:
struct tpm_buf *buf __free(kfree) buf = kzalloc(TPM_BUFSIZE, GFP_KERNEL); if (!buf) return -ENOMEM; tpm_buf_init(buf, TPM_BUFSIZE);Alternatively, stack allocations are also possible:
u8 buf_data[512]; struct tpm_buf *buf = (struct tpm_buf *)buf_data; tpm_buf_init(buf, sizeof(buf_data));
Quoted 6 lines
This is achieved by embedding buffer's header inside the allocated blob, instead of having an outer wrapper.
Cc Ross Philipson <[email protected]>Signed-off-by Jarkko Sakkinen <[email protected]>Reviewed-by Stefan Berger <[email protected]>
Since rest of the series does not seem to move forward maybe I should rebase this to bottom and send it as a separate patch?
This patch eliminates a category of memory bugs and is that way useful.
It also starts to be pretty well stress tested.
BR, Jarkko