Commit Graph

726 Commits

Author SHA1 Message Date
Stefan Berger
6b0c0f91d4 swtpm: Return TPM_FAIL from invalid header version case
Return TPM_FAIL in case of an invalid header version number to avoid a
potential segmentation fault when accessing an unavailable buffer due to
the header parser not returning an error code.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-24 20:32:04 -04:00
Stefan Berger
fcb7c58d80 swtpm_setup: Initialize key_description with a default string
Initialize key_description with a default string rather than using it
possibly uninitialized. In practice it would never be used uninitialized
since if a platform certificate is to be created, then the ek certificate
would have also been created and therefore swtpm2->ops->create_ek()
would have been called and set key_description.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-24 20:32:04 -04:00
Stefan Berger
ac911b6b82 SELinux: Add rules for user_tpm_t:sockfile to allow unlink
With a memoryBacking node added to the libvirt domain XML, the unlink
permission on user_tmp_t:sockfile becomes necessary to avoid an avc
denial.

  <currentMemory unit='KiB'>2097152</currentMemory>
  <memoryBacking>
    <hugepages>
      <page size='2048' unit='KiB'/>
    </hugepages>
  </memoryBacking>

Also add the unlink permission to the other occurrences of sock_file.

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2165142
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-15 17:35:40 -04:00
Stefan Berger
de06e3003b SELinux: Add rules for sock_file on user_tmp_t
The following command line did not function on an x86_64 host due to missing
SELinux rules:

virt-install -v \
 --name fedora-38-aarch64 \
 --ram 4096 \
 --disk path=fedora-38.img,cache=none \
 --nographics \
 --os-variant fedora38 \
 --import \
 --virt-type=qemu \
 --arch aarch64 \
 --check all=off

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2228423
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-15 17:35:40 -04:00
Stefan Berger
2355b28d3a swtpm_setup: Exit with '0' upon --version rather than '1'.
The --version option was using the wrong goto label error rather than
out to now also exit with exitcode 0.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-15 17:35:40 -04:00
Stefan Berger
3d5b339e6a swtpm_localca: Add missing NULL option to end of array
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-15 17:35:40 -04:00
Michal Privoznik
30e9f9b13d swtpm: Restore logging to stderr on log open failure
When opening a log file fails (--log file=/some/path) for
whatever reason the @logfd variable is left set to -1 (the retval
of open()). And due to how _logprintf() is written, the
subsequent attempt to log this failure (logprintf()) returns too
early not printing the useful error.

