]> git.proxmox.com Git - swtpm.git/log
swtpm.git
22 months agoswtpm: Also advertise the flags-opt-startup option for the CUSE interface
Stefan Berger [Wed, 13 Jul 2022 20:43:40 +0000 (16:43 -0400)]
swtpm: Also advertise the flags-opt-startup option for the CUSE interface

Commit 6559a902 implemented support for the startup-xyz flags for the CUSE
interface but the capability has not been advertised.

Adjust test cases to reflect the new verb being shown for
--print-capabilities.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
22 months agoconfigure: check for bash
William Roberts [Tue, 12 Jul 2022 17:04:10 +0000 (12:04 -0500)]
configure: check for bash

PCR Bank verification needs bash, so check for bash. While at it use the
autoconf shell construct macros over raw shell syntax which is slightly
more portable.

Examples:
./configure --enable-default-pcr-banks=sha256,sha920
checking which PCR banks to activate by default... configure: error: sha256,sha920 is an invalid list of PCR banks

./configure --enable-default-pcr-banks=sha256,sha512
checking which PCR banks to activate by default... sha256,sha512

./configure
checking which PCR banks to activate by default... sha256

Signed-off-by: William Roberts <william.c.roberts@intel.com>
22 months agogitignore: Ignore created files in man/man5/
Stefan Berger [Mon, 27 Jun 2022 14:19:35 +0000 (10:19 -0400)]
gitignore: Ignore created files in man/man5/

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
22 months agoselinux: Replace hardcoded install path with @prefix@
Stefan Berger [Mon, 27 Jun 2022 12:42:38 +0000 (08:42 -0400)]
selinux: Replace hardcoded install path with @prefix@

Replace the hardcoded install path in src/selinux/swtpm.fc and
src/selinux/swtpmcuse.fc with @prefix@ and append .in to these files so
that they are generated when running configure.

Add the selinux policy input files with their suffix to the CLEANFILES
variable so they get cleaned up and 'make distcheck' works.

Resolves: https://github.com/stefanberger/swtpm/issues/711
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
23 months agoswtpm: Disable OpenSSL FIPS mode to avoid libtpms failures
Stefan Berger [Wed, 8 Jun 2022 13:19:07 +0000 (09:19 -0400)]
swtpm: Disable OpenSSL FIPS mode to avoid libtpms failures

While libtpms does not provide any means to disable FIPS-disabled crypto
algorithms from being used, work around the issue by simply disabling the
FIPS mode of OpenSSL if it is enabled. If it cannot be disabled, exit
swtpm with a failure message that it cannot be disabled. If FIPS mode
was successfully disabled, print out a message as well.

Buglink: https://bugzilla.redhat.com/show_bug.cgi?id=2090219
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
23 months agotests: Do not activate SHA-1 PCR bank in test case
Stefan Berger [Mon, 13 Jun 2022 18:05:25 +0000 (14:05 -0400)]
tests: Do not activate SHA-1 PCR bank in test case

For being able to run tests with runtime-deactivated SHA-1 (in libtpms),
do not test with SHA-1 bank anymore.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
23 months agoswtpm_bios: Use TPM2_ALG_SHA256 as parameter to TPM2_IncrementalSelfTest
Stefan Berger [Mon, 13 Jun 2022 18:02:48 +0000 (14:02 -0400)]
swtpm_bios: Use TPM2_ALG_SHA256 as parameter to TPM2_IncrementalSelfTest

Do not use TPM2_ALG_SHA1 anymore as parameter to TPM2_IncrementalSelfTest()
so that this also works when SHA1 support in libtpms is runtime-disabled.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
23 months agoswtpm: Update printed copyright notice
Stefan Berger [Sun, 12 Jun 2022 18:29:52 +0000 (14:29 -0400)]
swtpm: Update printed copyright notice

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
23 months agoswtpm: Remove stale parameter from function documentation
Stefan Berger [Mon, 30 May 2022 15:19:03 +0000 (11:19 -0400)]
swtpm: Remove stale parameter from function documentation

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
23 months agoswtpm: Fix a typo in an error message
Stefan Berger [Mon, 30 May 2022 15:13:58 +0000 (11:13 -0400)]
swtpm: Fix a typo in an error message

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
23 months agoswtpm: Replace malloc + strcpy with strdup and handle OOM case
Stefan Berger [Wed, 25 May 2022 19:55:27 +0000 (15:55 -0400)]
swtpm: Replace malloc + strcpy with strdup and handle OOM case

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
23 months agoswtpm: Handle case where unknown blobtype is given (Coverity)
Stefan Berger [Wed, 25 May 2022 19:47:04 +0000 (15:47 -0400)]
swtpm: Handle case where unknown blobtype is given (Coverity)

Handle the case where an unknown blobtype is given and therefore
cannot be translated to a filename and blobname is NULL. Previously
this would have lead to an error when trying to read the file, now
we handle the failure case earlier.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
23 months agoswtpm: Cast '1' to uint64_t before shift and assign to uint64_t variable
Stefan Berger [Wed, 25 May 2022 19:31:05 +0000 (15:31 -0400)]
swtpm: Cast '1' to uint64_t before shift and assign to uint64_t variable

