Commit Graph

976 Commits

Author SHA1 Message Date
Stefan Berger
0c9a6c4a12 selinux: Re-add rule for svirt_tcg_t and user_tmp_t:sock_file (virt-install)
Re-add a missing rule to the swtpm_svirt policy that is needed for a
virt-install.

Reference: https://bugzilla.redhat.com/show_bug.cgi?id=2283878
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-05-31 14:55:23 -04:00
jeremie leska
857ec3ed93 swtpm: fix typo in help message
capabilites -> capabilities

Signed-off-by: jeremie leska <jeremie.leska@gmail.com>
2024-05-29 20:01:50 -04:00
Stefan Berger
3fe7895f9b selinux: Redevelop SELinux policy for Fedora 40 (ditch old rules)
Due to a significant change in the targeted SELinux policy re-develop
the SELinux policy for swtpm. New rules in swtpm_libvirt.te are needed
when libvirt causes new interactions between swtpm and other types.
Also consider extensions to the SELinux policy previously added due to
domain XML and command lines described in the following patches:

- 63825b296c
- 7dac1dbf6f

Also:
- add rules related to user_tmp_t are due to the following paths being used
  in session mode: /run/user/*/libvirt/qemu/run/swtpm
- add rules for swtpm_t accessing qemu_var_run_t file/dir/sockfile
- add rules for swtpm_localca (newly installed F40 system) needed once when
  /var/lib/swtpm-localca is empty and files there are created

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-05-27 20:06:17 -04:00
Stefan Berger
bfd6b8270a swtpm: cuse: Lock thread_busy_lock reading thread_busy (Coverity)
Coverity is complaining that thread_busy needs to be locked before
reading. For consistency reasons now also lock thread_busy before reading
it. However, in this case it does not make a difference whether this lock
is held when reading thread_busy since file_ops_lock is held when the
thread_busy flag is set and when it is read with a call to this function
(worker_thread_is_busy). Also while the thread is busy no further commands
can be submitted and it can then reset the thread_busy flag without holding
the file_ops_lock.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-04-22 08:48:48 -04:00
Stefan Berger
89b6991cee swtpm: cuse: Add lock comments to thread and variables (Coverity)
Coverity is complaining that ptm_response in the worker_thread needs to
be locked via the file_ops_lock since it is always locked when
ptm_response is accessed. However, once the thread has been started any
concurrent modification of the ptm_response is prevented with the
thread_busy flag. Therefore, add comments to the thread and ptm_reponse
variable about the locking.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-04-22 08:48:48 -04:00
Stefan Berger
249ead4572 swtpm: cuse: Lock file_ops_lock before sending startup (Coverity)
Even though concurrency is not possible before ptm_cuse_lowlevel_main
is called lock the file_ops_lock since Coverity is complaining that

- g_lastCommand in ptm_send_startup
- ptm_repsonse in ptm_send_startup
- tpm_running in main

are not locked. For consistency reasons now also lock these variables
even before activation of concurrency.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-04-22 08:48:48 -04:00
Stefan Berger
a18714f29a swtpm: cuse: Lock file_ops_lock before reading tpm_running
Lock the file_ops_lock before reading the tpm_running variable so that
access to access to it is serialized among all possible threads.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-03-22 16:03:03 -04:00
Stefan Berger
e52406a768 swtpm_ioctl: Separate return code from flags
Separate the return code returned from change_fd_flags from the current/
original flags of the file descriptor. Return the flags in an optional
integer.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-03-19 15:22:33 -04:00
Stefan Berger
f4fcd75685 swtpm_setup: Fail --pcr-banks without --tpm2
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-03-14 12:09:20 -04:00
Stefan Berger
e02626d038 swtpm_setup: Fail --decryption or --allow-signing without --tpm2
The help screen states that --decryption and --allow-signing require
--tpm2. Enforce this now also when checking flag combination since
neither flags would have any effect for a TPM 1.2.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-03-14 12:09:20 -04:00
Ross Lagerwall
f237f9f0b4 swtpm: Fix double free in error path
On error, the caller will call options_value_free() so don't free
ovs->options to avoid a double free.

Fixes: c94a1c4cbd ("swtpm: Fix memory leak in case realloc fails")
Signed-off-by: Ross Lagerwall <ross.lagerwall@citrix.com>
2024-03-14 08:45:35 -04:00
Ross Lagerwall
8655c6f8ec swtpm: Close fd after main loop
If we've accepted a connection fd during the main loop, close it once
the main loop terminates.

Signed-off-by: Ross Lagerwall <ross.lagerwall@citrix.com>
2024-03-14 08:45:35 -04:00
Stefan Berger
4b008b9067 Revert "utils: Implement find_program() to try BINDIR path before PATH"
Test cases using swtpm_localca were failing because swtpm_localca
now picked up the swtpm_cert installed in /usr/bin/swtpm_cert rather
than the one in the PATH. This revert fixes the issue and users will
have to adjust their PATH for out-of-PATH installations.

Resolves: https://github.com/stefanberger/swtpm/issues/829
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-09-01 22:48:45 -04:00
Stefan Berger
1950b1765d swtpm_localca: Add support for up to 20 bytes serial numbers
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-22 21:21:03 -04:00
Stefan Berger
c877bec553 swtpm_cert: Add support for serial numbers up to 20 bytes long
x509 certificate serial numbers can be up to 20 bytes long.
Support this via gmp library.

A serial number must not have its most significant bit set, which
would indicate a negative number. If this is the case, insert '0'
as the first byte.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-22 21:21:03 -04:00
Stefan Berger
6a2c342798 utils: Add 'const' to input parameter of type 'unsigned char *'
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-21 19:01:38 -04:00
Stefan Berger
a96509723f swtpm_setup: Flush spk after persisting to create room for another key
Future extensions will need room for another key, so flush the spk after
persisting it.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-19 11:36:21 -04:00
Stefan Berger
3e93c9b324 swtpm_localca: Introduce --key as more generic alias for --ek
swtpm_localca may also receive the key parameters of other keys than
the ek, therefore introduce the --key alias for --ek and rename the
variable ekparmas to key_params throughout the code.

Extend the man page to describe the --key option as an alias that
is to be used for other public key parameters than specifically those
of an ek.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-19 11:36:21 -04:00
Stefan Berger
f29b46f961 swtpm_setup: Refactor duplicate code into swtpm_tpm2_write_cert_nvram
Also rename the variable purpose to certtype since this better describes
what it is.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-19 09:18:09 -04:00
Stefan Berger
2c96242d13 swtpm_setup: Move persisting of certificate into tpm2_persist_certificate
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-19 09:18:09 -04:00
Stefan Berger
b3683ba107 swtpm_setup: Pass key_type to function creating filename for key
To make the function creating filenames for keys less ek-specific,
pass the key_type, such as "ek", into this function.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-19 09:18:09 -04:00
Stefan Berger
f03c5aaf71 swtpm_setup: Add scheme parameter before curveid to createprimary_ecc
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-19 09:18:09 -04:00
Stefan Berger
536f545042 swtpm_setup: Rename is_ek to preserve for future extension
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-19 09:18:09 -04:00
Stefan Berger
9a01be7c83 swtpm_setup: Mask-out EK and plaform certificate flags and set cert_flags
To be able to extend the flags_to_certfiles array with new certifcates
while carrying new flags for new certificate, mask-out exising EK and
platform certificate flags and set the cert_flags variable with it to limit
the view of the entries in the table.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-19 09:18:09 -04:00
Stefan Berger
4617e835c6 swtpm_setup: Move common code into new function read_certificate_file()
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-19 09:18:09 -04:00
Stefan Berger
32a53e5c9f swtpm_utils: Use SWTPM_G_FREE macro to free memory and set pointer to NULL
Use the macro in swtpm_setup.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-19 09:18:09 -04:00
Stefan Berger
63825b296c 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 10:30:08 -04:00
Stefan Berger
7dac1dbf6f 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 10:30:08 -04:00
Ross Lagerwall
51ccd2378b Make stdout unbuffered in swtpm_{setup,localca}
swtpm_setup and swtpm_localca may be run by a toolstack with log
messages going to a pipe via stdout. Set stdout to be unbuffered to
avoid log messages being lost if the process terminates unexpectedly.

Signed-off-by: Ross Lagerwall <ross.lagerwall@citrix.com>
2023-08-02 10:05:07 -04:00
Stefan Berger
f954e3b9d2 Fix a few spelling errors in code
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-07-17 20:46:43 -04:00
Stefan Berger
34f1e2a1b0 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-07-12 21:03:28 -04:00
Stefan Berger
2f86ffbac0 swtpm_setup: Close file descriptors passed to swtpm process on parent side
Close the file descriptors passed to the swtpm process on the parent
side for faster detection of errors in the swtpm process that can
occur if swtpm was passed a profile that it cannot run with.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-07-01 10:26:27 -04:00
Stefan Berger
1daa80d239 swtpm_setup: Use medium duration on TSC_PhysicalPresence to avoid timeouts
On busy systems TSC_PhysicalPresence may time out when used with short
duration, so use medium duration instead.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-06-26 18:36:35 -04:00
Stefan Berger
cfeea1b1c0 swtpm_setup: Add poll() after write() and before read() to detect errors
In case swtpm does not start properly the write() to it may work but
then the read() gets stuck. Detect a failed swtpm by adding a poll()
before the read(). Use some extended durations considering possibly
busy systems.

A terminating swtpm instance can be caused when an unacceptable
profile is passed.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-06-24 16:01:33 -04:00
Stefan Berger
48dffac182 swtpm_ioctl: Repeatedly call PTM_GET_INFO for long responses
Fix the display of long responses that require calling PTM_GET_INFO
multiple times to get parts of the response from increasing offsets
until the whole response has been received.

Long responses are only returned when libtpms has profiles support and
several flags are passed to PTM_GET_INFO.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-06-23 18:30:01 -04:00
Stefan Berger
ee0bce1bdf utils: Implement find_program() to try BINDIR path before PATH
To support install paths that are not covered by $PATH, and still allow
swtpm_setup and swtpm_localca to find swtpm and swtpm_cert executables,
implement find_program() to prepend the install path if only the program
name is given and otherwise fall back to g_find_program_in_path().

Update the man page stating that swtpm from the installation directory
(BINDIR) is tried to be used before one is attempted to be found in
the PATH.

Resolves: https://github.com/stefanberger/swtpm/issues/795
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-04-17 10:55:16 -04:00
Stefan Berger
fd2c9fdeab utils: Consolidate compile time configuration variables in swtpm_conf.h
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-04-17 10:55:16 -04:00
Stefan Berger
29ceff5c33 swtpm_localca: Add missing NULL option to end of array
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-02-22 17:27:05 -05:00
Michal Privoznik
72d9c031a0 swtpm_setup: Initialized @argv in get_swtpm_capabilities()
The compiler (though wrongly) identifies that the @argv variable
inside of get_swtpm_capabilities() function cam be used
uninitialized. While this is a spurious warning, it's common
practice to initialize g_autofree variables to NULL.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
2022-12-21 13:54:38 -05:00
Michal Privoznik
c6b78f49f5 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>
2022-12-02 11:08:46 -05:00
Ross Lagerwall
a2abd3b67d swtpm_setup: Configure swtpm to log to stdout/err if needed
If swtpm_setup is configured with a log file, it launches swtpm
configured with the same log file. If not, swtpm_setup logs will go to
stdout/stderr and it should configure swtpm to do the same.

Signed-off-by: Ross Lagerwall <ross.lagerwall@citrix.com>
2022-11-01 18:01:47 -04:00
Stefan Berger
87ce53ffc4 swtpm: Add another exit label to avoid gcc -fanalyzer false positive
Move existing exit label before the return statement and add another
label that includes the free(filebuffer). This avoids a false positive
by 'gcc -fanalyzer' that seems to think that free(filebuffer)
would double-free filebuffer after filebuffer = realloc(tmp, ..)
failure.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-10-05 07:37:14 -04:00
Stefan Berger
7d79ecd694 swtpm: Return TPM_FAIL if SWTPM_NVRAM_DecrytpData is called without key
Return TPM_FAIL if SWTPM_NVRAM_DecryptData() is called without a key or
if an unhandle type of encryption mode is encountered. Previously this
function would return no error but also would not do any decryption if
no key was provided. Consequently, it would then also not return a byte
array with decrypted data which in turn could led to potential NULL
pointer accesses in subsequent calls. However, all current callers check
whether they have a valid key before they call this function. So the
change is primarily done for static analyzers, such as gcc -fanalyzer,
to ease code analysis.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-09-30 13:12:06 -04:00
Stefan Berger
c94a1c4cbd swtpm: Fix memory leak in case realloc fails
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-09-30 11:06:13 -04:00
Stefan Berger
d5296e19e7 swtpm: Extend capabilities JSON and show ctrl-opt-terminate
Extend the capabilities JSON and show the support for the terminate
parameter of the --ctrl option.

Adjust test cases.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-09-22 13:27:38 -04:00
Stefan Berger
6ecf5880ed swtpm: Implement terminate parameter for ctrl channel loss
Implement support for the terminate parameter for the control channel
option so that swtpm terminates once the control channel connection is
lost. The primary use case is QEMU that holds the control channel
permanently.

Resolves: https://github.com/stefanberger/swtpm/issues/753
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-09-22 13:27:38 -04:00
Stefan Berger
185832c8d4 swtpm: Set tpm_running = false after TPMLIB_Terminate() on CMD_SHUTDOWN
Set tpm_running = false after TPMLIB_Terminate() call on CMD_SHUTDOWN
to prevent a call to tpmlib_maybe_send_tpm2_shutdown() at the exit
of the mainloop.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-09-22 09:52:10 -04:00
Stefan Berger
aa2ed3e460 swtpm: Also send TPM2_Shutdown when swtpm terminates by signal
Also send TPM2_Shutdown when swtpm is terminated by a signal or due to
lost connection (--terminate option). Previously supported reasons for
sending the TPM2_Shutdown were primarily related to commands sent via
the command channel.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-09-22 09:52:10 -04:00
Stefan Berger
fd5bad56d5 swtpm: Add missing '%' to %08x
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-09-22 09:52:10 -04:00
Stefan Berger
448f6326d7 swtpm,man: Clarify that --terminate works only for TCP data channel
The --terminate option is only for the TCP data channel.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-09-21 17:20:18 -04:00
Stefan Berger
4e4e6e82cb swtpm: Do 300 locking retries instead of only 100
Increase the number of locking retries to 300 over 3 seconds
instead of 100 over 1 second. This gives the failing side more
time to release the lock.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-09-20 13:00:34 -04:00
Stefan Berger
6fbb219db9 swtpm: Implement CMD_LOCK_STORAGE to lock storage
Implement CMD_LOCK_STORAGE / PTM_LOCK_STORAGE for a user to be able to
lock the storage of the storage backend (if supported) after its lock
has been released for example when the 'savestate' blob was received
while the TPM state was migrated.

Also adjust test case and extend man pages.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-09-06 14:08:45 -04:00
Stefan Berger
e821754bb6 swtpm: Advertise --migration option in capabilities JSON
Advertise the capability of supporting the --migration option
in the capabilies JSON that now has the cmdarg-migration verb:

$ swtpm socket --print-capabilities | jq
{
  "type": "swtpm",
  "features": [
    "tpm-1.2",
    "tpm-2.0",
    "tpm-send-command-header",
    "flags-opt-startup",
    "flags-opt-disable-auto-shutdown",
    "cmdarg-seccomp",
    "cmdarg-key-fd",
    "cmdarg-pwd-fd",
    "cmdarg-print-states",
    "cmdarg-migration",
    "nvram-backend-dir",
    "nvram-backend-file"
  ],
  "version": "0.8.0"
}

Adjust test cases and extend man page.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-09-06 14:08:45 -04:00
Stefan Berger
120d414693 swtpm: Implement release-lock-outgoing parameter for --migration option
Implement the release-lock-outgoing parameter for the --migration option
that causes the storage lock to be released once the 'savestate' blob
transfer has been initiated. To not release the lock too early users must
first get the 'permanent' and 'volatile' state blobs and the 'savestate'
blob must be transferred as the last blob.

When migrating a VM the migration may fail and execution will then resume
on the originating side. In this fallback case the swtpm on the
destination side may need some time to terminate and release the lock.
Therefore, add a loop to the code attempting to re-lock the storage
directory on the source side for a few times until on the destination
side swtpm has released the lock. Retry the locking for 100 times
with 10ms in between. The retries will only ever be necessary if a TPM
command is immediately executed upon resume [this may be difficult
to test]. The negative side effects of this could be that the loop is not
long enough to grab the lock or that a short-duration TPM command will
time out inside the VM due to the retries delaying when it is processed.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Resolves: https://github.com/stefanberger/swtpm/issues/724
2022-09-06 14:08:45 -04:00
Stefan Berger
8a2bbeceba swtpm: Introduce --migration option and 'incoming' parameter
Introduce the --migration option along with the 'incoming' parameter
that allows to defer the locking of the storage until either

- the reception of the TPM's state is started
- a TPM command is about to be processed

Note that the reception of CMD_INIT does not enable the storage lock.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
Resolves: https://github.com/stefanberger/swtpm/issues/724
2022-09-06 14:08:45 -04:00
Stefan Berger
44acf591c8 swtpm: Move locking of storage into tpmlib_start()
Move the locking of the storage into tpmlib_start() after the call to
TPMLIB_MainInit() which was previously doing the locking when the prepare
function was called in the SWTPM_NVRAM_Init() callback invoked by
TPMLIB_MainInit().

This allows for conditional locking in tpmlib_start() using a flag later
on.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-09-06 14:08:45 -04:00
Stefan Berger
98d009bc99 swtpm: Implement stub for fips_mode_enabled (OpenBSD)
On OpenBSD openssl/fips.h is not available and FIPS_mode() is not
available, so implement a stub for fips_mode_enabled().

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-08-25 19:06:19 -04:00
Stefan Berger
cdc39fd977 swtpm: Advertise the --chroot option with cmdarg-chroot
Advertise the availability of the chroot option with the cmdarg-chroot
verb. Document it in the man page.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-08-25 13:13:20 -04:00
Jennifer Herbert
68e5428492 swtpm: Add a chroot option
Add an option to enter a chroot after starting swtpm. This is useful for
sandboxing purposes. When this option is used, it is expected that swtpm
is started as root and the --runas option is used to subsequently drop
privileges (otherwise the chroot could be escaped).

Signed-off-by: Jennifer Herbert <jennifer.herbert@citrix.com>
Signed-off-by: Ross Lagerwall <ross.lagerwall@citrix.com>
2022-08-25 10:04:35 -04:00
Stefan Berger
ef005d9f0b swtpm_setup: Add missing description for --vmid to help screen
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-08-24 10:26:56 -04:00
Stefan Berger
df494163fa swtpm: Check for defined __SNR_MOUNT_setattr and __NR_mount_setattr
Address the following compilation error on Debian:

In file included from /usr/include/seccomp.h:821,
                 from seccomp_profile.c:44:
seccomp_profile.c: In function 'create_seccomp_profile':
seccomp_profile.c:115:9: error: '__NR_mount_setattr' undeclared (first use in this function)
  115 |         SCMP_SYS(mount_setattr),
      |         ^~~~~~~~
seccomp_profile.c:115:9: note: each undeclared identifier is reported only once for each function it appears in
seccomp_profile.c:172:9: error: '__NR_quotactl_fd' undeclared (first use in this function)
  172 |         SCMP_SYS(quotactl_fd),
      |         ^~~~~~~~

We need to do this since they are defined like this:

 #define __SNR_mount_setattr             __NR_mount_setattr
 #define __SNR_quotactl_fd               __NR_quotactl_fd

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-08-22 15:27:17 -04:00
Stefan Berger
98e84b2a85 swtpm: Include opensslv.h to get OPENSSL_VERSION_NUMBER
Include openssl/opensslv.h to avoid the following error on Ubuntu:

fips.c: In function 'fips_mode_enabled':
fips.c:61:16: error: implicit declaration of function 'EVP_default_properties_is_fips_enabled' [-Werror=implicit-function-declaration]
   61 |     int mode = EVP_default_properties_is_fips_enabled(NULL);
      |                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Resolves: https://github.com/stefanberger/libtpms/issues/345
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-08-22 15:27:17 -04:00
Stefan Berger
0ebe80466f swtpm: Refactor existing function to use new tpmlib_get_cmd_ordinal()
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-08-18 09:50:16 -04:00
Stefan Berger
97e910af58 swtpm: Introduce disable-auto-shutdown flag for --flags option
Introduce disable-auto-shutdown flag for the --flags option to disable
the sending of TPM2_Shutdown() if swtpm determines that it needs to send
this command to a TPM 2 before device reset or swtpm program termination.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-08-18 09:50:16 -04:00
Stefan Berger
9b3add22ac swtpm: If necessary send TPM2_Shutdown() before TPMLIB_Terminate()
If necessary send a TPM2_Shutdown() command to libtpms before processing
CMD_INIT. However, this is only necessary for a TPM 2 and only if the
TPM2_Shutdown command has not been sent by the client (VM TPM driver) as
the last command as it should do under normal circumstances, for example
upon graceful VM shutdown.

This fixes a bug where abrupt VM resets may trigger the TPM 2's dictionary
attack lockout logic due to the TPM 2 not having received a TPM2_Shutdown
command before it was reset using CMD_INIT for example. An OS driver is
typically supposed to send a TPM2_Shutdown to the TPM 2 but an abrupt VM
reset prevents it.

There are 3 control commands where this needs to be done since they
call TPMLIB_Terminate():

- CMD_STOP:
   This command is typically called before setting the state blobs of the
   TPM or before configuring the buffer size [QEMU, test cases].

- CMD_INIT:
   This command is called for resetting and initializing the TPM 2.

- CMD_SHUTDOWN:
   This command is called for a graceful shutdown of the TPM 2.

There are no negative side effects to be expected if TPM2_Shutdown()
is sent before any of these. Also, since none of these are sent before
the state of the TPM is marshalled (for migration for example) migrated
state will not have a TPM2_Shutdown() applied to it (accidentally).

Edk2 sends a sequence of TPM2_Shutdown(SU_STATE) + TPM2_GetRandom()
before suspend-to-ram. Upon wake up a CMD_INIT is sent to the TPM to
reset it, which in this case now requires a TPM2_Shutdown(SU_STATE)
to be sent to the TPM 2 so that certain TPM 2 state is available
again upon resume. To avoid invaliding the SU_STATE, first send a
TPM2_Shutdown(SU_STATE) in *all cases* and only if this fails send a
TPM2_Shutdown(SU_CLEAR). This way the internal state is preserved and
the VM (or user) are expected to use TPM2_Startup(SU_CLEAR) when
staring up the TPM 2 and no previous state needs to be resumed.

Note: The VM's firmware is trusted to use SU_CLEAR under normal circum-
stances and SU_STATE upon resume. So it wouldn't restore the state if
it wasn't needed.

Note: The TPM 2 spec describes the command as follows:

"This command is used to prepare the TPM for a power cycle. The
shutdownType parameter indicates how the subsequent TPM2_Startup() will be
processed.[...]
This command saves TPM state but does not change the state other than the
internal indication that the context has been saved. The TPM shall
continue to accept commands. If a subsequent command changes TPM state
saved by this command, then the effect of this command is nullified. The
TPM MAY nullify this command for any subsequent command rather than check
whether the command changed state saved by this command. If this command
is nullified and if no TPM2_Shutdown() occurs before the next
TPM2_Startup(), then the next TPM2_Startup() shall be
TPM2_Startup(CLEAR)."

Buglink: https://bugzilla.redhat.com/show_bug.cgi?id=2087538
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-08-18 09:50:16 -04:00
Stefan Berger
75fbda26f6 swtpm: Track last command processed by the TPM
Track the last command processed by the TPM so we can determine whether
we may need to send a TPM2_Shutdown() before reset of the TPM 2.

Introduce a variable lastCommand to help track the last command that
was sent to the TPM 2.

In relation to deciding whether a TPM2_Shutdown() needs to be sent, the
tracking of the last-sent command is merely an optimization since for
example a VM with EDK2 will send a TPM2_Shutdown() followed by a
TPM2_GetRandom() upon suspend-to-ram, thus indicating that the last
command was TPM2_GetRandom(). However, under most circumstances it helps
to avoid sending an additional TPM2_Shutdown() if the OS TPM driver sent
one already.

When the suspended VM resume swtpm gets a CMD_INIT that requires swtpm
to decide whether a TPM2_Shutdown() needs to be sent and per the last-sent
command it will then send a TPM2_Shutdown(SU_STATE) as in the abrupt
termination case.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-08-18 09:50:16 -04:00
Stefan Berger
132f51d41b swtpm_ioctl: Only close file descriptor if >= 0 (Coverity)
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-08-16 09:00:42 -04:00
Stefan Berger
841b9319e8 swtpm: seccomp: Check for __SNR_xyz rather than __NR_xyz
If seccomp-syscalls.h lags behind the syscall definition of __NR_xyz then
the __SNR_xyz #define is not available. Therefore, switch to check for
__SNR_xyz #define because they are available if __NR_xyz is available.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-08-15 15:44:51 -04:00
Stefan Berger
5e03ed6c32 swtpm: Implement fips_mode_enabled()
Implement fips_mode_enabeld() to check whether FIPS is enabledand
use the new function to check for FIPS mode enablement before
trying to disable it.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-08-15 12:56:43 -04:00
Stefan Berger
412a9067fd swtpm: Rename disable_fips_mode() and move into tpmlib_start()
Rename disable_fips_mode() to fips_mode_disable() amd move into
tpmlib_start() after TPMLIB_MainInit(). Clean up the duplicate
prototype.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-08-15 12:56:43 -04:00
Hans
eaa9f92201 swtpm: Fixed typo fs_mount vs fsmount and removed duplicates.
The project wouldn't compile on my ubuntu 20.04.1 based system with the error message:
```
  CC       libswtpm_libtpms_la-seccomp_profile.lo
In file included from seccomp_profile.c:44:
seccomp_profile.c: In function ‘create_seccomp_profile’:
seccomp_profile.c:105:9: error: ‘__SNR_fs_mount’ undeclared (first use in this function)
  105 |         SCMP_SYS(fs_mount),
      |         ^~~~~~~~
seccomp_profile.c:105:9: note: each undeclared identifier is reported only once for each function it appears in
```

Additionally, there were some duplicates in the profile.

Signed-off-by: Hans Niklas Jacob <hnj@posteo.de>
2022-08-15 11:33:01 -04:00
Stefan Berger
cf1f966c25 swtpm: Add some more recent syscalls to seccomp profile
Add some more recent syscalls to the disallowlist in the seccomp
profile.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-08-12 13:51:34 -04:00
Stefan Berger
c4adfa3e57 swtpm: cuse: Extend usage of FILE_OPS_LOCK to protect a reading thread
Extend usage of the FILE_OPS_LOCK to prevent other threads from reading or
writing commands or doing ioctls while the current thread is reading a
response. This prevents a race condition where ptm_read_offset is set to 0
by a thread writing a new command to the device while the current thread
is reading a response from the device and needs this offset.

Resolves: https://github.com/stefanberger/swtpm/issues/725
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-08-12 08:54:50 -04:00
Stefan Berger
b8c1a0fc88 swtpm_setup: Add missing newline to help screen
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2022-08-10 20:38:59 -04:00
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
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
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
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
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
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
Stefan Berger
00eb426004 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-02-23 09:17:11 -05:00
Stefan Berger
9b42d2d503 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 16:41:53 -05:00
Stefan Berger
9f740868fc 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>
2022-02-16 15:53:56 -05:00
Stefan Berger
48773fe3ea 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>
2022-02-16 15:53:56 -05:00
Stefan Berger
fbbb56b52b 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>
2022-02-16 15:53:56 -05:00
Stefan Berger
49b4b30500 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>
2022-02-16 15:53:56 -05:00
наб
0599e40053 swtpm_setup: fix newline before full stop in swtpm-not-found error
Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
2021-11-26 21:30:51 -05:00
Stefan Berger
6d1a7abbfc 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>
2021-11-22 07:39:28 -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
Stefan Berger
ca3379116b swtpm_setup: Use pidfile filename rather than fd (Cygwni, BSDs)
Use the pidfile filename rather than the fd because Cygwin for example
does not seem to support passed file descriptors and also OpenBSD
does not pass some test cases because of this.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-05 13:50:45 -04:00
Stefan Berger
0b5826e186 swtpm: Use compiler and system dependent header files
The header file endian.h does not exists everywhere, so we have to
use system_dependencies.h to get something similar on all systems.

Some compilers, such as gcc on FreeBSD, are picky about unused
function parameters, so we have to decorate the unused ones
with SWTPM_ATTR_UNUSED.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-05 13:50:45 -04:00
Stefan Berger
0841299317 swtpm: Use sys/mount.h and support block devs only if BLKGETSIZE64 exists
The header file fs/linux.h only exists on Linux but we can also
use sys/mount.h, which also exists on Cygwin and the BSDs.

Only support  block devices if BLKGETSIZE64 is defined.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-05 13:50:45 -04:00
Stefan Reiter
a4555cb887 swtpm_setup: mention dir:// and file:// in man page and usage
Signed-off-by: Stefan Reiter <stefan@pimaker.at>
2021-10-05 11:54:42 -04:00
Stefan Reiter
6f8b8c6268 swtpm_setup: add file backend
Add a second backend for supporting the file:// linear nvram store in
swtpm.

Signed-off-by: Stefan Reiter <stefan@pimaker.at>
2021-10-05 11:54:42 -04:00
Stefan Reiter
81371f665f swtpm_setup: add abstract swtpm_backend_ops with dir:// implementation
Abstract away implementation specific code for handling TPM state in
swtpm_setup. The current code for handling directories is moved to
'swtpm_backend_dir.c'.

Where possible, the input argument is simply passed verbatim as
'backend-uri' to swtpm.

No functional change intended, aside from supporting 'dir://' as
optional prefix. The checks for lock-file accessibility are moved to
check_access(), but that shouldn't affect anything AFAICT.

Signed-off-by: Stefan Reiter <stefan@pimaker.at>
2021-10-05 11:54:42 -04:00
Stefan Reiter
5d9edaf401 swtpm_setup: use mkstemp for pidfile
This avoids issues with state backends where we don't have a convenient
directory for a pidfile available.

Signed-off-by: Stefan Reiter <stefan@pimaker.at>
2021-10-05 11:54:42 -04:00
Stefan Reiter
3da95325ee swtpm_setup: remove redundant delete_state function
...and use delete_swtpm_statefiles instead. This function iterates the
folder instead of just deleting one file, but since it is already called
before the init call guarded here, it can only affect files created by
this run anyway.

Note that delete_state had slightly different return semantics, but it
doesn't matter, as the return value is ignored here anyway (best effort
cleanup).

Signed-off-by: Stefan Reiter <stefan@pimaker.at>
2021-10-05 11:54:42 -04:00
Stefan Reiter
0716f994be swtpm: Add "nvram-backend-linear" capability
Signed-off-by: Stefan Reiter <stefan@pimaker.at>
2021-10-05 09:01:30 -04:00
Stefan Reiter
8394db7126 swtpm: Add "linear file" nvram store backend
Implements a second abstraction layer as an NVRAM storage backend: The
"linear" backend stores data in a simple format that can contain
multiple files (multiple TPM states and numbers) in one linear address
space. This can then be mapped to files or other "block-device-like"
interfaces using nvram_linear_file_ops implementations.

A simple one using mmap is provided with the URI type "file://".

Does not support any locking at the moment, users must ensure exclusive
access themselves.

Signed-off-by: Stefan Reiter <stefan@pimaker.at>
2021-10-05 09:01:30 -04:00
Stefan Berger
d755072372 swtpm: Make fsync-related errors non-fatal (for libvirt using AppArmor)
Only recent libvirt versions have the patch for the AppArmor profile for
libvirt to allow fsync after opening a directory for reading. Rather
than failing hard on the open-directory-for-reading error, log it once
and continue and do not try it again after.

This patch addresses the problems seen on Ubuntu related to an older
version of libvirt without the AppArmor profile update.
- issue #484
- issue #549
- issue #559

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-02 17:12:30 -04:00
Stefan Berger
96b43e4d60 Revert "swtpm: Make fsync() errors non-fatal (for libvirt using AppArmor)"
This reverts commit 04cb07fa6c.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-02 17:12:30 -04:00
Stefan Berger
bc5728a974 swtpm: Make fsync() errors non-fatal (for libvirt using AppArmor)
Only recent libvirt versions have the patch for the AppArmor profile
for libvirt to allow fsync on dir and directory. Rather than failing
hard on this error, log it once and continue and do not try fsync
again after.

This patch addresses the problems seen on Ubuntu related to an older
version of libvirt without the AppArmor profile update.
- issue #484
- issue #549
- issue #559

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-10-01 14:16:32 -04:00
Stefan Berger
500f81ec0f swtpm_setup: Fix errno comparison on end-of-directory (FreeBSD)
FreeBSD may return errno EINVAL beside ENOENT once there are no more
entries in a directory to walk over. It claims that readdir() follows
the getdirentries() return codes, which do include EINVAL but not
ENOENT. But ENOENT is also being used.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-30 15:37:14 -04:00
Stefan Berger
68dea0aba1 swtpm_localca: Fix memory leak on arrays of strings
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-29 21:18:48 -04:00
Stefan Berger
1c3417f70b swtpm_localca: Use return from main rather than exit to resolve ASAN issue
Use return rather than exit() when returning from main so that g_autofree'd
variables can be freed.

This resolves issue #568 that occurs with clang only.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-29 21:18:48 -04:00
Stefan Berger
b30a16ed38 swtpm_setup: Use return from main rather than exit to resolve ASAN issue
Use return rather than exit() when returning from main so that g_autofree'd
variables can be freed.

This resolves issue #568 that occurs with clang only.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-29 21:18:48 -04:00
Stefan Berger
2bf7bd1827 swtpm_localca: Fix a TPM 1.2/TCSD related memory leak
Auto-free the variables used for holding values read from the config
file.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-29 14:57:38 -04:00
Eiichi Tsukata
7b7dcbb8d8 swtpm_setup: Forward --logfile option to swtpm
Whenever swtpm_setup is executed with --logfile option, forward the
option to swtpm (--log file=...). This helps debugging swtpm
initialization issues.

Signed-off-by: Eiichi Tsukata <eiichi.tsukata@nutanix.com>
2021-09-29 12:48:30 -04:00
Eiichi Tsukata
15ff0d967a swtpm: Initialize logging as early as possible
Parse "--log" option earlier to help debugging initialization issues.

Signed-off-by: Eiichi Tsukata <eiichi.tsukata@nutanix.com>
2021-09-29 12:48:30 -04:00
Stefan Berger
68ac6478b4 swtpm_setup: Fix exit code on error to be '1'.
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-28 09:23:28 -04:00
Stefan Berger
2fdb7c307b swtpm: Add size to each type of state and use JSON object
Add the size of the type state to the --print-states output and switch
back to a JSON object when enumerating the blobs.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-28 07:46:38 -04:00
Stefan Berger
f2aa3274b2 swtpm: Search for all state files and use abstracted names in JSON
Search for all the state files not just the permanent state and
when printing the JSON use the abstracted names rather than concrete
filenames that are only valid for the dir backend but will likely
not exist in other backends.

Adjust swtpm_setup to search for the abstracted name and also
adjust the error message to print out the abstracted name.

Adjust the test cases.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-25 10:26:07 -04:00
Stefan Berger
1b2b721e06 swtpm: Implement CheckState interface function for checking for stateblob
Implement a CheckState interface function for checking for the
TPM_PERMANENT_ALL_NAME blog. The dirctory backend does a simple stat on the
file without actually reading it, which otherwise may require the (correct)
key if it was encrypted.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-25 08:52:32 -04:00
Stefan Berger
3d8b87611c swtpm: Remove all interface functions from swtpm_nvstore_dir.h
Remove all interface functions from swtpm_nvstore_dir.h and make them
static in swtpm_nvstore_dir.c. This way we avoid direct calls to these
functions from elsewhere.

Move the declaration of the interface structure into swtpm_nvstore.h
to get rid of swtpm_nvstore_dir.h entirely.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-25 08:52:32 -04:00
Stefan Berger
78137e819f swtpm: Get rid of printf's in swtpm_aes.c
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-22 11:28:37 -04:00
Stefan Berger
6bc55a6752 swtpm: Cleanup storage backend on shutdown to unlock dir
Cleanup on the storage backend side on shutdown to unlock the
locked directory.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-21 21:21:41 -04:00
Stefan Berger
784d9e2194 swptm_ioctl: Eliminate an unreachable statement
If is_chardev is false, then we have a buffersize of 4096 bytes per
the following statement:

322    if (!is_chardev)
323        buffersize = 4096;

With this we end up in the following if branch:

384        if (buffersize) {
385            /* continue with the read interface */
[...]
418
419            break;
420        } else {

Per the break in line 419 we leave the loop, thus cannot reach the
statement we remove:

423        if (!is_chardev)
424            break;

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-21 17:53:14 -04:00
Nick Chevsky
1e2a6d99cd swtpm: Don't set END_ON_HUP flag in CMD_SET_DATAFD handler
Don't set flag MAIN_LOOP_FLAG_END_ON_HUP in the handler for control
channel command CMD_SET_DATAFD, since this caused the swtpm daemon to
unconditionally terminate when the data channel connection dropped.
Removing the flag allows this behavior to be controlled by the user
via the --terminate command-line switch, as it was meant to be.

Signed-off-by: Nick Chevsky <nchevsky@users.noreply.github.com>
2021-09-21 12:11:34 -04:00
Stefan Berger
ca4ee25ce7 swtpm: Use EVP_CIPHER in SWTPM_SymmetricKeyData_Encrypt/Decrypt
Use the EVP_CIPHER implementation for the AES CBC computations. This
API has been supported already in OpenSSL 1.1.x for sure and is also
not deprecated in OpenSSL 3.0.

This now resolves issue #538.
We can build src/swtpm without having to suppress deprecated API warnings.
Since this is the last issue related to OpenSSL deprecated APIs, we don't
need to suppress any OpenSSL 3.0 deprecated API warnings anymore.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-21 11:52:07 -04:00
Stefan Berger
eecb73ebbd swtpm_setup: Use non-deprecated APIs to contruct RSA key (OSSL 3)
Use only non-deprecated APIs to construct RSA key when compiling with
OpenSSL 3.0.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-21 10:32:16 -04:00
Stefan Berger
516acd1d6b swtpm_setup: Move check for pkey = NULL right after creation
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-21 10:32:16 -04:00
Stefan Berger
76a42942a4 build-sys: selinux: Compare absolute path against absolute path
This patch fixes issue #533.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-20 21:20:26 -04:00
Stefan Berger
4c5a410291 swtpm: Add implementation of SWTPM_HMAC using OpenSSL 3.0 APIs
Add an implementation of SWTPM_HMAC using non-deprecated APIs when
compiling with OpenSSL 3.0.

This partially addresses issue #538.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-20 12:08:18 -04:00
Stefan Berger
3b5d849eaf build-sys: Use -DOPENSSL_SUPPRESS_DEPRECATED (OSSL 3)
Use the (undocumented) OPENSSL_SUPPRESS_DEPRECATED to suppress the
deprecated API warnings when compiling swtpm and swtpm_setup with
OpenSSL 3.0.0 replacing the global -Wno-deprecated-declarations.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-09-18 18:57:31 -04:00
Eiichi Tsukata
784e05321f swtpm: Fix SWTPM_NVRAM_PrintJson() error handling logic
Return -1 when SWTPM_NVRAM_GetFilenameForName() or
SWTPM_NVRAM_Init() fails.

Signed-off-by: Eiichi Tsukata <eiichi.tsukata@nutanix.com>
2021-09-09 21:47:21 -04:00
Eiichi Tsukata
89f25f91ce swtpm: Rename SWTPM_NVRAM_Print_Json()
Rename that to SWTPM_NVRAM_PrintJson() to conform the existing function
naming rule.

Signed-off-by: Eiichi Tsukata <eiichi.tsukata@nutanix.com>
2021-09-09 21:47:21 -04:00
Eiichi Tsukata
e0d2c0ed57 swtpm_setup: Use --print-states in check_state_overwrite()
Instead of directly accessing TPM state store file, use --print-states
to check if there is existing state file or not. This can reduce TPM
state directory dependencies of swtpm_setup.

Signed-off-by: Eiichi Tsukata <eiichi.tsukata@nutanix.com>
2021-09-07 09:22:51 -04:00
Eiichi Tsukata
5bc59a74f3 swtpm: Add --print-states for reporting TPM states status
--print-states reports TPM states stored in --tpmstate backend.
This feature is supposed to be used by swtpm_setup for checking TPM state
existence.

Sample output is as follows:

  $ swtpm socket --print-states --tpmstate dir=/tmp --tpm2 | jq .
  {
    "type": "swtpm",
    "states": [
      {
        "name": "tpm2-00.permall"
      }
    ]
  }

Signed-off-by: Eiichi Tsukata <eiichi.tsukata@nutanix.com>
2021-09-07 09:22:51 -04:00
Eiichi Tsukata
7703d4461a swtpm: Add GetFilepathForName()
Introduce GetFilepathForName() and use it to get a rooted state file
path instead of existing GetFilenameForName().
Move GetFilenameForName() to swtpm_nvstore.c so that other TPM state
store backend can use it.

Signed-off-by: Eiichi Tsukata <eiichi.tsukata@nutanix.com>
2021-09-07 09:22:51 -04:00
Eiichi Tsukata
20ca1eb311 swtpm_setup: Use temp dir for certs instead of TPM state dir
Currently swtpm_setup saves ek.cert and platform.cert to TPM state
directory and removes them later. As the usage of these files are
temporary, temp directory can be used for this purpose. This reduces
dependencies on TPM state dir, which is convenient for pluggable state
store (https://github.com/stefanberger/swtpm/issues/461).

Signed-off-by: Eiichi Tsukata <eiichi.tsukata@nutanix.com>
2021-08-18 16:06:20 -04:00
Stefan Berger
7edc44d576 swtpm: Add missing braces around TPM_DEBUG after if statement
Fix the following compilation issue:

swtpm_nvstore_dir.c: In function 'SWTPM_NVRAM_Validate_Dir':
swtpm_nvstore_dir.c:149:86: error: suggest braces around empty body in an 'if' statement [-Werror=empty-body]
         TPM_DEBUG("SWTPM_NVRAM_Validate_Dir: Rooted state path %s\n", tpm_state_path);
                                                                                      ^
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-08-13 17:13:22 -04:00
Stefan Berger
e43946a597 swtpm: Allow sched_setattr in seccomp profile for CUSE TPM
glib's (v2.68) g_thread_pool_new() calls the syscall sched_setattr(),
which we must allow to avoid termination of the CUSE TPM.

This patch resolves issue #520.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-08-13 13:36:39 -04:00
Stefan Berger
8f57999d82 swtpm: Clear keys on heap at termination
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-08-13 10:34:40 -04:00
Eiichi Tsukata
59ad2983fd swtpm: Wipe out key to ensure it is not left on the stack
This change is conforming to the following SEI CERT recommendation.

MEM03-C. Clear sensitive information stored in reusable resources:
https://wiki.sei.cmu.edu/confluence/display/c/MEM03-C.+Clear+sensitive+information+stored+in+reusable+resources

Signed-off-by: Eiichi Tsukata <eiichi.tsukata@nutanix.com>
2021-08-13 07:15:05 -04:00
Eiichi Tsukata
86931b4f30 swtpm: Add "nvram-backend-dir" capability
Append "nvram-backend-dir" to --print-capabilities output.

Signed-off-by: Eiichi Tsukata <eiichi.tsukata@nutanix.com>
2021-08-13 06:35:18 -04:00
Eiichi Tsukata
b82eb7e788 swtpm: Add options for pluggable backend (--tpmstate backend-uri)
Add an options to specify pluggable backend URI.

Ex:
  --tpmstate backend-uri=dir://<path_to_dir>

Backend URI is specific to each backend plugin which points to the
location of the NVRAM files.
Currently, "dir" is the only one available backend. In this case
backend-uri should be a path to the directory where files are stored.

This option is designed to compatible with existing "dir" option.
If "dir" is specified, swtpm prioritize "dir" ignoring "backend-uri".

Signed-off-by: Eiichi Tsukata <eiichi.tsukata@nutanix.com>
2021-08-13 06:35:18 -04:00
Eiichi Tsukata
2d3deaef29 swtpm: Abstract NVRAM interface for pluggable state store
Related GH issue: https://github.com/stefanberger/swtpm/issues/461

This patch adds an abstraction layer to NVRAM state store implementation
for pluggable storage backends. No functional changes are intended in
this change. The default state store backend ("dir backend") keeps
current behavior.

To make swtpm ready for pluggable store, this patch moves file related
operations to the seperate file (swtpm_nvstore_dir.c) and defined the
interface for plugins (nvram_backend_ops in swtpm_nvstore.h). The
interface can be used by each plugin which will be added later.

With the interface, each plugin can access its "backend_uri" which
points to the location of the backend storage, for example S3 bucket
URL or iSCSI URL, and decide how it stores TPM state data.

Signed-off-by: Eiichi Tsukata <eiichi.tsukata@nutanix.com>
2021-08-13 06:35:18 -04:00
Stefan Berger
3eac247752 swtpm_setup: Add support for checking for TPM 1.2 and TPM 2 support
Implement get_supported_tpm_versions to get swtpm's support for TPM 1.2
and TPM 2 and use it error out in case user choose a TPM version that
is not supported. Also display the supported TPM versions in the
capabilites JSON.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-08-10 09:38:48 -04:00
Stefan Berger
afef705061 swtpm_setup: Make error message better when swtpm is not an exectuable
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2021-08-10 09:38:48 -04:00