Therefore, restore the original (CONSOLE_LOGGING) value.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
2023-08-15 17:35:40 -04:00
Stefan Berger
d9e4abf918 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 15:34:47 -04:00
Stefan Berger
a833fac5d9 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 09:01:06 -04:00
Stefan Berger
2214310d5b 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 19:01:56 -04:00
Stefan Berger
3cdfaeb9c2 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-30 12:11:24 -04:00
Stefan Berger
43c816c7b6 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-30 12:11:12 -04:00
Stefan Berger
9d32fd98b3 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-07 09:16:11 -05:00
Stefan Berger
50128f4cf6 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
2022-03-07 09:16:11 -05:00
Stefan Berger
b6b0611704 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>
2022-02-17 17:03:40 -05:00
Stefan Berger
b36664bfb8 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 crafted header could cause out-of-bounds
accesses on the byte array containing the swtpm's state.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-02-17 07:42:18 -05:00
Stefan Berger
7bf77806f4 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>
2021-11-05 18:47:12 -04:00
Stefan Berger
3de27959e6 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>
2021-11-05 18:47:12 -04:00
Stefan Berger
59d813574f 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>
2021-11-05 16:58:54 -04:00
Stefan Berger
25d4ac2d3a 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>
2021-11-05 06:39:49 -04:00
Stefan Berger
8422d06868 swtpm_setup: Create flags by shifting '1'
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-11-05 06:39:49 -04:00
Stefan Berger
b08876722a 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>
2021-11-01 10:27:10 -04:00
Stefan Berger
30fa8b47d2 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>
2021-10-29 15:36:35 -04:00
Stefan Berger
a5cc0bf6e2 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>
2021-10-29 15:36:35 -04:00
Stefan Berger
87755f8cc4 swtpm_setup: Move code to activate PCR banks into own function
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-29 15:36:35 -04:00
Stefan Berger
df4046d0e4 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>
2021-10-29 15:36:35 -04:00
Stefan Berger
1ffa0cc472 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>
2021-10-28 15:14:01 -04:00
Stefan Berger
d766b58d17 swtpm_setup: Add missing sys/wait.h include (BSDs)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-27 20:13:39 -04:00
Stefan Berger
343a038b58 swtpm: Add missing signal.h include (BSDs)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-27 20:13:39 -04:00
Nicolas Williams
98d1d126cd swtpm: Make --daemon not racy
Signed-off-by: Nicolas Williams <nico@cryptonector.com>
2021-10-27 16:51:35 -04:00
Stefan Berger
26cdfc77df swtpm_setup: Only activate SHA256 PCR bank, not SHA1 bank anymore
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-22 07:39:55 -04:00
Stefan Berger
c40fceb3ca 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>
2021-10-19 08:37:58 -04:00
Stefan Berger
4b5654a19f 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>
2021-10-17 16:32:38 -04:00
Stefan Berger
8def57facd 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>
2021-10-15 08:44:14 -04:00
Stefan Berger
57b1f7ef88 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>
2021-10-12 07:34:10 -04:00
Stefan Berger
0151ec3a52 swtpm: Fix a forgotten endianess-conversion
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-11 17:50:52 -04:00
Stefan Berger
e68cebaf5f 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>
2021-10-08 14:07:02 -04:00
Stefan Berger
9f5342fc68 swtpm_cert: Allow passing -1 for days to get a non-expiring certificate
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-08 14:07:02 -04:00
Stefan Berger
634e6705c3 swtpm_setup: Exit with error code 1 on unknown option
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-08 11:20:59 -04:00
Stefan Berger
79e4c44c04 swtpm_localca: Exit with error code 1 on unknown option
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-08 11:20:59 -04:00
Stefan Berger
f1d425b5c1 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>
2021-10-07 19:50:24 -04:00
Stefan Berger
874c333802 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>
2021-10-07 19:50:24 -04:00
Stefan Berger
a7254fab5e swtpm_setup: Implement --create-user-config skip-if-exist
Implement support for skipping over creating the files
if any one of the config files already exist.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-07 14:27:10 -04:00
Stefan Berger
2b60723766 swtpm_setup: Implement option --create-config-files to create config files
Implement the option --create-config-files to create config files
for swtpm_setup and swtpm-localca for a user account. The files will
be created under the $XDG_CONFIG_HOME or $HOME/.config directories.

This option supports optional arguments 'overwrite' to allow overwriting
existing config files as well as the optional argument 'root' to create
config files under root's home directory. Both options can be passed
by separating them with a ','.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-07 14:27:10 -04:00
Stefan Berger
50670dca12 swtpm: Move ARRAY_LEN and min #define's to swtpm_utils.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-07 14:27:10 -04:00
Stefan Berger
e6cbc55df6 swtpm: Display the --print-states capability and document it
Display the --print-states capability in the --print-capabilites
output as cmdarg-print-states.

Document availability in the man page.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-06 19:25:41 -04:00
Stefan Berger
2e3745db08 swtpm_setup: Move gmalloc after block that may return NULL on failure
To avoid a memory leak, move the gmalloc after a block that may
return NULL on failure.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-05 18:07:20 -04:00
Stefan Berger
b63a3e4b2e swtpm_setup: Introduce label to skip over closing of pidfile
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-05 18:07:20 -04:00
Stefan Berger
680ad059aa swtpm: Call msync with length = 0 on Cygwin
Cygwin internally uses the Windows API call FlushViewOfFile that
seems to not like to be called with an excessive number of bytes.
Instead, call it with length = 0 so that 'the file is flushed from
the base address to the end of the mapping' and then msync() succeeds.

Source:
https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-flushviewoffile

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-05 15:22:14 -04:00
Stefan Berger
57a8a914c7 swtpm_setup: Use SWTPM_ATTR_UNUSED on unused parameters (BSDs)
To silence the BSD gcc compiler on unused function parameters,
use SWTPM_ATTR_UNUSED.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-05 13:50:45 -04:00