To avoid an overflowing expression cast '1' to uint64_t before shifting
it and assigning it to a uint64_t variable. In practice this kind of
overflow would never happen because there aren't that many available
PCR banks.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
23 months agoswtpm: Initialize res variable (Coverity)
Stefan Berger [Wed, 25 May 2022 18:28:09 +0000 (14:28 -0400)]
swtpm: Initialize res variable (Coverity)

Initialize the 'res' variable at the beginning of the function
even though this wouldn't be necessary in this case.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
23 months agoswtpm: Remove assignment to unused variable
Stefan Berger [Wed, 25 May 2022 18:19:36 +0000 (14:19 -0400)]
swtpm: Remove assignment to unused variable

Remove the assigment to 'res' since the subsequent code path does not
need it.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
23 months agoswtpm_localca: Add comment that failure to read optsfile is not an issue
Stefan Berger [Wed, 25 May 2022 20:02:45 +0000 (16:02 -0400)]
swtpm_localca: Add comment that failure to read optsfile is not an issue

Add a comment stating that failure to read the optsfile is not an
issue since the optsfile does not need to exist.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
23 months agoswtpm_setup: Use g_strdup instead of strdup
Stefan Berger [Wed, 25 May 2022 19:13:09 +0000 (15:13 -0400)]
swtpm_setup: Use g_strdup instead of strdup

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
23 months agoswtpm_setup: Initialize pubek_len (Coverity)
Stefan Berger [Wed, 25 May 2022 18:23:41 +0000 (14:23 -0400)]
swtpm_setup: Initialize pubek_len (Coverity)

Initialize pubek_len even though it isn't necessary to do so since
it will be initialized in the first function to which it is passed.
However, Coverity complains about pubek_len passed to print_as_hex()
not being initialized, even though this is not possible.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
23 months agoswtpm_ioctl: Free variable before parsing it to avoid memory leak (Coverity)
Stefan Berger [Wed, 25 May 2022 19:19:19 +0000 (15:19 -0400)]
swtpm_ioctl: Free variable before parsing it to avoid memory leak (Coverity)

Avoid a memory leaks if --tcp is provided multiple times by freeing
the previously allocated memory in the tcp_hostname variable.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
23 months agoswtpm_ioctl: Close file descriptor at end of main (Coverity)
Stefan Berger [Wed, 25 May 2022 18:32:53 +0000 (14:32 -0400)]
swtpm_ioctl: Close file descriptor at end of main (Coverity)

Close the file descriptor at the end of the main function.
To avoid closing random file descriptors initialize it with -1.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
23 months agoswtpm_cert: Test for NULL pointer returned by malloc
Stefan Berger [Wed, 25 May 2022 19:34:57 +0000 (15:34 -0400)]
swtpm_cert: Test for NULL pointer returned by malloc

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
23 months agoswtpm_cert: Free variable before parsing it to avoid memory leak (Coverity)
Stefan Berger [Wed, 25 May 2022 19:26:52 +0000 (15:26 -0400)]
swtpm_cert: Free variable before parsing it to avoid memory leak (Coverity)

Avoid memory leaks if one of the parameters --tcp, --tpm-device,
or --unix was passed multiple times by freeing the previously
parsed value.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
23 months agoswtpm_cert: Free variable before parsing it to avoid memory leak (Coverity)
Stefan Berger [Wed, 25 May 2022 19:09:47 +0000 (15:09 -0400)]
swtpm_cert: Free variable before parsing it to avoid memory leak (Coverity)

Avoid memory leaks if one of the parameters --modulus, --ecc-x,
or --ecc-y was passed multiple times by freeing the previously
parsed value.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
23 months agoswtpm_bios: Do not assigned -1 to closed file descriptor at end of function
Stefan Berger [Wed, 25 May 2022 18:15:32 +0000 (14:15 -0400)]
swtpm_bios: Do not assigned -1 to closed file descriptor at end of function

There's no need to assigned -1 to a closed file descriptor at the
end of a function.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm: Avoid locking directory multiple times
Stefan Berger [Wed, 11 May 2022 02:28:30 +0000 (22:28 -0400)]
swtpm: Avoid locking directory multiple times

Commit 2d3deaef29 forgot to move the check for whether the lock file has
already been opened into the new function opening the lock file and there-
fore the lock file is now opened whenever swtpm gets a PTM_INIT. This fix
prevents the reopening of the lockfile if it has already been opened.
Otherwise many PTM_INIT's will lead to failure since no more files can
be opened.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm: Remove support for pre-v0.1 state files without header
Stefan Berger [Fri, 6 May 2022 22:41:17 +0000 (18:41 -0400)]
swtpm: Remove support for pre-v0.1 state files without header

Remove support for TPM state files that didn't have the header
because they were created some time before v0.1.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agotest: Recreate TPM 2 state files with header
Stefan Berger [Sat, 7 May 2022 18:18:35 +0000 (14:18 -0400)]
test: Recreate TPM 2 state files with header

