Commit Graph

918 Commits

Author SHA1 Message Date
Stefan Berger
1d918e5426 Fix regression from 'Recalculate the entrysize in front of ...'
The patch 'Recalculate the entrysize in front of every USER_NVRAM blob'
did not properly account for the TPM_HANDLE when recalculating the
entry size of an OBJECT in NVRAM. As a consequence all OBJECTS and NVRAM
indices following the first OBJECT got lost.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-07-27 17:22:20 -04:00
Stefan Berger
a5a359f4ca tpm2: Determine which PCR banks need to be restored
Determine which PCR banks need to be restored by determining which PCR banks
are active. We only expect data for active PCR banks and the other ones do
not need data.

The tricky part here is that the TPM 2 maintains a variable that indicates
which PCR banks are active in gp.pcrAllocated and another variable in NVRAM
indicating which ones will become active after a reboot. So far we only saved
the one from NVRAM when marshalling the state but now we have to save the one
holding the current active PCR banks also. The restauration of this variable
needs to happen when we resume the VM with volatile state.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-07-19 17:19:53 -04:00
Stefan Berger
073849aa46 tpm2: Recalculate the entrysize in front of every USER_NVRAM blob
Since the sizeof(TPM_NV_INDEX) and sizeof(OBJECT) may expand when new hash
algorithms are enabled, the size of each entry in the USER_NVRAM may also
expand. We have to account for this when writing the entry size into the
NVRAM and cannot use the one from the unmarshalled state unless it is 0
or the sizes were the same. So the best is to calculate the entry size and
write it again.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-07-17 20:40:45 -04:00
Stefan Berger
3ed7491323 tpm2: Add function to display USER_NVRAM contents
Add a function for displaying the USER_NVRAM contents for debugging.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-07-17 20:40:45 -04:00
Stefan Berger
dbc07c190c tpm2: disable the failure mode on totalResetCount return to 0
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-07-17 20:40:45 -04:00
Stefan Berger
d99e48eac2 NVMarshal: Fix handling of future header version handling
If we come across a header of a verions we don't know
in this implementation we just have to cause a skip.
2018-07-12 20:51:31 -04:00
Stefan Berger
5ddc17e001 tpm2: Use malloc/free rather than TPM_Malloc/TPM_Free
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-20 15:06:03 -04:00
Stefan Berger
1a24bcc550 build: Add missing header files for building RPM from tar file
2 header files were missing in the tar file and the RPM build failed.
This patch adds them.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:49 -04:00
Stefan Berger
1a7902bb6e tpm2: Implement TPM2B_Cmp() to compare to TPM2B's
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:49 -04:00
Stefan Berger
55bcad86f9 library: use __attribute__((format,...) to check printf format and args
Introduce ATTRIBUTE_FORMAT for gcc and clang and use it to check the format
of the printf type function TPMLIB_LogTPM2Error() and the parameters. Fix
several places with errors.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:49 -04:00
Stefan Berger
4c5b050537 tpm2: Add comments to unmarshalling of user NVRAM
The unmarshalling of the user NVRAM checks whether the NVRAM size allows us
to write the data into it. Since this function is called before writing the
other data into it (PERSISTENT DATA, etc. at lower offsets in the NVRAM),
we can be sure that the NVRAM is large enough to fit everything. Following
this logic, we should be able to loosen the restriction on the NVRAM size and
need not check what size the NVRAM was on the originating side. Since we will
test the unmarshalling of all the data we should be able to detect if the
local NVRAM size is too small and refuse to take the blob based on the failure
to unmarshal completely.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:49 -04:00
Stefan Berger
49e2af3ea5 tpm2: Run a compile-time check whether NVRAM is too small
Add a compile-time check for whether the NVRAM is too small.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:49 -04:00
Stefan Berger
eed97cf008 tpm2: adapt NVRAM offsets and check structure sizes
Make sure that the NVRAM offsets and structure sizes are the same
on all architectures so that we can fill up the NVRAM on one system
and migrate the state to another architecture and it will fit into
the NVRAM space there.

We leave some space for the first few data structures in the NVRAM to
grow. We do this by rounding up the NV_INDEX_RAM_DATA location to the
next kb boundary. This moves it from offset 4356 to 5120 on x86_64 and
from offset 4332 to 5120 on i386. This now leaves us with the same
amount of space for user dynamic NVRAM, which starts beyond offset 5120.

We also pad the OBJECT structure with 4 bytes so that it is the same
size on 32 and 64 bit architectures. This is a data structure that
is used in user dynamic NVRAM and should be the same size on all
architectures so that a full NVRAM always fits.

Also test the size of the NV_INDEX structure, which already has the
same size on all tested architectures (x86_64, i386, arm32, ppc64).

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:49 -04:00
Marc-André Lureau
48dabdbdef tests: add fuzz test
See README for details on how to use.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2018-06-15 18:07:49 -04:00
Marc-André Lureau
ef356d0ddb tests: add an initial corpus for fuzzing
This is taken directly from chromiumos TPM2 repository:

    commit 172eec5ea9b46ddfe720c854fa719fdf8613b4ed
    Author: nagendra modadugu <ngm@google.com>
    Date:   Fri Sep 16 14:07:13 2016 -0700

        Add an initial fuzz test corpus for execute-command

        This commit introduces a minimized corpus for
        the test in fuzz/execute-command.cc.

        The corpus was generated by parsing the TCG test
        suite log for messages sent to the TPM.  The corpus
        includes test cases that produced crashes reported
        in chrome-os-partner:56767 and chrome-os-partner:56825.

        Future work includes defining a dictionary for the
        protocol so that mutations can be more effective,
        and also generating test-cases that improve coverage
        (e.g. create & store primary key, followed by test case).

        BUG=chrome-os-partner:50646
        BRANCH=none
        TEST=follow instructions in fuzz/README

        Change-Id: I24332c0f58996626de9a08e2780595b0b2d76350
        Signed-off-by: nagendra modadugu <ngm@google.com>
        Reviewed-on: https://chromium-review.googlesource.com/386443
        Commit-Ready: Nagendra Modadugu <ngm@google.com>
        Tested-by: Nagendra Modadugu <ngm@google.com>
        Reviewed-by: Kostya Serebryany <kcc@chromium.org>
        Reviewed-by: Mike Aizitsky <aizatsky@chromium.org>
        Reviewed-by: Bill Richardson <wfrichar@chromium.org>

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2018-06-15 18:07:49 -04:00
Marc-André Lureau
37ca75c707 build-sys: remove -Wstrict-aliasing=3
The option doesn't exist with clang, and is default with gcc -Wall
already.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2018-06-15 18:07:49 -04:00
Marc-André Lureau
ec0f7ed195 build-sys: move hardening linker flags to ldflags
Fixes unusued flags warnings with clang.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2018-06-15 18:07:49 -04:00
Stefan Berger
effd551cfb tpm2: enable sha512 (breaks compatiblity)
Enable SHA512. Since the TPM 2 expects to see PCR values for
the SHA512 bank in some of its structures, this breaks
compatibility with existing state files.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:49 -04:00
Stefan Berger
470d94d242 tpm2: convert rest of structures to write downgradeable state
Also write those structure related to PERSISTENT_ALL to have
a header with version 2 and a tail that allows the state to be
extended and downgraded.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:49 -04:00
Stefan Berger
634db67243 tpm2: tie volatile state of TPM to persistent state
Write the persistent state's EP,SP, and PPSeed values into the
volatile state and check them when reading. This way we can
prevent that arbitrary volatile and persistent state blobs are
used.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:49 -04:00
Stefan Berger
8799e156ba Extend NVRAM memory size to 128kb
Increase the NVRAM memory size to 128kb. That should be it for good.

We accept smaller NVRAM sizes from stored state.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:49 -04:00
Stefan Berger
63ce125c7d Write different header for SHA384 and SHA512 in shared function
SHA512 is currently not enabled since until a short while ago the test
data was missing. So before we enable it, we should differentiate between
writing adn reading of SHA384 and SHA512 data by using a different magic
in the header. We assign the existing value of the magic for the SHA384
that we currently use and assign SHA512 a new value.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:49 -04:00
Stefan Berger
aa51031256 Increase MIN_EVICT_OBJECTS to 7 for value of TPM_PT_HR_PERSISTENT_MIN
Adapt the MIN_EVICT_OBJETCS #define value to 7 so that it meeds to
TPM Profile (PTP) Specification and TPM_PT_HR_PERSISTENT_MIN returns
the proper value.

We allow to read the state from a TPM that had a lesser value before.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:49 -04:00
Stefan Berger
f6ab7aeeb1 Update TpmToOsslMath.c to look similar to latest code
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:49 -04:00
Stefan Berger
8850c55258 Update to latest version: CryptHash.c
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:49 -04:00
Stefan Berger
f37f2efa31 Update to latest version: Ecc/Hash/RsaTestData.h
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:49 -04:00
Stefan Berger
33d616125f Update to latest version: MAC_fp.h
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:49 -04:00
Stefan Berger
22ce15897d Update to latest version: MathOnByteBuffers.c
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:49 -04:00
Stefan Berger
c5f6ffa6f8 Update to latest version: TpmToOsslMath.h
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:49 -04:00
Stefan Berger
8947327c7e tpm2: TPM_MainInit write permanent state blob if SetState() blob was used
If the permanent state was set using SetState() write the permanent
state once we successfully read the volatile state and can use it.
This way we have the state in a file.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:49 -04:00
Stefan Berger
e296c14939 tpm2: pass state set via SetState() to TPM 2
If state was set using SetState() then the cached blob needs
to be passed to the TPM 2 before trying to read the state from
a file.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:49 -04:00
Stefan Berger
e3ebc65206 tpm2: implement TPM2_Get/SetState
Implement the Get/SetState functions for TPM 2 so we can
also pass a blob in the TPM 2 case.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
ea41cca0e1 tpm2: Clear the cached states when a different TPM version is chosen
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
21b37cd6bb tpm2: Implement _rpc__Signal_IsPowerOn
Implement _rcp__Signal_IsPowerOn() so we can detect whether the
TPM 2 is running.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
fe003b7a2b tpm2: return TPM_RC_FAILURE in case of blockSize == 0
Coverity is complaining about the case of blockSize == 0. Later on in
CryptSymmetricDecrypt() this case is intercepted as well and
TPM_RC_FAILURE is returned. We just do this a little earlier.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
12ee6008f0 tpm2: Do NULL pointer check before dereferencing
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
90f1760304 tpm2: Do NULL pointer check before dereferencing
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
34924e876c tpm2: Do NULL pointer check before dereferencing
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
6bedcc9654 tpm2: initialize a couple of variable to make coverity happy
Coverity complains that a couple of variables read from the
stream may not be initialized. It shouldn't be possible to
have them not initialized since we would otherwise have an
error value in rc. Nevertheless, initialize them.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
f19bf6c6dc tpm2: Check against unreasonably large datasize value from stream
Check the read datasize against an unreasonably large value and
log in case we encounter a bad value. This particular value cannot
be larger than 64k and a few bytes.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
03d2b4f718 tpm2: Get return value into rc
In one case we missed to assign the return value for rc.
In another case check rc value before doing operation.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
387465d7eb tpm2: Cast values to proper datatypes
The unsigned int needs to be checked as an int against >= 0.
The bit to move needs to be a 1ULL type.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
f20c4d5da5 tpm2: Fix calculation of array_size
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
3c09c6a4da tpm2: Make loop counter a size_t
Some versions of gcc complain about the loop counter being
a signed int when compared against a sizeof() results. This
patch fixes this.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
ffb56b4448 tpm2: Enable a few more algorithms
Enable a few more algorithm for the TPM2. We have to adapt NVMarshal.c
to allow the enablement of these algorithms without rejecting existing
state. We do this through the 'LE' comparison operator that allows us
to read state from an implementation that didn't have it enabled while
we now have it enabled.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
b5f83051f2 tpm2: Enable support for SM2
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
1281c404ba tpm2: Enable support for ECMQV
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
358068c999 tpm2: Write comments about comparison operators
Write comments about the comparison operators used for comparing
compile time options/switches of the state that's being read from
another implementation versus the ones used in the current
implementation.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00
Stefan Berger
23af2b5472 merge into Allow state downgrading 2018-06-15 18:07:48 -04:00
Stefan Berger
ca29a44465 tpm2: Allow state downgrading starting with version 2 headers
Write version 2 headers for all versioned structures and append
a trailer that can hold future version data and that can be
skipped. The skipping allows us to downgrade state, meaning if
version 3 appended some data but we read it with version 2,
we don't need the version 3 data but can skip it. We loose data
this way, but can keep the TPM 2 running.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2018-06-15 18:07:48 -04:00