Commit Graph

1661 Commits

Author SHA1 Message Date
Stefan Berger
98fdcaa374 swtpm: Travial reformatting of arrays of structs
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-08-10 19:30:35 -04:00
Stefan Berger
bcf1fa951c swtpm: cuse: Restrict opening CUSE device to one openable file descriptor
Restrict the opening of the CUSE device to one single file descriptor. We
can modify the CUSE TPM in this way since the kernel's /dev/tpm0 cannot be
opened multiple times, either, and the CUSE TPM should behave in the same
way.

Adjust test the partial reads case to only open CUSE device file once by
using a python program. Close the open file descriptor 100 before using
swtpm_ioctl to avoid failures.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-08-09 18:27:07 -04:00
Stefan Berger
b255d07010 tests: Move swtpm_open_cmddev into swtpm_cmd_tx
Move swtpm_open_cmddev call into swtpm_cmd_tx since the latter function is
always called in a subshell that previously inherited the file descriptor
opened by the test cases. Remove swtpm_cmd_tx from nearly all test cases
and also remove closing of file descriptor 100 via 'exec 100>&-' from test
cases since this is not necessary anymore.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-08-09 17:10:37 -04:00
Stefan Berger
76001cd26c swtpm: Ignore error if TPMLIB_ChooseTPMVersion for printing caps fails
Revert the change from the previous patch that shows an error when
TPMLIB_ChooseTPMVersion fails but rather ignore the error as before.
If a TPM 2 is supported then tpm-2.0 capability verb will be shown
and if a TPM 1.2 is supported then tpm-1.2 will be shown, thus
allowing someone reading the JSON to determine what is supported.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-07-19 21:39:22 -04:00
Stefan Berger
bf3f517539 swtpm: Move TPMLIB_ChooseTPMVersion into capabilities_print_json
All callers to capabilities_print_json() call TPMLIB_ChooseTPMVersion
right before. Move it into the function now and check the return
code.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-07-19 19:17:47 -04:00
Stefan Berger
f67607db26 swtpm_setup: Implement get_swtpm_capabilities() and call from two functions
Implement get_swtpm_capabilities() and call it from two existing functions
that now become a lot simpler.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-07-19 11:52:34 -04:00
Stefan Berger
b3c32ed5a0 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>
2022-07-14 08:07:27 -04:00
William Roberts
b91fc6e610 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>
2022-07-12 13:47:36 -04:00
Stefan Berger
6a3c9d585e gitignore: Ignore created files in man/man5/
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-06-28 07:55:20 -04:00
Stefan Berger
a772d48c0c 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>
2022-06-28 07:55:20 -04:00
Stefan Berger
a39c3792ba 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>
2022-06-16 07:36:12 -04:00
Stefan Berger
1b21d052f7 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>
2022-06-13 21:46:56 -04:00
Stefan Berger
9ebd925619 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>
2022-06-13 21:46:56 -04:00
Stefan Berger
933ac94d5d swtpm: Update printed copyright notice
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-06-12 16:27:49 -04:00
Stefan Berger
2a1bfe464d swtpm: Remove stale parameter from function documentation
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-06-01 11:03:11 -04:00
Stefan Berger
0a27860c31 swtpm: Fix a typo in an error message
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-06-01 11:03:11 -04:00
Stefan Berger
6c87d54292 swtpm: Replace malloc + strcpy with strdup and handle OOM case
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-25 18:54:58 -04:00
Stefan Berger
760ec8ea96 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>
2022-05-25 18:54:58 -04:00
Stefan Berger
bfa79e91f2 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>
2022-05-25 18:54:58 -04:00
Stefan Berger
1c3ba535b2 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>
2022-05-25 18:54:58 -04:00
Stefan Berger
81f517feec 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>
2022-05-25 18:54:58 -04:00
Stefan Berger
f8e5cbd809 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>
2022-05-25 18:54:58 -04:00
Stefan Berger
ebc8f2e758 swtpm_setup: Use g_strdup instead of strdup
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-25 18:54:58 -04:00
Stefan Berger
1df0a141e7 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>
2022-05-25 18:54:58 -04:00
Stefan Berger
c9660fee88 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>
2022-05-25 18:54:58 -04:00
Stefan Berger
f1306a6313 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>
2022-05-25 18:54:58 -04:00
Stefan Berger
c269da7869 swtpm_cert: Test for NULL pointer returned by malloc
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-05-25 18:54:58 -04:00
Stefan Berger
42d5f3b50f 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>
2022-05-25 18:54:58 -04:00
Stefan Berger
85d9dc2a60 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>
2022-05-25 18:54:58 -04:00
Stefan Berger
1ce0467ebe 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>
2022-05-25 18:54:58 -04:00
Stefan Berger
49ac2bdc5b 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>
2022-05-11 07:37:38 -04:00
Stefan Berger
1a300e26dd 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>
2022-05-09 10:15:08 -04:00
Stefan Berger
54864ccc0e 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>
2022-05-09 10:15:08 -04:00
Stefan Berger
1add354ee4 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>
2022-05-09 10:15:08 -04:00
Stefan Berger
5614c9e89d 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>
2022-05-09 10:15:08 -04:00
Stefan Berger
3845a08e0a 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>
2022-05-09 10:15:08 -04:00
Stefan Berger
cafdcfe71a 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>
2022-05-09 10:15:08 -04:00
Lena Voytek
3d2747a1be 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>
2022-05-05 20:58:30 -04:00
Stefan Berger
85ec64573b 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>
2022-05-03 13:27:13 -04:00
Stefan Berger
15d446885b 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>
2022-05-03 13:27:13 -04:00
Stefan Berger
607f1f800a 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>
2022-04-04 11:45:36 -04:00
Stefan Berger
f4cdeb61b9 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>
2022-04-04 11:45:36 -04:00
Stefan Berger
9918259c6e 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>
2022-03-29 07:38:12 -04:00
Stefan Berger
71965cda6c 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>
2022-03-29 07:38:12 -04:00
Stefan Berger
47638ebc36 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>
2022-03-28 09:21:38 -04:00
Stefan Berger
81d4b3e0cd 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>
2022-03-28 09:21:38 -04:00
Stefan Berger
2579038d76 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>
2022-03-24 08:40:00 -04:00
Stefan Berger
b7f55fd041 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>
2022-03-24 08:40:00 -04:00
Stefan Berger
e735328e3c 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>
2022-03-24 08:40:00 -04:00
Stefan Berger
646cf413b1 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>
2022-03-04 07:44:22 -05:00