Use libtpms v0.6.6 and recreate the TPM 2 state file with header.
Start swtpm with the existing state files and have it rewrite the
volatiles state (swtpm_ioctl -v) and permanent state (tssnvdefine
+ tssnvundefine) files so that the header is on the files.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agotest: Recreate TPM 2 state files with header
Stefan Berger [Fri, 6 May 2022 21:28:50 +0000 (17:28 -0400)]
test: Recreate TPM 2 state files with header

Use libtpms v0.6.2 and recreate the TPM 2 state file with header.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agotest: Recreate TPM 2 state files with header
Stefan Berger [Fri, 6 May 2022 22:31:13 +0000 (18:31 -0400)]
test: Recreate TPM 2 state files with header

Recreate TPM 2 state files that didn't have a header. Use latest
version of libtpms from the stable-0.6.0 branch to create the
state that more recent version have to be able to read.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agotest: Recreate TPM 1.2 state files with header
Stefan Berger [Fri, 6 May 2022 22:08:17 +0000 (18:08 -0400)]
test: Recreate TPM 1.2 state files with header

Recreate a TPM 1.2 state file with header.

The state of the TPM 1.2 must be initialized with Startup(ST_CLEAR)
and then saved so that the proper error code appears as a result
when running this test.

The PCR values was originally created by extending PCR 10 with
sha1("test"). This was recreated using this sequence:

s=$(echo -en test | sha1sum | cut -d " " -f1 | sed -n 's/\([a-f0-9]\{2\}\)/\\x\1/pg')
echo -en $s > input
tss1extend  -ha 10 -if input

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agotest: Recreate TPM 1.2 state files with header
Stefan Berger [Fri, 6 May 2022 20:47:42 +0000 (16:47 -0400)]
test: Recreate TPM 1.2 state files with header

Recreate TPM 1.2 state files with similar content but with the state
file header. The older versions of the state files were created before
the header was introduced in v0.1. The goal is to be able to get rid
of code supporting pre-v0.1 files that had no header.

The PCR values was originally created by extending PCR 10 with
sha1("test"). This was recreated using this sequence:

s=$(echo -en test | sha1sum | cut -d " " -f1 | sed -n 's/\([a-f0-9]\{2\}\)/\\x\1/pg')
echo -en $s > input
tss1extend  -ha 10 -if input

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agodebian: Add swtpm apparmor profile
Lena Voytek [Thu, 5 May 2022 20:07:23 +0000 (13:07 -0700)]
debian: Add swtpm apparmor profile

An apparmor profile was added for Debian-based distributions in order to
increase security. This blocks swtpm from accessing restricted and unnecessary
files, folders, and network interfaces. swtpm works as normal alongside libvirt
and its configurations, however users may run into issues when using swtpm on
its own when providing it with a restricted directory. The apparmor profile can
be modified to include additional permissions by creating and adding to the
file /etc/apparmor.d/local/usr.bin.swtpm.

Signed-off-by: Lena Voytek <lena.voytek@canonical.com>
2 years agotests: Patch IBM TSS2 test suite for OpenSSL 3.x
Stefan Berger [Tue, 3 May 2022 00:46:50 +0000 (20:46 -0400)]
tests: Patch IBM TSS2 test suite for OpenSSL 3.x

Apply a patch to the IBM TSS2 v1.6 test suite when OpenSSL 3.x is de-
tected.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agotests: Repeat TSS command if it fails
Stefan Berger [Tue, 3 May 2022 14:48:07 +0000 (10:48 -0400)]
tests: Repeat TSS command if it fails

Repeat tss command since it may fail if the test case is run alone (-29).
The reason for this is that the command may fail because of this here:

https://github.com/stefanberger/libtpms/blob/stable-0.9/src/tpm2/SessionProcess.c#L1204

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agobuild-sys: Fix configure script to support _FORTIFY_SOURCE=3
Stefan Berger [Mon, 4 Apr 2022 12:49:37 +0000 (08:49 -0400)]
build-sys: Fix configure script to support _FORTIFY_SOURCE=3

gcc 12.1 supports _FORTIFY_SOURCE=3. Modify the existing check for whether
_FORTIFY_SOURCE=2 can be used to test compile with the user provided
CFLAGS and only add _D_FORTIFY_SOURCE=2 to the HARDENING_CFLAGS if the
user doesn't provide anything that's not compatible.

Following an online article _FORTIFY_SOURCE=3 may add more overhead, so
we only go up to level 2 for now and let build servers or user provide
the higher level via the CFLAGS.

https://developers.redhat.com/blog/2021/04/16/broadening-compiler-checks-for-buffer-overflows-in-_fortify_source#what_s_next_for__fortify_source

Resolves: https://github.com/stefanberger/swtpm/issues/688
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agobuild-sys: Define __USE_LINUX_IOCTL_DEFS in header file (Cygwin)
Stefan Berger [Mon, 4 Apr 2022 14:30:47 +0000 (10:30 -0400)]
build-sys: Define __USE_LINUX_IOCTL_DEFS in header file (Cygwin)

