Commit Graph

1523 Commits

Author SHA1 Message Date
Stefan Berger
1f9bb04869 tests: Retry NVWrite command after 0x922 return code and inc lockout counter
When returncode 0x922 is received from NVWrite then retry the command so
that it gets the expected error code from failing to provide a password.
When checking the lockout counter, increase the numbers now.

Patched versions of libtpms may not return 0x922 anymore, so write the code
that it can test both cases.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-05-13 08:56:31 -04:00
Stefan Berger
f813ad5064 tests: Extend regex to allow for optional RSA-4096 keys
libtpms v0.11 will support RSA-4096 keys. Adjust the test case
regex for optional output of 'tpm2-rsa-keysize-4096'.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2025-05-13 08:56:31 -04:00
Stefan Berger
29d533fbfe CHANGES: Add a note about OPENSSL_ENABLE_SHA1_SIGNATURES=1 for swtpm
Users should use swtpm >= 0.10.0, since it handles SHA1 signatures
better. This version needs users to set OPENSSL_ENABLE_SHA1_SIGNATURES=1
for swtpm to be able to sign a SHA1.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2025-05-06 19:21:50 -04:00
Stefan Berger
202290745d tests: Set OPENSSL_ENABLE_SHA1_SIGNATURES=1 on swtpm in IBMTSS2 test
To keep the IBMTSS2 test running, set OPENSSL_ENABLE_SHA1_SIGNATURES=1
on swtpm so that it is allowed to sign a SHA1 and does not create
and error.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2025-05-06 19:21:50 -04:00
Stefan Berger
5d0667e876 tests: Set OPENSSL_ENABLE_SHA1_SIGNATURES=1 for IBMTSS2 test
The IBMTSS2 tests suite creates signatures over SHA1 that may now fail on
RHEL 9.x and CentOS 9. To have these tests succeed set
OPENSSL_ENABLE_SHA1_SIGNATURES=1 so the tests do not need to be modified
and also check that the TPM 2 can handle SHA1 signatures as before.
'swtpm socket --tpm2' should set this environment variable automatically
if needed.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-05-06 19:21:50 -04:00
Stefan Berger
8aba7a9cf6 tests: Remove unnecessary include of openssl/engine.h
To keep the older version of the IBM TSS2 test suite working, remove the
unnecessary include of openssl/engine.h to allow it to compile with more
recent versions of OpenSSL where this header file is missing.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-05-06 19:21:50 -04:00
Stefan Berger
4b27cabead ci: Update from ubuntu-20.04 to ubuntu-24.04 due to EOL
Github actions does not run ubuntu-20.04 anymore due to EOL. Update the
20.04 entries to use 24.04.

cpp-coveralls needs an older version of python3 due to pkgutil.ImpImporter
having disappeared in more recent python versions. Therefore, leave
test-coveralls at 22.04.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-04-16 17:02:50 -04:00
Stefan Berger
7fdd7273c6 swtpm.spec: Apply previous changes also to swtpm.spec.in
Suggested-by: Ajeeth Adithya <ajeeth.adithya@nutanix.com>
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-04-11 18:21:07 -04:00
Ajeeth Adithya
7c5239efd4 swtpm.spec: Change the order of uninstallation of the SELinux modules
Reverse the order of uninstallation of the ‘swtpm’ and ‘swtpm_svirt’
selinux modules. The current order fails because 'swtpm-svirt' module
has a dependency on the 'swtpm' module. This results in the ‘swtpm'
module not being cleaned up during %postun:

$ semodule -l | grep swtpm
swtpm
swtpm_svirt

$ semodule -n -X 200 -s targeted -r swtpm
libsemanage.semanage_direct_remove_key: Removing last swtpm module (no other swtpm module exists at another priority).
Failed to resolve typeattributeset statement at /var/lib/selinux/targeted/tmp/modules/200/swtpm_svirt/cil:4
/sbin/semodule:  Failed!

$ sudo semodule -n -X 200 -s targeted -r swtpm_svirt
libsemanage.semanage_direct_remove_key: Removing last swtpm_svirt module (no other swtpm_svirt module exists at another priority).

$ semodule -l | grep swtpm
swtpm

Signed-off-by: Ajeeth Adithya <ajeeth.adithya@nutanix.com>
2025-04-09 18:05:53 -04:00
Ajeeth Adithya
fed506fa39 swtpm.spec: Back up the default file contexts
Add the %selinux_relabel_pre macro in the %pre section to back up the
current file contexts lists. This is required since %selinux_relabel_post
macro in the %posttrans section uses the backup to revert to the original contexts.

Signed-off-by: Ajeeth Adithya <ajeeth.adithya@nutanix.com>
2025-04-09 18:05:53 -04:00
Stefan Berger
8ee612a48c selinux: Add rule to allow swtpm_t opening of virt_log_t files (BZ 2278123)
Link: https://bugzilla.redhat.com/show_bug.cgi?id=2278123#c40
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-02-24 09:22:08 -05:00
Stefan Berger
c95ab58783 ci: Run apt-get update
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2025-02-24 09:22:08 -05:00
Marc-André Lureau
762c151408 SELinux: add NFS permissions for swtpm_t
swtpm fails with a NFS mount. `setsebool virt_use_nfs on` should fix it.

