When compiling with GCC 15 using `CFLAGS=-march=x86-64-v4`, the compiler's
aggressively optimized vectorizer triggers a false-positive
-Wstringop-overflow error. Because x86-64-v4 enables wide AVX-512 registers,
the compiler misinterprets the loop unrolling and warns that a 64-byte
vector write is overflowing the destination buffer:
```
tpm2/TPMCmd/tpm/src/crypt/AlgorithmTests.c:158:17: error:
writing 64 bytes into a region of size 15 [-Werror=stringop-overflow=]
158 | *iv = i;
```
This fixes the warning by marking the `iv` output pointer parameter as
`volatile`. This inhibits the over-aggressive loop vectorization on this
specific buffer, silencing the compiler error without changing the
underlying logic.
Signed-off-by: Arthur Gautier <arthur.gautier@arista.com>
Since swtpm now needs OpenSSL 3.5, move the testing into ubuntu:26.04
containers. Several basic dev packages need to be installed for a
container to be able to build.
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Install cpp-coveralls from its git repo to avoid having to use an old
version of setuptools and to get it to work again.
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Fix a bug where RuntimeProfileDedupStrItems incorrectly removes a
token that is a suffix of the last token in a comma-separated list.
For example, given "...,<token>,...,other-<token>", the function
would wrongly deduplicate "<token>" out of the list.
The root cause is the (dup[slen] == 0) branch in the boundary check,
which handles the last item in the list but does not verify that the
left boundary of the match is valid:
if (((dup[-1] == ',' || dup[-1] == 0) && dup[slen] == exp) ||
(dup[slen] == 0) /* last item in list */)
The fix requires a valid left-side delimiter in all cases:
if ((dup[-1] == ',' || dup[-1] == 0) &&
(dup[slen] == exp || dup[slen] == 0))
Signed-off-by: IWillBurn <eugene.boyarnikov@gmail.com>
In preparation for PQC support, introduce TPMPROP_TPM2_BUFFER_MAX for an
API user (e.g., swtpm) to be able to get the size of the TPM 2 request
and response buffer. Set the new TPM2_BUFFER_MAX to 4096 for now, which is
the same value as TPM_BUFFER_MAX. In the TPM 2 related code replace
TPM_BUFFER_MAX with TPM2_BUFFER_MAX.
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
The minimum buffer size was set to MAX_CONTEXT_SIZE + 128, which is fine
if the assumption that the actual biggest command or response buffer is
created by TPM2_ContextLoad/Save commands holds. If the assumption was
not holding, then the choice of the minimum buffer size would truncate
response sizes. Adjust this size to the sizeof(TPMS_CONTEXT), which is
80 bytes bigger than MAX_CONTEXT_SIZE but still related to
TPM2_ContextLoad/Save commands. Still add a generous additional 128 bytes
to it.
Add a comment about the maximum size and the sizes used by TIS and CRB.
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Limit the variable indicating to the caller how many bytes were returned
in a TPM response to the size returned from TPM2_GetBufferSize(). This
then reflects the buffer size that was negotiated with the
TPMLIB_SetBufferSize() call and for which the recipient of the buffer
should have enough space for.
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
CryptSelectMac is called (only) from TPM2_HMAC and TPM2_Mac_Start and needs
to check whether the creation of an HMAC with SHA1 is not allowed per the
profile attribute no-sha1-hmac-creation and/or no-sha1-hmac.
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Use the marshalled size of an OBJECT to check whether it still fits into
NVRAM rather than using the size of the OBJECT (sizeof(obj), which is much
bigger. Using the marshalled size also handles the case where the OBJECT is
still copied into NVRAM using memcpy for the NULL profile case of
libtpms v0.9. This should have been converted once NvObjectToBuffer()
was started to be called there.
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Only write the necessary number of bytes of the ppList and auditCommands
bitmaps. The entries in both of these bitmaps are set by a command's
index and the necessary number of bytes can be determined by finding the
command with the highest command code that is enabled in the current
profile (1). This in turn can be found by by searching for the last byte
in the enableCommandsByIdx bitmap that has any bit set.
1) It would be possible to skip writing these arrays entirely if they have
no bits set. The unmarshalling functions would clear the arrays.
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Use __builtin_popcount for counting the number of enabled commands in the
enabledCommandsByIdx bitmap.
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Base the enabledCommands array on the command index rather than the command
code since the latter left the first few bytes always unused since command
codes start at 0x11f only.
After this change, ppList, auditCommands, and enabledCommandsByIdx bitmaps
are all based on command indices.
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Coverity complains that the pAssert_RC's always evaluate to true and
are therefore useless. Deactivate them (wait for upstream to remove them).
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
On m68k int's only need to be 2-byte aligned and therefore the size of
some data structures or offsets of fields within data structures is not
as expected. Fix this by adding artificial m68k-specific padding where
necessary. If padding was added on any other architecture, it would not
make a difference there. Similarly, if some day m68k gcc was to align int's
as expected, the artifical padding would not have any influence on the
expected sizes and offsets and could be removed.
With the padding applied, swtpm should now be able to read state written
by other architectures. This is for example the case with swtpm test cases.
Link: https://wiki.debian.org/M68k/Alignment
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Before adding padding to the data structures, add asserts for offsets and
sizes of data structures so that none of these will change due to padding.
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Extend the test with 2 more expected sizes for marhshalled RSA key
OBJECT for up to the current StateFormatLevel 8.
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
OBJECTs are only written into NVRAM by using marshalling functions, which
should take the same number of bytes for the same OBJECT on all
architectures. So the increase of the size of an OBJECT is not a concern
anymore. Nevertheless, get 'notification' about the increase of the size
of OBJECTs on x86_64.
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
It is not necessary anymore to check for an increase of the sizes of the
PERSISTENT_DATA ppList and auditCommands fields since they are marshalled
with an array-size indicator.
Any previous version of libtpms should be able to resume a profile with
the ppList and auditCommands written with a later version. If later
versions have new commands, then those new commands must be added beyond
the current TPM_CC_FIRST. The ppList and auditCommands may be bigger
than before but can be truncated IFF new commands were added. These
new commands will not be usable with the older verison of libtpms based
on the StateFormatLevel then.
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Use the BITS_TO_BYTES macro where the number of bits is used for
calculating the size of a byte array.
Also, make the clearing of the rest of the byte arrays (ppList and
auditCommands) a bit more efficient than clearing the whole array
before copying the new data into it.
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
The current libtpms v0.10.2 does not accept a TPM 2 state that was written
with a more recent version of libtpms if the sizes of ppList and/or
auditCommands increased. Remove the asserts that trigger state reading
failures and limit array_size to the sizeof(data->ppList) and
sizeof(data->auditCommands) respectively . More recent versions of libtpms,
if they support more TPM 2 commands, will extend these arrays but those new
commands will not be usable by older versions of libtpms (via profile and
StateFormatLevel) and can therefore be ignored by truncating those arrays.
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
openssl defines SIXTY_FOUR_BIT for this architecture
./tpm2/crypto/openssl/tpm_radix.h:74:4: error: #error Need to determine RADIX_BITS value
74 | # error Need to determine RADIX_BITS value
Signed-off-by: Luca Boccassi <luca.boccassi@gmail.com>
The check covering bufferSize == 0 causes a compatibility issue as
described in #559 where a valid command is passed.
Also, bufferSize should never be <0 since the preceeding UINT16_Unmarshal()
would check whether sufficient bytes are available for unmarshalling
and would return an error code if this was not the case. Otherwise it would
return TPM_RC_SUCCESS along with the unmarshalled value. The bufferSize
would then only be 0 (as lowest possible value) in the success case.
Reported-by: Daniel Trick <daniel.trick@sit.fraunhofer.de>
Resolves: https://github.com/stefanberger/libtpms/issues/559
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
When the user passes no command profile, then the default command profile
of the (custom) profile will be used. If the user limits the
StateFormatLevel by setting it in the profile, then all those commands from
the default command profile that exceed the given StateFormatLevel have to
be filtered-out to avoid a rejection of the command profile. Therefore, add
a parameter to indicate that filtering of commands should be done and
return a new string with the enabled command codes if any command was
filtered-out.
Add a test case that demonstrates the filtering-out of commands 0x199 and
0x19a (from the default command profile) due to the choice of
StateFormatLevel '2'. Both command codes were enable in StateFormatLevel
'3'.
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Not all callers of RuntimeCommandsPrint will want to have the returned
string use quotation marks at the beginning and end of the string.
Therefore, add a quote parameter to this function that allows to avoid
the quoting. Adapt the single caller to pass a quotation mark.
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Refactor the code to have own function for ECC key size checking. Later on
other algorithms may get their own function as well.
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Refactor some Ecc-specific code in AlgorithmProfile.c so it can be reused
for other algorithms later on as well.
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Properly fill out the TPMS_SIG_SCHEME_RSAPSS structure, especially the
hashAlg that's found in TPMS_SCHEME_HASH, so that stricter marshalling
implementations do not refuse to marshal the structure.
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Move many files to the BSD-2-clause license that either
- have no modifications in them at all
- where all modifictions are from 'me'
- where I have permission to move the modifications by 3rd parties under
the new license
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>