To be able to test-compile with include/swtpm/tpm_ioctl.h in configure.ac
move the definition of __USE_LINUX_IOCTL_DEFS out of the configure script
into the header file so that the #define is there when needed. In the
configure.ac script the CFLAGS were extended only after the test-compiling
to determine the HARDENING_CFLAGS and the test-compilation failed on Cygwin
(only) since the tpm_ioctl.h didn't compile because of this missing
 #define.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm: Use uint64_t in tlv_data_append() to avoid integer overflows
Stefan Berger [Mon, 28 Mar 2022 15:23:11 +0000 (11:23 -0400)]
swtpm: Use uint64_t in tlv_data_append() to avoid integer overflows

Instead of uint32_t use uint64_t's for accumulating needed buffer sizes
that are calculated by adding uint32_t length indicators. Use the uint64_t
to check for excessively large buffer sizes that could cause an integer
overflow if uint32_t was used.

This patch addresses the case where a user passes an old version of TPM
state file to swtpm for reading and the file is 4GB in size and thus can
cause an integer overflow in this particular function.

Otherwise, the previous fix to tlv_data_find_tag() protects swtpm from
integer overflows and later out-of-bound accesses when the TPM state is
initially read from a file (assuming the state file has a header, which
is the case since swtpm 0.1). If an excessively large buffer was passed
to libtpms, it would reject it since it would never be able to take in
that much data.

Data written to the file are coming from libtpms that we can trust in
terms of length indicators.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm: Use uint64_t to avoid integer wrap-around when adding a uint32_t
Stefan Berger [Sat, 26 Mar 2022 03:28:21 +0000 (23:28 -0400)]
swtpm: Use uint64_t to avoid integer wrap-around when adding a uint32_t

To avoid an integer wrap-around use uint64_t for 'offset' so that adding
an untrusted 32-bit number will allow for comparison against the trusted
'buffer_len' 32-bit number:

        if (offset + td->tlv.length > buffer_len)
            return NULL;

This avoids possible out-of-bound accesses and crashes when reading
specially crafted TPM state input data that have a tlv.length that is so
large that is causes an integer overflow.

Resolves: https://github.com/stefanberger/swtpm/issues/678
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm_bios: Use unsigned int tcp_port to filter out negative port numbers
Stefan Berger [Sat, 26 Mar 2022 02:54:21 +0000 (22:54 -0400)]
swtpm_bios: Use unsigned int tcp_port to filter out negative port numbers

The port being parsed must be given as unsigned int so that the comparison
of *tcp_port >= 65536 also filters out negative numbers passed via the
command line. Previously one could pass -1 and swtpm_bios would try to
connect.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm_ioctl: Use unsigned int tcp_port to filter out negative port numbers
Stefan Berger [Sat, 26 Mar 2022 02:41:00 +0000 (22:41 -0400)]
swtpm_ioctl: Use unsigned int tcp_port to filter out negative port numbers

The port being parsed must be given as unsigned int so that the comparison
of *tcp_port >= 65536 also filters out negative numbers passed via the
command line. Previously one could pass -1 and swtpm_ioctl would try to
connect to port 65535.

Resolves: https://github.com/stefanberger/swtpm/issues/679
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agobuild-sys: Move block with CUSE-related tests after initial CUSE tests
Stefan Berger [Mon, 21 Mar 2022 19:55:29 +0000 (15:55 -0400)]
build-sys: Move block with CUSE-related tests after initial CUSE tests

Mof the block with the CUSE-related tests further up to be following the
first set of CUSE-related tests.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agobuild-sys: Show the value of with_chardev after test for chardev
Stefan Berger [Mon, 21 Mar 2022 19:55:11 +0000 (15:55 -0400)]
build-sys: Show the value of with_chardev after test for chardev

Show the value of with_chardev after the test for whether to build
with chadev rather than with_cuse.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agobuild-sys: Determine GNUTLS_LIBS using pkg-config [OS X]
Stefan Berger [Mon, 21 Mar 2022 19:55:09 +0000 (15:55 -0400)]
build-sys: Determine GNUTLS_LIBS using pkg-config [OS X]

Determine GNUTLS_LIBS using pkg-config rather than hard-coding
it. On OS X it may return a -L<path> to succeed the linking.

Resolves: https://github.com/stefanberger/swtpm/issues/676
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm: Do not chdir(/) when using --daemon
Stefan Berger [Thu, 3 Mar 2022 14:13:26 +0000 (09:13 -0500)]
swtpm: Do not chdir(/) when using --daemon

With relative paths being used the chdir("/") in daemonize_finish() will
cause file access errors.

Fixes: 98d1d12 ("swtpm: Make --daemon not racy")
Resolves: https://github.com/stefanberger/swtpm/issues/671
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agobuild-sys: Add probing for -fstack-protector
Stefan Berger [Wed, 2 Mar 2022 18:52:53 +0000 (13:52 -0500)]
build-sys: Add probing for -fstack-protector