Resolves: https://issues.redhat.com/browse/RHEL-73809

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2025-01-20 11:23:10 -05:00
Marc-André Lureau
0373c88cd6 SELinux: allow to map state file
Specify vtpm state to a file instead of dir:

<tpm model="tpm-crb">
  <backend type="emulator" version="2.0">
    <source type="file" path="/var/lib/libvirt/swtpm/mytest/mytpm2-00.permall"/>
  </backend>
</tpm>

$ virsh start avocado-vt-vm1
error: Failed to start domain 'avocado-vt-vm1'
error: internal error: Could not run '/usr/bin/swtpm_setup'. exitstatus: 1; Check error log '/var/log/swtpm/libvirt/qemu/avocado-vt-vm1-swtpm.log' for details.

$ cat /var/log/swtpm/libvirt/qemu/avocado-vt-vm1-swtpm.log
SWTPM_NVRAM_LinearFile_Mmap: Could not mmap file: Permission denied
/usr/bin/swtpm exit with status 256:

$ ausearch -m avc
----
time->Thu Dec 12 08:43:07 2024
type=PROCTITLE msg=audit(1734010987.020:1455): proctitle=2F7573722F62696E2F737774706D00736F636B6574002D2D7072696E742D737461746573002D2D74706D7374617465006261636B656E642D7572693D66696C653A2F2F2F7661722F6C69622F6C6962766972742F737774706D2F6D79746573742F6D7974706D322D30302E7065726D616C6C002D2D74706D32002D2D6C6F67
type=SYSCALL msg=audit(1734010987.020:1455): arch=c000003e syscall=9 success=no exit=-13 a0=0 a1=c0 a2=3 a3=1 items=0 ppid=22547 pid=22549 auid=4294967295 uid=59 gid=59 euid=59 suid=59 fsuid=59 egid=59 sgid=59 fsgid=59 tty=(none) ses=4294967295 comm="swtpm" exe="/usr/bin/swtpm" subj=system_u:system_r:swtpm_t:s0 key=(null)
type=AVC msg=audit(1734010987.020:1455): avc:  denied  { map } for
pid=22549 comm="swtpm"
path="/var/lib/libvirt/swtpm/mytest/mytpm2-00.permall" dev="dm-0"
ino=202744025 scontext=system_u:system_r:swtpm_t:s0
tcontext=system_u:object_r:virt_var_lib_t:s0 tclass=file permissive=0

Resolves: https://issues.redhat.com/browse/RHEL-70835

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2025-01-20 11:23:10 -05:00
Stefan Berger
7483a83bf9 SELinux: Add rule for swtpm to be able to read password from pipe
Link: https://bugzilla.redhat.com/show_bug.cgi?id=2334271
Resolves: https://github.com/stefanberger/swtpm/issues/964
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
2024-12-26 18:28:16 -05:00
Stefan Berger
ae8e57e032 ci: Add github actions to replace Travis
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-12-26 18:28:16 -05:00
Stefan Berger
50f441a561 Travis: Downgrade to setuptools 59.6.0 to avoid error in 71.x
There seems to be a well known error in setuptools 71.x that prevents
installation of cpp-coveralls on Travis now:

File "/usr/local/lib/python3.10/dist-packages/setuptools/_core_metadata.py", line 285, in _distribution_fullname

    canonicalize_version(version, strip_trailing_zero=False),

TypeError: canonicalize_version() got an unexpected keyword argument 'strip_trailing_zero'

Fall back to the default version that is used in Ubuntu Jammy (59.6.0)
since later versions also lead to the same error.

Link: https://github.com/pypa/setuptools/issues/4483
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-12-02 19:29:51 -05:00
Stefan Berger
a32ff85539 selinux: Add rule for logging to svirt_image_t labeled files from swtpm_t
I was able to recreate the issue on one of my machines where swtpm_t
was trying to append to the log labeled with svirt_image_t. On another
machine this combination of labels does not seem to cause a problem.

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=2306817
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-10-01 09:19:45 -04:00
Stefan Berger
56572341db swtpm: Return TPM_FAIL from invalid header version case
Return TPM_FAIL in case of an invalid header version number to avoid a
potential segmentation fault when accessing an unavailable buffer due to
the header parser not returning an error code.

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

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-25 07:22:50 -04:00
Stefan Berger
4b4e1b21bc build-sys: Build swtpm-0.9.1
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-09-25 07:22:50 -04:00
Stefan Berger
f756ee8a28 tests: Use regex to compare --print-states sizes output
Older versions of libtpms produced smaller initial state files.
Therefore, use a regular expression to compare the sizes.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-06-17 12:49:14 -04:00
Stefan Berger
76b58d7e09 debian/rpm: Adjust changelog for 0.9.0 release
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
2024-06-17 09:03:06 -04:00
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