Commit Graph

1700 Commits

Author SHA1 Message Date
Stefan Berger
e327269b1f CHANGES: Add documentation for changes in 0.9.0
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-06-17 09:03:06 -04:00
Stefan Berger
303d9cfdb5 swtpm: Use umask() to create/truncated state file rather than fchmod()
Since swtpm does not use concurrency while writing state files, set
the process umask to 0 when the state file is created or truncated for
writing and the user requested specific mode bits. This avoids an
additional syscall in the possibly timing critical path when processing
a TPM command and having to write the state.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-06-13 13:53:36 -04:00
Stefan Berger
c2e524dbc7 swtpm: Use fchmod to set mode bits provided by user
The mode bits that the user provided were only applied with open() and were
subject to masking with the value of current umask. When umask was set to
0027 the test case test_commandline was failing because the mode bits on
the create TPM state file were not the expected ones (masked by umask).
Therefore, set the mode bits using fchmod if the user provided them,
otherwise do not set them. This way the mode bits will be set to the values
the user requested.

Currently the directory storage backend was setting the mode bits to the
default value (0640) *after* opening the TPM state file. Now, if the user
did not provide any mode bits then the mode bits will be set so that the
file can be written to as owner. This ensures that at least mode bits 0600
are set by default. However, if the user provided mode bit flags then these
will be used without modification.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-06-12 14:23:54 -04:00
Stefan Berger
85a3a14a41 swtpm: Replace mkstemp with g_mkstemp_full (Coverity)
Replace mkstemp with g_mkstemp_full and pass parameters that lead to
the same mode bits and file opening flags and mkstemp had. This addresses
a Coverity complaint regarding missing application of umask before
mkstemp.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-06-11 12:02:28 -04:00
Stefan Berger
3d620d0dd5 debian: Sync AppArmor profile with what is used by Ubuntu
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-06-11 11:03:03 -04:00
Stefan Berger
b1b9a6a4ab tests: Add test cases for using swtpm --print-states while swtpm is running
swtpm <0.8 tried to lock the .lock file when executing --print-states,
which then failed when another swtpm was holding the lock. This adds
a test case for this scenario.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-06-11 08:43:01 -04:00
Stefan Berger
f7bfb5d8dc tests: Fix 3 occurrences of stray '\' before '-'
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-06-10 19:40:31 -04:00
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
d258668d51 build-sys: Add support for --disable-tests to disable tests
Add support for --disable-tests to disable the tests and the checking
for tools that only need to be installed for the tests. Keep the tests
enabled as default as it was before.

Resolves: https://github.com/stefanberger/swtpm/issues/843
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-03-22 11:18:05 -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
2534112322 tests: Rearrange order of test cases to run some also as 'root'
Some test cases can be run as root, so rearrange the order of the
test cases so that not all of them are skipped if the test case
runs as 'root'.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-03-19 14:28:01 -04:00
Stefan Berger
529988f1e4 tests: Add tests for command line options and combinations of options
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-03-14 12:09:20 -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
a672b2dc03 headers: Exclude TPM ioctls definitions for the GNU/Hurd
Follow changes in the QEMU codebase supporting GNU/Hurd.

Reference: https://lists.nongnu.org/archive/html/qemu-devel/2024-01/msg03702.html

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-02-15 10:07:19 -05:00
Stefan Berger
a4347aff21 tests: Remove unnecessary '\' before '/'
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-01-08 15:04:38 -05:00
Stefan Berger
06b1b1f9ff tests: Add softhsm_setup to shellcheck'ed files and fix issues
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-11-22 07:16:44 -05: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
63aae4d2a4 build-sys: Display GMP_LIBS and GMP_CFLAGS
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-29 18:59:13 -04:00
Stefan Berger
a61c08a4c7 build-sys: Only display warning if pkg-config for gmp fails
Since many older gmp development packages do not provide the pkg-config
file for gmp, just display a warning and fall back to default values.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-28 12:19:42 -04:00
Stefan Berger
3068b48d18 tests: Add missing 'exit 1' on unexpected file size on --reconfigure
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-26 17:19:29 -04:00
Stefan Berger
116953fb1d tests: Add test cases for swtpm_cert with max serial number
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-26 17:19:29 -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
605e0ce880 build-sys/rpm/debian: Add gmp library and devel package as dependency
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
efa030b9da tests: reformat regexs for easier readability and extension
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-19 09:18:09 -04:00
Stefan Berger
673ee421d2 rpm: Set BSD-3-Clause for the License on each subpackage
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-16 16:29:05 -04:00
Stefan Berger
b9e6d7c686 rpm: Split off SELinux files to build an selinux package
Follow the changes in Fedora to build a separate swtpm-selinux package
so that swtpm can also be installed without SELinux on the system.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2023-08-15 12:36:31 -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
Lena Voytek
f732f489c2 debian: Allow apparmor access to qemu session bus swtpm files
When running a VM connected to qemu:///session, swtpm needs access to its own
process id and socket files in /run/user/<UID>/libvirt/qemu/run/swtpm/. Add
permissions to access them in the apparmor profile.

Signed-off-by: Lena Voytek <lena.voytek@canonical.com>
2023-08-10 20:03:06 -04:00