Add probing for -fstack-protector to the existing
-fstack-protector-strong since not all platforms support either one
of them.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoman: Add missing .config directory to path description when using ${HOME}
Stefan Berger [Mon, 21 Feb 2022 23:58:26 +0000 (18:58 -0500)]
man: Add missing .config directory to path description when using ${HOME}

When the ${HOME} directory is used for finding swtpm_setup.conf it is
to be found in ${HOME}/.config/swtpm_setup.conf.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Resolves: https://github.com/stefanberger/swtpm/issues/664

2 years agotests: Use ${WORKDIR} in config files to test env. var replacement
Stefan Berger [Tue, 22 Feb 2022 12:46:26 +0000 (07:46 -0500)]
tests: Use ${WORKDIR} in config files to test env. var replacement

To test the replacement of environment variables with their values
use ${WORKDIR} in the test case config files.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm-localca: Re-implement variable resolution for swtpm-localca.conf
Stefan Berger [Mon, 21 Feb 2022 23:37:34 +0000 (18:37 -0500)]
swtpm-localca: Re-implement variable resolution for swtpm-localca.conf

swtpm_localca v0.5 supported resolution of environment variables for
the swtpm-localca.conf configuration file. This functionality was lost
during the port to 'C' in v0.6. This patch now re-implements it.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Resolves: https://github.com/stefanberger/swtpm/issues/663

2 years agoswtpm_localca: Test for available issuercert before creating CA
Stefan Berger [Tue, 1 Feb 2022 17:40:06 +0000 (12:40 -0500)]
swtpm_localca: Test for available issuercert before creating CA

Avoid trying to create TPM certificates while the issuer certificate has
not been created, yet (in a 2nd step).

To resolve this do not just test for availability of the signing key, which
is created first, but also test for the issuer certifcate, which is created
in a 2nd step when the local CA is created. If either one is missing,
attempt to create the CA.

Resolves: https://github.com/stefanberger/swtpm/issues/644
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm: Check header size indicator against expected size (CID 375869)
Stefan Berger [Wed, 16 Feb 2022 16:17:47 +0000 (11:17 -0500)]
swtpm: Check header size indicator against expected size (CID 375869)

This fix addresses Coverity issue CID 375869.

Check the header size indicated in the header of the state against the
expected size and return an error code in case the header size indicator
is different. There was only one header size so far since blobheader was
introduced, so we don't need to deal with different sizes.

Without this fix a specially craft header could have cause out-of-bounds
accesses on the byte array containing the swtpm's state.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm_setup: Check for unreasonable number of PCR banks (CID 370783)
Stefan Berger [Wed, 16 Feb 2022 15:21:15 +0000 (10:21 -0500)]
swtpm_setup: Check for unreasonable number of PCR banks (CID 370783)

This fix addresses Coverity issue CID 370783.

Check for an unreasonable number of PCR banks returned from command sent
to swtpm. Limit the number of PCR banks that can be returned to '20',
which is more than enough.

Previously we may not have sanitized the variable correctly but safeguards
were in place:

Even if the 16 bit variable count was the maximum possible (0xffff) we
should be able to allocate the all_pcr_banks array of string pointers.

Safeguards to not overstep the parsed array are in place in the loop
that's entered afterwards where the count variable serves as a limit
for the loop.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm_setup: Free string array in case of failure
Stefan Berger [Wed, 16 Feb 2022 15:35:14 +0000 (10:35 -0500)]
swtpm_setup: Free string array in case of failure

Free the allocated string array in case of failure.

Existing callers auto-free the array already, so there's no memory leak,
but it is better to free it in the function where it is allocated.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm: Cast constant to uint64_t before shifting (CID 375870)
Stefan Berger [Wed, 16 Feb 2022 15:05:30 +0000 (10:05 -0500)]
swtpm: Cast constant to uint64_t before shifting (CID 375870)

Cast the '1' to uint64_t as suggested by Coverity (CID 375870).
Since 'j' is always less than '32', the previous code was correct as
well.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm_setup: fix newline before full stop in swtpm-not-found error
наб [Thu, 25 Nov 2021 22:28:11 +0000 (23:28 +0100)]
swtpm_setup: fix newline before full stop in swtpm-not-found error

Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
2 years agoMove *.conf and *.options to man5
Seunghun Han [Thu, 11 Nov 2021 02:38:22 +0000 (11:38 +0900)]
Move *.conf and *.options to man5

According to the man page sections guideline, man8 should be used
for system administration commands. So this commit moves *.conf and
*.options files to man5.

Signed-off-by: Seunghun Han <kkamagui@gmail.com>
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm: Implement function reporting error when choosing unsupported TPM
Stefan Berger [Fri, 19 Nov 2021 22:35:33 +0000 (17:35 -0500)]
swtpm: Implement function reporting error when choosing unsupported TPM

Implement tpmlib_choose_tpm_version() that reports an error when an un-
supported version is chosen. Have it used by existing code where possible.

If TPM 1.2 is not supported by libtpms, the following message is now
displayed:

swtpm: Error: TPM 1.2 is not supported by libtpms.

Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=2024583
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agobuild-sys: Bump up version to 0.8.0 at beginning of dev cycle
Stefan Berger [Sat, 20 Nov 2021 16:14:44 +0000 (11:14 -0500)]
build-sys: Bump up version to 0.8.0 at beginning of dev cycle

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agodebian/rpm: Adjust changelog for 0.7.0 release
Stefan Berger [Mon, 18 Oct 2021 19:23:42 +0000 (15:23 -0400)]
debian/rpm: Adjust changelog for 0.7.0 release

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoCHANGES: Add more documentation for changes in 0.7.0
Stefan Berger [Mon, 18 Oct 2021 19:19:57 +0000 (15:19 -0400)]
CHANGES: Add more documentation for changes in 0.7.0

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agobuild-sys: Move regex to test PCR banks into configure script
Stefan Berger [Mon, 8 Nov 2021 12:24:06 +0000 (07:24 -0500)]
build-sys: Move regex to test PCR banks into configure script

Move the regex test for the PCR banks into the configure script.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm_localca: Do not assign pointer to g_strchomp result (Coverity)
Stefan Berger [Fri, 5 Nov 2021 22:05:18 +0000 (18:05 -0400)]
swtpm_localca: Do not assign pointer to g_strchomp result (Coverity)

Get rid of a Coverity complaint by not assigning the same pointer
to the result of g_strchomp() as the parameter to the function.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm_setup: Do not assign pointer to g_strchomp result (Coverity)
Stefan Berger [Fri, 5 Nov 2021 22:03:39 +0000 (18:03 -0400)]
swtpm_setup: Do not assign pointer to g_strchomp result (Coverity)

Get rid of a Coverity complaint by not assigning the same pointer
to the result of g_strchomp() as the parameter to the function.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agotests: Fix expiration date check for 32 bit machines
Stefan Berger [Fri, 5 Nov 2021 19:16:43 +0000 (15:16 -0400)]
tests: Fix expiration date check for 32 bit machines

certtool on 32 bit machines seems to expire a never expiring certificate
in 2037 rather than 9999.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm: Fix compilation error on 32bit machines
Stefan Berger [Fri, 5 Nov 2021 19:02:05 +0000 (15:02 -0400)]
swtpm: Fix compilation error on 32bit machines

Fix the following compilation error occurring on 32bit machines:

swtpm_nvstore_linear_file.c: In function 'SWTPM_NVRAM_LinearFile_Mmap':
swtpm_nvstore_linear_file.c:58:20: error: comparison of integer expressions of different signedness: '__off_t' {aka 'long int'} and 'unsigned int' [-Werror=sign-compare]
   58 |     if (st.st_size >= (uint32_t)sizeof(struct nvram_linear_hdr)) {
      |                    ^~

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agotests: OSSL 3: Make TPM 1.2 test compile; skip IBM TSS 2 test
Stefan Berger [Fri, 5 Nov 2021 12:23:04 +0000 (08:23 -0400)]
tests: OSSL 3: Make TPM 1.2 test compile; skip IBM TSS 2 test

Add CFLAGS="-DOPENSSL_SUPPRESS_DEPRECATED=1" to the configure line
to avoid compile-time errors when building the TPM 1.2 test with
OpenSSL 3.0.

IBM TSS2 v1.6 test does not currently work with OpenSSL 3.0, so
skip it.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm_setup: Add support for --reconfigure flag to change active PCR banks
Stefan Berger [Fri, 29 Oct 2021 17:04:07 +0000 (13:04 -0400)]
swtpm_setup: Add support for --reconfigure flag to change active PCR banks

Add support for --reconfigure option for the swtpm_setup to be able to
change the active PCR banks. This option only works with --tpm2 and does
not allow to pass several other options such --create-ek or
--create-ek-cert or --create-platform-cert that would alter the state of
the TPM 2 in other ways.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agotests: Support filenames with spaces in some functions
Stefan Berger [Fri, 29 Oct 2021 16:57:40 +0000 (12:57 -0400)]
tests: Support filenames with spaces in some functions

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm_setup: Create flags by shifting '1'
Stefan Berger [Fri, 29 Oct 2021 15:42:27 +0000 (11:42 -0400)]
swtpm_setup: Create flags by shifting '1'

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agotests: exit with error code if mktemp fails
Stefan Berger [Tue, 2 Nov 2021 01:15:32 +0000 (21:15 -0400)]
tests: exit with error code if mktemp fails

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm_localca: Replace '+' and ',' characters in VMId's
Stefan Berger [Mon, 1 Nov 2021 13:08:22 +0000 (09:08 -0400)]
swtpm_localca: Replace '+' and ',' characters in VMId's

Certain characters are not accepted by gnutls when creating the
subject with the 'CN' from the vmid, so we have to replace those
characters with another one, such as '_'.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm_setup: Write active_pcr_banks into swtpm_setup.conf
Stefan Berger [Thu, 28 Oct 2021 17:04:24 +0000 (13:04 -0400)]
swtpm_setup: Write active_pcr_banks into swtpm_setup.conf

Write the active_pcr_banks into swtpm_setup.conf with the default
PCR banks from the configure script.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm_setup: Get active PCR banks from swtpm_setup.conf
Stefan Berger [Thu, 28 Oct 2021 16:23:14 +0000 (12:23 -0400)]
swtpm_setup: Get active PCR banks from swtpm_setup.conf

If the user did not provide the PCR banks to activate through the command
line options, try to read it from the config file and if nothing is found
there, fall back to the DEFAULT_PCR_BANKS as set during configure time.

Move the check for the PCR banks after the access check to the
configuration file.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm_setup: Move code to activate PCR banks into own function
Stefan Berger [Thu, 28 Oct 2021 15:54:20 +0000 (11:54 -0400)]
swtpm_setup: Move code to activate PCR banks into own function

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agobuild-sys: Add support for --enable-default-pcr-banks=list of PCR banks
Stefan Berger [Thu, 28 Oct 2021 15:25:31 +0000 (11:25 -0400)]
build-sys: Add support for --enable-default-pcr-banks=list of PCR banks

Add an option that allows for the configuration of the default PCR bank
to use. This was currently hard coded to sha256 and now may be passed
via this option. The fallback is still sha256. Valid PCR bank names are
sha1, sha256, sha384, and sha512. The passed list must be a comma-
separated list of the valid PCR bank names.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoman: Add version when feature was added when missing fix some others
Stefan Berger [Fri, 29 Oct 2021 17:40:12 +0000 (13:40 -0400)]
man: Add version when feature was added when missing fix some others

Add the version to items indicating when the feature was added.
Fix and unify some other ones.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm_setup.conf: Use /usr/bin/swtpm_localca for create_certs_tool
Stefan Berger [Thu, 28 Oct 2021 18:06:29 +0000 (14:06 -0400)]
swtpm_setup.conf: Use /usr/bin/swtpm_localca for create_certs_tool

swtpm_setup.conf has traditionally pointed to
/usr/share/swtpm/swtpm-localca for create_certs_tool but since
/usr/bin/swtpm_localca is now available, have newly created
config files point to this executable instead.

Since there are possibly many swtpm_setup.conf out there pointing
to /usr/share/swtpm/swtpm-localca, we have to still install
swtm_localca there as well and package it.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm_setup: Add missing sys/wait.h include (BSDs)
Stefan Berger [Wed, 27 Oct 2021 23:24:06 +0000 (19:24 -0400)]
swtpm_setup: Add missing sys/wait.h include (BSDs)

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm: Add missing signal.h include (BSDs)
Stefan Berger [Wed, 27 Oct 2021 23:23:10 +0000 (19:23 -0400)]
swtpm: Add missing signal.h include (BSDs)

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agotests: Convert 2 test cases to use --daemon option
Stefan Berger [Wed, 27 Oct 2021 01:38:45 +0000 (21:38 -0400)]
tests: Convert 2 test cases to use --daemon option

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agotests: Add test case for chardev for swtpm --print-states option
Stefan Berger [Wed, 27 Oct 2021 01:38:00 +0000 (21:38 -0400)]
tests: Add test case for chardev for swtpm --print-states option

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm: Make --daemon not racy
Nicolas Williams [Tue, 12 Oct 2021 16:58:06 +0000 (11:58 -0500)]
swtpm: Make --daemon not racy

Signed-off-by: Nicolas Williams <nico@cryptonector.com>
2 years agoswtpm_setup: Only activate SHA256 PCR bank, not SHA1 bank anymore
Stefan Berger [Fri, 22 Oct 2021 10:53:36 +0000 (06:53 -0400)]
swtpm_setup: Only activate SHA256 PCR bank, not SHA1 bank anymore

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoman: Improve the documentation of swtpm_setup's --create-config-files
Stefan Berger [Tue, 19 Oct 2021 14:00:24 +0000 (10:00 -0400)]
man: Improve the documentation of swtpm_setup's --create-config-files

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm_setup: Report stderr as returned by external tool (swtpm-localcal)
Stefan Berger [Tue, 19 Oct 2021 11:10:44 +0000 (07:10 -0400)]
swtpm_setup: Report stderr as returned by external tool (swtpm-localcal)

Report the error printed out via stderr as returned by swtpm-localca for
example.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agorpm: Update spec file to also work on CentOS9-stream
Stefan Berger [Mon, 18 Oct 2021 16:06:22 +0000 (12:06 -0400)]
rpm: Update spec file to also work on CentOS9-stream

CentOS9 has no more trousers, so disable it for rhel >= 9.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agotest: Add missing _test_tpm2_file_permissions file to EXTRA_DIST
Stefan Berger [Mon, 18 Oct 2021 13:52:01 +0000 (09:52 -0400)]
test: Add missing _test_tpm2_file_permissions file to EXTRA_DIST

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agorpm/debian: Clean up packaging files
Stefan Berger [Mon, 18 Oct 2021 13:20:33 +0000 (09:20 -0400)]
rpm/debian: Clean up packaging files

- Remove unused python3-twisted and gmp-devel.
- Add back rule to package /man8/swtpm_localca.8*

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agorpm: Remove man8/swtpm_localca.8 from specfile
Stefan Berger [Mon, 18 Oct 2021 11:19:23 +0000 (07:19 -0400)]
rpm: Remove man8/swtpm_localca.8 from specfile

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm: Disable fsync on file & dir due to TPM timeouts (issue #597)
Stefan Berger [Fri, 15 Oct 2021 03:34:53 +0000 (23:34 -0400)]
swtpm: Disable fsync on file & dir due to TPM timeouts (issue #597)

We cannot currently fsync on the TPM's state file and the dir since this
takes too long and commands in a VM may time out. The reason for this is
that the TPM 2 code occasionally writes the permanent state out even on
commands like TPM2_PCR_Extend that must not take a long time.

See explanation for this in the libtpms PR https://github.com/stefanberger/libtpms/pull/274 .

We will re-enable this feature in 'a while' once the updated libtpms
version has been made more widely available.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm_setup: Initialize variables to avoid compiler warnings
Stefan Berger [Fri, 15 Oct 2021 11:44:53 +0000 (07:44 -0400)]
swtpm_setup: Initialize variables to avoid compiler warnings

Fix issue #591 by initializing the variables swtpm_has_tpm2 and
swtpm_has_tpm12.

swtpm_setup.c:1178:31: note: 'swtpm_has_tpm2' was declared here
     gboolean swtpm_has_tpm12, swtpm_has_tpm2;
                               ^~~~~~~~~~~~~~
swtpm_setup.c:1019:5: error: 'swtpm_has_tpm12' may be used uninitialized in this function [-Werror=maybe-uninitialized]
     printf("{ \"type\": \"swtpm_setup\", "
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            "\"features\": [ %s%s\"cmdarg-keyfile-fd\", \"cmdarg-pwdfile-fd\", \"tpm12-not-need-root\""

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm: Fix case when no backend URI has been specified
Stefan Berger [Tue, 12 Oct 2021 10:27:26 +0000 (06:27 -0400)]
swtpm: Fix case when no backend URI has been specified

It is possible to start swtpm with this command line even though
neither storage nor communication channels have been provided.

./src/swtpm/swtpm socket --flags not-need-init,startup-clear

Since sending a startup message to the TPM will cause it to
want to store permanent state, we have to handle the case when
no storage backend was given and therefore the backend_uri
is NULL.

Previously the above command line caused a NULL pointer exception
but now handles this case with the following output:

swtpm: SWTPM_NVRAM_Init: Missing backend URI.
swtpm: Error: Could not initialize libtpms.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm: Fix a forgotten endianess-conversion
Stefan Berger [Mon, 11 Oct 2021 20:20:46 +0000 (16:20 -0400)]
swtpm: Fix a forgotten endianess-conversion

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm_localca: Created certificates for CAs and TPM that do not expire
Stefan Berger [Fri, 8 Oct 2021 15:19:33 +0000 (11:19 -0400)]
swtpm_localca: Created certificates for CAs and TPM that do not expire

Rather than having the CA certificates, that are created on the fly,
expire in 10 years, have them not expire at all.

Also create TPM certificates that don't expire and extend a test
case for this.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm_cert: Allow passing -1 for days to get a non-expiring certificate
Stefan Berger [Fri, 8 Oct 2021 16:06:50 +0000 (12:06 -0400)]
swtpm_cert: Allow passing -1 for days to get a non-expiring certificate

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm_setup: Exit with error code 1 on unknown option
Stefan Berger [Fri, 8 Oct 2021 13:35:47 +0000 (09:35 -0400)]
swtpm_setup: Exit with error code 1 on unknown option

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm_localca: Exit with error code 1 on unknown option
Stefan Berger [Fri, 8 Oct 2021 13:40:55 +0000 (09:40 -0400)]
swtpm_localca: Exit with error code 1 on unknown option

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm_localca: Use g_get_user_config_dir() rather than own code
Stefan Berger [Thu, 7 Oct 2021 20:00:06 +0000 (16:00 -0400)]
swtpm_localca: Use g_get_user_config_dir() rather than own code

Replace own code with g_get_user_config_dir() which makes things
a lot simpler.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agoswtpm_setup: Use g_get_user_config_dir() rather than own code
Stefan Berger [Thu, 7 Oct 2021 19:47:39 +0000 (15:47 -0400)]
swtpm_setup: Use g_get_user_config_dir() rather than own code

Replace own code with g_get_user_config_dir() which makes things
a lot simpler.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agotests: Add a test case for the new swtpm_setup option
Stefan Berger [Thu, 7 Oct 2021 12:41:47 +0000 (08:41 -0400)]
tests: Add a test case for the new swtpm_setup option

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2 years agosamples: Have swtpm-create-user-config-files run swtpm_setup
Stefan Berger [Thu, 7 Oct 2021 00:18:01 +0000 (20:18 -0400)]
samples: Have swtpm-create-user-config-files run swtpm_setup

Have swtpm-create-user-config-files run swtpm_setup with the new
--create-config-files option and possible parameters.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>